Skip to content

[ConstraintElim] Refactor checkCondition. NFC. #75319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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)){
Expand All @@ -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();
});
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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())
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/Transforms/ConstraintElimination/debug.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down