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/CFG.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/GenericDomTree.h"
29 #include "llvm/Support/raw_ostream.h"
34 // FIXME: Replace this brittle forward declaration with the include of the new
35 // PassManager.h when doing so doesn't break the PassManagerBuilder.
36 template <typename IRUnitT> class AnalysisManager;
37 class PreservedAnalyses;
39 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
40 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
43 EXTERN_TEMPLATE_INSTANTIATION(void Calculate<Function LLVM_COMMA BasicBlock *>(
44 DominatorTreeBase<GraphTraits<BasicBlock *>::NodeType> &DT LLVM_COMMA
46 EXTERN_TEMPLATE_INSTANTIATION(
47 void Calculate<Function LLVM_COMMA Inverse<BasicBlock *> >(
48 DominatorTreeBase<GraphTraits<Inverse<BasicBlock *> >::NodeType> &DT
49 LLVM_COMMA Function &F));
52 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
54 class BasicBlockEdge {
55 const BasicBlock *Start;
56 const BasicBlock *End;
58 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
59 Start(Start_), End(End_) { }
60 const BasicBlock *getStart() const {
63 const BasicBlock *getEnd() const {
66 bool isSingleEdge() const;
69 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
70 /// normal dominator tree.
71 class DominatorTree : public DominatorTreeBase<BasicBlock> {
73 typedef DominatorTreeBase<BasicBlock> Base;
75 DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
77 DominatorTree(DominatorTree &&Arg)
78 : Base(std::move(static_cast<Base &>(Arg))) {}
79 DominatorTree &operator=(DominatorTree &&RHS) {
80 Base::operator=(std::move(static_cast<Base &>(RHS)));
84 /// \brief Returns *false* if the other dominator tree matches this dominator
86 inline bool compare(const DominatorTree &Other) const {
87 const DomTreeNode *R = getRootNode();
88 const DomTreeNode *OtherR = Other.getRootNode();
90 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
93 if (Base::compare(Other))
99 // Ensure base-class overloads are visible.
100 using Base::dominates;
102 /// \brief Return true if Def dominates a use in User.
104 /// This performs the special checks necessary if Def and User are in the same
105 /// basic block. Note that Def doesn't dominate a use in Def itself!
106 bool dominates(const Instruction *Def, const Use &U) const;
107 bool dominates(const Instruction *Def, const Instruction *User) const;
108 bool dominates(const Instruction *Def, const BasicBlock *BB) const;
109 bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
110 bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
112 // Ensure base class overloads are visible.
113 using Base::isReachableFromEntry;
115 /// \brief Provide an overload for a Use.
116 bool isReachableFromEntry(const Use &U) const;
118 /// \brief Verify the correctness of the domtree by re-computing it.
120 /// This should only be used for debugging as it aborts the program if the
121 /// verification fails.
122 void verifyDomTree() const;
125 //===-------------------------------------
126 // DominatorTree GraphTraits specializations so the DominatorTree can be
127 // iterable by generic graph iterators.
129 template <> struct GraphTraits<DomTreeNode*> {
130 typedef DomTreeNode NodeType;
131 typedef NodeType::iterator ChildIteratorType;
133 static NodeType *getEntryNode(NodeType *N) {
136 static inline ChildIteratorType child_begin(NodeType *N) {
139 static inline ChildIteratorType child_end(NodeType *N) {
143 typedef df_iterator<DomTreeNode*> nodes_iterator;
145 static nodes_iterator nodes_begin(DomTreeNode *N) {
146 return df_begin(getEntryNode(N));
149 static nodes_iterator nodes_end(DomTreeNode *N) {
150 return df_end(getEntryNode(N));
154 template <> struct GraphTraits<DominatorTree*>
155 : public GraphTraits<DomTreeNode*> {
156 static NodeType *getEntryNode(DominatorTree *DT) {
157 return DT->getRootNode();
160 static nodes_iterator nodes_begin(DominatorTree *N) {
161 return df_begin(getEntryNode(N));
164 static nodes_iterator nodes_end(DominatorTree *N) {
165 return df_end(getEntryNode(N));
169 /// \brief Analysis pass which computes a \c DominatorTree.
170 class DominatorTreeAnalysis {
172 /// \brief Provide the result typedef for this analysis pass.
173 typedef DominatorTree Result;
175 /// \brief Opaque, unique identifier for this analysis pass.
176 static void *ID() { return (void *)&PassID; }
178 /// \brief Run the analysis pass over a function and produce a dominator tree.
179 DominatorTree run(Function &F);
181 /// \brief Provide access to a name for this pass for debugging purposes.
182 static StringRef name() { return "DominatorTreeAnalysis"; }
188 /// \brief Printer pass for the \c DominatorTree.
189 class DominatorTreePrinterPass {
193 explicit DominatorTreePrinterPass(raw_ostream &OS);
194 PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
196 static StringRef name() { return "DominatorTreePrinterPass"; }
199 /// \brief Verifier pass for the \c DominatorTree.
200 struct DominatorTreeVerifierPass {
201 PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
203 static StringRef name() { return "DominatorTreeVerifierPass"; }
206 /// \brief Legacy analysis pass which computes a \c DominatorTree.
207 class DominatorTreeWrapperPass : public FunctionPass {
213 DominatorTreeWrapperPass() : FunctionPass(ID) {
214 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
217 DominatorTree &getDomTree() { return DT; }
218 const DominatorTree &getDomTree() const { return DT; }
220 bool runOnFunction(Function &F) override;
222 void verifyAnalysis() const override;
224 void getAnalysisUsage(AnalysisUsage &AU) const override {
225 AU.setPreservesAll();
228 void releaseMemory() override { DT.releaseMemory(); }
230 void print(raw_ostream &OS, const Module *M = nullptr) const override;
233 } // End llvm namespace