[PATCH] D152143: [ConstraintElimination] Refactor `checkAndReplaceCondition` (NFC)

Antonio Frighetto via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 5 04:01:37 PDT 2023


antoniofrighetto created this revision.
antoniofrighetto added a reviewer: fhahn.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
antoniofrighetto requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Handling `true` and `false` constant replacements is now abstracted out into a single lambda function `ReplaceCmpWithConstant`, so as to reduce code duplication.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152143

Files:
  llvm/lib/Transforms/Scalar/ConstraintElimination.cpp


Index: llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -947,43 +947,57 @@
       CSToUse.popLastConstraint();
   });
 
-  bool Changed = false;
-  if (CSToUse.isConditionImplied(R.Coefficients)) {
+  auto ReplaceCmpWithConstant = [&](CmpInst *Cmp, bool IsTrue) {
     if (!DebugCounter::shouldExecute(EliminatedCounter))
       return false;
 
     LLVM_DEBUG({
-      dbgs() << "Condition " << *Cmp << " implied by dominating constraints\n";
+      if (IsTrue) {
+        dbgs() << "Condition " << *Cmp;
+      } else {
+        auto InversePred = Cmp->getInversePredicate();
+        dbgs() << "Condition " << CmpInst::getPredicateName(InversePred) << " "
+               << *A << ", " << *B;
+      }
+      dbgs() << " implied by dominating constraints\n";
       CSToUse.dump();
     });
+
     generateReproducer(Cmp, ReproducerModule, ReproducerCondStack, Info, DT);
-    Constant *TrueC =
-        ConstantInt::getTrue(CmpInst::makeCmpResultType(Cmp->getType()));
-    Cmp->replaceUsesWithIf(TrueC, [](Use &U) {
+    Constant *ConstantC =
+        IsTrue
+            ? ConstantInt::getTrue(CmpInst::makeCmpResultType(Cmp->getType()))
+            : ConstantInt::getFalse(CmpInst::makeCmpResultType(Cmp->getType()));
+    Cmp->replaceUsesWithIf(ConstantC, [](Use &U) {
       // Conditions in an assume trivially simplify to true. Skip uses
       // in assume calls to not destroy the available information.
       auto *II = dyn_cast<IntrinsicInst>(U.getUser());
       return !II || II->getIntrinsicID() != Intrinsic::assume;
     });
     NumCondsRemoved++;
-    Changed = true;
+    return true;
+  };
+
+  bool Changed = false;
+  bool IsConditionImplied = CSToUse.isConditionImplied(R.Coefficients);
+
+  if (IsConditionImplied) {
+    Changed = ReplaceCmpWithConstant(Cmp, true);
+    if (!Changed)
+      return false;
   }
+
+  // Compute them separately.
   auto Negated = ConstraintSystem::negate(R.Coefficients);
-  if (!Negated.empty() && CSToUse.isConditionImplied(Negated)) {
-    if (!DebugCounter::shouldExecute(EliminatedCounter))
-      return false;
+  auto IsNegatedImplied =
+      !Negated.empty() && CSToUse.isConditionImplied(Negated);
 
-    LLVM_DEBUG({
-      dbgs() << "Condition !" << *Cmp << " implied by dominating constraints\n";
-      CSToUse.dump(); 
-    });
-    generateReproducer(Cmp, ReproducerModule, ReproducerCondStack, Info, DT);
-    Constant *FalseC =
-        ConstantInt::getFalse(CmpInst::makeCmpResultType(Cmp->getType()));
-    Cmp->replaceAllUsesWith(FalseC);
-    NumCondsRemoved++;
-    Changed = true;
+  if (IsNegatedImplied) {
+    Changed = ReplaceCmpWithConstant(Cmp, false);
+    if (!Changed)
+      return false;
   }
+
   return Changed;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152143.528367.patch
Type: text/x-patch
Size: 2906 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230605/78b5a9a3/attachment.bin>


More information about the llvm-commits mailing list