1 //===- Dominators.h - Dominator Info Calculation ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DominatorTree class, which provides fast and efficient
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_DOMINATORS_H
16 #define LLVM_IR_DOMINATORS_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DepthFirstIterator.h"
20 #include "llvm/ADT/GraphTraits.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Support/GenericDomTree.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/raw_ostream.h"
34 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
35 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
37 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
39 class BasicBlockEdge {
40 const BasicBlock *Start;
41 const BasicBlock *End;
43 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
44 Start(Start_), End(End_) { }
45 const BasicBlock *getStart() const {
48 const BasicBlock *getEnd() const {
51 bool isSingleEdge() const;
54 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
55 /// normal dominator tree.
56 class DominatorTree : public DominatorTreeBase<BasicBlock> {
58 typedef DominatorTreeBase<BasicBlock> Base;
60 DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
62 // FIXME: This is no longer needed and should be removed when its uses are
64 Base& getBase() { return *this; }
66 /// \brief Returns *false* if the other dominator tree matches this dominator
68 inline bool compare(const DominatorTree &Other) const {
69 const DomTreeNode *R = getRootNode();
70 const DomTreeNode *OtherR = Other.getRootNode();
72 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
75 if (Base::compare(Other))
81 // Ensure base-class overloads are visible.
82 using Base::dominates;
84 /// \brief Return true if Def dominates a use in User.
86 /// This performs the special checks necessary if Def and User are in the same
87 /// basic block. Note that Def doesn't dominate a use in Def itself!
88 bool dominates(const Instruction *Def, const Use &U) const;
89 bool dominates(const Instruction *Def, const Instruction *User) const;
90 bool dominates(const Instruction *Def, const BasicBlock *BB) const;
91 bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
92 bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
94 inline DomTreeNode *operator[](BasicBlock *BB) const {
98 // Ensure base class overloads are visible.
99 using Base::isReachableFromEntry;
101 /// \brief Provide an overload for a Use.
102 bool isReachableFromEntry(const Use &U) const;
104 /// \brief Verify the correctness of the domtree by re-computing it.
106 /// This should only be used for debugging as it aborts the program if the
107 /// verification fails.
108 void verifyDomTree() const;
111 //===-------------------------------------
112 // DominatorTree GraphTraits specializations so the DominatorTree can be
113 // iterable by generic graph iterators.
115 template <> struct GraphTraits<DomTreeNode*> {
116 typedef DomTreeNode NodeType;
117 typedef NodeType::iterator ChildIteratorType;
119 static NodeType *getEntryNode(NodeType *N) {
122 static inline ChildIteratorType child_begin(NodeType *N) {
125 static inline ChildIteratorType child_end(NodeType *N) {
129 typedef df_iterator<DomTreeNode*> nodes_iterator;
131 static nodes_iterator nodes_begin(DomTreeNode *N) {
132 return df_begin(getEntryNode(N));
135 static nodes_iterator nodes_end(DomTreeNode *N) {
136 return df_end(getEntryNode(N));
140 template <> struct GraphTraits<DominatorTree*>
141 : public GraphTraits<DomTreeNode*> {
142 static NodeType *getEntryNode(DominatorTree *DT) {
143 return DT->getRootNode();
146 static nodes_iterator nodes_begin(DominatorTree *N) {
147 return df_begin(getEntryNode(N));
150 static nodes_iterator nodes_end(DominatorTree *N) {
151 return df_end(getEntryNode(N));
155 /// \brief Analysis pass which computes a \c DominatorTree.
156 class DominatorTreeWrapperPass : public FunctionPass {
162 DominatorTreeWrapperPass() : FunctionPass(ID) {
163 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
166 DominatorTree &getDomTree() { return DT; }
167 const DominatorTree &getDomTree() const { return DT; }
169 virtual bool runOnFunction(Function &F);
171 virtual void verifyAnalysis() const;
173 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
174 AU.setPreservesAll();
177 virtual void releaseMemory() { DT.releaseMemory(); }
179 virtual void print(raw_ostream &OS, const Module *M = 0) const;
182 } // End llvm namespace