[llvm] 420cf63 - [ConstraintElimination] Refactor `checkAndReplaceCondition` (NFC)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 5 07:55:17 PDT 2023
Author: Antonio Frighetto
Date: 2023-06-05T16:54:58+02:00
New Revision: 420cf63ead105d024f0dd0e90b67a7ffb1fd64fb
URL: https://github.com/llvm/llvm-project/commit/420cf63ead105d024f0dd0e90b67a7ffb1fd64fb
DIFF: https://github.com/llvm/llvm-project/commit/420cf63ead105d024f0dd0e90b67a7ffb1fd64fb.diff
LOG: [ConstraintElimination] Refactor `checkAndReplaceCondition` (NFC)
Handling `true` and `false` constant replacements is now abstracted
out into a single lambda function `ReplaceCmpWithConstant`, so as to
reduce code duplication.
Added:
Modified:
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
llvm/test/Transforms/ConstraintElimination/debug.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index b519c374a4e85..19379fd327de7 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -947,43 +947,55 @@ static bool checkAndReplaceCondition(
CSToUse.popLastConstraint();
});
- bool Changed = false;
- if (CSToUse.isConditionImplied(R.Coefficients)) {
+ auto ReplaceCmpWithConstant = [&](CmpInst *Cmp, bool IsTrue) {
if (!DebugCounter::shouldExecute(EliminatedCounter))
return false;
LLVM_DEBUG({
- dbgs() << "Condition " << *Cmp << " implied by dominating constraints\n";
+ if (IsTrue) {
+ dbgs() << "Condition " << *Cmp;
+ } else {
+ auto InversePred = Cmp->getInversePredicate();
+ dbgs() << "Condition " << CmpInst::getPredicateName(InversePred) << " "
+ << *A << ", " << *B;
+ }
+ dbgs() << " implied by dominating constraints\n";
CSToUse.dump();
});
+
generateReproducer(Cmp, ReproducerModule, ReproducerCondStack, Info, DT);
- Constant *TrueC =
- ConstantInt::getTrue(CmpInst::makeCmpResultType(Cmp->getType()));
- Cmp->replaceUsesWithIf(TrueC, [](Use &U) {
+ Constant *ConstantC = ConstantInt::getBool(
+ CmpInst::makeCmpResultType(Cmp->getType()), IsTrue);
+ Cmp->replaceUsesWithIf(ConstantC, [](Use &U) {
// Conditions in an assume trivially simplify to true. Skip uses
// in assume calls to not destroy the available information.
auto *II = dyn_cast<IntrinsicInst>(U.getUser());
return !II || II->getIntrinsicID() != Intrinsic::assume;
});
NumCondsRemoved++;
- Changed = true;
+ return true;
+ };
+
+ bool Changed = false;
+ bool IsConditionImplied = CSToUse.isConditionImplied(R.Coefficients);
+
+ if (IsConditionImplied) {
+ Changed = ReplaceCmpWithConstant(Cmp, true);
+ if (!Changed)
+ return false;
}
+
+ // Compute them separately.
auto Negated = ConstraintSystem::negate(R.Coefficients);
- if (!Negated.empty() && CSToUse.isConditionImplied(Negated)) {
- if (!DebugCounter::shouldExecute(EliminatedCounter))
- return false;
+ auto IsNegatedImplied =
+ !Negated.empty() && CSToUse.isConditionImplied(Negated);
- LLVM_DEBUG({
- dbgs() << "Condition !" << *Cmp << " implied by dominating constraints\n";
- CSToUse.dump();
- });
- generateReproducer(Cmp, ReproducerModule, ReproducerCondStack, Info, DT);
- Constant *FalseC =
- ConstantInt::getFalse(CmpInst::makeCmpResultType(Cmp->getType()));
- Cmp->replaceAllUsesWith(FalseC);
- NumCondsRemoved++;
- Changed = true;
+ if (IsNegatedImplied) {
+ Changed = ReplaceCmpWithConstant(Cmp, false);
+ if (!Changed)
+ return false;
}
+
return Changed;
}
diff --git a/llvm/test/Transforms/ConstraintElimination/debug.ll b/llvm/test/Transforms/ConstraintElimination/debug.ll
index 580b6d9d0fb55..a114db8ac6d60 100644
--- a/llvm/test/Transforms/ConstraintElimination/debug.ll
+++ b/llvm/test/Transforms/ConstraintElimination/debug.ll
@@ -2,8 +2,6 @@
; REQUIRES: asserts
-declare void @use(i1)
-
define i1 @test_and_ule(i4 %x, i4 %y, i4 %z) {
; CHECK: Processing fact to add to the system: %c.1 = icmp ule i4 %x, %y
; CHECK-NEXT: Adding 'ule %x, %y'
@@ -33,3 +31,33 @@ exit:
%c.3 = icmp ule i4 %x, %z
ret i1 %c.3
}
+
+define i1 @test_and_ugt(i4 %x, i4 %y, i4 %z) {
+; CHECK: Processing fact to add to the system: %c.1 = icmp ugt i4 %x, %y
+; CHECK-NEXT: Adding 'ugt %x, %y'
+; CHECK-NEXT: constraint: -1 * %x + %y <= -1
+
+; CHECK: Processing fact to add to the system: %c.2 = icmp ugt i4 %y, %z
+; CHECK-NEXT: Adding 'ugt %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: Removing -1 * %y + %z <= -1
+; CHECK: Removing -1 * %x + %y <= -1
+
+entry:
+ %c.1 = icmp ugt i4 %x, %y
+ %c.2 = icmp ugt i4 %y, %z
+ %and = and i1 %c.1, %c.2
+ br i1 %and, label %bb1, label %exit
+
+bb1:
+ %f.1 = icmp ule i4 %x, %z
+ ret i1 %f.1
+
+exit:
+ %c.3 = icmp ule i4 %x, %z
+ ret i1 %c.3
+}
More information about the llvm-commits
mailing list