Skip to content

[LoopIdiom] Update strlen idiom body loop condition to be clean up by LoopDeletion #134906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,11 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
return false;

BasicBlock *Preheader = CurLoop->getLoopPreheader();
BasicBlock *LoopBody = *CurLoop->block_begin();
BasicBlock *LoopExitBB = CurLoop->getExitBlock();
BranchInst *LoopTerm = dyn_cast<BranchInst>(LoopBody->getTerminator());
assert(Preheader && LoopBody && LoopExitBB && LoopTerm &&
"Should be verified to be valid by StrlenVerifier");

if (Verifier.OpWidth == 8) {
if (DisableLIRP::Strlen)
Expand Down Expand Up @@ -1804,6 +1808,17 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
// up by later passes
for (PHINode *PN : Cleanup)
RecursivelyDeleteDeadPHINode(PN);

// LoopDeletion only delete invariant loops with known trip-count. We can
// update the condition so it will reliablely delete the invariant loop
assert(LoopTerm->getNumSuccessors() == 2 &&
(LoopTerm->getSuccessor(0) == LoopBody ||
LoopTerm->getSuccessor(1) == LoopBody) &&
"loop body must have a successor that is it self");
ConstantInt *NewLoopCond = LoopTerm->getSuccessor(0) == LoopBody
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a comment explaining that this intends to make the old code unreachable/dead.

Add assert that at least one of the successors is LoopBody?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a look. I added the asserts and comments.

I will take a look to see if there's any further testing required.

? Builder.getFalse()
: Builder.getTrue();
LoopTerm->setCondition(NewLoopCond);
SE->forgetLoop(CurLoop);

++NumStrLen;
Expand Down
43 changes: 43 additions & 0 deletions llvm/test/Transforms/LoopIdiom/strlen-cleanup.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
; 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}

12 changes: 6 additions & 6 deletions llvm/test/Transforms/LoopIdiom/strlen.ll
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ define i64 @valid_basic_strlen(ptr %str) {
; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[STR_ADDR_0]], align 1
; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp eq i8 [[TMP0]], 0
; CHECK-NEXT: [[INCDEC_PTR]] = getelementptr i8, ptr [[STR_ADDR_0]], i64 1
; CHECK-NEXT: br i1 [[CMP_NOT]], label %[[WHILE_END:.*]], label %[[WHILE_COND]]
; CHECK-NEXT: br i1 true, label %[[WHILE_END:.*]], label %[[WHILE_COND]]
; CHECK: [[WHILE_END]]:
; CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[SCEVGEP]] to i64
; CHECK-NEXT: [[SUB_PTR_RHS_CAST:%.*]] = ptrtoint ptr [[STR]] to i64
Expand Down Expand Up @@ -74,7 +74,7 @@ define i32 @valid_basic_strlen_rotated(ptr %str) {
; CHECK-NEXT: [[INCDEC_PTR]] = getelementptr inbounds nuw i8, ptr [[STR_ADDR_0]], i64 1
; CHECK-NEXT: [[TMP2:%.*]] = load i8, ptr [[INCDEC_PTR]], align 1
; CHECK-NEXT: [[TOBOOL1_NOT:%.*]] = icmp eq i8 [[TMP2]], 0
; CHECK-NEXT: br i1 [[TOBOOL1_NOT]], label %[[DO_END:.*]], label %[[DO_BODY]]
; CHECK-NEXT: br i1 true, label %[[DO_END:.*]], label %[[DO_BODY]]
; CHECK: [[DO_END]]:
; CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[SCEVGEP1]] to i64
; CHECK-NEXT: [[SUB_PTR_RHS_CAST:%.*]] = ptrtoint ptr [[STR]] to i64
Expand Down Expand Up @@ -163,7 +163,7 @@ define dso_local void @valid_strlen_with_aux_indvar(ptr noundef %str, ptr nounde
; CHECK-NEXT: [[INCDEC_PTR2]] = getelementptr inbounds nuw i8, ptr [[FOO_ADDR_011]], i64 1
; CHECK-NEXT: [[TMP10:%.*]] = load i8, ptr [[INCDEC_PTR]], align 1
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i8 [[TMP10]], 0
; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label %[[WHILE_END_LOOPEXIT:.*]], label %[[WHILE_BODY]]
; CHECK-NEXT: br i1 true, label %[[WHILE_END_LOOPEXIT:.*]], label %[[WHILE_BODY]]
; CHECK: [[WHILE_END_LOOPEXIT]]:
; CHECK-NEXT: br label %[[WHILE_END]]
; CHECK: [[WHILE_END]]:
Expand Down Expand Up @@ -232,7 +232,7 @@ define i32 @valid_strlen_index(ptr %str) {
; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[ARRAYIDX]], align 1
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i8 [[TMP0]], 0
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label %[[WHILE_END:.*]], label %[[WHILE_COND]]
; CHECK-NEXT: br i1 true, label %[[WHILE_END:.*]], label %[[WHILE_COND]]
; CHECK: [[WHILE_END]]:
; CHECK-NEXT: [[TMP1:%.*]] = trunc nuw nsw i64 [[STRLEN]] to i32
; CHECK-NEXT: ret i32 [[TMP1]]
Expand Down Expand Up @@ -290,7 +290,7 @@ define dso_local void @valid_strlen_offset(ptr noundef %str) local_unnamed_addr
; CHECK-NEXT: [[TMP4:%.*]] = load i8, ptr [[STR_ADDR_0]], align 1
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i8 [[TMP4]], 0
; CHECK-NEXT: [[INCDEC_PTR14]] = getelementptr inbounds nuw i8, ptr [[STR_ADDR_0]], i64 1
; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label %[[WHILE_END:.*]], label %[[WHILE_COND]]
; CHECK-NEXT: br i1 true, label %[[WHILE_END:.*]], label %[[WHILE_COND]]
; CHECK: [[WHILE_END]]:
; CHECK-NEXT: tail call void @use(ptr noundef nonnull [[SCEVGEP]])
; CHECK-NEXT: br label %[[RETURN]]
Expand Down Expand Up @@ -377,7 +377,7 @@ define void @valid_nested_idiom(ptr %strs, i32 %n) {
; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[COUNT_08]], 1
; CHECK-NEXT: [[TMP4:%.*]] = load i8, ptr [[INCDEC_PTR]], align 1
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i8 [[TMP4]], 0
; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label %[[WHILE_END_LOOPEXIT:.*]], label %[[WHILE_BODY]]
; CHECK-NEXT: br i1 true, label %[[WHILE_END_LOOPEXIT:.*]], label %[[WHILE_BODY]]
; CHECK: [[WHILE_END_LOOPEXIT]]:
; CHECK-NEXT: br label %[[WHILE_END]]
; CHECK: [[WHILE_END]]:
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LoopIdiom/wcslen16.ll
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ define i64 @valid_strlen16(ptr %src) {
; CHECK-NEXT: [[CURR_0]] = getelementptr inbounds i8, ptr [[SRC_PN]], i64 2
; CHECK-NEXT: [[TMP3:%.*]] = load i16, ptr [[CURR_0]], align 2
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i16 [[TMP3]], 0
; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label %[[WHILE_END:.*]], label %[[WHILE_COND]]
; CHECK-NEXT: br i1 true, label %[[WHILE_END:.*]], label %[[WHILE_COND]]
; CHECK: [[WHILE_END]]:
; CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[END]] to i64
; CHECK-NEXT: [[SUB_PTR_RHS_CAST:%.*]] = ptrtoint ptr [[SRC]] to i64
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LoopIdiom/wcslen32.ll
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ define i64 @valid_wcslen32(ptr %src) {
; CHECK-NEXT: [[CURR_0]] = getelementptr inbounds i8, ptr [[SRC_PN]], i64 4
; CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr [[CURR_0]], align 4
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[TMP3]], 0
; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label %[[WHILE_END:.*]], label %[[WHILE_COND]]
; CHECK-NEXT: br i1 true, label %[[WHILE_END:.*]], label %[[WHILE_COND]]
; CHECK: [[WHILE_END]]:
; CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[END]] to i64
; CHECK-NEXT: [[SUB_PTR_RHS_CAST:%.*]] = ptrtoint ptr [[SRC]] to i64
Expand Down