[llvm] r250143 - [SCEV] Put some utilites in the ScalarEvolution class

Sanjoy Das via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 12 19:53:30 PDT 2015


Author: sanjoy
Date: Mon Oct 12 21:53:27 2015
New Revision: 250143

URL: http://llvm.org/viewvc/llvm-project?rev=250143&view=rev
Log:
[SCEV] Put some utilites in the ScalarEvolution class

In a later commit, `SplitBinaryAdd` will be used outside `IsConstDiff`,
so lift that out.  And lift out `IsConstDiff` as
`computeConstantDifference` to keep things clean and to avoid playing
C++ access specifier games.

NFC.

Modified:
    llvm/trunk/include/llvm/Analysis/ScalarEvolution.h
    llvm/trunk/lib/Analysis/ScalarEvolution.cpp

Modified: llvm/trunk/include/llvm/Analysis/ScalarEvolution.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/ScalarEvolution.h?rev=250143&r1=250142&r2=250143&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/ScalarEvolution.h (original)
+++ llvm/trunk/include/llvm/Analysis/ScalarEvolution.h Mon Oct 12 21:53:27 2015
@@ -581,6 +581,15 @@ namespace llvm {
     bool isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, const SCEV *LHS,
                                       const SCEV *RHS);
 
+    /// Try to match the Expr as "(L + R)<Flags>".
+    bool splitBinaryAdd(const SCEV *Expr, const SCEV *&L, const SCEV *&R,
+                        SCEV::NoWrapFlags &Flags);
+
+    /// Return true if More == (Less + C), where C is a constant.  This is
+    /// intended to be used as a cheaper substitute for full SCEV subtraction.
+    bool computeConstantDifference(const SCEV *Less, const SCEV *More,
+                                   APInt &C);
+
     /// Drop memoized information computed for S.
     void forgetMemoizedResults(const SCEV *S);
 

Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=250143&r1=250142&r2=250143&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Mon Oct 12 21:53:27 2015
@@ -7509,21 +7509,24 @@ bool ScalarEvolution::isImpliedCond(ICmp
   return false;
 }
 
-// Return true if More == (Less + C), where C is a constant.
-static bool IsConstDiff(ScalarEvolution &SE, const SCEV *Less, const SCEV *More,
-                        APInt &C) {
-  // We avoid subtracting expressions here because this function is usually
-  // fairly deep in the call stack (i.e. is called many times).
+bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr,
+                                     const SCEV *&L, const SCEV *&R,
+                                     SCEV::NoWrapFlags &Flags) {
+  const auto *AE = dyn_cast<SCEVAddExpr>(Expr);
+  if (!AE || AE->getNumOperands() != 2)
+    return false;
 
-  auto SplitBinaryAdd = [](const SCEV *Expr, const SCEV *&L, const SCEV *&R) {
-    const auto *AE = dyn_cast<SCEVAddExpr>(Expr);
-    if (!AE || AE->getNumOperands() != 2)
-      return false;
+  L = AE->getOperand(0);
+  R = AE->getOperand(1);
+  Flags = AE->getNoWrapFlags();
+  return true;
+}
 
-    L = AE->getOperand(0);
-    R = AE->getOperand(1);
-    return true;
-  };
+bool ScalarEvolution::computeConstantDifference(const SCEV *Less,
+                                                const SCEV *More,
+                                                APInt &C) {
+  // We avoid subtracting expressions here because this function is usually
+  // fairly deep in the call stack (i.e. is called many times).
 
   if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) {
     const auto *LAR = cast<SCEVAddRecExpr>(Less);
@@ -7537,7 +7540,7 @@ static bool IsConstDiff(ScalarEvolution
     if (!LAR->isAffine() || !MAR->isAffine())
       return false;
 
-    if (LAR->getStepRecurrence(SE) != MAR->getStepRecurrence(SE))
+    if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this))
       return false;
 
     Less = LAR->getStart();
@@ -7554,14 +7557,15 @@ static bool IsConstDiff(ScalarEvolution
   }
 
   const SCEV *L, *R;
-  if (SplitBinaryAdd(Less, L, R))
+  SCEV::NoWrapFlags Flags;
+  if (splitBinaryAdd(Less, L, R, Flags))
     if (const auto *LC = dyn_cast<SCEVConstant>(L))
       if (R == More) {
         C = -(LC->getValue()->getValue());
         return true;
       }
 
-  if (SplitBinaryAdd(More, L, R))
+  if (splitBinaryAdd(More, L, R, Flags))
     if (const auto *LC = dyn_cast<SCEVConstant>(L))
       if (R == Less) {
         C = LC->getValue()->getValue();
@@ -7626,8 +7630,8 @@ bool ScalarEvolution::isImpliedCondOpera
   // C)".
 
   APInt LDiff, RDiff;
-  if (!IsConstDiff(*this, FoundLHS, LHS, LDiff) ||
-      !IsConstDiff(*this, FoundRHS, RHS, RDiff) ||
+  if (!computeConstantDifference(FoundLHS, LHS, LDiff) ||
+      !computeConstantDifference(FoundRHS, RHS, RDiff) ||
       LDiff != RDiff)
     return false;
 




More information about the llvm-commits mailing list