From 1cf9f8137160e4dca68dff0310f0292d1ec75e8e Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Tue, 13 Oct 2015 02:53:27 +0000 Subject: [PATCH] [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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@250143 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Analysis/ScalarEvolution.h | 9 ++++++ lib/Analysis/ScalarEvolution.cpp | 40 ++++++++++++++----------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/include/llvm/Analysis/ScalarEvolution.h b/include/llvm/Analysis/ScalarEvolution.h index fe2ceb94d61..d70f9d6fe42 100644 --- a/include/llvm/Analysis/ScalarEvolution.h +++ b/include/llvm/Analysis/ScalarEvolution.h @@ -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)". + 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); diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index fc7e09d88ba..a1a4f2501fa 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -7509,21 +7509,24 @@ bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, 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(Expr); + if (!AE || AE->getNumOperands() != 2) + return false; - auto SplitBinaryAdd = [](const SCEV *Expr, const SCEV *&L, const SCEV *&R) { - const auto *AE = dyn_cast(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(Less) && isa(More)) { const auto *LAR = cast(Less); @@ -7537,7 +7540,7 @@ static bool IsConstDiff(ScalarEvolution &SE, const SCEV *Less, const SCEV *More, 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 &SE, const SCEV *Less, const SCEV *More, } const SCEV *L, *R; - if (SplitBinaryAdd(Less, L, R)) + SCEV::NoWrapFlags Flags; + if (splitBinaryAdd(Less, L, R, Flags)) if (const auto *LC = dyn_cast(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(L)) if (R == Less) { C = LC->getValue()->getValue(); @@ -7626,8 +7630,8 @@ bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow( // 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; -- 2.34.1