LoopPass.cpp 12 KB

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