1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
19 // It can calculate on the fly various bits of information, for example:
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
28 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_ANALYSIS_LOOPINFO_H
31 #define LLVM_ANALYSIS_LOOPINFO_H
33 #include "llvm/ADT/DenseMap.h"
34 #include "llvm/ADT/DenseSet.h"
35 #include "llvm/ADT/GraphTraits.h"
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/Analysis/Dominators.h"
38 #include "llvm/Pass.h"
44 inline void RemoveFromVector(std::vector<T*> &V, T *N) {
45 typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
46 assert(I != V.end() && "N is not in this list!");
56 template<class N, class M> class LoopInfoBase;
57 template<class N, class M> class LoopBase;
59 //===----------------------------------------------------------------------===//
60 /// LoopBase class - Instances of this class are used to represent loops that
61 /// are detected in the flow graph
63 template<class BlockT, class LoopT>
66 // SubLoops - Loops contained entirely within this one.
67 std::vector<LoopT *> SubLoops;
69 // Blocks - The list of blocks in this loop. First entry is the header node.
70 std::vector<BlockT*> Blocks;
72 SmallPtrSet<const BlockT*, 8> DenseBlockSet;
74 LoopBase(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
75 const LoopBase<BlockT, LoopT>&
76 operator=(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
78 /// Loop ctor - This creates an empty loop.
79 LoopBase() : ParentLoop(0) {}
81 for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
85 /// getLoopDepth - Return the nesting level of this loop. An outer-most
86 /// loop has depth 1, for consistency with loop depth values used for basic
87 /// blocks, where depth 0 is used for blocks not inside any loops.
88 unsigned getLoopDepth() const {
90 for (const LoopT *CurLoop = ParentLoop; CurLoop;
91 CurLoop = CurLoop->ParentLoop)
95 BlockT *getHeader() const { return Blocks.front(); }
96 LoopT *getParentLoop() const { return ParentLoop; }
98 /// setParentLoop is a raw interface for bypassing addChildLoop.
99 void setParentLoop(LoopT *L) { ParentLoop = L; }
101 /// contains - Return true if the specified loop is contained within in
104 bool contains(const LoopT *L) const {
105 if (L == this) return true;
106 if (L == 0) return false;
107 return contains(L->getParentLoop());
110 /// contains - Return true if the specified basic block is in this loop.
112 bool contains(const BlockT *BB) const {
113 return DenseBlockSet.count(BB);
116 /// contains - Return true if the specified instruction is in this loop.
118 template<class InstT>
119 bool contains(const InstT *Inst) const {
120 return contains(Inst->getParent());
123 /// iterator/begin/end - Return the loops contained entirely within this loop.
125 const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
126 std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
127 typedef typename std::vector<LoopT *>::const_iterator iterator;
128 typedef typename std::vector<LoopT *>::const_reverse_iterator
130 iterator begin() const { return SubLoops.begin(); }
131 iterator end() const { return SubLoops.end(); }
132 reverse_iterator rbegin() const { return SubLoops.rbegin(); }
133 reverse_iterator rend() const { return SubLoops.rend(); }
134 bool empty() const { return SubLoops.empty(); }
136 /// getBlocks - Get a list of the basic blocks which make up this loop.
138 const std::vector<BlockT*> &getBlocks() const { return Blocks; }
139 typedef typename std::vector<BlockT*>::const_iterator block_iterator;
140 block_iterator block_begin() const { return Blocks.begin(); }
141 block_iterator block_end() const { return Blocks.end(); }
143 /// getNumBlocks - Get the number of blocks in this loop in constant time.
144 unsigned getNumBlocks() const {
145 return Blocks.size();
148 /// isLoopExiting - True if terminator in the block can branch to another
149 /// block that is outside of the current loop.
151 bool isLoopExiting(const BlockT *BB) const {
152 typedef GraphTraits<const BlockT*> BlockTraits;
153 for (typename BlockTraits::ChildIteratorType SI =
154 BlockTraits::child_begin(BB),
155 SE = BlockTraits::child_end(BB); SI != SE; ++SI) {
162 /// getNumBackEdges - Calculate the number of back edges to the loop header
164 unsigned getNumBackEdges() const {
165 unsigned NumBackEdges = 0;
166 BlockT *H = getHeader();
168 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
169 for (typename InvBlockTraits::ChildIteratorType I =
170 InvBlockTraits::child_begin(H),
171 E = InvBlockTraits::child_end(H); I != E; ++I)
178 //===--------------------------------------------------------------------===//
179 // APIs for simple analysis of the loop.
181 // Note that all of these methods can fail on general loops (ie, there may not
182 // be a preheader, etc). For best success, the loop simplification and
183 // induction variable canonicalization pass should be used to normalize loops
184 // for easy analysis. These methods assume canonical loops.
186 /// getExitingBlocks - Return all blocks inside the loop that have successors
187 /// outside of the loop. These are the blocks _inside of the current loop_
188 /// which branch out. The returned list is always unique.
190 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
192 /// getExitingBlock - If getExitingBlocks would return exactly one block,
193 /// return that block. Otherwise return null.
194 BlockT *getExitingBlock() const;
196 /// getExitBlocks - Return all of the successor blocks of this loop. These
197 /// are the blocks _outside of the current loop_ which are branched to.
199 void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
201 /// getExitBlock - If getExitBlocks would return exactly one block,
202 /// return that block. Otherwise return null.
203 BlockT *getExitBlock() const;
206 typedef std::pair<const BlockT*, const BlockT*> Edge;
208 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
209 void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
211 /// getLoopPreheader - If there is a preheader for this loop, return it. A
212 /// loop has a preheader if there is only one edge to the header of the loop
213 /// from outside of the loop. If this is the case, the block branching to the
214 /// header of the loop is the preheader node.
216 /// This method returns null if there is no preheader for the loop.
218 BlockT *getLoopPreheader() const;
220 /// getLoopPredecessor - If the given loop's header has exactly one unique
221 /// predecessor outside the loop, return it. Otherwise return null.
222 /// This is less strict that the loop "preheader" concept, which requires
223 /// the predecessor to have exactly one successor.
225 BlockT *getLoopPredecessor() const;
227 /// getLoopLatch - If there is a single latch block for this loop, return it.
228 /// A latch block is a block that contains a branch back to the header.
229 BlockT *getLoopLatch() const;
231 //===--------------------------------------------------------------------===//
232 // APIs for updating loop information after changing the CFG
235 /// addBasicBlockToLoop - This method is used by other analyses to update loop
236 /// information. NewBB is set to be a new member of the current loop.
237 /// Because of this, it is added as a member of all parent loops, and is added
238 /// to the specified LoopInfo object as being in the current basic block. It
239 /// is not valid to replace the loop header with this method.
241 void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
243 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
244 /// the OldChild entry in our children list with NewChild, and updates the
245 /// parent pointer of OldChild to be null and the NewChild to be this loop.
246 /// This updates the loop depth of the new child.
247 void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
249 /// addChildLoop - Add the specified loop to be a child of this loop. This
250 /// updates the loop depth of the new child.
252 void addChildLoop(LoopT *NewChild) {
253 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
254 NewChild->ParentLoop = static_cast<LoopT *>(this);
255 SubLoops.push_back(NewChild);
258 /// removeChildLoop - This removes the specified child from being a subloop of
259 /// this loop. The loop is not deleted, as it will presumably be inserted
260 /// into another loop.
261 LoopT *removeChildLoop(iterator I) {
262 assert(I != SubLoops.end() && "Cannot remove end iterator!");
264 assert(Child->ParentLoop == this && "Child is not a child of this loop!");
265 SubLoops.erase(SubLoops.begin()+(I-begin()));
266 Child->ParentLoop = 0;
270 /// addBlockEntry - This adds a basic block directly to the basic block list.
271 /// This should only be used by transformations that create new loops. Other
272 /// transformations should use addBasicBlockToLoop.
273 void addBlockEntry(BlockT *BB) {
274 Blocks.push_back(BB);
275 DenseBlockSet.insert(BB);
278 /// reverseBlocks - interface to reverse Blocks[from, end of loop] in this loop
279 void reverseBlock(unsigned from) {
280 std::reverse(Blocks.begin() + from, Blocks.end());
283 /// reserveBlocks- interface to do reserve() for Blocks
284 void reserveBlocks(unsigned size) {
285 Blocks.reserve(size);
288 /// moveToHeader - This method is used to move BB (which must be part of this
289 /// loop) to be the loop header of the loop (the block that dominates all
291 void moveToHeader(BlockT *BB) {
292 if (Blocks[0] == BB) return;
293 for (unsigned i = 0; ; ++i) {
294 assert(i != Blocks.size() && "Loop does not contain BB!");
295 if (Blocks[i] == BB) {
296 Blocks[i] = Blocks[0];
303 /// removeBlockFromLoop - This removes the specified basic block from the
304 /// current loop, updating the Blocks as appropriate. This does not update
305 /// the mapping in the LoopInfo class.
306 void removeBlockFromLoop(BlockT *BB) {
307 RemoveFromVector(Blocks, BB);
308 DenseBlockSet.erase(BB);
311 /// verifyLoop - Verify loop structure
312 void verifyLoop() const;
314 /// verifyLoop - Verify loop structure of this loop and all nested loops.
315 void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
317 void print(raw_ostream &OS, unsigned Depth = 0) const;
320 friend class LoopInfoBase<BlockT, LoopT>;
321 explicit LoopBase(BlockT *BB) : ParentLoop(0) {
322 Blocks.push_back(BB);
323 DenseBlockSet.insert(BB);
327 template<class BlockT, class LoopT>
328 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
333 // Implementation in LoopInfoImpl.h
335 __extension__ extern template class LoopBase<BasicBlock, Loop>;
338 class Loop : public LoopBase<BasicBlock, Loop> {
342 /// isLoopInvariant - Return true if the specified value is loop invariant
344 bool isLoopInvariant(Value *V) const;
346 /// hasLoopInvariantOperands - Return true if all the operands of the
347 /// specified instruction are loop invariant.
348 bool hasLoopInvariantOperands(Instruction *I) const;
350 /// makeLoopInvariant - If the given value is an instruction inside of the
351 /// loop and it can be hoisted, do so to make it trivially loop-invariant.
352 /// Return true if the value after any hoisting is loop invariant. This
353 /// function can be used as a slightly more aggressive replacement for
356 /// If InsertPt is specified, it is the point to hoist instructions to.
357 /// If null, the terminator of the loop preheader is used.
359 bool makeLoopInvariant(Value *V, bool &Changed,
360 Instruction *InsertPt = 0) const;
362 /// makeLoopInvariant - If the given instruction is inside of the
363 /// loop and it can be hoisted, do so to make it trivially loop-invariant.
364 /// Return true if the instruction after any hoisting is loop invariant. This
365 /// function can be used as a slightly more aggressive replacement for
368 /// If InsertPt is specified, it is the point to hoist instructions to.
369 /// If null, the terminator of the loop preheader is used.
371 bool makeLoopInvariant(Instruction *I, bool &Changed,
372 Instruction *InsertPt = 0) const;
374 /// getCanonicalInductionVariable - Check to see if the loop has a canonical
375 /// induction variable: an integer recurrence that starts at 0 and increments
376 /// by one each time through the loop. If so, return the phi node that
377 /// corresponds to it.
379 /// The IndVarSimplify pass transforms loops to have a canonical induction
382 PHINode *getCanonicalInductionVariable() const;
384 /// isLCSSAForm - Return true if the Loop is in LCSSA form
385 bool isLCSSAForm(DominatorTree &DT) const;
387 /// isLoopSimplifyForm - Return true if the Loop is in the form that
388 /// the LoopSimplify form transforms loops to, which is sometimes called
390 bool isLoopSimplifyForm() const;
392 /// isSafeToClone - Return true if the loop body is safe to clone in practice.
393 bool isSafeToClone() const;
395 /// Returns true if the loop is annotated parallel.
397 /// A parallel loop can be assumed to not contain any dependencies between
398 /// iterations by the compiler. That is, any loop-carried dependency checking
399 /// can be skipped completely when parallelizing the loop on the target
400 /// machine. Thus, if the parallel loop information originates from the
401 /// programmer, e.g. via the OpenMP parallel for pragma, it is the
402 /// programmer's responsibility to ensure there are no loop-carried
403 /// dependencies. The final execution order of the instructions across
404 /// iterations is not guaranteed, thus, the end result might or might not
405 /// implement actual concurrent execution of instructions across multiple
407 bool isAnnotatedParallel() const;
409 /// Return the llvm.loop loop id metadata node for this loop if it is present.
411 /// If this loop contains the same llvm.loop metadata on each branch to the
412 /// header then the node is returned. If any latch instruction does not
413 /// contain llvm.loop or or if multiple latches contain different nodes then
415 MDNode *getLoopID() const;
416 /// Set the llvm.loop loop id metadata for this loop.
418 /// The LoopID metadata node will be added to each terminator instruction in
419 /// the loop that branches to the loop header.
421 /// The LoopID metadata node should have one or more operands and the first
422 /// operand should should be the node itself.
423 void setLoopID(MDNode *LoopID) const;
425 /// hasDedicatedExits - Return true if no exit block for the loop
426 /// has a predecessor that is outside the loop.
427 bool hasDedicatedExits() const;
429 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
430 /// These are the blocks _outside of the current loop_ which are branched to.
431 /// This assumes that loop exits are in canonical form.
433 void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
435 /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
436 /// block, return that block. Otherwise return null.
437 BasicBlock *getUniqueExitBlock() const;
442 friend class LoopInfoBase<BasicBlock, Loop>;
443 explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
446 //===----------------------------------------------------------------------===//
447 /// LoopInfo - This class builds and contains all of the top level loop
448 /// structures in the specified function.
451 template<class BlockT, class LoopT>
453 // BBMap - Mapping of basic blocks to the inner most loop they occur in
454 DenseMap<BlockT *, LoopT *> BBMap;
455 std::vector<LoopT *> TopLevelLoops;
456 friend class LoopBase<BlockT, LoopT>;
457 friend class LoopInfo;
459 void operator=(const LoopInfoBase &) LLVM_DELETED_FUNCTION;
460 LoopInfoBase(const LoopInfo &) LLVM_DELETED_FUNCTION;
463 ~LoopInfoBase() { releaseMemory(); }
465 void releaseMemory() {
466 for (typename std::vector<LoopT *>::iterator I =
467 TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
468 delete *I; // Delete all of the loops...
470 BBMap.clear(); // Reset internal state of analysis
471 TopLevelLoops.clear();
474 /// iterator/begin/end - The interface to the top-level loops in the current
477 typedef typename std::vector<LoopT *>::const_iterator iterator;
478 typedef typename std::vector<LoopT *>::const_reverse_iterator
480 iterator begin() const { return TopLevelLoops.begin(); }
481 iterator end() const { return TopLevelLoops.end(); }
482 reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
483 reverse_iterator rend() const { return TopLevelLoops.rend(); }
484 bool empty() const { return TopLevelLoops.empty(); }
486 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
487 /// block is in no loop (for example the entry node), null is returned.
489 LoopT *getLoopFor(const BlockT *BB) const {
490 return BBMap.lookup(const_cast<BlockT*>(BB));
493 /// operator[] - same as getLoopFor...
495 const LoopT *operator[](const BlockT *BB) const {
496 return getLoopFor(BB);
499 /// getLoopDepth - Return the loop nesting level of the specified block. A
500 /// depth of 0 means the block is not inside any loop.
502 unsigned getLoopDepth(const BlockT *BB) const {
503 const LoopT *L = getLoopFor(BB);
504 return L ? L->getLoopDepth() : 0;
507 // isLoopHeader - True if the block is a loop header node
508 bool isLoopHeader(BlockT *BB) const {
509 const LoopT *L = getLoopFor(BB);
510 return L && L->getHeader() == BB;
513 /// removeLoop - This removes the specified top-level loop from this loop info
514 /// object. The loop is not deleted, as it will presumably be inserted into
516 LoopT *removeLoop(iterator I) {
517 assert(I != end() && "Cannot remove end iterator!");
519 assert(L->getParentLoop() == 0 && "Not a top-level loop!");
520 TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
524 /// changeLoopFor - Change the top-level loop that contains BB to the
525 /// specified loop. This should be used by transformations that restructure
526 /// the loop hierarchy tree.
527 void changeLoopFor(BlockT *BB, LoopT *L) {
535 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
536 /// list with the indicated loop.
537 void changeTopLevelLoop(LoopT *OldLoop,
539 typename std::vector<LoopT *>::iterator I =
540 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
541 assert(I != TopLevelLoops.end() && "Old loop not at top level!");
543 assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
544 "Loops already embedded into a subloop!");
547 /// addTopLevelLoop - This adds the specified loop to the collection of
549 void addTopLevelLoop(LoopT *New) {
550 assert(New->getParentLoop() == 0 && "Loop already in subloop!");
551 TopLevelLoops.push_back(New);
554 /// removeBlock - This method completely removes BB from all data structures,
555 /// including all of the Loop objects it is nested in and our mapping from
556 /// BasicBlocks to loops.
557 void removeBlock(BlockT *BB) {
558 typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
559 if (I != BBMap.end()) {
560 for (LoopT *L = I->second; L; L = L->getParentLoop())
561 L->removeBlockFromLoop(BB);
569 static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
570 const LoopT *ParentLoop) {
571 if (SubLoop == 0) return true;
572 if (SubLoop == ParentLoop) return false;
573 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
576 /// Create the loop forest using a stable algorithm.
577 void Analyze(DominatorTreeBase<BlockT> &DomTree);
581 void print(raw_ostream &OS) const;
584 // Implementation in LoopInfoImpl.h
586 __extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
589 class LoopInfo : public FunctionPass {
590 LoopInfoBase<BasicBlock, Loop> LI;
591 friend class LoopBase<BasicBlock, Loop>;
593 void operator=(const LoopInfo &) LLVM_DELETED_FUNCTION;
594 LoopInfo(const LoopInfo &) LLVM_DELETED_FUNCTION;
596 static char ID; // Pass identification, replacement for typeid
598 LoopInfo() : FunctionPass(ID) {
599 initializeLoopInfoPass(*PassRegistry::getPassRegistry());
602 LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
604 /// iterator/begin/end - The interface to the top-level loops in the current
607 typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
608 typedef LoopInfoBase<BasicBlock, Loop>::reverse_iterator reverse_iterator;
609 inline iterator begin() const { return LI.begin(); }
610 inline iterator end() const { return LI.end(); }
611 inline reverse_iterator rbegin() const { return LI.rbegin(); }
612 inline reverse_iterator rend() const { return LI.rend(); }
613 bool empty() const { return LI.empty(); }
615 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
616 /// block is in no loop (for example the entry node), null is returned.
618 inline Loop *getLoopFor(const BasicBlock *BB) const {
619 return LI.getLoopFor(BB);
622 /// operator[] - same as getLoopFor...
624 inline const Loop *operator[](const BasicBlock *BB) const {
625 return LI.getLoopFor(BB);
628 /// getLoopDepth - Return the loop nesting level of the specified block. A
629 /// depth of 0 means the block is not inside any loop.
631 inline unsigned getLoopDepth(const BasicBlock *BB) const {
632 return LI.getLoopDepth(BB);
635 // isLoopHeader - True if the block is a loop header node
636 inline bool isLoopHeader(BasicBlock *BB) const {
637 return LI.isLoopHeader(BB);
640 /// runOnFunction - Calculate the natural loop information.
642 virtual bool runOnFunction(Function &F);
644 virtual void verifyAnalysis() const;
646 virtual void releaseMemory() { LI.releaseMemory(); }
648 virtual void print(raw_ostream &O, const Module* M = 0) const;
650 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
652 /// removeLoop - This removes the specified top-level loop from this loop info
653 /// object. The loop is not deleted, as it will presumably be inserted into
655 inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
657 /// changeLoopFor - Change the top-level loop that contains BB to the
658 /// specified loop. This should be used by transformations that restructure
659 /// the loop hierarchy tree.
660 inline void changeLoopFor(BasicBlock *BB, Loop *L) {
661 LI.changeLoopFor(BB, L);
664 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
665 /// list with the indicated loop.
666 inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
667 LI.changeTopLevelLoop(OldLoop, NewLoop);
670 /// addTopLevelLoop - This adds the specified loop to the collection of
672 inline void addTopLevelLoop(Loop *New) {
673 LI.addTopLevelLoop(New);
676 /// removeBlock - This method completely removes BB from all data structures,
677 /// including all of the Loop objects it is nested in and our mapping from
678 /// BasicBlocks to loops.
679 void removeBlock(BasicBlock *BB) {
683 /// updateUnloop - Update LoopInfo after removing the last backedge from a
684 /// loop--now the "unloop". This updates the loop forest and parent loops for
685 /// each block so that Unloop is no longer referenced, but the caller must
686 /// actually delete the Unloop object.
687 void updateUnloop(Loop *Unloop);
689 /// replacementPreservesLCSSAForm - Returns true if replacing From with To
690 /// everywhere is guaranteed to preserve LCSSA form.
691 bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
692 // Preserving LCSSA form is only problematic if the replacing value is an
694 Instruction *I = dyn_cast<Instruction>(To);
696 // If both instructions are defined in the same basic block then replacement
697 // cannot break LCSSA form.
698 if (I->getParent() == From->getParent())
700 // If the instruction is not defined in a loop then it can safely replace
702 Loop *ToLoop = getLoopFor(I->getParent());
703 if (!ToLoop) return true;
704 // If the replacing instruction is defined in the same loop as the original
705 // instruction, or in a loop that contains it as an inner loop, then using
706 // it as a replacement will not break LCSSA form.
707 return ToLoop->contains(getLoopFor(From->getParent()));
712 // Allow clients to walk the list of nested loops...
713 template <> struct GraphTraits<const Loop*> {
714 typedef const Loop NodeType;
715 typedef LoopInfo::iterator ChildIteratorType;
717 static NodeType *getEntryNode(const Loop *L) { return L; }
718 static inline ChildIteratorType child_begin(NodeType *N) {
721 static inline ChildIteratorType child_end(NodeType *N) {
726 template <> struct GraphTraits<Loop*> {
727 typedef Loop NodeType;
728 typedef LoopInfo::iterator ChildIteratorType;
730 static NodeType *getEntryNode(Loop *L) { return L; }
731 static inline ChildIteratorType child_begin(NodeType *N) {
734 static inline ChildIteratorType child_end(NodeType *N) {
739 } // End llvm namespace