LoopPass.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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/IR/IRPrintingPasses.h"
  17. #include "llvm/IR/LLVMContext.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/Timer.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace llvm;
  22. #define DEBUG_TYPE "loop-pass-manager"
  23. namespace {
  24. /// PrintLoopPass - Print a Function corresponding to a Loop.
  25. ///
  26. class PrintLoopPass : public LoopPass {
  27. private:
  28. std::string Banner;
  29. raw_ostream &Out; // raw_ostream to print on.
  30. public:
  31. static char ID;
  32. PrintLoopPass(const std::string &B, raw_ostream &o)
  33. : LoopPass(ID), Banner(B), Out(o) {}
  34. void getAnalysisUsage(AnalysisUsage &AU) const override {
  35. AU.setPreservesAll();
  36. }
  37. bool runOnLoop(Loop *L, LPPassManager &) override {
  38. Out << Banner;
  39. for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
  40. b != be;
  41. ++b) {
  42. if (*b)
  43. (*b)->print(Out);
  44. else
  45. Out << "Printing <null> block";
  46. }
  47. return false;
  48. }
  49. };
  50. char PrintLoopPass::ID = 0;
  51. }
  52. //===----------------------------------------------------------------------===//
  53. // LPPassManager
  54. //
  55. char LPPassManager::ID = 0;
  56. LPPassManager::LPPassManager()
  57. : FunctionPass(ID), PMDataManager() {
  58. skipThisLoop = false;
  59. redoThisLoop = false;
  60. LI = nullptr;
  61. CurrentLoop = nullptr;
  62. }
  63. /// Delete loop from the loop queue and loop hierarchy (LoopInfo).
  64. void LPPassManager::deleteLoopFromQueue(Loop *L) {
  65. LI->updateUnloop(L);
  66. // Notify passes that the loop is being deleted.
  67. deleteSimpleAnalysisLoop(L);
  68. // If L is current loop then skip rest of the passes and let
  69. // runOnFunction remove L from LQ. Otherwise, remove L from LQ now
  70. // and continue applying other passes on CurrentLoop.
  71. if (CurrentLoop == L)
  72. skipThisLoop = true;
  73. delete L;
  74. if (skipThisLoop)
  75. return;
  76. for (std::deque<Loop *>::iterator I = LQ.begin(),
  77. E = LQ.end(); I != E; ++I) {
  78. if (*I == L) {
  79. LQ.erase(I);
  80. break;
  81. }
  82. }
  83. }
  84. // Inset loop into loop nest (LoopInfo) and loop queue (LQ).
  85. void LPPassManager::insertLoop(Loop *L, Loop *ParentLoop) {
  86. assert (CurrentLoop != L && "Cannot insert CurrentLoop");
  87. // Insert into loop nest
  88. if (ParentLoop)
  89. ParentLoop->addChildLoop(L);
  90. else
  91. LI->addTopLevelLoop(L);
  92. insertLoopIntoQueue(L);
  93. }
  94. void LPPassManager::insertLoopIntoQueue(Loop *L) {
  95. // Insert L into loop queue
  96. if (L == CurrentLoop)
  97. redoLoop(L);
  98. else if (!L->getParentLoop())
  99. // This is top level loop.
  100. LQ.push_front(L);
  101. else {
  102. // Insert L after the parent loop.
  103. for (std::deque<Loop *>::iterator I = LQ.begin(),
  104. E = LQ.end(); I != E; ++I) {
  105. if (*I == L->getParentLoop()) {
  106. // deque does not support insert after.
  107. ++I;
  108. LQ.insert(I, 1, L);
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. // Reoptimize this loop. LPPassManager will re-insert this loop into the
  115. // queue. This allows LoopPass to change loop nest for the loop. This
  116. // utility may send LPPassManager into infinite loops so use caution.
  117. void LPPassManager::redoLoop(Loop *L) {
  118. assert (CurrentLoop == L && "Can redo only CurrentLoop");
  119. redoThisLoop = true;
  120. }
  121. /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
  122. /// all loop passes.
  123. void LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From,
  124. BasicBlock *To, Loop *L) {
  125. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  126. LoopPass *LP = getContainedPass(Index);
  127. LP->cloneBasicBlockAnalysis(From, To, L);
  128. }
  129. }
  130. /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
  131. void LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) {
  132. if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
  133. for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;
  134. ++BI) {
  135. Instruction &I = *BI;
  136. deleteSimpleAnalysisValue(&I, L);
  137. }
  138. }
  139. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  140. LoopPass *LP = getContainedPass(Index);
  141. LP->deleteAnalysisValue(V, L);
  142. }
  143. }
  144. /// Invoke deleteAnalysisLoop hook for all passes.
  145. void LPPassManager::deleteSimpleAnalysisLoop(Loop *L) {
  146. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  147. LoopPass *LP = getContainedPass(Index);
  148. LP->deleteAnalysisLoop(L);
  149. }
  150. }
  151. // Recurse through all subloops and all loops into LQ.
  152. static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
  153. LQ.push_back(L);
  154. for (Loop::reverse_iterator I = L->rbegin(), E = L->rend(); I != E; ++I)
  155. addLoopIntoQueue(*I, LQ);
  156. }
  157. /// Pass Manager itself does not invalidate any analysis info.
  158. void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
  159. // LPPassManager needs LoopInfo. In the long term LoopInfo class will
  160. // become part of LPPassManager.
  161. Info.addRequired<LoopInfoWrapperPass>();
  162. Info.setPreservesAll();
  163. }
  164. /// run - Execute all of the passes scheduled for execution. Keep track of
  165. /// whether any of the passes modifies the function, and if so, return true.
  166. bool LPPassManager::runOnFunction(Function &F) {
  167. auto &LIWP = getAnalysis<LoopInfoWrapperPass>();
  168. LI = &LIWP.getLoopInfo();
  169. bool Changed = false;
  170. // Collect inherited analysis from Module level pass manager.
  171. populateInheritedAnalysis(TPM->activeStack);
  172. // Populate the loop queue in reverse program order. There is no clear need to
  173. // process sibling loops in either forward or reverse order. There may be some
  174. // advantage in deleting uses in a later loop before optimizing the
  175. // definitions in an earlier loop. If we find a clear reason to process in
  176. // forward order, then a forward variant of LoopPassManager should be created.
  177. //
  178. // Note that LoopInfo::iterator visits loops in reverse program
  179. // order. Here, reverse_iterator gives us a forward order, and the LoopQueue
  180. // reverses the order a third time by popping from the back.
  181. for (LoopInfo::reverse_iterator I = LI->rbegin(), E = LI->rend(); I != E; ++I)
  182. addLoopIntoQueue(*I, LQ);
  183. if (LQ.empty()) // No loops, skip calling finalizers
  184. return false;
  185. // Initialization
  186. for (std::deque<Loop *>::const_iterator I = LQ.begin(), E = LQ.end();
  187. I != E; ++I) {
  188. Loop *L = *I;
  189. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  190. LoopPass *P = getContainedPass(Index);
  191. Changed |= P->doInitialization(L, *this);
  192. }
  193. }
  194. // Walk Loops
  195. while (!LQ.empty()) {
  196. CurrentLoop = LQ.back();
  197. skipThisLoop = false;
  198. redoThisLoop = false;
  199. // Run all passes on the current Loop.
  200. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  201. LoopPass *P = getContainedPass(Index);
  202. dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG,
  203. CurrentLoop->getHeader()->getName());
  204. dumpRequiredSet(P);
  205. initializeAnalysisImpl(P);
  206. {
  207. PassManagerPrettyStackEntry X(P, *CurrentLoop->getHeader());
  208. TimeRegion PassTimer(getPassTimer(P));
  209. Changed |= P->runOnLoop(CurrentLoop, *this);
  210. }
  211. if (Changed)
  212. dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG,
  213. skipThisLoop ? "<deleted>" :
  214. CurrentLoop->getHeader()->getName());
  215. dumpPreservedSet(P);
  216. if (!skipThisLoop) {
  217. // Manually check that this loop is still healthy. This is done
  218. // instead of relying on LoopInfo::verifyLoop since LoopInfo
  219. // is a function pass and it's really expensive to verify every
  220. // loop in the function every time. That level of checking can be
  221. // enabled with the -verify-loop-info option.
  222. {
  223. TimeRegion PassTimer(getPassTimer(&LIWP));
  224. CurrentLoop->verifyLoop();
  225. }
  226. // Then call the regular verifyAnalysis functions.
  227. verifyPreservedAnalysis(P);
  228. F.getContext().yield();
  229. }
  230. removeNotPreservedAnalysis(P);
  231. recordAvailableAnalysis(P);
  232. removeDeadPasses(P,
  233. skipThisLoop ? "<deleted>" :
  234. CurrentLoop->getHeader()->getName(),
  235. ON_LOOP_MSG);
  236. if (skipThisLoop)
  237. // Do not run other passes on this loop.
  238. break;
  239. }
  240. // If the loop was deleted, release all the loop passes. This frees up
  241. // some memory, and avoids trouble with the pass manager trying to call
  242. // verifyAnalysis on them.
  243. if (skipThisLoop)
  244. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  245. Pass *P = getContainedPass(Index);
  246. freePass(P, "<deleted>", ON_LOOP_MSG);
  247. }
  248. // Pop the loop from queue after running all passes.
  249. LQ.pop_back();
  250. if (redoThisLoop)
  251. LQ.push_back(CurrentLoop);
  252. }
  253. // Finalization
  254. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  255. LoopPass *P = getContainedPass(Index);
  256. Changed |= P->doFinalization();
  257. }
  258. return Changed;
  259. }
  260. /// Print passes managed by this manager
  261. void LPPassManager::dumpPassStructure(unsigned Offset) {
  262. errs().indent(Offset*2) << "Loop Pass Manager\n";
  263. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  264. Pass *P = getContainedPass(Index);
  265. P->dumpPassStructure(Offset + 1);
  266. dumpLastUses(P, Offset+1);
  267. }
  268. }
  269. //===----------------------------------------------------------------------===//
  270. // LoopPass
  271. Pass *LoopPass::createPrinterPass(raw_ostream &O,
  272. const std::string &Banner) const {
  273. return new PrintLoopPass(Banner, O);
  274. }
  275. // Check if this pass is suitable for the current LPPassManager, if
  276. // available. This pass P is not suitable for a LPPassManager if P
  277. // is not preserving higher level analysis info used by other
  278. // LPPassManager passes. In such case, pop LPPassManager from the
  279. // stack. This will force assignPassManager() to create new
  280. // LPPassManger as expected.
  281. void LoopPass::preparePassManager(PMStack &PMS) {
  282. // Find LPPassManager
  283. while (!PMS.empty() &&
  284. PMS.top()->getPassManagerType() > PMT_LoopPassManager)
  285. PMS.pop();
  286. // If this pass is destroying high level information that is used
  287. // by other passes that are managed by LPM then do not insert
  288. // this pass in current LPM. Use new LPPassManager.
  289. if (PMS.top()->getPassManagerType() == PMT_LoopPassManager &&
  290. !PMS.top()->preserveHigherLevelAnalysis(this))
  291. PMS.pop();
  292. }
  293. /// Assign pass manager to manage this pass.
  294. void LoopPass::assignPassManager(PMStack &PMS,
  295. PassManagerType PreferredType) {
  296. std::unique_ptr<LoopPass> thisPtr(this); // HLSL Change
  297. // Find LPPassManager
  298. while (!PMS.empty() &&
  299. PMS.top()->getPassManagerType() > PMT_LoopPassManager)
  300. PMS.pop();
  301. LPPassManager *LPPM;
  302. if (PMS.top()->getPassManagerType() == PMT_LoopPassManager)
  303. LPPM = (LPPassManager*)PMS.top();
  304. else {
  305. // Create new Loop Pass Manager if it does not exist.
  306. assert (!PMS.empty() && "Unable to create Loop Pass Manager");
  307. PMDataManager *PMD = PMS.top();
  308. // [1] Create new Loop Pass Manager
  309. LPPM = new LPPassManager();
  310. LPPM->populateInheritedAnalysis(PMS);
  311. // [2] Set up new manager's top level manager
  312. PMTopLevelManager *TPM = PMD->getTopLevelManager();
  313. TPM->addIndirectPassManager(LPPM);
  314. // [3] Assign manager to manage this new manager. This may create
  315. // and push new managers into PMS
  316. Pass *P = LPPM->getAsPass();
  317. TPM->schedulePass(P);
  318. // [4] Push new manager into PMS
  319. PMS.push(LPPM);
  320. }
  321. thisPtr.release(); // HLSL Change
  322. LPPM->add(this);
  323. }
  324. // Containing function has Attribute::OptimizeNone and transformation
  325. // passes should skip it.
  326. bool LoopPass::skipOptnoneFunction(const Loop *L) const {
  327. const Function *F = L->getHeader()->getParent();
  328. if (F && F->hasFnAttribute(Attribute::OptimizeNone)) {
  329. // FIXME: Report this to dbgs() only once per function.
  330. DEBUG(dbgs() << "Skipping pass '" << getPassName()
  331. << "' in function " << F->getName() << "\n");
  332. // FIXME: Delete loop from pass manager's queue?
  333. return true;
  334. }
  335. return false;
  336. }