[SCEV] Refactor out isHighCostExpansion. NFCI.

Summary:
Move isHighCostExpansion from IndVarSimplify to SCEVExpander.  This
exposed function will be used in a subsequent change.

Reviewers: bogner, atrick

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D8995

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@234844 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sanjoy Das
2015-04-14 03:20:28 +00:00
parent 41ed49389b
commit dee1e82714
3 changed files with 74 additions and 56 deletions

View File

@@ -23,6 +23,7 @@
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
@@ -1804,6 +1805,61 @@ unsigned SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
return NumElim;
}
bool SCEVExpander::isHighCostExpansionHelper(
const SCEV *S, Loop *L, SmallPtrSetImpl<const SCEV *> &Processed) {
if (!Processed.insert(S).second)
return false;
// If the backedge-taken count is a UDiv, it's very likely a UDiv that
// ScalarEvolution's HowFarToZero or HowManyLessThans produced to compute a
// precise expression, rather than a UDiv from the user's code. If we can't
// find a UDiv in the code with some simple searching, assume the former and
// forego rewriting the loop.
if (isa<SCEVUDivExpr>(S)) {
BasicBlock *ExitingBB = L->getExitingBlock();
if (!ExitingBB)
return true;
BranchInst *ExitingBI = dyn_cast<BranchInst>(ExitingBB->getTerminator());
if (!ExitingBI || !ExitingBI->isConditional())
return true;
ICmpInst *OrigCond = dyn_cast<ICmpInst>(ExitingBI->getCondition());
if (!OrigCond)
return true;
const SCEV *RHS = SE.getSCEV(OrigCond->getOperand(1));
RHS = SE.getMinusSCEV(RHS, SE.getConstant(RHS->getType(), 1));
if (RHS != S) {
const SCEV *LHS = SE.getSCEV(OrigCond->getOperand(0));
LHS = SE.getMinusSCEV(LHS, SE.getConstant(LHS->getType(), 1));
if (LHS != S)
return true;
}
}
// Recurse past add expressions, which commonly occur in the
// BackedgeTakenCount. They may already exist in program code, and if not,
// they are not too expensive rematerialize.
if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
I != E; ++I) {
if (isHighCostExpansionHelper(*I, L, Processed))
return true;
}
return false;
}
// HowManyLessThans uses a Max expression whenever the loop is not guarded by
// the exit condition.
if (isa<SCEVSMaxExpr>(S) || isa<SCEVUMaxExpr>(S))
return true;
// If we haven't recognized an expensive SCEV pattern, assume it's an
// expression produced by program code.
return false;
}
namespace {
// Search for a SCEV subexpression that is not safe to expand. Any expression
// that may expand to a !isSafeToSpeculativelyExecute value is unsafe, namely