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
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-02 05:45:17 +00:00
|
|
|
//
|
2011-01-02 22:09:33 +00:00
|
|
|
// This file defines the DominatorTree class, which provides fast and efficient
|
|
|
|
// dominance queries.
|
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"
|
2007-10-23 21:04:37 +00:00
|
|
|
#include "llvm/Function.h"
|
2007-10-16 19:59:25 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2009-10-18 04:05:53 +00:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2007-10-23 21:04:37 +00:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2007-10-16 19:59:25 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-10-23 21:04:37 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-10-16 19:59:25 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-08-23 05:17:37 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-10-08 07:44:39 +00:00
|
|
|
#include <algorithm>
|
2003-06-30 21:59:07 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
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.
|
|
|
|
///
|
2007-10-16 19:59:25 +00:00
|
|
|
template <class NodeT>
|
2007-10-23 21:42:49 +00:00
|
|
|
class DominatorBase {
|
2001-07-06 16:57:21 +00:00
|
|
|
protected:
|
2007-10-16 19:59:25 +00:00
|
|
|
std::vector<NodeT*> Roots;
|
2002-01-30 23:27:55 +00:00
|
|
|
const bool IsPostDominators;
|
2008-03-25 22:06:05 +00:00
|
|
|
inline explicit DominatorBase(bool isPostDom) :
|
2007-10-23 21:42:49 +00:00
|
|
|
Roots(), IsPostDominators(isPostDom) {}
|
2001-07-06 16:57:21 +00:00
|
|
|
public:
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2010-03-01 17:47:21 +00:00
|
|
|
/// getRoots - Return the root blocks of the current CFG. This may include
|
2004-03-11 23:08:20 +00:00
|
|
|
/// multiple blocks if we are computing post dominators. For forward
|
|
|
|
/// dominators, this will always be a single block (the entry node).
|
|
|
|
///
|
2007-10-16 19:59:25 +00:00
|
|
|
inline const std::vector<NodeT*> &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-10-16 19:59:25 +00:00
|
|
|
template<class NodeT> class DominatorTreeBase;
|
2007-10-17 14:56:40 +00:00
|
|
|
struct PostDominatorTree;
|
2007-10-08 07:44:39 +00:00
|
|
|
class MachineBasicBlock;
|
|
|
|
|
|
|
|
template <class NodeT>
|
|
|
|
class DomTreeNodeBase {
|
|
|
|
NodeT *TheBB;
|
|
|
|
DomTreeNodeBase<NodeT> *IDom;
|
|
|
|
std::vector<DomTreeNodeBase<NodeT> *> Children;
|
2007-06-12 00:14:41 +00:00
|
|
|
int DFSNumIn, DFSNumOut;
|
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
template<class N> friend class DominatorTreeBase;
|
2007-10-17 14:56:40 +00:00
|
|
|
friend struct PostDominatorTree;
|
2007-06-04 00:32:22 +00:00
|
|
|
public:
|
2007-10-08 07:44:39 +00:00
|
|
|
typedef typename std::vector<DomTreeNodeBase<NodeT> *>::iterator iterator;
|
|
|
|
typedef typename std::vector<DomTreeNodeBase<NodeT> *>::const_iterator
|
|
|
|
const_iterator;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-06-04 00:32:22 +00:00
|
|
|
iterator begin() { return Children.begin(); }
|
|
|
|
iterator end() { return Children.end(); }
|
|
|
|
const_iterator begin() const { return Children.begin(); }
|
|
|
|
const_iterator end() const { return Children.end(); }
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-08 07:44:39 +00:00
|
|
|
NodeT *getBlock() const { return TheBB; }
|
|
|
|
DomTreeNodeBase<NodeT> *getIDom() const { return IDom; }
|
|
|
|
const std::vector<DomTreeNodeBase<NodeT>*> &getChildren() const {
|
|
|
|
return Children;
|
|
|
|
}
|
2008-07-01 21:41:00 +00:00
|
|
|
|
2007-10-08 07:44:39 +00:00
|
|
|
DomTreeNodeBase(NodeT *BB, DomTreeNodeBase<NodeT> *iDom)
|
2007-06-12 00:54:38 +00:00
|
|
|
: TheBB(BB), IDom(iDom), DFSNumIn(-1), DFSNumOut(-1) { }
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-08 07:44:39 +00:00
|
|
|
DomTreeNodeBase<NodeT> *addChild(DomTreeNodeBase<NodeT> *C) {
|
|
|
|
Children.push_back(C);
|
|
|
|
return C;
|
|
|
|
}
|
2008-07-01 17:44:24 +00:00
|
|
|
|
2008-04-07 09:59:07 +00:00
|
|
|
size_t getNumChildren() const {
|
|
|
|
return Children.size();
|
|
|
|
}
|
2008-07-01 21:41:00 +00:00
|
|
|
|
|
|
|
void clearAllChildren() {
|
|
|
|
Children.clear();
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2008-07-01 21:41:00 +00:00
|
|
|
bool compare(DomTreeNodeBase<NodeT> *Other) {
|
|
|
|
if (getNumChildren() != Other->getNumChildren())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
SmallPtrSet<NodeT *, 4> OtherChildren;
|
2010-03-24 00:27:49 +00:00
|
|
|
for (iterator I = Other->begin(), E = Other->end(); I != E; ++I) {
|
2008-07-01 21:41:00 +00:00
|
|
|
NodeT *Nd = (*I)->getBlock();
|
|
|
|
OtherChildren.insert(Nd);
|
|
|
|
}
|
|
|
|
|
2010-03-24 00:27:49 +00:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I) {
|
2008-07-01 21:41:00 +00:00
|
|
|
NodeT *N = (*I)->getBlock();
|
|
|
|
if (OtherChildren.count(N) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-10-08 07:44:39 +00:00
|
|
|
void setIDom(DomTreeNodeBase<NodeT> *NewIDom) {
|
|
|
|
assert(IDom && "No immediate dominator?");
|
|
|
|
if (IDom != NewIDom) {
|
2007-10-29 04:50:50 +00:00
|
|
|
typename std::vector<DomTreeNodeBase<NodeT>*>::iterator I =
|
2007-10-08 07:44:39 +00:00
|
|
|
std::find(IDom->Children.begin(), IDom->Children.end(), this);
|
|
|
|
assert(I != IDom->Children.end() &&
|
|
|
|
"Not in immediate dominator children set!");
|
|
|
|
// I am no longer your child...
|
|
|
|
IDom->Children.erase(I);
|
|
|
|
|
|
|
|
// Switch to new dominator
|
|
|
|
IDom = NewIDom;
|
|
|
|
IDom->Children.push_back(this);
|
|
|
|
}
|
|
|
|
}
|
2009-08-27 17:29:49 +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-10-08 07:44:39 +00:00
|
|
|
bool DominatedBy(const DomTreeNodeBase<NodeT> *other) const {
|
2007-06-12 00:14:41 +00:00
|
|
|
return this->DFSNumIn >= other->DFSNumIn &&
|
|
|
|
this->DFSNumOut <= other->DFSNumOut;
|
|
|
|
}
|
2007-06-04 00:32:22 +00:00
|
|
|
};
|
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
|
2007-10-31 03:30:14 +00:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
|
2007-10-16 19:59:25 +00:00
|
|
|
|
|
|
|
template<class NodeT>
|
2009-08-23 05:17:37 +00:00
|
|
|
static raw_ostream &operator<<(raw_ostream &o,
|
|
|
|
const DomTreeNodeBase<NodeT> *Node) {
|
2007-10-16 19:59:25 +00:00
|
|
|
if (Node->getBlock())
|
|
|
|
WriteAsOperand(o, Node->getBlock(), false);
|
|
|
|
else
|
|
|
|
o << " <<exit node>>";
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
o << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "}";
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
return o << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NodeT>
|
2009-08-23 05:17:37 +00:00
|
|
|
static void PrintDomTree(const DomTreeNodeBase<NodeT> *N, raw_ostream &o,
|
2007-10-16 19:59:25 +00:00
|
|
|
unsigned Lev) {
|
2009-08-23 05:17:37 +00:00
|
|
|
o.indent(2*Lev) << "[" << Lev << "] " << N;
|
2007-10-16 19:59:25 +00:00
|
|
|
for (typename DomTreeNodeBase<NodeT>::const_iterator I = N->begin(),
|
|
|
|
E = N->end(); I != E; ++I)
|
|
|
|
PrintDomTree<NodeT>(*I, o, Lev+1);
|
|
|
|
}
|
|
|
|
|
2007-10-08 07:44:39 +00:00
|
|
|
typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
|
|
|
|
|
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-10-16 19:59:25 +00:00
|
|
|
|
2007-10-25 00:16:57 +00:00
|
|
|
template<class FuncT, class N>
|
|
|
|
void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT,
|
|
|
|
FuncT& F);
|
2007-10-17 02:03:17 +00:00
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
template<class NodeT>
|
|
|
|
class DominatorTreeBase : public DominatorBase<NodeT> {
|
2003-12-07 00:36:16 +00:00
|
|
|
protected:
|
2007-10-16 19:59:25 +00:00
|
|
|
typedef DenseMap<NodeT*, DomTreeNodeBase<NodeT>*> DomTreeNodeMapType;
|
2007-06-07 17:47:21 +00:00
|
|
|
DomTreeNodeMapType DomTreeNodes;
|
2007-10-16 19:59:25 +00:00
|
|
|
DomTreeNodeBase<NodeT> *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 {
|
2008-04-16 04:21:16 +00:00
|
|
|
unsigned DFSNum;
|
2011-01-23 06:16:06 +00:00
|
|
|
unsigned Parent;
|
2006-03-20 19:32:48 +00:00
|
|
|
unsigned Semi;
|
2011-01-23 05:11:18 +00:00
|
|
|
NodeT *Label;
|
2007-04-15 08:47:27 +00:00
|
|
|
|
2011-01-23 06:16:06 +00:00
|
|
|
InfoRec() : DFSNum(0), Parent(0), Semi(0), Label(0) {}
|
2006-03-20 19:32:48 +00:00
|
|
|
};
|
2007-04-15 08:47:27 +00:00
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
DenseMap<NodeT*, NodeT*> IDoms;
|
2006-03-20 19:32:48 +00:00
|
|
|
|
|
|
|
// Vertex - Map the DFS number to the BasicBlock*
|
2007-10-16 19:59:25 +00:00
|
|
|
std::vector<NodeT*> 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-10-16 19:59:25 +00:00
|
|
|
DenseMap<NodeT*, InfoRec> Info;
|
|
|
|
|
|
|
|
void reset() {
|
2010-03-01 17:47:21 +00:00
|
|
|
for (typename DomTreeNodeMapType::iterator I = this->DomTreeNodes.begin(),
|
2007-10-16 19:59:25 +00:00
|
|
|
E = DomTreeNodes.end(); I != E; ++I)
|
|
|
|
delete I->second;
|
|
|
|
DomTreeNodes.clear();
|
|
|
|
IDoms.clear();
|
|
|
|
this->Roots.clear();
|
|
|
|
Vertex.clear();
|
|
|
|
RootNode = 0;
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-18 05:13:52 +00:00
|
|
|
// NewBB is split and now it has one successor. Update dominator tree to
|
|
|
|
// reflect this change.
|
|
|
|
template<class N, class GraphT>
|
|
|
|
void Split(DominatorTreeBase<typename GraphT::NodeType>& DT,
|
|
|
|
typename GraphT::NodeType* NewBB) {
|
2010-03-24 00:27:49 +00:00
|
|
|
assert(std::distance(GraphT::child_begin(NewBB),
|
|
|
|
GraphT::child_end(NewBB)) == 1 &&
|
|
|
|
"NewBB should have a single successor!");
|
2007-10-18 05:13:52 +00:00
|
|
|
typename GraphT::NodeType* NewBBSucc = *GraphT::child_begin(NewBB);
|
|
|
|
|
|
|
|
std::vector<typename GraphT::NodeType*> PredBlocks;
|
2010-07-09 14:00:56 +00:00
|
|
|
typedef GraphTraits<Inverse<N> > InvTraits;
|
|
|
|
for (typename InvTraits::ChildIteratorType PI =
|
|
|
|
InvTraits::child_begin(NewBB),
|
|
|
|
PE = InvTraits::child_end(NewBB); PI != PE; ++PI)
|
2010-03-01 17:47:21 +00:00
|
|
|
PredBlocks.push_back(*PI);
|
2007-10-18 05:13:52 +00:00
|
|
|
|
2010-07-09 14:00:56 +00:00
|
|
|
assert(!PredBlocks.empty() && "No predblocks?");
|
2009-05-21 21:08:47 +00:00
|
|
|
|
|
|
|
bool NewBBDominatesNewBBSucc = true;
|
2010-07-09 14:00:56 +00:00
|
|
|
for (typename InvTraits::ChildIteratorType PI =
|
|
|
|
InvTraits::child_begin(NewBBSucc),
|
|
|
|
E = InvTraits::child_end(NewBBSucc); PI != E; ++PI) {
|
2010-07-09 14:46:49 +00:00
|
|
|
typename InvTraits::NodeType *ND = *PI;
|
|
|
|
if (ND != NewBB && !DT.dominates(NewBBSucc, ND) &&
|
|
|
|
DT.isReachableFromEntry(ND)) {
|
2009-05-21 21:47:54 +00:00
|
|
|
NewBBDominatesNewBBSucc = false;
|
|
|
|
break;
|
2009-05-21 21:08:47 +00:00
|
|
|
}
|
2010-07-09 14:00:56 +00:00
|
|
|
}
|
2009-05-21 21:08:47 +00:00
|
|
|
|
2007-10-18 05:13:52 +00:00
|
|
|
// Find NewBB's immediate dominator and create new dominator tree node for
|
|
|
|
// NewBB.
|
2007-10-29 04:50:50 +00:00
|
|
|
NodeT *NewBBIDom = 0;
|
2007-10-18 05:13:52 +00:00
|
|
|
unsigned i = 0;
|
|
|
|
for (i = 0; i < PredBlocks.size(); ++i)
|
|
|
|
if (DT.isReachableFromEntry(PredBlocks[i])) {
|
|
|
|
NewBBIDom = PredBlocks[i];
|
|
|
|
break;
|
|
|
|
}
|
2009-06-03 21:42:06 +00:00
|
|
|
|
|
|
|
// It's possible that none of the predecessors of NewBB are reachable;
|
|
|
|
// in that case, NewBB itself is unreachable, so nothing needs to be
|
|
|
|
// changed.
|
|
|
|
if (!NewBBIDom)
|
|
|
|
return;
|
|
|
|
|
2007-10-18 05:13:52 +00:00
|
|
|
for (i = i + 1; i < PredBlocks.size(); ++i) {
|
|
|
|
if (DT.isReachableFromEntry(PredBlocks[i]))
|
|
|
|
NewBBIDom = DT.findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new dominator tree node... and set the idom of NewBB.
|
2007-10-29 04:50:50 +00:00
|
|
|
DomTreeNodeBase<NodeT> *NewBBNode = DT.addNewBlock(NewBB, NewBBIDom);
|
2007-10-18 05:13:52 +00:00
|
|
|
|
|
|
|
// If NewBB strictly dominates other blocks, then it is now the immediate
|
|
|
|
// dominator of NewBBSucc. Update the dominator tree as appropriate.
|
|
|
|
if (NewBBDominatesNewBBSucc) {
|
2007-10-29 04:50:50 +00:00
|
|
|
DomTreeNodeBase<NodeT> *NewBBSuccNode = DT.getNode(NewBBSucc);
|
2007-10-18 05:13:52 +00:00
|
|
|
DT.changeImmediateDominator(NewBBSuccNode, NewBBNode);
|
|
|
|
}
|
|
|
|
}
|
2003-09-10 20:36:51 +00:00
|
|
|
|
2007-08-08 05:51:24 +00:00
|
|
|
public:
|
2008-03-25 22:06:05 +00:00
|
|
|
explicit DominatorTreeBase(bool isPostDom)
|
2007-10-23 21:42:49 +00:00
|
|
|
: DominatorBase<NodeT>(isPostDom), DFSInfoValid(false), SlowQueries(0) {}
|
|
|
|
virtual ~DominatorTreeBase() { reset(); }
|
2002-07-26 18:40:06 +00:00
|
|
|
|
2008-07-01 19:50:56 +00:00
|
|
|
/// compare - Return false if the other dominator tree base matches this
|
2008-07-01 17:44:24 +00:00
|
|
|
/// dominator tree base. Otherwise return true.
|
|
|
|
bool compare(DominatorTreeBase &Other) const {
|
|
|
|
|
2008-07-01 21:41:00 +00:00
|
|
|
const DomTreeNodeMapType &OtherDomTreeNodes = Other.DomTreeNodes;
|
|
|
|
if (DomTreeNodes.size() != OtherDomTreeNodes.size())
|
|
|
|
return true;
|
|
|
|
|
2010-03-01 17:47:21 +00:00
|
|
|
for (typename DomTreeNodeMapType::const_iterator
|
2008-07-01 17:44:24 +00:00
|
|
|
I = this->DomTreeNodes.begin(),
|
|
|
|
E = this->DomTreeNodes.end(); I != E; ++I) {
|
2008-07-01 21:41:00 +00:00
|
|
|
NodeT *BB = I->first;
|
|
|
|
typename DomTreeNodeMapType::const_iterator OI = OtherDomTreeNodes.find(BB);
|
|
|
|
if (OI == OtherDomTreeNodes.end())
|
|
|
|
return true;
|
2008-07-01 17:44:24 +00:00
|
|
|
|
2008-07-01 21:41:00 +00:00
|
|
|
DomTreeNodeBase<NodeT>* MyNd = I->second;
|
|
|
|
DomTreeNodeBase<NodeT>* OtherNd = OI->second;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2008-07-01 21:41:00 +00:00
|
|
|
if (MyNd->compare(OtherNd))
|
2008-07-01 17:44:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-01 21:41:00 +00:00
|
|
|
|
2008-07-01 17:44:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-07-26 18:40:06 +00:00
|
|
|
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-10-16 19:59:25 +00:00
|
|
|
inline DomTreeNodeBase<NodeT> *getNode(NodeT *BB) const {
|
|
|
|
typename DomTreeNodeMapType::const_iterator I = DomTreeNodes.find(BB);
|
2007-08-04 23:48:07 +00:00
|
|
|
return I != DomTreeNodes.end() ? I->second : 0;
|
2002-07-26 18:40:06 +00:00
|
|
|
}
|
2002-07-27 01:12:15 +00:00
|
|
|
|
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-10-16 19:59:25 +00:00
|
|
|
DomTreeNodeBase<NodeT> *getRootNode() { return RootNode; }
|
|
|
|
const DomTreeNodeBase<NodeT> *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!
|
|
|
|
///
|
2007-10-16 19:59:25 +00:00
|
|
|
bool properlyDominates(const DomTreeNodeBase<NodeT> *A,
|
2009-08-27 17:54:15 +00:00
|
|
|
const DomTreeNodeBase<NodeT> *B) const {
|
2007-06-07 17:47:21 +00:00
|
|
|
if (A == 0 || B == 0) return false;
|
|
|
|
return dominatedBySlowTreeWalk(A, B);
|
|
|
|
}
|
|
|
|
|
2010-09-27 16:33:31 +00:00
|
|
|
inline bool properlyDominates(const NodeT *A, const NodeT *B) {
|
|
|
|
if (A == B)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Cast away the const qualifiers here. This is ok since
|
|
|
|
// this function doesn't actually return the values returned
|
|
|
|
// from getNode.
|
|
|
|
return properlyDominates(getNode(const_cast<NodeT *>(A)),
|
|
|
|
getNode(const_cast<NodeT *>(B)));
|
2007-06-07 21:34:22 +00:00
|
|
|
}
|
|
|
|
|
2010-03-01 17:47:21 +00:00
|
|
|
bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A,
|
2007-10-16 19:59:25 +00:00
|
|
|
const DomTreeNodeBase<NodeT> *B) const {
|
|
|
|
const DomTreeNodeBase<NodeT> *IDom;
|
2007-06-07 17:47:21 +00:00
|
|
|
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.
|
2010-10-06 15:49:14 +00:00
|
|
|
bool isReachableFromEntry(const NodeT* A) {
|
2010-03-24 00:27:49 +00:00
|
|
|
assert(!this->isPostDominator() &&
|
|
|
|
"This is not implemented for post dominators");
|
2007-10-29 04:50:50 +00:00
|
|
|
return dominates(&A->getParent()->front(), A);
|
2007-10-16 19:59:25 +00:00
|
|
|
}
|
2009-08-27 17:29:49 +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!
|
|
|
|
///
|
2007-10-16 19:59:25 +00:00
|
|
|
inline bool dominates(const DomTreeNodeBase<NodeT> *A,
|
2009-08-27 17:54:15 +00:00
|
|
|
const DomTreeNodeBase<NodeT> *B) {
|
2010-03-01 17:47:21 +00:00
|
|
|
if (B == A)
|
2007-06-07 18:39:40 +00:00
|
|
|
return true; // A node trivially dominates itself.
|
|
|
|
|
|
|
|
if (A == 0 || B == 0)
|
|
|
|
return false;
|
2007-06-07 17:47:21 +00:00
|
|
|
|
2010-01-07 23:50:41 +00:00
|
|
|
// Compare the result of the tree walk and the dfs numbers, if expensive
|
|
|
|
// checks are enabled.
|
|
|
|
#ifdef XDEBUG
|
2010-03-24 00:27:49 +00:00
|
|
|
assert((!DFSInfoValid ||
|
|
|
|
(dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
|
|
|
|
"Tree walk disagrees with dfs numbers!");
|
2010-01-07 23:50:41 +00:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2009-09-02 17:05:05 +00:00
|
|
|
inline bool dominates(const NodeT *A, const NodeT *B) {
|
2010-03-01 17:47:21 +00:00
|
|
|
if (A == B)
|
2007-06-07 18:39:40 +00:00
|
|
|
return true;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2009-09-02 17:05:05 +00:00
|
|
|
// Cast away the const qualifiers here. This is ok since
|
|
|
|
// this function doesn't actually return the values returned
|
|
|
|
// from getNode.
|
|
|
|
return dominates(getNode(const_cast<NodeT *>(A)),
|
|
|
|
getNode(const_cast<NodeT *>(B)));
|
2007-06-07 18:39:40 +00:00
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
NodeT *getRoot() const {
|
|
|
|
assert(this->Roots.size() == 1 && "Should always have entry node!");
|
|
|
|
return this->Roots[0];
|
|
|
|
}
|
2007-06-07 18:39:40 +00:00
|
|
|
|
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.
|
2007-10-16 19:59:25 +00:00
|
|
|
NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) {
|
2010-03-24 00:27:49 +00:00
|
|
|
assert(A->getParent() == B->getParent() &&
|
|
|
|
"Two blocks are not in same function");
|
2007-10-16 19:59:25 +00:00
|
|
|
|
2010-03-24 00:22:24 +00:00
|
|
|
// If either A or B is a entry block then it is nearest common dominator
|
|
|
|
// (for forward-dominators).
|
|
|
|
if (!this->isPostDominator()) {
|
2010-03-24 00:27:49 +00:00
|
|
|
NodeT &Entry = A->getParent()->front();
|
2010-03-24 00:22:24 +00:00
|
|
|
if (A == &Entry || B == &Entry)
|
|
|
|
return &Entry;
|
|
|
|
}
|
2007-10-16 19:59:25 +00:00
|
|
|
|
|
|
|
// If B dominates A then B is nearest common dominator.
|
|
|
|
if (dominates(B, A))
|
|
|
|
return B;
|
|
|
|
|
|
|
|
// If A dominates B then A is nearest common dominator.
|
|
|
|
if (dominates(A, B))
|
|
|
|
return A;
|
|
|
|
|
|
|
|
DomTreeNodeBase<NodeT> *NodeA = getNode(A);
|
|
|
|
DomTreeNodeBase<NodeT> *NodeB = getNode(B);
|
|
|
|
|
|
|
|
// Collect NodeA dominators set.
|
|
|
|
SmallPtrSet<DomTreeNodeBase<NodeT>*, 16> NodeADoms;
|
|
|
|
NodeADoms.insert(NodeA);
|
|
|
|
DomTreeNodeBase<NodeT> *IDomA = NodeA->getIDom();
|
|
|
|
while (IDomA) {
|
|
|
|
NodeADoms.insert(IDomA);
|
|
|
|
IDomA = IDomA->getIDom();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk NodeB immediate dominators chain and find common dominator node.
|
|
|
|
DomTreeNodeBase<NodeT> *IDomB = NodeB->getIDom();
|
2010-03-24 00:27:49 +00:00
|
|
|
while (IDomB) {
|
2007-10-16 19:59:25 +00:00
|
|
|
if (NodeADoms.count(IDomB) != 0)
|
|
|
|
return IDomB->getBlock();
|
|
|
|
|
|
|
|
IDomB = IDomB->getIDom();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-11 23:31:22 +00:00
|
|
|
|
2010-09-27 16:33:31 +00:00
|
|
|
const NodeT *findNearestCommonDominator(const NodeT *A, const NodeT *B) {
|
|
|
|
// Cast away the const qualifiers here. This is ok since
|
|
|
|
// const is re-introduced on the return type.
|
|
|
|
return findNearestCommonDominator(const_cast<NodeT *>(A),
|
|
|
|
const_cast<NodeT *>(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
|
2010-03-01 17:47:21 +00:00
|
|
|
/// creates a new node as a child of DomBB dominator node,linking it into
|
2007-06-04 16:43:25 +00:00
|
|
|
/// the children list of the immediate dominator.
|
2007-10-16 19:59:25 +00:00
|
|
|
DomTreeNodeBase<NodeT> *addNewBlock(NodeT *BB, NodeT *DomBB) {
|
2002-09-06 02:16:27 +00:00
|
|
|
assert(getNode(BB) == 0 && "Block already in dominator tree!");
|
2007-10-16 19:59:25 +00:00
|
|
|
DomTreeNodeBase<NodeT> *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;
|
2010-03-01 17:47:21 +00:00
|
|
|
return DomTreeNodes[BB] =
|
2007-10-29 04:50:50 +00:00
|
|
|
IDomNode->addChild(new DomTreeNodeBase<NodeT>(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-10-16 19:59:25 +00:00
|
|
|
void changeImmediateDominator(DomTreeNodeBase<NodeT> *N,
|
|
|
|
DomTreeNodeBase<NodeT> *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-10-16 19:59:25 +00:00
|
|
|
void changeImmediateDominator(NodeT *BB, NodeT *NewBB) {
|
2007-06-04 16:22:33 +00:00
|
|
|
changeImmediateDominator(getNode(BB), getNode(NewBB));
|
|
|
|
}
|
|
|
|
|
2010-03-01 17:47:21 +00:00
|
|
|
/// eraseNode - Removes a node from the dominator tree. Block must not
|
2010-09-10 22:25:58 +00:00
|
|
|
/// dominate any other blocks. Removes node from its immediate dominator's
|
2007-08-13 22:10:29 +00:00
|
|
|
/// children list. Deletes dominator node associated with basic block BB.
|
2007-10-16 19:59:25 +00:00
|
|
|
void eraseNode(NodeT *BB) {
|
|
|
|
DomTreeNodeBase<NodeT> *Node = getNode(BB);
|
2010-03-24 00:27:49 +00:00
|
|
|
assert(Node && "Removing node that isn't in dominator tree.");
|
|
|
|
assert(Node->getChildren().empty() && "Node is not a leaf node.");
|
2007-10-16 19:59:25 +00:00
|
|
|
|
|
|
|
// Remove node from immediate dominator's children list.
|
|
|
|
DomTreeNodeBase<NodeT> *IDom = Node->getIDom();
|
|
|
|
if (IDom) {
|
|
|
|
typename std::vector<DomTreeNodeBase<NodeT>*>::iterator I =
|
|
|
|
std::find(IDom->Children.begin(), IDom->Children.end(), Node);
|
|
|
|
assert(I != IDom->Children.end() &&
|
|
|
|
"Not in immediate dominator children set!");
|
|
|
|
// I am no longer your child...
|
|
|
|
IDom->Children.erase(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
DomTreeNodes.erase(BB);
|
|
|
|
delete Node;
|
|
|
|
}
|
2007-08-13 22:10:29 +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.
|
2007-10-16 19:59:25 +00:00
|
|
|
void removeNode(NodeT *BB) {
|
2006-09-12 00:18:28 +00:00
|
|
|
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
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-18 05:13:52 +00:00
|
|
|
/// splitBlock - BB is split and now it has one successor. Update dominator
|
|
|
|
/// tree to reflect this change.
|
|
|
|
void splitBlock(NodeT* NewBB) {
|
|
|
|
if (this->IsPostDominators)
|
2007-10-23 21:04:37 +00:00
|
|
|
this->Split<Inverse<NodeT*>, GraphTraits<Inverse<NodeT*> > >(*this, NewBB);
|
2007-10-18 05:13:52 +00:00
|
|
|
else
|
2007-10-23 21:04:37 +00:00
|
|
|
this->Split<NodeT*, GraphTraits<NodeT*> >(*this, NewBB);
|
2007-10-18 05:13:52 +00:00
|
|
|
}
|
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
|
|
|
///
|
2009-08-23 05:17:37 +00:00
|
|
|
void print(raw_ostream &o) const {
|
2007-10-16 19:59:25 +00:00
|
|
|
o << "=============================--------------------------------\n";
|
2008-02-27 18:38:29 +00:00
|
|
|
if (this->isPostDominator())
|
|
|
|
o << "Inorder PostDominator Tree: ";
|
|
|
|
else
|
|
|
|
o << "Inorder Dominator Tree: ";
|
2011-01-09 16:00:09 +00:00
|
|
|
if (!this->DFSInfoValid)
|
2007-10-16 19:59:25 +00:00
|
|
|
o << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
|
|
|
|
o << "\n";
|
|
|
|
|
2009-09-08 01:22:54 +00:00
|
|
|
// The postdom tree can have a null root if there are no returns.
|
|
|
|
if (getRootNode())
|
|
|
|
PrintDomTree<NodeT>(getRootNode(), o, 1);
|
2007-10-16 19:59:25 +00:00
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-08-08 05:51:24 +00:00
|
|
|
protected:
|
2007-10-16 22:59:15 +00:00
|
|
|
template<class GraphT>
|
|
|
|
friend typename GraphT::NodeType* Eval(
|
|
|
|
DominatorTreeBase<typename GraphT::NodeType>& DT,
|
2011-01-23 06:16:06 +00:00
|
|
|
typename GraphT::NodeType* V,
|
|
|
|
unsigned LastLinked);
|
2007-10-16 22:59:15 +00:00
|
|
|
|
|
|
|
template<class GraphT>
|
|
|
|
friend unsigned DFSPass(DominatorTreeBase<typename GraphT::NodeType>& DT,
|
|
|
|
typename GraphT::NodeType* V,
|
|
|
|
unsigned N);
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-25 00:16:57 +00:00
|
|
|
template<class FuncT, class N>
|
|
|
|
friend void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT,
|
|
|
|
FuncT& F);
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-08-08 05:51:24 +00:00
|
|
|
/// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
|
|
|
|
/// dominator tree in dfs order.
|
2007-10-16 19:59:25 +00:00
|
|
|
void updateDFSNumbers() {
|
|
|
|
unsigned DFSNum = 0;
|
|
|
|
|
|
|
|
SmallVector<std::pair<DomTreeNodeBase<NodeT>*,
|
|
|
|
typename DomTreeNodeBase<NodeT>::iterator>, 32> WorkStack;
|
|
|
|
|
2010-01-07 23:50:06 +00:00
|
|
|
DomTreeNodeBase<NodeT> *ThisRoot = getRootNode();
|
|
|
|
|
|
|
|
if (!ThisRoot)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Even in the case of multiple exits that form the post dominator root
|
|
|
|
// nodes, do not iterate over all exits, but start from the virtual root
|
|
|
|
// node. Otherwise bbs, that are not post dominated by any exit but by the
|
|
|
|
// virtual root node, will never be assigned a DFS number.
|
|
|
|
WorkStack.push_back(std::make_pair(ThisRoot, ThisRoot->begin()));
|
|
|
|
ThisRoot->DFSNumIn = DFSNum++;
|
|
|
|
|
|
|
|
while (!WorkStack.empty()) {
|
|
|
|
DomTreeNodeBase<NodeT> *Node = WorkStack.back().first;
|
|
|
|
typename DomTreeNodeBase<NodeT>::iterator ChildIt =
|
|
|
|
WorkStack.back().second;
|
|
|
|
|
|
|
|
// If we visited all of the children of this node, "recurse" back up the
|
|
|
|
// stack setting the DFOutNum.
|
|
|
|
if (ChildIt == Node->end()) {
|
|
|
|
Node->DFSNumOut = DFSNum++;
|
|
|
|
WorkStack.pop_back();
|
|
|
|
} else {
|
|
|
|
// Otherwise, recursively visit this child.
|
|
|
|
DomTreeNodeBase<NodeT> *Child = *ChildIt;
|
|
|
|
++WorkStack.back().second;
|
|
|
|
|
|
|
|
WorkStack.push_back(std::make_pair(Child, Child->begin()));
|
|
|
|
Child->DFSNumIn = DFSNum++;
|
2007-10-16 19:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
SlowQueries = 0;
|
|
|
|
DFSInfoValid = true;
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
DomTreeNodeBase<NodeT> *getNodeForBlock(NodeT *BB) {
|
2009-07-02 00:17:47 +00:00
|
|
|
typename DomTreeNodeMapType::iterator I = this->DomTreeNodes.find(BB);
|
|
|
|
if (I != this->DomTreeNodes.end() && I->second)
|
|
|
|
return I->second;
|
2007-10-16 19:59:25 +00:00
|
|
|
|
|
|
|
// Haven't calculated this node yet? Get or calculate the node for the
|
|
|
|
// immediate dominator.
|
|
|
|
NodeT *IDom = getIDom(BB);
|
2008-04-16 04:21:16 +00:00
|
|
|
|
2008-05-04 21:07:35 +00:00
|
|
|
assert(IDom || this->DomTreeNodes[NULL]);
|
2007-10-16 19:59:25 +00:00
|
|
|
DomTreeNodeBase<NodeT> *IDomNode = getNodeForBlock(IDom);
|
|
|
|
|
|
|
|
// Add a new tree node for this BasicBlock, and link it as a child of
|
|
|
|
// IDomNode
|
|
|
|
DomTreeNodeBase<NodeT> *C = new DomTreeNodeBase<NodeT>(BB, IDomNode);
|
|
|
|
return this->DomTreeNodes[BB] = IDomNode->addChild(C);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
inline NodeT *getIDom(NodeT *BB) const {
|
|
|
|
typename DenseMap<NodeT*, NodeT*>::const_iterator I = IDoms.find(BB);
|
2007-09-23 21:31:44 +00:00
|
|
|
return I != IDoms.end() ? I->second : 0;
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-31 03:30:14 +00:00
|
|
|
inline void addRoot(NodeT* BB) {
|
2008-04-16 04:21:16 +00:00
|
|
|
this->Roots.push_back(BB);
|
2007-10-31 03:30:14 +00:00
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
public:
|
|
|
|
/// recalculate - compute a dominator tree for the given function
|
2007-10-25 00:16:57 +00:00
|
|
|
template<class FT>
|
|
|
|
void recalculate(FT& F) {
|
2010-01-07 23:50:25 +00:00
|
|
|
reset();
|
|
|
|
this->Vertex.push_back(0);
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2010-01-07 23:50:25 +00:00
|
|
|
if (!this->IsPostDominators) {
|
|
|
|
// Initialize root
|
2007-10-31 03:30:14 +00:00
|
|
|
this->Roots.push_back(&F.front());
|
|
|
|
this->IDoms[&F.front()] = 0;
|
|
|
|
this->DomTreeNodes[&F.front()] = 0;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-25 00:16:57 +00:00
|
|
|
Calculate<FT, NodeT*>(*this, F);
|
2007-10-23 21:04:37 +00:00
|
|
|
} else {
|
|
|
|
// Initialize the roots list
|
2007-10-31 03:30:14 +00:00
|
|
|
for (typename FT::iterator I = F.begin(), E = F.end(); I != E; ++I) {
|
|
|
|
if (std::distance(GraphTraits<FT*>::child_begin(I),
|
|
|
|
GraphTraits<FT*>::child_end(I)) == 0)
|
|
|
|
addRoot(I);
|
2007-10-23 21:04:37 +00:00
|
|
|
|
|
|
|
// Prepopulate maps so that we don't get iterator invalidation issues later.
|
|
|
|
this->IDoms[I] = 0;
|
|
|
|
this->DomTreeNodes[I] = 0;
|
|
|
|
}
|
|
|
|
|
2007-10-25 00:16:57 +00:00
|
|
|
Calculate<FT, Inverse<NodeT*> >(*this, F);
|
2007-10-23 21:04:37 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-26 18:40:06 +00:00
|
|
|
};
|
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
|
|
|
|
|
2006-10-03 05:24:56 +00:00
|
|
|
//===-------------------------------------
|
|
|
|
/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
|
|
|
|
/// compute a normal dominator tree.
|
|
|
|
///
|
2007-10-23 21:04:37 +00:00
|
|
|
class DominatorTree : public FunctionPass {
|
2006-10-03 05:24:56 +00:00
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2007-10-23 21:04:37 +00:00
|
|
|
DominatorTreeBase<BasicBlock>* DT;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2010-08-06 18:33:48 +00:00
|
|
|
DominatorTree() : FunctionPass(ID) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeDominatorTreePass(*PassRegistry::getPassRegistry());
|
2007-10-23 21:42:49 +00:00
|
|
|
DT = new DominatorTreeBase<BasicBlock>(false);
|
2007-10-23 21:04:37 +00:00
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
~DominatorTree() {
|
|
|
|
delete DT;
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-11-27 03:33:40 +00:00
|
|
|
DominatorTreeBase<BasicBlock>& getBase() { return *DT; }
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2010-03-01 17:47:21 +00:00
|
|
|
/// getRoots - Return the root blocks of the current CFG. This may include
|
2007-10-23 21:04:37 +00:00
|
|
|
/// multiple blocks if we are computing post dominators. For forward
|
|
|
|
/// dominators, this will always be a single block (the entry node).
|
|
|
|
///
|
|
|
|
inline const std::vector<BasicBlock*> &getRoots() const {
|
|
|
|
return DT->getRoots();
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
inline BasicBlock *getRoot() const {
|
|
|
|
return DT->getRoot();
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
inline DomTreeNode *getRootNode() const {
|
|
|
|
return DT->getRootNode();
|
2006-10-03 05:24:56 +00:00
|
|
|
}
|
2008-07-01 17:44:24 +00:00
|
|
|
|
2008-07-01 19:50:56 +00:00
|
|
|
/// compare - Return false if the other dominator tree matches this
|
2008-07-01 17:44:24 +00:00
|
|
|
/// dominator tree. Otherwise return true.
|
|
|
|
inline bool compare(DominatorTree &Other) const {
|
|
|
|
DomTreeNode *R = getRootNode();
|
|
|
|
DomTreeNode *OtherR = Other.getRootNode();
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2008-07-01 17:44:24 +00:00
|
|
|
if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
|
|
|
|
return true;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2008-07-01 17:44:24 +00:00
|
|
|
if (DT->compare(Other.getBase()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-04-15 23:14:18 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2009-09-28 00:27:48 +00:00
|
|
|
virtual void verifyAnalysis() const;
|
|
|
|
|
2006-10-03 05:24:56 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2010-12-22 22:10:08 +00:00
|
|
|
inline bool dominates(const DomTreeNode* A, const DomTreeNode* B) const {
|
2007-10-23 21:04:37 +00:00
|
|
|
return DT->dominates(A, B);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2009-09-02 17:05:05 +00:00
|
|
|
inline bool dominates(const BasicBlock* A, const BasicBlock* B) const {
|
2007-10-23 21:04:37 +00:00
|
|
|
return DT->dominates(A, B);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +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.
|
2009-09-21 22:30:50 +00:00
|
|
|
bool dominates(const Instruction *A, const Instruction *B) const;
|
2007-10-23 21:04:37 +00:00
|
|
|
|
2009-09-21 22:30:50 +00:00
|
|
|
bool properlyDominates(const DomTreeNode *A, const DomTreeNode *B) const {
|
2007-10-23 21:04:37 +00:00
|
|
|
return DT->properlyDominates(A, B);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2010-09-27 16:33:31 +00:00
|
|
|
bool properlyDominates(const BasicBlock *A, const BasicBlock *B) const {
|
2007-10-23 21:04:37 +00:00
|
|
|
return DT->properlyDominates(A, B);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
/// findNearestCommonDominator - Find nearest common dominator basic block
|
|
|
|
/// for basic block A and B. If there is no such block then return NULL.
|
|
|
|
inline BasicBlock *findNearestCommonDominator(BasicBlock *A, BasicBlock *B) {
|
|
|
|
return DT->findNearestCommonDominator(A, B);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2010-09-27 16:33:31 +00:00
|
|
|
inline const BasicBlock *findNearestCommonDominator(const BasicBlock *A,
|
|
|
|
const BasicBlock *B) {
|
|
|
|
return DT->findNearestCommonDominator(A, B);
|
|
|
|
}
|
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
inline DomTreeNode *operator[](BasicBlock *BB) const {
|
|
|
|
return DT->getNode(BB);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
/// getNode - return the (Post)DominatorTree node for the specified basic
|
|
|
|
/// block. This is the same as using operator[] on this class.
|
|
|
|
///
|
|
|
|
inline DomTreeNode *getNode(BasicBlock *BB) const {
|
|
|
|
return DT->getNode(BB);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
/// addNewBlock - Add a new node to the dominator tree information. This
|
2010-03-01 17:47:21 +00:00
|
|
|
/// creates a new node as a child of DomBB dominator node,linking it into
|
2007-10-23 21:04:37 +00:00
|
|
|
/// the children list of the immediate dominator.
|
|
|
|
inline DomTreeNode *addNewBlock(BasicBlock *BB, BasicBlock *DomBB) {
|
|
|
|
return DT->addNewBlock(BB, DomBB);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
/// changeImmediateDominator - This method is used to update the dominator
|
|
|
|
/// tree information when a node's immediate dominator changes.
|
|
|
|
///
|
|
|
|
inline void changeImmediateDominator(BasicBlock *N, BasicBlock* NewIDom) {
|
|
|
|
DT->changeImmediateDominator(N, NewIDom);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
inline void changeImmediateDominator(DomTreeNode *N, DomTreeNode* NewIDom) {
|
|
|
|
DT->changeImmediateDominator(N, NewIDom);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2010-03-01 17:47:21 +00:00
|
|
|
/// eraseNode - Removes a node from the dominator tree. Block must not
|
2010-09-10 22:25:58 +00:00
|
|
|
/// dominate any other blocks. Removes node from its immediate dominator's
|
2007-10-23 21:04:37 +00:00
|
|
|
/// children list. Deletes dominator node associated with basic block BB.
|
|
|
|
inline void eraseNode(BasicBlock *BB) {
|
|
|
|
DT->eraseNode(BB);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2007-10-23 21:04:37 +00:00
|
|
|
/// splitBlock - BB is split and now it has one successor. Update dominator
|
|
|
|
/// tree to reflect this change.
|
|
|
|
inline void splitBlock(BasicBlock* NewBB) {
|
|
|
|
DT->splitBlock(NewBB);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2010-10-06 15:49:14 +00:00
|
|
|
bool isReachableFromEntry(const BasicBlock* A) {
|
2008-06-30 20:28:02 +00:00
|
|
|
return DT->isReachableFromEntry(A);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
|
|
|
|
2010-03-01 17:47:21 +00:00
|
|
|
virtual void releaseMemory() {
|
2007-10-23 21:04:37 +00:00
|
|
|
DT->releaseMemory();
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2009-08-23 06:03:38 +00:00
|
|
|
virtual void print(raw_ostream &OS, const Module* M= 0) const;
|
2006-10-03 05:24:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===-------------------------------------
|
|
|
|
/// DominatorTree GraphTraits specialization so the DominatorTree can be
|
|
|
|
/// iterable by generic graph iterators.
|
|
|
|
///
|
2009-10-18 04:05:53 +00:00
|
|
|
template <> struct GraphTraits<DomTreeNode*> {
|
2007-06-04 00:32:22 +00:00
|
|
|
typedef DomTreeNode NodeType;
|
2006-10-03 05:24:56 +00:00
|
|
|
typedef NodeType::iterator ChildIteratorType;
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2006-10-03 05:24:56 +00:00
|
|
|
static NodeType *getEntryNode(NodeType *N) {
|
|
|
|
return N;
|
|
|
|
}
|
2009-10-18 04:05:53 +00:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2006-10-03 05:24:56 +00:00
|
|
|
return N->begin();
|
|
|
|
}
|
2009-10-18 04:05:53 +00:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2006-10-03 05:24:56 +00:00
|
|
|
return N->end();
|
|
|
|
}
|
2009-10-18 04:05:53 +00:00
|
|
|
|
|
|
|
typedef df_iterator<DomTreeNode*> nodes_iterator;
|
|
|
|
|
|
|
|
static nodes_iterator nodes_begin(DomTreeNode *N) {
|
|
|
|
return df_begin(getEntryNode(N));
|
|
|
|
}
|
|
|
|
|
|
|
|
static nodes_iterator nodes_end(DomTreeNode *N) {
|
|
|
|
return df_end(getEntryNode(N));
|
|
|
|
}
|
2006-10-03 05:24:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct GraphTraits<DominatorTree*>
|
2009-10-18 04:05:53 +00:00
|
|
|
: public GraphTraits<DomTreeNode*> {
|
2006-10-03 05:24:56 +00:00
|
|
|
static NodeType *getEntryNode(DominatorTree *DT) {
|
|
|
|
return DT->getRootNode();
|
|
|
|
}
|
2009-10-18 04:05:53 +00:00
|
|
|
|
|
|
|
static nodes_iterator nodes_begin(DominatorTree *N) {
|
|
|
|
return df_begin(getEntryNode(N));
|
|
|
|
}
|
|
|
|
|
|
|
|
static nodes_iterator nodes_end(DominatorTree *N) {
|
|
|
|
return df_end(getEntryNode(N));
|
|
|
|
}
|
2006-10-03 05:24:56 +00:00
|
|
|
};
|
|
|
|
|
2002-07-26 18:40:06 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-07-02 05:45:17 +00:00
|
|
|
#endif
|