Henry Jiang 68b7cba2b0
[LoopIdiom] Update strlen idiom body loop condition to be clean up by LoopDeletion (#134906)
Fixes the case where subsequent passes were unable to find and delete
the invariant loop left over by the strlen idiom conversion. Since
`loop-deletion` only operate on computable loops, we can update the loop
condition to something more easily picked up by `loop-deletion`

As pointed out in https://github.com/llvm/llvm-project/issues/134736
2025-04-11 12:55:45 -04:00

44 lines
2.1 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -S -passes=loop-idiom,loop-deletion | FileCheck %s
define [2 x i64] @convert_null_terminated_to_bounded_ptr(ptr noundef %p) {
; CHECK-LABEL: define [2 x i64] @convert_null_terminated_to_bounded_ptr(
; CHECK-SAME: ptr noundef [[P:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[WCSLEN:%.*]] = call i64 @wcslen(ptr [[P]])
; CHECK-NEXT: br label %[[TERMINATED_BY_LOOP_END:.*]]
; CHECK: [[TERMINATED_BY_LOOP_END]]:
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds i32, ptr [[P]], i64 [[WCSLEN]]
; CHECK-NEXT: [[TERMINATED_BY_UPPER:%.*]] = getelementptr i8, ptr [[TMP0]], i64 4
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[P]] to i64
; CHECK-NEXT: [[DOTFCA_0_INSERT:%.*]] = insertvalue [2 x i64] poison, i64 [[TMP1]], 0
; CHECK-NEXT: [[TMP2:%.*]] = ptrtoint ptr [[TERMINATED_BY_UPPER]] to i64
; CHECK-NEXT: [[DOTFCA_1_INSERT:%.*]] = insertvalue [2 x i64] [[DOTFCA_0_INSERT]], i64 [[TMP2]], 1
; CHECK-NEXT: ret [2 x i64] [[DOTFCA_1_INSERT]]
;
entry:
br label %terminated_by.loop_cond
terminated_by.loop_cond: ; preds = %terminated_by.loop_cond, %entry
%terminated_by.len.0 = phi i64 [ 0, %entry ], [ %terminated_by.new_len, %terminated_by.loop_cond ]
%0 = getelementptr inbounds i32, ptr %p, i64 %terminated_by.len.0
%terminated_by.elem = load i32, ptr %0, align 4
%terminted_by.check_terminator = icmp eq i32 %terminated_by.elem, 0
%terminated_by.new_len = add i64 %terminated_by.len.0, 1
br i1 %terminted_by.check_terminator, label %terminated_by.loop_end, label %terminated_by.loop_cond
terminated_by.loop_end: ; preds = %terminated_by.loop_cond
%1 = getelementptr inbounds i32, ptr %p, i64 %terminated_by.len.0
%terminated_by.upper = getelementptr i8, ptr %1, i64 4
%2 = ptrtoint ptr %p to i64
%.fca.0.insert = insertvalue [2 x i64] poison, i64 %2, 0
%3 = ptrtoint ptr %terminated_by.upper to i64
%.fca.1.insert = insertvalue [2 x i64] %.fca.0.insert, i64 %3, 1
ret [2 x i64] %.fca.1.insert
}
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"wchar_size", i32 4}