[ConstraintElimination] Update Changed status in ssub simplification.
authorFlorian Hahn <flo@fhahn.com>
Sun, 2 Oct 2022 13:25:51 +0000 (14:25 +0100)
committerFlorian Hahn <flo@fhahn.com>
Sun, 2 Oct 2022 13:25:51 +0000 (14:25 +0100)
Update tryToSimplifyOverflowMath to indicate whether the function made
any changes to the IR.

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
llvm/test/Transforms/ConstraintElimination/analysis-invalidation.ll

index bc51c3b..b881722 100644 (file)
@@ -670,7 +670,7 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B,
   }
 }
 
-static void
+static bool
 tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
                           SmallVectorImpl<Instruction *> &ToRemove) {
   auto DoesConditionHold = [](CmpInst::Predicate Pred, Value *A, Value *B,
@@ -684,6 +684,7 @@ tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
     return CSToUse.isConditionImplied(R.Coefficients);
   };
 
+  bool Changed = false;
   if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow) {
     // If A s>= B && B s>= 0, ssub.with.overflow(a, b) should not overflow and
     // can be simplified to a regular sub.
@@ -692,7 +693,7 @@ tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
     if (!DoesConditionHold(CmpInst::ICMP_SGE, A, B, Info) ||
         !DoesConditionHold(CmpInst::ICMP_SGE, B,
                            ConstantInt::get(A->getType(), 0), Info))
-      return;
+      return false;
 
     IRBuilder<> Builder(II->getParent(), II->getIterator());
     Value *Sub = nullptr;
@@ -701,21 +702,27 @@ tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
         if (!Sub)
           Sub = Builder.CreateSub(A, B);
         U->replaceAllUsesWith(Sub);
-      } else if (match(U, m_ExtractValue<1>(m_Value())))
+        Changed = true;
+      } else if (match(U, m_ExtractValue<1>(m_Value()))) {
         U->replaceAllUsesWith(Builder.getFalse());
-      else
+        Changed = true;
+      } else
         continue;
 
       if (U->use_empty()) {
         auto *I = cast<Instruction>(U);
         ToRemove.push_back(I);
         I->setOperand(0, PoisonValue::get(II->getType()));
+        Changed = true;
       }
     }
 
-    if (II->use_empty())
+    if (II->use_empty()) {
       II->eraseFromParent();
+      Changed = true;
+    }
   }
+  return Changed;
 }
 
 static bool eliminateConstraints(Function &F, DominatorTree &DT) {
@@ -786,7 +793,7 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
     if (CB.IsBlock) {
       for (Instruction &I : make_early_inc_range(*CB.BB)) {
         if (auto *II = dyn_cast<WithOverflowInst>(&I)) {
-          tryToSimplifyOverflowMath(II, Info, ToRemove);
+          Changed |= tryToSimplifyOverflowMath(II, Info, ToRemove);
           continue;
         }
         auto *Cmp = dyn_cast<ICmpInst>(&I);
index 211635d..66f3e19 100644 (file)
@@ -11,7 +11,9 @@
 ; CHECK-NEXT: Running analysis: TargetIRAnalysis on ssub_no_overflow_due_to_or_conds
 ; CHECK-NEXT: Running analysis: DominatorTreeAnalysis on ssub_no_overflow_due_to_or_conds
 ; CHECK-NEXT: Running pass: ConstraintEliminationPass on ssub_no_overflow_due_to_or_conds
+; CHECK-NEXT: Invalidating analysis: DemandedBitsAnalysis on ssub_no_overflow_due_to_or_conds
 ; CHECK-NEXT: Running pass: RequireAnalysisPass
+; CHECK-NEXT: Running analysis: DemandedBitsAnalysis on ssub_no_overflow_due_to_or_conds
 
 ; CHECK-NEXT: Running pass: RequireAnalysisPass
 ; CHECK-NEXT: Running analysis: DemandedBitsAnalysis on uge_zext