diff options
| author | Max Kazantsev <max.kazantsev@azul.com> | 2017-07-28 06:42:15 +0000 |
|---|---|---|
| committer | Max Kazantsev <max.kazantsev@azul.com> | 2017-07-28 06:42:15 +0000 |
| commit | fa4969539ad13bfd2c67caa3b206754cef373ba6 (patch) | |
| tree | 57735d3e5470018dd0427ceb986ae06293f985a8 /llvm/lib | |
| parent | 30caae6d23d92a72126cdb2223bd5d2155fd8544 (diff) | |
| download | bcm5719-llvm-fa4969539ad13bfd2c67caa3b206754cef373ba6.tar.gz bcm5719-llvm-fa4969539ad13bfd2c67caa3b206754cef373ba6.zip | |
[SCEV] Do not visit nodes twice in containsConstantSomewhere
This patch reworks the function that searches constants in Add and Mul SCEV expression
chains so that now it does not visit a node more than once, and also renames this function
for better correspondence between its implementation and semantics.
Differential Revision: https://reviews.llvm.org/D35931
llvm-svn: 309367
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/Analysis/ScalarEvolution.cpp | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index 4d7b59c4763..ecd124a576c 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -2676,20 +2676,23 @@ static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) { /// Determine if any of the operands in this SCEV are a constant or if /// any of the add or multiply expressions in this SCEV contain a constant. -static bool containsConstantSomewhere(const SCEV *StartExpr) { - SmallVector<const SCEV *, 4> Ops; - Ops.push_back(StartExpr); - while (!Ops.empty()) { - const SCEV *CurrentExpr = Ops.pop_back_val(); - if (isa<SCEVConstant>(*CurrentExpr)) - return true; +static bool containsConstantInAddMulChain(const SCEV *StartExpr) { + struct FindConstantInAddMulChain { + bool FoundConstant = false; - if (isa<SCEVAddExpr>(*CurrentExpr) || isa<SCEVMulExpr>(*CurrentExpr)) { - const auto *CurrentNAry = cast<SCEVNAryExpr>(CurrentExpr); - Ops.append(CurrentNAry->op_begin(), CurrentNAry->op_end()); + bool follow(const SCEV *S) { + FoundConstant |= isa<SCEVConstant>(S); + return isa<SCEVAddExpr>(S) || isa<SCEVMulExpr>(S); } - } - return false; + bool isDone() const { + return FoundConstant; + } + }; + + FindConstantInAddMulChain F; + SCEVTraversal<FindConstantInAddMulChain> ST(F); + ST.visitAll(StartExpr); + return F.FoundConstant; } /// Get a canonical multiply expression, or something simpler if possible. @@ -2726,7 +2729,11 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, // If any of Add's ops are Adds or Muls with a constant, // apply this transformation as well. if (Add->getNumOperands() == 2) - if (containsConstantSomewhere(Add)) + // TODO: There are some cases where this transformation is not + // profitable, for example: + // Add = (C0 + X) * Y + Z. + // Maybe the scope of this transformation should be narrowed down. + if (containsConstantInAddMulChain(Add)) return getAddExpr(getMulExpr(LHSC, Add->getOperand(0), SCEV::FlagAnyWrap, Depth + 1), getMulExpr(LHSC, Add->getOperand(1), |

