diff options
author | Junmo Park <junmoz.park@samsung.com> | 2016-02-16 06:46:58 +0000 |
---|---|---|
committer | Junmo Park <junmoz.park@samsung.com> | 2016-02-16 06:46:58 +0000 |
commit | 6ebdc14cf1b240bbd28c46564d50b7ea1f33ecb3 (patch) | |
tree | a561a9a5328a468ff10bb1fd5f5993828afd1326 /llvm/lib | |
parent | e89a1795987327106de20c781fc841d21b9ce7b4 (diff) | |
download | llvm-6ebdc14cf1b240bbd28c46564d50b7ea1f33ecb3.zip llvm-6ebdc14cf1b240bbd28c46564d50b7ea1f33ecb3.tar.gz llvm-6ebdc14cf1b240bbd28c46564d50b7ea1f33ecb3.tar.bz2 |
[SCEVExpander] Make findExistingExpansion smarter
Summary:
Extending findExistingExpansion can use existing value in ExprValueMap.
This patch gives 0.3~0.5% performance improvements on
benchmarks(test-suite, spec2000, spec2006, commercial benchmark)
Reviewers: mzolotukhin, sanjoy, zzheng
Differential Revision: http://reviews.llvm.org/D15559
llvm-svn: 260938
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Analysis/ScalarEvolutionExpander.cpp | 60 | ||||
-rw-r--r-- | llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp | 8 |
2 files changed, 40 insertions, 28 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp index 45b0ada..58c0c33 100644 --- a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp +++ b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp @@ -1598,6 +1598,34 @@ Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty) { return V; } +Value *SCEVExpander::FindValueInExprValueMap(const SCEV *S, + const Instruction *InsertPt) { + SetVector<Value *> *Set = SE.getSCEVValues(S); + // If the expansion is not in CanonicalMode, and the SCEV contains any + // sub scAddRecExpr type SCEV, it is required to expand the SCEV literally. + if (CanonicalMode || !SE.containsAddRecurrence(S)) { + // If S is scConstant, it may be worse to reuse an existing Value. + if (S->getSCEVType() != scConstant && Set) { + // Choose a Value from the set which dominates the insertPt. + // insertPt should be inside the Value's parent loop so as not to break + // the LCSSA form. + for (auto const &Ent : *Set) { + Instruction *EntInst = nullptr; + if (Ent && isa<Instruction>(Ent) && + (EntInst = cast<Instruction>(Ent)) && + S->getType() == Ent->getType() && + EntInst->getFunction() == InsertPt->getFunction() && + SE.DT.dominates(EntInst, InsertPt) && + (SE.LI.getLoopFor(EntInst->getParent()) == nullptr || + SE.LI.getLoopFor(EntInst->getParent())->contains(InsertPt))) { + return Ent; + } + } + } + } + return nullptr; +} + // The expansion of SCEV will either reuse a previous Value in ExprValueMap, // or expand the SCEV literally. Specifically, if the expansion is in LSRMode, // and the SCEV contains any sub scAddRecExpr type SCEV, it will be expanded @@ -1643,31 +1671,8 @@ Value *SCEVExpander::expand(const SCEV *S) { Builder.SetInsertPoint(InsertPt); // Expand the expression into instructions. - SetVector<Value *> *Set = SE.getSCEVValues(S); - Value *V = nullptr; - // If the expansion is not in CanonicalMode, and the SCEV contains any - // sub scAddRecExpr type SCEV, it is required to expand the SCEV literally. - if (CanonicalMode || !SE.containsAddRecurrence(S)) { - // If S is scConstant, it may be worse to reuse an existing Value. - if (S->getSCEVType() != scConstant && Set) { - // Choose a Value from the set which dominates the insertPt. - // insertPt should be inside the Value's parent loop so as not to break - // the LCSSA form. - for (auto const &Ent : *Set) { - Instruction *EntInst = nullptr; - if (Ent && isa<Instruction>(Ent) && - (EntInst = cast<Instruction>(Ent)) && - S->getType() == Ent->getType() && - EntInst->getFunction() == InsertPt->getFunction() && - SE.DT.dominates(EntInst, InsertPt) && - (SE.LI.getLoopFor(EntInst->getParent()) == nullptr || - SE.LI.getLoopFor(EntInst->getParent())->contains(InsertPt))) { - V = Ent; - break; - } - } - } - } + Value *V = FindValueInExprValueMap(S, InsertPt); + if (!V) V = visit(S); @@ -1877,6 +1882,11 @@ Value *SCEVExpander::findExistingExpansion(const SCEV *S, return RHS; } + // Use expand's logic which is used for reusing a previous Value in + // ExprValueMap. + if (Value *Val = FindValueInExprValueMap(S, At)) + return Val; + // There is potential to make this significantly smarter, but this simple // heuristic already gets some interesting cases. diff --git a/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp b/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp index ec0e491b..232707d 100644 --- a/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp +++ b/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp @@ -311,9 +311,12 @@ bool llvm::UnrollRuntimeLoopProlog(Loop *L, unsigned Count, return false; BasicBlock *Header = L->getHeader(); + BasicBlock *PH = L->getLoopPreheader(); + BranchInst *PreHeaderBR = cast<BranchInst>(PH->getTerminator()); const DataLayout &DL = Header->getModule()->getDataLayout(); SCEVExpander Expander(*SE, DL, "loop-unroll"); - if (!AllowExpensiveTripCount && Expander.isHighCostExpansion(TripCountSC, L)) + if (!AllowExpensiveTripCount && + Expander.isHighCostExpansion(TripCountSC, L, PreHeaderBR)) return false; // We only handle cases when the unroll factor is a power of 2. @@ -331,13 +334,12 @@ bool llvm::UnrollRuntimeLoopProlog(Loop *L, unsigned Count, if (Loop *ParentLoop = L->getParentLoop()) SE->forgetLoop(ParentLoop); - BasicBlock *PH = L->getLoopPreheader(); BasicBlock *Latch = L->getLoopLatch(); // It helps to split the original preheader twice, one for the end of the // prolog code and one for a new loop preheader. BasicBlock *PEnd = SplitEdge(PH, Header, DT, LI); BasicBlock *NewPH = SplitBlock(PEnd, PEnd->getTerminator(), DT, LI); - BranchInst *PreHeaderBR = cast<BranchInst>(PH->getTerminator()); + PreHeaderBR = cast<BranchInst>(PH->getTerminator()); // Compute the number of extra iterations required, which is: // extra iterations = run-time trip count % (loop unroll factor + 1) |