[llvm] [DA] Widening SCEV expressions in strong SIV test to prevent overflow (PR #164704)

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 23 10:00:58 PDT 2025


================
@@ -1668,17 +1668,35 @@ bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
   LLVM_DEBUG(dbgs() << "\t    Delta = " << *Delta);
   LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
 
+  TypeSize CoeffSize =
+      Coeff->getType()->getScalarType()->getPrimitiveSizeInBits();
+  TypeSize SrcSize =
+      SrcConst->getType()->getScalarType()->getPrimitiveSizeInBits();
+  TypeSize DstSize =
+      DstConst->getType()->getScalarType()->getPrimitiveSizeInBits();
+  TypeSize WideSize = std::max(CoeffSize, std::max(SrcSize, DstSize)) * 2;
+  LLVMContext &Context = CurDstLoop->getHeader()->getParent()->getContext();
+  Type *WideTy = IntegerType::get(Context, WideSize);
+  const SCEV *WideSrcC = SE->getSignExtendExpr(SrcConst, WideTy);
+  const SCEV *WideDstC = SE->getSignExtendExpr(DstConst, WideTy);
+  const SCEV *WideDelta = SE->getMinusSCEV(WideSrcC, WideDstC);
+  const SCEV *WideCoeff = SE->getSignExtendExpr(Coeff, WideTy);
+
   // check that |Delta| < iteration count
-  if (const SCEV *UpperBound =
-          collectUpperBound(CurSrcLoop, Delta->getType())) {
-    LLVM_DEBUG(dbgs() << "\t    UpperBound = " << *UpperBound);
-    LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
-    const SCEV *AbsDelta =
-        SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
-    const SCEV *AbsCoeff =
-        SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
-    const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
-    if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
+  if (const SCEV *WideUpperBound =
+          collectUpperBound(CurSrcLoop, WideDelta->getType())) {
+    LLVM_DEBUG(dbgs() << "\t    WideUpperBound = " << *WideUpperBound);
+    LLVM_DEBUG(dbgs() << ", " << *WideUpperBound->getType() << "\n");
+
+    // FIXME: Use SCEV getAbsExpr function to compute the abstract values
----------------
kasuga-fj wrote:

JFYI: `Abs` means absolute value.

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


More information about the llvm-commits mailing list