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>
|
|
|
|
static void RemoveFromVector(std::vector<T*> &V, T *N) {
|
|
|
|
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
|
|
|
|
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; }
|
|
|
|
typedef typename std::vector<LoopT *>::const_iterator iterator;
|
2004-01-08 01:09:44 +01:00
|
|
|
iterator begin() const { return SubLoops.begin(); }
|
|
|
|
iterator end() const { return SubLoops.end(); }
|
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; }
|
|
|
|
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.
|
|
|
|
///
|
2007-11-14 03:33:58 +01:00
|
|
|
void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
|
|
|
|
// Sort the blocks vector so that we can use binary search to do quick
|
|
|
|
// lookups.
|
|
|
|
SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
|
|
|
|
std::sort(LoopBBs.begin(), LoopBBs.end());
|
|
|
|
|
2007-11-27 04:43:35 +01:00
|
|
|
typedef GraphTraits<BlockT*> BlockTraits;
|
2009-02-07 17:10:35 +01:00
|
|
|
for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
|
2007-11-27 04:43:35 +01:00
|
|
|
for (typename BlockTraits::ChildIteratorType I =
|
|
|
|
BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
|
|
|
|
I != E; ++I)
|
2007-11-14 03:33:58 +01:00
|
|
|
if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
|
|
|
|
// Not in current loop? It must be an exit block.
|
|
|
|
ExitingBlocks.push_back(*BI);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
BlockT *getExitingBlock() const {
|
|
|
|
SmallVector<BlockT*, 8> ExitingBlocks;
|
|
|
|
getExitingBlocks(ExitingBlocks);
|
|
|
|
if (ExitingBlocks.size() == 1)
|
|
|
|
return ExitingBlocks[0];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
///
|
2007-11-14 03:33:58 +01:00
|
|
|
void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
|
|
|
|
// Sort the blocks vector so that we can use binary search to do quick
|
|
|
|
// lookups.
|
|
|
|
SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
|
|
|
|
std::sort(LoopBBs.begin(), LoopBBs.end());
|
|
|
|
|
2007-11-27 04:43:35 +01:00
|
|
|
typedef GraphTraits<BlockT*> BlockTraits;
|
2009-02-07 17:10:35 +01:00
|
|
|
for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
|
2007-11-27 04:43:35 +01:00
|
|
|
for (typename BlockTraits::ChildIteratorType I =
|
|
|
|
BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
|
|
|
|
I != E; ++I)
|
2007-11-14 03:33:58 +01:00
|
|
|
if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
|
|
|
|
// Not in current loop? It must be an exit block.
|
|
|
|
ExitBlocks.push_back(*I);
|
|
|
|
}
|
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.
|
|
|
|
BlockT *getExitBlock() const {
|
|
|
|
SmallVector<BlockT*, 8> ExitBlocks;
|
|
|
|
getExitBlocks(ExitBlocks);
|
|
|
|
if (ExitBlocks.size() == 1)
|
|
|
|
return ExitBlocks[0];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-17 09:34:01 +02:00
|
|
|
/// Edge type.
|
|
|
|
typedef std::pair<BlockT*, BlockT*> Edge;
|
|
|
|
|
2009-08-08 20:44:18 +02:00
|
|
|
/// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
|
2010-07-17 09:34:01 +02:00
|
|
|
template <typename EdgeT>
|
|
|
|
void getExitEdges(SmallVectorImpl<EdgeT> &ExitEdges) const {
|
2009-08-08 20:44:18 +02:00
|
|
|
// Sort the blocks vector so that we can use binary search to do quick
|
|
|
|
// lookups.
|
|
|
|
SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
|
2010-07-17 11:21:53 +02:00
|
|
|
array_pod_sort(LoopBBs.begin(), LoopBBs.end());
|
2009-08-08 20:44:18 +02:00
|
|
|
|
|
|
|
typedef GraphTraits<BlockT*> BlockTraits;
|
|
|
|
for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
|
|
|
|
for (typename BlockTraits::ChildIteratorType I =
|
|
|
|
BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
|
|
|
|
I != E; ++I)
|
|
|
|
if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
|
|
|
|
// Not in current loop? It must be an exit block.
|
2010-07-17 09:34:01 +02:00
|
|
|
ExitEdges.push_back(EdgeT(*BI, *I));
|
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
|
|
|
///
|
2007-11-14 03:33:58 +01:00
|
|
|
BlockT *getLoopPreheader() const {
|
2010-06-22 17:25:42 +02:00
|
|
|
// Keep track of nodes outside the loop branching to the header...
|
|
|
|
BlockT *Out = getLoopPredecessor();
|
|
|
|
if (!Out) return 0;
|
|
|
|
|
|
|
|
// Make sure there is only one exit out of the preheader.
|
|
|
|
typedef GraphTraits<BlockT*> BlockTraits;
|
|
|
|
typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
|
|
|
|
++SI;
|
|
|
|
if (SI != BlockTraits::child_end(Out))
|
|
|
|
return 0; // Multiple exits from the block, must not be a preheader.
|
|
|
|
|
|
|
|
// The predecessor has exactly one successor, so it is a preheader.
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
BlockT *getLoopPredecessor() const {
|
2007-11-14 03:33:58 +01:00
|
|
|
// Keep track of nodes outside the loop branching to the header...
|
|
|
|
BlockT *Out = 0;
|
|
|
|
|
|
|
|
// Loop over the predecessors of the header node...
|
|
|
|
BlockT *Header = getHeader();
|
2007-11-27 04:43:35 +01:00
|
|
|
typedef GraphTraits<BlockT*> BlockTraits;
|
|
|
|
typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
|
|
|
|
for (typename InvBlockTraits::ChildIteratorType PI =
|
|
|
|
InvBlockTraits::child_begin(Header),
|
2010-07-09 12:32:31 +02:00
|
|
|
PE = InvBlockTraits::child_end(Header); PI != PE; ++PI) {
|
|
|
|
typename InvBlockTraits::NodeType *N = *PI;
|
|
|
|
if (!contains(N)) { // If the block is not in the loop...
|
|
|
|
if (Out && Out != N)
|
2007-11-14 03:33:58 +01:00
|
|
|
return 0; // Multiple predecessors outside the loop
|
2010-07-09 12:32:31 +02:00
|
|
|
Out = N;
|
2007-11-14 03:33:58 +01:00
|
|
|
}
|
2010-07-09 12:32:31 +02:00
|
|
|
}
|
2007-11-14 03:33:58 +01:00
|
|
|
|
|
|
|
// Make sure there is only one exit out of the preheader.
|
|
|
|
assert(Out && "Header of loop has no predecessors from outside loop?");
|
|
|
|
return Out;
|
|
|
|
}
|
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.
|
2007-11-14 03:33:58 +01:00
|
|
|
BlockT *getLoopLatch() const {
|
|
|
|
BlockT *Header = getHeader();
|
2007-11-27 04:43:35 +01:00
|
|
|
typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
|
|
|
|
typename InvBlockTraits::ChildIteratorType PI =
|
|
|
|
InvBlockTraits::child_begin(Header);
|
|
|
|
typename InvBlockTraits::ChildIteratorType PE =
|
|
|
|
InvBlockTraits::child_end(Header);
|
2007-11-14 03:33:58 +01:00
|
|
|
BlockT *Latch = 0;
|
2010-07-09 12:42:13 +02:00
|
|
|
for (; PI != PE; ++PI) {
|
|
|
|
typename InvBlockTraits::NodeType *N = *PI;
|
|
|
|
if (contains(N)) {
|
2009-11-20 21:51:18 +01:00
|
|
|
if (Latch) return 0;
|
2010-07-09 12:42:13 +02:00
|
|
|
Latch = N;
|
2009-11-20 21:51:18 +01:00
|
|
|
}
|
2010-07-09 12:42:13 +02:00
|
|
|
}
|
2007-11-14 03:33:58 +01:00
|
|
|
|
|
|
|
return Latch;
|
|
|
|
}
|
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.
|
2009-07-13 23:51:15 +02:00
|
|
|
void replaceChildLoopWith(LoopT *OldChild,
|
|
|
|
LoopT *NewChild) {
|
2007-11-14 03:33:58 +01:00
|
|
|
assert(OldChild->ParentLoop == this && "This loop is already broken!");
|
|
|
|
assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
|
2009-07-13 23:51:15 +02:00
|
|
|
typename std::vector<LoopT *>::iterator I =
|
2007-11-14 03:33:58 +01:00
|
|
|
std::find(SubLoops.begin(), SubLoops.end(), OldChild);
|
|
|
|
assert(I != SubLoops.end() && "OldChild not in loop!");
|
|
|
|
*I = NewChild;
|
|
|
|
OldChild->ParentLoop = 0;
|
2009-07-13 23:51:15 +02:00
|
|
|
NewChild->ParentLoop = static_cast<LoopT *>(this);
|
2007-11-14 03:33:58 +01:00
|
|
|
}
|
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
|
2007-11-14 03:33:58 +01:00
|
|
|
void verifyLoop() const {
|
|
|
|
#ifndef NDEBUG
|
2009-09-08 17:45:00 +02:00
|
|
|
assert(!Blocks.empty() && "Loop header is missing");
|
|
|
|
|
2011-11-09 05:16:01 +01:00
|
|
|
// Setup for using a depth-first iterator to visit every block in the loop.
|
|
|
|
SmallVector<BlockT*, 8> ExitBBs;
|
|
|
|
getExitBlocks(ExitBBs);
|
|
|
|
llvm::SmallPtrSet<BlockT*, 8> VisitSet;
|
|
|
|
VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
|
|
|
|
df_ext_iterator<BlockT*, llvm::SmallPtrSet<BlockT*, 8> >
|
|
|
|
BI = df_ext_begin(getHeader(), VisitSet),
|
|
|
|
BE = df_ext_end(getHeader(), VisitSet);
|
|
|
|
|
|
|
|
// Keep track of the number of BBs visited.
|
|
|
|
unsigned NumVisited = 0;
|
|
|
|
|
2009-09-08 17:45:00 +02:00
|
|
|
// Sort the blocks vector so that we can use binary search to do quick
|
|
|
|
// lookups.
|
|
|
|
SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
|
|
|
|
std::sort(LoopBBs.begin(), LoopBBs.end());
|
|
|
|
|
|
|
|
// Check the individual blocks.
|
2011-11-09 05:16:01 +01:00
|
|
|
for ( ; BI != BE; ++BI) {
|
|
|
|
BlockT *BB = *BI;
|
2009-09-08 17:45:00 +02:00
|
|
|
bool HasInsideLoopSuccs = false;
|
|
|
|
bool HasInsideLoopPreds = false;
|
|
|
|
SmallVector<BlockT *, 2> OutsideLoopPreds;
|
|
|
|
|
|
|
|
typedef GraphTraits<BlockT*> BlockTraits;
|
|
|
|
for (typename BlockTraits::ChildIteratorType SI =
|
|
|
|
BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
|
|
|
|
SI != SE; ++SI)
|
|
|
|
if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *SI)) {
|
|
|
|
HasInsideLoopSuccs = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
|
|
|
|
for (typename InvBlockTraits::ChildIteratorType PI =
|
|
|
|
InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB);
|
|
|
|
PI != PE; ++PI) {
|
2011-11-09 05:16:01 +01:00
|
|
|
BlockT *N = *PI;
|
2010-07-09 12:42:13 +02:00
|
|
|
if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), N))
|
2009-09-08 17:45:00 +02:00
|
|
|
HasInsideLoopPreds = true;
|
|
|
|
else
|
2010-07-09 12:42:13 +02:00
|
|
|
OutsideLoopPreds.push_back(N);
|
2009-09-08 17:45:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (BB == getHeader()) {
|
|
|
|
assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
|
|
|
|
} else if (!OutsideLoopPreds.empty()) {
|
|
|
|
// A non-header loop shouldn't be reachable from outside the loop,
|
|
|
|
// though it is permitted if the predecessor is not itself actually
|
|
|
|
// reachable.
|
|
|
|
BlockT *EntryBB = BB->getParent()->begin();
|
|
|
|
for (df_iterator<BlockT *> NI = df_begin(EntryBB),
|
|
|
|
NE = df_end(EntryBB); NI != NE; ++NI)
|
|
|
|
for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
|
|
|
|
assert(*NI != OutsideLoopPreds[i] &&
|
|
|
|
"Loop has multiple entry points!");
|
|
|
|
}
|
|
|
|
assert(HasInsideLoopPreds && "Loop block has no in-loop predecessors!");
|
|
|
|
assert(HasInsideLoopSuccs && "Loop block has no in-loop successors!");
|
|
|
|
assert(BB != getHeader()->getParent()->begin() &&
|
|
|
|
"Loop contains function entry block!");
|
2011-11-09 05:16:01 +01:00
|
|
|
|
|
|
|
NumVisited++;
|
2009-09-08 17:45:00 +02:00
|
|
|
}
|
|
|
|
|
2011-11-09 05:16:01 +01:00
|
|
|
assert(NumVisited == getNumBlocks() && "Unreachable block in loop");
|
|
|
|
|
2009-09-08 17:45:00 +02:00
|
|
|
// Check the subloops.
|
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
// Each block in each subloop should be contained within this loop.
|
|
|
|
for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
assert(std::binary_search(LoopBBs.begin(), LoopBBs.end(), *BI) &&
|
|
|
|
"Loop does not contain all the blocks of a subloop!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the parent loop pointer.
|
|
|
|
if (ParentLoop) {
|
|
|
|
assert(std::find(ParentLoop->begin(), ParentLoop->end(), this) !=
|
|
|
|
ParentLoop->end() &&
|
|
|
|
"Loop is not a subloop of its parent!");
|
|
|
|
}
|
2007-11-14 03:33:58 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-09-08 17:45:00 +02:00
|
|
|
/// verifyLoop - Verify loop structure of this loop and all nested loops.
|
2011-08-26 05:06:34 +02:00
|
|
|
void verifyLoopNest(DenseSet<const LoopT*> *Loops) const {
|
|
|
|
Loops->insert(static_cast<const LoopT *>(this));
|
2009-09-08 17:45:00 +02:00
|
|
|
// Verify this loop.
|
|
|
|
verifyLoop();
|
|
|
|
// Verify the subloops.
|
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
2011-08-26 05:06:34 +02:00
|
|
|
(*I)->verifyLoopNest(Loops);
|
2009-09-08 17:45:00 +02:00
|
|
|
}
|
|
|
|
|
2009-08-23 07:17:37 +02:00
|
|
|
void print(raw_ostream &OS, unsigned Depth = 0) const {
|
|
|
|
OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
|
2009-02-27 01:05:54 +01:00
|
|
|
<< " containing: ";
|
2007-11-14 03:33:58 +01:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < getBlocks().size(); ++i) {
|
|
|
|
if (i) OS << ",";
|
2009-02-27 01:05:54 +01:00
|
|
|
BlockT *BB = getBlocks()[i];
|
|
|
|
WriteAsOperand(OS, BB, false);
|
|
|
|
if (BB == getHeader()) OS << "<header>";
|
|
|
|
if (BB == getLoopLatch()) OS << "<latch>";
|
2009-10-25 01:34:26 +02:00
|
|
|
if (isLoopExiting(BB)) OS << "<exiting>";
|
2007-11-14 03:33:58 +01:00
|
|
|
}
|
|
|
|
OS << "\n";
|
2007-07-19 20:02:32 +02:00
|
|
|
|
2007-11-14 03:33:58 +01:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
(*I)->print(OS, Depth+2);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
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;
|
2004-01-08 01:09:44 +01:00
|
|
|
iterator begin() const { return TopLevelLoops.begin(); }
|
|
|
|
iterator end() const { return TopLevelLoops.end(); }
|
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
|
|
|
|
2007-11-27 04:43:35 +01:00
|
|
|
void Calculate(DominatorTreeBase<BlockT> &DT) {
|
2007-11-15 06:00:15 +01:00
|
|
|
BlockT *RootNode = DT.getRootNode()->getBlock();
|
|
|
|
|
|
|
|
for (df_iterator<BlockT*> NI = df_begin(RootNode),
|
|
|
|
NE = df_end(RootNode); NI != NE; ++NI)
|
2009-07-13 23:51:15 +02:00
|
|
|
if (LoopT *L = ConsiderForLoop(*NI, DT))
|
2007-11-15 06:00:15 +01:00
|
|
|
TopLevelLoops.push_back(L);
|
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *ConsiderForLoop(BlockT *BB, DominatorTreeBase<BlockT> &DT) {
|
2012-01-04 22:41:24 +01:00
|
|
|
if (BBMap.count(BB)) return 0; // Haven't processed this node?
|
2007-11-15 06:00:15 +01:00
|
|
|
|
|
|
|
std::vector<BlockT *> TodoStack;
|
|
|
|
|
|
|
|
// Scan the predecessors of BB, checking to see if BB dominates any of
|
|
|
|
// them. This identifies backedges which target this node...
|
2007-11-27 04:43:35 +01:00
|
|
|
typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
|
|
|
|
for (typename InvBlockTraits::ChildIteratorType I =
|
|
|
|
InvBlockTraits::child_begin(BB), E = InvBlockTraits::child_end(BB);
|
2010-07-09 11:50:51 +02:00
|
|
|
I != E; ++I) {
|
|
|
|
typename InvBlockTraits::NodeType *N = *I;
|
|
|
|
if (DT.dominates(BB, N)) // If BB dominates its predecessor...
|
|
|
|
TodoStack.push_back(N);
|
|
|
|
}
|
2007-11-15 06:00:15 +01:00
|
|
|
|
|
|
|
if (TodoStack.empty()) return 0; // No backedges to this block...
|
|
|
|
|
|
|
|
// Create a new loop to represent this basic block...
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *L = new LoopT(BB);
|
2007-11-15 06:00:15 +01:00
|
|
|
BBMap[BB] = L;
|
|
|
|
|
2007-11-27 23:47:08 +01:00
|
|
|
BlockT *EntryBlock = BB->getParent()->begin();
|
2007-11-15 06:00:15 +01:00
|
|
|
|
|
|
|
while (!TodoStack.empty()) { // Process all the nodes in the loop
|
|
|
|
BlockT *X = TodoStack.back();
|
|
|
|
TodoStack.pop_back();
|
|
|
|
|
|
|
|
if (!L->contains(X) && // As of yet unprocessed??
|
|
|
|
DT.dominates(EntryBlock, X)) { // X is reachable from entry block?
|
|
|
|
// Check to see if this block already belongs to a loop. If this occurs
|
2008-01-11 07:27:42 +01:00
|
|
|
// then we have a case where a loop that is supposed to be a child of
|
|
|
|
// the current loop was processed before the current loop. When this
|
|
|
|
// occurs, this child loop gets added to a part of the current loop,
|
|
|
|
// making it a sibling to the current loop. We have to reparent this
|
|
|
|
// loop.
|
2009-07-13 23:51:15 +02:00
|
|
|
if (LoopT *SubLoop =
|
|
|
|
const_cast<LoopT *>(getLoopFor(X)))
|
2008-01-11 07:27:42 +01:00
|
|
|
if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)){
|
2009-09-15 17:08:33 +02:00
|
|
|
// Remove the subloop from its current parent...
|
2007-11-15 06:00:15 +01:00
|
|
|
assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L);
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *SLP = SubLoop->ParentLoop; // SubLoopParent
|
|
|
|
typename std::vector<LoopT *>::iterator I =
|
2007-11-15 06:00:15 +01:00
|
|
|
std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop);
|
2008-01-11 07:27:42 +01:00
|
|
|
assert(I != SLP->SubLoops.end() &&"SubLoop not a child of parent?");
|
2007-11-15 06:00:15 +01:00
|
|
|
SLP->SubLoops.erase(I); // Remove from parent...
|
|
|
|
|
|
|
|
// Add the subloop to THIS loop...
|
|
|
|
SubLoop->ParentLoop = L;
|
|
|
|
L->SubLoops.push_back(SubLoop);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal case, add the block to our loop...
|
|
|
|
L->Blocks.push_back(X);
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2007-11-27 04:43:35 +01:00
|
|
|
typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2007-11-15 06:00:15 +01:00
|
|
|
// Add all of the predecessors of X to the end of the work stack...
|
2007-11-27 04:43:35 +01:00
|
|
|
TodoStack.insert(TodoStack.end(), InvBlockTraits::child_begin(X),
|
|
|
|
InvBlockTraits::child_end(X));
|
2007-11-15 06:00:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are any loops nested within this loop, create them now!
|
|
|
|
for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
|
|
|
|
E = L->Blocks.end(); I != E; ++I)
|
2009-07-13 23:51:15 +02:00
|
|
|
if (LoopT *NewLoop = ConsiderForLoop(*I, DT)) {
|
2007-11-15 06:00:15 +01:00
|
|
|
L->SubLoops.push_back(NewLoop);
|
|
|
|
NewLoop->ParentLoop = L;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the basic blocks that comprise this loop to the BBMap so that this
|
|
|
|
// loop can be found for them.
|
|
|
|
//
|
|
|
|
for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
|
2009-08-27 19:59:08 +02:00
|
|
|
E = L->Blocks.end(); I != E; ++I)
|
|
|
|
BBMap.insert(std::make_pair(*I, L));
|
2007-11-15 06:00:15 +01:00
|
|
|
|
|
|
|
// Now that we have a list of all of the child loops of this loop, check to
|
2008-01-11 07:27:42 +01:00
|
|
|
// see if any of them should actually be nested inside of each other. We
|
|
|
|
// can accidentally pull loops our of their parents, so we must make sure to
|
2007-11-15 06:00:15 +01:00
|
|
|
// organize the loop nests correctly now.
|
|
|
|
{
|
2009-07-13 23:51:15 +02:00
|
|
|
std::map<BlockT *, LoopT *> ContainingLoops;
|
2007-11-15 06:00:15 +01:00
|
|
|
for (unsigned i = 0; i != L->SubLoops.size(); ++i) {
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *Child = L->SubLoops[i];
|
2007-11-15 06:00:15 +01:00
|
|
|
assert(Child->getParentLoop() == L && "Not proper child loop?");
|
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
if (LoopT *ContainingLoop = ContainingLoops[Child->getHeader()]) {
|
2007-11-15 06:00:15 +01:00
|
|
|
// If there is already a loop which contains this loop, move this loop
|
|
|
|
// into the containing loop.
|
|
|
|
MoveSiblingLoopInto(Child, ContainingLoop);
|
|
|
|
--i; // The loop got removed from the SubLoops list.
|
|
|
|
} else {
|
2008-01-11 07:27:42 +01:00
|
|
|
// This is currently considered to be a top-level loop. Check to see
|
|
|
|
// if any of the contained blocks are loop headers for subloops we
|
|
|
|
// have already processed.
|
2007-11-15 06:00:15 +01:00
|
|
|
for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) {
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *&BlockLoop = ContainingLoops[Child->Blocks[b]];
|
2007-11-15 06:00:15 +01:00
|
|
|
if (BlockLoop == 0) { // Child block not processed yet...
|
|
|
|
BlockLoop = Child;
|
|
|
|
} else if (BlockLoop != Child) {
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *SubLoop = BlockLoop;
|
2007-11-15 06:00:15 +01:00
|
|
|
// Reparent all of the blocks which used to belong to BlockLoops
|
2010-02-22 05:04:24 +01:00
|
|
|
for (unsigned j = 0, f = SubLoop->Blocks.size(); j != f; ++j)
|
2007-11-15 06:00:15 +01:00
|
|
|
ContainingLoops[SubLoop->Blocks[j]] = Child;
|
|
|
|
|
|
|
|
// There is already a loop which contains this block, that means
|
|
|
|
// that we should reparent the loop which the block is currently
|
|
|
|
// considered to belong to to be a child of this loop.
|
|
|
|
MoveSiblingLoopInto(SubLoop, Child);
|
|
|
|
--i; // We just shrunk the SubLoops list.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return L;
|
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2008-01-11 07:27:42 +01:00
|
|
|
/// MoveSiblingLoopInto - This method moves the NewChild loop to live inside
|
|
|
|
/// of the NewParent Loop, instead of being a sibling of it.
|
2009-07-13 23:51:15 +02:00
|
|
|
void MoveSiblingLoopInto(LoopT *NewChild,
|
|
|
|
LoopT *NewParent) {
|
|
|
|
LoopT *OldParent = NewChild->getParentLoop();
|
2007-11-15 06:00:15 +01:00
|
|
|
assert(OldParent && OldParent == NewParent->getParentLoop() &&
|
|
|
|
NewChild != NewParent && "Not sibling loops!");
|
|
|
|
|
|
|
|
// Remove NewChild from being a child of OldParent
|
2009-07-13 23:51:15 +02:00
|
|
|
typename std::vector<LoopT *>::iterator I =
|
2008-01-11 07:27:42 +01:00
|
|
|
std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(),
|
|
|
|
NewChild);
|
2007-11-15 06:00:15 +01:00
|
|
|
assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??");
|
|
|
|
OldParent->SubLoops.erase(I); // Remove from parent's subloops list
|
|
|
|
NewChild->ParentLoop = 0;
|
|
|
|
|
|
|
|
InsertLoopInto(NewChild, NewParent);
|
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2008-01-11 07:27:42 +01:00
|
|
|
/// InsertLoopInto - This inserts loop L into the specified parent loop. If
|
|
|
|
/// the parent loop contains a loop which should contain L, the loop gets
|
|
|
|
/// inserted into L instead.
|
2009-07-13 23:51:15 +02:00
|
|
|
void InsertLoopInto(LoopT *L, LoopT *Parent) {
|
2007-11-15 06:00:15 +01:00
|
|
|
BlockT *LHeader = L->getHeader();
|
2008-01-11 07:27:42 +01:00
|
|
|
assert(Parent->contains(LHeader) &&
|
|
|
|
"This loop should not be inserted here!");
|
2007-11-15 06:00:15 +01:00
|
|
|
|
|
|
|
// Check to see if it belongs in a child loop...
|
2008-05-05 20:30:58 +02:00
|
|
|
for (unsigned i = 0, e = static_cast<unsigned>(Parent->SubLoops.size());
|
|
|
|
i != e; ++i)
|
2007-11-15 06:00:15 +01:00
|
|
|
if (Parent->SubLoops[i]->contains(LHeader)) {
|
|
|
|
InsertLoopInto(L, Parent->SubLoops[i]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not, insert it here!
|
|
|
|
Parent->SubLoops.push_back(L);
|
|
|
|
L->ParentLoop = Parent;
|
|
|
|
}
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2007-11-15 06:00:15 +01:00
|
|
|
// Debugging
|
2011-08-04 01:43:49 +02:00
|
|
|
|
2009-08-23 07:17:37 +02:00
|
|
|
void print(raw_ostream &OS) const {
|
2007-11-15 06:00:15 +01:00
|
|
|
for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
|
|
|
|
TopLevelLoops[i]->print(OS);
|
|
|
|
#if 0
|
2010-11-30 19:51:19 +01:00
|
|
|
for (DenseMap<BasicBlock*, LoopT*>::const_iterator I = BBMap.begin(),
|
2007-11-15 06:00:15 +01:00
|
|
|
E = BBMap.end(); I != E; ++I)
|
|
|
|
OS << "BB '" << I->first->getName() << "' level = "
|
|
|
|
<< I->second->getLoopDepth() << "\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2009-06-27 23:22:48 +02:00
|
|
|
inline iterator begin() const { return LI.begin(); }
|
|
|
|
inline iterator end() const { return LI.end(); }
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
template<class BlockT, class LoopT>
|
|
|
|
void
|
|
|
|
LoopBase<BlockT, LoopT>::addBasicBlockToLoop(BlockT *NewBB,
|
|
|
|
LoopInfoBase<BlockT, LoopT> &LIB) {
|
2007-11-27 04:43:35 +01:00
|
|
|
assert((Blocks.empty() || LIB[getHeader()] == this) &&
|
2007-11-14 03:33:58 +01:00
|
|
|
"Incorrect LI specified for this loop!");
|
|
|
|
assert(NewBB && "Cannot add a null basic block to the loop!");
|
2007-11-27 04:43:35 +01:00
|
|
|
assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!");
|
2007-11-15 06:00:15 +01:00
|
|
|
|
2009-07-13 23:51:15 +02:00
|
|
|
LoopT *L = static_cast<LoopT *>(this);
|
|
|
|
|
2007-11-14 03:33:58 +01:00
|
|
|
// Add the loop mapping to the LoopInfo object...
|
2009-07-13 23:51:15 +02:00
|
|
|
LIB.BBMap[NewBB] = L;
|
2007-11-14 03:33:58 +01:00
|
|
|
|
|
|
|
// Add the basic block to this loop and all parent loops...
|
|
|
|
while (L) {
|
|
|
|
L->Blocks.push_back(NewBB);
|
|
|
|
L = L->getParentLoop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-11-26 19:41:20 +01:00
|
|
|
#endif
|