LoopInfo.h 52 KB

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