LoopInfo.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the LoopInfo class that is used to identify natural loops
  10. // and determine the loop depth of various nodes of the CFG. A natural loop
  11. // has exactly one entry-point, which is called the header. Note that natural
  12. // loops may actually be several loops that share the same header node.
  13. //
  14. // This analysis calculates the nesting structure of loops in a function. For
  15. // each natural loop identified, this analysis identifies natural loops
  16. // contained entirely within the loop and the basic blocks the make up the loop.
  17. //
  18. // It can calculate on the fly various bits of information, for example:
  19. //
  20. // * whether there is a preheader for the loop
  21. // * the number of back edges to the header
  22. // * whether or not a particular block branches out of the loop
  23. // * the successor blocks of the loop
  24. // * the loop depth
  25. // * etc...
  26. //
  27. // Note that this analysis specifically identifies *Loops* not cycles or SCCs
  28. // in the CFG. There can be strongly connected components in the CFG which
  29. // this analysis will not recognize and that will not be represented by a Loop
  30. // instance. In particular, a Loop might be inside such a non-loop SCC, or a
  31. // non-loop SCC might contain a sub-SCC which is a Loop.
  32. //
  33. // For an overview of terminology used in this API (and thus all of our loop
  34. // analyses or transforms), see docs/LoopTerminology.rst.
  35. //
  36. //===----------------------------------------------------------------------===//
  37. #ifndef LLVM_ANALYSIS_LOOPINFO_H
  38. #define LLVM_ANALYSIS_LOOPINFO_H
  39. #include "llvm/ADT/DenseMap.h"
  40. #include "llvm/ADT/DenseSet.h"
  41. #include "llvm/ADT/GraphTraits.h"
  42. #include "llvm/ADT/SmallPtrSet.h"
  43. #include "llvm/ADT/SmallVector.h"
  44. #include "llvm/IR/CFG.h"
  45. #include "llvm/IR/Instruction.h"
  46. #include "llvm/IR/Instructions.h"
  47. #include "llvm/IR/PassManager.h"
  48. #include "llvm/Pass.h"
  49. #include "llvm/Support/Allocator.h"
  50. #include <algorithm>
  51. #include <utility>
  52. namespace llvm {
  53. class DominatorTree;
  54. class LoopInfo;
  55. class Loop;
  56. class InductionDescriptor;
  57. class MDNode;
  58. class MemorySSAUpdater;
  59. class ScalarEvolution;
  60. class raw_ostream;
  61. template <class N, bool IsPostDom> class DominatorTreeBase;
  62. template <class N, class M> class LoopInfoBase;
  63. template <class N, class M> class LoopBase;
  64. //===----------------------------------------------------------------------===//
  65. /// Instances of this class are used to represent loops that are detected in the
  66. /// flow graph.
  67. ///
  68. template <class BlockT, class LoopT> class LoopBase {
  69. LoopT *ParentLoop;
  70. // Loops contained entirely within this one.
  71. std::vector<LoopT *> SubLoops;
  72. // The list of blocks in this loop. First entry is the header node.
  73. std::vector<BlockT *> Blocks;
  74. SmallPtrSet<const BlockT *, 8> DenseBlockSet;
  75. #if LLVM_ENABLE_ABI_BREAKING_CHECKS
  76. /// Indicator that this loop is no longer a valid loop.
  77. bool IsInvalid = false;
  78. #endif
  79. LoopBase(const LoopBase<BlockT, LoopT> &) = delete;
  80. const LoopBase<BlockT, LoopT> &
  81. operator=(const LoopBase<BlockT, LoopT> &) = delete;
  82. public:
  83. /// Return the nesting level of this loop. An outer-most loop has depth 1,
  84. /// for consistency with loop depth values used for basic blocks, where depth
  85. /// 0 is used for blocks not inside any loops.
  86. unsigned getLoopDepth() const {
  87. assert(!isInvalid() && "Loop not in a valid state!");
  88. unsigned D = 1;
  89. for (const LoopT *CurLoop = ParentLoop; CurLoop;
  90. CurLoop = CurLoop->ParentLoop)
  91. ++D;
  92. return D;
  93. }
  94. BlockT *getHeader() const { return getBlocks().front(); }
  95. /// Return the parent loop if it exists or nullptr for top
  96. /// level loops.
  97. /// A loop is either top-level in a function (that is, it is not
  98. /// contained in any other loop) or it is entirely enclosed in
  99. /// some other loop.
  100. /// If a loop is top-level, it has no parent, otherwise its
  101. /// parent is the innermost loop in which it is enclosed.
  102. LoopT *getParentLoop() const { return ParentLoop; }
  103. /// This is a raw interface for bypassing addChildLoop.
  104. void setParentLoop(LoopT *L) {
  105. assert(!isInvalid() && "Loop not in a valid state!");
  106. ParentLoop = L;
  107. }
  108. /// Return true if the specified loop is contained within in this loop.
  109. bool contains(const LoopT *L) const {
  110. assert(!isInvalid() && "Loop not in a valid state!");
  111. if (L == this)
  112. return true;
  113. if (!L)
  114. return false;
  115. return contains(L->getParentLoop());
  116. }
  117. /// Return true if the specified basic block is in this loop.
  118. bool contains(const BlockT *BB) const {
  119. assert(!isInvalid() && "Loop not in a valid state!");
  120. return DenseBlockSet.count(BB);
  121. }
  122. /// Return true if the specified instruction is in this loop.
  123. template <class InstT> bool contains(const InstT *Inst) const {
  124. return contains(Inst->getParent());
  125. }
  126. /// Return the loops contained entirely within this loop.
  127. const std::vector<LoopT *> &getSubLoops() const {
  128. assert(!isInvalid() && "Loop not in a valid state!");
  129. return SubLoops;
  130. }
  131. std::vector<LoopT *> &getSubLoopsVector() {
  132. assert(!isInvalid() && "Loop not in a valid state!");
  133. return SubLoops;
  134. }
  135. typedef typename std::vector<LoopT *>::const_iterator iterator;
  136. typedef
  137. typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
  138. iterator begin() const { return getSubLoops().begin(); }
  139. iterator end() const { return getSubLoops().end(); }
  140. reverse_iterator rbegin() const { return getSubLoops().rbegin(); }
  141. reverse_iterator rend() const { return getSubLoops().rend(); }
  142. // LoopInfo does not detect irreducible control flow, just natural
  143. // loops. That is, it is possible that there is cyclic control
  144. // flow within the "innermost loop" or around the "outermost
  145. // loop".
  146. /// Return true if the loop does not contain any (natural) loops.
  147. bool isInnermost() const { return getSubLoops().empty(); }
  148. /// Return true if the loop does not have a parent (natural) loop
  149. // (i.e. it is outermost, which is the same as top-level).
  150. bool isOutermost() const { return getParentLoop() == nullptr; }
  151. /// Get a list of the basic blocks which make up this loop.
  152. ArrayRef<BlockT *> getBlocks() const {
  153. assert(!isInvalid() && "Loop not in a valid state!");
  154. return Blocks;
  155. }
  156. typedef typename ArrayRef<BlockT *>::const_iterator block_iterator;
  157. block_iterator block_begin() const { return getBlocks().begin(); }
  158. block_iterator block_end() const { return getBlocks().end(); }
  159. inline iterator_range<block_iterator> blocks() const {
  160. assert(!isInvalid() && "Loop not in a valid state!");
  161. return make_range(block_begin(), block_end());
  162. }
  163. /// Get the number of blocks in this loop in constant time.
  164. /// Invalidate the loop, indicating that it is no longer a loop.
  165. unsigned getNumBlocks() const {
  166. assert(!isInvalid() && "Loop not in a valid state!");
  167. return Blocks.size();
  168. }
  169. /// Return a direct, mutable handle to the blocks vector so that we can
  170. /// mutate it efficiently with techniques like `std::remove`.
  171. std::vector<BlockT *> &getBlocksVector() {
  172. assert(!isInvalid() && "Loop not in a valid state!");
  173. return Blocks;
  174. }
  175. /// Return a direct, mutable handle to the blocks set so that we can
  176. /// mutate it efficiently.
  177. SmallPtrSetImpl<const BlockT *> &getBlocksSet() {
  178. assert(!isInvalid() && "Loop not in a valid state!");
  179. return DenseBlockSet;
  180. }
  181. /// Return a direct, immutable handle to the blocks set.
  182. const SmallPtrSetImpl<const BlockT *> &getBlocksSet() const {
  183. assert(!isInvalid() && "Loop not in a valid state!");
  184. return DenseBlockSet;
  185. }
  186. /// Return true if this loop is no longer valid. The only valid use of this
  187. /// helper is "assert(L.isInvalid())" or equivalent, since IsInvalid is set to
  188. /// true by the destructor. In other words, if this accessor returns true,
  189. /// the caller has already triggered UB by calling this accessor; and so it
  190. /// can only be called in a context where a return value of true indicates a
  191. /// programmer error.
  192. bool isInvalid() const {
  193. #if LLVM_ENABLE_ABI_BREAKING_CHECKS
  194. return IsInvalid;
  195. #else
  196. return false;
  197. #endif
  198. }
  199. /// True if terminator in the block can branch to another block that is
  200. /// outside of the current loop. \p BB must be inside the loop.
  201. bool isLoopExiting(const BlockT *BB) const {
  202. assert(!isInvalid() && "Loop not in a valid state!");
  203. assert(contains(BB) && "Exiting block must be part of the loop");
  204. for (const auto *Succ : children<const BlockT *>(BB)) {
  205. if (!contains(Succ))
  206. return true;
  207. }
  208. return false;
  209. }
  210. /// Returns true if \p BB is a loop-latch.
  211. /// A latch block is a block that contains a branch back to the header.
  212. /// This function is useful when there are multiple latches in a loop
  213. /// because \fn getLoopLatch will return nullptr in that case.
  214. bool isLoopLatch(const BlockT *BB) const {
  215. assert(!isInvalid() && "Loop not in a valid state!");
  216. assert(contains(BB) && "block does not belong to the loop");
  217. BlockT *Header = getHeader();
  218. auto PredBegin = GraphTraits<Inverse<BlockT *>>::child_begin(Header);
  219. auto PredEnd = GraphTraits<Inverse<BlockT *>>::child_end(Header);
  220. return std::find(PredBegin, PredEnd, BB) != PredEnd;
  221. }
  222. /// Calculate the number of back edges to the loop header.
  223. unsigned getNumBackEdges() const {
  224. assert(!isInvalid() && "Loop not in a valid state!");
  225. unsigned NumBackEdges = 0;
  226. BlockT *H = getHeader();
  227. for (const auto Pred : children<Inverse<BlockT *>>(H))
  228. if (contains(Pred))
  229. ++NumBackEdges;
  230. return NumBackEdges;
  231. }
  232. //===--------------------------------------------------------------------===//
  233. // APIs for simple analysis of the loop.
  234. //
  235. // Note that all of these methods can fail on general loops (ie, there may not
  236. // be a preheader, etc). For best success, the loop simplification and
  237. // induction variable canonicalization pass should be used to normalize loops
  238. // for easy analysis. These methods assume canonical loops.
  239. /// Return all blocks inside the loop that have successors outside of the
  240. /// loop. These are the blocks _inside of the current loop_ which branch out.
  241. /// The returned list is always unique.
  242. void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
  243. /// If getExitingBlocks would return exactly one block, return that block.
  244. /// Otherwise return null.
  245. BlockT *getExitingBlock() const;
  246. /// Return all of the successor blocks of this loop. These are the blocks
  247. /// _outside of the current loop_ which are branched to.
  248. void getExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
  249. /// If getExitBlocks would return exactly one block, return that block.
  250. /// Otherwise return null.
  251. BlockT *getExitBlock() const;
  252. /// Return true if no exit block for the loop has a predecessor that is
  253. /// outside the loop.
  254. bool hasDedicatedExits() const;
  255. /// Return all unique successor blocks of this loop.
  256. /// These are the blocks _outside of the current loop_ which are branched to.
  257. void getUniqueExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
  258. /// Return all unique successor blocks of this loop except successors from
  259. /// Latch block are not considered. If the exit comes from Latch has also
  260. /// non Latch predecessor in a loop it will be added to ExitBlocks.
  261. /// These are the blocks _outside of the current loop_ which are branched to.
  262. void getUniqueNonLatchExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
  263. /// If getUniqueExitBlocks would return exactly one block, return that block.
  264. /// Otherwise return null.
  265. BlockT *getUniqueExitBlock() const;
  266. /// Return true if this loop does not have any exit blocks.
  267. bool hasNoExitBlocks() const;
  268. /// Edge type.
  269. typedef std::pair<BlockT *, BlockT *> Edge;
  270. /// Return all pairs of (_inside_block_,_outside_block_).
  271. void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
  272. /// If there is a preheader for this loop, return it. A loop has a preheader
  273. /// if there is only one edge to the header of the loop from outside of the
  274. /// loop. If this is the case, the block branching to the header of the loop
  275. /// is the preheader node.
  276. ///
  277. /// This method returns null if there is no preheader for the loop.
  278. BlockT *getLoopPreheader() const;
  279. /// If the given loop's header has exactly one unique predecessor outside the
  280. /// loop, return it. Otherwise return null.
  281. /// This is less strict that the loop "preheader" concept, which requires
  282. /// the predecessor to have exactly one successor.
  283. BlockT *getLoopPredecessor() const;
  284. /// If there is a single latch block for this loop, return it.
  285. /// A latch block is a block that contains a branch back to the header.
  286. BlockT *getLoopLatch() const;
  287. /// Return all loop latch blocks of this loop. A latch block is a block that
  288. /// contains a branch back to the header.
  289. void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
  290. assert(!isInvalid() && "Loop not in a valid state!");
  291. BlockT *H = getHeader();
  292. for (const auto Pred : children<Inverse<BlockT *>>(H))
  293. if (contains(Pred))
  294. LoopLatches.push_back(Pred);
  295. }
  296. /// Return all inner loops in the loop nest rooted by the loop in preorder,
  297. /// with siblings in forward program order.
  298. template <class Type>
  299. static void getInnerLoopsInPreorder(const LoopT &L,
  300. SmallVectorImpl<Type> &PreOrderLoops) {
  301. SmallVector<LoopT *, 4> PreOrderWorklist;
  302. PreOrderWorklist.append(L.rbegin(), L.rend());
  303. while (!PreOrderWorklist.empty()) {
  304. LoopT *L = PreOrderWorklist.pop_back_val();
  305. // Sub-loops are stored in forward program order, but will process the
  306. // worklist backwards so append them in reverse order.
  307. PreOrderWorklist.append(L->rbegin(), L->rend());
  308. PreOrderLoops.push_back(L);
  309. }
  310. }
  311. /// Return all loops in the loop nest rooted by the loop in preorder, with
  312. /// siblings in forward program order.
  313. SmallVector<const LoopT *, 4> getLoopsInPreorder() const {
  314. SmallVector<const LoopT *, 4> PreOrderLoops;
  315. const LoopT *CurLoop = static_cast<const LoopT *>(this);
  316. PreOrderLoops.push_back(CurLoop);
  317. getInnerLoopsInPreorder(*CurLoop, PreOrderLoops);
  318. return PreOrderLoops;
  319. }
  320. SmallVector<LoopT *, 4> getLoopsInPreorder() {
  321. SmallVector<LoopT *, 4> PreOrderLoops;
  322. LoopT *CurLoop = static_cast<LoopT *>(this);
  323. PreOrderLoops.push_back(CurLoop);
  324. getInnerLoopsInPreorder(*CurLoop, PreOrderLoops);
  325. return PreOrderLoops;
  326. }
  327. //===--------------------------------------------------------------------===//
  328. // APIs for updating loop information after changing the CFG
  329. //
  330. /// This method is used by other analyses to update loop information.
  331. /// NewBB is set to be a new member of the current loop.
  332. /// Because of this, it is added as a member of all parent loops, and is added
  333. /// to the specified LoopInfo object as being in the current basic block. It
  334. /// is not valid to replace the loop header with this method.
  335. void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
  336. /// This is used when splitting loops up. It replaces the OldChild entry in
  337. /// our children list with NewChild, and updates the parent pointer of
  338. /// OldChild to be null and the NewChild to be this loop.
  339. /// This updates the loop depth of the new child.
  340. void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
  341. /// Add the specified loop to be a child of this loop.
  342. /// This updates the loop depth of the new child.
  343. void addChildLoop(LoopT *NewChild) {
  344. assert(!isInvalid() && "Loop not in a valid state!");
  345. assert(!NewChild->ParentLoop && "NewChild already has a parent!");
  346. NewChild->ParentLoop = static_cast<LoopT *>(this);
  347. SubLoops.push_back(NewChild);
  348. }
  349. /// This removes the specified child from being a subloop of this loop. The
  350. /// loop is not deleted, as it will presumably be inserted into another loop.
  351. LoopT *removeChildLoop(iterator I) {
  352. assert(!isInvalid() && "Loop not in a valid state!");
  353. assert(I != SubLoops.end() && "Cannot remove end iterator!");
  354. LoopT *Child = *I;
  355. assert(Child->ParentLoop == this && "Child is not a child of this loop!");
  356. SubLoops.erase(SubLoops.begin() + (I - begin()));
  357. Child->ParentLoop = nullptr;
  358. return Child;
  359. }
  360. /// This removes the specified child from being a subloop of this loop. The
  361. /// loop is not deleted, as it will presumably be inserted into another loop.
  362. LoopT *removeChildLoop(LoopT *Child) {
  363. return removeChildLoop(llvm::find(*this, Child));
  364. }
  365. /// This adds a basic block directly to the basic block list.
  366. /// This should only be used by transformations that create new loops. Other
  367. /// transformations should use addBasicBlockToLoop.
  368. void addBlockEntry(BlockT *BB) {
  369. assert(!isInvalid() && "Loop not in a valid state!");
  370. Blocks.push_back(BB);
  371. DenseBlockSet.insert(BB);
  372. }
  373. /// interface to reverse Blocks[from, end of loop] in this loop
  374. void reverseBlock(unsigned from) {
  375. assert(!isInvalid() && "Loop not in a valid state!");
  376. std::reverse(Blocks.begin() + from, Blocks.end());
  377. }
  378. /// interface to do reserve() for Blocks
  379. void reserveBlocks(unsigned size) {
  380. assert(!isInvalid() && "Loop not in a valid state!");
  381. Blocks.reserve(size);
  382. }
  383. /// This method is used to move BB (which must be part of this loop) to be the
  384. /// loop header of the loop (the block that dominates all others).
  385. void moveToHeader(BlockT *BB) {
  386. assert(!isInvalid() && "Loop not in a valid state!");
  387. if (Blocks[0] == BB)
  388. return;
  389. for (unsigned i = 0;; ++i) {
  390. assert(i != Blocks.size() && "Loop does not contain BB!");
  391. if (Blocks[i] == BB) {
  392. Blocks[i] = Blocks[0];
  393. Blocks[0] = BB;
  394. return;
  395. }
  396. }
  397. }
  398. /// This removes the specified basic block from the current loop, updating the
  399. /// Blocks as appropriate. This does not update the mapping in the LoopInfo
  400. /// class.
  401. void removeBlockFromLoop(BlockT *BB) {
  402. assert(!isInvalid() && "Loop not in a valid state!");
  403. auto I = find(Blocks, BB);
  404. assert(I != Blocks.end() && "N is not in this list!");
  405. Blocks.erase(I);
  406. DenseBlockSet.erase(BB);
  407. }
  408. /// Verify loop structure
  409. void verifyLoop() const;
  410. /// Verify loop structure of this loop and all nested loops.
  411. void verifyLoopNest(DenseSet<const LoopT *> *Loops) const;
  412. /// Returns true if the loop is annotated parallel.
  413. ///
  414. /// Derived classes can override this method using static template
  415. /// polymorphism.
  416. bool isAnnotatedParallel() const { return false; }
  417. /// Print loop with all the BBs inside it.
  418. void print(raw_ostream &OS, bool Verbose = false, bool PrintNested = true,
  419. unsigned Depth = 0) const;
  420. protected:
  421. friend class LoopInfoBase<BlockT, LoopT>;
  422. /// This creates an empty loop.
  423. LoopBase() : ParentLoop(nullptr) {}
  424. explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
  425. Blocks.push_back(BB);
  426. DenseBlockSet.insert(BB);
  427. }
  428. // Since loop passes like SCEV are allowed to key analysis results off of
  429. // `Loop` pointers, we cannot re-use pointers within a loop pass manager.
  430. // This means loop passes should not be `delete` ing `Loop` objects directly
  431. // (and risk a later `Loop` allocation re-using the address of a previous one)
  432. // but should be using LoopInfo::markAsRemoved, which keeps around the `Loop`
  433. // pointer till the end of the lifetime of the `LoopInfo` object.
  434. //
  435. // To make it easier to follow this rule, we mark the destructor as
  436. // non-public.
  437. ~LoopBase() {
  438. for (auto *SubLoop : SubLoops)
  439. SubLoop->~LoopT();
  440. #if LLVM_ENABLE_ABI_BREAKING_CHECKS
  441. IsInvalid = true;
  442. #endif
  443. SubLoops.clear();
  444. Blocks.clear();
  445. DenseBlockSet.clear();
  446. ParentLoop = nullptr;
  447. }
  448. };
  449. template <class BlockT, class LoopT>
  450. raw_ostream &operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
  451. Loop.print(OS);
  452. return OS;
  453. }
  454. // Implementation in LoopInfoImpl.h
  455. extern template class LoopBase<BasicBlock, Loop>;
  456. /// Represents a single loop in the control flow graph. Note that not all SCCs
  457. /// in the CFG are necessarily loops.
  458. class Loop : public LoopBase<BasicBlock, Loop> {
  459. public:
  460. /// A range representing the start and end location of a loop.
  461. class LocRange {
  462. DebugLoc Start;
  463. DebugLoc End;
  464. public:
  465. LocRange() {}
  466. LocRange(DebugLoc Start) : Start(Start), End(Start) {}
  467. LocRange(DebugLoc Start, DebugLoc End)
  468. : Start(std::move(Start)), End(std::move(End)) {}
  469. const DebugLoc &getStart() const { return Start; }
  470. const DebugLoc &getEnd() const { return End; }
  471. /// Check for null.
  472. ///
  473. explicit operator bool() const { return Start && End; }
  474. };
  475. /// Return true if the specified value is loop invariant.
  476. bool isLoopInvariant(const Value *V) const;
  477. /// Return true if all the operands of the specified instruction are loop
  478. /// invariant.
  479. bool hasLoopInvariantOperands(const Instruction *I) const;
  480. /// If the given value is an instruction inside of the loop and it can be
  481. /// hoisted, do so to make it trivially loop-invariant.
  482. /// Return true if the value after any hoisting is loop invariant. This
  483. /// function can be used as a slightly more aggressive replacement for
  484. /// isLoopInvariant.
  485. ///
  486. /// If InsertPt is specified, it is the point to hoist instructions to.
  487. /// If null, the terminator of the loop preheader is used.
  488. bool makeLoopInvariant(Value *V, bool &Changed,
  489. Instruction *InsertPt = nullptr,
  490. MemorySSAUpdater *MSSAU = nullptr) const;
  491. /// If the given instruction is inside of the loop and it can be hoisted, do
  492. /// so to make it trivially loop-invariant.
  493. /// Return true if the instruction after any hoisting is loop invariant. This
  494. /// function can be used as a slightly more aggressive replacement for
  495. /// isLoopInvariant.
  496. ///
  497. /// If InsertPt is specified, it is the point to hoist instructions to.
  498. /// If null, the terminator of the loop preheader is used.
  499. ///
  500. bool makeLoopInvariant(Instruction *I, bool &Changed,
  501. Instruction *InsertPt = nullptr,
  502. MemorySSAUpdater *MSSAU = nullptr) const;
  503. /// Check to see if the loop has a canonical induction variable: an integer
  504. /// recurrence that starts at 0 and increments by one each time through the
  505. /// loop. If so, return the phi node that corresponds to it.
  506. ///
  507. /// The IndVarSimplify pass transforms loops to have a canonical induction
  508. /// variable.
  509. ///
  510. PHINode *getCanonicalInductionVariable() const;
  511. /// Obtain the unique incoming and back edge. Return false if they are
  512. /// non-unique or the loop is dead; otherwise, return true.
  513. bool getIncomingAndBackEdge(BasicBlock *&Incoming,
  514. BasicBlock *&Backedge) const;
  515. /// Below are some utilities to get the loop guard, loop bounds and induction
  516. /// variable, and to check if a given phinode is an auxiliary induction
  517. /// variable, if the loop is guarded, and if the loop is canonical.
  518. ///
  519. /// Here is an example:
  520. /// \code
  521. /// for (int i = lb; i < ub; i+=step)
  522. /// <loop body>
  523. /// --- pseudo LLVMIR ---
  524. /// beforeloop:
  525. /// guardcmp = (lb < ub)
  526. /// if (guardcmp) goto preheader; else goto afterloop
  527. /// preheader:
  528. /// loop:
  529. /// i_1 = phi[{lb, preheader}, {i_2, latch}]
  530. /// <loop body>
  531. /// i_2 = i_1 + step
  532. /// latch:
  533. /// cmp = (i_2 < ub)
  534. /// if (cmp) goto loop
  535. /// exit:
  536. /// afterloop:
  537. /// \endcode
  538. ///
  539. /// - getBounds
  540. /// - getInitialIVValue --> lb
  541. /// - getStepInst --> i_2 = i_1 + step
  542. /// - getStepValue --> step
  543. /// - getFinalIVValue --> ub
  544. /// - getCanonicalPredicate --> '<'
  545. /// - getDirection --> Increasing
  546. ///
  547. /// - getInductionVariable --> i_1
  548. /// - isAuxiliaryInductionVariable(x) --> true if x == i_1
  549. /// - getLoopGuardBranch()
  550. /// --> `if (guardcmp) goto preheader; else goto afterloop`
  551. /// - isGuarded() --> true
  552. /// - isCanonical --> false
  553. struct LoopBounds {
  554. /// Return the LoopBounds object if
  555. /// - the given \p IndVar is an induction variable
  556. /// - the initial value of the induction variable can be found
  557. /// - the step instruction of the induction variable can be found
  558. /// - the final value of the induction variable can be found
  559. ///
  560. /// Else None.
  561. static Optional<Loop::LoopBounds> getBounds(const Loop &L, PHINode &IndVar,
  562. ScalarEvolution &SE);
  563. /// Get the initial value of the loop induction variable.
  564. Value &getInitialIVValue() const { return InitialIVValue; }
  565. /// Get the instruction that updates the loop induction variable.
  566. Instruction &getStepInst() const { return StepInst; }
  567. /// Get the step that the loop induction variable gets updated by in each
  568. /// loop iteration. Return nullptr if not found.
  569. Value *getStepValue() const { return StepValue; }
  570. /// Get the final value of the loop induction variable.
  571. Value &getFinalIVValue() const { return FinalIVValue; }
  572. /// Return the canonical predicate for the latch compare instruction, if
  573. /// able to be calcuated. Else BAD_ICMP_PREDICATE.
  574. ///
  575. /// A predicate is considered as canonical if requirements below are all
  576. /// satisfied:
  577. /// 1. The first successor of the latch branch is the loop header
  578. /// If not, inverse the predicate.
  579. /// 2. One of the operands of the latch comparison is StepInst
  580. /// If not, and
  581. /// - if the current calcuated predicate is not ne or eq, flip the
  582. /// predicate.
  583. /// - else if the loop is increasing, return slt
  584. /// (notice that it is safe to change from ne or eq to sign compare)
  585. /// - else if the loop is decreasing, return sgt
  586. /// (notice that it is safe to change from ne or eq to sign compare)
  587. ///
  588. /// Here is an example when both (1) and (2) are not satisfied:
  589. /// \code
  590. /// loop.header:
  591. /// %iv = phi [%initialiv, %loop.preheader], [%inc, %loop.header]
  592. /// %inc = add %iv, %step
  593. /// %cmp = slt %iv, %finaliv
  594. /// br %cmp, %loop.exit, %loop.header
  595. /// loop.exit:
  596. /// \endcode
  597. /// - The second successor of the latch branch is the loop header instead
  598. /// of the first successor (slt -> sge)
  599. /// - The first operand of the latch comparison (%cmp) is the IndVar (%iv)
  600. /// instead of the StepInst (%inc) (sge -> sgt)
  601. ///
  602. /// The predicate would be sgt if both (1) and (2) are satisfied.
  603. /// getCanonicalPredicate() returns sgt for this example.
  604. /// Note: The IR is not changed.
  605. ICmpInst::Predicate getCanonicalPredicate() const;
  606. /// An enum for the direction of the loop
  607. /// - for (int i = 0; i < ub; ++i) --> Increasing
  608. /// - for (int i = ub; i > 0; --i) --> Descresing
  609. /// - for (int i = x; i != y; i+=z) --> Unknown
  610. enum class Direction { Increasing, Decreasing, Unknown };
  611. /// Get the direction of the loop.
  612. Direction getDirection() const;
  613. private:
  614. LoopBounds(const Loop &Loop, Value &I, Instruction &SI, Value *SV, Value &F,
  615. ScalarEvolution &SE)
  616. : L(Loop), InitialIVValue(I), StepInst(SI), StepValue(SV),
  617. FinalIVValue(F), SE(SE) {}
  618. const Loop &L;
  619. // The initial value of the loop induction variable
  620. Value &InitialIVValue;
  621. // The instruction that updates the loop induction variable
  622. Instruction &StepInst;
  623. // The value that the loop induction variable gets updated by in each loop
  624. // iteration
  625. Value *StepValue;
  626. // The final value of the loop induction variable
  627. Value &FinalIVValue;
  628. ScalarEvolution &SE;
  629. };
  630. /// Return the struct LoopBounds collected if all struct members are found,
  631. /// else None.
  632. Optional<LoopBounds> getBounds(ScalarEvolution &SE) const;
  633. /// Return the loop induction variable if found, else return nullptr.
  634. /// An instruction is considered as the loop induction variable if
  635. /// - it is an induction variable of the loop; and
  636. /// - it is used to determine the condition of the branch in the loop latch
  637. ///
  638. /// Note: the induction variable doesn't need to be canonical, i.e. starts at
  639. /// zero and increments by one each time through the loop (but it can be).
  640. PHINode *getInductionVariable(ScalarEvolution &SE) const;
  641. /// Get the loop induction descriptor for the loop induction variable. Return
  642. /// true if the loop induction variable is found.
  643. bool getInductionDescriptor(ScalarEvolution &SE,
  644. InductionDescriptor &IndDesc) const;
  645. /// Return true if the given PHINode \p AuxIndVar is
  646. /// - in the loop header
  647. /// - not used outside of the loop
  648. /// - incremented by a loop invariant step for each loop iteration
  649. /// - step instruction opcode should be add or sub
  650. /// Note: auxiliary induction variable is not required to be used in the
  651. /// conditional branch in the loop latch. (but it can be)
  652. bool isAuxiliaryInductionVariable(PHINode &AuxIndVar,
  653. ScalarEvolution &SE) const;
  654. /// Return the loop guard branch, if it exists.
  655. ///
  656. /// This currently only works on simplified loop, as it requires a preheader
  657. /// and a latch to identify the guard. It will work on loops of the form:
  658. /// \code
  659. /// GuardBB:
  660. /// br cond1, Preheader, ExitSucc <== GuardBranch
  661. /// Preheader:
  662. /// br Header
  663. /// Header:
  664. /// ...
  665. /// br Latch
  666. /// Latch:
  667. /// br cond2, Header, ExitBlock
  668. /// ExitBlock:
  669. /// br ExitSucc
  670. /// ExitSucc:
  671. /// \endcode
  672. BranchInst *getLoopGuardBranch() const;
  673. /// Return true iff the loop is
  674. /// - in simplify rotated form, and
  675. /// - guarded by a loop guard branch.
  676. bool isGuarded() const { return (getLoopGuardBranch() != nullptr); }
  677. /// Return true if the loop is in rotated form.
  678. ///
  679. /// This does not check if the loop was rotated by loop rotation, instead it
  680. /// only checks if the loop is in rotated form (has a valid latch that exists
  681. /// the loop).
  682. bool isRotatedForm() const {
  683. assert(!isInvalid() && "Loop not in a valid state!");
  684. BasicBlock *Latch = getLoopLatch();
  685. return Latch && isLoopExiting(Latch);
  686. }
  687. /// Return true if the loop induction variable starts at zero and increments
  688. /// by one each time through the loop.
  689. bool isCanonical(ScalarEvolution &SE) const;
  690. /// Return true if the Loop is in LCSSA form.
  691. bool isLCSSAForm(const DominatorTree &DT) const;
  692. /// Return true if this Loop and all inner subloops are in LCSSA form.
  693. bool isRecursivelyLCSSAForm(const DominatorTree &DT,
  694. const LoopInfo &LI) const;
  695. /// Return true if the Loop is in the form that the LoopSimplify form
  696. /// transforms loops to, which is sometimes called normal form.
  697. bool isLoopSimplifyForm() const;
  698. /// Return true if the loop body is safe to clone in practice.
  699. bool isSafeToClone() const;
  700. /// Returns true if the loop is annotated parallel.
  701. ///
  702. /// A parallel loop can be assumed to not contain any dependencies between
  703. /// iterations by the compiler. That is, any loop-carried dependency checking
  704. /// can be skipped completely when parallelizing the loop on the target
  705. /// machine. Thus, if the parallel loop information originates from the
  706. /// programmer, e.g. via the OpenMP parallel for pragma, it is the
  707. /// programmer's responsibility to ensure there are no loop-carried
  708. /// dependencies. The final execution order of the instructions across
  709. /// iterations is not guaranteed, thus, the end result might or might not
  710. /// implement actual concurrent execution of instructions across multiple
  711. /// iterations.
  712. bool isAnnotatedParallel() const;
  713. /// Return the llvm.loop loop id metadata node for this loop if it is present.
  714. ///
  715. /// If this loop contains the same llvm.loop metadata on each branch to the
  716. /// header then the node is returned. If any latch instruction does not
  717. /// contain llvm.loop or if multiple latches contain different nodes then
  718. /// 0 is returned.
  719. MDNode *getLoopID() const;
  720. /// Set the llvm.loop loop id metadata for this loop.
  721. ///
  722. /// The LoopID metadata node will be added to each terminator instruction in
  723. /// the loop that branches to the loop header.
  724. ///
  725. /// The LoopID metadata node should have one or more operands and the first
  726. /// operand should be the node itself.
  727. void setLoopID(MDNode *LoopID) const;
  728. /// Add llvm.loop.unroll.disable to this loop's loop id metadata.
  729. ///
  730. /// Remove existing unroll metadata and add unroll disable metadata to
  731. /// indicate the loop has already been unrolled. This prevents a loop
  732. /// from being unrolled more than is directed by a pragma if the loop
  733. /// unrolling pass is run more than once (which it generally is).
  734. void setLoopAlreadyUnrolled();
  735. /// Add llvm.loop.mustprogress to this loop's loop id metadata.
  736. void setLoopMustProgress();
  737. void dump() const;
  738. void dumpVerbose() const;
  739. /// Return the debug location of the start of this loop.
  740. /// This looks for a BB terminating instruction with a known debug
  741. /// location by looking at the preheader and header blocks. If it
  742. /// cannot find a terminating instruction with location information,
  743. /// it returns an unknown location.
  744. DebugLoc getStartLoc() const;
  745. /// Return the source code span of the loop.
  746. LocRange getLocRange() const;
  747. StringRef getName() const {
  748. if (BasicBlock *Header = getHeader())
  749. if (Header->hasName())
  750. return Header->getName();
  751. return "<unnamed loop>";
  752. }
  753. private:
  754. Loop() = default;
  755. friend class LoopInfoBase<BasicBlock, Loop>;
  756. friend class LoopBase<BasicBlock, Loop>;
  757. explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
  758. ~Loop() = default;
  759. };
  760. //===----------------------------------------------------------------------===//
  761. /// This class builds and contains all of the top-level loop
  762. /// structures in the specified function.
  763. ///
  764. template <class BlockT, class LoopT> class LoopInfoBase {
  765. // BBMap - Mapping of basic blocks to the inner most loop they occur in
  766. DenseMap<const BlockT *, LoopT *> BBMap;
  767. std::vector<LoopT *> TopLevelLoops;
  768. BumpPtrAllocator LoopAllocator;
  769. friend class LoopBase<BlockT, LoopT>;
  770. friend class LoopInfo;
  771. void operator=(const LoopInfoBase &) = delete;
  772. LoopInfoBase(const LoopInfoBase &) = delete;
  773. public:
  774. LoopInfoBase() {}
  775. ~LoopInfoBase() { releaseMemory(); }
  776. LoopInfoBase(LoopInfoBase &&Arg)
  777. : BBMap(std::move(Arg.BBMap)),
  778. TopLevelLoops(std::move(Arg.TopLevelLoops)),
  779. LoopAllocator(std::move(Arg.LoopAllocator)) {
  780. // We have to clear the arguments top level loops as we've taken ownership.
  781. Arg.TopLevelLoops.clear();
  782. }
  783. LoopInfoBase &operator=(LoopInfoBase &&RHS) {
  784. BBMap = std::move(RHS.BBMap);
  785. for (auto *L : TopLevelLoops)
  786. L->~LoopT();
  787. TopLevelLoops = std::move(RHS.TopLevelLoops);
  788. LoopAllocator = std::move(RHS.LoopAllocator);
  789. RHS.TopLevelLoops.clear();
  790. return *this;
  791. }
  792. void releaseMemory() {
  793. BBMap.clear();
  794. for (auto *L : TopLevelLoops)
  795. L->~LoopT();
  796. TopLevelLoops.clear();
  797. LoopAllocator.Reset();
  798. }
  799. template <typename... ArgsTy> LoopT *AllocateLoop(ArgsTy &&... Args) {
  800. LoopT *Storage = LoopAllocator.Allocate<LoopT>();
  801. return new (Storage) LoopT(std::forward<ArgsTy>(Args)...);
  802. }
  803. /// iterator/begin/end - The interface to the top-level loops in the current
  804. /// function.
  805. ///
  806. typedef typename std::vector<LoopT *>::const_iterator iterator;
  807. typedef
  808. typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
  809. iterator begin() const { return TopLevelLoops.begin(); }
  810. iterator end() const { return TopLevelLoops.end(); }
  811. reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
  812. reverse_iterator rend() const { return TopLevelLoops.rend(); }
  813. bool empty() const { return TopLevelLoops.empty(); }
  814. /// Return all of the loops in the function in preorder across the loop
  815. /// nests, with siblings in forward program order.
  816. ///
  817. /// Note that because loops form a forest of trees, preorder is equivalent to
  818. /// reverse postorder.
  819. SmallVector<LoopT *, 4> getLoopsInPreorder();
  820. /// Return all of the loops in the function in preorder across the loop
  821. /// nests, with siblings in *reverse* program order.
  822. ///
  823. /// Note that because loops form a forest of trees, preorder is equivalent to
  824. /// reverse postorder.
  825. ///
  826. /// Also note that this is *not* a reverse preorder. Only the siblings are in
  827. /// reverse program order.
  828. SmallVector<LoopT *, 4> getLoopsInReverseSiblingPreorder();
  829. /// Return the inner most loop that BB lives in. If a basic block is in no
  830. /// loop (for example the entry node), null is returned.
  831. LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
  832. /// Same as getLoopFor.
  833. const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); }
  834. /// Return the loop nesting level of the specified block. A depth of 0 means
  835. /// the block is not inside any loop.
  836. unsigned getLoopDepth(const BlockT *BB) const {
  837. const LoopT *L = getLoopFor(BB);
  838. return L ? L->getLoopDepth() : 0;
  839. }
  840. // True if the block is a loop header node
  841. bool isLoopHeader(const BlockT *BB) const {
  842. const LoopT *L = getLoopFor(BB);
  843. return L && L->getHeader() == BB;
  844. }
  845. /// Return the top-level loops.
  846. const std::vector<LoopT *> &getTopLevelLoops() const { return TopLevelLoops; }
  847. /// Return the top-level loops.
  848. std::vector<LoopT *> &getTopLevelLoopsVector() { return TopLevelLoops; }
  849. /// This removes the specified top-level loop from this loop info object.
  850. /// The loop is not deleted, as it will presumably be inserted into
  851. /// another loop.
  852. LoopT *removeLoop(iterator I) {
  853. assert(I != end() && "Cannot remove end iterator!");
  854. LoopT *L = *I;
  855. assert(L->isOutermost() && "Not a top-level loop!");
  856. TopLevelLoops.erase(TopLevelLoops.begin() + (I - begin()));
  857. return L;
  858. }
  859. /// Change the top-level loop that contains BB to the specified loop.
  860. /// This should be used by transformations that restructure the loop hierarchy
  861. /// tree.
  862. void changeLoopFor(BlockT *BB, LoopT *L) {
  863. if (!L) {
  864. BBMap.erase(BB);
  865. return;
  866. }
  867. BBMap[BB] = L;
  868. }
  869. /// Replace the specified loop in the top-level loops list with the indicated
  870. /// loop.
  871. void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) {
  872. auto I = find(TopLevelLoops, OldLoop);
  873. assert(I != TopLevelLoops.end() && "Old loop not at top level!");
  874. *I = NewLoop;
  875. assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
  876. "Loops already embedded into a subloop!");
  877. }
  878. /// This adds the specified loop to the collection of top-level loops.
  879. void addTopLevelLoop(LoopT *New) {
  880. assert(New->isOutermost() && "Loop already in subloop!");
  881. TopLevelLoops.push_back(New);
  882. }
  883. /// This method completely removes BB from all data structures,
  884. /// including all of the Loop objects it is nested in and our mapping from
  885. /// BasicBlocks to loops.
  886. void removeBlock(BlockT *BB) {
  887. auto I = BBMap.find(BB);
  888. if (I != BBMap.end()) {
  889. for (LoopT *L = I->second; L; L = L->getParentLoop())
  890. L->removeBlockFromLoop(BB);
  891. BBMap.erase(I);
  892. }
  893. }
  894. // Internals
  895. static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
  896. const LoopT *ParentLoop) {
  897. if (!SubLoop)
  898. return true;
  899. if (SubLoop == ParentLoop)
  900. return false;
  901. return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
  902. }
  903. /// Create the loop forest using a stable algorithm.
  904. void analyze(const DominatorTreeBase<BlockT, false> &DomTree);
  905. // Debugging
  906. void print(raw_ostream &OS) const;
  907. void verify(const DominatorTreeBase<BlockT, false> &DomTree) const;
  908. /// Destroy a loop that has been removed from the `LoopInfo` nest.
  909. ///
  910. /// This runs the destructor of the loop object making it invalid to
  911. /// reference afterward. The memory is retained so that the *pointer* to the
  912. /// loop remains valid.
  913. ///
  914. /// The caller is responsible for removing this loop from the loop nest and
  915. /// otherwise disconnecting it from the broader `LoopInfo` data structures.
  916. /// Callers that don't naturally handle this themselves should probably call
  917. /// `erase' instead.
  918. void destroy(LoopT *L) {
  919. L->~LoopT();
  920. // Since LoopAllocator is a BumpPtrAllocator, this Deallocate only poisons
  921. // \c L, but the pointer remains valid for non-dereferencing uses.
  922. LoopAllocator.Deallocate(L);
  923. }
  924. };
  925. // Implementation in LoopInfoImpl.h
  926. extern template class LoopInfoBase<BasicBlock, Loop>;
  927. class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
  928. typedef LoopInfoBase<BasicBlock, Loop> BaseT;
  929. friend class LoopBase<BasicBlock, Loop>;
  930. void operator=(const LoopInfo &) = delete;
  931. LoopInfo(const LoopInfo &) = delete;
  932. public:
  933. LoopInfo() {}
  934. explicit LoopInfo(const DominatorTreeBase<BasicBlock, false> &DomTree);
  935. LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
  936. LoopInfo &operator=(LoopInfo &&RHS) {
  937. BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
  938. return *this;
  939. }
  940. /// Handle invalidation explicitly.
  941. bool invalidate(Function &F, const PreservedAnalyses &PA,
  942. FunctionAnalysisManager::Invalidator &);
  943. // Most of the public interface is provided via LoopInfoBase.
  944. /// Update LoopInfo after removing the last backedge from a loop. This updates
  945. /// the loop forest and parent loops for each block so that \c L is no longer
  946. /// referenced, but does not actually delete \c L immediately. The pointer
  947. /// will remain valid until this LoopInfo's memory is released.
  948. void erase(Loop *L);
  949. /// Returns true if replacing From with To everywhere is guaranteed to
  950. /// preserve LCSSA form.
  951. bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
  952. // Preserving LCSSA form is only problematic if the replacing value is an
  953. // instruction.
  954. Instruction *I = dyn_cast<Instruction>(To);
  955. if (!I)
  956. return true;
  957. // If both instructions are defined in the same basic block then replacement
  958. // cannot break LCSSA form.
  959. if (I->getParent() == From->getParent())
  960. return true;
  961. // If the instruction is not defined in a loop then it can safely replace
  962. // anything.
  963. Loop *ToLoop = getLoopFor(I->getParent());
  964. if (!ToLoop)
  965. return true;
  966. // If the replacing instruction is defined in the same loop as the original
  967. // instruction, or in a loop that contains it as an inner loop, then using
  968. // it as a replacement will not break LCSSA form.
  969. return ToLoop->contains(getLoopFor(From->getParent()));
  970. }
  971. /// Checks if moving a specific instruction can break LCSSA in any loop.
  972. ///
  973. /// Return true if moving \p Inst to before \p NewLoc will break LCSSA,
  974. /// assuming that the function containing \p Inst and \p NewLoc is currently
  975. /// in LCSSA form.
  976. bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc) {
  977. assert(Inst->getFunction() == NewLoc->getFunction() &&
  978. "Can't reason about IPO!");
  979. auto *OldBB = Inst->getParent();
  980. auto *NewBB = NewLoc->getParent();
  981. // Movement within the same loop does not break LCSSA (the equality check is
  982. // to avoid doing a hashtable lookup in case of intra-block movement).
  983. if (OldBB == NewBB)
  984. return true;
  985. auto *OldLoop = getLoopFor(OldBB);
  986. auto *NewLoop = getLoopFor(NewBB);
  987. if (OldLoop == NewLoop)
  988. return true;
  989. // Check if Outer contains Inner; with the null loop counting as the
  990. // "outermost" loop.
  991. auto Contains = [](const Loop *Outer, const Loop *Inner) {
  992. return !Outer || Outer->contains(Inner);
  993. };
  994. // To check that the movement of Inst to before NewLoc does not break LCSSA,
  995. // we need to check two sets of uses for possible LCSSA violations at
  996. // NewLoc: the users of NewInst, and the operands of NewInst.
  997. // If we know we're hoisting Inst out of an inner loop to an outer loop,
  998. // then the uses *of* Inst don't need to be checked.
  999. if (!Contains(NewLoop, OldLoop)) {
  1000. for (Use &U : Inst->uses()) {
  1001. auto *UI = cast<Instruction>(U.getUser());
  1002. auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U)
  1003. : UI->getParent();
  1004. if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
  1005. return false;
  1006. }
  1007. }
  1008. // If we know we're sinking Inst from an outer loop into an inner loop, then
  1009. // the *operands* of Inst don't need to be checked.
  1010. if (!Contains(OldLoop, NewLoop)) {
  1011. // See below on why we can't handle phi nodes here.
  1012. if (isa<PHINode>(Inst))
  1013. return false;
  1014. for (Use &U : Inst->operands()) {
  1015. auto *DefI = dyn_cast<Instruction>(U.get());
  1016. if (!DefI)
  1017. return false;
  1018. // This would need adjustment if we allow Inst to be a phi node -- the
  1019. // new use block won't simply be NewBB.
  1020. auto *DefBlock = DefI->getParent();
  1021. if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
  1022. return false;
  1023. }
  1024. }
  1025. return true;
  1026. }
  1027. // Return true if a new use of V added in ExitBB would require an LCSSA PHI
  1028. // to be inserted at the begining of the block. Note that V is assumed to
  1029. // dominate ExitBB, and ExitBB must be the exit block of some loop. The
  1030. // IR is assumed to be in LCSSA form before the planned insertion.
  1031. bool wouldBeOutOfLoopUseRequiringLCSSA(const Value *V,
  1032. const BasicBlock *ExitBB) const;
  1033. };
  1034. // Allow clients to walk the list of nested loops...
  1035. template <> struct GraphTraits<const Loop *> {
  1036. typedef const Loop *NodeRef;
  1037. typedef LoopInfo::iterator ChildIteratorType;
  1038. static NodeRef getEntryNode(const Loop *L) { return L; }
  1039. static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
  1040. static ChildIteratorType child_end(NodeRef N) { return N->end(); }
  1041. };
  1042. template <> struct GraphTraits<Loop *> {
  1043. typedef Loop *NodeRef;
  1044. typedef LoopInfo::iterator ChildIteratorType;
  1045. static NodeRef getEntryNode(Loop *L) { return L; }
  1046. static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
  1047. static ChildIteratorType child_end(NodeRef N) { return N->end(); }
  1048. };
  1049. /// Analysis pass that exposes the \c LoopInfo for a function.
  1050. class LoopAnalysis : public AnalysisInfoMixin<LoopAnalysis> {
  1051. friend AnalysisInfoMixin<LoopAnalysis>;
  1052. static AnalysisKey Key;
  1053. public:
  1054. typedef LoopInfo Result;
  1055. LoopInfo run(Function &F, FunctionAnalysisManager &AM);
  1056. };
  1057. /// Printer pass for the \c LoopAnalysis results.
  1058. class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> {
  1059. raw_ostream &OS;
  1060. public:
  1061. explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
  1062. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  1063. };
  1064. /// Verifier pass for the \c LoopAnalysis results.
  1065. struct LoopVerifierPass : public PassInfoMixin<LoopVerifierPass> {
  1066. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  1067. };
  1068. /// The legacy pass manager's analysis pass to compute loop information.
  1069. class LoopInfoWrapperPass : public FunctionPass {
  1070. LoopInfo LI;
  1071. public:
  1072. static char ID; // Pass identification, replacement for typeid
  1073. LoopInfoWrapperPass();
  1074. LoopInfo &getLoopInfo() { return LI; }
  1075. const LoopInfo &getLoopInfo() const { return LI; }
  1076. /// Calculate the natural loop information for a given function.
  1077. bool runOnFunction(Function &F) override;
  1078. void verifyAnalysis() const override;
  1079. void releaseMemory() override { LI.releaseMemory(); }
  1080. void print(raw_ostream &O, const Module *M = nullptr) const override;
  1081. void getAnalysisUsage(AnalysisUsage &AU) const override;
  1082. };
  1083. /// Function to print a loop's contents as LLVM's text IR assembly.
  1084. void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner = "");
  1085. /// Find and return the loop attribute node for the attribute @p Name in
  1086. /// @p LoopID. Return nullptr if there is no such attribute.
  1087. MDNode *findOptionMDForLoopID(MDNode *LoopID, StringRef Name);
  1088. /// Find string metadata for a loop.
  1089. ///
  1090. /// Returns the MDNode where the first operand is the metadata's name. The
  1091. /// following operands are the metadata's values. If no metadata with @p Name is
  1092. /// found, return nullptr.
  1093. MDNode *findOptionMDForLoop(const Loop *TheLoop, StringRef Name);
  1094. /// Return whether an MDNode might represent an access group.
  1095. ///
  1096. /// Access group metadata nodes have to be distinct and empty. Being
  1097. /// always-empty ensures that it never needs to be changed (which -- because
  1098. /// MDNodes are designed immutable -- would require creating a new MDNode). Note
  1099. /// that this is not a sufficient condition: not every distinct and empty NDNode
  1100. /// is representing an access group.
  1101. bool isValidAsAccessGroup(MDNode *AccGroup);
  1102. /// Create a new LoopID after the loop has been transformed.
  1103. ///
  1104. /// This can be used when no follow-up loop attributes are defined
  1105. /// (llvm::makeFollowupLoopID returning None) to stop transformations to be
  1106. /// applied again.
  1107. ///
  1108. /// @param Context The LLVMContext in which to create the new LoopID.
  1109. /// @param OrigLoopID The original LoopID; can be nullptr if the original
  1110. /// loop has no LoopID.
  1111. /// @param RemovePrefixes Remove all loop attributes that have these prefixes.
  1112. /// Use to remove metadata of the transformation that has
  1113. /// been applied.
  1114. /// @param AddAttrs Add these loop attributes to the new LoopID.
  1115. ///
  1116. /// @return A new LoopID that can be applied using Loop::setLoopID().
  1117. llvm::MDNode *
  1118. makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID,
  1119. llvm::ArrayRef<llvm::StringRef> RemovePrefixes,
  1120. llvm::ArrayRef<llvm::MDNode *> AddAttrs);
  1121. } // End llvm namespace
  1122. #endif