LoopInfo.h 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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, unsigned Depth = 0, bool Verbose = false) const;
  424. protected:
  425. friend class LoopInfoBase<BlockT, LoopT>;
  426. /// This creates an empty loop.
  427. LoopBase() : ParentLoop(nullptr) {}
  428. explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
  429. Blocks.push_back(BB);
  430. DenseBlockSet.insert(BB);
  431. }
  432. // Since loop passes like SCEV are allowed to key analysis results off of
  433. // `Loop` pointers, we cannot re-use pointers within a loop pass manager.
  434. // This means loop passes should not be `delete` ing `Loop` objects directly
  435. // (and risk a later `Loop` allocation re-using the address of a previous one)
  436. // but should be using LoopInfo::markAsRemoved, which keeps around the `Loop`
  437. // pointer till the end of the lifetime of the `LoopInfo` object.
  438. //
  439. // To make it easier to follow this rule, we mark the destructor as
  440. // non-public.
  441. ~LoopBase() {
  442. for (auto *SubLoop : SubLoops)
  443. SubLoop->~LoopT();
  444. #if LLVM_ENABLE_ABI_BREAKING_CHECKS
  445. IsInvalid = true;
  446. #endif
  447. SubLoops.clear();
  448. Blocks.clear();
  449. DenseBlockSet.clear();
  450. ParentLoop = nullptr;
  451. }
  452. };
  453. template <class BlockT, class LoopT>
  454. raw_ostream &operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
  455. Loop.print(OS);
  456. return OS;
  457. }
  458. // Implementation in LoopInfoImpl.h
  459. extern template class LoopBase<BasicBlock, Loop>;
  460. /// Represents a single loop in the control flow graph. Note that not all SCCs
  461. /// in the CFG are necessarily loops.
  462. class Loop : public LoopBase<BasicBlock, Loop> {
  463. public:
  464. /// A range representing the start and end location of a loop.
  465. class LocRange {
  466. DebugLoc Start;
  467. DebugLoc End;
  468. public:
  469. LocRange() {}
  470. LocRange(DebugLoc Start) : Start(Start), End(Start) {}
  471. LocRange(DebugLoc Start, DebugLoc End)
  472. : Start(std::move(Start)), End(std::move(End)) {}
  473. const DebugLoc &getStart() const { return Start; }
  474. const DebugLoc &getEnd() const { return End; }
  475. /// Check for null.
  476. ///
  477. explicit operator bool() const { return Start && End; }
  478. };
  479. /// Return true if the specified value is loop invariant.
  480. bool isLoopInvariant(const Value *V) const;
  481. /// Return true if all the operands of the specified instruction are loop
  482. /// invariant.
  483. bool hasLoopInvariantOperands(const Instruction *I) const;
  484. /// If the given value is an instruction inside of the loop and it can be
  485. /// hoisted, do so to make it trivially loop-invariant.
  486. /// Return true if the value after any hoisting is loop invariant. This
  487. /// function can be used as a slightly more aggressive replacement for
  488. /// isLoopInvariant.
  489. ///
  490. /// If InsertPt is specified, it is the point to hoist instructions to.
  491. /// If null, the terminator of the loop preheader is used.
  492. bool makeLoopInvariant(Value *V, bool &Changed,
  493. Instruction *InsertPt = nullptr,
  494. MemorySSAUpdater *MSSAU = nullptr) const;
  495. /// If the given instruction is inside of the loop and it can be hoisted, do
  496. /// so to make it trivially loop-invariant.
  497. /// Return true if the instruction after any hoisting is loop invariant. This
  498. /// function can be used as a slightly more aggressive replacement for
  499. /// isLoopInvariant.
  500. ///
  501. /// If InsertPt is specified, it is the point to hoist instructions to.
  502. /// If null, the terminator of the loop preheader is used.
  503. ///
  504. bool makeLoopInvariant(Instruction *I, bool &Changed,
  505. Instruction *InsertPt = nullptr,
  506. MemorySSAUpdater *MSSAU = nullptr) const;
  507. /// Check to see if the loop has a canonical induction variable: an integer
  508. /// recurrence that starts at 0 and increments by one each time through the
  509. /// loop. If so, return the phi node that corresponds to it.
  510. ///
  511. /// The IndVarSimplify pass transforms loops to have a canonical induction
  512. /// variable.
  513. ///
  514. PHINode *getCanonicalInductionVariable() const;
  515. /// Obtain the unique incoming and back edge. Return false if they are
  516. /// non-unique or the loop is dead; otherwise, return true.
  517. bool getIncomingAndBackEdge(BasicBlock *&Incoming,
  518. BasicBlock *&Backedge) const;
  519. /// Below are some utilities to get the loop guard, loop bounds and induction
  520. /// variable, and to check if a given phinode is an auxiliary induction
  521. /// variable, if the loop is guarded, and if the loop is canonical.
  522. ///
  523. /// Here is an example:
  524. /// \code
  525. /// for (int i = lb; i < ub; i+=step)
  526. /// <loop body>
  527. /// --- pseudo LLVMIR ---
  528. /// beforeloop:
  529. /// guardcmp = (lb < ub)
  530. /// if (guardcmp) goto preheader; else goto afterloop
  531. /// preheader:
  532. /// loop:
  533. /// i_1 = phi[{lb, preheader}, {i_2, latch}]
  534. /// <loop body>
  535. /// i_2 = i_1 + step
  536. /// latch:
  537. /// cmp = (i_2 < ub)
  538. /// if (cmp) goto loop
  539. /// exit:
  540. /// afterloop:
  541. /// \endcode
  542. ///
  543. /// - getBounds
  544. /// - getInitialIVValue --> lb
  545. /// - getStepInst --> i_2 = i_1 + step
  546. /// - getStepValue --> step
  547. /// - getFinalIVValue --> ub
  548. /// - getCanonicalPredicate --> '<'
  549. /// - getDirection --> Increasing
  550. ///
  551. /// - getInductionVariable --> i_1
  552. /// - isAuxiliaryInductionVariable(x) --> true if x == i_1
  553. /// - getLoopGuardBranch()
  554. /// --> `if (guardcmp) goto preheader; else goto afterloop`
  555. /// - isGuarded() --> true
  556. /// - isCanonical --> false
  557. struct LoopBounds {
  558. /// Return the LoopBounds object if
  559. /// - the given \p IndVar is an induction variable
  560. /// - the initial value of the induction variable can be found
  561. /// - the step instruction of the induction variable can be found
  562. /// - the final value of the induction variable can be found
  563. ///
  564. /// Else None.
  565. static Optional<Loop::LoopBounds> getBounds(const Loop &L, PHINode &IndVar,
  566. ScalarEvolution &SE);
  567. /// Get the initial value of the loop induction variable.
  568. Value &getInitialIVValue() const { return InitialIVValue; }
  569. /// Get the instruction that updates the loop induction variable.
  570. Instruction &getStepInst() const { return StepInst; }
  571. /// Get the step that the loop induction variable gets updated by in each
  572. /// loop iteration. Return nullptr if not found.
  573. Value *getStepValue() const { return StepValue; }
  574. /// Get the final value of the loop induction variable.
  575. Value &getFinalIVValue() const { return FinalIVValue; }
  576. /// Return the canonical predicate for the latch compare instruction, if
  577. /// able to be calcuated. Else BAD_ICMP_PREDICATE.
  578. ///
  579. /// A predicate is considered as canonical if requirements below are all
  580. /// satisfied:
  581. /// 1. The first successor of the latch branch is the loop header
  582. /// If not, inverse the predicate.
  583. /// 2. One of the operands of the latch comparison is StepInst
  584. /// If not, and
  585. /// - if the current calcuated predicate is not ne or eq, flip the
  586. /// predicate.
  587. /// - else if the loop is increasing, return slt
  588. /// (notice that it is safe to change from ne or eq to sign compare)
  589. /// - else if the loop is decreasing, return sgt
  590. /// (notice that it is safe to change from ne or eq to sign compare)
  591. ///
  592. /// Here is an example when both (1) and (2) are not satisfied:
  593. /// \code
  594. /// loop.header:
  595. /// %iv = phi [%initialiv, %loop.preheader], [%inc, %loop.header]
  596. /// %inc = add %iv, %step
  597. /// %cmp = slt %iv, %finaliv
  598. /// br %cmp, %loop.exit, %loop.header
  599. /// loop.exit:
  600. /// \endcode
  601. /// - The second successor of the latch branch is the loop header instead
  602. /// of the first successor (slt -> sge)
  603. /// - The first operand of the latch comparison (%cmp) is the IndVar (%iv)
  604. /// instead of the StepInst (%inc) (sge -> sgt)
  605. ///
  606. /// The predicate would be sgt if both (1) and (2) are satisfied.
  607. /// getCanonicalPredicate() returns sgt for this example.
  608. /// Note: The IR is not changed.
  609. ICmpInst::Predicate getCanonicalPredicate() const;
  610. /// An enum for the direction of the loop
  611. /// - for (int i = 0; i < ub; ++i) --> Increasing
  612. /// - for (int i = ub; i > 0; --i) --> Descresing
  613. /// - for (int i = x; i != y; i+=z) --> Unknown
  614. enum class Direction { Increasing, Decreasing, Unknown };
  615. /// Get the direction of the loop.
  616. Direction getDirection() const;
  617. private:
  618. LoopBounds(const Loop &Loop, Value &I, Instruction &SI, Value *SV, Value &F,
  619. ScalarEvolution &SE)
  620. : L(Loop), InitialIVValue(I), StepInst(SI), StepValue(SV),
  621. FinalIVValue(F), SE(SE) {}
  622. const Loop &L;
  623. // The initial value of the loop induction variable
  624. Value &InitialIVValue;
  625. // The instruction that updates the loop induction variable
  626. Instruction &StepInst;
  627. // The value that the loop induction variable gets updated by in each loop
  628. // iteration
  629. Value *StepValue;
  630. // The final value of the loop induction variable
  631. Value &FinalIVValue;
  632. ScalarEvolution &SE;
  633. };
  634. /// Return the struct LoopBounds collected if all struct members are found,
  635. /// else None.
  636. Optional<LoopBounds> getBounds(ScalarEvolution &SE) const;
  637. /// Return the loop induction variable if found, else return nullptr.
  638. /// An instruction is considered as the loop induction variable if
  639. /// - it is an induction variable of the loop; and
  640. /// - it is used to determine the condition of the branch in the loop latch
  641. ///
  642. /// Note: the induction variable doesn't need to be canonical, i.e. starts at
  643. /// zero and increments by one each time through the loop (but it can be).
  644. PHINode *getInductionVariable(ScalarEvolution &SE) const;
  645. /// Get the loop induction descriptor for the loop induction variable. Return
  646. /// true if the loop induction variable is found.
  647. bool getInductionDescriptor(ScalarEvolution &SE,
  648. InductionDescriptor &IndDesc) const;
  649. /// Return true if the given PHINode \p AuxIndVar is
  650. /// - in the loop header
  651. /// - not used outside of the loop
  652. /// - incremented by a loop invariant step for each loop iteration
  653. /// - step instruction opcode should be add or sub
  654. /// Note: auxiliary induction variable is not required to be used in the
  655. /// conditional branch in the loop latch. (but it can be)
  656. bool isAuxiliaryInductionVariable(PHINode &AuxIndVar,
  657. ScalarEvolution &SE) const;
  658. /// Return the loop guard branch, if it exists.
  659. ///
  660. /// This currently only works on simplified loop, as it requires a preheader
  661. /// and a latch to identify the guard. It will work on loops of the form:
  662. /// \code
  663. /// GuardBB:
  664. /// br cond1, Preheader, ExitSucc <== GuardBranch
  665. /// Preheader:
  666. /// br Header
  667. /// Header:
  668. /// ...
  669. /// br Latch
  670. /// Latch:
  671. /// br cond2, Header, ExitBlock
  672. /// ExitBlock:
  673. /// br ExitSucc
  674. /// ExitSucc:
  675. /// \endcode
  676. BranchInst *getLoopGuardBranch() const;
  677. /// Return true iff the loop is
  678. /// - in simplify rotated form, and
  679. /// - guarded by a loop guard branch.
  680. bool isGuarded() const { return (getLoopGuardBranch() != nullptr); }
  681. /// Return true if the loop is in rotated form.
  682. ///
  683. /// This does not check if the loop was rotated by loop rotation, instead it
  684. /// only checks if the loop is in rotated form (has a valid latch that exists
  685. /// the loop).
  686. bool isRotatedForm() const {
  687. assert(!isInvalid() && "Loop not in a valid state!");
  688. BasicBlock *Latch = getLoopLatch();
  689. return Latch && isLoopExiting(Latch);
  690. }
  691. /// Return true if the loop induction variable starts at zero and increments
  692. /// by one each time through the loop.
  693. bool isCanonical(ScalarEvolution &SE) const;
  694. /// Return true if the Loop is in LCSSA form.
  695. bool isLCSSAForm(const DominatorTree &DT) const;
  696. /// Return true if this Loop and all inner subloops are in LCSSA form.
  697. bool isRecursivelyLCSSAForm(const DominatorTree &DT,
  698. const LoopInfo &LI) const;
  699. /// Return true if the Loop is in the form that the LoopSimplify form
  700. /// transforms loops to, which is sometimes called normal form.
  701. bool isLoopSimplifyForm() const;
  702. /// Return true if the loop body is safe to clone in practice.
  703. bool isSafeToClone() const;
  704. /// Returns true if the loop is annotated parallel.
  705. ///
  706. /// A parallel loop can be assumed to not contain any dependencies between
  707. /// iterations by the compiler. That is, any loop-carried dependency checking
  708. /// can be skipped completely when parallelizing the loop on the target
  709. /// machine. Thus, if the parallel loop information originates from the
  710. /// programmer, e.g. via the OpenMP parallel for pragma, it is the
  711. /// programmer's responsibility to ensure there are no loop-carried
  712. /// dependencies. The final execution order of the instructions across
  713. /// iterations is not guaranteed, thus, the end result might or might not
  714. /// implement actual concurrent execution of instructions across multiple
  715. /// iterations.
  716. bool isAnnotatedParallel() const;
  717. /// Return the llvm.loop loop id metadata node for this loop if it is present.
  718. ///
  719. /// If this loop contains the same llvm.loop metadata on each branch to the
  720. /// header then the node is returned. If any latch instruction does not
  721. /// contain llvm.loop or if multiple latches contain different nodes then
  722. /// 0 is returned.
  723. MDNode *getLoopID() const;
  724. /// Set the llvm.loop loop id metadata for this loop.
  725. ///
  726. /// The LoopID metadata node will be added to each terminator instruction in
  727. /// the loop that branches to the loop header.
  728. ///
  729. /// The LoopID metadata node should have one or more operands and the first
  730. /// operand should be the node itself.
  731. void setLoopID(MDNode *LoopID) const;
  732. /// Add llvm.loop.unroll.disable to this loop's loop id metadata.
  733. ///
  734. /// Remove existing unroll metadata and add unroll disable metadata to
  735. /// indicate the loop has already been unrolled. This prevents a loop
  736. /// from being unrolled more than is directed by a pragma if the loop
  737. /// unrolling pass is run more than once (which it generally is).
  738. void setLoopAlreadyUnrolled();
  739. /// Add llvm.loop.mustprogress to this loop's loop id metadata.
  740. void setLoopMustProgress();
  741. void dump() const;
  742. void dumpVerbose() const;
  743. /// Return the debug location of the start of this loop.
  744. /// This looks for a BB terminating instruction with a known debug
  745. /// location by looking at the preheader and header blocks. If it
  746. /// cannot find a terminating instruction with location information,
  747. /// it returns an unknown location.
  748. DebugLoc getStartLoc() const;
  749. /// Return the source code span of the loop.
  750. LocRange getLocRange() const;
  751. StringRef getName() const {
  752. if (BasicBlock *Header = getHeader())
  753. if (Header->hasName())
  754. return Header->getName();
  755. return "<unnamed loop>";
  756. }
  757. private:
  758. Loop() = default;
  759. friend class LoopInfoBase<BasicBlock, Loop>;
  760. friend class LoopBase<BasicBlock, Loop>;
  761. explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
  762. ~Loop() = default;
  763. };
  764. //===----------------------------------------------------------------------===//
  765. /// This class builds and contains all of the top-level loop
  766. /// structures in the specified function.
  767. ///
  768. template <class BlockT, class LoopT> class LoopInfoBase {
  769. // BBMap - Mapping of basic blocks to the inner most loop they occur in
  770. DenseMap<const BlockT *, LoopT *> BBMap;
  771. std::vector<LoopT *> TopLevelLoops;
  772. BumpPtrAllocator LoopAllocator;
  773. friend class LoopBase<BlockT, LoopT>;
  774. friend class LoopInfo;
  775. void operator=(const LoopInfoBase &) = delete;
  776. LoopInfoBase(const LoopInfoBase &) = delete;
  777. public:
  778. LoopInfoBase() {}
  779. ~LoopInfoBase() { releaseMemory(); }
  780. LoopInfoBase(LoopInfoBase &&Arg)
  781. : BBMap(std::move(Arg.BBMap)),
  782. TopLevelLoops(std::move(Arg.TopLevelLoops)),
  783. LoopAllocator(std::move(Arg.LoopAllocator)) {
  784. // We have to clear the arguments top level loops as we've taken ownership.
  785. Arg.TopLevelLoops.clear();
  786. }
  787. LoopInfoBase &operator=(LoopInfoBase &&RHS) {
  788. BBMap = std::move(RHS.BBMap);
  789. for (auto *L : TopLevelLoops)
  790. L->~LoopT();
  791. TopLevelLoops = std::move(RHS.TopLevelLoops);
  792. LoopAllocator = std::move(RHS.LoopAllocator);
  793. RHS.TopLevelLoops.clear();
  794. return *this;
  795. }
  796. void releaseMemory() {
  797. BBMap.clear();
  798. for (auto *L : TopLevelLoops)
  799. L->~LoopT();
  800. TopLevelLoops.clear();
  801. LoopAllocator.Reset();
  802. }
  803. template <typename... ArgsTy> LoopT *AllocateLoop(ArgsTy &&... Args) {
  804. LoopT *Storage = LoopAllocator.Allocate<LoopT>();
  805. return new (Storage) LoopT(std::forward<ArgsTy>(Args)...);
  806. }
  807. /// iterator/begin/end - The interface to the top-level loops in the current
  808. /// function.
  809. ///
  810. typedef typename std::vector<LoopT *>::const_iterator iterator;
  811. typedef
  812. typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
  813. iterator begin() const { return TopLevelLoops.begin(); }
  814. iterator end() const { return TopLevelLoops.end(); }
  815. reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
  816. reverse_iterator rend() const { return TopLevelLoops.rend(); }
  817. bool empty() const { return TopLevelLoops.empty(); }
  818. /// Return all of the loops in the function in preorder across the loop
  819. /// nests, with siblings in forward program order.
  820. ///
  821. /// Note that because loops form a forest of trees, preorder is equivalent to
  822. /// reverse postorder.
  823. SmallVector<LoopT *, 4> getLoopsInPreorder();
  824. /// Return all of the loops in the function in preorder across the loop
  825. /// nests, with siblings in *reverse* program order.
  826. ///
  827. /// Note that because loops form a forest of trees, preorder is equivalent to
  828. /// reverse postorder.
  829. ///
  830. /// Also note that this is *not* a reverse preorder. Only the siblings are in
  831. /// reverse program order.
  832. SmallVector<LoopT *, 4> getLoopsInReverseSiblingPreorder();
  833. /// Return the inner most loop that BB lives in. If a basic block is in no
  834. /// loop (for example the entry node), null is returned.
  835. LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
  836. /// Same as getLoopFor.
  837. const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); }
  838. /// Return the loop nesting level of the specified block. A depth of 0 means
  839. /// the block is not inside any loop.
  840. unsigned getLoopDepth(const BlockT *BB) const {
  841. const LoopT *L = getLoopFor(BB);
  842. return L ? L->getLoopDepth() : 0;
  843. }
  844. // True if the block is a loop header node
  845. bool isLoopHeader(const BlockT *BB) const {
  846. const LoopT *L = getLoopFor(BB);
  847. return L && L->getHeader() == BB;
  848. }
  849. /// Return the top-level loops.
  850. const std::vector<LoopT *> &getTopLevelLoops() const { return TopLevelLoops; }
  851. /// Return the top-level loops.
  852. std::vector<LoopT *> &getTopLevelLoopsVector() { return TopLevelLoops; }
  853. /// This removes the specified top-level loop from this loop info object.
  854. /// The loop is not deleted, as it will presumably be inserted into
  855. /// another loop.
  856. LoopT *removeLoop(iterator I) {
  857. assert(I != end() && "Cannot remove end iterator!");
  858. LoopT *L = *I;
  859. assert(L->isOutermost() && "Not a top-level loop!");
  860. TopLevelLoops.erase(TopLevelLoops.begin() + (I - begin()));
  861. return L;
  862. }
  863. /// Change the top-level loop that contains BB to the specified loop.
  864. /// This should be used by transformations that restructure the loop hierarchy
  865. /// tree.
  866. void changeLoopFor(BlockT *BB, LoopT *L) {
  867. if (!L) {
  868. BBMap.erase(BB);
  869. return;
  870. }
  871. BBMap[BB] = L;
  872. }
  873. /// Replace the specified loop in the top-level loops list with the indicated
  874. /// loop.
  875. void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) {
  876. auto I = find(TopLevelLoops, OldLoop);
  877. assert(I != TopLevelLoops.end() && "Old loop not at top level!");
  878. *I = NewLoop;
  879. assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
  880. "Loops already embedded into a subloop!");
  881. }
  882. /// This adds the specified loop to the collection of top-level loops.
  883. void addTopLevelLoop(LoopT *New) {
  884. assert(New->isOutermost() && "Loop already in subloop!");
  885. TopLevelLoops.push_back(New);
  886. }
  887. /// This method completely removes BB from all data structures,
  888. /// including all of the Loop objects it is nested in and our mapping from
  889. /// BasicBlocks to loops.
  890. void removeBlock(BlockT *BB) {
  891. auto I = BBMap.find(BB);
  892. if (I != BBMap.end()) {
  893. for (LoopT *L = I->second; L; L = L->getParentLoop())
  894. L->removeBlockFromLoop(BB);
  895. BBMap.erase(I);
  896. }
  897. }
  898. // Internals
  899. static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
  900. const LoopT *ParentLoop) {
  901. if (!SubLoop)
  902. return true;
  903. if (SubLoop == ParentLoop)
  904. return false;
  905. return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
  906. }
  907. /// Create the loop forest using a stable algorithm.
  908. void analyze(const DominatorTreeBase<BlockT, false> &DomTree);
  909. // Debugging
  910. void print(raw_ostream &OS) const;
  911. void verify(const DominatorTreeBase<BlockT, false> &DomTree) const;
  912. /// Destroy a loop that has been removed from the `LoopInfo` nest.
  913. ///
  914. /// This runs the destructor of the loop object making it invalid to
  915. /// reference afterward. The memory is retained so that the *pointer* to the
  916. /// loop remains valid.
  917. ///
  918. /// The caller is responsible for removing this loop from the loop nest and
  919. /// otherwise disconnecting it from the broader `LoopInfo` data structures.
  920. /// Callers that don't naturally handle this themselves should probably call
  921. /// `erase' instead.
  922. void destroy(LoopT *L) {
  923. L->~LoopT();
  924. // Since LoopAllocator is a BumpPtrAllocator, this Deallocate only poisons
  925. // \c L, but the pointer remains valid for non-dereferencing uses.
  926. LoopAllocator.Deallocate(L);
  927. }
  928. };
  929. // Implementation in LoopInfoImpl.h
  930. extern template class LoopInfoBase<BasicBlock, Loop>;
  931. class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
  932. typedef LoopInfoBase<BasicBlock, Loop> BaseT;
  933. friend class LoopBase<BasicBlock, Loop>;
  934. void operator=(const LoopInfo &) = delete;
  935. LoopInfo(const LoopInfo &) = delete;
  936. public:
  937. LoopInfo() {}
  938. explicit LoopInfo(const DominatorTreeBase<BasicBlock, false> &DomTree);
  939. LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
  940. LoopInfo &operator=(LoopInfo &&RHS) {
  941. BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
  942. return *this;
  943. }
  944. /// Handle invalidation explicitly.
  945. bool invalidate(Function &F, const PreservedAnalyses &PA,
  946. FunctionAnalysisManager::Invalidator &);
  947. // Most of the public interface is provided via LoopInfoBase.
  948. /// Update LoopInfo after removing the last backedge from a loop. This updates
  949. /// the loop forest and parent loops for each block so that \c L is no longer
  950. /// referenced, but does not actually delete \c L immediately. The pointer
  951. /// will remain valid until this LoopInfo's memory is released.
  952. void erase(Loop *L);
  953. /// Returns true if replacing From with To everywhere is guaranteed to
  954. /// preserve LCSSA form.
  955. bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
  956. // Preserving LCSSA form is only problematic if the replacing value is an
  957. // instruction.
  958. Instruction *I = dyn_cast<Instruction>(To);
  959. if (!I)
  960. return true;
  961. // If both instructions are defined in the same basic block then replacement
  962. // cannot break LCSSA form.
  963. if (I->getParent() == From->getParent())
  964. return true;
  965. // If the instruction is not defined in a loop then it can safely replace
  966. // anything.
  967. Loop *ToLoop = getLoopFor(I->getParent());
  968. if (!ToLoop)
  969. return true;
  970. // If the replacing instruction is defined in the same loop as the original
  971. // instruction, or in a loop that contains it as an inner loop, then using
  972. // it as a replacement will not break LCSSA form.
  973. return ToLoop->contains(getLoopFor(From->getParent()));
  974. }
  975. /// Checks if moving a specific instruction can break LCSSA in any loop.
  976. ///
  977. /// Return true if moving \p Inst to before \p NewLoc will break LCSSA,
  978. /// assuming that the function containing \p Inst and \p NewLoc is currently
  979. /// in LCSSA form.
  980. bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc) {
  981. assert(Inst->getFunction() == NewLoc->getFunction() &&
  982. "Can't reason about IPO!");
  983. auto *OldBB = Inst->getParent();
  984. auto *NewBB = NewLoc->getParent();
  985. // Movement within the same loop does not break LCSSA (the equality check is
  986. // to avoid doing a hashtable lookup in case of intra-block movement).
  987. if (OldBB == NewBB)
  988. return true;
  989. auto *OldLoop = getLoopFor(OldBB);
  990. auto *NewLoop = getLoopFor(NewBB);
  991. if (OldLoop == NewLoop)
  992. return true;
  993. // Check if Outer contains Inner; with the null loop counting as the
  994. // "outermost" loop.
  995. auto Contains = [](const Loop *Outer, const Loop *Inner) {
  996. return !Outer || Outer->contains(Inner);
  997. };
  998. // To check that the movement of Inst to before NewLoc does not break LCSSA,
  999. // we need to check two sets of uses for possible LCSSA violations at
  1000. // NewLoc: the users of NewInst, and the operands of NewInst.
  1001. // If we know we're hoisting Inst out of an inner loop to an outer loop,
  1002. // then the uses *of* Inst don't need to be checked.
  1003. if (!Contains(NewLoop, OldLoop)) {
  1004. for (Use &U : Inst->uses()) {
  1005. auto *UI = cast<Instruction>(U.getUser());
  1006. auto *UBB = isa<PHINode>(UI) ? cast<PHINode>(UI)->getIncomingBlock(U)
  1007. : UI->getParent();
  1008. if (UBB != NewBB && getLoopFor(UBB) != NewLoop)
  1009. return false;
  1010. }
  1011. }
  1012. // If we know we're sinking Inst from an outer loop into an inner loop, then
  1013. // the *operands* of Inst don't need to be checked.
  1014. if (!Contains(OldLoop, NewLoop)) {
  1015. // See below on why we can't handle phi nodes here.
  1016. if (isa<PHINode>(Inst))
  1017. return false;
  1018. for (Use &U : Inst->operands()) {
  1019. auto *DefI = dyn_cast<Instruction>(U.get());
  1020. if (!DefI)
  1021. return false;
  1022. // This would need adjustment if we allow Inst to be a phi node -- the
  1023. // new use block won't simply be NewBB.
  1024. auto *DefBlock = DefI->getParent();
  1025. if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)
  1026. return false;
  1027. }
  1028. }
  1029. return true;
  1030. }
  1031. };
  1032. // Allow clients to walk the list of nested loops...
  1033. template <> struct GraphTraits<const Loop *> {
  1034. typedef const Loop *NodeRef;
  1035. typedef LoopInfo::iterator ChildIteratorType;
  1036. static NodeRef getEntryNode(const Loop *L) { return L; }
  1037. static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
  1038. static ChildIteratorType child_end(NodeRef N) { return N->end(); }
  1039. };
  1040. template <> struct GraphTraits<Loop *> {
  1041. typedef Loop *NodeRef;
  1042. typedef LoopInfo::iterator ChildIteratorType;
  1043. static NodeRef getEntryNode(Loop *L) { return L; }
  1044. static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
  1045. static ChildIteratorType child_end(NodeRef N) { return N->end(); }
  1046. };
  1047. /// Analysis pass that exposes the \c LoopInfo for a function.
  1048. class LoopAnalysis : public AnalysisInfoMixin<LoopAnalysis> {
  1049. friend AnalysisInfoMixin<LoopAnalysis>;
  1050. static AnalysisKey Key;
  1051. public:
  1052. typedef LoopInfo Result;
  1053. LoopInfo run(Function &F, FunctionAnalysisManager &AM);
  1054. };
  1055. /// Printer pass for the \c LoopAnalysis results.
  1056. class LoopPrinterPass : public PassInfoMixin<LoopPrinterPass> {
  1057. raw_ostream &OS;
  1058. public:
  1059. explicit LoopPrinterPass(raw_ostream &OS) : OS(OS) {}
  1060. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  1061. };
  1062. /// Verifier pass for the \c LoopAnalysis results.
  1063. struct LoopVerifierPass : public PassInfoMixin<LoopVerifierPass> {
  1064. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  1065. };
  1066. /// The legacy pass manager's analysis pass to compute loop information.
  1067. class LoopInfoWrapperPass : public FunctionPass {
  1068. LoopInfo LI;
  1069. public:
  1070. static char ID; // Pass identification, replacement for typeid
  1071. LoopInfoWrapperPass();
  1072. LoopInfo &getLoopInfo() { return LI; }
  1073. const LoopInfo &getLoopInfo() const { return LI; }
  1074. /// Calculate the natural loop information for a given function.
  1075. bool runOnFunction(Function &F) override;
  1076. void verifyAnalysis() const override;
  1077. void releaseMemory() override { LI.releaseMemory(); }
  1078. void print(raw_ostream &O, const Module *M = nullptr) const override;
  1079. void getAnalysisUsage(AnalysisUsage &AU) const override;
  1080. };
  1081. /// Function to print a loop's contents as LLVM's text IR assembly.
  1082. void printLoop(Loop &L, raw_ostream &OS, const std::string &Banner = "");
  1083. /// Find and return the loop attribute node for the attribute @p Name in
  1084. /// @p LoopID. Return nullptr if there is no such attribute.
  1085. MDNode *findOptionMDForLoopID(MDNode *LoopID, StringRef Name);
  1086. /// Find string metadata for a loop.
  1087. ///
  1088. /// Returns the MDNode where the first operand is the metadata's name. The
  1089. /// following operands are the metadata's values. If no metadata with @p Name is
  1090. /// found, return nullptr.
  1091. MDNode *findOptionMDForLoop(const Loop *TheLoop, StringRef Name);
  1092. /// Return whether an MDNode might represent an access group.
  1093. ///
  1094. /// Access group metadata nodes have to be distinct and empty. Being
  1095. /// always-empty ensures that it never needs to be changed (which -- because
  1096. /// MDNodes are designed immutable -- would require creating a new MDNode). Note
  1097. /// that this is not a sufficient condition: not every distinct and empty NDNode
  1098. /// is representing an access group.
  1099. bool isValidAsAccessGroup(MDNode *AccGroup);
  1100. /// Create a new LoopID after the loop has been transformed.
  1101. ///
  1102. /// This can be used when no follow-up loop attributes are defined
  1103. /// (llvm::makeFollowupLoopID returning None) to stop transformations to be
  1104. /// applied again.
  1105. ///
  1106. /// @param Context The LLVMContext in which to create the new LoopID.
  1107. /// @param OrigLoopID The original LoopID; can be nullptr if the original
  1108. /// loop has no LoopID.
  1109. /// @param RemovePrefixes Remove all loop attributes that have these prefixes.
  1110. /// Use to remove metadata of the transformation that has
  1111. /// been applied.
  1112. /// @param AddAttrs Add these loop attributes to the new LoopID.
  1113. ///
  1114. /// @return A new LoopID that can be applied using Loop::setLoopID().
  1115. llvm::MDNode *
  1116. makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID,
  1117. llvm::ArrayRef<llvm::StringRef> RemovePrefixes,
  1118. llvm::ArrayRef<llvm::MDNode *> AddAttrs);
  1119. } // End llvm namespace
  1120. #endif
  1121. #ifdef __GNUC__
  1122. #pragma GCC diagnostic pop
  1123. #endif