Skip to content

Commit cce5324

Browse files
committed
[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
1 parent 24bb36e commit cce5324

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -865,11 +865,15 @@ void State::addInfoFor(BasicBlock &BB) {
865865
namespace {
866866
/// Helper to keep track of a condition and if it should be treated as negated
867867
/// for reproducer construction.
868+
/// Pred == Predicate::BAD_ICMP_PREDICATE indicates that this entry is a
869+
/// placeholder to keep the ReproducerCondStack in sync with DFSInStack.
868870
struct ReproducerEntry {
869-
CmpInst *Cond;
870-
bool IsNot;
871+
ICmpInst::Predicate Pred;
872+
Value *LHS;
873+
Value *RHS;
871874

872-
ReproducerEntry(CmpInst *Cond, bool IsNot) : Cond(Cond), IsNot(IsNot) {}
875+
ReproducerEntry(ICmpInst::Predicate Pred, Value *LHS, Value *RHS)
876+
: Pred(Pred), LHS(LHS), RHS(RHS) {}
873877
};
874878
} // namespace
875879

@@ -898,13 +902,9 @@ static void generateReproducer(CmpInst *Cond, Module *M,
898902
// ConstraintElimination can decompose. Such values will be considered as
899903
// external inputs to the reproducer, they are collected and added as function
900904
// arguments later.
901-
auto CollectArguments = [&](CmpInst *Cond) {
902-
if (!Cond)
903-
return;
904-
auto &Value2Index =
905-
Info.getValue2Index(CmpInst::isSigned(Cond->getPredicate()));
906-
SmallVector<Value *, 4> WorkList;
907-
WorkList.push_back(Cond);
905+
auto CollectArguments = [&](ArrayRef<Value *> Ops, bool IsSigned) {
906+
auto &Value2Index = Info.getValue2Index(IsSigned);
907+
SmallVector<Value *, 4> WorkList(Ops);
908908
while (!WorkList.empty()) {
909909
Value *V = WorkList.pop_back_val();
910910
if (!Seen.insert(V).second)
@@ -927,8 +927,9 @@ static void generateReproducer(CmpInst *Cond, Module *M,
927927
};
928928

929929
for (auto &Entry : Stack)
930-
CollectArguments(Entry.Cond);
931-
CollectArguments(Cond);
930+
if (Entry.Pred != ICmpInst::BAD_ICMP_PREDICATE)
931+
CollectArguments({Entry.LHS, Entry.RHS}, ICmpInst::isSigned(Entry.Pred));
932+
CollectArguments(Cond, ICmpInst::isSigned(Cond->getPredicate()));
932933

933934
SmallVector<Type *> ParamTys;
934935
for (auto *P : Args)
@@ -990,19 +991,16 @@ static void generateReproducer(CmpInst *Cond, Module *M,
990991
// function. Then add an ICmp for the condition (with the inverse predicate if
991992
// the entry is negated) and an assert using the ICmp.
992993
for (auto &Entry : Stack) {
993-
if (!Entry.Cond)
994+
if (Entry.Pred == ICmpInst::BAD_ICMP_PREDICATE)
994995
continue;
995996

996-
LLVM_DEBUG(dbgs() << " Materializing assumption " << *Entry.Cond << "\n");
997-
CmpInst::Predicate Pred = Entry.Cond->getPredicate();
998-
if (Entry.IsNot)
999-
Pred = CmpInst::getInversePredicate(Pred);
1000-
1001-
CloneInstructions({Entry.Cond->getOperand(0), Entry.Cond->getOperand(1)},
1002-
CmpInst::isSigned(Entry.Cond->getPredicate()));
997+
LLVM_DEBUG(
998+
dbgs() << " Materializing assumption icmp " << Entry.Pred << ' ';
999+
Entry.LHS->printAsOperand(dbgs(), /*PrintType=*/true); dbgs() << ", ";
1000+
Entry.RHS->printAsOperand(dbgs(), /*PrintType=*/false); dbgs() << "\n");
1001+
CloneInstructions({Entry.LHS, Entry.RHS}, CmpInst::isSigned(Entry.Pred));
10031002

1004-
auto *Cmp = Builder.CreateICmp(Pred, Entry.Cond->getOperand(0),
1005-
Entry.Cond->getOperand(1));
1003+
auto *Cmp = Builder.CreateICmp(Entry.Pred, Entry.LHS, Entry.RHS);
10061004
Builder.CreateAssumption(Cmp);
10071005
}
10081006

@@ -1383,7 +1381,7 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT,
13831381

13841382
Info.addFact(Pred, A, B, CB.NumIn, CB.NumOut, DFSInStack);
13851383
if (ReproducerModule && DFSInStack.size() > ReproducerCondStack.size())
1386-
ReproducerCondStack.emplace_back(cast<CmpInst>(Cmp), CB.Not);
1384+
ReproducerCondStack.emplace_back(Pred, A, B);
13871385

13881386
Info.transferToOtherSystem(Pred, A, B, CB.NumIn, CB.NumOut, DFSInStack);
13891387
if (ReproducerModule && DFSInStack.size() > ReproducerCondStack.size()) {
@@ -1392,7 +1390,8 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT,
13921390
for (unsigned I = 0,
13931391
E = (DFSInStack.size() - ReproducerCondStack.size());
13941392
I < E; ++I) {
1395-
ReproducerCondStack.emplace_back(nullptr, false);
1393+
ReproducerCondStack.emplace_back(ICmpInst::BAD_ICMP_PREDICATE,
1394+
nullptr, nullptr);
13961395
}
13971396
}
13981397
}

llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16
88
; CHECK-NEXT: %a + -1 * null <= 0
99
; CHECK-NEXT: Creating reproducer for %c.2 = icmp eq ptr %a, null
1010
; CHECK-NEXT: found external input ptr %a
11-
; CHECK-NEXT: Materializing assumption %c.1 = icmp eq ptr %a, null
11+
; CHECK-NEXT: Materializing assumption icmp eq ptr %a, null
1212

1313
define i1 @test_ptr_null_constant(ptr %a) {
1414
; CHECK-LABEL: define i1 @"{{.+}}test_ptr_null_constantrepro"(ptr %a) {

0 commit comments

Comments
 (0)