[llvm] 413e47e - [ConstraintElimination] Handle degenerate case with branch to same dest.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 3 03:09:34 PST 2022


Author: Florian Hahn
Date: 2022-02-03T11:09:14Z
New Revision: 413e47ecd4476aec29699bcbd8425461a8821c80

URL: https://github.com/llvm/llvm-project/commit/413e47ecd4476aec29699bcbd8425461a8821c80
DIFF: https://github.com/llvm/llvm-project/commit/413e47ecd4476aec29699bcbd8425461a8821c80.diff

LOG: [ConstraintElimination] Handle degenerate case with branch to same dest.

When a conditional branch has the same block as both true and false
successor it is not safe to add the condition.

Fixes PR49819.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
    llvm/test/Transforms/ConstraintElimination/dom.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 13963657d1830..063d91b89e7cf 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -343,9 +343,12 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
     // Succ (e.g. the case when adding a condition from a pre-header to a loop
     // header).
     auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
-      return all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) {
-        return Pred == &BB || DT.dominates(Succ, Pred);
-      });
+      assert(isa<BranchInst>(BB.getTerminator()));
+      return any_of(successors(&BB),
+                    [Succ](const BasicBlock *S) { return S != Succ; }) &&
+             all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) {
+               return Pred == &BB || DT.dominates(Succ, Pred);
+             });
     };
     // If the condition is an OR of 2 compares and the false successor only has
     // the current block as predecessor, queue both negated conditions for the

diff  --git a/llvm/test/Transforms/ConstraintElimination/dom.ll b/llvm/test/Transforms/ConstraintElimination/dom.ll
index 1b161eb5c1ee1..9cf84ca4efede 100644
--- a/llvm/test/Transforms/ConstraintElimination/dom.ll
+++ b/llvm/test/Transforms/ConstraintElimination/dom.ll
@@ -540,7 +540,7 @@ define i1 @both_branch_to_same_block(i4 %x) {
 ; CHECK:       exit:
 ; CHECK-NEXT:    [[C_2:%.*]] = icmp ne i4 [[X]], 0
 ; CHECK-NEXT:    [[C_3:%.*]] = icmp eq i4 [[X]], 0
-; CHECK-NEXT:    [[RES:%.*]] = xor i1 true, true
+; CHECK-NEXT:    [[RES:%.*]] = xor i1 [[C_2]], [[C_3]]
 ; CHECK-NEXT:    ret i1 [[RES]]
 ;
 entry:
@@ -564,7 +564,7 @@ define i1 @both_branch_to_same_block_and(i4 %x, i4 %y) {
 ; CHECK:       exit:
 ; CHECK-NEXT:    [[C_3:%.*]] = icmp ne i4 [[X]], 0
 ; CHECK-NEXT:    [[C_4:%.*]] = icmp eq i4 [[X]], 0
-; CHECK-NEXT:    [[RES:%.*]] = xor i1 true, false
+; CHECK-NEXT:    [[RES:%.*]] = xor i1 [[C_3]], [[C_4]]
 ; CHECK-NEXT:    ret i1 [[RES]]
 ;
 entry:
@@ -591,7 +591,7 @@ define i1 @both_branch_to_same_block_or(i4 %x, i4 %y) {
 ; CHECK:       exit:
 ; CHECK-NEXT:    [[C_3:%.*]] = icmp ne i4 [[X]], 0
 ; CHECK-NEXT:    [[C_4:%.*]] = icmp eq i4 [[X]], 0
-; CHECK-NEXT:    [[RES:%.*]] = xor i1 false, true
+; CHECK-NEXT:    [[RES:%.*]] = xor i1 [[C_3]], [[C_4]]
 ; CHECK-NEXT:    ret i1 [[RES]]
 ;
 entry:


        


More information about the llvm-commits mailing list