2014-01-13 10:26:24 +01:00
|
|
|
//===- 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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-13 10:26:24 +01:00
|
|
|
#ifndef LLVM_IR_DOMINATORS_H
|
|
|
|
#define LLVM_IR_DOMINATORS_H
|
2001-07-02 07:45:17 +02:00
|
|
|
|
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"
|
2014-01-13 11:52:56 +01:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
2012-12-03 18:02:12 +01:00
|
|
|
#include "llvm/Pass.h"
|
2007-10-23 23:04:37 +02:00
|
|
|
#include "llvm/Support/CFG.h"
|
2014-01-13 11:52:56 +01:00
|
|
|
#include "llvm/Support/GenericDomTree.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 {
|
|
|
|
|
2007-10-16 21:59:25 +02:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
|
2014-01-13 11:52:56 +01:00
|
|
|
EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
|
2007-10-16 21:59:25 +02:00
|
|
|
|
2007-10-08 09:44:39 +02:00
|
|
|
typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
|
|
|
|
|
2012-08-16 17:09:43 +02: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 19:30:46 +02:00
|
|
|
|
2014-01-13 14:06:58 +01:00
|
|
|
/// \brief 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
|
|
|
|
2014-01-13 14:06:58 +01:00
|
|
|
/// \brief Returns the root blocks of the current CFG.
|
2007-10-23 23:04:37 +02:00
|
|
|
///
|
2014-01-13 14:06:58 +01: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 23:04:37 +02:00
|
|
|
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
|
|
|
|
2013-11-26 21:11:12 +01:00
|
|
|
/// Get all nodes dominated by R, including R itself.
|
2013-09-19 19:18:35 +02:00
|
|
|
void getDescendants(BasicBlock *R,
|
|
|
|
SmallVectorImpl<BasicBlock *> &Result) const {
|
|
|
|
DT->getDescendants(R, Result);
|
|
|
|
}
|
|
|
|
|
2014-01-13 14:06:58 +01:00
|
|
|
/// \brief Returns *false* if the other dominator tree matches this dominator
|
|
|
|
/// tree.
|
2008-07-01 19:44:24 +02:00
|
|
|
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
|
|
|
|
2014-01-13 14:06:58 +01: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-13 01:31:46 +02:00
|
|
|
bool dominates(const Instruction *Def, const Use &U) const;
|
2012-02-26 03:19:19 +01:00
|
|
|
bool dominates(const Instruction *Def, const Instruction *User) const;
|
|
|
|
bool dominates(const Instruction *Def, const BasicBlock *BB) const;
|
2012-08-07 19:30:46 +02:00
|
|
|
bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
|
|
|
|
bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
|
2012-01-20 08:41:13 +01: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
|
|
|
|
2014-01-13 14:06:58 +01: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 23:04:37 +02:00
|
|
|
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
|
|
|
|
2014-01-13 14:06:58 +01:00
|
|
|
/// \brief Returns the DominatorTree node for the specified basic block.
|
2007-10-23 23:04:37 +02:00
|
|
|
///
|
2014-01-13 14:06:58 +01:00
|
|
|
/// This is the same as using operator[] on this class.
|
2007-10-23 23:04:37 +02:00
|
|
|
inline DomTreeNode *getNode(BasicBlock *BB) const {
|
|
|
|
return DT->getNode(BB);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2014-01-13 14:06:58 +01: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 23:04:37 +02:00
|
|
|
inline DomTreeNode *addNewBlock(BasicBlock *BB, BasicBlock *DomBB) {
|
|
|
|
return DT->addNewBlock(BB, DomBB);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2014-01-13 14:06:58 +01:00
|
|
|
/// \brief Updates the dominator tree information when a node's immediate
|
|
|
|
/// dominator changes.
|
2007-10-23 23:04:37 +02:00
|
|
|
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
|
|
|
|
2014-01-13 14:06:58 +01: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 23:04:37 +02:00
|
|
|
inline void eraseNode(BasicBlock *BB) {
|
|
|
|
DT->eraseNode(BB);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2014-01-13 14:06:58 +01:00
|
|
|
/// \brief BB is split and now it has one successor; update dominator tree to
|
|
|
|
/// reflect this change.
|
2007-10-23 23:04:37 +02:00
|
|
|
inline void splitBlock(BasicBlock* NewBB) {
|
|
|
|
DT->splitBlock(NewBB);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2012-02-26 02:50:14 +01:00
|
|
|
bool isReachableFromEntry(const BasicBlock* A) const {
|
2008-06-30 22:28:02 +02:00
|
|
|
return DT->isReachableFromEntry(A);
|
|
|
|
}
|
2009-08-27 19:29:49 +02:00
|
|
|
|
2012-04-13 01:31:46 +02:00
|
|
|
bool isReachableFromEntry(const Use &U) const;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
//===-------------------------------------
|
2014-01-13 14:06:58 +01:00
|
|
|
// DominatorTree GraphTraits specializations 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
|
|
|
};
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-07-02 07:45:17 +02:00
|
|
|
#endif
|