Skip to content

Conversation

bwmaples
Copy link

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().

…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().
Copy link

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 @ followed by their GitHub username.

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.

@llvmbot
Copy link
Member

llvmbot commented Oct 18, 2024

@llvm/pr-subscribers-llvm-transforms

Author: bwmaples (bwmaples)

Changes

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().


Full diff: https://github.com/llvm/llvm-project/pull/112845.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp (+9)
  • (modified) llvm/test/Transforms/LoopSimplifyCFG/constant-fold-branch.ll (+66)
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) {

Copy link
Contributor

@nikic nikic left a 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.

@bwmaples
Copy link
Author

Oh, I'm sorry to reply late. Is this the right way to fix this bug.

You shouldn't check for loop simplify form -- instead check the specific property that is needed to make the transform legal.

@bwmaples bwmaples requested a review from nikic October 22, 2024 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants