2014-01-13 09:26:24 +00:00
|
|
|
//===- 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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-13 09:26:24 +00:00
|
|
|
#ifndef LLVM_IR_DOMINATORS_H
|
|
|
|
#define LLVM_IR_DOMINATORS_H
|
2001-07-02 05:45:17 +00:00
|
|
|
|
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"
|
2014-01-13 10:52:56 +00:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/Pass.h"
|
2007-10-23 21:04:37 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2014-01-13 10:52:56 +00:00
|
|
|
#include "llvm/Support/GenericDomTree.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 {
|
|
|
|
|
2007-10-16 19:59:25 +00:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
|
2014-01-13 10:52:56 +00:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
|
2007-10-16 19:59:25 +00:00
|
|
|
|
2007-10-08 07:44:39 +00:00
|
|
|
typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
|
|
|
|
|
2012-08-16 15:09:43 +00:00
|
|
|
class BasicBlockEdge {
|
|
|
|
const BasicBlock *Start;
|
|
|
|
const BasicBlock *End;
|
|
|
|
public:
|
|
|
|
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
|
|
|
|
Start(Start_), End(End_) { }
|
|
|
|
const BasicBlock *getStart() const {
|
|
|
|
return Start;
|
|
|
|
}
|
|
|
|
const BasicBlock *getEnd() const {
|
|
|
|
return End;
|
|
|
|
}
|
|
|
|
bool isSingleEdge() const;
|
|
|
|
};
|
2012-08-07 17:30:46 +00:00
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief 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
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief Returns the root blocks of the current CFG.
|
2007-10-23 21:04:37 +00:00
|
|
|
///
|
2014-01-13 13:06:58 +00:00
|
|
|
/// This may include multiple blocks if we are computing post dominators.
|
|
|
|
/// For forward dominators, this will always be a single block (the entry
|
|
|
|
/// node).
|
2007-10-23 21:04:37 +00:00
|
|
|
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
|
|
|
|
2013-11-26 20:11:12 +00:00
|
|
|
/// Get all nodes dominated by R, including R itself.
|
2013-09-19 17:18:35 +00:00
|
|
|
void getDescendants(BasicBlock *R,
|
|
|
|
SmallVectorImpl<BasicBlock *> &Result) const {
|
|
|
|
DT->getDescendants(R, Result);
|
|
|
|
}
|
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief Returns *false* if the other dominator tree matches this dominator
|
|
|
|
/// tree.
|
2008-07-01 17:44:24 +00:00
|
|
|
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
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
// \brief Return true if Def dominates a use in User.
|
|
|
|
//
|
|
|
|
// This performs the special checks necessary if Def and User are in the same
|
|
|
|
// basic block. Note that Def doesn't dominate a use in Def itself!
|
2012-04-12 23:31:46 +00:00
|
|
|
bool dominates(const Instruction *Def, const Use &U) const;
|
2012-02-26 02:19:19 +00:00
|
|
|
bool dominates(const Instruction *Def, const Instruction *User) const;
|
|
|
|
bool dominates(const Instruction *Def, const BasicBlock *BB) const;
|
2012-08-07 17:30:46 +00:00
|
|
|
bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
|
|
|
|
bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
|
2012-01-20 07:41:13 +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
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief Find nearest common dominator basic block for basic block A and B.
|
|
|
|
///
|
|
|
|
/// If there is no such block then return NULL.
|
2007-10-23 21:04:37 +00:00
|
|
|
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
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief Returns the DominatorTree node for the specified basic block.
|
2007-10-23 21:04:37 +00:00
|
|
|
///
|
2014-01-13 13:06:58 +00:00
|
|
|
/// This is the same as using operator[] on this class.
|
2007-10-23 21:04:37 +00:00
|
|
|
inline DomTreeNode *getNode(BasicBlock *BB) const {
|
|
|
|
return DT->getNode(BB);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief Add a new node to the dominator tree information.
|
|
|
|
///
|
|
|
|
/// This creates a new node as a child of DomBB dominator node, linking it
|
|
|
|
/// into the children list of the immediate dominator.
|
2007-10-23 21:04:37 +00:00
|
|
|
inline DomTreeNode *addNewBlock(BasicBlock *BB, BasicBlock *DomBB) {
|
|
|
|
return DT->addNewBlock(BB, DomBB);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief Updates the dominator tree information when a node's immediate
|
|
|
|
/// dominator changes.
|
2007-10-23 21:04:37 +00:00
|
|
|
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
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief Removes a node from the dominator tree.
|
|
|
|
///
|
|
|
|
/// The block must not dominate any other blocks. Removes node from its
|
|
|
|
/// immediate dominator's children list. Deletes dominator node associated
|
|
|
|
/// with basic block BB.
|
2007-10-23 21:04:37 +00:00
|
|
|
inline void eraseNode(BasicBlock *BB) {
|
|
|
|
DT->eraseNode(BB);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2014-01-13 13:06:58 +00:00
|
|
|
/// \brief BB is split and now it has one successor; update dominator tree to
|
|
|
|
/// reflect this change.
|
2007-10-23 21:04:37 +00:00
|
|
|
inline void splitBlock(BasicBlock* NewBB) {
|
|
|
|
DT->splitBlock(NewBB);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2012-02-26 01:50:14 +00:00
|
|
|
bool isReachableFromEntry(const BasicBlock* A) const {
|
2008-06-30 20:28:02 +00:00
|
|
|
return DT->isReachableFromEntry(A);
|
|
|
|
}
|
2009-08-27 17:29:49 +00:00
|
|
|
|
2012-04-12 23:31:46 +00:00
|
|
|
bool isReachableFromEntry(const Use &U) const;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
//===-------------------------------------
|
2014-01-13 13:06:58 +00:00
|
|
|
// DominatorTree GraphTraits specializations 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
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-07-02 05:45:17 +00:00
|
|
|
#endif
|