comparison lib/Transforms/Scalar/LICM.cpp @ 83:60c9769439b8

LLVM 3.7
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Wed, 18 Feb 2015 14:55:36 +0900
parents 54457678186b
children afa8332a0e37
comparison
equal deleted inserted replaced
78:af83660cff7b 83:60c9769439b8
50 #include "llvm/IR/Metadata.h" 50 #include "llvm/IR/Metadata.h"
51 #include "llvm/IR/PredIteratorCache.h" 51 #include "llvm/IR/PredIteratorCache.h"
52 #include "llvm/Support/CommandLine.h" 52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/Debug.h" 53 #include "llvm/Support/Debug.h"
54 #include "llvm/Support/raw_ostream.h" 54 #include "llvm/Support/raw_ostream.h"
55 #include "llvm/Target/TargetLibraryInfo.h" 55 #include "llvm/Analysis/TargetLibraryInfo.h"
56 #include "llvm/Transforms/Utils/Local.h" 56 #include "llvm/Transforms/Utils/Local.h"
57 #include "llvm/Transforms/Utils/LoopUtils.h" 57 #include "llvm/Transforms/Utils/LoopUtils.h"
58 #include "llvm/Transforms/Utils/SSAUpdater.h" 58 #include "llvm/Transforms/Utils/SSAUpdater.h"
59 #include <algorithm> 59 #include <algorithm>
60 using namespace llvm; 60 using namespace llvm;
84 /// loop preheaders be inserted into the CFG... 84 /// loop preheaders be inserted into the CFG...
85 /// 85 ///
86 void getAnalysisUsage(AnalysisUsage &AU) const override { 86 void getAnalysisUsage(AnalysisUsage &AU) const override {
87 AU.setPreservesCFG(); 87 AU.setPreservesCFG();
88 AU.addRequired<DominatorTreeWrapperPass>(); 88 AU.addRequired<DominatorTreeWrapperPass>();
89 AU.addRequired<LoopInfo>(); 89 AU.addRequired<LoopInfoWrapperPass>();
90 AU.addRequiredID(LoopSimplifyID); 90 AU.addRequiredID(LoopSimplifyID);
91 AU.addPreservedID(LoopSimplifyID); 91 AU.addPreservedID(LoopSimplifyID);
92 AU.addRequiredID(LCSSAID); 92 AU.addRequiredID(LCSSAID);
93 AU.addPreservedID(LCSSAID); 93 AU.addPreservedID(LCSSAID);
94 AU.addRequired<AliasAnalysis>(); 94 AU.addRequired<AliasAnalysis>();
95 AU.addPreserved<AliasAnalysis>(); 95 AU.addPreserved<AliasAnalysis>();
96 AU.addPreserved<ScalarEvolution>(); 96 AU.addPreserved<ScalarEvolution>();
97 AU.addRequired<TargetLibraryInfo>(); 97 AU.addRequired<TargetLibraryInfoWrapperPass>();
98 } 98 }
99 99
100 using llvm::Pass::doFinalization; 100 using llvm::Pass::doFinalization;
101 101
102 bool doFinalization() override { 102 bool doFinalization() override {
118 Loop *CurLoop; // The current loop we are working on... 118 Loop *CurLoop; // The current loop we are working on...
119 AliasSetTracker *CurAST; // AliasSet information for the current loop... 119 AliasSetTracker *CurAST; // AliasSet information for the current loop...
120 bool MayThrow; // The current loop contains an instruction which 120 bool MayThrow; // The current loop contains an instruction which
121 // may throw, thus preventing code motion of 121 // may throw, thus preventing code motion of
122 // instructions with side effects. 122 // instructions with side effects.
123 bool HeaderMayThrow; // Same as previous, but specific to loop header
123 DenseMap<Loop*, AliasSetTracker*> LoopToAliasSetMap; 124 DenseMap<Loop*, AliasSetTracker*> LoopToAliasSetMap;
124 125
125 /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info. 126 /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info.
126 void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, 127 void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To,
127 Loop *L) override; 128 Loop *L) override;
128 129
129 /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias 130 /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias
130 /// set. 131 /// set.
131 void deleteAnalysisValue(Value *V, Loop *L) override; 132 void deleteAnalysisValue(Value *V, Loop *L) override;
133
134 /// Simple Analysis hook. Delete loop L from alias set map.
135 void deleteAnalysisLoop(Loop *L) override;
132 136
133 /// SinkRegion - Walk the specified region of the CFG (defined by all blocks 137 /// SinkRegion - Walk the specified region of the CFG (defined by all blocks
134 /// dominated by the specified block, and that are in the current loop) in 138 /// dominated by the specified block, and that are in the current loop) in
135 /// reverse depth first order w.r.t the DominatorTree. This allows us to 139 /// reverse depth first order w.r.t the DominatorTree. This allows us to
136 /// visit uses before definitions, allowing us to sink a loop body in one 140 /// visit uses before definitions, allowing us to sink a loop body in one
204 } 208 }
205 209
206 char LICM::ID = 0; 210 char LICM::ID = 0;
207 INITIALIZE_PASS_BEGIN(LICM, "licm", "Loop Invariant Code Motion", false, false) 211 INITIALIZE_PASS_BEGIN(LICM, "licm", "Loop Invariant Code Motion", false, false)
208 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 212 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
209 INITIALIZE_PASS_DEPENDENCY(LoopInfo) 213 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
210 INITIALIZE_PASS_DEPENDENCY(LoopSimplify) 214 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
211 INITIALIZE_PASS_DEPENDENCY(LCSSA) 215 INITIALIZE_PASS_DEPENDENCY(LCSSA)
212 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) 216 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
213 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) 217 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
214 INITIALIZE_AG_DEPENDENCY(AliasAnalysis) 218 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
215 INITIALIZE_PASS_END(LICM, "licm", "Loop Invariant Code Motion", false, false) 219 INITIALIZE_PASS_END(LICM, "licm", "Loop Invariant Code Motion", false, false)
216 220
217 Pass *llvm::createLICMPass() { return new LICM(); } 221 Pass *llvm::createLICMPass() { return new LICM(); }
218 222
225 return false; 229 return false;
226 230
227 Changed = false; 231 Changed = false;
228 232
229 // Get our Loop and Alias Analysis information... 233 // Get our Loop and Alias Analysis information...
230 LI = &getAnalysis<LoopInfo>(); 234 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
231 AA = &getAnalysis<AliasAnalysis>(); 235 AA = &getAnalysis<AliasAnalysis>();
232 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 236 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
233 237
234 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); 238 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
235 DL = DLP ? &DLP->getDataLayout() : nullptr; 239 DL = DLP ? &DLP->getDataLayout() : nullptr;
236 TLI = &getAnalysis<TargetLibraryInfo>(); 240 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
237 241
238 assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form."); 242 assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form.");
239 243
240 CurAST = new AliasSetTracker(*AA); 244 CurAST = new AliasSetTracker(*AA);
241 // Collect Alias info from subloops. 245 // Collect Alias info from subloops.
268 BasicBlock *BB = *I; 272 BasicBlock *BB = *I;
269 if (LI->getLoopFor(BB) == L) // Ignore blocks in subloops. 273 if (LI->getLoopFor(BB) == L) // Ignore blocks in subloops.
270 CurAST->add(*BB); // Incorporate the specified basic block 274 CurAST->add(*BB); // Incorporate the specified basic block
271 } 275 }
272 276
273 MayThrow = false; 277 HeaderMayThrow = false;
278 BasicBlock *Header = L->getHeader();
279 for (BasicBlock::iterator I = Header->begin(), E = Header->end();
280 (I != E) && !HeaderMayThrow; ++I)
281 HeaderMayThrow |= I->mayThrow();
282 MayThrow = HeaderMayThrow;
274 // TODO: We've already searched for instructions which may throw in subloops. 283 // TODO: We've already searched for instructions which may throw in subloops.
275 // We may want to reuse this information. 284 // We may want to reuse this information.
276 for (Loop::block_iterator BB = L->block_begin(), BBE = L->block_end(); 285 for (Loop::block_iterator BB = L->block_begin(), BBE = L->block_end();
277 (BB != BBE) && !MayThrow ; ++BB) 286 (BB != BBE) && !MayThrow ; ++BB)
278 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); 287 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end();
311 // loop used in the outer loop. 320 // loop used in the outer loop.
312 // FIXME: This is really heavy handed. It would be a bit better to use an 321 // FIXME: This is really heavy handed. It would be a bit better to use an
313 // SSAUpdater strategy during promotion that was LCSSA aware and reformed 322 // SSAUpdater strategy during promotion that was LCSSA aware and reformed
314 // it as it went. 323 // it as it went.
315 if (Changed) 324 if (Changed)
316 formLCSSARecursively(*L, *DT, getAnalysisIfAvailable<ScalarEvolution>()); 325 formLCSSARecursively(*L, *DT, LI,
326 getAnalysisIfAvailable<ScalarEvolution>());
317 } 327 }
318 328
319 // Check that neither this loop nor its parent have had LCSSA broken. LICM is 329 // Check that neither this loop nor its parent have had LCSSA broken. LICM is
320 // specifically moving instructions across the loop boundary and so it is 330 // specifically moving instructions across the loop boundary and so it is
321 // especially in need of sanity checking here. 331 // especially in need of sanity checking here.
439 449
440 // Loads from constant memory are always safe to move, even if they end up 450 // Loads from constant memory are always safe to move, even if they end up
441 // in the same alias set as something that ends up being modified. 451 // in the same alias set as something that ends up being modified.
442 if (AA->pointsToConstantMemory(LI->getOperand(0))) 452 if (AA->pointsToConstantMemory(LI->getOperand(0)))
443 return true; 453 return true;
444 if (LI->getMetadata("invariant.load")) 454 if (LI->getMetadata(LLVMContext::MD_invariant_load))
445 return true; 455 return true;
446 456
447 // Don't hoist loads which have may-aliased stores in loop. 457 // Don't hoist loads which have may-aliased stores in loop.
448 uint64_t Size = 0; 458 uint64_t Size = 0;
449 if (LI->getType()->isSized()) 459 if (LI->getType()->isSized())
653 return isGuaranteedToExecute(Inst); 663 return isGuaranteedToExecute(Inst);
654 } 664 }
655 665
656 bool LICM::isGuaranteedToExecute(Instruction &Inst) { 666 bool LICM::isGuaranteedToExecute(Instruction &Inst) {
657 667
668 // We have to check to make sure that the instruction dominates all
669 // of the exit blocks. If it doesn't, then there is a path out of the loop
670 // which does not execute this instruction, so we can't hoist it.
671
672 // If the instruction is in the header block for the loop (which is very
673 // common), it is always guaranteed to dominate the exit blocks. Since this
674 // is a common case, and can save some work, check it now.
675 if (Inst.getParent() == CurLoop->getHeader())
676 // If there's a throw in the header block, we can't guarantee we'll reach
677 // Inst.
678 return !HeaderMayThrow;
679
658 // Somewhere in this loop there is an instruction which may throw and make us 680 // Somewhere in this loop there is an instruction which may throw and make us
659 // exit the loop. 681 // exit the loop.
660 if (MayThrow) 682 if (MayThrow)
661 return false; 683 return false;
662
663 // Otherwise we have to check to make sure that the instruction dominates all
664 // of the exit blocks. If it doesn't, then there is a path out of the loop
665 // which does not execute this instruction, so we can't hoist it.
666
667 // If the instruction is in the header block for the loop (which is very
668 // common), it is always guaranteed to dominate the exit blocks. Since this
669 // is a common case, and can save some work, check it now.
670 if (Inst.getParent() == CurLoop->getHeader())
671 return true;
672 684
673 // Get the exit blocks for the current loop. 685 // Get the exit blocks for the current loop.
674 SmallVector<BasicBlock*, 8> ExitBlocks; 686 SmallVector<BasicBlock*, 8> ExitBlocks;
675 CurLoop->getExitBlocks(ExitBlocks); 687 CurLoop->getExitBlocks(ExitBlocks);
676 688
805 817
806 // We start with an alignment of one and try to find instructions that allow 818 // We start with an alignment of one and try to find instructions that allow
807 // us to prove better alignment. 819 // us to prove better alignment.
808 unsigned Alignment = 1; 820 unsigned Alignment = 1;
809 AAMDNodes AATags; 821 AAMDNodes AATags;
822 bool HasDedicatedExits = CurLoop->hasDedicatedExits();
810 823
811 // Check that all of the pointers in the alias set have the same type. We 824 // Check that all of the pointers in the alias set have the same type. We
812 // cannot (yet) promote a memory location that is loaded and stored in 825 // cannot (yet) promote a memory location that is loaded and stored in
813 // different sizes. While we are at it, collect alignment and AA info. 826 // different sizes. While we are at it, collect alignment and AA info.
814 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) { 827 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
839 if (UI->getOperand(1) != ASIV) 852 if (UI->getOperand(1) != ASIV)
840 continue; 853 continue;
841 assert(!store->isVolatile() && "AST broken"); 854 assert(!store->isVolatile() && "AST broken");
842 if (!store->isSimple()) 855 if (!store->isSimple())
843 return; 856 return;
857 // Don't sink stores from loops without dedicated block exits. Exits
858 // containing indirect branches are not transformed by loop simplify,
859 // make sure we catch that. An additional load may be generated in the
860 // preheader for SSA updater, so also avoid sinking when no preheader
861 // is available.
862 if (!HasDedicatedExits || !Preheader)
863 return;
844 864
845 // Note that we only check GuaranteedToExecute inside the store case 865 // Note that we only check GuaranteedToExecute inside the store case
846 // so that we do not introduce stores where they did not exist before 866 // so that we do not introduce stores where they did not exist before
847 // (which would break the LLVM concurrency model). 867 // (which would break the LLVM concurrency model).
848 868
941 if (!AST) 961 if (!AST)
942 return; 962 return;
943 963
944 AST->deleteValue(V); 964 AST->deleteValue(V);
945 } 965 }
966
967 /// Simple Analysis hook. Delete value L from alias set map.
968 void LICM::deleteAnalysisLoop(Loop *L) {
969 AliasSetTracker *AST = LoopToAliasSetMap.lookup(L);
970 if (!AST)
971 return;
972
973 delete AST;
974 LoopToAliasSetMap.erase(L);
975 }