[PATCH] D148618: [SCEV] Support sub in willNotOverflow

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 18 01:51:44 PDT 2023


mkazantsev created this revision.
mkazantsev added reviewers: nikic, fhahn, reames, dmakogon, lebedev.ri.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
mkazantsev requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Basically overflow check with sub is the same as overflow check with
add and negated constant.


https://reviews.llvm.org/D148618

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Transforms/IndVarSimplify/infer-poison-flags.ll


Index: llvm/test/Transforms/IndVarSimplify/infer-poison-flags.ll
===================================================================
--- llvm/test/Transforms/IndVarSimplify/infer-poison-flags.ll
+++ llvm/test/Transforms/IndVarSimplify/infer-poison-flags.ll
@@ -144,7 +144,7 @@
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       loop:
 ; CHECK-NEXT:    [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[I_NEXT:%.*]], [[LOOP]] ]
-; CHECK-NEXT:    [[I_NEXT]] = sub nsw i32 [[I]], -1
+; CHECK-NEXT:    [[I_NEXT]] = sub nuw nsw i32 [[I]], -1
 ; CHECK-NEXT:    store i32 [[I]], ptr @A, align 4
 ; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[I_NEXT]], 1000
 ; CHECK-NEXT:    br i1 [[C]], label [[LOOP]], label [[LOOPEXIT:%.*]]
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -2340,16 +2340,18 @@
   // Can we use context to prove the fact we need?
   if (!CtxI)
     return false;
-  // We can prove that add(x, constant) doesn't wrap if isKnownPredicateAt can
-  // guarantee that x <= max_int - constant at the given context.
-  // TODO: Support other operations.
-  if (BinOp != Instruction::Add)
-    return false;
   auto *RHSC = dyn_cast<SCEVConstant>(RHS);
   // TODO: Lift this limitation.
   if (!RHSC)
     return false;
   APInt C = RHSC->getAPInt();
+  // We can prove that add(x, constant) doesn't wrap if isKnownPredicateAt can
+  // guarantee that x <= max_int - constant at the given context.
+  // TODO: Support mul.
+  if (BinOp == Instruction::Sub)
+    C = -C;
+  else if (BinOp != Instruction::Add)
+    return false;
   // TODO: Also lift this limitation.
   if (Signed && C.isNegative())
     return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148618.514565.patch
Type: text/x-patch
Size: 1773 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230418/e80e2315/attachment.bin>


More information about the llvm-commits mailing list