[llvm] [SCEV] Support ule/sle exit counts via widening (PR #92206)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue May 14 19:29:16 PDT 2024


================
@@ -9198,8 +9198,21 @@ ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
     // Since the loop is finite, an invariant RHS cannot include the boundary
     // value, otherwise it would loop forever.
     if (!EnableFiniteLoopControl || !ControllingFiniteLoop ||
-        !isLoopInvariant(RHS, L))
-      break;
+        !isLoopInvariant(RHS, L)) {
+      // Otherwise, perform the addition in a wider type, to avoid overflow.
+      auto *OldType = dyn_cast<IntegerType>(LHS->getType());
+      if (!OldType)
+        break;
+      auto *NewType =
+          Type::getIntNTy(OldType->getContext(), OldType->getBitWidth() * 2);
----------------
nikic wrote:

Just adding +1 would be sufficient here, but I've opted to go for *2 which will likely result in friendlier IR if it gets expanded.

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


More information about the llvm-commits mailing list