-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[LoopSimplifyCFG] Added the judgment whether the loop is in LoopSimplifyForm #112845
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
base: main
Are you sure you want to change the base?
Conversation
…ifyForm We can't add a single preheader for loop when any preheader of loop ends with a indirectbranch inst. We will get nullptr in function handleDeadExits when L.getLoopPreheader().
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-transforms Author: bwmaples (bwmaples) ChangesWe can't add a single preheader for loop when any preheader of loop ends with a indirectbranch inst. Full diff: https://github.com/llvm/llvm-project/pull/112845.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp b/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
index ae9103d0608a11..2b0885f2686994 100644
--- a/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
@@ -588,6 +588,15 @@ class ConstantTerminatorFoldingImpl {
return false;
}
+ // we can't handle the case when the loop isn't in LoopSimplifyForm.
+ // eg: indirectbranch
+ if (!DeadExitBlocks.empty() && !L.isLoopSimplifyForm()) {
+ LLVM_DEBUG(dbgs() << "Give up constant terminator folding in loop "
+ << Header->getName()
+ << ": loop isn't in SimplifyForm.\n");
+ return false;
+ }
+
SE.forgetTopmostLoop(&L);
// Dump analysis results.
LLVM_DEBUG(dump());
diff --git a/llvm/test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll b/llvm/test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll
index 95667ebcc9fb24..aac12ee1c70e85 100644
--- a/llvm/test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll
+++ b/llvm/test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll
@@ -280,6 +280,72 @@ exit:
ret i32 %i.1
}
+; Check that we can handle indirectbranch.
+define i32 @dead_exit_test_indirectbranch_loop(i32 %end) {
+; CHECK-LABEL: @dead_exit_test_indirectbranch_loop(
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[COND:%.*]] = icmp slt i32 10, [[END:%.*]]
+; CHECK-NEXT: br i1 [[COND]], label [[PREBB1:%.*]], label [[PREBB2:%.*]]
+; CHECK: preBB1:
+; CHECK-NEXT: indirectbr ptr blockaddress(@dead_exit_test_indirectbranch_loop, [[HEADER:%.*]]), [label [[HEADER]], label %exit]
+; CHECK: preBB2:
+; CHECK-NEXT: indirectbr ptr blockaddress(@dead_exit_test_indirectbranch_loop, [[EXIT:%.*]]), [label [[HEADER]], label %exit]
+; CHECK: header:
+; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[PREBB1]] ], [ 1, [[PREBB2]] ], [ [[I_INC:%.*]], [[BACKEDGE:%.*]] ]
+; CHECK-NEXT: br i1 true, label [[BACKEDGE]], label [[DEAD:%.*]]
+; CHECK: dead:
+; CHECK-NEXT: [[I_LCSSA:%.*]] = phi i32 [ [[I]], [[HEADER]] ]
+; CHECK-NEXT: br label [[DUMMY:%.*]]
+; CHECK: dummy:
+; CHECK-NEXT: br label [[LOOP_EXIT:%.*]]
+; CHECK: backedge:
+; CHECK-NEXT: [[I_INC]] = add i32 [[I]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[I_INC]], [[END]]
+; CHECK-NEXT: br i1 [[CMP]], label [[HEADER]], label [[LOOP_EXIT_LOOPEXIT:%.*]]
+; CHECK: loop.exit.loopexit:
+; CHECK-NEXT: [[I_INC_LCSSA:%.*]] = phi i32 [ [[I_INC]], [[BACKEDGE]] ]
+; CHECK-NEXT: br label [[LOOP_EXIT]]
+; CHECK: loop.exit:
+; CHECK-NEXT: [[I_1:%.*]] = phi i32 [ [[I_LCSSA]], [[DUMMY]] ], [ [[I_INC_LCSSA]], [[LOOP_EXIT_LOOPEXIT]] ]
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: exit:
+; CHECK-NEXT: [[RET:%.*]] = phi i32 [ [[I_1]], [[LOOP_EXIT]] ], [ -1, [[PREBB1]] ], [ -1, [[PREBB2]] ]
+; CHECK-NEXT: ret i32 [[RET]]
+;
+start:
+ %cond = icmp slt i32 10, %end
+ br i1 %cond, label %preBB1, label %preBB2
+
+preBB1:
+ indirectbr ptr blockaddress(@dead_exit_test_indirectbranch_loop, %header), [label %header, label %exit]
+
+preBB2:
+ indirectbr ptr blockaddress(@dead_exit_test_indirectbranch_loop, %exit), [label %header, label %exit]
+
+header:
+ %i = phi i32 [0, %preBB1], [1, %preBB2], [%i.inc, %backedge]
+ br i1 true, label %backedge, label %dead
+
+dead:
+ br label %dummy
+
+dummy:
+ br label %loop.exit
+
+backedge:
+ %i.inc = add i32 %i, 1
+ %cmp = icmp slt i32 %i.inc, %end
+ br i1 %cmp, label %header, label %loop.exit
+
+loop.exit:
+ %i.1 = phi i32 [%i.inc, %backedge], [%i, %dummy]
+ br label %exit
+
+exit:
+ %ret = phi i32 [%i.1, %loop.exit], [-1, %preBB1], [-1, %preBB2]
+ ret i32 %ret
+}
+
; Check that we preserve static reachibility of a dead exit block while deleting
; a switch.
define i32 @dead_exit_test_switch_loop(i32 %end) {
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't check for loop simplify form -- instead check the specific property that is needed to make the transform legal.
Oh, I'm sorry to reply late. Is this the right way to fix this bug.
|
We can't add a single preheader for loop when any preheader of loop ends with a indirectbranch inst.
We will get nullptr in function handleDeadExits when L.getLoopPreheader().