2003-09-30 18:37:50 +00:00
|
|
|
//===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-26 18:41:20 +00:00
|
|
|
//
|
|
|
|
// This file defines the LoopInfo class that is used to identify natural loops
|
2009-09-26 15:09:53 +00:00
|
|
|
// and determine the loop depth of various nodes of the CFG. A natural loop
|
|
|
|
// has exactly one entry-point, which is called the header. Note that natural
|
2004-04-12 20:26:17 +00:00
|
|
|
// loops may actually be several loops that share the same header node.
|
2003-02-27 00:37:22 +00:00
|
|
|
//
|
|
|
|
// This analysis calculates the nesting structure of loops in a function. For
|
|
|
|
// each natural loop identified, this analysis identifies natural loops
|
2004-04-19 03:03:19 +00:00
|
|
|
// contained entirely within the loop and the basic blocks the make up the loop.
|
2003-02-27 00:37:22 +00:00
|
|
|
//
|
2004-04-19 03:03:19 +00:00
|
|
|
// It can calculate on the fly various bits of information, for example:
|
|
|
|
//
|
|
|
|
// * whether there is a preheader for the loop
|
|
|
|
// * the number of back edges to the header
|
|
|
|
// * whether or not a particular block branches out of the loop
|
|
|
|
// * the successor blocks of the loop
|
|
|
|
// * the loop depth
|
|
|
|
// * etc...
|
2001-11-26 18:41:20 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_ANALYSIS_LOOPINFO_H
|
|
|
|
#define LLVM_ANALYSIS_LOOPINFO_H
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2010-11-30 18:51:19 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2011-08-26 03:06:34 +00:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-11-15 05:00:15 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/Pass.h"
|
2007-11-14 02:33:58 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2008-05-14 01:02:49 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2007-11-14 02:33:58 +00:00
|
|
|
template<typename T>
|
2012-06-20 08:39:33 +00:00
|
|
|
inline void RemoveFromVector(std::vector<T*> &V, T *N) {
|
2007-11-14 02:33:58 +00:00
|
|
|
typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
|
|
|
|
assert(I != V.end() && "N is not in this list!");
|
|
|
|
V.erase(I);
|
|
|
|
}
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2007-06-08 00:17:13 +00:00
|
|
|
class DominatorTree;
|
2002-04-28 16:19:42 +00:00
|
|
|
class LoopInfo;
|
2009-07-13 21:51:15 +00:00
|
|
|
class Loop;
|
2013-05-28 20:00:34 +00:00
|
|
|
class MDNode;
|
2011-01-02 22:09:33 +00:00
|
|
|
class PHINode;
|
2013-03-09 18:05:34 +00:00
|
|
|
class raw_ostream;
|
2009-07-13 21:51:15 +00:00
|
|
|
template<class N, class M> class LoopInfoBase;
|
|
|
|
template<class N, class M> class LoopBase;
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-11 06:27:42 +00:00
|
|
|
/// LoopBase class - Instances of this class are used to represent loops that
|
|
|
|
/// are detected in the flow graph
|
2002-09-26 05:32:43 +00:00
|
|
|
///
|
2009-07-13 21:51:15 +00:00
|
|
|
template<class BlockT, class LoopT>
|
2007-11-14 02:33:58 +00:00
|
|
|
class LoopBase {
|
2009-07-13 21:51:15 +00:00
|
|
|
LoopT *ParentLoop;
|
2008-01-11 06:27:42 +00:00
|
|
|
// SubLoops - Loops contained entirely within this one.
|
2009-07-13 21:51:15 +00:00
|
|
|
std::vector<LoopT *> SubLoops;
|
2008-01-11 06:27:42 +00:00
|
|
|
|
|
|
|
// Blocks - The list of blocks in this loop. First entry is the header node.
|
|
|
|
std::vector<BlockT*> Blocks;
|
2007-11-14 02:33:58 +00:00
|
|
|
|
2012-09-17 06:43:55 +00:00
|
|
|
LoopBase(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
|
|
|
|
const LoopBase<BlockT, LoopT>&
|
|
|
|
operator=(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
|
2001-11-26 18:41:20 +00:00
|
|
|
public:
|
2004-04-12 20:26:17 +00:00
|
|
|
/// Loop ctor - This creates an empty loop.
|
2007-11-14 02:33:58 +00:00
|
|
|
LoopBase() : ParentLoop(0) {}
|
|
|
|
~LoopBase() {
|
2008-05-05 18:30:58 +00:00
|
|
|
for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
|
2004-04-18 05:37:42 +00:00
|
|
|
delete SubLoops[i];
|
|
|
|
}
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2008-01-29 12:12:40 +00:00
|
|
|
/// getLoopDepth - Return the nesting level of this loop. An outer-most
|
|
|
|
/// loop has depth 1, for consistency with loop depth values used for basic
|
|
|
|
/// blocks, where depth 0 is used for blocks not inside any loops.
|
2004-04-19 03:03:19 +00:00
|
|
|
unsigned getLoopDepth() const {
|
2008-01-29 12:12:40 +00:00
|
|
|
unsigned D = 1;
|
2009-07-13 21:51:15 +00:00
|
|
|
for (const LoopT *CurLoop = ParentLoop; CurLoop;
|
2007-11-14 02:33:58 +00:00
|
|
|
CurLoop = CurLoop->ParentLoop)
|
2004-04-19 03:03:19 +00:00
|
|
|
++D;
|
|
|
|
return D;
|
|
|
|
}
|
2007-11-14 02:33:58 +00:00
|
|
|
BlockT *getHeader() const { return Blocks.front(); }
|
2009-07-13 21:51:15 +00:00
|
|
|
LoopT *getParentLoop() const { return ParentLoop; }
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2012-06-20 05:23:33 +00:00
|
|
|
/// setParentLoop is a raw interface for bypassing addChildLoop.
|
|
|
|
void setParentLoop(LoopT *L) { ParentLoop = L; }
|
|
|
|
|
2009-12-18 01:24:09 +00:00
|
|
|
/// contains - Return true if the specified loop is contained within in
|
|
|
|
/// this loop.
|
|
|
|
///
|
|
|
|
bool contains(const LoopT *L) const {
|
|
|
|
if (L == this) return true;
|
|
|
|
if (L == 0) return false;
|
|
|
|
return contains(L->getParentLoop());
|
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2009-12-18 01:24:09 +00:00
|
|
|
/// contains - Return true if the specified basic block is in this loop.
|
2004-01-30 17:22:50 +00:00
|
|
|
///
|
2007-11-14 02:33:58 +00:00
|
|
|
bool contains(const BlockT *BB) const {
|
2009-02-07 16:10:35 +00:00
|
|
|
return std::find(block_begin(), block_end(), BB) != block_end();
|
2007-11-14 02:33:58 +00:00
|
|
|
}
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2009-12-18 01:24:09 +00:00
|
|
|
/// contains - Return true if the specified instruction is in this loop.
|
|
|
|
///
|
|
|
|
template<class InstT>
|
|
|
|
bool contains(const InstT *Inst) const {
|
|
|
|
return contains(Inst->getParent());
|
|
|
|
}
|
|
|
|
|
2004-01-08 00:09:44 +00:00
|
|
|
/// iterator/begin/end - Return the loops contained entirely within this loop.
|
2002-09-26 05:32:43 +00:00
|
|
|
///
|
2009-07-13 21:51:15 +00:00
|
|
|
const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
|
2012-06-20 05:23:33 +00:00
|
|
|
std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
|
2009-07-13 21:51:15 +00:00
|
|
|
typedef typename std::vector<LoopT *>::const_iterator iterator;
|
2012-06-26 04:11:38 +00:00
|
|
|
typedef typename std::vector<LoopT *>::const_reverse_iterator
|
|
|
|
reverse_iterator;
|
2004-01-08 00:09:44 +00:00
|
|
|
iterator begin() const { return SubLoops.begin(); }
|
|
|
|
iterator end() const { return SubLoops.end(); }
|
2012-06-26 04:11:38 +00:00
|
|
|
reverse_iterator rbegin() const { return SubLoops.rbegin(); }
|
|
|
|
reverse_iterator rend() const { return SubLoops.rend(); }
|
2007-10-01 16:01:23 +00:00
|
|
|
bool empty() const { return SubLoops.empty(); }
|
2003-02-27 00:37:22 +00:00
|
|
|
|
|
|
|
/// getBlocks - Get a list of the basic blocks which make up this loop.
|
|
|
|
///
|
2007-11-14 02:33:58 +00:00
|
|
|
const std::vector<BlockT*> &getBlocks() const { return Blocks; }
|
2012-06-20 05:23:33 +00:00
|
|
|
std::vector<BlockT*> &getBlocksVector() { return Blocks; }
|
2007-11-14 02:33:58 +00:00
|
|
|
typedef typename std::vector<BlockT*>::const_iterator block_iterator;
|
2004-04-18 23:37:03 +00:00
|
|
|
block_iterator block_begin() const { return Blocks.begin(); }
|
|
|
|
block_iterator block_end() const { return Blocks.end(); }
|
2003-02-27 00:37:22 +00:00
|
|
|
|
2011-08-10 00:49:12 +00:00
|
|
|
/// getNumBlocks - Get the number of blocks in this loop in constant time.
|
2011-08-10 00:28:10 +00:00
|
|
|
unsigned getNumBlocks() const {
|
2011-08-10 00:49:12 +00:00
|
|
|
return Blocks.size();
|
2011-08-10 00:28:10 +00:00
|
|
|
}
|
|
|
|
|
2009-11-09 18:20:38 +00:00
|
|
|
/// isLoopExiting - True if terminator in the block can branch to another
|
|
|
|
/// block that is outside of the current loop.
|
2003-02-27 00:37:22 +00:00
|
|
|
///
|
2009-10-24 23:34:26 +00:00
|
|
|
bool isLoopExiting(const BlockT *BB) const {
|
2013-03-09 18:24:26 +00:00
|
|
|
typedef GraphTraits<const BlockT*> BlockTraits;
|
2007-11-27 03:43:35 +00:00
|
|
|
for (typename BlockTraits::ChildIteratorType SI =
|
2013-03-09 18:24:26 +00:00
|
|
|
BlockTraits::child_begin(BB),
|
|
|
|
SE = BlockTraits::child_end(BB); SI != SE; ++SI) {
|
2007-11-14 02:33:58 +00:00
|
|
|
if (!contains(*SI))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2002-10-11 05:31:10 +00:00
|
|
|
|
2003-02-27 00:37:22 +00:00
|
|
|
/// getNumBackEdges - Calculate the number of back edges to the loop header
|
|
|
|
///
|
2007-11-14 02:33:58 +00:00
|
|
|
unsigned getNumBackEdges() const {
|
|
|
|
unsigned NumBackEdges = 0;
|
|
|
|
BlockT *H = getHeader();
|
|
|
|
|
2007-11-27 03:43:35 +00:00
|
|
|
typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
|
|
|
|
for (typename InvBlockTraits::ChildIteratorType I =
|
2013-03-09 18:24:26 +00:00
|
|
|
InvBlockTraits::child_begin(H),
|
|
|
|
E = InvBlockTraits::child_end(H); I != E; ++I)
|
2007-11-14 02:33:58 +00:00
|
|
|
if (contains(*I))
|
|
|
|
++NumBackEdges;
|
|
|
|
|
|
|
|
return NumBackEdges;
|
|
|
|
}
|
2002-09-25 19:10:06 +00:00
|
|
|
|
2004-04-15 15:15:40 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// APIs for simple analysis of the loop.
|
|
|
|
//
|
|
|
|
// Note that all of these methods can fail on general loops (ie, there may not
|
|
|
|
// be a preheader, etc). For best success, the loop simplification and
|
|
|
|
// induction variable canonicalization pass should be used to normalize loops
|
|
|
|
// for easy analysis. These methods assume canonical loops.
|
|
|
|
|
2006-10-28 01:24:05 +00:00
|
|
|
/// getExitingBlocks - Return all blocks inside the loop that have successors
|
|
|
|
/// outside of the loop. These are the blocks _inside of the current loop_
|
|
|
|
/// which branch out. The returned list is always unique.
|
|
|
|
///
|
2012-06-20 03:42:09 +00:00
|
|
|
void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
|
2006-10-28 01:24:05 +00:00
|
|
|
|
2009-02-12 18:08:24 +00:00
|
|
|
/// getExitingBlock - If getExitingBlocks would return exactly one block,
|
|
|
|
/// return that block. Otherwise return null.
|
2012-06-20 03:42:09 +00:00
|
|
|
BlockT *getExitingBlock() const;
|
2009-02-12 18:08:24 +00:00
|
|
|
|
2004-04-18 22:14:10 +00:00
|
|
|
/// getExitBlocks - Return all of the successor blocks of this loop. These
|
|
|
|
/// are the blocks _outside of the current loop_ which are branched to.
|
|
|
|
///
|
2012-06-20 03:42:09 +00:00
|
|
|
void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
|
2004-04-18 22:14:10 +00:00
|
|
|
|
2009-05-08 20:07:23 +00:00
|
|
|
/// getExitBlock - If getExitBlocks would return exactly one block,
|
|
|
|
/// return that block. Otherwise return null.
|
2012-06-20 03:42:09 +00:00
|
|
|
BlockT *getExitBlock() const;
|
2009-05-08 20:07:23 +00:00
|
|
|
|
2010-07-17 07:34:01 +00:00
|
|
|
/// Edge type.
|
2012-06-20 03:42:09 +00:00
|
|
|
typedef std::pair<const BlockT*, const BlockT*> Edge;
|
2010-07-17 07:34:01 +00:00
|
|
|
|
2009-08-08 18:44:18 +00:00
|
|
|
/// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
|
2012-06-20 03:42:09 +00:00
|
|
|
void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
|
2009-08-08 18:44:18 +00:00
|
|
|
|
2002-09-26 05:32:43 +00:00
|
|
|
/// getLoopPreheader - If there is a preheader for this loop, return it. A
|
|
|
|
/// loop has a preheader if there is only one edge to the header of the loop
|
|
|
|
/// from outside of the loop. If this is the case, the block branching to the
|
2004-04-15 15:15:40 +00:00
|
|
|
/// header of the loop is the preheader node.
|
2002-09-26 05:32:43 +00:00
|
|
|
///
|
2004-04-15 15:15:40 +00:00
|
|
|
/// This method returns null if there is no preheader for the loop.
|
2002-09-26 05:32:43 +00:00
|
|
|
///
|
2012-06-20 03:42:09 +00:00
|
|
|
BlockT *getLoopPreheader() const;
|
2010-06-22 15:25:42 +00:00
|
|
|
|
|
|
|
/// getLoopPredecessor - If the given loop's header has exactly one unique
|
|
|
|
/// predecessor outside the loop, return it. Otherwise return null.
|
|
|
|
/// This is less strict that the loop "preheader" concept, which requires
|
|
|
|
/// the predecessor to have exactly one successor.
|
|
|
|
///
|
2012-06-20 03:42:09 +00:00
|
|
|
BlockT *getLoopPredecessor() const;
|
2002-09-26 05:32:43 +00:00
|
|
|
|
2009-02-18 16:43:19 +00:00
|
|
|
/// getLoopLatch - If there is a single latch block for this loop, return it.
|
|
|
|
/// A latch block is a block that contains a branch back to the header.
|
2012-06-20 03:42:09 +00:00
|
|
|
BlockT *getLoopLatch() const;
|
2004-04-15 15:15:40 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// APIs for updating loop information after changing the CFG
|
|
|
|
//
|
|
|
|
|
2003-02-27 00:37:22 +00:00
|
|
|
/// addBasicBlockToLoop - This method is used by other analyses to update loop
|
|
|
|
/// information. NewBB is set to be a new member of the current loop.
|
2002-09-26 05:32:43 +00:00
|
|
|
/// Because of this, it is added as a member of all parent loops, and is added
|
|
|
|
/// to the specified LoopInfo object as being in the current basic block. It
|
|
|
|
/// is not valid to replace the loop header with this method.
|
|
|
|
///
|
2009-07-13 21:51:15 +00:00
|
|
|
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
|
2002-09-26 05:32:43 +00:00
|
|
|
|
2004-04-12 20:26:17 +00:00
|
|
|
/// replaceChildLoopWith - This is used when splitting loops up. It replaces
|
|
|
|
/// the OldChild entry in our children list with NewChild, and updates the
|
|
|
|
/// parent pointer of OldChild to be null and the NewChild to be this loop.
|
|
|
|
/// This updates the loop depth of the new child.
|
2012-06-20 03:42:09 +00:00
|
|
|
void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
|
2004-04-12 20:26:17 +00:00
|
|
|
|
|
|
|
/// addChildLoop - Add the specified loop to be a child of this loop. This
|
|
|
|
/// updates the loop depth of the new child.
|
|
|
|
///
|
2009-07-13 21:51:15 +00:00
|
|
|
void addChildLoop(LoopT *NewChild) {
|
2007-11-14 02:33:58 +00:00
|
|
|
assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
|
2009-07-13 21:51:15 +00:00
|
|
|
NewChild->ParentLoop = static_cast<LoopT *>(this);
|
2007-11-14 02:33:58 +00:00
|
|
|
SubLoops.push_back(NewChild);
|
|
|
|
}
|
2004-04-12 20:26:17 +00:00
|
|
|
|
|
|
|
/// removeChildLoop - This removes the specified child from being a subloop of
|
|
|
|
/// this loop. The loop is not deleted, as it will presumably be inserted
|
|
|
|
/// into another loop.
|
2009-07-13 21:51:15 +00:00
|
|
|
LoopT *removeChildLoop(iterator I) {
|
2007-11-14 02:33:58 +00:00
|
|
|
assert(I != SubLoops.end() && "Cannot remove end iterator!");
|
2009-07-13 21:51:15 +00:00
|
|
|
LoopT *Child = *I;
|
2007-11-14 02:33:58 +00:00
|
|
|
assert(Child->ParentLoop == this && "Child is not a child of this loop!");
|
|
|
|
SubLoops.erase(SubLoops.begin()+(I-begin()));
|
|
|
|
Child->ParentLoop = 0;
|
|
|
|
return Child;
|
|
|
|
}
|
2004-04-12 20:26:17 +00:00
|
|
|
|
|
|
|
/// addBlockEntry - This adds a basic block directly to the basic block list.
|
|
|
|
/// This should only be used by transformations that create new loops. Other
|
|
|
|
/// transformations should use addBasicBlockToLoop.
|
2007-11-14 02:33:58 +00:00
|
|
|
void addBlockEntry(BlockT *BB) {
|
2004-04-12 20:26:17 +00:00
|
|
|
Blocks.push_back(BB);
|
|
|
|
}
|
|
|
|
|
2005-03-06 20:55:34 +00:00
|
|
|
/// moveToHeader - This method is used to move BB (which must be part of this
|
|
|
|
/// loop) to be the loop header of the loop (the block that dominates all
|
|
|
|
/// others).
|
2007-11-14 02:33:58 +00:00
|
|
|
void moveToHeader(BlockT *BB) {
|
2005-03-06 20:55:34 +00:00
|
|
|
if (Blocks[0] == BB) return;
|
|
|
|
for (unsigned i = 0; ; ++i) {
|
|
|
|
assert(i != Blocks.size() && "Loop does not contain BB!");
|
|
|
|
if (Blocks[i] == BB) {
|
|
|
|
Blocks[i] = Blocks[0];
|
|
|
|
Blocks[0] = BB;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-12 20:26:17 +00:00
|
|
|
/// removeBlockFromLoop - This removes the specified basic block from the
|
2004-04-18 22:14:10 +00:00
|
|
|
/// current loop, updating the Blocks as appropriate. This does not update
|
|
|
|
/// the mapping in the LoopInfo class.
|
2007-11-14 02:33:58 +00:00
|
|
|
void removeBlockFromLoop(BlockT *BB) {
|
|
|
|
RemoveFromVector(Blocks, BB);
|
|
|
|
}
|
2004-04-12 20:26:17 +00:00
|
|
|
|
2007-07-19 18:02:32 +00:00
|
|
|
/// verifyLoop - Verify loop structure
|
2012-06-20 03:42:09 +00:00
|
|
|
void verifyLoop() const;
|
2007-11-14 02:33:58 +00:00
|
|
|
|
2009-09-08 15:45:00 +00:00
|
|
|
/// verifyLoop - Verify loop structure of this loop and all nested loops.
|
2012-06-20 03:42:09 +00:00
|
|
|
void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
|
2007-07-19 18:02:32 +00:00
|
|
|
|
2012-06-20 03:42:09 +00:00
|
|
|
void print(raw_ostream &OS, unsigned Depth = 0) const;
|
2010-01-05 21:08:02 +00:00
|
|
|
|
2009-07-13 21:51:15 +00:00
|
|
|
protected:
|
|
|
|
friend class LoopInfoBase<BlockT, LoopT>;
|
2008-07-07 18:00:37 +00:00
|
|
|
explicit LoopBase(BlockT *BB) : ParentLoop(0) {
|
2004-04-19 03:03:19 +00:00
|
|
|
Blocks.push_back(BB);
|
2001-11-26 18:41:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-20 21:46:58 +00:00
|
|
|
template<class BlockT, class LoopT>
|
|
|
|
raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
|
|
|
|
Loop.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2012-06-20 20:17:20 +00:00
|
|
|
// Implementation in LoopInfoImpl.h
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__extension__ extern template class LoopBase<BasicBlock, Loop>;
|
|
|
|
#endif
|
|
|
|
|
2009-07-13 21:51:15 +00:00
|
|
|
class Loop : public LoopBase<BasicBlock, Loop> {
|
|
|
|
public:
|
|
|
|
Loop() {}
|
2009-07-13 22:02:44 +00:00
|
|
|
|
|
|
|
/// isLoopInvariant - Return true if the specified value is loop invariant
|
|
|
|
///
|
|
|
|
bool isLoopInvariant(Value *V) const;
|
|
|
|
|
2010-09-06 01:05:37 +00:00
|
|
|
/// hasLoopInvariantOperands - Return true if all the operands of the
|
2011-08-03 23:43:49 +00:00
|
|
|
/// specified instruction are loop invariant.
|
2010-09-06 01:05:37 +00:00
|
|
|
bool hasLoopInvariantOperands(Instruction *I) const;
|
2009-07-14 01:06:29 +00:00
|
|
|
|
2009-07-15 17:38:23 +00:00
|
|
|
/// makeLoopInvariant - If the given value is an instruction inside of the
|
2009-07-14 01:06:29 +00:00
|
|
|
/// loop and it can be hoisted, do so to make it trivially loop-invariant.
|
|
|
|
/// Return true if the value after any hoisting is loop invariant. This
|
|
|
|
/// function can be used as a slightly more aggressive replacement for
|
|
|
|
/// isLoopInvariant.
|
|
|
|
///
|
|
|
|
/// If InsertPt is specified, it is the point to hoist instructions to.
|
|
|
|
/// If null, the terminator of the loop preheader is used.
|
|
|
|
///
|
2009-07-15 01:25:43 +00:00
|
|
|
bool makeLoopInvariant(Value *V, bool &Changed,
|
|
|
|
Instruction *InsertPt = 0) const;
|
2009-07-14 01:06:29 +00:00
|
|
|
|
|
|
|
/// makeLoopInvariant - If the given instruction is inside of the
|
|
|
|
/// loop and it can be hoisted, do so to make it trivially loop-invariant.
|
|
|
|
/// Return true if the instruction after any hoisting is loop invariant. This
|
|
|
|
/// function can be used as a slightly more aggressive replacement for
|
|
|
|
/// isLoopInvariant.
|
|
|
|
///
|
|
|
|
/// If InsertPt is specified, it is the point to hoist instructions to.
|
|
|
|
/// If null, the terminator of the loop preheader is used.
|
|
|
|
///
|
2009-07-15 01:25:43 +00:00
|
|
|
bool makeLoopInvariant(Instruction *I, bool &Changed,
|
|
|
|
Instruction *InsertPt = 0) const;
|
2009-07-14 01:06:29 +00:00
|
|
|
|
2009-07-13 22:02:44 +00:00
|
|
|
/// getCanonicalInductionVariable - Check to see if the loop has a canonical
|
|
|
|
/// induction variable: an integer recurrence that starts at 0 and increments
|
|
|
|
/// by one each time through the loop. If so, return the phi node that
|
|
|
|
/// corresponds to it.
|
|
|
|
///
|
|
|
|
/// The IndVarSimplify pass transforms loops to have a canonical induction
|
|
|
|
/// variable.
|
|
|
|
///
|
|
|
|
PHINode *getCanonicalInductionVariable() const;
|
|
|
|
|
|
|
|
/// isLCSSAForm - Return true if the Loop is in LCSSA form
|
2010-03-10 19:38:49 +00:00
|
|
|
bool isLCSSAForm(DominatorTree &DT) const;
|
2009-07-13 22:02:44 +00:00
|
|
|
|
2009-07-16 16:16:23 +00:00
|
|
|
/// isLoopSimplifyForm - Return true if the Loop is in the form that
|
|
|
|
/// the LoopSimplify form transforms loops to, which is sometimes called
|
|
|
|
/// normal form.
|
|
|
|
bool isLoopSimplifyForm() const;
|
|
|
|
|
2012-04-10 05:14:42 +00:00
|
|
|
/// isSafeToClone - Return true if the loop body is safe to clone in practice.
|
|
|
|
bool isSafeToClone() const;
|
|
|
|
|
2013-02-13 18:08:57 +00:00
|
|
|
/// Returns true if the loop is annotated parallel.
|
|
|
|
///
|
|
|
|
/// A parallel loop can be assumed to not contain any dependencies between
|
|
|
|
/// iterations by the compiler. That is, any loop-carried dependency checking
|
|
|
|
/// can be skipped completely when parallelizing the loop on the target
|
|
|
|
/// machine. Thus, if the parallel loop information originates from the
|
|
|
|
/// programmer, e.g. via the OpenMP parallel for pragma, it is the
|
|
|
|
/// programmer's responsibility to ensure there are no loop-carried
|
|
|
|
/// dependencies. The final execution order of the instructions across
|
|
|
|
/// iterations is not guaranteed, thus, the end result might or might not
|
|
|
|
/// implement actual concurrent execution of instructions across multiple
|
|
|
|
/// iterations.
|
|
|
|
bool isAnnotatedParallel() const;
|
|
|
|
|
2013-05-28 20:00:34 +00:00
|
|
|
/// Return the llvm.loop loop id metadata node for this loop if it is present.
|
|
|
|
///
|
|
|
|
/// If this loop contains the same llvm.loop metadata on each branch to the
|
|
|
|
/// header then the node is returned. If any latch instruction does not
|
|
|
|
/// contain llvm.loop or or if multiple latches contain different nodes then
|
|
|
|
/// 0 is returned.
|
|
|
|
MDNode *getLoopID() const;
|
|
|
|
/// Set the llvm.loop loop id metadata for this loop.
|
|
|
|
///
|
|
|
|
/// The LoopID metadata node will be added to each terminator instruction in
|
|
|
|
/// the loop that branches to the loop header.
|
|
|
|
///
|
|
|
|
/// The LoopID metadata node should have one or more operands and the first
|
|
|
|
/// operand should should be the node itself.
|
|
|
|
void setLoopID(MDNode *LoopID) const;
|
|
|
|
|
2009-11-05 19:21:41 +00:00
|
|
|
/// hasDedicatedExits - Return true if no exit block for the loop
|
|
|
|
/// has a predecessor that is outside the loop.
|
|
|
|
bool hasDedicatedExits() const;
|
|
|
|
|
2011-08-03 23:43:49 +00:00
|
|
|
/// getUniqueExitBlocks - Return all unique successor blocks of this loop.
|
2009-09-03 16:10:48 +00:00
|
|
|
/// These are the blocks _outside of the current loop_ which are branched to.
|
2009-12-11 20:05:23 +00:00
|
|
|
/// This assumes that loop exits are in canonical form.
|
2009-09-03 16:10:48 +00:00
|
|
|
///
|
|
|
|
void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
|
|
|
|
|
|
|
|
/// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
|
|
|
|
/// block, return that block. Otherwise return null.
|
|
|
|
BasicBlock *getUniqueExitBlock() const;
|
|
|
|
|
2010-01-05 21:08:02 +00:00
|
|
|
void dump() const;
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2009-07-13 21:51:15 +00:00
|
|
|
private:
|
|
|
|
friend class LoopInfoBase<BasicBlock, Loop>;
|
|
|
|
explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
|
|
|
|
};
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-26 05:32:43 +00:00
|
|
|
/// LoopInfo - This class builds and contains all of the top level loop
|
|
|
|
/// structures in the specified function.
|
|
|
|
///
|
2007-11-15 05:00:15 +00:00
|
|
|
|
2009-07-13 21:51:15 +00:00
|
|
|
template<class BlockT, class LoopT>
|
2007-11-15 05:00:15 +00:00
|
|
|
class LoopInfoBase {
|
2001-11-26 18:41:20 +00:00
|
|
|
// BBMap - Mapping of basic blocks to the inner most loop they occur in
|
2010-11-30 18:51:19 +00:00
|
|
|
DenseMap<BlockT *, LoopT *> BBMap;
|
2009-07-13 21:51:15 +00:00
|
|
|
std::vector<LoopT *> TopLevelLoops;
|
|
|
|
friend class LoopBase<BlockT, LoopT>;
|
2011-08-26 03:06:34 +00:00
|
|
|
friend class LoopInfo;
|
2009-06-27 21:22:48 +00:00
|
|
|
|
2012-09-17 06:43:55 +00:00
|
|
|
void operator=(const LoopInfoBase &) LLVM_DELETED_FUNCTION;
|
|
|
|
LoopInfoBase(const LoopInfo &) LLVM_DELETED_FUNCTION;
|
2001-11-26 18:41:20 +00:00
|
|
|
public:
|
2007-11-15 05:00:15 +00:00
|
|
|
LoopInfoBase() { }
|
|
|
|
~LoopInfoBase() { releaseMemory(); }
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2007-11-15 05:00:15 +00:00
|
|
|
void releaseMemory() {
|
2009-07-13 21:51:15 +00:00
|
|
|
for (typename std::vector<LoopT *>::iterator I =
|
2007-11-15 05:00:15 +00:00
|
|
|
TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
|
|
|
|
delete *I; // Delete all of the loops...
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2008-01-11 06:27:42 +00:00
|
|
|
BBMap.clear(); // Reset internal state of analysis
|
2007-11-15 05:00:15 +00:00
|
|
|
TopLevelLoops.clear();
|
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2004-01-08 00:09:44 +00:00
|
|
|
/// iterator/begin/end - The interface to the top-level loops in the current
|
|
|
|
/// function.
|
|
|
|
///
|
2009-07-13 21:51:15 +00:00
|
|
|
typedef typename std::vector<LoopT *>::const_iterator iterator;
|
2012-06-26 04:11:38 +00:00
|
|
|
typedef typename std::vector<LoopT *>::const_reverse_iterator
|
|
|
|
reverse_iterator;
|
2004-01-08 00:09:44 +00:00
|
|
|
iterator begin() const { return TopLevelLoops.begin(); }
|
|
|
|
iterator end() const { return TopLevelLoops.end(); }
|
2012-06-26 04:11:38 +00:00
|
|
|
reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
|
|
|
|
reverse_iterator rend() const { return TopLevelLoops.rend(); }
|
2008-08-14 18:13:49 +00:00
|
|
|
bool empty() const { return TopLevelLoops.empty(); }
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2002-09-26 05:32:43 +00:00
|
|
|
/// getLoopFor - Return the inner most loop that BB lives in. If a basic
|
|
|
|
/// block is in no loop (for example the entry node), null is returned.
|
|
|
|
///
|
2009-07-13 21:51:15 +00:00
|
|
|
LoopT *getLoopFor(const BlockT *BB) const {
|
2012-01-04 21:41:24 +00:00
|
|
|
return BBMap.lookup(const_cast<BlockT*>(BB));
|
2001-11-26 18:41:20 +00:00
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2002-09-26 05:32:43 +00:00
|
|
|
/// operator[] - same as getLoopFor...
|
|
|
|
///
|
2009-07-13 21:51:15 +00:00
|
|
|
const LoopT *operator[](const BlockT *BB) const {
|
2001-11-26 18:41:20 +00:00
|
|
|
return getLoopFor(BB);
|
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2008-01-29 12:12:40 +00:00
|
|
|
/// getLoopDepth - Return the loop nesting level of the specified block. A
|
|
|
|
/// depth of 0 means the block is not inside any loop.
|
2002-09-26 05:32:43 +00:00
|
|
|
///
|
2007-11-15 05:00:15 +00:00
|
|
|
unsigned getLoopDepth(const BlockT *BB) const {
|
2009-07-13 21:51:15 +00:00
|
|
|
const LoopT *L = getLoopFor(BB);
|
2001-11-26 18:41:20 +00:00
|
|
|
return L ? L->getLoopDepth() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// isLoopHeader - True if the block is a loop header node
|
2007-11-15 05:00:15 +00:00
|
|
|
bool isLoopHeader(BlockT *BB) const {
|
2009-07-13 21:51:15 +00:00
|
|
|
const LoopT *L = getLoopFor(BB);
|
2005-06-29 17:41:25 +00:00
|
|
|
return L && L->getHeader() == BB;
|
2001-11-26 18:41:20 +00:00
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2007-11-15 05:00:15 +00:00
|
|
|
/// removeLoop - This removes the specified top-level loop from this loop info
|
|
|
|
/// object. The loop is not deleted, as it will presumably be inserted into
|
|
|
|
/// another loop.
|
2009-07-13 21:51:15 +00:00
|
|
|
LoopT *removeLoop(iterator I) {
|
2007-11-15 05:00:15 +00:00
|
|
|
assert(I != end() && "Cannot remove end iterator!");
|
2009-07-13 21:51:15 +00:00
|
|
|
LoopT *L = *I;
|
2007-11-15 05:00:15 +00:00
|
|
|
assert(L->getParentLoop() == 0 && "Not a top-level loop!");
|
|
|
|
TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
|
|
|
|
return L;
|
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2007-11-15 05:00:15 +00:00
|
|
|
/// changeLoopFor - Change the top-level loop that contains BB to the
|
|
|
|
/// specified loop. This should be used by transformations that restructure
|
|
|
|
/// the loop hierarchy tree.
|
2009-07-13 21:51:15 +00:00
|
|
|
void changeLoopFor(BlockT *BB, LoopT *L) {
|
2011-08-10 23:22:57 +00:00
|
|
|
if (!L) {
|
2012-01-04 21:41:24 +00:00
|
|
|
BBMap.erase(BB);
|
2011-08-10 23:22:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
BBMap[BB] = L;
|
2007-11-15 05:00:15 +00:00
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2007-11-15 05:00:15 +00:00
|
|
|
/// changeTopLevelLoop - Replace the specified loop in the top-level loops
|
|
|
|
/// list with the indicated loop.
|
2009-07-13 21:51:15 +00:00
|
|
|
void changeTopLevelLoop(LoopT *OldLoop,
|
|
|
|
LoopT *NewLoop) {
|
|
|
|
typename std::vector<LoopT *>::iterator I =
|
2007-11-15 05:00:15 +00:00
|
|
|
std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
|
|
|
|
assert(I != TopLevelLoops.end() && "Old loop not at top level!");
|
|
|
|
*I = NewLoop;
|
|
|
|
assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
|
|
|
|
"Loops already embedded into a subloop!");
|
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2007-11-15 05:00:15 +00:00
|
|
|
/// addTopLevelLoop - This adds the specified loop to the collection of
|
|
|
|
/// top-level loops.
|
2009-07-13 21:51:15 +00:00
|
|
|
void addTopLevelLoop(LoopT *New) {
|
2007-11-15 05:00:15 +00:00
|
|
|
assert(New->getParentLoop() == 0 && "Loop already in subloop!");
|
|
|
|
TopLevelLoops.push_back(New);
|
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2007-11-15 05:00:15 +00:00
|
|
|
/// removeBlock - This method completely removes BB from all data structures,
|
|
|
|
/// including all of the Loop objects it is nested in and our mapping from
|
|
|
|
/// BasicBlocks to loops.
|
|
|
|
void removeBlock(BlockT *BB) {
|
2010-11-30 18:51:19 +00:00
|
|
|
typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
|
2007-11-15 05:00:15 +00:00
|
|
|
if (I != BBMap.end()) {
|
2009-07-13 21:51:15 +00:00
|
|
|
for (LoopT *L = I->second; L; L = L->getParentLoop())
|
2007-11-15 05:00:15 +00:00
|
|
|
L->removeBlockFromLoop(BB);
|
|
|
|
|
|
|
|
BBMap.erase(I);
|
|
|
|
}
|
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2007-11-15 05:00:15 +00:00
|
|
|
// Internals
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2009-07-13 21:51:15 +00:00
|
|
|
static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
|
|
|
|
const LoopT *ParentLoop) {
|
2007-11-15 05:00:15 +00:00
|
|
|
if (SubLoop == 0) return true;
|
|
|
|
if (SubLoop == ParentLoop) return false;
|
|
|
|
return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
|
|
|
|
}
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2012-06-20 05:23:33 +00:00
|
|
|
/// Create the loop forest using a stable algorithm.
|
|
|
|
void Analyze(DominatorTreeBase<BlockT> &DomTree);
|
|
|
|
|
2007-11-15 05:00:15 +00:00
|
|
|
// Debugging
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2012-06-20 03:42:09 +00:00
|
|
|
void print(raw_ostream &OS) const;
|
2007-11-15 05:00:15 +00:00
|
|
|
};
|
|
|
|
|
2012-06-20 20:17:20 +00:00
|
|
|
// Implementation in LoopInfoImpl.h
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
|
|
|
|
#endif
|
|
|
|
|
2007-11-15 05:00:15 +00:00
|
|
|
class LoopInfo : public FunctionPass {
|
2009-07-13 21:51:15 +00:00
|
|
|
LoopInfoBase<BasicBlock, Loop> LI;
|
|
|
|
friend class LoopBase<BasicBlock, Loop>;
|
2009-06-27 21:22:48 +00:00
|
|
|
|
2012-09-17 06:43:55 +00:00
|
|
|
void operator=(const LoopInfo &) LLVM_DELETED_FUNCTION;
|
|
|
|
LoopInfo(const LoopInfo &) LLVM_DELETED_FUNCTION;
|
2007-11-15 05:00:15 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
2010-10-19 17:21:58 +00:00
|
|
|
LoopInfo() : FunctionPass(ID) {
|
|
|
|
initializeLoopInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-11-15 05:00:15 +00:00
|
|
|
|
2009-07-13 21:51:15 +00:00
|
|
|
LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
|
2007-11-27 03:43:35 +00:00
|
|
|
|
2007-11-15 05:00:15 +00:00
|
|
|
/// iterator/begin/end - The interface to the top-level loops in the current
|
|
|
|
/// function.
|
|
|
|
///
|
2009-07-13 21:51:15 +00:00
|
|
|
typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
|
2012-06-26 04:11:38 +00:00
|
|
|
typedef LoopInfoBase<BasicBlock, Loop>::reverse_iterator reverse_iterator;
|
2009-06-27 21:22:48 +00:00
|
|
|
inline iterator begin() const { return LI.begin(); }
|
|
|
|
inline iterator end() const { return LI.end(); }
|
2012-06-26 04:11:38 +00:00
|
|
|
inline reverse_iterator rbegin() const { return LI.rbegin(); }
|
|
|
|
inline reverse_iterator rend() const { return LI.rend(); }
|
2009-06-27 21:22:48 +00:00
|
|
|
bool empty() const { return LI.empty(); }
|
2007-11-15 05:00:15 +00:00
|
|
|
|
|
|
|
/// getLoopFor - Return the inner most loop that BB lives in. If a basic
|
|
|
|
/// block is in no loop (for example the entry node), null is returned.
|
|
|
|
///
|
|
|
|
inline Loop *getLoopFor(const BasicBlock *BB) const {
|
2009-06-27 21:22:48 +00:00
|
|
|
return LI.getLoopFor(BB);
|
2007-11-15 05:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// operator[] - same as getLoopFor...
|
|
|
|
///
|
|
|
|
inline const Loop *operator[](const BasicBlock *BB) const {
|
2009-06-27 21:22:48 +00:00
|
|
|
return LI.getLoopFor(BB);
|
2007-11-15 05:00:15 +00:00
|
|
|
}
|
|
|
|
|
2008-01-29 12:12:40 +00:00
|
|
|
/// getLoopDepth - Return the loop nesting level of the specified block. A
|
|
|
|
/// depth of 0 means the block is not inside any loop.
|
2007-11-15 05:00:15 +00:00
|
|
|
///
|
|
|
|
inline unsigned getLoopDepth(const BasicBlock *BB) const {
|
2009-06-27 21:22:48 +00:00
|
|
|
return LI.getLoopDepth(BB);
|
2007-11-15 05:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isLoopHeader - True if the block is a loop header node
|
|
|
|
inline bool isLoopHeader(BasicBlock *BB) const {
|
2009-06-27 21:22:48 +00:00
|
|
|
return LI.isLoopHeader(BB);
|
2007-11-15 05:00:15 +00:00
|
|
|
}
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2002-09-26 05:32:43 +00:00
|
|
|
/// runOnFunction - Calculate the natural loop information.
|
|
|
|
///
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-01-30 23:27:55 +00:00
|
|
|
|
2009-09-08 15:45:00 +00:00
|
|
|
virtual void verifyAnalysis() const;
|
|
|
|
|
2009-06-27 21:22:48 +00:00
|
|
|
virtual void releaseMemory() { LI.releaseMemory(); }
|
2006-12-17 05:15:13 +00:00
|
|
|
|
2009-08-23 06:03:38 +00:00
|
|
|
virtual void print(raw_ostream &O, const Module* M = 0) const;
|
2011-08-03 23:43:49 +00:00
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
2004-04-18 05:37:42 +00:00
|
|
|
/// removeLoop - This removes the specified top-level loop from this loop info
|
|
|
|
/// object. The loop is not deleted, as it will presumably be inserted into
|
|
|
|
/// another loop.
|
2009-06-27 21:22:48 +00:00
|
|
|
inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
|
2004-04-18 05:37:42 +00:00
|
|
|
|
2004-04-12 20:26:17 +00:00
|
|
|
/// changeLoopFor - Change the top-level loop that contains BB to the
|
|
|
|
/// specified loop. This should be used by transformations that restructure
|
|
|
|
/// the loop hierarchy tree.
|
2007-11-15 05:00:15 +00:00
|
|
|
inline void changeLoopFor(BasicBlock *BB, Loop *L) {
|
2009-06-27 21:22:48 +00:00
|
|
|
LI.changeLoopFor(BB, L);
|
2007-11-15 05:00:15 +00:00
|
|
|
}
|
2004-04-12 20:26:17 +00:00
|
|
|
|
|
|
|
/// changeTopLevelLoop - Replace the specified loop in the top-level loops
|
|
|
|
/// list with the indicated loop.
|
2007-11-15 05:00:15 +00:00
|
|
|
inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
|
2009-06-27 21:22:48 +00:00
|
|
|
LI.changeTopLevelLoop(OldLoop, NewLoop);
|
2007-11-15 05:00:15 +00:00
|
|
|
}
|
2004-04-12 20:26:17 +00:00
|
|
|
|
2004-04-19 03:03:19 +00:00
|
|
|
/// addTopLevelLoop - This adds the specified loop to the collection of
|
|
|
|
/// top-level loops.
|
2007-11-15 05:00:15 +00:00
|
|
|
inline void addTopLevelLoop(Loop *New) {
|
2009-06-27 21:22:48 +00:00
|
|
|
LI.addTopLevelLoop(New);
|
2004-04-19 03:03:19 +00:00
|
|
|
}
|
|
|
|
|
2004-04-18 06:54:34 +00:00
|
|
|
/// removeBlock - This method completely removes BB from all data structures,
|
|
|
|
/// including all of the Loop objects it is nested in and our mapping from
|
|
|
|
/// BasicBlocks to loops.
|
2007-11-15 05:00:15 +00:00
|
|
|
void removeBlock(BasicBlock *BB) {
|
2009-06-27 21:22:48 +00:00
|
|
|
LI.removeBlock(BB);
|
2007-11-15 05:00:15 +00:00
|
|
|
}
|
2010-11-18 19:59:41 +00:00
|
|
|
|
2011-08-10 23:22:57 +00:00
|
|
|
/// updateUnloop - Update LoopInfo after removing the last backedge from a
|
|
|
|
/// loop--now the "unloop". This updates the loop forest and parent loops for
|
|
|
|
/// each block so that Unloop is no longer referenced, but the caller must
|
|
|
|
/// actually delete the Unloop object.
|
|
|
|
void updateUnloop(Loop *Unloop);
|
|
|
|
|
2010-11-18 19:59:41 +00:00
|
|
|
/// replacementPreservesLCSSAForm - Returns true if replacing From with To
|
|
|
|
/// everywhere is guaranteed to preserve LCSSA form.
|
|
|
|
bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
|
|
|
|
// Preserving LCSSA form is only problematic if the replacing value is an
|
|
|
|
// instruction.
|
|
|
|
Instruction *I = dyn_cast<Instruction>(To);
|
|
|
|
if (!I) return true;
|
2010-12-28 18:00:24 +00:00
|
|
|
// If both instructions are defined in the same basic block then replacement
|
|
|
|
// cannot break LCSSA form.
|
|
|
|
if (I->getParent() == From->getParent())
|
|
|
|
return true;
|
2010-11-18 19:59:41 +00:00
|
|
|
// If the instruction is not defined in a loop then it can safely replace
|
|
|
|
// anything.
|
|
|
|
Loop *ToLoop = getLoopFor(I->getParent());
|
|
|
|
if (!ToLoop) return true;
|
|
|
|
// If the replacing instruction is defined in the same loop as the original
|
|
|
|
// instruction, or in a loop that contains it as an inner loop, then using
|
|
|
|
// it as a replacement will not break LCSSA form.
|
2010-11-19 08:33:20 +00:00
|
|
|
return ToLoop->contains(getLoopFor(From->getParent()));
|
2010-11-18 19:59:41 +00:00
|
|
|
}
|
2001-11-26 18:41:20 +00:00
|
|
|
};
|
|
|
|
|
2002-08-26 16:45:19 +00:00
|
|
|
|
2003-02-28 03:05:15 +00:00
|
|
|
// Allow clients to walk the list of nested loops...
|
|
|
|
template <> struct GraphTraits<const Loop*> {
|
|
|
|
typedef const Loop NodeType;
|
2009-06-27 21:22:48 +00:00
|
|
|
typedef LoopInfo::iterator ChildIteratorType;
|
2003-02-28 03:05:15 +00:00
|
|
|
|
|
|
|
static NodeType *getEntryNode(const Loop *L) { return L; }
|
2005-04-21 20:19:05 +00:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2004-01-08 00:09:44 +00:00
|
|
|
return N->begin();
|
2003-02-28 03:05:15 +00:00
|
|
|
}
|
2005-04-21 20:19:05 +00:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2004-01-08 00:09:44 +00:00
|
|
|
return N->end();
|
2003-02-28 03:05:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct GraphTraits<Loop*> {
|
|
|
|
typedef Loop NodeType;
|
2009-06-27 21:22:48 +00:00
|
|
|
typedef LoopInfo::iterator ChildIteratorType;
|
2003-02-28 03:05:15 +00:00
|
|
|
|
|
|
|
static NodeType *getEntryNode(Loop *L) { return L; }
|
2005-04-21 20:19:05 +00:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2004-01-08 00:09:44 +00:00
|
|
|
return N->begin();
|
2003-02-28 03:05:15 +00:00
|
|
|
}
|
2005-04-21 20:19:05 +00:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2004-01-08 00:09:44 +00:00
|
|
|
return N->end();
|
2003-02-28 03:05:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-11-26 18:41:20 +00:00
|
|
|
#endif
|