[llvm] [ConstraintElim] Refactor `checkCondition`. NFC. (PR #75319)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 13 03:10:17 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Yingwei Zheng (dtcxzyw)

<details>
<summary>Changes</summary>

This patch refactors `checkCondition` to handle min/max intrinsic calls in #<!-- -->75306.


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


3 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/ConstraintElimination.cpp (+15-14) 
- (modified) llvm/test/Transforms/ConstraintElimination/debug.ll (+1-1) 
- (modified) llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll (+1-1) 


``````````diff
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index fafbe17583f5da..ccef74c921e619 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1260,14 +1260,12 @@ static void generateReproducer(CmpInst *Cond, Module *M,
   assert(!verifyFunction(*F, &dbgs()));
 }
 
-static std::optional<bool> checkCondition(CmpInst *Cmp, ConstraintInfo &Info,
-                                          unsigned NumIn, unsigned NumOut,
+static std::optional<bool> checkCondition(CmpInst::Predicate Pred, Value *A,
+                                          Value *B, Instruction *CheckInst,
+                                          ConstraintInfo &Info, unsigned NumIn,
+                                          unsigned NumOut,
                                           Instruction *ContextInst) {
-  LLVM_DEBUG(dbgs() << "Checking " << *Cmp << "\n");
-
-  CmpInst::Predicate Pred = Cmp->getPredicate();
-  Value *A = Cmp->getOperand(0);
-  Value *B = Cmp->getOperand(1);
+  LLVM_DEBUG(dbgs() << "Checking " << *CheckInst << "\n");
 
   auto R = Info.getConstraintForSolving(Pred, A, B);
   if (R.empty() || !R.isValid(Info)){
@@ -1293,9 +1291,10 @@ static std::optional<bool> checkCondition(CmpInst *Cmp, ConstraintInfo &Info,
 
     LLVM_DEBUG({
       if (*ImpliedCondition) {
-        dbgs() << "Condition " << *Cmp;
+        dbgs() << "Condition " << CmpInst::getPredicateName(Pred) << " " << *A
+               << ", " << *B;
       } else {
-        auto InversePred = Cmp->getInversePredicate();
+        auto InversePred = CmpInst::getInversePredicate(Pred);
         dbgs() << "Condition " << CmpInst::getPredicateName(InversePred) << " "
                << *A << ", " << *B;
       }
@@ -1338,8 +1337,9 @@ static bool checkAndReplaceCondition(
     return true;
   };
 
-  if (auto ImpliedCondition =
-          checkCondition(Cmp, Info, NumIn, NumOut, ContextInst))
+  if (auto ImpliedCondition = checkCondition(
+          Cmp->getPredicate(), Cmp->getOperand(0), Cmp->getOperand(1), Cmp,
+          Info, NumIn, NumOut, ContextInst))
     return ReplaceCmpWithConstant(Cmp, *ImpliedCondition);
   return false;
 }
@@ -1380,9 +1380,10 @@ static bool checkAndSecondOpImpliedByFirst(
 
   bool Changed = false;
   // Check if the second condition can be simplified now.
-  if (auto ImpliedCondition =
-          checkCondition(cast<ICmpInst>(And->getOperand(1)), Info, CB.NumIn,
-                         CB.NumOut, CB.getContextInst())) {
+  ICmpInst *Cmp = cast<ICmpInst>(And->getOperand(1));
+  if (auto ImpliedCondition = checkCondition(
+          Cmp->getPredicate(), Cmp->getOperand(0), Cmp->getOperand(1), Cmp,
+          Info, CB.NumIn, CB.NumOut, CB.getContextInst())) {
     And->setOperand(1, ConstantInt::getBool(And->getType(), *ImpliedCondition));
     Changed = true;
   }
diff --git a/llvm/test/Transforms/ConstraintElimination/debug.ll b/llvm/test/Transforms/ConstraintElimination/debug.ll
index f3f0f5056135c2..f967a290c4cc79 100644
--- a/llvm/test/Transforms/ConstraintElimination/debug.ll
+++ b/llvm/test/Transforms/ConstraintElimination/debug.ll
@@ -12,7 +12,7 @@ define i1 @test_and_ule(i4 %x, i4 %y, i4 %z) {
 ; CHECK-NEXT:  constraint: %y + -1 * %z <= 0
 
 ; CHECK: Checking   %t.1 = icmp ule i4 %x, %z
-; CHECK: Condition   %t.1 = icmp ule i4 %x, %z implied by dominating constraints
+; CHECK: Condition ule i4 %x, i4 %z implied by dominating constraints
 
 ; CHECK: Removing %y + -1 * %z <= 0
 ; CHECK: Removing %x + -1 * %y <= 0
diff --git a/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll b/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll
index b8343aed8b4af7..b4b2e2ee7077ee 100644
--- a/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll
+++ b/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll
@@ -4,7 +4,7 @@
 
 target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
 
-; CHECK:      Condition   %c.2 = icmp eq ptr %a, null implied by dominating constraints
+; CHECK:      Condition eq ptr %a, ptr null implied by dominating constraints
 ; CHECK-NEXT: %a <= 0
 ; CHECK-NEXT: Creating reproducer for   %c.2 = icmp eq ptr %a, null
 ; CHECK-NEXT:   found external input ptr %a

``````````

</details>


https://github.com/llvm/llvm-project/pull/75319


More information about the llvm-commits mailing list