[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.
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index b519c37..19379fd 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -947,43 +947,55 @@
       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;
-  }
-  auto Negated = ConstraintSystem::negate(R.Coefficients);
-  if (!Negated.empty() && CSToUse.isConditionImplied(Negated)) {
-    if (!DebugCounter::shouldExecute(EliminatedCounter))
-      return false;
+    return true;
+  };
 
-    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;
+  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);
+  auto IsNegatedImplied =
+      !Negated.empty() && CSToUse.isConditionImplied(Negated);
+
+  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 580b6d9..a114db8 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 @@
   %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
+}