2003-09-30 20:37:50 +02:00
|
|
|
//===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-26 19:41:20 +01:00
|
|
|
//
|
|
|
|
// This file defines the LoopInfo class that is used to identify natural loops
|
2009-09-26 17:09:53 +02: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 22:26:17 +02:00
|
|
|
// loops may actually be several loops that share the same header node.
|
2003-02-27 01:37:22 +01: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 05:03:19 +02:00
|
|
|
// contained entirely within the loop and the basic blocks the make up the loop.
|
2003-02-27 01:37:22 +01:00
|
|
|
//
|
2004-04-19 05:03:19 +02: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 19:41:20 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ANALYSIS_LOOP_INFO_H
|
|
|
|
#define LLVM_ANALYSIS_LOOP_INFO_H
|
|
|
|
|
2002-01-31 00:27:55 +01:00
|
|
|
#include "llvm/Pass.h"
|
2010-11-30 19:51:19 +01:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2011-08-26 05:06:34 +02:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2007-11-15 06:00:15 +01:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2007-08-21 02:31:24 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-07-17 11:21:53 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2007-11-15 06:00:15 +01:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2007-11-14 03:33:58 +01:00
|
|
|
#include "llvm/Support/CFG.h"
|
2009-08-23 09:19:13 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-11-14 03:33:58 +01:00
|
|
|
#include <algorithm>
|
2011-01-02 23:09:33 +01:00
|
|
|
#include <map>
|
2007-11-14 03:33:58 +01:00
|
|
|
|
2008-05-14 03:02:49 +02:00
|
|
|
namespace llvm {
|
|
|
|
|
2007-11-14 03:33:58 +01:00
|
|
|
template<typename T>
|
2012-06-20 10:39:33 +02:00
|
|
|
inline void RemoveFromVector(std::vector<T*> &V, T *N) {
|
2007-11-14 03:33:58 +01: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 19:41:20 +01:00
|
|
|
|
2007-06-08 02:17:13 +02:00
|
|
|
class DominatorTree;
|
2002-04-28 18:19:42 +02:00
|
|
|
class LoopInfo;
|
2009-07-13 23:51:15 +02:00
|
|
|
class Loop;
|
2011-01-02 23:09:33 +01:00
|
|
|
class PHINode;
|
2009-07-13 23:51:15 +02:00
|
|
|
template<class N, class M> class LoopInfoBase;
|
|
|
|
template<class N, class M> class LoopBase;
|
2001-11-26 19:41:20 +01:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-11 07:27:42 +01:00
|
|
|
/// LoopBase class - Instances of this class are used to represent loops that
|
|
|
|
/// are detected in the flow graph
|
2002-09-26 07:32:43 +02:00
|
|
|
///
|
2009-07-13 23:51:15 +02:00
|
|
|
template<class BlockT, class LoopT>
|
2007-11-14 03:33:58 +01:00
|
|
|
class LoopBase {
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *ParentLoop;
|
2008-01-11 07:27:42 +01:00
|
|
|
// SubLoops - Loops contained entirely within this one.
|
2009-07-13 23:51:15 +02:00
|
|
|
std::vector<LoopT *> SubLoops;
|
2008-01-11 07:27:42 +01:00
|
|
|
|
|
|
|
// Blocks - The list of blocks in this loop. First entry is the header node.
|
|
|
|
std::vector<BlockT*> Blocks;
|
2007-11-14 03:33:58 +01:00
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
// DO NOT IMPLEMENT
|
|
|
|
LoopBase(const LoopBase<BlockT, LoopT> &);
|
|
|
|
// DO NOT IMPLEMENT
|
|
|
|
const LoopBase<BlockT, LoopT>&operator=(const LoopBase<BlockT, LoopT> &);
|
2001-11-26 19:41:20 +01:00
|
|
|
public:
|
2004-04-12 22:26:17 +02:00
|
|
|
/// Loop ctor - This creates an empty loop.
|
2007-11-14 03:33:58 +01:00
|
|
|
LoopBase() : ParentLoop(0) {}
|
|
|
|
~LoopBase() {
|
2008-05-05 20:30:58 +02:00
|
|
|
for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
|
2004-04-18 07:37:42 +02:00
|
|
|
delete SubLoops[i];
|
|
|
|
}
|
2001-11-26 19:41:20 +01:00
|
|
|
|
2008-01-29 13:12:40 +01: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 05:03:19 +02:00
|
|
|
unsigned getLoopDepth() const {
|
2008-01-29 13:12:40 +01:00
|
|
|
unsigned D = 1;
|
2009-07-13 23:51:15 +02:00
|
|
|
for (const LoopT *CurLoop = ParentLoop; CurLoop;
|
2007-11-14 03:33:58 +01:00
|
|
|
CurLoop = CurLoop->ParentLoop)
|
2004-04-19 05:03:19 +02:00
|
|
|
++D;
|
|
|
|
return D;
|
|
|
|
}
|
2007-11-14 03:33:58 +01:00
|
|
|
BlockT *getHeader() const { return Blocks.front(); }
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *getParentLoop() const { return ParentLoop; }
|
2001-11-26 19:41:20 +01:00
|
|
|
|
2012-06-20 07:23:33 +02:00
|
|
|
/// setParentLoop is a raw interface for bypassing addChildLoop.
|
|
|
|
void setParentLoop(LoopT *L) { ParentLoop = L; }
|
|
|
|
|
2009-12-18 02:24:09 +01: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-04 01:43:49 +02:00
|
|
|
|
2009-12-18 02:24:09 +01:00
|
|
|
/// contains - Return true if the specified basic block is in this loop.
|
2004-01-30 18:22:50 +01:00
|
|
|
///
|
2007-11-14 03:33:58 +01:00
|
|
|
bool contains(const BlockT *BB) const {
|
2009-02-07 17:10:35 +01:00
|
|
|
return std::find(block_begin(), block_end(), BB) != block_end();
|
2007-11-14 03:33:58 +01:00
|
|
|
}
|
2001-11-26 19:41:20 +01:00
|
|
|
|
2009-12-18 02:24:09 +01: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 01:09:44 +01:00
|
|
|
/// iterator/begin/end - Return the loops contained entirely within this loop.
|
2002-09-26 07:32:43 +02:00
|
|
|
///
|
2009-07-13 23:51:15 +02:00
|
|
|
const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
|
2012-06-20 07:23:33 +02:00
|
|
|
std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
|
2009-07-13 23:51:15 +02:00
|
|
|
typedef typename std::vector<LoopT *>::const_iterator iterator;
|
2012-06-26 06:11:38 +02:00
|
|
|
typedef typename std::vector<LoopT *>::const_reverse_iterator
|
|
|
|
reverse_iterator;
|
2004-01-08 01:09:44 +01:00
|
|
|
iterator begin() const { return SubLoops.begin(); }
|
|
|
|
iterator end() const { return SubLoops.end(); }
|
2012-06-26 06:11:38 +02:00
|
|
|
reverse_iterator rbegin() const { return SubLoops.rbegin(); }
|
|
|
|
reverse_iterator rend() const { return SubLoops.rend(); }
|
2007-10-01 18:01:23 +02:00
|
|
|
bool empty() const { return SubLoops.empty(); }
|
2003-02-27 01:37:22 +01:00
|
|
|
|
|
|
|
/// getBlocks - Get a list of the basic blocks which make up this loop.
|
|
|
|
///
|
2007-11-14 03:33:58 +01:00
|
|
|
const std::vector<BlockT*> &getBlocks() const { return Blocks; }
|
2012-06-20 07:23:33 +02:00
|
|
|
std::vector<BlockT*> &getBlocksVector() { return Blocks; }
|
2007-11-14 03:33:58 +01:00
|
|
|
typedef typename std::vector<BlockT*>::const_iterator block_iterator;
|
2004-04-19 01:37:03 +02:00
|
|
|
block_iterator block_begin() const { return Blocks.begin(); }
|
|
|
|
block_iterator block_end() const { return Blocks.end(); }
|
2003-02-27 01:37:22 +01:00
|
|
|
|
2011-08-10 02:49:12 +02:00
|
|
|
/// getNumBlocks - Get the number of blocks in this loop in constant time.
|
2011-08-10 02:28:10 +02:00
|
|
|
unsigned getNumBlocks() const {
|
2011-08-10 02:49:12 +02:00
|
|
|
return Blocks.size();
|
2011-08-10 02:28:10 +02:00
|
|
|
}
|
|
|
|
|
2009-11-09 19:20:38 +01:00
|
|
|
/// isLoopExiting - True if terminator in the block can branch to another
|
|
|
|
/// block that is outside of the current loop.
|
2003-02-27 01:37:22 +01:00
|
|
|
///
|
2009-10-25 01:34:26 +02:00
|
|
|
bool isLoopExiting(const BlockT *BB) const {
|
2007-11-27 04:43:35 +01:00
|
|
|
typedef GraphTraits<BlockT*> BlockTraits;
|
|
|
|
for (typename BlockTraits::ChildIteratorType SI =
|
|
|
|
BlockTraits::child_begin(const_cast<BlockT*>(BB)),
|
|
|
|
SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) {
|
2007-11-14 03:33:58 +01:00
|
|
|
if (!contains(*SI))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2002-10-11 07:31:10 +02:00
|
|
|
|
2003-02-27 01:37:22 +01:00
|
|
|
/// getNumBackEdges - Calculate the number of back edges to the loop header
|
|
|
|
///
|
2007-11-14 03:33:58 +01:00
|
|
|
unsigned getNumBackEdges() const {
|
|
|
|
unsigned NumBackEdges = 0;
|
|
|
|
BlockT *H = getHeader();
|
|
|
|
|
2007-11-27 04:43:35 +01:00
|
|
|
typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
|
|
|
|
for (typename InvBlockTraits::ChildIteratorType I =
|
|
|
|
InvBlockTraits::child_begin(const_cast<BlockT*>(H)),
|
|
|
|
E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I)
|
2007-11-14 03:33:58 +01:00
|
|
|
if (contains(*I))
|
|
|
|
++NumBackEdges;
|
|
|
|
|
|
|
|
return NumBackEdges;
|
|
|
|
}
|
2002-09-25 21:10:06 +02:00
|
|
|
|
2004-04-15 17:15:40 +02: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 03:24:05 +02: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 05:42:09 +02:00
|
|
|
void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const;
|
2006-10-28 03:24:05 +02:00
|
|
|
|
2009-02-12 19:08:24 +01:00
|
|
|
/// getExitingBlock - If getExitingBlocks would return exactly one block,
|
|
|
|
/// return that block. Otherwise return null.
|
2012-06-20 05:42:09 +02:00
|
|
|
BlockT *getExitingBlock() const;
|
2009-02-12 19:08:24 +01:00
|
|
|
|
2004-04-19 00:14:10 +02: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 05:42:09 +02:00
|
|
|
void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
|
2004-04-19 00:14:10 +02:00
|
|
|
|
2009-05-08 22:07:23 +02:00
|
|
|
/// getExitBlock - If getExitBlocks would return exactly one block,
|
|
|
|
/// return that block. Otherwise return null.
|
2012-06-20 05:42:09 +02:00
|
|
|
BlockT *getExitBlock() const;
|
2009-05-08 22:07:23 +02:00
|
|
|
|
2010-07-17 09:34:01 +02:00
|
|
|
/// Edge type.
|
2012-06-20 05:42:09 +02:00
|
|
|
typedef std::pair<const BlockT*, const BlockT*> Edge;
|
2010-07-17 09:34:01 +02:00
|
|
|
|
2009-08-08 20:44:18 +02:00
|
|
|
/// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
|
2012-06-20 05:42:09 +02:00
|
|
|
void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
|
2009-08-08 20:44:18 +02:00
|
|
|
|
2002-09-26 07:32:43 +02: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 17:15:40 +02:00
|
|
|
/// header of the loop is the preheader node.
|
2002-09-26 07:32:43 +02:00
|
|
|
///
|
2004-04-15 17:15:40 +02:00
|
|
|
/// This method returns null if there is no preheader for the loop.
|
2002-09-26 07:32:43 +02:00
|
|
|
///
|
2012-06-20 05:42:09 +02:00
|
|
|
BlockT *getLoopPreheader() const;
|
2010-06-22 17:25:42 +02: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 05:42:09 +02:00
|
|
|
BlockT *getLoopPredecessor() const;
|
2002-09-26 07:32:43 +02:00
|
|
|
|
2009-02-18 17:43:19 +01: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 05:42:09 +02:00
|
|
|
BlockT *getLoopLatch() const;
|
2004-04-15 17:15:40 +02:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// APIs for updating loop information after changing the CFG
|
|
|
|
//
|
|
|
|
|
2003-02-27 01:37:22 +01: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 07:32:43 +02: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 23:51:15 +02:00
|
|
|
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
|
2002-09-26 07:32:43 +02:00
|
|
|
|
2004-04-12 22:26:17 +02: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 05:42:09 +02:00
|
|
|
void replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild);
|
2004-04-12 22:26:17 +02: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 23:51:15 +02:00
|
|
|
void addChildLoop(LoopT *NewChild) {
|
2007-11-14 03:33:58 +01:00
|
|
|
assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
|
2009-07-13 23:51:15 +02:00
|
|
|
NewChild->ParentLoop = static_cast<LoopT *>(this);
|
2007-11-14 03:33:58 +01:00
|
|
|
SubLoops.push_back(NewChild);
|
|
|
|
}
|
2004-04-12 22:26:17 +02: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 23:51:15 +02:00
|
|
|
LoopT *removeChildLoop(iterator I) {
|
2007-11-14 03:33:58 +01:00
|
|
|
assert(I != SubLoops.end() && "Cannot remove end iterator!");
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *Child = *I;
|
2007-11-14 03:33:58 +01: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 22:26:17 +02: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 03:33:58 +01:00
|
|
|
void addBlockEntry(BlockT *BB) {
|
2004-04-12 22:26:17 +02:00
|
|
|
Blocks.push_back(BB);
|
|
|
|
}
|
|
|
|
|
2005-03-06 21:55:34 +01: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 03:33:58 +01:00
|
|
|
void moveToHeader(BlockT *BB) {
|
2005-03-06 21:55:34 +01: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 22:26:17 +02:00
|
|
|
/// removeBlockFromLoop - This removes the specified basic block from the
|
2004-04-19 00:14:10 +02:00
|
|
|
/// current loop, updating the Blocks as appropriate. This does not update
|
|
|
|
/// the mapping in the LoopInfo class.
|
2007-11-14 03:33:58 +01:00
|
|
|
void removeBlockFromLoop(BlockT *BB) {
|
|
|
|
RemoveFromVector(Blocks, BB);
|
|
|
|
}
|
2004-04-12 22:26:17 +02:00
|
|
|
|
2007-07-19 20:02:32 +02:00
|
|
|
/// verifyLoop - Verify loop structure
|
2012-06-20 05:42:09 +02:00
|
|
|
void verifyLoop() const;
|
2007-11-14 03:33:58 +01:00
|
|
|
|
2009-09-08 17:45:00 +02:00
|
|
|
/// verifyLoop - Verify loop structure of this loop and all nested loops.
|
2012-06-20 05:42:09 +02:00
|
|
|
void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
|
2007-07-19 20:02:32 +02:00
|
|
|
|
2012-06-20 05:42:09 +02:00
|
|
|
void print(raw_ostream &OS, unsigned Depth = 0) const;
|
2010-01-05 22:08:02 +01:00
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
protected:
|
|
|
|
friend class LoopInfoBase<BlockT, LoopT>;
|
2008-07-07 20:00:37 +02:00
|
|
|
explicit LoopBase(BlockT *BB) : ParentLoop(0) {
|
2004-04-19 05:03:19 +02:00
|
|
|
Blocks.push_back(BB);
|
2001-11-26 19:41:20 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-20 23:46:58 +02: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 22:17:20 +02:00
|
|
|
// Implementation in LoopInfoImpl.h
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__extension__ extern template class LoopBase<BasicBlock, Loop>;
|
|
|
|
#endif
|
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
class Loop : public LoopBase<BasicBlock, Loop> {
|
|
|
|
public:
|
|
|
|
Loop() {}
|
2009-07-14 00:02:44 +02:00
|
|
|
|
|
|
|
/// isLoopInvariant - Return true if the specified value is loop invariant
|
|
|
|
///
|
|
|
|
bool isLoopInvariant(Value *V) const;
|
|
|
|
|
2010-09-06 03:05:37 +02:00
|
|
|
/// hasLoopInvariantOperands - Return true if all the operands of the
|
2011-08-04 01:43:49 +02:00
|
|
|
/// specified instruction are loop invariant.
|
2010-09-06 03:05:37 +02:00
|
|
|
bool hasLoopInvariantOperands(Instruction *I) const;
|
2009-07-14 03:06:29 +02:00
|
|
|
|
2009-07-15 19:38:23 +02:00
|
|
|
/// makeLoopInvariant - If the given value is an instruction inside of the
|
2009-07-14 03:06:29 +02: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 03:25:43 +02:00
|
|
|
bool makeLoopInvariant(Value *V, bool &Changed,
|
|
|
|
Instruction *InsertPt = 0) const;
|
2009-07-14 03:06:29 +02: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 03:25:43 +02:00
|
|
|
bool makeLoopInvariant(Instruction *I, bool &Changed,
|
|
|
|
Instruction *InsertPt = 0) const;
|
2009-07-14 03:06:29 +02:00
|
|
|
|
2009-07-14 00:02:44 +02: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 20:38:49 +01:00
|
|
|
bool isLCSSAForm(DominatorTree &DT) const;
|
2009-07-14 00:02:44 +02:00
|
|
|
|
2009-07-16 18:16:23 +02: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 07:14:42 +02:00
|
|
|
/// isSafeToClone - Return true if the loop body is safe to clone in practice.
|
|
|
|
bool isSafeToClone() const;
|
|
|
|
|
2009-11-05 20:21:41 +01:00
|
|
|
/// hasDedicatedExits - Return true if no exit block for the loop
|
|
|
|
/// has a predecessor that is outside the loop.
|
|
|
|
bool hasDedicatedExits() const;
|
|
|
|
|
2011-08-04 01:43:49 +02:00
|
|
|
/// getUniqueExitBlocks - Return all unique successor blocks of this loop.
|
2009-09-03 18:10:48 +02:00
|
|
|
/// These are the blocks _outside of the current loop_ which are branched to.
|
2009-12-11 21:05:23 +01:00
|
|
|
/// This assumes that loop exits are in canonical form.
|
2009-09-03 18:10:48 +02: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 22:08:02 +01:00
|
|
|
void dump() const;
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
private:
|
|
|
|
friend class LoopInfoBase<BasicBlock, Loop>;
|
|
|
|
explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
|
|
|
|
};
|
2001-11-26 19:41:20 +01:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-26 07:32:43 +02:00
|
|
|
/// LoopInfo - This class builds and contains all of the top level loop
|
|
|
|
/// structures in the specified function.
|
|
|
|
///
|
2007-11-15 06:00:15 +01:00
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
template<class BlockT, class LoopT>
|
2007-11-15 06:00:15 +01:00
|
|
|
class LoopInfoBase {
|
2001-11-26 19:41:20 +01:00
|
|
|
// BBMap - Mapping of basic blocks to the inner most loop they occur in
|
2010-11-30 19:51:19 +01:00
|
|
|
DenseMap<BlockT *, LoopT *> BBMap;
|
2009-07-13 23:51:15 +02:00
|
|
|
std::vector<LoopT *> TopLevelLoops;
|
|
|
|
friend class LoopBase<BlockT, LoopT>;
|
2011-08-26 05:06:34 +02:00
|
|
|
friend class LoopInfo;
|
2009-06-27 23:22:48 +02:00
|
|
|
|
|
|
|
void operator=(const LoopInfoBase &); // do not implement
|
|
|
|
LoopInfoBase(const LoopInfo &); // do not implement
|
2001-11-26 19:41:20 +01:00
|
|
|
public:
|
2007-11-15 06:00:15 +01:00
|
|
|
LoopInfoBase() { }
|
|
|
|
~LoopInfoBase() { releaseMemory(); }
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2007-11-15 06:00:15 +01:00
|
|
|
void releaseMemory() {
|
2009-07-13 23:51:15 +02:00
|
|
|
for (typename std::vector<LoopT *>::iterator I =
|
2007-11-15 06:00:15 +01:00
|
|
|
TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
|
|
|
|
delete *I; // Delete all of the loops...
|
2001-11-26 19:41:20 +01:00
|
|
|
|
2008-01-11 07:27:42 +01:00
|
|
|
BBMap.clear(); // Reset internal state of analysis
|
2007-11-15 06:00:15 +01:00
|
|
|
TopLevelLoops.clear();
|
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2004-01-08 01:09:44 +01:00
|
|
|
/// iterator/begin/end - The interface to the top-level loops in the current
|
|
|
|
/// function.
|
|
|
|
///
|
2009-07-13 23:51:15 +02:00
|
|
|
typedef typename std::vector<LoopT *>::const_iterator iterator;
|
2012-06-26 06:11:38 +02:00
|
|
|
typedef typename std::vector<LoopT *>::const_reverse_iterator
|
|
|
|
reverse_iterator;
|
2004-01-08 01:09:44 +01:00
|
|
|
iterator begin() const { return TopLevelLoops.begin(); }
|
|
|
|
iterator end() const { return TopLevelLoops.end(); }
|
2012-06-26 06:11:38 +02:00
|
|
|
reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
|
|
|
|
reverse_iterator rend() const { return TopLevelLoops.rend(); }
|
2008-08-14 20:13:49 +02:00
|
|
|
bool empty() const { return TopLevelLoops.empty(); }
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2002-09-26 07:32:43 +02: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 23:51:15 +02:00
|
|
|
LoopT *getLoopFor(const BlockT *BB) const {
|
2012-01-04 22:41:24 +01:00
|
|
|
return BBMap.lookup(const_cast<BlockT*>(BB));
|
2001-11-26 19:41:20 +01:00
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2002-09-26 07:32:43 +02:00
|
|
|
/// operator[] - same as getLoopFor...
|
|
|
|
///
|
2009-07-13 23:51:15 +02:00
|
|
|
const LoopT *operator[](const BlockT *BB) const {
|
2001-11-26 19:41:20 +01:00
|
|
|
return getLoopFor(BB);
|
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2008-01-29 13:12:40 +01: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 07:32:43 +02:00
|
|
|
///
|
2007-11-15 06:00:15 +01:00
|
|
|
unsigned getLoopDepth(const BlockT *BB) const {
|
2009-07-13 23:51:15 +02:00
|
|
|
const LoopT *L = getLoopFor(BB);
|
2001-11-26 19:41:20 +01:00
|
|
|
return L ? L->getLoopDepth() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// isLoopHeader - True if the block is a loop header node
|
2007-11-15 06:00:15 +01:00
|
|
|
bool isLoopHeader(BlockT *BB) const {
|
2009-07-13 23:51:15 +02:00
|
|
|
const LoopT *L = getLoopFor(BB);
|
2005-06-29 19:41:25 +02:00
|
|
|
return L && L->getHeader() == BB;
|
2001-11-26 19:41:20 +01:00
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2007-11-15 06:00:15 +01: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 23:51:15 +02:00
|
|
|
LoopT *removeLoop(iterator I) {
|
2007-11-15 06:00:15 +01:00
|
|
|
assert(I != end() && "Cannot remove end iterator!");
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *L = *I;
|
2007-11-15 06:00:15 +01:00
|
|
|
assert(L->getParentLoop() == 0 && "Not a top-level loop!");
|
|
|
|
TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
|
|
|
|
return L;
|
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2007-11-15 06:00:15 +01: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 23:51:15 +02:00
|
|
|
void changeLoopFor(BlockT *BB, LoopT *L) {
|
2011-08-11 01:22:57 +02:00
|
|
|
if (!L) {
|
2012-01-04 22:41:24 +01:00
|
|
|
BBMap.erase(BB);
|
2011-08-11 01:22:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
BBMap[BB] = L;
|
2007-11-15 06:00:15 +01:00
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2007-11-15 06:00:15 +01:00
|
|
|
/// changeTopLevelLoop - Replace the specified loop in the top-level loops
|
|
|
|
/// list with the indicated loop.
|
2009-07-13 23:51:15 +02:00
|
|
|
void changeTopLevelLoop(LoopT *OldLoop,
|
|
|
|
LoopT *NewLoop) {
|
|
|
|
typename std::vector<LoopT *>::iterator I =
|
2007-11-15 06:00:15 +01: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-04 01:43:49 +02:00
|
|
|
|
2007-11-15 06:00:15 +01:00
|
|
|
/// addTopLevelLoop - This adds the specified loop to the collection of
|
|
|
|
/// top-level loops.
|
2009-07-13 23:51:15 +02:00
|
|
|
void addTopLevelLoop(LoopT *New) {
|
2007-11-15 06:00:15 +01:00
|
|
|
assert(New->getParentLoop() == 0 && "Loop already in subloop!");
|
|
|
|
TopLevelLoops.push_back(New);
|
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2007-11-15 06:00:15 +01: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 19:51:19 +01:00
|
|
|
typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
|
2007-11-15 06:00:15 +01:00
|
|
|
if (I != BBMap.end()) {
|
2009-07-13 23:51:15 +02:00
|
|
|
for (LoopT *L = I->second; L; L = L->getParentLoop())
|
2007-11-15 06:00:15 +01:00
|
|
|
L->removeBlockFromLoop(BB);
|
|
|
|
|
|
|
|
BBMap.erase(I);
|
|
|
|
}
|
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2007-11-15 06:00:15 +01:00
|
|
|
// Internals
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
|
|
|
|
const LoopT *ParentLoop) {
|
2007-11-15 06:00:15 +01:00
|
|
|
if (SubLoop == 0) return true;
|
|
|
|
if (SubLoop == ParentLoop) return false;
|
|
|
|
return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
|
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2012-06-20 07:23:33 +02:00
|
|
|
/// Create the loop forest using a stable algorithm.
|
|
|
|
void Analyze(DominatorTreeBase<BlockT> &DomTree);
|
|
|
|
|
2007-11-15 06:00:15 +01:00
|
|
|
// Debugging
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2012-06-20 05:42:09 +02:00
|
|
|
void print(raw_ostream &OS) const;
|
2007-11-15 06:00:15 +01:00
|
|
|
};
|
|
|
|
|
2012-06-20 22:17:20 +02:00
|
|
|
// Implementation in LoopInfoImpl.h
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__extension__ extern template class LoopInfoBase<BasicBlock, Loop>;
|
|
|
|
#endif
|
|
|
|
|
2007-11-15 06:00:15 +01:00
|
|
|
class LoopInfo : public FunctionPass {
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopInfoBase<BasicBlock, Loop> LI;
|
|
|
|
friend class LoopBase<BasicBlock, Loop>;
|
2009-06-27 23:22:48 +02:00
|
|
|
|
|
|
|
void operator=(const LoopInfo &); // do not implement
|
|
|
|
LoopInfo(const LoopInfo &); // do not implement
|
2007-11-15 06:00:15 +01:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
2010-10-19 19:21:58 +02:00
|
|
|
LoopInfo() : FunctionPass(ID) {
|
|
|
|
initializeLoopInfoPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-11-15 06:00:15 +01:00
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
|
2007-11-27 04:43:35 +01:00
|
|
|
|
2007-11-15 06:00:15 +01:00
|
|
|
/// iterator/begin/end - The interface to the top-level loops in the current
|
|
|
|
/// function.
|
|
|
|
///
|
2009-07-13 23:51:15 +02:00
|
|
|
typedef LoopInfoBase<BasicBlock, Loop>::iterator iterator;
|
2012-06-26 06:11:38 +02:00
|
|
|
typedef LoopInfoBase<BasicBlock, Loop>::reverse_iterator reverse_iterator;
|
2009-06-27 23:22:48 +02:00
|
|
|
inline iterator begin() const { return LI.begin(); }
|
|
|
|
inline iterator end() const { return LI.end(); }
|
2012-06-26 06:11:38 +02:00
|
|
|
inline reverse_iterator rbegin() const { return LI.rbegin(); }
|
|
|
|
inline reverse_iterator rend() const { return LI.rend(); }
|
2009-06-27 23:22:48 +02:00
|
|
|
bool empty() const { return LI.empty(); }
|
2007-11-15 06:00:15 +01: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 23:22:48 +02:00
|
|
|
return LI.getLoopFor(BB);
|
2007-11-15 06:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// operator[] - same as getLoopFor...
|
|
|
|
///
|
|
|
|
inline const Loop *operator[](const BasicBlock *BB) const {
|
2009-06-27 23:22:48 +02:00
|
|
|
return LI.getLoopFor(BB);
|
2007-11-15 06:00:15 +01:00
|
|
|
}
|
|
|
|
|
2008-01-29 13:12:40 +01: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 06:00:15 +01:00
|
|
|
///
|
|
|
|
inline unsigned getLoopDepth(const BasicBlock *BB) const {
|
2009-06-27 23:22:48 +02:00
|
|
|
return LI.getLoopDepth(BB);
|
2007-11-15 06:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// isLoopHeader - True if the block is a loop header node
|
|
|
|
inline bool isLoopHeader(BasicBlock *BB) const {
|
2009-06-27 23:22:48 +02:00
|
|
|
return LI.isLoopHeader(BB);
|
2007-11-15 06:00:15 +01:00
|
|
|
}
|
2001-11-26 19:41:20 +01:00
|
|
|
|
2002-09-26 07:32:43 +02:00
|
|
|
/// runOnFunction - Calculate the natural loop information.
|
|
|
|
///
|
2002-06-25 18:13:24 +02:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-01-31 00:27:55 +01:00
|
|
|
|
2009-09-08 17:45:00 +02:00
|
|
|
virtual void verifyAnalysis() const;
|
|
|
|
|
2009-06-27 23:22:48 +02:00
|
|
|
virtual void releaseMemory() { LI.releaseMemory(); }
|
2006-12-17 06:15:13 +01:00
|
|
|
|
2009-08-23 08:03:38 +02:00
|
|
|
virtual void print(raw_ostream &O, const Module* M = 0) const;
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
2004-04-18 07:37:42 +02: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 23:22:48 +02:00
|
|
|
inline Loop *removeLoop(iterator I) { return LI.removeLoop(I); }
|
2004-04-18 07:37:42 +02:00
|
|
|
|
2004-04-12 22:26:17 +02: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 06:00:15 +01:00
|
|
|
inline void changeLoopFor(BasicBlock *BB, Loop *L) {
|
2009-06-27 23:22:48 +02:00
|
|
|
LI.changeLoopFor(BB, L);
|
2007-11-15 06:00:15 +01:00
|
|
|
}
|
2004-04-12 22:26:17 +02:00
|
|
|
|
|
|
|
/// changeTopLevelLoop - Replace the specified loop in the top-level loops
|
|
|
|
/// list with the indicated loop.
|
2007-11-15 06:00:15 +01:00
|
|
|
inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
|
2009-06-27 23:22:48 +02:00
|
|
|
LI.changeTopLevelLoop(OldLoop, NewLoop);
|
2007-11-15 06:00:15 +01:00
|
|
|
}
|
2004-04-12 22:26:17 +02:00
|
|
|
|
2004-04-19 05:03:19 +02:00
|
|
|
/// addTopLevelLoop - This adds the specified loop to the collection of
|
|
|
|
/// top-level loops.
|
2007-11-15 06:00:15 +01:00
|
|
|
inline void addTopLevelLoop(Loop *New) {
|
2009-06-27 23:22:48 +02:00
|
|
|
LI.addTopLevelLoop(New);
|
2004-04-19 05:03:19 +02:00
|
|
|
}
|
|
|
|
|
2004-04-18 08:54:34 +02: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 06:00:15 +01:00
|
|
|
void removeBlock(BasicBlock *BB) {
|
2009-06-27 23:22:48 +02:00
|
|
|
LI.removeBlock(BB);
|
2007-11-15 06:00:15 +01:00
|
|
|
}
|
2010-11-18 20:59:41 +01:00
|
|
|
|
2011-08-11 01:22:57 +02: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 20:59:41 +01: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 19:00:24 +01: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 20:59:41 +01: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 09:33:20 +01:00
|
|
|
return ToLoop->contains(getLoopFor(From->getParent()));
|
2010-11-18 20:59:41 +01:00
|
|
|
}
|
2001-11-26 19:41:20 +01:00
|
|
|
};
|
|
|
|
|
2002-08-26 18:45:19 +02:00
|
|
|
|
2003-02-28 04:05:15 +01:00
|
|
|
// Allow clients to walk the list of nested loops...
|
|
|
|
template <> struct GraphTraits<const Loop*> {
|
|
|
|
typedef const Loop NodeType;
|
2009-06-27 23:22:48 +02:00
|
|
|
typedef LoopInfo::iterator ChildIteratorType;
|
2003-02-28 04:05:15 +01:00
|
|
|
|
|
|
|
static NodeType *getEntryNode(const Loop *L) { return L; }
|
2005-04-21 22:19:05 +02:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2004-01-08 01:09:44 +01:00
|
|
|
return N->begin();
|
2003-02-28 04:05:15 +01:00
|
|
|
}
|
2005-04-21 22:19:05 +02:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2004-01-08 01:09:44 +01:00
|
|
|
return N->end();
|
2003-02-28 04:05:15 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct GraphTraits<Loop*> {
|
|
|
|
typedef Loop NodeType;
|
2009-06-27 23:22:48 +02:00
|
|
|
typedef LoopInfo::iterator ChildIteratorType;
|
2003-02-28 04:05:15 +01:00
|
|
|
|
|
|
|
static NodeType *getEntryNode(Loop *L) { return L; }
|
2005-04-21 22:19:05 +02:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2004-01-08 01:09:44 +01:00
|
|
|
return N->begin();
|
2003-02-28 04:05:15 +01:00
|
|
|
}
|
2005-04-21 22:19:05 +02:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2004-01-08 01:09:44 +01:00
|
|
|
return N->end();
|
2003-02-28 04:05:15 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-11-26 19:41:20 +01:00
|
|
|
#endif
|