[PATCH] D104319: [SCEV] Retain AddExpr flags when subtracting a foldable constant.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 15 13:28:27 PDT 2021


fhahn created this revision.
fhahn added reviewers: efriedma, nikic, reames, mkazantsev.
Herald added a subscriber: hiraditya.
fhahn requested review of this revision.
Herald added a project: LLVM.

Currently we drop wrapping flags for expressions like (A + C1)<flags> - C2.
If C1 >= 0, C2 >= 0 and C1 >= C2, we should be able to fold (C1 - C2),
which will be >= 0 && <= C1, so we should be able to retain the flags of
the original expression. The key here is that we fold (C1 - C2) to a
non-negative constant.

I would appreciate a careful look in case I am missing a case. The logic
at the moment is quite specialized, do you think this could/should be
generalized?

This can improve results after using `SimplifyICmpOperands`, which may
subtract one in order to use stricter predicates, as is the case for
`isKnownPredicate`.

Simple example with Alive2. Not how to best construct an example without
concrete constants.

https://alive2.llvm.org/ce/z/QtNrFd


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104319

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Analysis/LoopAccessAnalysis/reverse-memcheck-bounds.ll
  llvm/test/Transforms/IndVarSimplify/eliminate-exit-no-dl.ll


Index: llvm/test/Transforms/IndVarSimplify/eliminate-exit-no-dl.ll
===================================================================
--- llvm/test/Transforms/IndVarSimplify/eliminate-exit-no-dl.ll
+++ llvm/test/Transforms/IndVarSimplify/eliminate-exit-no-dl.ll
@@ -17,8 +17,7 @@
 ; CHECK-NEXT:    [[TMP:%.*]] = phi i8* [ [[TMP4:%.*]], [[BB7:%.*]] ], [ getelementptr inbounds ([0 x i8], [0 x i8]* @global, i64 0, i64 2), [[BB:%.*]] ]
 ; CHECK-NEXT:    [[TMP4]] = getelementptr inbounds i8, i8* [[TMP]], i64 -1
 ; CHECK-NEXT:    [[TMP6:%.*]] = load i8, i8* [[TMP4]], align 1
-; CHECK-NEXT:    [[TMP5:%.*]] = icmp ugt i8* [[TMP4]], getelementptr inbounds ([0 x i8], [0 x i8]* @global, i64 0, i64 500)
-; CHECK-NEXT:    br i1 [[TMP5]], label [[BB7]], label [[BB11:%.*]]
+; CHECK-NEXT:    br i1 false, label [[BB7]], label [[BB11:%.*]]
 ; CHECK:       bb7:
 ; CHECK-NEXT:    [[TMP8:%.*]] = zext i8 [[TMP6]] to i64
 ; CHECK-NEXT:    br i1 true, label [[BB11]], label [[BB3]]
Index: llvm/test/Analysis/LoopAccessAnalysis/reverse-memcheck-bounds.ll
===================================================================
--- llvm/test/Analysis/LoopAccessAnalysis/reverse-memcheck-bounds.ll
+++ llvm/test/Analysis/LoopAccessAnalysis/reverse-memcheck-bounds.ll
@@ -16,7 +16,7 @@
 target triple = "aarch64--linux-gnueabi"
 
 ; CHECK: function 'f':
-; CHECK: (Low: (20000 + %a) High: (60004 + %a))
+; CHECK: (Low: (20000 + %a)<nuw> High: (60004 + %a))
 
 @B = common global i32* null, align 8
 @A = common global i32* null, align 8
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -2537,6 +2537,25 @@
     }
   }
 
+  if (Ops.size() == 2 && OrigFlags == SCEV::FlagAnyWrap) {
+    // Check if we have ((X + C1) - C2), where C1 >= 0, C2 >= 0 and C1 >= C2. In
+    // that case, we can fold (C1 - C2) and retain the wrapping flags of (A +
+    // C1).
+    const SCEV *A = Ops[0];
+    const SCEV *B = Ops[1];
+    auto *AddExpr = dyn_cast<SCEVAddExpr>(B);
+    auto *C = dyn_cast<SCEVConstant>(A);
+    if (AddExpr && C && AddExpr->getNumOperands() == 2 &&
+        isa<SCEVConstant>(AddExpr->getOperand(0))) {
+      auto C1 = cast<SCEVConstant>(AddExpr->getOperand(0))->getAPInt();
+      auto C2 = C->getAPInt();
+      if (C1.isNonNegative() && C2.isNegative() && C2.abs().ule(C1)) {
+        return getAddExpr(AddExpr->getOperand(1), getConstant(C1 + C2),
+                          AddExpr->getNoWrapFlags());
+      }
+    }
+  }
+
   // Skip past any other cast SCEVs.
   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
     ++Idx;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104319.352230.patch
Type: text/x-patch
Size: 2691 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210615/52bf4528/attachment.bin>


More information about the llvm-commits mailing list