LoopInfo.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
  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 defines the LoopInfo class that is used to identify natural loops
  11. // and determine the loop depth of various nodes of the CFG. Note that the
  12. // loops identified may actually be several natural loops that share the same
  13. // header node... not just a single natural loop.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/ADT/DepthFirstIterator.h"
  18. #include "llvm/ADT/ScopeExit.h"
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. #include "llvm/Analysis/LoopInfoImpl.h"
  21. #include "llvm/Analysis/LoopIterator.h"
  22. #include "llvm/Analysis/ValueTracking.h"
  23. #include "llvm/Config/llvm-config.h"
  24. #include "llvm/IR/CFG.h"
  25. #include "llvm/IR/Constants.h"
  26. #include "llvm/IR/DebugLoc.h"
  27. #include "llvm/IR/Dominators.h"
  28. #include "llvm/IR/IRPrintingPasses.h"
  29. #include "llvm/IR/Instructions.h"
  30. #include "llvm/IR/LLVMContext.h"
  31. #include "llvm/IR/Metadata.h"
  32. #include "llvm/IR/PassManager.h"
  33. #include "llvm/Support/CommandLine.h"
  34. #include "llvm/Support/Debug.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. #include <algorithm>
  37. using namespace llvm;
  38. // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
  39. template class llvm::LoopBase<BasicBlock, Loop>;
  40. template class llvm::LoopInfoBase<BasicBlock, Loop>;
  41. // Always verify loopinfo if expensive checking is enabled.
  42. #ifdef EXPENSIVE_CHECKS
  43. bool llvm::VerifyLoopInfo = true;
  44. #else
  45. bool llvm::VerifyLoopInfo = false;
  46. #endif
  47. static cl::opt<bool, true>
  48. VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
  49. cl::Hidden, cl::desc("Verify loop info (time consuming)"));
  50. //===----------------------------------------------------------------------===//
  51. // Loop implementation
  52. //
  53. bool Loop::isLoopInvariant(const Value *V) const {
  54. if (const Instruction *I = dyn_cast<Instruction>(V))
  55. return !contains(I);
  56. return true; // All non-instructions are loop invariant
  57. }
  58. bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
  59. return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
  60. }
  61. bool Loop::makeLoopInvariant(Value *V, bool &Changed,
  62. Instruction *InsertPt) const {
  63. if (Instruction *I = dyn_cast<Instruction>(V))
  64. return makeLoopInvariant(I, Changed, InsertPt);
  65. return true; // All non-instructions are loop-invariant.
  66. }
  67. bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
  68. Instruction *InsertPt) const {
  69. // Test if the value is already loop-invariant.
  70. if (isLoopInvariant(I))
  71. return true;
  72. if (!isSafeToSpeculativelyExecute(I))
  73. return false;
  74. if (I->mayReadFromMemory())
  75. return false;
  76. // EH block instructions are immobile.
  77. if (I->isEHPad())
  78. return false;
  79. // Determine the insertion point, unless one was given.
  80. if (!InsertPt) {
  81. BasicBlock *Preheader = getLoopPreheader();
  82. // Without a preheader, hoisting is not feasible.
  83. if (!Preheader)
  84. return false;
  85. InsertPt = Preheader->getTerminator();
  86. }
  87. // Don't hoist instructions with loop-variant operands.
  88. for (Value *Operand : I->operands())
  89. if (!makeLoopInvariant(Operand, Changed, InsertPt))
  90. return false;
  91. // Hoist.
  92. I->moveBefore(InsertPt);
  93. // There is possibility of hoisting this instruction above some arbitrary
  94. // condition. Any metadata defined on it can be control dependent on this
  95. // condition. Conservatively strip it here so that we don't give any wrong
  96. // information to the optimizer.
  97. I->dropUnknownNonDebugMetadata();
  98. Changed = true;
  99. return true;
  100. }
  101. PHINode *Loop::getCanonicalInductionVariable() const {
  102. BasicBlock *H = getHeader();
  103. BasicBlock *Incoming = nullptr, *Backedge = nullptr;
  104. pred_iterator PI = pred_begin(H);
  105. assert(PI != pred_end(H) && "Loop must have at least one backedge!");
  106. Backedge = *PI++;
  107. if (PI == pred_end(H))
  108. return nullptr; // dead loop
  109. Incoming = *PI++;
  110. if (PI != pred_end(H))
  111. return nullptr; // multiple backedges?
  112. if (contains(Incoming)) {
  113. if (contains(Backedge))
  114. return nullptr;
  115. std::swap(Incoming, Backedge);
  116. } else if (!contains(Backedge))
  117. return nullptr;
  118. // Loop over all of the PHI nodes, looking for a canonical indvar.
  119. for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
  120. PHINode *PN = cast<PHINode>(I);
  121. if (ConstantInt *CI =
  122. dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
  123. if (CI->isZero())
  124. if (Instruction *Inc =
  125. dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
  126. if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
  127. if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
  128. if (CI->isOne())
  129. return PN;
  130. }
  131. return nullptr;
  132. }
  133. // Check that 'BB' doesn't have any uses outside of the 'L'
  134. static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
  135. DominatorTree &DT) {
  136. for (const Instruction &I : BB) {
  137. // Tokens can't be used in PHI nodes and live-out tokens prevent loop
  138. // optimizations, so for the purposes of considered LCSSA form, we
  139. // can ignore them.
  140. if (I.getType()->isTokenTy())
  141. continue;
  142. for (const Use &U : I.uses()) {
  143. const Instruction *UI = cast<Instruction>(U.getUser());
  144. const BasicBlock *UserBB = UI->getParent();
  145. if (const PHINode *P = dyn_cast<PHINode>(UI))
  146. UserBB = P->getIncomingBlock(U);
  147. // Check the current block, as a fast-path, before checking whether
  148. // the use is anywhere in the loop. Most values are used in the same
  149. // block they are defined in. Also, blocks not reachable from the
  150. // entry are special; uses in them don't need to go through PHIs.
  151. if (UserBB != &BB && !L.contains(UserBB) &&
  152. DT.isReachableFromEntry(UserBB))
  153. return false;
  154. }
  155. }
  156. return true;
  157. }
  158. bool Loop::isLCSSAForm(DominatorTree &DT) const {
  159. // For each block we check that it doesn't have any uses outside of this loop.
  160. return all_of(this->blocks(), [&](const BasicBlock *BB) {
  161. return isBlockInLCSSAForm(*this, *BB, DT);
  162. });
  163. }
  164. bool Loop::isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const {
  165. // For each block we check that it doesn't have any uses outside of its
  166. // innermost loop. This process will transitively guarantee that the current
  167. // loop and all of the nested loops are in LCSSA form.
  168. return all_of(this->blocks(), [&](const BasicBlock *BB) {
  169. return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
  170. });
  171. }
  172. bool Loop::isLoopSimplifyForm() const {
  173. // Normal-form loops have a preheader, a single backedge, and all of their
  174. // exits have all their predecessors inside the loop.
  175. return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
  176. }
  177. // Routines that reform the loop CFG and split edges often fail on indirectbr.
  178. bool Loop::isSafeToClone() const {
  179. // Return false if any loop blocks contain indirectbrs, or there are any calls
  180. // to noduplicate functions.
  181. for (BasicBlock *BB : this->blocks()) {
  182. if (isa<IndirectBrInst>(BB->getTerminator()))
  183. return false;
  184. for (Instruction &I : *BB)
  185. if (auto CS = CallSite(&I))
  186. if (CS.cannotDuplicate())
  187. return false;
  188. }
  189. return true;
  190. }
  191. MDNode *Loop::getLoopID() const {
  192. MDNode *LoopID = nullptr;
  193. // Go through the latch blocks and check the terminator for the metadata.
  194. SmallVector<BasicBlock *, 4> LatchesBlocks;
  195. getLoopLatches(LatchesBlocks);
  196. for (BasicBlock *BB : LatchesBlocks) {
  197. Instruction *TI = BB->getTerminator();
  198. MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
  199. if (!MD)
  200. return nullptr;
  201. if (!LoopID)
  202. LoopID = MD;
  203. else if (MD != LoopID)
  204. return nullptr;
  205. }
  206. if (!LoopID || LoopID->getNumOperands() == 0 ||
  207. LoopID->getOperand(0) != LoopID)
  208. return nullptr;
  209. return LoopID;
  210. }
  211. void Loop::setLoopID(MDNode *LoopID) const {
  212. assert((!LoopID || LoopID->getNumOperands() > 0) &&
  213. "Loop ID needs at least one operand");
  214. assert((!LoopID || LoopID->getOperand(0) == LoopID) &&
  215. "Loop ID should refer to itself");
  216. BasicBlock *H = getHeader();
  217. for (BasicBlock *BB : this->blocks()) {
  218. Instruction *TI = BB->getTerminator();
  219. for (BasicBlock *Successor : successors(TI)) {
  220. if (Successor == H) {
  221. TI->setMetadata(LLVMContext::MD_loop, LoopID);
  222. break;
  223. }
  224. }
  225. }
  226. }
  227. void Loop::setLoopAlreadyUnrolled() {
  228. MDNode *LoopID = getLoopID();
  229. // First remove any existing loop unrolling metadata.
  230. SmallVector<Metadata *, 4> MDs;
  231. // Reserve first location for self reference to the LoopID metadata node.
  232. MDs.push_back(nullptr);
  233. if (LoopID) {
  234. for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
  235. bool IsUnrollMetadata = false;
  236. MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
  237. if (MD) {
  238. const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
  239. IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
  240. }
  241. if (!IsUnrollMetadata)
  242. MDs.push_back(LoopID->getOperand(i));
  243. }
  244. }
  245. // Add unroll(disable) metadata to disable future unrolling.
  246. LLVMContext &Context = getHeader()->getContext();
  247. SmallVector<Metadata *, 1> DisableOperands;
  248. DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
  249. MDNode *DisableNode = MDNode::get(Context, DisableOperands);
  250. MDs.push_back(DisableNode);
  251. MDNode *NewLoopID = MDNode::get(Context, MDs);
  252. // Set operand 0 to refer to the loop id itself.
  253. NewLoopID->replaceOperandWith(0, NewLoopID);
  254. setLoopID(NewLoopID);
  255. }
  256. bool Loop::isAnnotatedParallel() const {
  257. MDNode *DesiredLoopIdMetadata = getLoopID();
  258. if (!DesiredLoopIdMetadata)
  259. return false;
  260. // The loop branch contains the parallel loop metadata. In order to ensure
  261. // that any parallel-loop-unaware optimization pass hasn't added loop-carried
  262. // dependencies (thus converted the loop back to a sequential loop), check
  263. // that all the memory instructions in the loop contain parallelism metadata
  264. // that point to the same unique "loop id metadata" the loop branch does.
  265. for (BasicBlock *BB : this->blocks()) {
  266. for (Instruction &I : *BB) {
  267. if (!I.mayReadOrWriteMemory())
  268. continue;
  269. // The memory instruction can refer to the loop identifier metadata
  270. // directly or indirectly through another list metadata (in case of
  271. // nested parallel loops). The loop identifier metadata refers to
  272. // itself so we can check both cases with the same routine.
  273. MDNode *LoopIdMD =
  274. I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
  275. if (!LoopIdMD)
  276. return false;
  277. bool LoopIdMDFound = false;
  278. for (const MDOperand &MDOp : LoopIdMD->operands()) {
  279. if (MDOp == DesiredLoopIdMetadata) {
  280. LoopIdMDFound = true;
  281. break;
  282. }
  283. }
  284. if (!LoopIdMDFound)
  285. return false;
  286. }
  287. }
  288. return true;
  289. }
  290. DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
  291. Loop::LocRange Loop::getLocRange() const {
  292. // If we have a debug location in the loop ID, then use it.
  293. if (MDNode *LoopID = getLoopID()) {
  294. DebugLoc Start;
  295. // We use the first DebugLoc in the header as the start location of the loop
  296. // and if there is a second DebugLoc in the header we use it as end location
  297. // of the loop.
  298. for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
  299. if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
  300. if (!Start)
  301. Start = DebugLoc(L);
  302. else
  303. return LocRange(Start, DebugLoc(L));
  304. }
  305. }
  306. if (Start)
  307. return LocRange(Start);
  308. }
  309. // Try the pre-header first.
  310. if (BasicBlock *PHeadBB = getLoopPreheader())
  311. if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
  312. return LocRange(DL);
  313. // If we have no pre-header or there are no instructions with debug
  314. // info in it, try the header.
  315. if (BasicBlock *HeadBB = getHeader())
  316. return LocRange(HeadBB->getTerminator()->getDebugLoc());
  317. return LocRange();
  318. }
  319. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  320. LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
  321. LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
  322. print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
  323. }
  324. #endif
  325. //===----------------------------------------------------------------------===//
  326. // UnloopUpdater implementation
  327. //
  328. namespace {
  329. /// Find the new parent loop for all blocks within the "unloop" whose last
  330. /// backedges has just been removed.
  331. class UnloopUpdater {
  332. Loop &Unloop;
  333. LoopInfo *LI;
  334. LoopBlocksDFS DFS;
  335. // Map unloop's immediate subloops to their nearest reachable parents. Nested
  336. // loops within these subloops will not change parents. However, an immediate
  337. // subloop's new parent will be the nearest loop reachable from either its own
  338. // exits *or* any of its nested loop's exits.
  339. DenseMap<Loop *, Loop *> SubloopParents;
  340. // Flag the presence of an irreducible backedge whose destination is a block
  341. // directly contained by the original unloop.
  342. bool FoundIB;
  343. public:
  344. UnloopUpdater(Loop *UL, LoopInfo *LInfo)
  345. : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
  346. void updateBlockParents();
  347. void removeBlocksFromAncestors();
  348. void updateSubloopParents();
  349. protected:
  350. Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
  351. };
  352. } // end anonymous namespace
  353. /// Update the parent loop for all blocks that are directly contained within the
  354. /// original "unloop".
  355. void UnloopUpdater::updateBlockParents() {
  356. if (Unloop.getNumBlocks()) {
  357. // Perform a post order CFG traversal of all blocks within this loop,
  358. // propagating the nearest loop from successors to predecessors.
  359. LoopBlocksTraversal Traversal(DFS, LI);
  360. for (BasicBlock *POI : Traversal) {
  361. Loop *L = LI->getLoopFor(POI);
  362. Loop *NL = getNearestLoop(POI, L);
  363. if (NL != L) {
  364. // For reducible loops, NL is now an ancestor of Unloop.
  365. assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
  366. "uninitialized successor");
  367. LI->changeLoopFor(POI, NL);
  368. } else {
  369. // Or the current block is part of a subloop, in which case its parent
  370. // is unchanged.
  371. assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
  372. }
  373. }
  374. }
  375. // Each irreducible loop within the unloop induces a round of iteration using
  376. // the DFS result cached by Traversal.
  377. bool Changed = FoundIB;
  378. for (unsigned NIters = 0; Changed; ++NIters) {
  379. assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
  380. // Iterate over the postorder list of blocks, propagating the nearest loop
  381. // from successors to predecessors as before.
  382. Changed = false;
  383. for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
  384. POE = DFS.endPostorder();
  385. POI != POE; ++POI) {
  386. Loop *L = LI->getLoopFor(*POI);
  387. Loop *NL = getNearestLoop(*POI, L);
  388. if (NL != L) {
  389. assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
  390. "uninitialized successor");
  391. LI->changeLoopFor(*POI, NL);
  392. Changed = true;
  393. }
  394. }
  395. }
  396. }
  397. /// Remove unloop's blocks from all ancestors below their new parents.
  398. void UnloopUpdater::removeBlocksFromAncestors() {
  399. // Remove all unloop's blocks (including those in nested subloops) from
  400. // ancestors below the new parent loop.
  401. for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
  402. BI != BE; ++BI) {
  403. Loop *OuterParent = LI->getLoopFor(*BI);
  404. if (Unloop.contains(OuterParent)) {
  405. while (OuterParent->getParentLoop() != &Unloop)
  406. OuterParent = OuterParent->getParentLoop();
  407. OuterParent = SubloopParents[OuterParent];
  408. }
  409. // Remove blocks from former Ancestors except Unloop itself which will be
  410. // deleted.
  411. for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
  412. OldParent = OldParent->getParentLoop()) {
  413. assert(OldParent && "new loop is not an ancestor of the original");
  414. OldParent->removeBlockFromLoop(*BI);
  415. }
  416. }
  417. }
  418. /// Update the parent loop for all subloops directly nested within unloop.
  419. void UnloopUpdater::updateSubloopParents() {
  420. while (!Unloop.empty()) {
  421. Loop *Subloop = *std::prev(Unloop.end());
  422. Unloop.removeChildLoop(std::prev(Unloop.end()));
  423. assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
  424. if (Loop *Parent = SubloopParents[Subloop])
  425. Parent->addChildLoop(Subloop);
  426. else
  427. LI->addTopLevelLoop(Subloop);
  428. }
  429. }
  430. /// Return the nearest parent loop among this block's successors. If a successor
  431. /// is a subloop header, consider its parent to be the nearest parent of the
  432. /// subloop's exits.
  433. ///
  434. /// For subloop blocks, simply update SubloopParents and return NULL.
  435. Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
  436. // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
  437. // is considered uninitialized.
  438. Loop *NearLoop = BBLoop;
  439. Loop *Subloop = nullptr;
  440. if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
  441. Subloop = NearLoop;
  442. // Find the subloop ancestor that is directly contained within Unloop.
  443. while (Subloop->getParentLoop() != &Unloop) {
  444. Subloop = Subloop->getParentLoop();
  445. assert(Subloop && "subloop is not an ancestor of the original loop");
  446. }
  447. // Get the current nearest parent of the Subloop exits, initially Unloop.
  448. NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
  449. }
  450. succ_iterator I = succ_begin(BB), E = succ_end(BB);
  451. if (I == E) {
  452. assert(!Subloop && "subloop blocks must have a successor");
  453. NearLoop = nullptr; // unloop blocks may now exit the function.
  454. }
  455. for (; I != E; ++I) {
  456. if (*I == BB)
  457. continue; // self loops are uninteresting
  458. Loop *L = LI->getLoopFor(*I);
  459. if (L == &Unloop) {
  460. // This successor has not been processed. This path must lead to an
  461. // irreducible backedge.
  462. assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
  463. FoundIB = true;
  464. }
  465. if (L != &Unloop && Unloop.contains(L)) {
  466. // Successor is in a subloop.
  467. if (Subloop)
  468. continue; // Branching within subloops. Ignore it.
  469. // BB branches from the original into a subloop header.
  470. assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
  471. // Get the current nearest parent of the Subloop's exits.
  472. L = SubloopParents[L];
  473. // L could be Unloop if the only exit was an irreducible backedge.
  474. }
  475. if (L == &Unloop) {
  476. continue;
  477. }
  478. // Handle critical edges from Unloop into a sibling loop.
  479. if (L && !L->contains(&Unloop)) {
  480. L = L->getParentLoop();
  481. }
  482. // Remember the nearest parent loop among successors or subloop exits.
  483. if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
  484. NearLoop = L;
  485. }
  486. if (Subloop) {
  487. SubloopParents[Subloop] = NearLoop;
  488. return BBLoop;
  489. }
  490. return NearLoop;
  491. }
  492. LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
  493. bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
  494. FunctionAnalysisManager::Invalidator &) {
  495. // Check whether the analysis, all analyses on functions, or the function's
  496. // CFG have been preserved.
  497. auto PAC = PA.getChecker<LoopAnalysis>();
  498. return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
  499. PAC.preservedSet<CFGAnalyses>());
  500. }
  501. void LoopInfo::erase(Loop *Unloop) {
  502. assert(!Unloop->isInvalid() && "Loop has already been erased!");
  503. auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
  504. // First handle the special case of no parent loop to simplify the algorithm.
  505. if (!Unloop->getParentLoop()) {
  506. // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
  507. for (Loop::block_iterator I = Unloop->block_begin(),
  508. E = Unloop->block_end();
  509. I != E; ++I) {
  510. // Don't reparent blocks in subloops.
  511. if (getLoopFor(*I) != Unloop)
  512. continue;
  513. // Blocks no longer have a parent but are still referenced by Unloop until
  514. // the Unloop object is deleted.
  515. changeLoopFor(*I, nullptr);
  516. }
  517. // Remove the loop from the top-level LoopInfo object.
  518. for (iterator I = begin();; ++I) {
  519. assert(I != end() && "Couldn't find loop");
  520. if (*I == Unloop) {
  521. removeLoop(I);
  522. break;
  523. }
  524. }
  525. // Move all of the subloops to the top-level.
  526. while (!Unloop->empty())
  527. addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
  528. return;
  529. }
  530. // Update the parent loop for all blocks within the loop. Blocks within
  531. // subloops will not change parents.
  532. UnloopUpdater Updater(Unloop, this);
  533. Updater.updateBlockParents();
  534. // Remove blocks from former ancestor loops.
  535. Updater.removeBlocksFromAncestors();
  536. // Add direct subloops as children in their new parent loop.
  537. Updater.updateSubloopParents();
  538. // Remove unloop from its parent loop.
  539. Loop *ParentLoop = Unloop->getParentLoop();
  540. for (Loop::iterator I = ParentLoop->begin();; ++I) {
  541. assert(I != ParentLoop->end() && "Couldn't find loop");
  542. if (*I == Unloop) {
  543. ParentLoop->removeChildLoop(I);
  544. break;
  545. }
  546. }
  547. }
  548. AnalysisKey LoopAnalysis::Key;
  549. LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
  550. // FIXME: Currently we create a LoopInfo from scratch for every function.
  551. // This may prove to be too wasteful due to deallocating and re-allocating
  552. // memory each time for the underlying map and vector datastructures. At some
  553. // point it may prove worthwhile to use a freelist and recycle LoopInfo
  554. // objects. I don't want to add that kind of complexity until the scope of
  555. // the problem is better understood.
  556. LoopInfo LI;
  557. LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
  558. return LI;
  559. }
  560. PreservedAnalyses LoopPrinterPass::run(Function &F,
  561. FunctionAnalysisManager &AM) {
  562. AM.getResult<LoopAnalysis>(F).print(OS);
  563. return PreservedAnalyses::all();
  564. }
  565. void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
  566. if (forcePrintModuleIR()) {
  567. // handling -print-module-scope
  568. OS << Banner << " (loop: ";
  569. L.getHeader()->printAsOperand(OS, false);
  570. OS << ")\n";
  571. // printing whole module
  572. OS << *L.getHeader()->getModule();
  573. return;
  574. }
  575. OS << Banner;
  576. auto *PreHeader = L.getLoopPreheader();
  577. if (PreHeader) {
  578. OS << "\n; Preheader:";
  579. PreHeader->print(OS);
  580. OS << "\n; Loop:";
  581. }
  582. for (auto *Block : L.blocks())
  583. if (Block)
  584. Block->print(OS);
  585. else
  586. OS << "Printing <null> block";
  587. SmallVector<BasicBlock *, 8> ExitBlocks;
  588. L.getExitBlocks(ExitBlocks);
  589. if (!ExitBlocks.empty()) {
  590. OS << "\n; Exit blocks";
  591. for (auto *Block : ExitBlocks)
  592. if (Block)
  593. Block->print(OS);
  594. else
  595. OS << "Printing <null> block";
  596. }
  597. }
  598. //===----------------------------------------------------------------------===//
  599. // LoopInfo implementation
  600. //
  601. char LoopInfoWrapperPass::ID = 0;
  602. INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
  603. true, true)
  604. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  605. INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
  606. true, true)
  607. bool LoopInfoWrapperPass::runOnFunction(Function &) {
  608. releaseMemory();
  609. LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
  610. return false;
  611. }
  612. void LoopInfoWrapperPass::verifyAnalysis() const {
  613. // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
  614. // function each time verifyAnalysis is called is very expensive. The
  615. // -verify-loop-info option can enable this. In order to perform some
  616. // checking by default, LoopPass has been taught to call verifyLoop manually
  617. // during loop pass sequences.
  618. if (VerifyLoopInfo) {
  619. auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  620. LI.verify(DT);
  621. }
  622. }
  623. void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  624. AU.setPreservesAll();
  625. AU.addRequired<DominatorTreeWrapperPass>();
  626. }
  627. void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
  628. LI.print(OS);
  629. }
  630. PreservedAnalyses LoopVerifierPass::run(Function &F,
  631. FunctionAnalysisManager &AM) {
  632. LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
  633. auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  634. LI.verify(DT);
  635. return PreservedAnalyses::all();
  636. }
  637. //===----------------------------------------------------------------------===//
  638. // LoopBlocksDFS implementation
  639. //
  640. /// Traverse the loop blocks and store the DFS result.
  641. /// Useful for clients that just want the final DFS result and don't need to
  642. /// visit blocks during the initial traversal.
  643. void LoopBlocksDFS::perform(LoopInfo *LI) {
  644. LoopBlocksTraversal Traversal(*this, LI);
  645. for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
  646. POE = Traversal.end();
  647. POI != POE; ++POI)
  648. ;
  649. }