Skip to content

Commit

Permalink
[LLE] Use the PredicatedScalarEvolution interface to query SCEVs for …
Browse files Browse the repository at this point in the history
…dependences

Summary:
LAA uses the PredicatedScalarEvolution interface, so it can produce
forward/backward dependences having SCEVs that are AddRecExprs only after being
transformed by PredicatedScalarEvolution.

Use PredicatedScalarEvolution to get the expected expressions.

Reviewers: anemet

Subscribers: llvm-commits, sanjoy

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255241 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
sbaranga-arm committed Dec 10, 2015
1 parent 9296968 commit f1cbc17
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions lib/Transforms/Scalar/LoopLoadElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct StoreToLoadForwardingCandidate {

/// \brief Return true if the dependence from the store to the load has a
/// distance of one. E.g. A[i+1] = A[i]
bool isDependenceDistanceOfOne(ScalarEvolution *SE) const {
bool isDependenceDistanceOfOne(PredicatedScalarEvolution &PSE) const {
Value *LoadPtr = Load->getPointerOperand();
Value *StorePtr = Store->getPointerOperand();
Type *LoadPtrType = LoadPtr->getType();
Expand All @@ -75,13 +75,13 @@ struct StoreToLoadForwardingCandidate {
auto &DL = Load->getParent()->getModule()->getDataLayout();
unsigned TypeByteSize = DL.getTypeAllocSize(const_cast<Type *>(LoadType));

auto *LoadPtrSCEV = cast<SCEVAddRecExpr>(SE->getSCEV(LoadPtr));
auto *StorePtrSCEV = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
auto *LoadPtrSCEV = cast<SCEVAddRecExpr>(PSE.getSCEV(LoadPtr));
auto *StorePtrSCEV = cast<SCEVAddRecExpr>(PSE.getSCEV(StorePtr));

// We don't need to check non-wrapping here because forward/backward
// dependence wouldn't be valid if these weren't monotonic accesses.
auto *Dist =
cast<SCEVConstant>(SE->getMinusSCEV(StorePtrSCEV, LoadPtrSCEV));
auto *Dist = cast<SCEVConstant>(
PSE.getSE()->getMinusSCEV(StorePtrSCEV, LoadPtrSCEV));
const APInt &Val = Dist->getValue()->getValue();
return Val.abs() == TypeByteSize;
}
Expand Down Expand Up @@ -114,8 +114,8 @@ bool doesStoreDominatesAllLatches(BasicBlock *StoreBlock, Loop *L,
class LoadEliminationForLoop {
public:
LoadEliminationForLoop(Loop *L, LoopInfo *LI, const LoopAccessInfo &LAI,
DominatorTree *DT, ScalarEvolution *SE)
: L(L), LI(LI), LAI(LAI), DT(DT), SE(SE) {}
DominatorTree *DT)
: L(L), LI(LI), LAI(LAI), DT(DT), PSE(LAI.PSE) {}

/// \brief Look through the loop-carried and loop-independent dependences in
/// this loop and find store->load dependences.
Expand Down Expand Up @@ -223,8 +223,8 @@ class LoadEliminationForLoop {
// block so deciding which one forwards is easy. The later one forwards
// as long as they both have a dependence distance of one to the load.
if (Cand.Store->getParent() == OtherCand->Store->getParent() &&
Cand.isDependenceDistanceOfOne(SE) &&
OtherCand->isDependenceDistanceOfOne(SE)) {
Cand.isDependenceDistanceOfOne(PSE) &&
OtherCand->isDependenceDistanceOfOne(PSE)) {
// They are in the same block, the later one will forward to the load.
if (getInstrIndex(OtherCand->Store) < getInstrIndex(Cand.Store))
OtherCand = &Cand;
Expand Down Expand Up @@ -372,7 +372,7 @@ class LoadEliminationForLoop {
// store %y, %gep_i_plus_1

Value *Ptr = Cand.Load->getPointerOperand();
auto *PtrSCEV = cast<SCEVAddRecExpr>(SE->getSCEV(Ptr));
auto *PtrSCEV = cast<SCEVAddRecExpr>(PSE.getSCEV(Ptr));
auto *PH = L->getLoopPreheader();
Value *InitialPtr = SEE.expandCodeFor(PtrSCEV->getStart(), Ptr->getType(),
PH->getTerminator());
Expand Down Expand Up @@ -436,7 +436,7 @@ class LoadEliminationForLoop {

// Check whether the SCEV difference is the same as the induction step,
// thus we load the value in the next iteration.
if (!Cand.isDependenceDistanceOfOne(SE))
if (!Cand.isDependenceDistanceOfOne(PSE))
continue;

++NumForwarding;
Expand Down Expand Up @@ -468,15 +468,15 @@ class LoadEliminationForLoop {
// Point of no-return, start the transformation. First, version the loop if
// necessary.
if (!Checks.empty() || !LAI.PSE.getUnionPredicate().isAlwaysTrue()) {
LoopVersioning LV(LAI, L, LI, DT, SE, false);
LoopVersioning LV(LAI, L, LI, DT, PSE.getSE(), false);
LV.setAliasChecks(std::move(Checks));
LV.setSCEVChecks(LAI.PSE.getUnionPredicate());
LV.versionLoop();
}

// Next, propagate the value stored by the store to the users of the load.
// Also for the first iteration, generate the initial value of the load.
SCEVExpander SEE(*SE, L->getHeader()->getModule()->getDataLayout(),
SCEVExpander SEE(*PSE.getSE(), L->getHeader()->getModule()->getDataLayout(),
"storeforward");
for (const auto &Cand : Candidates)
propagateStoredValueToLoadUsers(Cand, SEE);
Expand All @@ -496,7 +496,7 @@ class LoadEliminationForLoop {
LoopInfo *LI;
const LoopAccessInfo &LAI;
DominatorTree *DT;
ScalarEvolution *SE;
PredicatedScalarEvolution PSE;
};

/// \brief The pass. Most of the work is delegated to the per-loop
Expand All @@ -511,7 +511,6 @@ class LoopLoadElimination : public FunctionPass {
auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
auto *LAA = &getAnalysis<LoopAccessAnalysis>();
auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();

// Build up a worklist of inner-loops to vectorize. This is necessary as the
// act of distributing a loop creates new loops and can invalidate iterators
Expand All @@ -529,7 +528,7 @@ class LoopLoadElimination : public FunctionPass {
for (Loop *L : Worklist) {
const LoopAccessInfo &LAI = LAA->getInfo(L, ValueToValueMap());
// The actual work is performed by LoadEliminationForLoop.
LoadEliminationForLoop LEL(L, LI, LAI, DT, SE);
LoadEliminationForLoop LEL(L, LI, LAI, DT);
Changed |= LEL.processLoop();
}

Expand Down

0 comments on commit f1cbc17

Please sign in to comment.