2003-08-18 22:10:57 +00:00
|
|
|
//===- llvm/Analysis/Dominators.h - Dominator Info Calculation --*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-02 05:45:17 +00:00
|
|
|
//
|
|
|
|
// This file defines the following classes:
|
2007-05-14 01:54:16 +00:00
|
|
|
// 1. DominatorTree: Represent dominators as an explicit tree structure.
|
2007-06-27 20:53:52 +00:00
|
|
|
// 2. DominanceFrontier: Calculate and hold the dominance frontier for a
|
2002-04-27 06:56:12 +00:00
|
|
|
// function.
|
2001-07-03 05:35:23 +00:00
|
|
|
//
|
|
|
|
// These data structures are listed in increasing order of complexity. It
|
2005-04-21 20:19:05 +00:00
|
|
|
// takes longer to calculate the dominator frontier, for example, than the
|
2007-05-14 01:54:16 +00:00
|
|
|
// DominatorTree mapping.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2001-07-02 05:45:17 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-08-21 23:43:50 +00:00
|
|
|
#ifndef LLVM_ANALYSIS_DOMINATORS_H
|
|
|
|
#define LLVM_ANALYSIS_DOMINATORS_H
|
2001-07-02 05:45:17 +00:00
|
|
|
|
2002-01-30 23:27:55 +00:00
|
|
|
#include "llvm/Pass.h"
|
2001-07-02 05:45:17 +00:00
|
|
|
#include <set>
|
2007-08-04 23:48:07 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2003-06-30 21:59:07 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-05-13 22:03:16 +00:00
|
|
|
class Instruction;
|
2001-07-02 05:45:17 +00:00
|
|
|
|
2002-10-17 01:36:08 +00:00
|
|
|
template <typename GraphType> struct GraphTraits;
|
|
|
|
|
2001-07-06 16:57:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-03-11 23:08:20 +00:00
|
|
|
/// DominatorBase - Base class that other, more interesting dominator analyses
|
|
|
|
/// inherit from.
|
|
|
|
///
|
2002-04-27 06:56:12 +00:00
|
|
|
class DominatorBase : public FunctionPass {
|
2001-07-06 16:57:21 +00:00
|
|
|
protected:
|
2003-09-10 20:36:51 +00:00
|
|
|
std::vector<BasicBlock*> Roots;
|
2002-01-30 23:27:55 +00:00
|
|
|
const bool IsPostDominators;
|
2007-05-01 21:15:47 +00:00
|
|
|
inline DominatorBase(intptr_t ID, bool isPostDom) :
|
|
|
|
FunctionPass(ID), Roots(), IsPostDominators(isPostDom) {}
|
2001-07-06 16:57:21 +00:00
|
|
|
public:
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// getRoots - Return the root blocks of the current CFG. This may include
|
|
|
|
/// multiple blocks if we are computing post dominators. For forward
|
|
|
|
/// dominators, this will always be a single block (the entry node).
|
|
|
|
///
|
2003-09-10 20:36:51 +00:00
|
|
|
inline const std::vector<BasicBlock*> &getRoots() const { return Roots; }
|
2002-01-30 23:27:55 +00:00
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// isPostDominator - Returns true if analysis based of postdoms
|
|
|
|
///
|
2002-01-30 23:27:55 +00:00
|
|
|
bool isPostDominator() const { return IsPostDominators; }
|
2001-07-06 16:57:21 +00:00
|
|
|
};
|
|
|
|
|
2007-06-04 00:32:22 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DomTreeNode - Dominator Tree Node
|
2007-06-12 05:49:31 +00:00
|
|
|
class DominatorTreeBase;
|
|
|
|
class PostDominatorTree;
|
2007-06-04 00:32:22 +00:00
|
|
|
class DomTreeNode {
|
|
|
|
BasicBlock *TheBB;
|
|
|
|
DomTreeNode *IDom;
|
|
|
|
std::vector<DomTreeNode*> Children;
|
2007-06-12 00:14:41 +00:00
|
|
|
int DFSNumIn, DFSNumOut;
|
|
|
|
|
2007-06-12 05:49:31 +00:00
|
|
|
friend class DominatorTreeBase;
|
|
|
|
friend class PostDominatorTree;
|
2007-06-04 00:32:22 +00:00
|
|
|
public:
|
|
|
|
typedef std::vector<DomTreeNode*>::iterator iterator;
|
|
|
|
typedef std::vector<DomTreeNode*>::const_iterator const_iterator;
|
|
|
|
|
|
|
|
iterator begin() { return Children.begin(); }
|
|
|
|
iterator end() { return Children.end(); }
|
|
|
|
const_iterator begin() const { return Children.begin(); }
|
|
|
|
const_iterator end() const { return Children.end(); }
|
|
|
|
|
2007-08-06 06:15:43 +00:00
|
|
|
BasicBlock *getBlock() const { return TheBB; }
|
|
|
|
DomTreeNode *getIDom() const { return IDom; }
|
|
|
|
const std::vector<DomTreeNode*> &getChildren() const { return Children; }
|
2007-06-04 00:32:22 +00:00
|
|
|
|
2007-08-06 06:15:43 +00:00
|
|
|
DomTreeNode(BasicBlock *BB, DomTreeNode *iDom)
|
2007-06-12 00:54:38 +00:00
|
|
|
: TheBB(BB), IDom(iDom), DFSNumIn(-1), DFSNumOut(-1) { }
|
2007-08-06 06:15:43 +00:00
|
|
|
DomTreeNode *addChild(DomTreeNode *C) { Children.push_back(C); return C; }
|
2007-06-04 00:32:22 +00:00
|
|
|
void setIDom(DomTreeNode *NewIDom);
|
2007-06-12 00:14:41 +00:00
|
|
|
|
2007-08-06 06:15:43 +00:00
|
|
|
|
|
|
|
/// getDFSNumIn/getDFSNumOut - These are an internal implementation detail, do
|
|
|
|
/// not call them.
|
|
|
|
unsigned getDFSNumIn() const { return DFSNumIn; }
|
|
|
|
unsigned getDFSNumOut() const { return DFSNumOut; }
|
2007-06-12 05:49:31 +00:00
|
|
|
private:
|
2007-08-06 06:15:43 +00:00
|
|
|
// Return true if this node is dominated by other. Use this only if DFS info
|
|
|
|
// is valid.
|
2007-06-12 00:14:41 +00:00
|
|
|
bool DominatedBy(const DomTreeNode *other) const {
|
|
|
|
return this->DFSNumIn >= other->DFSNumIn &&
|
|
|
|
this->DFSNumOut <= other->DFSNumOut;
|
|
|
|
}
|
2007-06-04 00:32:22 +00:00
|
|
|
};
|
|
|
|
|
2003-12-07 00:36:16 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-04-15 08:47:27 +00:00
|
|
|
/// DominatorTree - Calculate the immediate dominator tree for a function.
|
2004-03-11 23:08:20 +00:00
|
|
|
///
|
2007-04-15 08:47:27 +00:00
|
|
|
class DominatorTreeBase : public DominatorBase {
|
2003-12-07 00:36:16 +00:00
|
|
|
protected:
|
2007-04-15 08:47:27 +00:00
|
|
|
void reset();
|
2007-08-04 23:48:07 +00:00
|
|
|
typedef DenseMap<BasicBlock*, DomTreeNode*> DomTreeNodeMapType;
|
2007-06-07 17:47:21 +00:00
|
|
|
DomTreeNodeMapType DomTreeNodes;
|
2007-06-03 06:26:14 +00:00
|
|
|
DomTreeNode *RootNode;
|
2007-04-15 08:47:27 +00:00
|
|
|
|
2007-06-07 17:47:21 +00:00
|
|
|
bool DFSInfoValid;
|
|
|
|
unsigned int SlowQueries;
|
2007-06-04 16:22:33 +00:00
|
|
|
// Information record used during immediate dominators computation.
|
2007-04-15 23:14:18 +00:00
|
|
|
struct InfoRec {
|
2006-03-20 19:32:48 +00:00
|
|
|
unsigned Semi;
|
|
|
|
unsigned Size;
|
|
|
|
BasicBlock *Label, *Parent, *Child, *Ancestor;
|
2007-04-15 08:47:27 +00:00
|
|
|
|
2006-03-20 19:32:48 +00:00
|
|
|
std::vector<BasicBlock*> Bucket;
|
2007-04-15 08:47:27 +00:00
|
|
|
|
2007-08-06 06:15:43 +00:00
|
|
|
InfoRec() : Semi(0), Size(0), Label(0), Parent(0), Child(0), Ancestor(0) {}
|
2006-03-20 19:32:48 +00:00
|
|
|
};
|
2007-04-15 08:47:27 +00:00
|
|
|
|
2007-08-04 23:48:07 +00:00
|
|
|
DenseMap<BasicBlock*, BasicBlock*> IDoms;
|
2006-03-20 19:32:48 +00:00
|
|
|
|
|
|
|
// Vertex - Map the DFS number to the BasicBlock*
|
|
|
|
std::vector<BasicBlock*> Vertex;
|
2007-04-15 08:47:27 +00:00
|
|
|
|
2006-03-20 19:32:48 +00:00
|
|
|
// Info - Collection of information used during the computation of idoms.
|
2007-08-05 00:02:00 +00:00
|
|
|
DenseMap<BasicBlock*, InfoRec> Info;
|
2003-09-10 20:36:51 +00:00
|
|
|
|
2007-08-08 05:51:24 +00:00
|
|
|
public:
|
2007-05-01 21:15:47 +00:00
|
|
|
DominatorTreeBase(intptr_t ID, bool isPostDom)
|
2007-06-07 17:47:21 +00:00
|
|
|
: DominatorBase(ID, isPostDom), DFSInfoValid(false), SlowQueries(0) {}
|
2002-07-26 18:40:06 +00:00
|
|
|
~DominatorTreeBase() { reset(); }
|
|
|
|
|
|
|
|
virtual void releaseMemory() { reset(); }
|
|
|
|
|
2002-09-06 02:16:27 +00:00
|
|
|
/// getNode - return the (Post)DominatorTree node for the specified basic
|
|
|
|
/// block. This is the same as using operator[] on this class.
|
|
|
|
///
|
2007-06-03 06:26:14 +00:00
|
|
|
inline DomTreeNode *getNode(BasicBlock *BB) const {
|
2007-08-04 23:48:07 +00:00
|
|
|
DomTreeNodeMapType::const_iterator I = DomTreeNodes.find(BB);
|
|
|
|
return I != DomTreeNodes.end() ? I->second : 0;
|
2002-07-26 18:40:06 +00:00
|
|
|
}
|
2002-07-27 01:12:15 +00:00
|
|
|
|
2007-06-03 06:26:14 +00:00
|
|
|
inline DomTreeNode *operator[](BasicBlock *BB) const {
|
2002-09-06 02:16:27 +00:00
|
|
|
return getNode(BB);
|
|
|
|
}
|
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// getRootNode - This returns the entry node for the CFG of the function. If
|
|
|
|
/// this tree represents the post-dominance relations for a function, however,
|
|
|
|
/// this root may be a node with the block == NULL. This is the case when
|
|
|
|
/// there are multiple exit nodes from a particular function. Consumers of
|
|
|
|
/// post-dominance information must be capable of dealing with this
|
|
|
|
/// possibility.
|
|
|
|
///
|
2007-06-03 06:26:14 +00:00
|
|
|
DomTreeNode *getRootNode() { return RootNode; }
|
|
|
|
const DomTreeNode *getRootNode() const { return RootNode; }
|
2003-09-10 20:36:51 +00:00
|
|
|
|
2007-06-07 17:47:21 +00:00
|
|
|
/// properlyDominates - Returns true iff this dominates N and this != N.
|
|
|
|
/// Note that this is not a constant time operation!
|
|
|
|
///
|
|
|
|
bool properlyDominates(const DomTreeNode *A, DomTreeNode *B) const {
|
|
|
|
if (A == 0 || B == 0) return false;
|
|
|
|
return dominatedBySlowTreeWalk(A, B);
|
|
|
|
}
|
|
|
|
|
2007-06-07 21:34:22 +00:00
|
|
|
inline bool properlyDominates(BasicBlock *A, BasicBlock *B) {
|
|
|
|
return properlyDominates(getNode(A), getNode(B));
|
|
|
|
}
|
|
|
|
|
2007-06-07 17:47:21 +00:00
|
|
|
bool dominatedBySlowTreeWalk(const DomTreeNode *A,
|
|
|
|
const DomTreeNode *B) const {
|
|
|
|
const DomTreeNode *IDom;
|
|
|
|
if (A == 0 || B == 0) return false;
|
2007-06-28 02:07:08 +00:00
|
|
|
while ((IDom = B->getIDom()) != 0 && IDom != A && IDom != B)
|
2007-06-07 17:47:21 +00:00
|
|
|
B = IDom; // Walk up the tree
|
|
|
|
return IDom != 0;
|
|
|
|
}
|
|
|
|
|
2007-06-07 18:39:40 +00:00
|
|
|
|
2007-06-08 01:50:32 +00:00
|
|
|
/// isReachableFromEntry - Return true if A is dominated by the entry
|
|
|
|
/// block of the function containing it.
|
|
|
|
const bool isReachableFromEntry(BasicBlock* A);
|
2007-06-08 00:21:17 +00:00
|
|
|
|
2007-06-12 17:30:56 +00:00
|
|
|
/// dominates - Returns true iff A dominates B. Note that this is not a
|
2007-06-07 17:47:21 +00:00
|
|
|
/// constant time operation!
|
|
|
|
///
|
|
|
|
inline bool dominates(const DomTreeNode *A, DomTreeNode *B) {
|
2007-06-07 18:39:40 +00:00
|
|
|
if (B == A)
|
|
|
|
return true; // A node trivially dominates itself.
|
|
|
|
|
|
|
|
if (A == 0 || B == 0)
|
|
|
|
return false;
|
2007-06-07 17:47:21 +00:00
|
|
|
|
|
|
|
if (DFSInfoValid)
|
2007-06-12 00:14:41 +00:00
|
|
|
return B->DominatedBy(A);
|
2007-06-07 17:47:21 +00:00
|
|
|
|
|
|
|
// If we end up with too many slow queries, just update the
|
|
|
|
// DFS numbers on the theory that we are going to keep querying.
|
|
|
|
SlowQueries++;
|
|
|
|
if (SlowQueries > 32) {
|
|
|
|
updateDFSNumbers();
|
2007-06-12 00:14:41 +00:00
|
|
|
return B->DominatedBy(A);
|
2007-06-07 17:47:21 +00:00
|
|
|
}
|
2007-06-12 00:54:38 +00:00
|
|
|
|
2007-06-07 17:47:21 +00:00
|
|
|
return dominatedBySlowTreeWalk(A, B);
|
|
|
|
}
|
2007-06-07 18:39:40 +00:00
|
|
|
|
|
|
|
inline bool dominates(BasicBlock *A, BasicBlock *B) {
|
|
|
|
if (A == B)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return dominates(getNode(A), getNode(B));
|
|
|
|
}
|
|
|
|
|
2007-06-11 23:31:22 +00:00
|
|
|
/// findNearestCommonDominator - Find nearest common dominator basic block
|
|
|
|
/// for basic block A and B. If there is no such block then return NULL.
|
|
|
|
BasicBlock *findNearestCommonDominator(BasicBlock *A, BasicBlock *B);
|
|
|
|
|
2007-06-07 23:52:40 +00:00
|
|
|
// dominates - Return true if A dominates B. This performs the
|
|
|
|
// special checks necessary if A and B are in the same basic block.
|
|
|
|
bool dominates(Instruction *A, Instruction *B);
|
|
|
|
|
2003-09-10 20:36:51 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// API to update (Post)DominatorTree information based on modifications to
|
2002-09-06 02:16:27 +00:00
|
|
|
// the CFG...
|
|
|
|
|
2007-06-04 16:43:25 +00:00
|
|
|
/// addNewBlock - Add a new node to the dominator tree information. This
|
|
|
|
/// creates a new node as a child of DomBB dominator node,linking it into
|
|
|
|
/// the children list of the immediate dominator.
|
2007-06-04 16:49:36 +00:00
|
|
|
DomTreeNode *addNewBlock(BasicBlock *BB, BasicBlock *DomBB) {
|
2002-09-06 02:16:27 +00:00
|
|
|
assert(getNode(BB) == 0 && "Block already in dominator tree!");
|
2007-06-04 16:49:36 +00:00
|
|
|
DomTreeNode *IDomNode = getNode(DomBB);
|
2002-09-29 21:37:08 +00:00
|
|
|
assert(IDomNode && "Not immediate dominator specified for block!");
|
2007-06-07 17:47:21 +00:00
|
|
|
DFSInfoValid = false;
|
|
|
|
return DomTreeNodes[BB] =
|
2007-06-12 00:54:38 +00:00
|
|
|
IDomNode->addChild(new DomTreeNode(BB, IDomNode));
|
2002-09-06 02:16:27 +00:00
|
|
|
}
|
|
|
|
|
2002-09-26 16:14:37 +00:00
|
|
|
/// changeImmediateDominator - This method is used to update the dominator
|
|
|
|
/// tree information when a node's immediate dominator changes.
|
|
|
|
///
|
2007-06-03 06:26:14 +00:00
|
|
|
void changeImmediateDominator(DomTreeNode *N, DomTreeNode *NewIDom) {
|
2004-05-21 18:38:16 +00:00
|
|
|
assert(N && NewIDom && "Cannot change null node pointers!");
|
2007-06-07 17:47:21 +00:00
|
|
|
DFSInfoValid = false;
|
2004-05-21 18:38:16 +00:00
|
|
|
N->setIDom(NewIDom);
|
2002-09-26 16:14:37 +00:00
|
|
|
}
|
|
|
|
|
2007-06-04 16:22:33 +00:00
|
|
|
void changeImmediateDominator(BasicBlock *BB, BasicBlock *NewBB) {
|
|
|
|
changeImmediateDominator(getNode(BB), getNode(NewBB));
|
|
|
|
}
|
|
|
|
|
2007-08-14 16:53:24 +00:00
|
|
|
/// eraseNode - Removes a node from the dominator tree. Block must not
|
2007-08-13 22:10:29 +00:00
|
|
|
/// domiante any other blocks. Removes node from its immediate dominator's
|
|
|
|
/// children list. Deletes dominator node associated with basic block BB.
|
|
|
|
void eraseNode(BasicBlock *BB);
|
|
|
|
|
2006-09-12 00:18:28 +00:00
|
|
|
/// removeNode - Removes a node from the dominator tree. Block must not
|
|
|
|
/// dominate any other blocks. Invalidates any node pointing to removed
|
|
|
|
/// block.
|
|
|
|
void removeNode(BasicBlock *BB) {
|
|
|
|
assert(getNode(BB) && "Removing node that isn't in dominator tree.");
|
2007-06-03 06:26:14 +00:00
|
|
|
DomTreeNodes.erase(BB);
|
2006-09-12 00:18:28 +00:00
|
|
|
}
|
|
|
|
|
2002-09-06 02:16:27 +00:00
|
|
|
/// print - Convert to human readable form
|
2004-03-11 23:08:20 +00:00
|
|
|
///
|
2004-12-07 04:03:45 +00:00
|
|
|
virtual void print(std::ostream &OS, const Module* = 0) const;
|
2006-12-17 05:15:13 +00:00
|
|
|
void print(std::ostream *OS, const Module* M = 0) const {
|
|
|
|
if (OS) print(*OS, M);
|
|
|
|
}
|
2007-05-23 19:55:36 +00:00
|
|
|
virtual void dump();
|
2007-08-08 05:51:24 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
|
|
|
|
/// dominator tree in dfs order.
|
|
|
|
void updateDFSNumbers();
|
2002-07-26 18:40:06 +00:00
|
|
|
};
|
|
|
|
|
2006-10-03 05:24:56 +00:00
|
|
|
//===-------------------------------------
|
|
|
|
/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
|
|
|
|
/// compute a normal dominator tree.
|
|
|
|
///
|
|
|
|
class DominatorTree : public DominatorTreeBase {
|
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2007-08-12 08:12:35 +00:00
|
|
|
DominatorTree() : DominatorTreeBase(intptr_t(&ID), false) {}
|
2006-10-03 05:24:56 +00:00
|
|
|
|
|
|
|
BasicBlock *getRoot() const {
|
|
|
|
assert(Roots.size() == 1 && "Should always have entry node!");
|
|
|
|
return Roots[0];
|
|
|
|
}
|
|
|
|
|
2007-04-15 23:14:18 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2006-10-03 05:24:56 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2007-06-21 17:23:45 +00:00
|
|
|
|
|
|
|
/// splitBlock
|
|
|
|
/// BB is split and now it has one successor. Update dominator tree to
|
|
|
|
/// reflect this change.
|
|
|
|
void splitBlock(BasicBlock *BB);
|
2006-10-03 05:24:56 +00:00
|
|
|
private:
|
2007-04-15 08:47:27 +00:00
|
|
|
void calculate(Function& F);
|
2007-06-03 06:26:14 +00:00
|
|
|
DomTreeNode *getNodeForBlock(BasicBlock *BB);
|
2007-08-05 00:02:00 +00:00
|
|
|
unsigned DFSPass(BasicBlock *V, unsigned N);
|
2007-05-03 20:55:18 +00:00
|
|
|
void Compress(BasicBlock *V);
|
2007-04-15 08:47:27 +00:00
|
|
|
BasicBlock *Eval(BasicBlock *v);
|
|
|
|
void Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo);
|
2007-04-15 23:14:18 +00:00
|
|
|
inline BasicBlock *getIDom(BasicBlock *BB) const {
|
2007-08-04 23:48:07 +00:00
|
|
|
DenseMap<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
|
|
|
|
return I != IDoms.end() ? I->second : 0;
|
|
|
|
}
|
2006-10-03 05:24:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===-------------------------------------
|
|
|
|
/// DominatorTree GraphTraits specialization so the DominatorTree can be
|
|
|
|
/// iterable by generic graph iterators.
|
|
|
|
///
|
2007-06-04 00:32:22 +00:00
|
|
|
template <> struct GraphTraits<DomTreeNode*> {
|
|
|
|
typedef DomTreeNode NodeType;
|
2006-10-03 05:24:56 +00:00
|
|
|
typedef NodeType::iterator ChildIteratorType;
|
|
|
|
|
|
|
|
static NodeType *getEntryNode(NodeType *N) {
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
static inline ChildIteratorType child_begin(NodeType* N) {
|
|
|
|
return N->begin();
|
|
|
|
}
|
|
|
|
static inline ChildIteratorType child_end(NodeType* N) {
|
|
|
|
return N->end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct GraphTraits<DominatorTree*>
|
2007-06-04 00:32:22 +00:00
|
|
|
: public GraphTraits<DomTreeNode*> {
|
2006-10-03 05:24:56 +00:00
|
|
|
static NodeType *getEntryNode(DominatorTree *DT) {
|
|
|
|
return DT->getRootNode();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-07-26 18:40:06 +00:00
|
|
|
|
2001-07-02 05:45:17 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2006-05-27 06:57:55 +00:00
|
|
|
/// DominanceFrontierBase - Common base class for computing forward and inverse
|
|
|
|
/// dominance frontiers for a function.
|
2004-03-11 23:08:20 +00:00
|
|
|
///
|
2006-05-19 17:17:12 +00:00
|
|
|
class DominanceFrontierBase : public DominatorBase {
|
|
|
|
public:
|
2002-04-28 00:15:57 +00:00
|
|
|
typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
|
|
|
|
typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
|
2002-07-26 18:40:06 +00:00
|
|
|
protected:
|
2001-07-02 05:45:17 +00:00
|
|
|
DomSetMapType Frontiers;
|
|
|
|
public:
|
2007-05-01 21:15:47 +00:00
|
|
|
DominanceFrontierBase(intptr_t ID, bool isPostDom)
|
|
|
|
: DominatorBase(ID, isPostDom) {}
|
2002-07-26 18:40:06 +00:00
|
|
|
|
|
|
|
virtual void releaseMemory() { Frontiers.clear(); }
|
|
|
|
|
|
|
|
// Accessor interface:
|
2003-02-27 20:24:17 +00:00
|
|
|
typedef DomSetMapType::iterator iterator;
|
2002-07-26 18:40:06 +00:00
|
|
|
typedef DomSetMapType::const_iterator const_iterator;
|
2003-02-27 20:24:17 +00:00
|
|
|
iterator begin() { return Frontiers.begin(); }
|
2002-10-31 02:39:48 +00:00
|
|
|
const_iterator begin() const { return Frontiers.begin(); }
|
2003-02-27 20:24:17 +00:00
|
|
|
iterator end() { return Frontiers.end(); }
|
2002-10-31 02:39:48 +00:00
|
|
|
const_iterator end() const { return Frontiers.end(); }
|
2003-02-27 20:24:17 +00:00
|
|
|
iterator find(BasicBlock *B) { return Frontiers.find(B); }
|
|
|
|
const_iterator find(BasicBlock *B) const { return Frontiers.find(B); }
|
2002-10-31 02:39:48 +00:00
|
|
|
|
|
|
|
void addBasicBlock(BasicBlock *BB, const DomSetType &frontier) {
|
|
|
|
assert(find(BB) == end() && "Block already in DominanceFrontier!");
|
|
|
|
Frontiers.insert(std::make_pair(BB, frontier));
|
|
|
|
}
|
|
|
|
|
2007-08-13 22:10:29 +00:00
|
|
|
/// removeBlock - Remove basic block BB's frontier.
|
|
|
|
void removeBlock(BasicBlock *BB) {
|
|
|
|
assert(find(BB) != end() && "Block is not in DominanceFrontier!");
|
2007-08-14 16:53:24 +00:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
2007-08-18 14:46:55 +00:00
|
|
|
I->second.erase(BB);
|
2007-08-14 16:53:24 +00:00
|
|
|
Frontiers.erase(BB);
|
2007-08-13 22:10:29 +00:00
|
|
|
}
|
|
|
|
|
2003-02-27 20:24:17 +00:00
|
|
|
void addToFrontier(iterator I, BasicBlock *Node) {
|
2002-10-31 02:39:48 +00:00
|
|
|
assert(I != end() && "BB is not in DominanceFrontier!");
|
|
|
|
I->second.insert(Node);
|
|
|
|
}
|
|
|
|
|
2003-02-27 20:24:17 +00:00
|
|
|
void removeFromFrontier(iterator I, BasicBlock *Node) {
|
2002-10-31 02:39:48 +00:00
|
|
|
assert(I != end() && "BB is not in DominanceFrontier!");
|
|
|
|
assert(I->second.count(Node) && "Node is not in DominanceFrontier of BB");
|
|
|
|
I->second.erase(Node);
|
|
|
|
}
|
2002-07-27 01:12:15 +00:00
|
|
|
|
2004-03-11 23:08:20 +00:00
|
|
|
/// print - Convert to human readable form
|
|
|
|
///
|
2004-12-07 04:03:45 +00:00
|
|
|
virtual void print(std::ostream &OS, const Module* = 0) const;
|
2006-12-17 05:15:13 +00:00
|
|
|
void print(std::ostream *OS, const Module* M = 0) const {
|
|
|
|
if (OS) print(*OS, M);
|
|
|
|
}
|
2007-05-23 19:55:36 +00:00
|
|
|
virtual void dump();
|
2002-07-26 18:40:06 +00:00
|
|
|
};
|
2002-01-30 23:27:55 +00:00
|
|
|
|
|
|
|
|
2002-07-26 18:40:06 +00:00
|
|
|
//===-------------------------------------
|
2006-05-27 06:57:55 +00:00
|
|
|
/// DominanceFrontier Class - Concrete subclass of DominanceFrontierBase that is
|
|
|
|
/// used to compute a forward dominator frontiers.
|
2004-03-11 23:08:20 +00:00
|
|
|
///
|
2006-05-19 17:17:12 +00:00
|
|
|
class DominanceFrontier : public DominanceFrontierBase {
|
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2007-05-01 21:15:47 +00:00
|
|
|
DominanceFrontier() :
|
2007-08-12 08:12:35 +00:00
|
|
|
DominanceFrontierBase(intptr_t(&ID), false) {}
|
2002-01-30 23:27:55 +00:00
|
|
|
|
2003-09-10 20:36:51 +00:00
|
|
|
BasicBlock *getRoot() const {
|
|
|
|
assert(Roots.size() == 1 && "Should always have entry node!");
|
|
|
|
return Roots[0];
|
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &) {
|
2002-01-30 23:27:55 +00:00
|
|
|
Frontiers.clear();
|
2002-07-26 18:40:06 +00:00
|
|
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
2003-09-10 20:36:51 +00:00
|
|
|
Roots = DT.getRoots();
|
|
|
|
assert(Roots.size() == 1 && "Only one entry block for forward domfronts!");
|
|
|
|
calculate(DT, DT[Roots[0]]);
|
2002-01-30 23:27:55 +00:00
|
|
|
return false;
|
2001-07-02 05:45:17 +00:00
|
|
|
}
|
|
|
|
|
2002-07-26 18:40:06 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequired<DominatorTree>();
|
2002-07-26 18:40:06 +00:00
|
|
|
}
|
2007-06-04 00:32:22 +00:00
|
|
|
|
2007-08-06 06:15:43 +00:00
|
|
|
/// splitBlock - BB is split and now it has one successor. Update dominance
|
|
|
|
/// frontier to reflect this change.
|
2007-06-21 17:23:45 +00:00
|
|
|
void splitBlock(BasicBlock *BB);
|
|
|
|
|
2007-08-17 21:59:16 +00:00
|
|
|
/// BasicBlock BB's new dominator is NewBB. Update BB's dominance frontier
|
|
|
|
/// to reflect this change.
|
|
|
|
void changeImmediateDominator(BasicBlock *BB, BasicBlock *NewBB,
|
|
|
|
DominatorTree *DT) {
|
|
|
|
// NewBB is now dominating BB. Which means BB's dominance
|
|
|
|
// frontier is now part of NewBB's dominance frontier. However, BB
|
|
|
|
// itself is not member of NewBB's dominance frontier.
|
|
|
|
DominanceFrontier::iterator NewDFI = find(NewBB);
|
|
|
|
DominanceFrontier::iterator DFI = find(BB);
|
|
|
|
DominanceFrontier::DomSetType BBSet = DFI->second;
|
|
|
|
for (DominanceFrontier::DomSetType::iterator BBSetI = BBSet.begin(),
|
|
|
|
BBSetE = BBSet.end(); BBSetI != BBSetE; ++BBSetI) {
|
|
|
|
BasicBlock *DFMember = *BBSetI;
|
|
|
|
// Insert only if NewBB dominates DFMember.
|
|
|
|
if (!DT->dominates(NewBB, DFMember))
|
|
|
|
NewDFI->second.insert(DFMember);
|
|
|
|
}
|
|
|
|
NewDFI->second.erase(BB);
|
|
|
|
}
|
|
|
|
|
2002-07-26 18:40:06 +00:00
|
|
|
private:
|
|
|
|
const DomSetType &calculate(const DominatorTree &DT,
|
2007-06-04 00:32:22 +00:00
|
|
|
const DomTreeNode *Node);
|
2002-07-26 18:40:06 +00:00
|
|
|
};
|
|
|
|
|
2006-01-08 08:22:18 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-07-02 05:45:17 +00:00
|
|
|
#endif
|