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!");
55 template<class N
, class M
> class LoopInfoBase
;
56 template<class N
, class M
> class LoopBase
;
58 //===----------------------------------------------------------------------===//
59 /// LoopBase class - Instances of this class are used to represent loops that
60 /// are detected in the flow graph
62 template<class BlockT
, class LoopT
>
65 // SubLoops - Loops contained entirely within this one.
66 std::vector
<LoopT
*> SubLoops
;
68 // Blocks - The list of blocks in this loop. First entry is the header node.
69 std::vector
<BlockT
*> Blocks
;
71 LoopBase(const LoopBase
<BlockT
, LoopT
> &) LLVM_DELETED_FUNCTION
;
72 const LoopBase
<BlockT
, LoopT
>&
73 operator=(const LoopBase
<BlockT
, LoopT
> &) LLVM_DELETED_FUNCTION
;
75 /// Loop ctor - This creates an empty loop.
76 LoopBase() : ParentLoop(0) {}
78 for (size_t i
= 0, e
= SubLoops
.size(); i
!= e
; ++i
)
82 /// getLoopDepth - Return the nesting level of this loop. An outer-most
83 /// loop has depth 1, for consistency with loop depth values used for basic
84 /// blocks, where depth 0 is used for blocks not inside any loops.
85 unsigned getLoopDepth() const {
87 for (const LoopT
*CurLoop
= ParentLoop
; CurLoop
;
88 CurLoop
= CurLoop
->ParentLoop
)
92 BlockT
*getHeader() const { return Blocks
.front(); }
93 LoopT
*getParentLoop() const { return ParentLoop
; }
95 /// setParentLoop is a raw interface for bypassing addChildLoop.
96 void setParentLoop(LoopT
*L
) { ParentLoop
= L
; }
98 /// contains - Return true if the specified loop is contained within in
101 bool contains(const LoopT
*L
) const {
102 if (L
== this) return true;
103 if (L
== 0) return false;
104 return contains(L
->getParentLoop());
107 /// contains - Return true if the specified basic block is in this loop.
109 bool contains(const BlockT
*BB
) const {
110 return std::find(block_begin(), block_end(), BB
) != block_end();
113 /// contains - Return true if the specified instruction is in this loop.
115 template<class InstT
>
116 bool contains(const InstT
*Inst
) const {
117 return contains(Inst
->getParent());
120 /// iterator/begin/end - Return the loops contained entirely within this loop.
122 const std::vector
<LoopT
*> &getSubLoops() const { return SubLoops
; }
123 std::vector
<LoopT
*> &getSubLoopsVector() { return SubLoops
; }
124 typedef typename
std::vector
<LoopT
*>::const_iterator iterator
;
125 typedef typename
std::vector
<LoopT
*>::const_reverse_iterator
127 iterator
begin() const { return SubLoops
.begin(); }
128 iterator
end() const { return SubLoops
.end(); }
129 reverse_iterator
rbegin() const { return SubLoops
.rbegin(); }
130 reverse_iterator
rend() const { return SubLoops
.rend(); }
131 bool empty() const { return SubLoops
.empty(); }
133 /// getBlocks - Get a list of the basic blocks which make up this loop.
135 const std::vector
<BlockT
*> &getBlocks() const { return Blocks
; }
136 std::vector
<BlockT
*> &getBlocksVector() { return Blocks
; }
137 typedef typename
std::vector
<BlockT
*>::const_iterator block_iterator
;
138 block_iterator
block_begin() const { return Blocks
.begin(); }
139 block_iterator
block_end() const { return Blocks
.end(); }
141 /// getNumBlocks - Get the number of blocks in this loop in constant time.
142 unsigned getNumBlocks() const {
143 return Blocks
.size();
146 /// isLoopExiting - True if terminator in the block can branch to another
147 /// block that is outside of the current loop.
149 bool isLoopExiting(const BlockT
*BB
) const {
150 typedef GraphTraits
<const BlockT
*> BlockTraits
;
151 for (typename
BlockTraits::ChildIteratorType SI
=
152 BlockTraits::child_begin(BB
),
153 SE
= BlockTraits::child_end(BB
); SI
!= SE
; ++SI
) {
160 /// getNumBackEdges - Calculate the number of back edges to the loop header
162 unsigned getNumBackEdges() const {
163 unsigned NumBackEdges
= 0;
164 BlockT
*H
= getHeader();
166 typedef GraphTraits
<Inverse
<BlockT
*> > InvBlockTraits
;
167 for (typename
InvBlockTraits::ChildIteratorType I
=
168 InvBlockTraits::child_begin(H
),
169 E
= InvBlockTraits::child_end(H
); I
!= E
; ++I
)
176 //===--------------------------------------------------------------------===//
177 // APIs for simple analysis of the loop.
179 // Note that all of these methods can fail on general loops (ie, there may not
180 // be a preheader, etc). For best success, the loop simplification and
181 // induction variable canonicalization pass should be used to normalize loops
182 // for easy analysis. These methods assume canonical loops.
184 /// getExitingBlocks - Return all blocks inside the loop that have successors
185 /// outside of the loop. These are the blocks _inside of the current loop_
186 /// which branch out. The returned list is always unique.
188 void getExitingBlocks(SmallVectorImpl
<BlockT
*> &ExitingBlocks
) const;
190 /// getExitingBlock - If getExitingBlocks would return exactly one block,
191 /// return that block. Otherwise return null.
192 BlockT
*getExitingBlock() const;
194 /// getExitBlocks - Return all of the successor blocks of this loop. These
195 /// are the blocks _outside of the current loop_ which are branched to.
197 void getExitBlocks(SmallVectorImpl
<BlockT
*> &ExitBlocks
) const;
199 /// getExitBlock - If getExitBlocks would return exactly one block,
200 /// return that block. Otherwise return null.
201 BlockT
*getExitBlock() const;
204 typedef std::pair
<const BlockT
*, const BlockT
*> Edge
;
206 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
207 void getExitEdges(SmallVectorImpl
<Edge
> &ExitEdges
) const;
209 /// getLoopPreheader - If there is a preheader for this loop, return it. A
210 /// loop has a preheader if there is only one edge to the header of the loop
211 /// from outside of the loop. If this is the case, the block branching to the
212 /// header of the loop is the preheader node.
214 /// This method returns null if there is no preheader for the loop.
216 BlockT
*getLoopPreheader() const;
218 /// getLoopPredecessor - If the given loop's header has exactly one unique
219 /// predecessor outside the loop, return it. Otherwise return null.
220 /// This is less strict that the loop "preheader" concept, which requires
221 /// the predecessor to have exactly one successor.
223 BlockT
*getLoopPredecessor() const;
225 /// getLoopLatch - If there is a single latch block for this loop, return it.
226 /// A latch block is a block that contains a branch back to the header.
227 BlockT
*getLoopLatch() const;
229 //===--------------------------------------------------------------------===//
230 // APIs for updating loop information after changing the CFG
233 /// addBasicBlockToLoop - This method is used by other analyses to update loop
234 /// information. NewBB is set to be a new member of the current loop.
235 /// Because of this, it is added as a member of all parent loops, and is added
236 /// to the specified LoopInfo object as being in the current basic block. It
237 /// is not valid to replace the loop header with this method.
239 void addBasicBlockToLoop(BlockT
*NewBB
, LoopInfoBase
<BlockT
, LoopT
> &LI
);
241 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
242 /// the OldChild entry in our children list with NewChild, and updates the
243 /// parent pointer of OldChild to be null and the NewChild to be this loop.
244 /// This updates the loop depth of the new child.
245 void replaceChildLoopWith(LoopT
*OldChild
, LoopT
*NewChild
);
247 /// addChildLoop - Add the specified loop to be a child of this loop. This
248 /// updates the loop depth of the new child.
250 void addChildLoop(LoopT
*NewChild
) {
251 assert(NewChild
->ParentLoop
== 0 && "NewChild already has a parent!");
252 NewChild
->ParentLoop
= static_cast<LoopT
*>(this);
253 SubLoops
.push_back(NewChild
);
256 /// removeChildLoop - This removes the specified child from being a subloop of
257 /// this loop. The loop is not deleted, as it will presumably be inserted
258 /// into another loop.
259 LoopT
*removeChildLoop(iterator I
) {
260 assert(I
!= SubLoops
.end() && "Cannot remove end iterator!");
262 assert(Child
->ParentLoop
== this && "Child is not a child of this loop!");
263 SubLoops
.erase(SubLoops
.begin()+(I
-begin()));
264 Child
->ParentLoop
= 0;
268 /// addBlockEntry - This adds a basic block directly to the basic block list.
269 /// This should only be used by transformations that create new loops. Other
270 /// transformations should use addBasicBlockToLoop.
271 void addBlockEntry(BlockT
*BB
) {
272 Blocks
.push_back(BB
);
275 /// moveToHeader - This method is used to move BB (which must be part of this
276 /// loop) to be the loop header of the loop (the block that dominates all
278 void moveToHeader(BlockT
*BB
) {
279 if (Blocks
[0] == BB
) return;
280 for (unsigned i
= 0; ; ++i
) {
281 assert(i
!= Blocks
.size() && "Loop does not contain BB!");
282 if (Blocks
[i
] == BB
) {
283 Blocks
[i
] = Blocks
[0];
290 /// removeBlockFromLoop - This removes the specified basic block from the
291 /// current loop, updating the Blocks as appropriate. This does not update
292 /// the mapping in the LoopInfo class.
293 void removeBlockFromLoop(BlockT
*BB
) {
294 RemoveFromVector(Blocks
, BB
);
297 /// verifyLoop - Verify loop structure
298 void verifyLoop() const;
300 /// verifyLoop - Verify loop structure of this loop and all nested loops.
301 void verifyLoopNest(DenseSet
<const LoopT
*> *Loops
) const;
303 void print(raw_ostream
&OS
, unsigned Depth
= 0) const;
306 friend class LoopInfoBase
<BlockT
, LoopT
>;
307 explicit LoopBase(BlockT
*BB
) : ParentLoop(0) {
308 Blocks
.push_back(BB
);
312 template<class BlockT
, class LoopT
>
313 raw_ostream
& operator<<(raw_ostream
&OS
, const LoopBase
<BlockT
, LoopT
> &Loop
) {
318 // Implementation in LoopInfoImpl.h
320 __extension__
extern template class LoopBase
<BasicBlock
, Loop
>;
323 class Loop
: public LoopBase
<BasicBlock
, Loop
> {
327 /// isLoopInvariant - Return true if the specified value is loop invariant
329 bool isLoopInvariant(Value
*V
) const;
331 /// hasLoopInvariantOperands - Return true if all the operands of the
332 /// specified instruction are loop invariant.
333 bool hasLoopInvariantOperands(Instruction
*I
) const;
335 /// makeLoopInvariant - If the given value is an instruction inside of the
336 /// loop and it can be hoisted, do so to make it trivially loop-invariant.
337 /// Return true if the value after any hoisting is loop invariant. This
338 /// function can be used as a slightly more aggressive replacement for
341 /// If InsertPt is specified, it is the point to hoist instructions to.
342 /// If null, the terminator of the loop preheader is used.
344 bool makeLoopInvariant(Value
*V
, bool &Changed
,
345 Instruction
*InsertPt
= 0) const;
347 /// makeLoopInvariant - If the given instruction is inside of the
348 /// loop and it can be hoisted, do so to make it trivially loop-invariant.
349 /// Return true if the instruction after any hoisting is loop invariant. This
350 /// function can be used as a slightly more aggressive replacement for
353 /// If InsertPt is specified, it is the point to hoist instructions to.
354 /// If null, the terminator of the loop preheader is used.
356 bool makeLoopInvariant(Instruction
*I
, bool &Changed
,
357 Instruction
*InsertPt
= 0) const;
359 /// getCanonicalInductionVariable - Check to see if the loop has a canonical
360 /// induction variable: an integer recurrence that starts at 0 and increments
361 /// by one each time through the loop. If so, return the phi node that
362 /// corresponds to it.
364 /// The IndVarSimplify pass transforms loops to have a canonical induction
367 PHINode
*getCanonicalInductionVariable() const;
369 /// isLCSSAForm - Return true if the Loop is in LCSSA form
370 bool isLCSSAForm(DominatorTree
&DT
) const;
372 /// isLoopSimplifyForm - Return true if the Loop is in the form that
373 /// the LoopSimplify form transforms loops to, which is sometimes called
375 bool isLoopSimplifyForm() const;
377 /// isSafeToClone - Return true if the loop body is safe to clone in practice.
378 bool isSafeToClone() const;
380 /// Returns true if the loop is annotated parallel.
382 /// A parallel loop can be assumed to not contain any dependencies between
383 /// iterations by the compiler. That is, any loop-carried dependency checking
384 /// can be skipped completely when parallelizing the loop on the target
385 /// machine. Thus, if the parallel loop information originates from the
386 /// programmer, e.g. via the OpenMP parallel for pragma, it is the
387 /// programmer's responsibility to ensure there are no loop-carried
388 /// dependencies. The final execution order of the instructions across
389 /// iterations is not guaranteed, thus, the end result might or might not
390 /// implement actual concurrent execution of instructions across multiple
392 bool isAnnotatedParallel() const;
394 /// hasDedicatedExits - Return true if no exit block for the loop
395 /// has a predecessor that is outside the loop.
396 bool hasDedicatedExits() const;
398 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
399 /// These are the blocks _outside of the current loop_ which are branched to.
400 /// This assumes that loop exits are in canonical form.
402 void getUniqueExitBlocks(SmallVectorImpl
<BasicBlock
*> &ExitBlocks
) const;
404 /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
405 /// block, return that block. Otherwise return null.
406 BasicBlock
*getUniqueExitBlock() const;
411 friend class LoopInfoBase
<BasicBlock
, Loop
>;
412 explicit Loop(BasicBlock
*BB
) : LoopBase
<BasicBlock
, Loop
>(BB
) {}
415 //===----------------------------------------------------------------------===//
416 /// LoopInfo - This class builds and contains all of the top level loop
417 /// structures in the specified function.
420 template<class BlockT
, class LoopT
>
422 // BBMap - Mapping of basic blocks to the inner most loop they occur in
423 DenseMap
<BlockT
*, LoopT
*> BBMap
;
424 std::vector
<LoopT
*> TopLevelLoops
;
425 friend class LoopBase
<BlockT
, LoopT
>;
426 friend class LoopInfo
;
428 void operator=(const LoopInfoBase
&) LLVM_DELETED_FUNCTION
;
429 LoopInfoBase(const LoopInfo
&) LLVM_DELETED_FUNCTION
;
432 ~LoopInfoBase() { releaseMemory(); }
434 void releaseMemory() {
435 for (typename
std::vector
<LoopT
*>::iterator I
=
436 TopLevelLoops
.begin(), E
= TopLevelLoops
.end(); I
!= E
; ++I
)
437 delete *I
; // Delete all of the loops...
439 BBMap
.clear(); // Reset internal state of analysis
440 TopLevelLoops
.clear();
443 /// iterator/begin/end - The interface to the top-level loops in the current
446 typedef typename
std::vector
<LoopT
*>::const_iterator iterator
;
447 typedef typename
std::vector
<LoopT
*>::const_reverse_iterator
449 iterator
begin() const { return TopLevelLoops
.begin(); }
450 iterator
end() const { return TopLevelLoops
.end(); }
451 reverse_iterator
rbegin() const { return TopLevelLoops
.rbegin(); }
452 reverse_iterator
rend() const { return TopLevelLoops
.rend(); }
453 bool empty() const { return TopLevelLoops
.empty(); }
455 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
456 /// block is in no loop (for example the entry node), null is returned.
458 LoopT
*getLoopFor(const BlockT
*BB
) const {
459 return BBMap
.lookup(const_cast<BlockT
*>(BB
));
462 /// operator[] - same as getLoopFor...
464 const LoopT
*operator[](const BlockT
*BB
) const {
465 return getLoopFor(BB
);
468 /// getLoopDepth - Return the loop nesting level of the specified block. A
469 /// depth of 0 means the block is not inside any loop.
471 unsigned getLoopDepth(const BlockT
*BB
) const {
472 const LoopT
*L
= getLoopFor(BB
);
473 return L
? L
->getLoopDepth() : 0;
476 // isLoopHeader - True if the block is a loop header node
477 bool isLoopHeader(BlockT
*BB
) const {
478 const LoopT
*L
= getLoopFor(BB
);
479 return L
&& L
->getHeader() == BB
;
482 /// removeLoop - This removes the specified top-level loop from this loop info
483 /// object. The loop is not deleted, as it will presumably be inserted into
485 LoopT
*removeLoop(iterator I
) {
486 assert(I
!= end() && "Cannot remove end iterator!");
488 assert(L
->getParentLoop() == 0 && "Not a top-level loop!");
489 TopLevelLoops
.erase(TopLevelLoops
.begin() + (I
-begin()));
493 /// changeLoopFor - Change the top-level loop that contains BB to the
494 /// specified loop. This should be used by transformations that restructure
495 /// the loop hierarchy tree.
496 void changeLoopFor(BlockT
*BB
, LoopT
*L
) {
504 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
505 /// list with the indicated loop.
506 void changeTopLevelLoop(LoopT
*OldLoop
,
508 typename
std::vector
<LoopT
*>::iterator I
=
509 std::find(TopLevelLoops
.begin(), TopLevelLoops
.end(), OldLoop
);
510 assert(I
!= TopLevelLoops
.end() && "Old loop not at top level!");
512 assert(NewLoop
->ParentLoop
== 0 && OldLoop
->ParentLoop
== 0 &&
513 "Loops already embedded into a subloop!");
516 /// addTopLevelLoop - This adds the specified loop to the collection of
518 void addTopLevelLoop(LoopT
*New
) {
519 assert(New
->getParentLoop() == 0 && "Loop already in subloop!");
520 TopLevelLoops
.push_back(New
);
523 /// removeBlock - This method completely removes BB from all data structures,
524 /// including all of the Loop objects it is nested in and our mapping from
525 /// BasicBlocks to loops.
526 void removeBlock(BlockT
*BB
) {
527 typename DenseMap
<BlockT
*, LoopT
*>::iterator I
= BBMap
.find(BB
);
528 if (I
!= BBMap
.end()) {
529 for (LoopT
*L
= I
->second
; L
; L
= L
->getParentLoop())
530 L
->removeBlockFromLoop(BB
);
538 static bool isNotAlreadyContainedIn(const LoopT
*SubLoop
,
539 const LoopT
*ParentLoop
) {
540 if (SubLoop
== 0) return true;
541 if (SubLoop
== ParentLoop
) return false;
542 return isNotAlreadyContainedIn(SubLoop
->getParentLoop(), ParentLoop
);
545 /// Create the loop forest using a stable algorithm.
546 void Analyze(DominatorTreeBase
<BlockT
> &DomTree
);
550 void print(raw_ostream
&OS
) const;
553 // Implementation in LoopInfoImpl.h
555 __extension__
extern template class LoopInfoBase
<BasicBlock
, Loop
>;
558 class LoopInfo
: public FunctionPass
{
559 LoopInfoBase
<BasicBlock
, Loop
> LI
;
560 friend class LoopBase
<BasicBlock
, Loop
>;
562 void operator=(const LoopInfo
&) LLVM_DELETED_FUNCTION
;
563 LoopInfo(const LoopInfo
&) LLVM_DELETED_FUNCTION
;
565 static char ID
; // Pass identification, replacement for typeid
567 LoopInfo() : FunctionPass(ID
) {
568 initializeLoopInfoPass(*PassRegistry::getPassRegistry());
571 LoopInfoBase
<BasicBlock
, Loop
>& getBase() { return LI
; }
573 /// iterator/begin/end - The interface to the top-level loops in the current
576 typedef LoopInfoBase
<BasicBlock
, Loop
>::iterator iterator
;
577 typedef LoopInfoBase
<BasicBlock
, Loop
>::reverse_iterator reverse_iterator
;
578 inline iterator
begin() const { return LI
.begin(); }
579 inline iterator
end() const { return LI
.end(); }
580 inline reverse_iterator
rbegin() const { return LI
.rbegin(); }
581 inline reverse_iterator
rend() const { return LI
.rend(); }
582 bool empty() const { return LI
.empty(); }
584 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
585 /// block is in no loop (for example the entry node), null is returned.
587 inline Loop
*getLoopFor(const BasicBlock
*BB
) const {
588 return LI
.getLoopFor(BB
);
591 /// operator[] - same as getLoopFor...
593 inline const Loop
*operator[](const BasicBlock
*BB
) const {
594 return LI
.getLoopFor(BB
);
597 /// getLoopDepth - Return the loop nesting level of the specified block. A
598 /// depth of 0 means the block is not inside any loop.
600 inline unsigned getLoopDepth(const BasicBlock
*BB
) const {
601 return LI
.getLoopDepth(BB
);
604 // isLoopHeader - True if the block is a loop header node
605 inline bool isLoopHeader(BasicBlock
*BB
) const {
606 return LI
.isLoopHeader(BB
);
609 /// runOnFunction - Calculate the natural loop information.
611 virtual bool runOnFunction(Function
&F
);
613 virtual void verifyAnalysis() const;
615 virtual void releaseMemory() { LI
.releaseMemory(); }
617 virtual void print(raw_ostream
&O
, const Module
* M
= 0) const;
619 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const;
621 /// removeLoop - This removes the specified top-level loop from this loop info
622 /// object. The loop is not deleted, as it will presumably be inserted into
624 inline Loop
*removeLoop(iterator I
) { return LI
.removeLoop(I
); }
626 /// changeLoopFor - Change the top-level loop that contains BB to the
627 /// specified loop. This should be used by transformations that restructure
628 /// the loop hierarchy tree.
629 inline void changeLoopFor(BasicBlock
*BB
, Loop
*L
) {
630 LI
.changeLoopFor(BB
, L
);
633 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
634 /// list with the indicated loop.
635 inline void changeTopLevelLoop(Loop
*OldLoop
, Loop
*NewLoop
) {
636 LI
.changeTopLevelLoop(OldLoop
, NewLoop
);
639 /// addTopLevelLoop - This adds the specified loop to the collection of
641 inline void addTopLevelLoop(Loop
*New
) {
642 LI
.addTopLevelLoop(New
);
645 /// removeBlock - This method completely removes BB from all data structures,
646 /// including all of the Loop objects it is nested in and our mapping from
647 /// BasicBlocks to loops.
648 void removeBlock(BasicBlock
*BB
) {
652 /// updateUnloop - Update LoopInfo after removing the last backedge from a
653 /// loop--now the "unloop". This updates the loop forest and parent loops for
654 /// each block so that Unloop is no longer referenced, but the caller must
655 /// actually delete the Unloop object.
656 void updateUnloop(Loop
*Unloop
);
658 /// replacementPreservesLCSSAForm - Returns true if replacing From with To
659 /// everywhere is guaranteed to preserve LCSSA form.
660 bool replacementPreservesLCSSAForm(Instruction
*From
, Value
*To
) {
661 // Preserving LCSSA form is only problematic if the replacing value is an
663 Instruction
*I
= dyn_cast
<Instruction
>(To
);
665 // If both instructions are defined in the same basic block then replacement
666 // cannot break LCSSA form.
667 if (I
->getParent() == From
->getParent())
669 // If the instruction is not defined in a loop then it can safely replace
671 Loop
*ToLoop
= getLoopFor(I
->getParent());
672 if (!ToLoop
) return true;
673 // If the replacing instruction is defined in the same loop as the original
674 // instruction, or in a loop that contains it as an inner loop, then using
675 // it as a replacement will not break LCSSA form.
676 return ToLoop
->contains(getLoopFor(From
->getParent()));
681 // Allow clients to walk the list of nested loops...
682 template <> struct GraphTraits
<const Loop
*> {
683 typedef const Loop NodeType
;
684 typedef LoopInfo::iterator ChildIteratorType
;
686 static NodeType
*getEntryNode(const Loop
*L
) { return L
; }
687 static inline ChildIteratorType
child_begin(NodeType
*N
) {
690 static inline ChildIteratorType
child_end(NodeType
*N
) {
695 template <> struct GraphTraits
<Loop
*> {
696 typedef Loop NodeType
;
697 typedef LoopInfo::iterator ChildIteratorType
;
699 static NodeType
*getEntryNode(Loop
*L
) { return L
; }
700 static inline ChildIteratorType
child_begin(NodeType
*N
) {
703 static inline ChildIteratorType
child_end(NodeType
*N
) {
708 } // End llvm namespace