LoopPass.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements LoopPass and LPPassManager. All loop optimization
  11. // and transformation passes are derived from LoopPass. LPPassManager is
  12. // responsible for managing LoopPasses.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Analysis/LoopPass.h"
  16. #include "llvm/Analysis/ScalarEvolution.h"
  17. using namespace llvm;
  18. //===----------------------------------------------------------------------===//
  19. // LPPassManager
  20. //
  21. char LPPassManager::ID = 0;
  22. /// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
  23. LPPassManager::LPPassManager(int Depth)
  24. : FunctionPass(&ID), PMDataManager(Depth) {
  25. skipThisLoop = false;
  26. redoThisLoop = false;
  27. LI = NULL;
  28. CurrentLoop = NULL;
  29. }
  30. /// Delete loop from the loop queue and loop hierarchy (LoopInfo).
  31. void LPPassManager::deleteLoopFromQueue(Loop *L) {
  32. if (Loop *ParentLoop = L->getParentLoop()) { // Not a top-level loop.
  33. // Reparent all of the blocks in this loop. Since BBLoop had a parent,
  34. // they are now all in it.
  35. for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
  36. I != E; ++I)
  37. if (LI->getLoopFor(*I) == L) // Don't change blocks in subloops.
  38. LI->changeLoopFor(*I, ParentLoop);
  39. // Remove the loop from its parent loop.
  40. for (Loop::iterator I = ParentLoop->begin(), E = ParentLoop->end();;
  41. ++I) {
  42. assert(I != E && "Couldn't find loop");
  43. if (*I == L) {
  44. ParentLoop->removeChildLoop(I);
  45. break;
  46. }
  47. }
  48. // Move all subloops into the parent loop.
  49. while (!L->empty())
  50. ParentLoop->addChildLoop(L->removeChildLoop(L->end()-1));
  51. } else {
  52. // Reparent all of the blocks in this loop. Since BBLoop had no parent,
  53. // they no longer in a loop at all.
  54. for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
  55. // Don't change blocks in subloops.
  56. if (LI->getLoopFor(L->getBlocks()[i]) == L) {
  57. LI->removeBlock(L->getBlocks()[i]);
  58. --i;
  59. }
  60. }
  61. // Remove the loop from the top-level LoopInfo object.
  62. for (LoopInfo::iterator I = LI->begin(), E = LI->end();; ++I) {
  63. assert(I != E && "Couldn't find loop");
  64. if (*I == L) {
  65. LI->removeLoop(I);
  66. break;
  67. }
  68. }
  69. // Move all of the subloops to the top-level.
  70. while (!L->empty())
  71. LI->addTopLevelLoop(L->removeChildLoop(L->end()-1));
  72. }
  73. delete L;
  74. // If L is current loop then skip rest of the passes and let
  75. // runOnFunction remove L from LQ. Otherwise, remove L from LQ now
  76. // and continue applying other passes on CurrentLoop.
  77. if (CurrentLoop == L) {
  78. skipThisLoop = true;
  79. return;
  80. }
  81. for (std::deque<Loop *>::iterator I = LQ.begin(),
  82. E = LQ.end(); I != E; ++I) {
  83. if (*I == L) {
  84. LQ.erase(I);
  85. break;
  86. }
  87. }
  88. }
  89. // Inset loop into loop nest (LoopInfo) and loop queue (LQ).
  90. void LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) {
  91. assert (CurrentLoop != L && "Cannot insert CurrentLoop");
  92. // Insert into loop nest
  93. if (ParentLoop)
  94. ParentLoop->addChildLoop(L);
  95. else
  96. LI->addTopLevelLoop(L);
  97. // Insert L into loop queue
  98. if (L == CurrentLoop)
  99. redoLoop(L);
  100. else if (!ParentLoop)
  101. // This is top level loop.
  102. LQ.push_front(L);
  103. else {
  104. // Insert L after ParentLoop
  105. for (std::deque<Loop *>::iterator I = LQ.begin(),
  106. E = LQ.end(); I != E; ++I) {
  107. if (*I == ParentLoop) {
  108. // deque does not support insert after.
  109. ++I;
  110. LQ.insert(I, 1, L);
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. // Reoptimize this loop. LPPassManager will re-insert this loop into the
  117. // queue. This allows LoopPass to change loop nest for the loop. This
  118. // utility may send LPPassManager into infinite loops so use caution.
  119. void LPPassManager::redoLoop(Loop *L) {
  120. assert (CurrentLoop == L && "Can redo only CurrentLoop");
  121. redoThisLoop = true;
  122. }
  123. /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
  124. /// all loop passes.
  125. void LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From,
  126. BasicBlock *To, Loop *L) {
  127. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  128. Pass *P = getContainedPass(Index);
  129. LoopPass *LP = dynamic_cast<LoopPass *>(P);
  130. LP->cloneBasicBlockAnalysis(From, To, L);
  131. }
  132. }
  133. /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
  134. void LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) {
  135. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  136. Pass *P = getContainedPass(Index);
  137. LoopPass *LP = dynamic_cast<LoopPass *>(P);
  138. LP->deleteAnalysisValue(V, L);
  139. }
  140. }
  141. // Recurse through all subloops and all loops into LQ.
  142. static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
  143. LQ.push_back(L);
  144. for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
  145. addLoopIntoQueue(*I, LQ);
  146. }
  147. /// Pass Manager itself does not invalidate any analysis info.
  148. void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
  149. // LPPassManager needs LoopInfo. In the long term LoopInfo class will
  150. // become part of LPPassManager.
  151. Info.addRequired<LoopInfo>();
  152. // Used by IndVar doInitialization.
  153. Info.addRequired<ScalarEvolution>();
  154. Info.setPreservesAll();
  155. }
  156. /// run - Execute all of the passes scheduled for execution. Keep track of
  157. /// whether any of the passes modifies the function, and if so, return true.
  158. bool LPPassManager::runOnFunction(Function &F) {
  159. LI = &getAnalysis<LoopInfo>();
  160. bool Changed = false;
  161. // Collect inherited analysis from Module level pass manager.
  162. populateInheritedAnalysis(TPM->activeStack);
  163. // Populate Loop Queue
  164. for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
  165. addLoopIntoQueue(*I, LQ);
  166. // Initialization
  167. for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
  168. I != E; ++I) {
  169. Loop *L = *I;
  170. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  171. Pass *P = getContainedPass(Index);
  172. LoopPass *LP = dynamic_cast<LoopPass *>(P);
  173. if (LP)
  174. Changed |= LP->doInitialization(L, *this);
  175. }
  176. }
  177. // Walk Loops
  178. while (!LQ.empty()) {
  179. CurrentLoop = LQ.back();
  180. skipThisLoop = false;
  181. redoThisLoop = false;
  182. // Run all passes on current SCC
  183. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  184. Pass *P = getContainedPass(Index);
  185. dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG, "");
  186. dumpRequiredSet(P);
  187. initializeAnalysisImpl(P);
  188. StartPassTimer(P);
  189. LoopPass *LP = dynamic_cast<LoopPass *>(P);
  190. assert (LP && "Invalid LPPassManager member");
  191. Changed |= LP->runOnLoop(CurrentLoop, *this);
  192. StopPassTimer(P);
  193. if (Changed)
  194. dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG, "");
  195. dumpPreservedSet(P);
  196. verifyPreservedAnalysis(LP);
  197. removeNotPreservedAnalysis(P);
  198. recordAvailableAnalysis(P);
  199. removeDeadPasses(P, "", ON_LOOP_MSG);
  200. // If dominator information is available then verify the info if requested.
  201. verifyDomInfo(*LP, F);
  202. if (skipThisLoop)
  203. // Do not run other passes on this loop.
  204. break;
  205. }
  206. // Pop the loop from queue after running all passes.
  207. LQ.pop_back();
  208. if (redoThisLoop)
  209. LQ.push_back(CurrentLoop);
  210. }
  211. // Finalization
  212. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  213. Pass *P = getContainedPass(Index);
  214. LoopPass *LP = dynamic_cast <LoopPass *>(P);
  215. if (LP)
  216. Changed |= LP->doFinalization();
  217. }
  218. return Changed;
  219. }
  220. //===----------------------------------------------------------------------===//
  221. // LoopPass
  222. // Check if this pass is suitable for the current LPPassManager, if
  223. // available. This pass P is not suitable for a LPPassManager if P
  224. // is not preserving higher level analysis info used by other
  225. // LPPassManager passes. In such case, pop LPPassManager from the
  226. // stack. This will force assignPassManager() to create new
  227. // LPPassManger as expected.
  228. void LoopPass::preparePassManager(PMStack &PMS) {
  229. // Find LPPassManager
  230. while (!PMS.empty() &&
  231. PMS.top()->getPassManagerType() > PMT_LoopPassManager)
  232. PMS.pop();
  233. LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
  234. // If this pass is destroying high level information that is used
  235. // by other passes that are managed by LPM then do not insert
  236. // this pass in current LPM. Use new LPPassManager.
  237. if (LPPM && !LPPM->preserveHigherLevelAnalysis(this))
  238. PMS.pop();
  239. }
  240. /// Assign pass manager to manage this pass.
  241. void LoopPass::assignPassManager(PMStack &PMS,
  242. PassManagerType PreferredType) {
  243. // Find LPPassManager
  244. while (!PMS.empty() &&
  245. PMS.top()->getPassManagerType() > PMT_LoopPassManager)
  246. PMS.pop();
  247. LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
  248. // Create new Loop Pass Manager if it does not exist.
  249. if (!LPPM) {
  250. assert (!PMS.empty() && "Unable to create Loop Pass Manager");
  251. PMDataManager *PMD = PMS.top();
  252. // [1] Create new Call Graph Pass Manager
  253. LPPM = new LPPassManager(PMD->getDepth() + 1);
  254. LPPM->populateInheritedAnalysis(PMS);
  255. // [2] Set up new manager's top level manager
  256. PMTopLevelManager *TPM = PMD->getTopLevelManager();
  257. TPM->addIndirectPassManager(LPPM);
  258. // [3] Assign manager to manage this new manager. This may create
  259. // and push new managers into PMS
  260. Pass *P = dynamic_cast<Pass *>(LPPM);
  261. TPM->schedulePass(P);
  262. // [4] Push new manager into PMS
  263. PMS.push(LPPM);
  264. }
  265. LPPM->add(this);
  266. }