[llvm] [ConstraintElim] Refactor `checkCondition`. NFC. (PR #75319)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 13 03:54:18 PST 2023
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/75319
>From 08ad897ead4a555bd7575a5c685e73285c8d1f47 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 13 Dec 2023 18:56:05 +0800
Subject: [PATCH] [ConstraintElim] Refactor `checkCondition`. NFC.
---
.../Scalar/ConstraintElimination.cpp | 61 ++++++++++---------
.../Transforms/ConstraintElimination/debug.ll | 20 +++---
.../reproducer-remarks-debug.ll | 2 +-
3 files changed, 42 insertions(+), 41 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index fafbe17583f5da..9984276e9db349 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1109,6 +1109,14 @@ void State::addInfoFor(BasicBlock &BB) {
CmpI->getOperand(1)));
}
+static void dumpUnpackedICmp(raw_ostream &OS, ICmpInst::Predicate Pred,
+ Value *LHS, Value *RHS) {
+ OS << "icmp " << Pred << ' ';
+ LHS->printAsOperand(OS, /*PrintType=*/true);
+ OS << ", ";
+ RHS->printAsOperand(OS, /*PrintType=*/false);
+}
+
namespace {
/// Helper to keep track of a condition and if it should be treated as negated
/// for reproducer construction.
@@ -1241,10 +1249,9 @@ static void generateReproducer(CmpInst *Cond, Module *M,
if (Entry.Pred == ICmpInst::BAD_ICMP_PREDICATE)
continue;
- LLVM_DEBUG(
- dbgs() << " Materializing assumption icmp " << Entry.Pred << ' ';
- Entry.LHS->printAsOperand(dbgs(), /*PrintType=*/true); dbgs() << ", ";
- Entry.RHS->printAsOperand(dbgs(), /*PrintType=*/false); dbgs() << "\n");
+ LLVM_DEBUG(dbgs() << " Materializing assumption ";
+ dumpUnpackedICmp(dbgs(), Entry.Pred, Entry.LHS, Entry.RHS);
+ dbgs() << "\n");
CloneInstructions({Entry.LHS, Entry.RHS}, CmpInst::isSigned(Entry.Pred));
auto *Cmp = Builder.CreateICmp(Entry.Pred, Entry.LHS, Entry.RHS);
@@ -1260,14 +1267,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)){
@@ -1292,13 +1297,10 @@ static std::optional<bool> checkCondition(CmpInst *Cmp, ConstraintInfo &Info,
return std::nullopt;
LLVM_DEBUG({
- if (*ImpliedCondition) {
- dbgs() << "Condition " << *Cmp;
- } else {
- auto InversePred = Cmp->getInversePredicate();
- dbgs() << "Condition " << CmpInst::getPredicateName(InversePred) << " "
- << *A << ", " << *B;
- }
+ dbgs() << "Condition ";
+ dumpUnpackedICmp(
+ dbgs(), *ImpliedCondition ? Pred : CmpInst::getInversePredicate(Pred),
+ A, B);
dbgs() << " implied by dominating constraints\n";
CSToUse.dump();
});
@@ -1338,8 +1340,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 +1383,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;
}
@@ -1408,9 +1412,8 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B,
if (!R.isValid(*this) || R.isNe())
return;
- LLVM_DEBUG(dbgs() << "Adding '" << Pred << " ";
- A->printAsOperand(dbgs(), false); dbgs() << ", ";
- B->printAsOperand(dbgs(), false); dbgs() << "'\n");
+ LLVM_DEBUG(dbgs() << "Adding '"; dumpUnpackedICmp(dbgs(), Pred, A, B);
+ dbgs() << "'\n");
bool Added = false;
auto &CSToUse = getCS(R.IsSigned);
if (R.Coefficients.empty())
@@ -1616,10 +1619,8 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
}
auto AddFact = [&](CmpInst::Predicate Pred, Value *A, Value *B) {
- LLVM_DEBUG(dbgs() << "fact to add to the system: "
- << CmpInst::getPredicateName(Pred) << " ";
- A->printAsOperand(dbgs()); dbgs() << ", ";
- B->printAsOperand(dbgs(), false); dbgs() << "\n");
+ LLVM_DEBUG(dbgs() << "fact to add to the system: ";
+ dumpUnpackedICmp(dbgs(), Pred, A, B); dbgs() << "\n");
if (Info.getCS(CmpInst::isSigned(Pred)).size() > MaxRows) {
LLVM_DEBUG(
dbgs()
diff --git a/llvm/test/Transforms/ConstraintElimination/debug.ll b/llvm/test/Transforms/ConstraintElimination/debug.ll
index f3f0f5056135c2..2409af09735d1e 100644
--- a/llvm/test/Transforms/ConstraintElimination/debug.ll
+++ b/llvm/test/Transforms/ConstraintElimination/debug.ll
@@ -3,16 +3,16 @@
; REQUIRES: asserts
define i1 @test_and_ule(i4 %x, i4 %y, i4 %z) {
-; CHECK: Processing fact to add to the system: ule i4 %x, %y
-; CHECK-NEXT: Adding 'ule %x, %y'
+; CHECK: Processing fact to add to the system: icmp ule i4 %x, %y
+; CHECK-NEXT: Adding 'icmp ule i4 %x, %y'
; CHECK-NEXT: constraint: %x + -1 * %y <= 0
-; CHECK: Processing fact to add to the system: ule i4 %y, %z
-; CHECK-NEXT: Adding 'ule %y, %z'
+; CHECK: Processing fact to add to the system: icmp ule i4 %y, %z
+; CHECK-NEXT: Adding 'icmp ule i4 %y, %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 icmp ule i4 %x, %z implied by dominating constraints
; CHECK: Removing %y + -1 * %z <= 0
; CHECK: Removing %x + -1 * %y <= 0
@@ -33,16 +33,16 @@ exit:
}
define i1 @test_and_ugt(i4 %x, i4 %y, i4 %z) {
-; CHECK: Processing fact to add to the system: ugt i4 %x, %y
-; CHECK-NEXT: Adding 'ugt %x, %y'
+; CHECK: Processing fact to add to the system: icmp ugt i4 %x, %y
+; CHECK-NEXT: Adding 'icmp ugt i4 %x, %y'
; CHECK-NEXT: constraint: -1 * %x + %y <= -1
-; CHECK: Processing fact to add to the system: ugt i4 %y, %z
-; CHECK-NEXT: Adding 'ugt %y, %z'
+; CHECK: Processing fact to add to the system: icmp ugt i4 %y, %z
+; CHECK-NEXT: Adding 'icmp ugt i4 %y, %z'
; CHECK-NEXT: constraint: -1 * %y + %z <= -1
; CHECK: Checking %f.1 = icmp ule i4 %x, %z
-; CHECK: Condition ugt i4 %x, i4 %z implied by dominating constraints
+; CHECK: Condition icmp ugt i4 %x, %z implied by dominating constraints
; CHECK: Removing -1 * %y + %z <= -1
; CHECK: Removing -1 * %x + %y <= -1
diff --git a/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll b/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll
index b8343aed8b4af7..4fdc8e583112ef 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 icmp eq ptr %a, 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
More information about the llvm-commits
mailing list