LoopInfo.h 52 KB

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