[llvm] c60cdb4 - [ConstraintElimination] Only add cond from assume to succs if valid.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 7 04:15:11 PST 2022


Author: Florian Hahn
Date: 2022-03-07T12:01:15Z
New Revision: c60cdb44f7ecb4b02edf8b3ada707e8a74e79211

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

LOG: [ConstraintElimination] Only add cond from assume to succs if valid.

Add missing CanAdd check before adding a condition from an assume
to the successor blocks. When adding information from assume to
successor blocks we need to perform the same CanAdd as we do for adding
a condition from a branch.

Fixes https://github.com/llvm/llvm-project/issues/54217

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index af90043c93528..c094b5c660471 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -413,6 +413,19 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
       continue;
     WorkList.emplace_back(DT.getNode(&BB));
 
+    // Returns true if we can add a known condition from BB to its successor
+    // block Succ. Each predecessor of Succ can either be BB or be dominated by
+    // Succ (e.g. the case when adding a condition from a pre-header to a loop
+    // header).
+    auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
+      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);
+             });
+    };
+
     // True as long as long as the current instruction is guaranteed to execute.
     bool GuaranteedToExecute = true;
     // Scan BB for assume calls.
@@ -431,9 +444,12 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
           WorkList.emplace_back(DT.getNode(&BB), cast<ICmpInst>(Cond), false);
         } else {
           // Otherwise the condition only holds in the successors.
-          for (BasicBlock *Succ : successors(&BB))
+          for (BasicBlock *Succ : successors(&BB)) {
+            if (!CanAdd(Succ))
+              continue;
             WorkList.emplace_back(DT.getNode(Succ), cast<ICmpInst>(Cond),
                                   false);
+          }
         }
       }
       GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I);
@@ -443,18 +459,6 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
     if (!Br || !Br->isConditional())
       continue;
 
-    // Returns true if we can add a known condition from BB to its successor
-    // block Succ. Each predecessor of Succ can either be BB or be dominated by
-    // Succ (e.g. the case when adding a condition from a pre-header to a loop
-    // header).
-    auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
-      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
     // false successor.

diff  --git a/llvm/test/Transforms/ConstraintElimination/assumes.ll b/llvm/test/Transforms/ConstraintElimination/assumes.ll
index 73a24dd1cac56..d51098e6c4df3 100644
--- a/llvm/test/Transforms/ConstraintElimination/assumes.ll
+++ b/llvm/test/Transforms/ConstraintElimination/assumes.ll
@@ -164,7 +164,7 @@ define i1 @assume_does_not_dominates_successor_with_may_unwind_call_before_assum
 ; CHECK-NEXT:    br label [[EXIT]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    [[C_2:%.*]] = icmp eq i16 [[A]], 0
-; CHECK-NEXT:    ret i1 true
+; CHECK-NEXT:    ret i1 [[C_2]]
 ;
 entry:
   br i1 %i.0, label %exit, label %if.then


        


More information about the llvm-commits mailing list