[llvm] cce5324 - [ConstraintElim] Store the triple Pred + LHS + RHS in ReproducerEntry instead of CmpInst + Not

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 20 08:23:05 PDT 2023


Author: Yingwei Zheng
Date: 2023-07-20T23:21:36+08:00
New Revision: cce5324994d6ef0c37c2619230154b0658eda398

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

LOG: [ConstraintElim] Store the triple Pred + LHS + RHS in ReproducerEntry instead of CmpInst + Not

This patch represents a condition with `Pred + LHS + RHS` in ReproducerEntry instead of `CmpInst + Not`.
It avoids creating temporary ICmpInsts in D155412.

Reviewed By: nikic, fhahn

Differential Revision: https://reviews.llvm.org/D155782

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
    llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 55e4503ff8aa07..e5773438fe5935 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -865,11 +865,15 @@ void State::addInfoFor(BasicBlock &BB) {
 namespace {
 /// Helper to keep track of a condition and if it should be treated as negated
 /// for reproducer construction.
+/// Pred == Predicate::BAD_ICMP_PREDICATE indicates that this entry is a
+/// placeholder to keep the ReproducerCondStack in sync with DFSInStack.
 struct ReproducerEntry {
-  CmpInst *Cond;
-  bool IsNot;
+  ICmpInst::Predicate Pred;
+  Value *LHS;
+  Value *RHS;
 
-  ReproducerEntry(CmpInst *Cond, bool IsNot) : Cond(Cond), IsNot(IsNot) {}
+  ReproducerEntry(ICmpInst::Predicate Pred, Value *LHS, Value *RHS)
+      : Pred(Pred), LHS(LHS), RHS(RHS) {}
 };
 } // namespace
 
@@ -898,13 +902,9 @@ static void generateReproducer(CmpInst *Cond, Module *M,
   // ConstraintElimination can decompose. Such values will be considered as
   // external inputs to the reproducer, they are collected and added as function
   // arguments later.
-  auto CollectArguments = [&](CmpInst *Cond) {
-    if (!Cond)
-      return;
-    auto &Value2Index =
-        Info.getValue2Index(CmpInst::isSigned(Cond->getPredicate()));
-    SmallVector<Value *, 4> WorkList;
-    WorkList.push_back(Cond);
+  auto CollectArguments = [&](ArrayRef<Value *> Ops, bool IsSigned) {
+    auto &Value2Index = Info.getValue2Index(IsSigned);
+    SmallVector<Value *, 4> WorkList(Ops);
     while (!WorkList.empty()) {
       Value *V = WorkList.pop_back_val();
       if (!Seen.insert(V).second)
@@ -927,8 +927,9 @@ static void generateReproducer(CmpInst *Cond, Module *M,
   };
 
   for (auto &Entry : Stack)
-    CollectArguments(Entry.Cond);
-  CollectArguments(Cond);
+    if (Entry.Pred != ICmpInst::BAD_ICMP_PREDICATE)
+      CollectArguments({Entry.LHS, Entry.RHS}, ICmpInst::isSigned(Entry.Pred));
+  CollectArguments(Cond, ICmpInst::isSigned(Cond->getPredicate()));
 
   SmallVector<Type *> ParamTys;
   for (auto *P : Args)
@@ -990,19 +991,16 @@ static void generateReproducer(CmpInst *Cond, Module *M,
   // function. Then add an ICmp for the condition (with the inverse predicate if
   // the entry is negated) and an assert using the ICmp.
   for (auto &Entry : Stack) {
-    if (!Entry.Cond)
+    if (Entry.Pred == ICmpInst::BAD_ICMP_PREDICATE)
       continue;
 
-    LLVM_DEBUG(dbgs() << "  Materializing assumption " << *Entry.Cond << "\n");
-    CmpInst::Predicate Pred = Entry.Cond->getPredicate();
-    if (Entry.IsNot)
-      Pred = CmpInst::getInversePredicate(Pred);
-
-    CloneInstructions({Entry.Cond->getOperand(0), Entry.Cond->getOperand(1)},
-                      CmpInst::isSigned(Entry.Cond->getPredicate()));
+    LLVM_DEBUG(
+        dbgs() << "  Materializing assumption icmp " << Entry.Pred << ' ';
+        Entry.LHS->printAsOperand(dbgs(), /*PrintType=*/true); dbgs() << ", ";
+        Entry.RHS->printAsOperand(dbgs(), /*PrintType=*/false); dbgs() << "\n");
+    CloneInstructions({Entry.LHS, Entry.RHS}, CmpInst::isSigned(Entry.Pred));
 
-    auto *Cmp = Builder.CreateICmp(Pred, Entry.Cond->getOperand(0),
-                                   Entry.Cond->getOperand(1));
+    auto *Cmp = Builder.CreateICmp(Entry.Pred, Entry.LHS, Entry.RHS);
     Builder.CreateAssumption(Cmp);
   }
 
@@ -1383,7 +1381,7 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT,
 
       Info.addFact(Pred, A, B, CB.NumIn, CB.NumOut, DFSInStack);
       if (ReproducerModule && DFSInStack.size() > ReproducerCondStack.size())
-        ReproducerCondStack.emplace_back(cast<CmpInst>(Cmp), CB.Not);
+        ReproducerCondStack.emplace_back(Pred, A, B);
 
       Info.transferToOtherSystem(Pred, A, B, CB.NumIn, CB.NumOut, DFSInStack);
       if (ReproducerModule && DFSInStack.size() > ReproducerCondStack.size()) {
@@ -1392,7 +1390,8 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT,
         for (unsigned I = 0,
                       E = (DFSInStack.size() - ReproducerCondStack.size());
              I < E; ++I) {
-          ReproducerCondStack.emplace_back(nullptr, false);
+          ReproducerCondStack.emplace_back(ICmpInst::BAD_ICMP_PREDICATE,
+                                           nullptr, nullptr);
         }
       }
     }

diff  --git a/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll b/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll
index de22c1a1f8a388..56a4b69f742547 100644
--- a/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll
+++ b/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll
@@ -8,7 +8,7 @@ target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16
 ; CHECK-NEXT: %a + -1 * null <= 0
 ; CHECK-NEXT: Creating reproducer for   %c.2 = icmp eq ptr %a, null
 ; CHECK-NEXT:   found external input ptr %a
-; CHECK-NEXT:   Materializing assumption   %c.1 = icmp eq ptr %a, null
+; CHECK-NEXT:   Materializing assumption icmp eq ptr %a, null
 
 define i1 @test_ptr_null_constant(ptr %a) {
 ; CHECK-LABEL: define i1 @"{{.+}}test_ptr_null_constantrepro"(ptr %a) {


        


More information about the llvm-commits mailing list