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-04-15 08:47:27 +00:00
|
|
|
// 1. DominatorTree: Represent the ImmediateDominator as an explicit tree
|
2001-07-03 05:35:23 +00:00
|
|
|
// structure.
|
2007-04-15 08:47:27 +00:00
|
|
|
// 2. ETForest: Efficient data structure for dominance comparisons and
|
2006-01-08 08:22:18 +00:00
|
|
|
// nearest-common-ancestor queries.
|
2007-04-15 08:47:27 +00:00
|
|
|
// 3. 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
|
2001-07-03 05:35:23 +00:00
|
|
|
// ImmediateDominator 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
|
|
|
|
2006-01-08 08:22:18 +00:00
|
|
|
#include "llvm/Analysis/ET-Forest.h"
|
2002-01-30 23:27:55 +00:00
|
|
|
#include "llvm/Pass.h"
|
2001-07-02 05:45:17 +00:00
|
|
|
#include <set>
|
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
|
|
|
};
|
|
|
|
|
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 {
|
|
|
|
public:
|
|
|
|
class Node;
|
2003-12-07 00:36:16 +00:00
|
|
|
protected:
|
2007-04-15 08:47:27 +00:00
|
|
|
std::map<BasicBlock*, Node*> Nodes;
|
|
|
|
void reset();
|
|
|
|
typedef std::map<BasicBlock*, Node*> NodeMapType;
|
|
|
|
|
|
|
|
Node *RootNode;
|
|
|
|
|
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
|
|
|
|
2006-03-20 19:32:48 +00:00
|
|
|
InfoRec() : Semi(0), Size(0), Label(0), Parent(0), Child(0), Ancestor(0){}
|
|
|
|
};
|
2007-04-15 08:47:27 +00:00
|
|
|
|
2003-12-07 00:36:16 +00:00
|
|
|
std::map<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.
|
|
|
|
std::map<BasicBlock*, InfoRec> Info;
|
2003-09-10 20:36:51 +00:00
|
|
|
|
2001-07-02 05:45:17 +00:00
|
|
|
public:
|
2003-09-11 16:25:06 +00:00
|
|
|
class Node {
|
2006-11-05 19:31:28 +00:00
|
|
|
friend class DominatorTree;
|
2004-10-27 16:14:51 +00:00
|
|
|
friend struct PostDominatorTree;
|
2006-11-05 19:31:28 +00:00
|
|
|
friend class DominatorTreeBase;
|
2003-09-11 16:25:06 +00:00
|
|
|
BasicBlock *TheBB;
|
|
|
|
Node *IDom;
|
2002-09-26 16:14:37 +00:00
|
|
|
std::vector<Node*> Children;
|
2001-07-02 05:45:17 +00:00
|
|
|
public:
|
2002-09-26 16:14:37 +00:00
|
|
|
typedef std::vector<Node*>::iterator iterator;
|
|
|
|
typedef std::vector<Node*>::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(); }
|
|
|
|
|
2003-09-11 16:25:06 +00:00
|
|
|
inline BasicBlock *getBlock() const { return TheBB; }
|
|
|
|
inline Node *getIDom() const { return IDom; }
|
2002-09-26 16:14:37 +00:00
|
|
|
inline const std::vector<Node*> &getChildren() const { return Children; }
|
2001-07-02 05:45:17 +00:00
|
|
|
|
2005-11-18 07:27:33 +00:00
|
|
|
/// properlyDominates - Returns true iff this dominates N and this != N.
|
|
|
|
/// Note that this is not a constant time operation!
|
2004-03-11 23:08:20 +00:00
|
|
|
///
|
2005-11-18 07:27:33 +00:00
|
|
|
bool properlyDominates(const Node *N) const {
|
2003-09-11 16:25:06 +00:00
|
|
|
const Node *IDom;
|
2005-11-29 01:07:12 +00:00
|
|
|
if (this == 0 || N == 0) return false;
|
2001-07-02 05:45:17 +00:00
|
|
|
while ((IDom = N->getIDom()) != 0 && IDom != this)
|
2005-11-18 07:27:33 +00:00
|
|
|
N = IDom; // Walk up the tree
|
2001-07-02 05:45:17 +00:00
|
|
|
return IDom != 0;
|
|
|
|
}
|
|
|
|
|
2005-11-18 07:27:33 +00:00
|
|
|
/// dominates - Returns true iff this dominates N. Note that this is not a
|
|
|
|
/// constant time operation!
|
|
|
|
///
|
|
|
|
inline bool dominates(const Node *N) const {
|
|
|
|
if (N == this) return true; // A node trivially dominates itself.
|
|
|
|
return properlyDominates(N);
|
|
|
|
}
|
|
|
|
|
2001-07-02 05:45:17 +00:00
|
|
|
private:
|
2004-03-11 23:08:20 +00:00
|
|
|
inline Node(BasicBlock *BB, Node *iDom) : TheBB(BB), IDom(iDom) {}
|
2003-09-11 16:25:06 +00:00
|
|
|
inline Node *addChild(Node *C) { Children.push_back(C); return C; }
|
2002-09-26 16:14:37 +00:00
|
|
|
|
2003-09-11 16:25:06 +00:00
|
|
|
void setIDom(Node *NewIDom);
|
2001-07-02 05:45:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2007-05-01 21:15:47 +00:00
|
|
|
DominatorTreeBase(intptr_t ID, bool isPostDom)
|
|
|
|
: DominatorBase(ID, isPostDom) {}
|
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.
|
|
|
|
///
|
|
|
|
inline Node *getNode(BasicBlock *BB) const {
|
2002-07-26 18:40:06 +00:00
|
|
|
NodeMapType::const_iterator i = Nodes.find(BB);
|
|
|
|
return (i != Nodes.end()) ? i->second : 0;
|
|
|
|
}
|
2002-07-27 01:12:15 +00:00
|
|
|
|
2002-09-06 02:16:27 +00:00
|
|
|
inline Node *operator[](BasicBlock *BB) const {
|
|
|
|
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.
|
|
|
|
///
|
2003-09-10 20:36:51 +00:00
|
|
|
Node *getRootNode() { return RootNode; }
|
|
|
|
const Node *getRootNode() const { return RootNode; }
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// API to update (Post)DominatorTree information based on modifications to
|
2002-09-06 02:16:27 +00:00
|
|
|
// the CFG...
|
|
|
|
|
|
|
|
/// createNewNode - Add a new node to the dominator tree information. This
|
|
|
|
/// creates a new node as a child of IDomNode, linking it into the children
|
|
|
|
/// list of the immediate dominator.
|
|
|
|
///
|
|
|
|
Node *createNewNode(BasicBlock *BB, Node *IDomNode) {
|
|
|
|
assert(getNode(BB) == 0 && "Block already in dominator tree!");
|
2002-09-29 21:37:08 +00:00
|
|
|
assert(IDomNode && "Not immediate dominator specified for block!");
|
|
|
|
return Nodes[BB] = IDomNode->addChild(new Node(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.
|
|
|
|
///
|
2004-05-21 18:38:16 +00:00
|
|
|
void changeImmediateDominator(Node *N, Node *NewIDom) {
|
|
|
|
assert(N && NewIDom && "Cannot change null node pointers!");
|
|
|
|
N->setIDom(NewIDom);
|
2002-09-26 16:14:37 +00:00
|
|
|
}
|
|
|
|
|
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.");
|
|
|
|
Nodes.erase(BB);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
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-05-01 21:15:47 +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();
|
|
|
|
}
|
|
|
|
private:
|
2007-04-15 08:47:27 +00:00
|
|
|
void calculate(Function& F);
|
2006-10-03 05:24:56 +00:00
|
|
|
Node *getNodeForBlock(BasicBlock *BB);
|
2007-04-15 08:47:27 +00:00
|
|
|
unsigned DFSPass(BasicBlock *V, InfoRec &VInfo, 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 {
|
|
|
|
std::map<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.
|
|
|
|
///
|
|
|
|
template <> struct GraphTraits<DominatorTree::Node*> {
|
|
|
|
typedef DominatorTree::Node NodeType;
|
|
|
|
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*>
|
|
|
|
: public GraphTraits<DominatorTree::Node*> {
|
|
|
|
static NodeType *getEntryNode(DominatorTree *DT) {
|
|
|
|
return DT->getRootNode();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-07-26 18:40:06 +00:00
|
|
|
|
2006-01-08 08:22:18 +00:00
|
|
|
//===-------------------------------------
|
|
|
|
/// ET-Forest Class - Class used to construct forwards and backwards
|
|
|
|
/// ET-Forests
|
|
|
|
///
|
2006-05-19 17:17:12 +00:00
|
|
|
class ETForestBase : public DominatorBase {
|
|
|
|
public:
|
2007-05-01 21:15:47 +00:00
|
|
|
ETForestBase(intptr_t ID, bool isPostDom)
|
|
|
|
: DominatorBase(ID, isPostDom), Nodes(),
|
|
|
|
DFSInfoValid(false), SlowQueries(0) {}
|
2006-01-08 08:22:18 +00:00
|
|
|
|
|
|
|
virtual void releaseMemory() { reset(); }
|
|
|
|
|
|
|
|
typedef std::map<BasicBlock*, ETNode*> ETMapType;
|
|
|
|
|
2006-01-14 20:55:09 +00:00
|
|
|
void updateDFSNumbers();
|
|
|
|
|
2006-01-08 08:22:18 +00:00
|
|
|
/// dominates - Return true if A dominates B.
|
|
|
|
///
|
2006-01-14 20:55:09 +00:00
|
|
|
inline bool dominates(BasicBlock *A, BasicBlock *B) {
|
2006-01-08 08:22:18 +00:00
|
|
|
if (A == B)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ETNode *NodeA = getNode(A);
|
|
|
|
ETNode *NodeB = getNode(B);
|
|
|
|
|
|
|
|
if (DFSInfoValid)
|
|
|
|
return NodeB->DominatedBy(NodeA);
|
2006-01-14 20:55:09 +00:00
|
|
|
else {
|
|
|
|
// 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();
|
|
|
|
return NodeB->DominatedBy(NodeA);
|
|
|
|
}
|
2006-01-08 08:22:18 +00:00
|
|
|
return NodeB->DominatedBySlow(NodeA);
|
2006-01-14 20:55:09 +00:00
|
|
|
}
|
2006-01-08 08:22:18 +00:00
|
|
|
}
|
|
|
|
|
2007-04-09 00:52:49 +00:00
|
|
|
// dominates - Return true if A dominates B. This performs the
|
2007-03-20 20:19:48 +00:00
|
|
|
// special checks necessary if A and B are in the same basic block.
|
|
|
|
bool dominates(Instruction *A, Instruction *B);
|
|
|
|
|
2006-01-08 08:22:18 +00:00
|
|
|
/// properlyDominates - Return true if A dominates B and A != B.
|
|
|
|
///
|
2006-01-14 20:55:09 +00:00
|
|
|
bool properlyDominates(BasicBlock *A, BasicBlock *B) {
|
2006-01-08 08:22:18 +00:00
|
|
|
return dominates(A, B) && A != B;
|
|
|
|
}
|
|
|
|
|
2007-04-09 00:52:49 +00:00
|
|
|
/// isReachableFromEntry - Return true if A is dominated by the entry
|
|
|
|
/// block of the function containing it.
|
2007-04-09 04:07:36 +00:00
|
|
|
const bool isReachableFromEntry(BasicBlock* A);
|
2007-04-09 00:52:49 +00:00
|
|
|
|
2006-01-08 08:22:18 +00:00
|
|
|
/// Return the nearest common dominator of A and B.
|
|
|
|
BasicBlock *nearestCommonDominator(BasicBlock *A, BasicBlock *B) const {
|
|
|
|
ETNode *NodeA = getNode(A);
|
|
|
|
ETNode *NodeB = getNode(B);
|
|
|
|
|
|
|
|
ETNode *Common = NodeA->NCA(NodeB);
|
|
|
|
if (!Common)
|
|
|
|
return NULL;
|
|
|
|
return Common->getData<BasicBlock>();
|
|
|
|
}
|
2007-04-18 04:38:39 +00:00
|
|
|
|
|
|
|
/// Return the immediate dominator of A.
|
2007-04-20 05:44:16 +00:00
|
|
|
BasicBlock *getIDom(BasicBlock *A) const {
|
2007-04-18 04:38:39 +00:00
|
|
|
ETNode *NodeA = getNode(A);
|
2007-04-21 07:04:45 +00:00
|
|
|
if (!NodeA) return 0;
|
2007-04-18 04:38:39 +00:00
|
|
|
const ETNode *idom = NodeA->getFather();
|
|
|
|
return idom ? idom->getData<BasicBlock>() : 0;
|
|
|
|
}
|
2007-04-18 05:25:09 +00:00
|
|
|
|
2007-04-20 05:44:16 +00:00
|
|
|
void getChildren(BasicBlock *A, std::vector<BasicBlock*>& children) const {
|
2007-04-18 05:25:09 +00:00
|
|
|
ETNode *NodeA = getNode(A);
|
2007-04-21 07:04:45 +00:00
|
|
|
if (!NodeA) return;
|
2007-04-18 05:25:09 +00:00
|
|
|
const ETNode* son = NodeA->getSon();
|
|
|
|
|
|
|
|
if (!son) return;
|
|
|
|
children.push_back(son->getData<BasicBlock>());
|
|
|
|
|
|
|
|
const ETNode* brother = son->getBrother();
|
|
|
|
while (brother != son) {
|
|
|
|
children.push_back(brother->getData<BasicBlock>());
|
|
|
|
brother = brother->getBrother();
|
|
|
|
}
|
|
|
|
}
|
2006-01-08 08:22:18 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2007-04-14 23:49:24 +00:00
|
|
|
AU.addRequired<DominatorTree>();
|
2006-01-08 08:22:18 +00:00
|
|
|
}
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// API to update Forest information based on modifications
|
|
|
|
// to the CFG...
|
|
|
|
|
|
|
|
/// addNewBlock - Add a new block to the CFG, with the specified immediate
|
|
|
|
/// dominator.
|
|
|
|
///
|
|
|
|
void addNewBlock(BasicBlock *BB, BasicBlock *IDom);
|
|
|
|
|
|
|
|
/// setImmediateDominator - Update the immediate dominator information to
|
|
|
|
/// change the current immediate dominator for the specified block
|
|
|
|
/// to another block. This method requires that BB for NewIDom
|
|
|
|
/// already have an ETNode, otherwise just use addNewBlock.
|
|
|
|
///
|
|
|
|
void setImmediateDominator(BasicBlock *BB, BasicBlock *NewIDom);
|
|
|
|
/// print - Convert to human readable form
|
|
|
|
///
|
|
|
|
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);
|
|
|
|
}
|
2006-01-08 08:22:18 +00:00
|
|
|
protected:
|
|
|
|
/// getNode - return the (Post)DominatorTree node for the specified basic
|
|
|
|
/// block. This is the same as using operator[] on this class.
|
|
|
|
///
|
|
|
|
inline ETNode *getNode(BasicBlock *BB) const {
|
|
|
|
ETMapType::const_iterator i = Nodes.find(BB);
|
|
|
|
return (i != Nodes.end()) ? i->second : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ETNode *operator[](BasicBlock *BB) const {
|
|
|
|
return getNode(BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
ETMapType Nodes;
|
|
|
|
bool DFSInfoValid;
|
2006-01-14 20:55:09 +00:00
|
|
|
unsigned int SlowQueries;
|
2006-01-08 08:22:18 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
//==-------------------------------------
|
|
|
|
/// ETForest Class - Concrete subclass of ETForestBase that is used to
|
|
|
|
/// compute a forwards ET-Forest.
|
|
|
|
|
2006-05-19 17:17:12 +00:00
|
|
|
class ETForest : public ETForestBase {
|
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Pass identifcation, replacement for typeid
|
2007-05-01 21:15:47 +00:00
|
|
|
|
|
|
|
ETForest() : ETForestBase((intptr_t)&ID, false) {}
|
2006-01-08 08:22:18 +00:00
|
|
|
|
|
|
|
BasicBlock *getRoot() const {
|
|
|
|
assert(Roots.size() == 1 && "Should always have entry node!");
|
|
|
|
return Roots[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
reset(); // Reset from the last time we were run...
|
2007-04-14 23:49:24 +00:00
|
|
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
|
|
|
Roots = DT.getRoots();
|
|
|
|
calculate(DT);
|
2006-01-08 08:22:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-04-14 23:49:24 +00:00
|
|
|
void calculate(const DominatorTree &DT);
|
2006-01-08 08:22:18 +00:00
|
|
|
ETNode *getNodeForBlock(BasicBlock *BB);
|
|
|
|
};
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
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() :
|
|
|
|
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
|
|
|
}
|
|
|
|
private:
|
|
|
|
const DomSetType &calculate(const DominatorTree &DT,
|
|
|
|
const DominatorTree::Node *Node);
|
|
|
|
};
|
|
|
|
|
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
|