[llvm] [ConstraintElim] Derive signed facts for post-increment inductions. (PR #210079)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 21 03:04:17 PDT 2026


================
@@ -1074,38 +1074,42 @@ void State::addInfoForInductions(BasicBlock &BB) {
   }
 
   Value *LowerBound = StartValue;
+  bool UnsignedInBounds = true, SignedInBounds = true;
   if (IncStep) {
-    // Adjust lower bound when dealing with a post-increment value.
     auto *StartC = dyn_cast<ConstantInt>(StartValue);
     if (!StartC)
       return;
-    bool Overflow = false;
-    APInt Sum = StartC->getValue().uadd_ov(*StepOffset, Overflow);
-    if (Overflow)
-      return;
+    bool UOverflow = false, SOverflow = false;
+    APInt Sum = StartC->getValue().uadd_ov(*StepOffset, UOverflow);
+    (void)StartC->getValue().sadd_ov(*StepOffset, SOverflow);
     LowerBound = ConstantInt::get(StartValue->getType(), Sum);
+    UnsignedInBounds = !UOverflow;
+    SignedInBounds = !SOverflow;
   }
 
-  // AR may wrap. Add PN >= StartValue conditional on LowerBound <= B which
+  // AR may wrap. Add PN >= StartValue conditional on LowerBound <= B, which
   // guarantees that the loop exits before wrapping in combination with the
   // restrictions on B and the step above.
-  if (!MonotonicallyIncreasingUnsigned)
+  if (!MonotonicallyIncreasingUnsigned && UnsignedInBounds)
     WorkList.push_back(FactOrCheck::getConditionFact(
         DTN, CmpInst::ICMP_UGE, PN, StartValue,
         ConditionTy(CmpInst::ICMP_ULE, LowerBound, B)));
-  // Only unsigned facts are derived for the post-increment path.
-  if (!MonotonicallyIncreasingSigned && !IncStep)
+  if (!MonotonicallyIncreasingSigned && SignedInBounds)
     WorkList.push_back(FactOrCheck::getConditionFact(
         DTN, CmpInst::ICMP_SGE, PN, StartValue,
-        ConditionTy(CmpInst::ICMP_SLE, StartValue, B)));
+        ConditionTy(CmpInst::ICMP_SLE, LowerBound, B)));
----------------
antoniofrighetto wrote:

Would extracting the preconditions (IV starting at or below the exit bound) into dedicated variables for each domain improve readability? (I think then we could drop `Precond` variable below).

https://github.com/llvm/llvm-project/pull/210079


More information about the llvm-commits mailing list