Skip to content

Commit 26fbdff

Browse files
authored
[ConstraintElim] Refactor checkCondition. NFC. (#75319)
This patch refactors `checkCondition` to handle min/max intrinsic calls in #75306.
1 parent 3564c85 commit 26fbdff

File tree

3 files changed

+42
-41
lines changed

3 files changed

+42
-41
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,14 @@ void State::addInfoFor(BasicBlock &BB) {
11091109
CmpI->getOperand(1)));
11101110
}
11111111

1112+
static void dumpUnpackedICmp(raw_ostream &OS, ICmpInst::Predicate Pred,
1113+
Value *LHS, Value *RHS) {
1114+
OS << "icmp " << Pred << ' ';
1115+
LHS->printAsOperand(OS, /*PrintType=*/true);
1116+
OS << ", ";
1117+
RHS->printAsOperand(OS, /*PrintType=*/false);
1118+
}
1119+
11121120
namespace {
11131121
/// Helper to keep track of a condition and if it should be treated as negated
11141122
/// for reproducer construction.
@@ -1241,10 +1249,9 @@ static void generateReproducer(CmpInst *Cond, Module *M,
12411249
if (Entry.Pred == ICmpInst::BAD_ICMP_PREDICATE)
12421250
continue;
12431251

1244-
LLVM_DEBUG(
1245-
dbgs() << " Materializing assumption icmp " << Entry.Pred << ' ';
1246-
Entry.LHS->printAsOperand(dbgs(), /*PrintType=*/true); dbgs() << ", ";
1247-
Entry.RHS->printAsOperand(dbgs(), /*PrintType=*/false); dbgs() << "\n");
1252+
LLVM_DEBUG(dbgs() << " Materializing assumption ";
1253+
dumpUnpackedICmp(dbgs(), Entry.Pred, Entry.LHS, Entry.RHS);
1254+
dbgs() << "\n");
12481255
CloneInstructions({Entry.LHS, Entry.RHS}, CmpInst::isSigned(Entry.Pred));
12491256

12501257
auto *Cmp = Builder.CreateICmp(Entry.Pred, Entry.LHS, Entry.RHS);
@@ -1260,14 +1267,12 @@ static void generateReproducer(CmpInst *Cond, Module *M,
12601267
assert(!verifyFunction(*F, &dbgs()));
12611268
}
12621269

1263-
static std::optional<bool> checkCondition(CmpInst *Cmp, ConstraintInfo &Info,
1264-
unsigned NumIn, unsigned NumOut,
1270+
static std::optional<bool> checkCondition(CmpInst::Predicate Pred, Value *A,
1271+
Value *B, Instruction *CheckInst,
1272+
ConstraintInfo &Info, unsigned NumIn,
1273+
unsigned NumOut,
12651274
Instruction *ContextInst) {
1266-
LLVM_DEBUG(dbgs() << "Checking " << *Cmp << "\n");
1267-
1268-
CmpInst::Predicate Pred = Cmp->getPredicate();
1269-
Value *A = Cmp->getOperand(0);
1270-
Value *B = Cmp->getOperand(1);
1275+
LLVM_DEBUG(dbgs() << "Checking " << *CheckInst << "\n");
12711276

12721277
auto R = Info.getConstraintForSolving(Pred, A, B);
12731278
if (R.empty() || !R.isValid(Info)){
@@ -1292,13 +1297,10 @@ static std::optional<bool> checkCondition(CmpInst *Cmp, ConstraintInfo &Info,
12921297
return std::nullopt;
12931298

12941299
LLVM_DEBUG({
1295-
if (*ImpliedCondition) {
1296-
dbgs() << "Condition " << *Cmp;
1297-
} else {
1298-
auto InversePred = Cmp->getInversePredicate();
1299-
dbgs() << "Condition " << CmpInst::getPredicateName(InversePred) << " "
1300-
<< *A << ", " << *B;
1301-
}
1300+
dbgs() << "Condition ";
1301+
dumpUnpackedICmp(
1302+
dbgs(), *ImpliedCondition ? Pred : CmpInst::getInversePredicate(Pred),
1303+
A, B);
13021304
dbgs() << " implied by dominating constraints\n";
13031305
CSToUse.dump();
13041306
});
@@ -1338,8 +1340,9 @@ static bool checkAndReplaceCondition(
13381340
return true;
13391341
};
13401342

1341-
if (auto ImpliedCondition =
1342-
checkCondition(Cmp, Info, NumIn, NumOut, ContextInst))
1343+
if (auto ImpliedCondition = checkCondition(
1344+
Cmp->getPredicate(), Cmp->getOperand(0), Cmp->getOperand(1), Cmp,
1345+
Info, NumIn, NumOut, ContextInst))
13431346
return ReplaceCmpWithConstant(Cmp, *ImpliedCondition);
13441347
return false;
13451348
}
@@ -1380,9 +1383,10 @@ static bool checkAndSecondOpImpliedByFirst(
13801383

13811384
bool Changed = false;
13821385
// Check if the second condition can be simplified now.
1383-
if (auto ImpliedCondition =
1384-
checkCondition(cast<ICmpInst>(And->getOperand(1)), Info, CB.NumIn,
1385-
CB.NumOut, CB.getContextInst())) {
1386+
ICmpInst *Cmp = cast<ICmpInst>(And->getOperand(1));
1387+
if (auto ImpliedCondition = checkCondition(
1388+
Cmp->getPredicate(), Cmp->getOperand(0), Cmp->getOperand(1), Cmp,
1389+
Info, CB.NumIn, CB.NumOut, CB.getContextInst())) {
13861390
And->setOperand(1, ConstantInt::getBool(And->getType(), *ImpliedCondition));
13871391
Changed = true;
13881392
}
@@ -1408,9 +1412,8 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B,
14081412
if (!R.isValid(*this) || R.isNe())
14091413
return;
14101414

1411-
LLVM_DEBUG(dbgs() << "Adding '" << Pred << " ";
1412-
A->printAsOperand(dbgs(), false); dbgs() << ", ";
1413-
B->printAsOperand(dbgs(), false); dbgs() << "'\n");
1415+
LLVM_DEBUG(dbgs() << "Adding '"; dumpUnpackedICmp(dbgs(), Pred, A, B);
1416+
dbgs() << "'\n");
14141417
bool Added = false;
14151418
auto &CSToUse = getCS(R.IsSigned);
14161419
if (R.Coefficients.empty())
@@ -1616,10 +1619,8 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
16161619
}
16171620

16181621
auto AddFact = [&](CmpInst::Predicate Pred, Value *A, Value *B) {
1619-
LLVM_DEBUG(dbgs() << "fact to add to the system: "
1620-
<< CmpInst::getPredicateName(Pred) << " ";
1621-
A->printAsOperand(dbgs()); dbgs() << ", ";
1622-
B->printAsOperand(dbgs(), false); dbgs() << "\n");
1622+
LLVM_DEBUG(dbgs() << "fact to add to the system: ";
1623+
dumpUnpackedICmp(dbgs(), Pred, A, B); dbgs() << "\n");
16231624
if (Info.getCS(CmpInst::isSigned(Pred)).size() > MaxRows) {
16241625
LLVM_DEBUG(
16251626
dbgs()

llvm/test/Transforms/ConstraintElimination/debug.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
; REQUIRES: asserts
44

55
define i1 @test_and_ule(i4 %x, i4 %y, i4 %z) {
6-
; CHECK: Processing fact to add to the system: ule i4 %x, %y
7-
; CHECK-NEXT: Adding 'ule %x, %y'
6+
; CHECK: Processing fact to add to the system: icmp ule i4 %x, %y
7+
; CHECK-NEXT: Adding 'icmp ule i4 %x, %y'
88
; CHECK-NEXT: constraint: %x + -1 * %y <= 0
99

10-
; CHECK: Processing fact to add to the system: ule i4 %y, %z
11-
; CHECK-NEXT: Adding 'ule %y, %z'
10+
; CHECK: Processing fact to add to the system: icmp ule i4 %y, %z
11+
; CHECK-NEXT: Adding 'icmp ule i4 %y, %z'
1212
; CHECK-NEXT: constraint: %y + -1 * %z <= 0
1313

1414
; CHECK: Checking %t.1 = icmp ule i4 %x, %z
15-
; CHECK: Condition %t.1 = icmp ule i4 %x, %z implied by dominating constraints
15+
; CHECK: Condition icmp ule i4 %x, %z implied by dominating constraints
1616

1717
; CHECK: Removing %y + -1 * %z <= 0
1818
; CHECK: Removing %x + -1 * %y <= 0
@@ -33,16 +33,16 @@ exit:
3333
}
3434

3535
define i1 @test_and_ugt(i4 %x, i4 %y, i4 %z) {
36-
; CHECK: Processing fact to add to the system: ugt i4 %x, %y
37-
; CHECK-NEXT: Adding 'ugt %x, %y'
36+
; CHECK: Processing fact to add to the system: icmp ugt i4 %x, %y
37+
; CHECK-NEXT: Adding 'icmp ugt i4 %x, %y'
3838
; CHECK-NEXT: constraint: -1 * %x + %y <= -1
3939

40-
; CHECK: Processing fact to add to the system: ugt i4 %y, %z
41-
; CHECK-NEXT: Adding 'ugt %y, %z'
40+
; CHECK: Processing fact to add to the system: icmp ugt i4 %y, %z
41+
; CHECK-NEXT: Adding 'icmp ugt i4 %y, %z'
4242
; CHECK-NEXT: constraint: -1 * %y + %z <= -1
4343

4444
; CHECK: Checking %f.1 = icmp ule i4 %x, %z
45-
; CHECK: Condition ugt i4 %x, i4 %z implied by dominating constraints
45+
; CHECK: Condition icmp ugt i4 %x, %z implied by dominating constraints
4646

4747
; CHECK: Removing -1 * %y + %z <= -1
4848
; CHECK: Removing -1 * %x + %y <= -1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
66

7-
; CHECK: Condition %c.2 = icmp eq ptr %a, null implied by dominating constraints
7+
; CHECK: Condition icmp eq ptr %a, null implied by dominating constraints
88
; CHECK-NEXT: %a <= 0
99
; CHECK-NEXT: Creating reproducer for %c.2 = icmp eq ptr %a, null
1010
; CHECK-NEXT: found external input ptr %a

0 commit comments

Comments
 (0)