2003-08-19 00:10:57 +02:00
|
|
|
//===- llvm/Analysis/Dominators.h - Dominator Info Calculation --*- C++ -*-===//
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-02 07:45:17 +02:00
|
|
|
//
|
2011-01-02 23:09:33 +01:00
|
|
|
// This file defines the DominatorTree class, which provides fast and efficient
|
|
|
|
// dominance queries.
|
2005-04-21 22:19:05 +02:00
|
|
|
//
|
2001-07-02 07:45:17 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-08-22 01:43:50 +02:00
|
|
|
#ifndef LLVM_ANALYSIS_DOMINATORS_H
|
|
|
|
#define LLVM_ANALYSIS_DOMINATORS_H
|
2001-07-02 07:45:17 +02:00
|
|
|
|
2002-01-31 00:27:55 +01:00
|
|
|
#include "llvm/Pass.h"
|
2007-10-23 23:04:37 +02:00
|
|
|
#include "llvm/Function.h"
|
2007-10-16 21:59:25 +02:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2009-10-18 06:05:53 +02:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2007-10-23 23:04:37 +02:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2007-10-16 21:59:25 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-10-23 23:04:37 +02:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-10-16 21:59:25 +02:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-08-23 07:17:37 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-10-08 09:44:39 +02:00
|
|
|
#include <algorithm>
|
2003-06-30 23:59:07 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2001-07-06 18:57:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-03-12 00:08:20 +01:00
|
|
|
/// DominatorBase - Base class that other, more interesting dominator analyses
|
|
|
|
/// inherit from.
|
|
|
|
///
|
2007-10-16 21:59:25 +02:00
|
|
|
template <class NodeT>
|
2007-10-23 23:42:49 +02:00
|
|
|
class DominatorBase {
|
2001-07-06 18:57:21 +02:00
|
|
|
protected:
|
2007-10-16 21:59:25 +02:00
|
|
|
std::vector<NodeT*> Roots;
|
2002-01-31 00:27:55 +01:00
|
|
|
const bool IsPostDominators;
|
2008-03-25 23:06:05 +01:00
|
|
|
inline explicit DominatorBase(bool isPostDom) :
|
2007-10-23 23:42:49 +02:00
|
|
|
Roots(), IsPostDominators(isPostDom) {}
|
2001-07-06 18:57:21 +02:00
|
|
|
public:
|
2007-05-01 23:15:47 +02:00
|
|
|
|
2010-03-01 18:47:21 +01:00
|
|
|
/// getRoots - Return the root blocks of the current CFG. This may include
|
2004-03-12 00:08:20 +01: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 21:59:25 +02:00
|
|
|
inline const std::vector<NodeT*> &getRoots() const { return Roots; }
|
2002-01-31 00:27:55 +01:00
|
|
|
|
2004-03-12 00:08:20 +01:00
|
|
|
/// isPostDominator - Returns true if analysis based of postdoms
|
|
|
|
///
|
2002-01-31 00:27:55 +01:00
|
|
|
bool isPostDominator() const { return IsPostDominators; }
|
2001-07-06 18:57:21 +02:00
|
|
|
};
|
|
|
|
|
2007-06-04 02:32:22 +02:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DomTreeNode - Dominator Tree Node
|
2007-10-16 21:59:25 +02:00
|
|
|
template<class NodeT> class DominatorTreeBase;
|
2007-10-17 16:56:40 +02:00
|
|
|
struct PostDominatorTree;
|
2007-10-08 09:44:39 +02:00
|
|
|
class MachineBasicBlock;
|
|
|
|
|
|
|
|
template <class NodeT>
|
|
|
|
class DomTreeNodeBase {
|
|
|
|
NodeT *TheBB;
|
|
|
|
DomTreeNodeBase<NodeT> *IDom;
|
|
|
|
std::vector<DomTreeNodeBase<NodeT> *> Children;
|
2007-06-12 02:14:41 +02:00
|
|
|
int DFSNumIn, DFSNumOut;
|
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
template<class N> friend class DominatorTreeBase;
|
2007-10-17 16:56:40 +02:00
|
|
|
friend struct PostDominatorTree;
|
2007-06-04 02:32:22 +02:00
|
|
|
public:
|
2007-10-08 09:44:39 +02:00
|
|
|
typedef typename std::vector<DomTreeNodeBase<NodeT> *>::iterator iterator;
|
|
|
|
typedef typename std::vector<DomTreeNodeBase<NodeT> *>::const_iterator
|
|
|
|
const_iterator;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-06-04 02:32:22 +02: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 19:29:49 +02:00
|
|
|
|
2007-10-08 09:44:39 +02:00
|
|
|
NodeT *getBlock() const { return TheBB; }
|
|
|
|
DomTreeNodeBase<NodeT> *getIDom() const { return IDom; }
|
|
|
|
const std::vector<DomTreeNodeBase<NodeT>*> &getChildren() const {
|
|
|
|
return Children;
|
|
|
|
}
|
2008-07-01 23:41:00 +02:00
|
|
|
|
2007-10-08 09:44:39 +02:00
|
|
|
DomTreeNodeBase(NodeT *BB, DomTreeNodeBase<NodeT> *iDom)
|
2007-06-12 02:54:38 +02:00
|
|
|
: TheBB(BB), IDom(iDom), DFSNumIn(-1), DFSNumOut(-1) { }
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-08 09:44:39 +02:00
|
|
|
DomTreeNodeBase<NodeT> *addChild(DomTreeNodeBase<NodeT> *C) {
|
|
|
|
Children.push_back(C);
|
|
|
|
return C;
|
|
|
|
}
|
2008-07-01 19:44:24 +02:00
|
|
|
|
2008-04-07 11:59:07 +02:00
|
|
|
size_t getNumChildren() const {
|
|
|
|
return Children.size();
|
|
|
|
}
|
2008-07-01 23:41:00 +02:00
|
|
|
|
|
|
|
void clearAllChildren() {
|
|
|
|
Children.clear();
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2008-07-01 23:41:00 +02:00
|
|
|
bool compare(DomTreeNodeBase<NodeT> *Other) {
|
|
|
|
if (getNumChildren() != Other->getNumChildren())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
SmallPtrSet<NodeT *, 4> OtherChildren;
|
2010-03-24 01:27:49 +01:00
|
|
|
for (iterator I = Other->begin(), E = Other->end(); I != E; ++I) {
|
2008-07-01 23:41:00 +02:00
|
|
|
NodeT *Nd = (*I)->getBlock();
|
|
|
|
OtherChildren.insert(Nd);
|
|
|
|
}
|
|
|
|
|
2010-03-24 01:27:49 +01:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I) {
|
2008-07-01 23:41:00 +02:00
|
|
|
NodeT *N = (*I)->getBlock();
|
|
|
|
if (OtherChildren.count(N) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-10-08 09:44:39 +02:00
|
|
|
void setIDom(DomTreeNodeBase<NodeT> *NewIDom) {
|
|
|
|
assert(IDom && "No immediate dominator?");
|
|
|
|
if (IDom != NewIDom) {
|
2007-10-29 05:50:50 +01:00
|
|
|
typename std::vector<DomTreeNodeBase<NodeT>*>::iterator I =
|
2007-10-08 09:44:39 +02: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 19:29:49 +02:00
|
|
|
|
2007-08-06 08:15:43 +02: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 07:49:31 +02:00
|
|
|
private:
|
2007-08-06 08:15:43 +02:00
|
|
|
// Return true if this node is dominated by other. Use this only if DFS info
|
|
|
|
// is valid.
|
2007-10-08 09:44:39 +02:00
|
|
|
bool DominatedBy(const DomTreeNodeBase<NodeT> *other) const {
|
2007-06-12 02:14:41 +02:00
|
|
|
return this->DFSNumIn >= other->DFSNumIn &&
|
|
|
|
this->DFSNumOut <= other->DFSNumOut;
|
|
|
|
}
|
2007-06-04 02:32:22 +02:00
|
|
|
};
|
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
|
2007-10-31 04:30:14 +01:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
|
2007-10-16 21:59:25 +02:00
|
|
|
|
|
|
|
template<class NodeT>
|
2009-08-23 07:17:37 +02:00
|
|
|
static raw_ostream &operator<<(raw_ostream &o,
|
|
|
|
const DomTreeNodeBase<NodeT> *Node) {
|
2007-10-16 21:59:25 +02:00
|
|
|
if (Node->getBlock())
|
|
|
|
WriteAsOperand(o, Node->getBlock(), false);
|
|
|
|
else
|
|
|
|
o << " <<exit node>>";
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
o << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "}";
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
return o << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class NodeT>
|
2009-08-23 07:17:37 +02:00
|
|
|
static void PrintDomTree(const DomTreeNodeBase<NodeT> *N, raw_ostream &o,
|
2007-10-16 21:59:25 +02:00
|
|
|
unsigned Lev) {
|
2009-08-23 07:17:37 +02:00
|
|
|
o.indent(2*Lev) << "[" << Lev << "] " << N;
|
2007-10-16 21:59:25 +02: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 09:44:39 +02:00
|
|
|
typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
|
|
|
|
|
2003-12-07 01:36:16 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-04-15 10:47:27 +02:00
|
|
|
/// DominatorTree - Calculate the immediate dominator tree for a function.
|
2004-03-12 00:08:20 +01:00
|
|
|
///
|
2007-10-16 21:59:25 +02:00
|
|
|
|
2007-10-25 02:16:57 +02:00
|
|
|
template<class FuncT, class N>
|
|
|
|
void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT,
|
|
|
|
FuncT& F);
|
2007-10-17 04:03:17 +02:00
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
template<class NodeT>
|
|
|
|
class DominatorTreeBase : public DominatorBase<NodeT> {
|
2003-12-07 01:36:16 +01:00
|
|
|
protected:
|
2007-10-16 21:59:25 +02:00
|
|
|
typedef DenseMap<NodeT*, DomTreeNodeBase<NodeT>*> DomTreeNodeMapType;
|
2007-06-07 19:47:21 +02:00
|
|
|
DomTreeNodeMapType DomTreeNodes;
|
2007-10-16 21:59:25 +02:00
|
|
|
DomTreeNodeBase<NodeT> *RootNode;
|
2007-04-15 10:47:27 +02:00
|
|
|
|
2007-06-07 19:47:21 +02:00
|
|
|
bool DFSInfoValid;
|
|
|
|
unsigned int SlowQueries;
|
2007-06-04 18:22:33 +02:00
|
|
|
// Information record used during immediate dominators computation.
|
2007-04-16 01:14:18 +02:00
|
|
|
struct InfoRec {
|
2008-04-16 06:21:16 +02:00
|
|
|
unsigned DFSNum;
|
2011-01-23 07:16:06 +01:00
|
|
|
unsigned Parent;
|
2006-03-20 20:32:48 +01:00
|
|
|
unsigned Semi;
|
2011-01-23 06:11:18 +01:00
|
|
|
NodeT *Label;
|
2007-04-15 10:47:27 +02:00
|
|
|
|
2011-01-23 07:16:06 +01:00
|
|
|
InfoRec() : DFSNum(0), Parent(0), Semi(0), Label(0) {}
|
2006-03-20 20:32:48 +01:00
|
|
|
};
|
2007-04-15 10:47:27 +02:00
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
DenseMap<NodeT*, NodeT*> IDoms;
|
2006-03-20 20:32:48 +01:00
|
|
|
|
|
|
|
// Vertex - Map the DFS number to the BasicBlock*
|
2007-10-16 21:59:25 +02:00
|
|
|
std::vector<NodeT*> Vertex;
|
2007-04-15 10:47:27 +02:00
|
|
|
|
2006-03-20 20:32:48 +01:00
|
|
|
// Info - Collection of information used during the computation of idoms.
|
2007-10-16 21:59:25 +02:00
|
|
|
DenseMap<NodeT*, InfoRec> Info;
|
|
|
|
|
|
|
|
void reset() {
|
2010-03-01 18:47:21 +01:00
|
|
|
for (typename DomTreeNodeMapType::iterator I = this->DomTreeNodes.begin(),
|
2007-10-16 21:59:25 +02:00
|
|
|
E = DomTreeNodes.end(); I != E; ++I)
|
|
|
|
delete I->second;
|
|
|
|
DomTreeNodes.clear();
|
|
|
|
IDoms.clear();
|
|
|
|
this->Roots.clear();
|
|
|
|
Vertex.clear();
|
|
|
|
RootNode = 0;
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-18 07:13:52 +02: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 01:27:49 +01:00
|
|
|
assert(std::distance(GraphT::child_begin(NewBB),
|
|
|
|
GraphT::child_end(NewBB)) == 1 &&
|
|
|
|
"NewBB should have a single successor!");
|
2007-10-18 07:13:52 +02:00
|
|
|
typename GraphT::NodeType* NewBBSucc = *GraphT::child_begin(NewBB);
|
|
|
|
|
|
|
|
std::vector<typename GraphT::NodeType*> PredBlocks;
|
2010-07-09 16:00:56 +02: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 18:47:21 +01:00
|
|
|
PredBlocks.push_back(*PI);
|
2007-10-18 07:13:52 +02:00
|
|
|
|
2010-07-09 16:00:56 +02:00
|
|
|
assert(!PredBlocks.empty() && "No predblocks?");
|
2009-05-21 23:08:47 +02:00
|
|
|
|
|
|
|
bool NewBBDominatesNewBBSucc = true;
|
2010-07-09 16:00:56 +02:00
|
|
|
for (typename InvTraits::ChildIteratorType PI =
|
|
|
|
InvTraits::child_begin(NewBBSucc),
|
|
|
|
E = InvTraits::child_end(NewBBSucc); PI != E; ++PI) {
|
2010-07-09 16:46:49 +02:00
|
|
|
typename InvTraits::NodeType *ND = *PI;
|
|
|
|
if (ND != NewBB && !DT.dominates(NewBBSucc, ND) &&
|
|
|
|
DT.isReachableFromEntry(ND)) {
|
2009-05-21 23:47:54 +02:00
|
|
|
NewBBDominatesNewBBSucc = false;
|
|
|
|
break;
|
2009-05-21 23:08:47 +02:00
|
|
|
}
|
2010-07-09 16:00:56 +02:00
|
|
|
}
|
2009-05-21 23:08:47 +02:00
|
|
|
|
2007-10-18 07:13:52 +02:00
|
|
|
// Find NewBB's immediate dominator and create new dominator tree node for
|
|
|
|
// NewBB.
|
2007-10-29 05:50:50 +01:00
|
|
|
NodeT *NewBBIDom = 0;
|
2007-10-18 07:13:52 +02:00
|
|
|
unsigned i = 0;
|
|
|
|
for (i = 0; i < PredBlocks.size(); ++i)
|
|
|
|
if (DT.isReachableFromEntry(PredBlocks[i])) {
|
|
|
|
NewBBIDom = PredBlocks[i];
|
|
|
|
break;
|
|
|
|
}
|
2009-06-03 23:42:06 +02: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 07:13:52 +02: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 05:50:50 +01:00
|
|
|
DomTreeNodeBase<NodeT> *NewBBNode = DT.addNewBlock(NewBB, NewBBIDom);
|
2007-10-18 07:13:52 +02: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 05:50:50 +01:00
|
|
|
DomTreeNodeBase<NodeT> *NewBBSuccNode = DT.getNode(NewBBSucc);
|
2007-10-18 07:13:52 +02:00
|
|
|
DT.changeImmediateDominator(NewBBSuccNode, NewBBNode);
|
|
|
|
}
|
|
|
|
}
|
2003-09-10 22:36:51 +02:00
|
|
|
|
2007-08-08 07:51:24 +02:00
|
|
|
public:
|
2008-03-25 23:06:05 +01:00
|
|
|
explicit DominatorTreeBase(bool isPostDom)
|
2007-10-23 23:42:49 +02:00
|
|
|
: DominatorBase<NodeT>(isPostDom), DFSInfoValid(false), SlowQueries(0) {}
|
|
|
|
virtual ~DominatorTreeBase() { reset(); }
|
2002-07-26 20:40:06 +02:00
|
|
|
|
2008-07-01 21:50:56 +02:00
|
|
|
/// compare - Return false if the other dominator tree base matches this
|
2008-07-01 19:44:24 +02:00
|
|
|
/// dominator tree base. Otherwise return true.
|
|
|
|
bool compare(DominatorTreeBase &Other) const {
|
|
|
|
|
2008-07-01 23:41:00 +02:00
|
|
|
const DomTreeNodeMapType &OtherDomTreeNodes = Other.DomTreeNodes;
|
|
|
|
if (DomTreeNodes.size() != OtherDomTreeNodes.size())
|
|
|
|
return true;
|
|
|
|
|
2010-03-01 18:47:21 +01:00
|
|
|
for (typename DomTreeNodeMapType::const_iterator
|
2008-07-01 19:44:24 +02:00
|
|
|
I = this->DomTreeNodes.begin(),
|
|
|
|
E = this->DomTreeNodes.end(); I != E; ++I) {
|
2008-07-01 23:41:00 +02:00
|
|
|
NodeT *BB = I->first;
|
|
|
|
typename DomTreeNodeMapType::const_iterator OI = OtherDomTreeNodes.find(BB);
|
|
|
|
if (OI == OtherDomTreeNodes.end())
|
|
|
|
return true;
|
2008-07-01 19:44:24 +02:00
|
|
|
|
2008-07-01 23:41:00 +02:00
|
|
|
DomTreeNodeBase<NodeT>* MyNd = I->second;
|
|
|
|
DomTreeNodeBase<NodeT>* OtherNd = OI->second;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2008-07-01 23:41:00 +02:00
|
|
|
if (MyNd->compare(OtherNd))
|
2008-07-01 19:44:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-01 23:41:00 +02:00
|
|
|
|
2008-07-01 19:44:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-07-26 20:40:06 +02:00
|
|
|
virtual void releaseMemory() { reset(); }
|
|
|
|
|
2002-09-06 04:16:27 +02: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 21:59:25 +02:00
|
|
|
inline DomTreeNodeBase<NodeT> *getNode(NodeT *BB) const {
|
|
|
|
typename DomTreeNodeMapType::const_iterator I = DomTreeNodes.find(BB);
|
2007-08-05 01:48:07 +02:00
|
|
|
return I != DomTreeNodes.end() ? I->second : 0;
|
2002-07-26 20:40:06 +02:00
|
|
|
}
|
2002-07-27 03:12:15 +02:00
|
|
|
|
2004-03-12 00:08:20 +01: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 21:59:25 +02:00
|
|
|
DomTreeNodeBase<NodeT> *getRootNode() { return RootNode; }
|
|
|
|
const DomTreeNodeBase<NodeT> *getRootNode() const { return RootNode; }
|
2003-09-10 22:36:51 +02:00
|
|
|
|
2007-06-07 19:47:21 +02:00
|
|
|
/// properlyDominates - Returns true iff this dominates N and this != N.
|
|
|
|
/// Note that this is not a constant time operation!
|
|
|
|
///
|
2007-10-16 21:59:25 +02:00
|
|
|
bool properlyDominates(const DomTreeNodeBase<NodeT> *A,
|
2009-08-27 19:54:15 +02:00
|
|
|
const DomTreeNodeBase<NodeT> *B) const {
|
2007-06-07 19:47:21 +02:00
|
|
|
if (A == 0 || B == 0) return false;
|
|
|
|
return dominatedBySlowTreeWalk(A, B);
|
|
|
|
}
|
|
|
|
|
2010-09-27 18:33:31 +02: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 23:34:22 +02:00
|
|
|
}
|
|
|
|
|
2010-03-01 18:47:21 +01:00
|
|
|
bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A,
|
2007-10-16 21:59:25 +02:00
|
|
|
const DomTreeNodeBase<NodeT> *B) const {
|
|
|
|
const DomTreeNodeBase<NodeT> *IDom;
|
2007-06-07 19:47:21 +02:00
|
|
|
if (A == 0 || B == 0) return false;
|
2007-06-28 04:07:08 +02:00
|
|
|
while ((IDom = B->getIDom()) != 0 && IDom != A && IDom != B)
|
2007-06-07 19:47:21 +02:00
|
|
|
B = IDom; // Walk up the tree
|
|
|
|
return IDom != 0;
|
|
|
|
}
|
|
|
|
|
2007-06-07 20:39:40 +02:00
|
|
|
|
2007-06-08 03:50:32 +02:00
|
|
|
/// isReachableFromEntry - Return true if A is dominated by the entry
|
|
|
|
/// block of the function containing it.
|
2010-10-06 17:49:14 +02:00
|
|
|
bool isReachableFromEntry(const NodeT* A) {
|
2010-03-24 01:27:49 +01:00
|
|
|
assert(!this->isPostDominator() &&
|
|
|
|
"This is not implemented for post dominators");
|
2007-10-29 05:50:50 +01:00
|
|
|
return dominates(&A->getParent()->front(), A);
|
2007-10-16 21:59:25 +02:00
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-06-12 19:30:56 +02:00
|
|
|
/// dominates - Returns true iff A dominates B. Note that this is not a
|
2007-06-07 19:47:21 +02:00
|
|
|
/// constant time operation!
|
|
|
|
///
|
2007-10-16 21:59:25 +02:00
|
|
|
inline bool dominates(const DomTreeNodeBase<NodeT> *A,
|
2009-08-27 19:54:15 +02:00
|
|
|
const DomTreeNodeBase<NodeT> *B) {
|
2010-03-01 18:47:21 +01:00
|
|
|
if (B == A)
|
2007-06-07 20:39:40 +02:00
|
|
|
return true; // A node trivially dominates itself.
|
|
|
|
|
|
|
|
if (A == 0 || B == 0)
|
|
|
|
return false;
|
2007-06-07 19:47:21 +02:00
|
|
|
|
2010-01-08 00:50:41 +01:00
|
|
|
// Compare the result of the tree walk and the dfs numbers, if expensive
|
|
|
|
// checks are enabled.
|
|
|
|
#ifdef XDEBUG
|
2010-03-24 01:27:49 +01:00
|
|
|
assert((!DFSInfoValid ||
|
|
|
|
(dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
|
|
|
|
"Tree walk disagrees with dfs numbers!");
|
2010-01-08 00:50:41 +01:00
|
|
|
#endif
|
|
|
|
|
2007-06-07 19:47:21 +02:00
|
|
|
if (DFSInfoValid)
|
2007-06-12 02:14:41 +02:00
|
|
|
return B->DominatedBy(A);
|
2007-06-07 19:47:21 +02: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 02:14:41 +02:00
|
|
|
return B->DominatedBy(A);
|
2007-06-07 19:47:21 +02:00
|
|
|
}
|
2007-06-12 02:54:38 +02:00
|
|
|
|
2007-06-07 19:47:21 +02:00
|
|
|
return dominatedBySlowTreeWalk(A, B);
|
|
|
|
}
|
2007-06-07 20:39:40 +02:00
|
|
|
|
2009-09-02 19:05:05 +02:00
|
|
|
inline bool dominates(const NodeT *A, const NodeT *B) {
|
2010-03-01 18:47:21 +01:00
|
|
|
if (A == B)
|
2007-06-07 20:39:40 +02:00
|
|
|
return true;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2009-09-02 19:05:05 +02: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 20:39:40 +02:00
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02:00
|
|
|
NodeT *getRoot() const {
|
|
|
|
assert(this->Roots.size() == 1 && "Should always have entry node!");
|
|
|
|
return this->Roots[0];
|
|
|
|
}
|
2007-06-07 20:39:40 +02:00
|
|
|
|
2007-06-12 01:31:22 +02: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 21:59:25 +02:00
|
|
|
NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) {
|
2010-03-24 01:27:49 +01:00
|
|
|
assert(A->getParent() == B->getParent() &&
|
|
|
|
"Two blocks are not in same function");
|
2007-10-16 21:59:25 +02:00
|
|
|
|
2010-03-24 01:22:24 +01: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 01:27:49 +01:00
|
|
|
NodeT &Entry = A->getParent()->front();
|
2010-03-24 01:22:24 +01:00
|
|
|
if (A == &Entry || B == &Entry)
|
|
|
|
return &Entry;
|
|
|
|
}
|
2007-10-16 21:59:25 +02: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 01:27:49 +01:00
|
|
|
while (IDomB) {
|
2007-10-16 21:59:25 +02:00
|
|
|
if (NodeADoms.count(IDomB) != 0)
|
|
|
|
return IDomB->getBlock();
|
|
|
|
|
|
|
|
IDomB = IDomB->getIDom();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-12 01:31:22 +02:00
|
|
|
|
2010-09-27 18:33:31 +02: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 22:36:51 +02:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// API to update (Post)DominatorTree information based on modifications to
|
2002-09-06 04:16:27 +02:00
|
|
|
// the CFG...
|
|
|
|
|
2007-06-04 18:43:25 +02:00
|
|
|
/// addNewBlock - Add a new node to the dominator tree information. This
|
2010-03-01 18:47:21 +01:00
|
|
|
/// creates a new node as a child of DomBB dominator node,linking it into
|
2007-06-04 18:43:25 +02:00
|
|
|
/// the children list of the immediate dominator.
|
2007-10-16 21:59:25 +02:00
|
|
|
DomTreeNodeBase<NodeT> *addNewBlock(NodeT *BB, NodeT *DomBB) {
|
2002-09-06 04:16:27 +02:00
|
|
|
assert(getNode(BB) == 0 && "Block already in dominator tree!");
|
2007-10-16 21:59:25 +02:00
|
|
|
DomTreeNodeBase<NodeT> *IDomNode = getNode(DomBB);
|
2002-09-29 23:37:08 +02:00
|
|
|
assert(IDomNode && "Not immediate dominator specified for block!");
|
2007-06-07 19:47:21 +02:00
|
|
|
DFSInfoValid = false;
|
2010-03-01 18:47:21 +01:00
|
|
|
return DomTreeNodes[BB] =
|
2007-10-29 05:50:50 +01:00
|
|
|
IDomNode->addChild(new DomTreeNodeBase<NodeT>(BB, IDomNode));
|
2002-09-06 04:16:27 +02:00
|
|
|
}
|
|
|
|
|
2002-09-26 18:14:37 +02:00
|
|
|
/// changeImmediateDominator - This method is used to update the dominator
|
|
|
|
/// tree information when a node's immediate dominator changes.
|
|
|
|
///
|
2007-10-16 21:59:25 +02:00
|
|
|
void changeImmediateDominator(DomTreeNodeBase<NodeT> *N,
|
|
|
|
DomTreeNodeBase<NodeT> *NewIDom) {
|
2004-05-21 20:38:16 +02:00
|
|
|
assert(N && NewIDom && "Cannot change null node pointers!");
|
2007-06-07 19:47:21 +02:00
|
|
|
DFSInfoValid = false;
|
2004-05-21 20:38:16 +02:00
|
|
|
N->setIDom(NewIDom);
|
2002-09-26 18:14:37 +02:00
|
|
|
}
|
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
void changeImmediateDominator(NodeT *BB, NodeT *NewBB) {
|
2007-06-04 18:22:33 +02:00
|
|
|
changeImmediateDominator(getNode(BB), getNode(NewBB));
|
|
|
|
}
|
|
|
|
|
2010-03-01 18:47:21 +01:00
|
|
|
/// eraseNode - Removes a node from the dominator tree. Block must not
|
2010-09-11 00:25:58 +02:00
|
|
|
/// dominate any other blocks. Removes node from its immediate dominator's
|
2007-08-14 00:10:29 +02:00
|
|
|
/// children list. Deletes dominator node associated with basic block BB.
|
2007-10-16 21:59:25 +02:00
|
|
|
void eraseNode(NodeT *BB) {
|
|
|
|
DomTreeNodeBase<NodeT> *Node = getNode(BB);
|
2010-03-24 01:27:49 +01:00
|
|
|
assert(Node && "Removing node that isn't in dominator tree.");
|
|
|
|
assert(Node->getChildren().empty() && "Node is not a leaf node.");
|
2007-10-16 21:59:25 +02: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-14 00:10:29 +02:00
|
|
|
|
2006-09-12 02:18:28 +02: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 21:59:25 +02:00
|
|
|
void removeNode(NodeT *BB) {
|
2006-09-12 02:18:28 +02:00
|
|
|
assert(getNode(BB) && "Removing node that isn't in dominator tree.");
|
2007-06-03 08:26:14 +02:00
|
|
|
DomTreeNodes.erase(BB);
|
2006-09-12 02:18:28 +02:00
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-18 07:13:52 +02: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 23:04:37 +02:00
|
|
|
this->Split<Inverse<NodeT*>, GraphTraits<Inverse<NodeT*> > >(*this, NewBB);
|
2007-10-18 07:13:52 +02:00
|
|
|
else
|
2007-10-23 23:04:37 +02:00
|
|
|
this->Split<NodeT*, GraphTraits<NodeT*> >(*this, NewBB);
|
2007-10-18 07:13:52 +02:00
|
|
|
}
|
2006-09-12 02:18:28 +02:00
|
|
|
|
2002-09-06 04:16:27 +02:00
|
|
|
/// print - Convert to human readable form
|
2004-03-12 00:08:20 +01:00
|
|
|
///
|
2009-08-23 07:17:37 +02:00
|
|
|
void print(raw_ostream &o) const {
|
2007-10-16 21:59:25 +02:00
|
|
|
o << "=============================--------------------------------\n";
|
2008-02-27 19:38:29 +01:00
|
|
|
if (this->isPostDominator())
|
|
|
|
o << "Inorder PostDominator Tree: ";
|
|
|
|
else
|
|
|
|
o << "Inorder Dominator Tree: ";
|
2011-01-09 17:00:09 +01:00
|
|
|
if (!this->DFSInfoValid)
|
2007-10-16 21:59:25 +02:00
|
|
|
o << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
|
|
|
|
o << "\n";
|
|
|
|
|
2009-09-08 03:22:54 +02:00
|
|
|
// The postdom tree can have a null root if there are no returns.
|
|
|
|
if (getRootNode())
|
|
|
|
PrintDomTree<NodeT>(getRootNode(), o, 1);
|
2007-10-16 21:59:25 +02:00
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-08-08 07:51:24 +02:00
|
|
|
protected:
|
2007-10-17 00:59:15 +02:00
|
|
|
template<class GraphT>
|
|
|
|
friend typename GraphT::NodeType* Eval(
|
|
|
|
DominatorTreeBase<typename GraphT::NodeType>& DT,
|
2011-01-23 07:16:06 +01:00
|
|
|
typename GraphT::NodeType* V,
|
|
|
|
unsigned LastLinked);
|
2007-10-17 00:59:15 +02:00
|
|
|
|
|
|
|
template<class GraphT>
|
|
|
|
friend unsigned DFSPass(DominatorTreeBase<typename GraphT::NodeType>& DT,
|
|
|
|
typename GraphT::NodeType* V,
|
|
|
|
unsigned N);
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-25 02:16:57 +02:00
|
|
|
template<class FuncT, class N>
|
|
|
|
friend void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT,
|
|
|
|
FuncT& F);
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-08-08 07:51:24 +02:00
|
|
|
/// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
|
|
|
|
/// dominator tree in dfs order.
|
2007-10-16 21:59:25 +02:00
|
|
|
void updateDFSNumbers() {
|
|
|
|
unsigned DFSNum = 0;
|
|
|
|
|
|
|
|
SmallVector<std::pair<DomTreeNodeBase<NodeT>*,
|
|
|
|
typename DomTreeNodeBase<NodeT>::iterator>, 32> WorkStack;
|
|
|
|
|
2010-01-08 00:50:06 +01: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 21:59:25 +02:00
|
|
|
}
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
SlowQueries = 0;
|
|
|
|
DFSInfoValid = true;
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
DomTreeNodeBase<NodeT> *getNodeForBlock(NodeT *BB) {
|
2009-07-02 02:17:47 +02:00
|
|
|
typename DomTreeNodeMapType::iterator I = this->DomTreeNodes.find(BB);
|
|
|
|
if (I != this->DomTreeNodes.end() && I->second)
|
|
|
|
return I->second;
|
2007-10-16 21:59:25 +02:00
|
|
|
|
|
|
|
// Haven't calculated this node yet? Get or calculate the node for the
|
|
|
|
// immediate dominator.
|
|
|
|
NodeT *IDom = getIDom(BB);
|
2008-04-16 06:21:16 +02:00
|
|
|
|
2008-05-04 23:07:35 +02:00
|
|
|
assert(IDom || this->DomTreeNodes[NULL]);
|
2007-10-16 21:59:25 +02: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 19:29:49 +02:00
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
inline NodeT *getIDom(NodeT *BB) const {
|
|
|
|
typename DenseMap<NodeT*, NodeT*>::const_iterator I = IDoms.find(BB);
|
2007-09-23 23:31:44 +02:00
|
|
|
return I != IDoms.end() ? I->second : 0;
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-31 04:30:14 +01:00
|
|
|
inline void addRoot(NodeT* BB) {
|
2008-04-16 06:21:16 +02:00
|
|
|
this->Roots.push_back(BB);
|
2007-10-31 04:30:14 +01:00
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02:00
|
|
|
public:
|
|
|
|
/// recalculate - compute a dominator tree for the given function
|
2007-10-25 02:16:57 +02:00
|
|
|
template<class FT>
|
|
|
|
void recalculate(FT& F) {
|
2010-01-08 00:50:25 +01:00
|
|
|
reset();
|
|
|
|
this->Vertex.push_back(0);
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2010-01-08 00:50:25 +01:00
|
|
|
if (!this->IsPostDominators) {
|
|
|
|
// Initialize root
|
2007-10-31 04:30:14 +01:00
|
|
|
this->Roots.push_back(&F.front());
|
|
|
|
this->IDoms[&F.front()] = 0;
|
|
|
|
this->DomTreeNodes[&F.front()] = 0;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-25 02:16:57 +02:00
|
|
|
Calculate<FT, NodeT*>(*this, F);
|
2007-10-23 23:04:37 +02:00
|
|
|
} else {
|
|
|
|
// Initialize the roots list
|
2007-10-31 04:30:14 +01: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 23:04:37 +02:00
|
|
|
|
|
|
|
// Prepopulate maps so that we don't get iterator invalidation issues later.
|
|
|
|
this->IDoms[I] = 0;
|
|
|
|
this->DomTreeNodes[I] = 0;
|
|
|
|
}
|
|
|
|
|
2007-10-25 02:16:57 +02:00
|
|
|
Calculate<FT, Inverse<NodeT*> >(*this, F);
|
2007-10-23 23:04:37 +02:00
|
|
|
}
|
|
|
|
}
|
2002-07-26 20:40:06 +02:00
|
|
|
};
|
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
|
|
|
|
|
2006-10-03 07:24:56 +02:00
|
|
|
//===-------------------------------------
|
|
|
|
/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
|
|
|
|
/// compute a normal dominator tree.
|
|
|
|
///
|
2007-10-23 23:04:37 +02:00
|
|
|
class DominatorTree : public FunctionPass {
|
2006-10-03 07:24:56 +02:00
|
|
|
public:
|
2007-05-03 03:11:54 +02:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2007-10-23 23:04:37 +02:00
|
|
|
DominatorTreeBase<BasicBlock>* DT;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2010-08-06 20:33:48 +02:00
|
|
|
DominatorTree() : FunctionPass(ID) {
|
2010-10-19 19:21:58 +02:00
|
|
|
initializeDominatorTreePass(*PassRegistry::getPassRegistry());
|
2007-10-23 23:42:49 +02:00
|
|
|
DT = new DominatorTreeBase<BasicBlock>(false);
|
2007-10-23 23:04:37 +02:00
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02:00
|
|
|
~DominatorTree() {
|
|
|
|
delete DT;
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-11-27 04:33:40 +01:00
|
|
|
DominatorTreeBase<BasicBlock>& getBase() { return *DT; }
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2010-03-01 18:47:21 +01:00
|
|
|
/// getRoots - Return the root blocks of the current CFG. This may include
|
2007-10-23 23:04:37 +02: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 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02:00
|
|
|
inline BasicBlock *getRoot() const {
|
|
|
|
return DT->getRoot();
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02:00
|
|
|
inline DomTreeNode *getRootNode() const {
|
|
|
|
return DT->getRootNode();
|
2006-10-03 07:24:56 +02:00
|
|
|
}
|
2008-07-01 19:44:24 +02:00
|
|
|
|
2008-07-01 21:50:56 +02:00
|
|
|
/// compare - Return false if the other dominator tree matches this
|
2008-07-01 19:44:24 +02:00
|
|
|
/// dominator tree. Otherwise return true.
|
|
|
|
inline bool compare(DominatorTree &Other) const {
|
|
|
|
DomTreeNode *R = getRootNode();
|
|
|
|
DomTreeNode *OtherR = Other.getRootNode();
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2008-07-01 19:44:24 +02:00
|
|
|
if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
|
|
|
|
return true;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2008-07-01 19:44:24 +02:00
|
|
|
if (DT->compare(Other.getBase()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-04-16 01:14:18 +02:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2009-09-28 02:27:48 +02:00
|
|
|
virtual void verifyAnalysis() const;
|
|
|
|
|
2006-10-03 07:24:56 +02:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2010-12-22 23:10:08 +01:00
|
|
|
inline bool dominates(const DomTreeNode* A, const DomTreeNode* B) const {
|
2007-10-23 23:04:37 +02:00
|
|
|
return DT->dominates(A, B);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2009-09-02 19:05:05 +02:00
|
|
|
inline bool dominates(const BasicBlock* A, const BasicBlock* B) const {
|
2007-10-23 23:04:37 +02:00
|
|
|
return DT->dominates(A, B);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02: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-22 00:30:50 +02:00
|
|
|
bool dominates(const Instruction *A, const Instruction *B) const;
|
2007-10-23 23:04:37 +02:00
|
|
|
|
2009-09-22 00:30:50 +02:00
|
|
|
bool properlyDominates(const DomTreeNode *A, const DomTreeNode *B) const {
|
2007-10-23 23:04:37 +02:00
|
|
|
return DT->properlyDominates(A, B);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2010-09-27 18:33:31 +02:00
|
|
|
bool properlyDominates(const BasicBlock *A, const BasicBlock *B) const {
|
2007-10-23 23:04:37 +02:00
|
|
|
return DT->properlyDominates(A, B);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02: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 19:29:49 +02:00
|
|
|
|
2010-09-27 18:33:31 +02:00
|
|
|
inline const BasicBlock *findNearestCommonDominator(const BasicBlock *A,
|
|
|
|
const BasicBlock *B) {
|
|
|
|
return DT->findNearestCommonDominator(A, B);
|
|
|
|
}
|
|
|
|
|
2007-10-23 23:04:37 +02:00
|
|
|
inline DomTreeNode *operator[](BasicBlock *BB) const {
|
|
|
|
return DT->getNode(BB);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02: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 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02:00
|
|
|
/// addNewBlock - Add a new node to the dominator tree information. This
|
2010-03-01 18:47:21 +01:00
|
|
|
/// creates a new node as a child of DomBB dominator node,linking it into
|
2007-10-23 23:04:37 +02:00
|
|
|
/// the children list of the immediate dominator.
|
|
|
|
inline DomTreeNode *addNewBlock(BasicBlock *BB, BasicBlock *DomBB) {
|
|
|
|
return DT->addNewBlock(BB, DomBB);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02: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 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02:00
|
|
|
inline void changeImmediateDominator(DomTreeNode *N, DomTreeNode* NewIDom) {
|
|
|
|
DT->changeImmediateDominator(N, NewIDom);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2010-03-01 18:47:21 +01:00
|
|
|
/// eraseNode - Removes a node from the dominator tree. Block must not
|
2010-09-11 00:25:58 +02:00
|
|
|
/// dominate any other blocks. Removes node from its immediate dominator's
|
2007-10-23 23:04:37 +02:00
|
|
|
/// children list. Deletes dominator node associated with basic block BB.
|
|
|
|
inline void eraseNode(BasicBlock *BB) {
|
|
|
|
DT->eraseNode(BB);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2007-10-23 23:04:37 +02: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 19:29:49 +02:00
|
|
|
|
2010-10-06 17:49:14 +02:00
|
|
|
bool isReachableFromEntry(const BasicBlock* A) {
|
2008-06-30 22:28:02 +02:00
|
|
|
return DT->isReachableFromEntry(A);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
|
|
|
|
2010-03-01 18:47:21 +01:00
|
|
|
virtual void releaseMemory() {
|
2007-10-23 23:04:37 +02:00
|
|
|
DT->releaseMemory();
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2009-08-23 08:03:38 +02:00
|
|
|
virtual void print(raw_ostream &OS, const Module* M= 0) const;
|
2006-10-03 07:24:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//===-------------------------------------
|
|
|
|
/// DominatorTree GraphTraits specialization so the DominatorTree can be
|
|
|
|
/// iterable by generic graph iterators.
|
|
|
|
///
|
2009-10-18 06:05:53 +02:00
|
|
|
template <> struct GraphTraits<DomTreeNode*> {
|
2007-06-04 02:32:22 +02:00
|
|
|
typedef DomTreeNode NodeType;
|
2006-10-03 07:24:56 +02:00
|
|
|
typedef NodeType::iterator ChildIteratorType;
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2006-10-03 07:24:56 +02:00
|
|
|
static NodeType *getEntryNode(NodeType *N) {
|
|
|
|
return N;
|
|
|
|
}
|
2009-10-18 06:05:53 +02:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2006-10-03 07:24:56 +02:00
|
|
|
return N->begin();
|
|
|
|
}
|
2009-10-18 06:05:53 +02:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2006-10-03 07:24:56 +02:00
|
|
|
return N->end();
|
|
|
|
}
|
2009-10-18 06:05:53 +02: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 07:24:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct GraphTraits<DominatorTree*>
|
2009-10-18 06:05:53 +02:00
|
|
|
: public GraphTraits<DomTreeNode*> {
|
2006-10-03 07:24:56 +02:00
|
|
|
static NodeType *getEntryNode(DominatorTree *DT) {
|
|
|
|
return DT->getRootNode();
|
|
|
|
}
|
2009-10-18 06:05:53 +02: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 07:24:56 +02:00
|
|
|
};
|
|
|
|
|
2002-07-26 20:40:06 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-07-02 07:45:17 +02:00
|
|
|
#endif
|