Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

710 lines
26 KiB

  1. //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the LoopInfo class that is used to identify natural loops
  11. // and determine the loop depth of various nodes of the CFG. A natural loop
  12. // has exactly one entry-point, which is called the header. Note that natural
  13. // loops may actually be several loops that share the same header node.
  14. //
  15. // This analysis calculates the nesting structure of loops in a function. For
  16. // each natural loop identified, this analysis identifies natural loops
  17. // contained entirely within the loop and the basic blocks the make up the loop.
  18. //
  19. // It can calculate on the fly various bits of information, for example:
  20. //
  21. // * whether there is a preheader for the loop
  22. // * the number of back edges to the header
  23. // * whether or not a particular block branches out of the loop
  24. // * the successor blocks of the loop
  25. // * the loop depth
  26. // * etc...
  27. //
  28. //===----------------------------------------------------------------------===//
  29. #ifndef LLVM_ANALYSIS_LOOPINFO_H
  30. #define LLVM_ANALYSIS_LOOPINFO_H
  31. #include "llvm/ADT/DenseMap.h"
  32. #include "llvm/ADT/DenseSet.h"
  33. #include "llvm/ADT/GraphTraits.h"
  34. #include "llvm/ADT/SmallVector.h"
  35. #include "llvm/Analysis/Dominators.h"
  36. #include "llvm/Pass.h"
  37. #include <algorithm>
  38. namespace llvm {
  39. template<typename T>
  40. inline void RemoveFromVector(std::vector<T*> &V, T *N) {
  41. typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
  42. assert(I != V.end() && "N is not in this list!");
  43. V.erase(I);
  44. }
  45. class DominatorTree;
  46. class LoopInfo;
  47. class Loop;
  48. class PHINode;
  49. class raw_ostream;
  50. template<class N, class M> class LoopInfoBase;
  51. template<class N, class M> class LoopBase;
  52. //===----------------------------------------------------------------------===//
  53. /// LoopBase class - Instances of this class are used to represent loops that
  54. /// are detected in the flow graph
  55. ///
  56. template<class BlockT, class LoopT>
  57. class LoopBase {
  58. LoopT *ParentLoop;
  59. // SubLoops - Loops contained entirely within this one.
  60. std::vector<LoopT *> SubLoops;
  61. // Blocks - The list of blocks in this loop. First entry is the header node.
  62. std::vector<BlockT*> Blocks;
  63. LoopBase(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
  64. const LoopBase<BlockT, LoopT>&
  65. operator=(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
  66. public:
  67. /// Loop ctor - This creates an empty loop.
  68. LoopBase() : ParentLoop(0) {}
  69. ~LoopBase() {
  70. for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
  71. delete SubLoops[i];
  72. }
  73. /// getLoopDepth - Return the nesting level of this loop. An outer-most
  74. /// loop has depth 1, for consistency with loop depth values used for basic
  75. /// blocks, where depth 0 is used for blocks not inside any loops.
  76. unsigned getLoopDepth() const {
  77. unsigned D = 1;
  78. for (const LoopT *CurLoop = ParentLoop; CurLoop;
  79. CurLoop = CurLoop->ParentLoop)
  80. ++D;
  81. return D;
  82. }
  83. BlockT *getHeader() const { return Blocks.front(); }
  84. LoopT *getParentLoop() const { return ParentLoop; }
  85. /// setParentLoop is a raw interface for bypassing addChildLoop.
  86. void setParentLoop(LoopT *L) { ParentLoop = L; }
  87. /// contains - Return true if the specified loop is contained within in
  88. /// this loop.
  89. ///
  90. bool contains(const LoopT *L) const {
  91. if (L == this) return true;
  92. if (L == 0) return false;
  93. return contains(L->getParentLoop());
  94. }
  95. /// contains - Return true if the specified basic block is in this loop.
  96. ///
  97. bool contains(const BlockT *BB) const {
  98. return std::find(block_begin(), block_end(), BB) != block_end();
  99. }
  100. /// contains - Return true if the specified instruction is in this loop.
  101. ///
  102. template<class InstT>
  103. bool contains(const InstT *Inst) const {
  104. return contains(Inst->getParent());
  105. }
  106. /// iterator/begin/end - Return the loops contained entirely within this loop.
  107. ///
  108. const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
  109. std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
  110. typedef typename std::vector<LoopT *>::const_iterator iterator;
  111. typedef typename std::vector<LoopT *>::const_reverse_iterator
  112. reverse_iterator;
  113. iterator begin() const { return SubLoops.begin(); }
  114. iterator end() const { return SubLoops.end(); }
  115. reverse_iterator rbegin() const { return SubLoops.rbegin(); }
  116. reverse_iterator rend() const { return SubLoops.rend(); }
  117. bool empty() const { return SubLoops.empty(); }
  118. /// getBlocks - Get a list of the basic blocks which make up this loop.
  119. ///
  120. const std::vector<BlockT*> &getBlocks() const { return Blocks; }
  121. std::vector<BlockT*> &getBlocksVector() { return Blocks; }
  122. typedef typename std::vector<BlockT*>::const_iterator block_iterator;
  123. block_iterator block_begin() const { return Blocks.begin(); }
  124. block_iterator block_end() const { return Blocks.end(); }
  125. /// getNumBlocks - Get the number of blocks in this loop in constant time.
  126. unsigned getNumBlocks() const {
  127. return Blocks.size();
  128. }
  129. /// isLoopExiting - True if terminator in the block can branch to another
  130. /// block that is outside of the current loop.
  131. ///
  132. bool isLoopExiting(const BlockT *BB) const {
  133. typedef GraphTraits<const BlockT*> BlockTraits;
  134. for (typename BlockTraits::ChildIteratorType SI =
  135. BlockTraits::child_begin(BB),
  136. SE = BlockTraits::child_end(BB); SI != SE; ++SI) {
  137. if (!contains(*SI))
  138. return true;
  139. }
  140. return false;
  141. }
  142. /// getNumBackEdges - Calculate the number of back edges to the loop header
  143. ///
  144. unsigned getNumBackEdges() const {
  145. unsigned NumBackEdges = 0;
  146. BlockT *H = getHeader();
  147. typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
  148. for (typename InvBlockTraits::ChildIteratorType I =
  149. InvBlockTraits::child_begin(H),
  150. E = InvBlockTraits::child_end(H); I != E; ++I)
  151. if (contains(*I))
  152. ++NumBackEdges;
  153. return NumBackEdges;
  154. }
  155. //===--------------------------------------------------------------------===//
  156. // APIs for simple analysis of the loop.
  157. //
  158. // Note that all of these methods can fail on general loops (ie, there may not
  159. // be a preheader, etc). For best success, the loop simplification and
  160. // induction variable canonicalization pass should be used to normalize loops
  161. // for easy analysis. These methods assume canonical loops.
  162. /// getExitingBlocks - Return all blocks inside the loop that have successors
  163. /// outside of the loop. These are the blocks _inside of the current loop_
  164. /// which branch out. The returned list is always unique.
  165. ///
  166. void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
  167. /// getExitingBlock - If getExitingBlocks would return exactly one block,
  168. /// return that block. Otherwise return null.
  169. BlockT *getExitingBlock() const;
  170. /// getExitBlocks - Return all of the successor blocks of this loop. These
  171. /// are the blocks _outside of the current loop_ which are branched to.
  172. ///
  173. void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
  174. /// getExitBlock - If getExitBlocks would return exactly one block,
  175. /// return that block. Otherwise return null.
  176. BlockT *getExitBlock() const;
  177. /// Edge type.
  178. typedef std::pair<const BlockT*, const BlockT*> Edge;
  179. /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
  180. void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
  181. /// getLoopPreheader - If there is a preheader for this loop, return it. A
  182. /// loop has a preheader if there is only one edge to the header of the loop
  183. /// from outside of the loop. If this is the case, the block branching to the
  184. /// header of the loop is the preheader node.
  185. ///
  186. /// This method returns null if there is no preheader for the loop.
  187. ///
  188. BlockT *getLoopPreheader() const;
  189. /// getLoopPredecessor - If the given loop's header has exactly one unique
  190. /// predecessor outside the loop, return it. Otherwise return null.
  191. /// This is less strict that the loop "preheader" concept, which requires
  192. /// the predecessor to have exactly one successor.
  193. ///
  194. BlockT *getLoopPredecessor() const;
  195. /// getLoopLatch - If there is a single latch block for this loop, return it.
  196. /// A latch block is a block that contains a branch back to the header.
  197. BlockT *getLoopLatch() const;
  198. //===--------------------------------------------------------------------===//
  199. // APIs for updating loop information after changing the CFG
  200. //
  201. /// addBasicBlockToLoop - This method is used by other analyses to update loop
  202. /// information. NewBB is set to be a new member of the current loop.
  203. /// Because of this, it is added as a member of all parent loops, and is added
  204. /// to the specified LoopInfo object as being in the current basic block. It
  205. /// is not valid to replace the loop header with this method.
  206. ///
  207. void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
  208. /// replaceChildLoopWith - This is used when splitting loops up. It replaces
  209. /// the OldChild entry in our children list with NewChild, and updates the
  210. /// parent pointer of OldChild to be null and the NewChild to be this loop.
  211. /// This updates the loop depth of the new child.
  212. void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
  213. /// addChildLoop - Add the specified loop to be a child of this loop. This
  214. /// updates the loop depth of the new child.
  215. ///
  216. void addChildLoop(LoopT *NewChild) {
  217. assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
  218. NewChild->ParentLoop = static_cast<LoopT *>(this);
  219. SubLoops.push_back(NewChild);
  220. }
  221. /// removeChildLoop - This removes the specified child from being a subloop of
  222. /// this loop. The loop is not deleted, as it will presumably be inserted
  223. /// into another loop.
  224. LoopT *removeChildLoop(iterator I) {
  225. assert(I != SubLoops.end() && "Cannot remove end iterator!");
  226. LoopT *Child = *I;
  227. assert(Child->ParentLoop == this && "Child is not a child of this loop!");
  228. SubLoops.erase(SubLoops.begin()+(I-begin()));
  229. Child->ParentLoop = 0;
  230. return Child;
  231. }
  232. /// addBlockEntry - This adds a basic block directly to the basic block list.
  233. /// This should only be used by transformations that create new loops. Other
  234. /// transformations should use addBasicBlockToLoop.
  235. void addBlockEntry(BlockT *BB) {
  236. Blocks.push_back(BB);
  237. }
  238. /// moveToHeader - This method is used to move BB (which must be part of this
  239. /// loop) to be the loop header of the loop (the block that dominates all
  240. /// others).
  241. void moveToHeader(BlockT *BB) {
  242. if (Blocks[0] == BB) return;
  243. for (unsigned i = 0; ; ++i) {
  244. assert(i != Blocks.size() && "Loop does not contain BB!");
  245. if (Blocks[i] == BB) {
  246. Blocks[i] = Blocks[0];
  247. Blocks[0] = BB;
  248. return;
  249. }
  250. }
  251. }
  252. /// removeBlockFromLoop - This removes the specified basic block from the
  253. /// current loop, updating the Blocks as appropriate. This does not update
  254. /// the mapping in the LoopInfo class.
  255. void removeBlockFromLoop(BlockT *BB) {
  256. RemoveFromVector(Blocks, BB);
  257. }
  258. /// verifyLoop - Verify loop structure
  259. void verifyLoop() const;
  260. /// verifyLoop - Verify loop structure of this loop and all nested loops.
  261. void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
  262. void print(raw_ostream &OS, unsigned Depth = 0) const;
  263. protected:
  264. friend class LoopInfoBase<BlockT, LoopT>;
  265. explicit LoopBase(BlockT *BB) : ParentLoop(0) {
  266. Blocks.push_back(BB);
  267. }
  268. };
  269. template<class BlockT, class LoopT>
  270. raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
  271. Loop.print(OS);
  272. return OS;
  273. }
  274. // Implementation in LoopInfoImpl.h
  275. #ifdef __GNUC__
  276. __extension__ extern template class LoopBase<BasicBlock, Loop>;
  277. #endif
  278. class Loop : public LoopBase<BasicBlock, Loop> {
  279. public:
  280. Loop() {}
  281. /// isLoopInvariant - Return true if the specified value is loop invariant
  282. ///
  283. bool isLoopInvariant(Value *V) const;
  284. /// hasLoopInvariantOperands - Return true if all the operands of the
  285. /// specified instruction are loop invariant.
  286. bool hasLoopInvariantOperands(Instruction *I) const;
  287. /// makeLoopInvariant - If the given value is an instruction inside of the
  288. /// loop and it can be hoisted, do so to make it trivially loop-invariant.
  289. /// Return true if the value after any hoisting is loop invariant. This
  290. /// function can be used as a slightly more aggressive replacement for
  291. /// isLoopInvariant.
  292. ///
  293. /// If InsertPt is specified, it is the point to hoist instructions to.
  294. /// If null, the terminator of the loop preheader is used.
  295. ///
  296. bool makeLoopInvariant(Value *V, bool &Changed,
  297. Instruction *InsertPt = 0) const;
  298. /// makeLoopInvariant - If the given instruction is inside of the
  299. /// loop and it can be hoisted, do so to make it trivially loop-invariant.
  300. /// Return true if the instruction after any hoisting is loop invariant. This
  301. /// function can be used as a slightly more aggressive replacement for
  302. /// isLoopInvariant.
  303. ///
  304. /// If InsertPt is specified, it is the point to hoist instructions to.
  305. /// If null, the terminator of the loop preheader is used.
  306. ///
  307. bool makeLoopInvariant(Instruction *I, bool &Changed,
  308. Instruction *InsertPt = 0) const;
  309. /// getCanonicalInductionVariable - Check to see if the loop has a canonical
  310. /// induction variable: an integer recurrence that starts at 0 and increments
  311. /// by one each time through the loop. If so, return the phi node that
  312. /// corresponds to it.
  313. ///
  314. /// The IndVarSimplify pass transforms loops to have a canonical induction
  315. /// variable.
  316. ///
  317. PHINode *getCanonicalInductionVariable() const;
  318. /// isLCSSAForm - Return true if the Loop is in LCSSA form
  319. bool isLCSSAForm(DominatorTree &DT) const;
  320. /// isLoopSimplifyForm - Return true if the Loop is in the form that
  321. /// the LoopSimplify form transforms loops to, which is sometimes called
  322. /// normal form.
  323. bool isLoopSimplifyForm() const;
  324. /// isSafeToClone - Return true if the loop body is safe to clone in practice.
  325. bool isSafeToClone() const;
  326. /// Returns true if the loop is annotated parallel.
  327. ///
  328. /// A parallel loop can be assumed to not contain any dependencies between
  329. /// iterations by the compiler. That is, any loop-carried dependency checking
  330. /// can be skipped completely when parallelizing the loop on the target
  331. /// machine. Thus, if the parallel loop information originates from the
  332. /// programmer, e.g. via the OpenMP parallel for pragma, it is the
  333. /// programmer's responsibility to ensure there are no loop-carried
  334. /// dependencies. The final execution order of the instructions across
  335. /// iterations is not guaranteed, thus, the end result might or might not
  336. /// implement actual concurrent execution of instructions across multiple
  337. /// iterations.
  338. bool isAnnotatedParallel() const;
  339. /// hasDedicatedExits - Return true if no exit block for the loop
  340. /// has a predecessor that is outside the loop.
  341. bool hasDedicatedExits() const;
  342. /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
  343. /// These are the blocks _outside of the current loop_ which are branched to.
  344. /// This assumes that loop exits are in canonical form.
  345. ///
  346. void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
  347. /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
  348. /// block, return that block. Otherwise return null.
  349. BasicBlock *getUniqueExitBlock() const;
  350. void dump() const;
  351. private:
  352. friend class LoopInfoBase<BasicBlock, Loop>;
  353. explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
  354. };
  355. //===----------------------------------------------------------------------===//
  356. /// LoopInfo - This class builds and contains all of the top level loop
  357. /// structures in the specified function.
  358. ///
  359. template<class BlockT, class LoopT>
  360. class LoopInfoBase {
  361. // BBMap - Mapping of basic blocks to the inner most loop they occur in
  362. DenseMap<BlockT *, LoopT *> BBMap;
  363. std::vector<LoopT *> TopLevelLoops;
  364. friend class LoopBase<BlockT, LoopT>;
  365. friend class LoopInfo;
  366. void operator=(const LoopInfoBase &) LLVM_DELETED_FUNCTION;
  367. LoopInfoBase(const LoopInfo &) LLVM_DELETED_FUNCTION;
  368. public:
  369. LoopInfoBase() { }
  370. ~LoopInfoBase() { releaseMemory(); }
  371. void releaseMemory() {
  372. for (typename std::vector<LoopT *>::iterator I =
  373. TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
  374. delete *I; // Delete all of the loops...
  375. BBMap.clear(); // Reset internal state of analysis
  376. TopLevelLoops.clear();
  377. }
  378. /// iterator/begin/end - The interface to the top-level loops in the current
  379. /// function.
  380. ///
  381. typedef typename std::vector<LoopT *>::const_iterator iterator;
  382. typedef typename std::vector<LoopT *>::const_reverse_iterator
  383. reverse_iterator;
  384. iterator begin() const { return TopLevelLoops.begin(); }
  385. iterator end() const { return TopLevelLoops.end(); }
  386. reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
  387. reverse_iterator rend() const { return TopLevelLoops.rend(); }
  388. bool empty() const { return TopLevelLoops.empty(); }
  389. /// getLoopFor - Return the inner most loop that BB lives in. If a basic
  390. /// block is in no loop (for example the entry node), null is returned.
  391. ///
  392. LoopT *getLoopFor(const BlockT *BB) const {
  393. return BBMap.lookup(const_cast<BlockT*>(BB));
  394. }
  395. /// operator[] - same as getLoopFor...
  396. ///
  397. const LoopT *operator[](const BlockT *BB) const {
  398. return getLoopFor(BB);
  399. }
  400. /// getLoopDepth - Return the loop nesting level of the specified block. A
  401. /// depth of 0 means the block is not inside any loop.
  402. ///
  403. unsigned getLoopDepth(const BlockT *BB) const {
  404. const LoopT *L = getLoopFor(BB);
  405. return L ? L->getLoopDepth() : 0;
  406. }
  407. // isLoopHeader - True if the block is a loop header node
  408. bool isLoopHeader(BlockT *BB) const {
  409. const LoopT *L = getLoopFor(BB);
  410. return L && L->getHeader() == BB;
  411. }
  412. /// removeLoop - This removes the specified top-level loop from this loop info
  413. /// object. The loop is not deleted, as it will presumably be inserted into
  414. /// another loop.
  415. LoopT *removeLoop(iterator I) {
  416. assert(I != end() && "Cannot remove end iterator!");
  417. LoopT *L = *I;
  418. assert(L->getParentLoop() == 0 && "Not a top-level loop!");
  419. TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
  420. return L;
  421. }
  422. /// changeLoopFor - Change the top-level loop that contains BB to the
  423. /// specified loop. This should be used by transformations that restructure
  424. /// the loop hierarchy tree.
  425. void changeLoopFor(BlockT *BB, LoopT *L) {
  426. if (!L) {
  427. BBMap.erase(BB);
  428. return;
  429. }
  430. BBMap[BB] = L;
  431. }
  432. /// changeTopLevelLoop - Replace the specified loop in the top-level loops
  433. /// list with the indicated loop.
  434. void changeTopLevelLoop(LoopT *OldLoop,
  435. LoopT *NewLoop) {
  436. typename std::vector<LoopT *>::iterator I =
  437. std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
  438. assert(I != TopLevelLoops.end() && "Old loop not at top level!");
  439. *I = NewLoop;
  440. assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
  441. "Loops already embedded into a subloop!");
  442. }
  443. /// addTopLevelLoop - This adds the specified loop to the collection of
  444. /// top-level loops.
  445. void addTopLevelLoop(LoopT *New) {
  446. assert(New->getParentLoop() == 0 && "Loop already in subloop!");
  447. TopLevelLoops.push_back(New);
  448. }
  449. /// removeBlock - This method completely removes BB from all data structures,
  450. /// including all of the Loop objects it is nested in and our mapping from
  451. /// BasicBlocks to loops.
  452. void removeBlock(BlockT *BB) {
  453. typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
  454. if (I != BBMap.end()) {
  455. for (LoopT *L = I->second; L; L = L->getParentLoop())
  456. L->removeBlockFromLoop(BB);
  457. BBMap.erase(I);
  458. }
  459. }
  460. // Internals
  461. static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
  462. const LoopT *ParentLoop) {
  463. if (SubLoop == 0) return true;
  464. if (SubLoop == ParentLoop) return false;
  465. return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
  466. }
  467. /// Create the loop forest using a stable algorithm.
  468. void Analyze(DominatorTreeBase<BlockT> &DomTree);
  469. // Debugging
  470. void print(raw_ostream &OS) const;
  471. };
  472. // Implementation in LoopInfoImpl.h
  473. #ifdef __GNUC__
  474. __extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
  475. #endif
  476. class LoopInfo : public FunctionPass {
  477. LoopInfoBase<BasicBlock, Loop> LI;
  478. friend class LoopBase<BasicBlock, Loop>;
  479. void operator=(const LoopInfo &) LLVM_DELETED_FUNCTION;
  480. LoopInfo(const LoopInfo &) LLVM_DELETED_FUNCTION;
  481. public:
  482. static char ID; // Pass identification, replacement for typeid
  483. LoopInfo() : FunctionPass(ID) {
  484. initializeLoopInfoPass(*PassRegistry::getPassRegistry());
  485. }
  486. LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
  487. /// iterator/begin/end - The interface to the top-level loops in the current
  488. /// function.
  489. ///
  490. typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
  491. typedef LoopInfoBase<BasicBlock, Loop>::reverse_iterator reverse_iterator;
  492. inline iterator begin() const { return LI.begin(); }
  493. inline iterator end() const { return LI.end(); }
  494. inline reverse_iterator rbegin() const { return LI.rbegin(); }
  495. inline reverse_iterator rend() const { return LI.rend(); }
  496. bool empty() const { return LI.empty(); }
  497. /// getLoopFor - Return the inner most loop that BB lives in. If a basic
  498. /// block is in no loop (for example the entry node), null is returned.
  499. ///
  500. inline Loop *getLoopFor(const BasicBlock *BB) const {
  501. return LI.getLoopFor(BB);
  502. }
  503. /// operator[] - same as getLoopFor...
  504. ///
  505. inline const Loop *operator[](const BasicBlock *BB) const {
  506. return LI.getLoopFor(BB);
  507. }
  508. /// getLoopDepth - Return the loop nesting level of the specified block. A
  509. /// depth of 0 means the block is not inside any loop.
  510. ///
  511. inline unsigned getLoopDepth(const BasicBlock *BB) const {
  512. return LI.getLoopDepth(BB);
  513. }
  514. // isLoopHeader - True if the block is a loop header node
  515. inline bool isLoopHeader(BasicBlock *BB) const {
  516. return LI.isLoopHeader(BB);
  517. }
  518. /// runOnFunction - Calculate the natural loop information.
  519. ///
  520. virtual bool runOnFunction(Function &F);
  521. virtual void verifyAnalysis() const;
  522. virtual void releaseMemory() { LI.releaseMemory(); }
  523. virtual void print(raw_ostream &O, const Module* M = 0) const;
  524. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  525. /// removeLoop - This removes the specified top-level loop from this loop info
  526. /// object. The loop is not deleted, as it will presumably be inserted into
  527. /// another loop.
  528. inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
  529. /// changeLoopFor - Change the top-level loop that contains BB to the
  530. /// specified loop. This should be used by transformations that restructure
  531. /// the loop hierarchy tree.
  532. inline void changeLoopFor(BasicBlock *BB, Loop *L) {
  533. LI.changeLoopFor(BB, L);
  534. }
  535. /// changeTopLevelLoop - Replace the specified loop in the top-level loops
  536. /// list with the indicated loop.
  537. inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
  538. LI.changeTopLevelLoop(OldLoop, NewLoop);
  539. }
  540. /// addTopLevelLoop - This adds the specified loop to the collection of
  541. /// top-level loops.
  542. inline void addTopLevelLoop(Loop *New) {
  543. LI.addTopLevelLoop(New);
  544. }
  545. /// removeBlock - This method completely removes BB from all data structures,
  546. /// including all of the Loop objects it is nested in and our mapping from
  547. /// BasicBlocks to loops.
  548. void removeBlock(BasicBlock *BB) {
  549. LI.removeBlock(BB);
  550. }
  551. /// updateUnloop - Update LoopInfo after removing the last backedge from a
  552. /// loop--now the "unloop". This updates the loop forest and parent loops for
  553. /// each block so that Unloop is no longer referenced, but the caller must
  554. /// actually delete the Unloop object.
  555. void updateUnloop(Loop *Unloop);
  556. /// replacementPreservesLCSSAForm - Returns true if replacing From with To
  557. /// everywhere is guaranteed to preserve LCSSA form.
  558. bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
  559. // Preserving LCSSA form is only problematic if the replacing value is an
  560. // instruction.
  561. Instruction *I = dyn_cast<Instruction>(To);
  562. if (!I) return true;
  563. // If both instructions are defined in the same basic block then replacement
  564. // cannot break LCSSA form.
  565. if (I->getParent() == From->getParent())
  566. return true;
  567. // If the instruction is not defined in a loop then it can safely replace
  568. // anything.
  569. Loop *ToLoop = getLoopFor(I->getParent());
  570. if (!ToLoop) return true;
  571. // If the replacing instruction is defined in the same loop as the original
  572. // instruction, or in a loop that contains it as an inner loop, then using
  573. // it as a replacement will not break LCSSA form.
  574. return ToLoop->contains(getLoopFor(From->getParent()));
  575. }
  576. };
  577. // Allow clients to walk the list of nested loops...
  578. template <> struct GraphTraits<const Loop*> {
  579. typedef const Loop NodeType;
  580. typedef LoopInfo::iterator ChildIteratorType;
  581. static NodeType *getEntryNode(const Loop *L) { return L; }
  582. static inline ChildIteratorType child_begin(NodeType *N) {
  583. return N->begin();
  584. }
  585. static inline ChildIteratorType child_end(NodeType *N) {
  586. return N->end();
  587. }
  588. };
  589. template <> struct GraphTraits<Loop*> {
  590. typedef Loop NodeType;
  591. typedef LoopInfo::iterator ChildIteratorType;
  592. static NodeType *getEntryNode(Loop *L) { return L; }
  593. static inline ChildIteratorType child_begin(NodeType *N) {
  594. return N->begin();
  595. }
  596. static inline ChildIteratorType child_end(NodeType *N) {
  597. return N->end();
  598. }
  599. };
  600. } // End llvm namespace
  601. #endif