LLVM API Documentation

Dominators.h
Go to the documentation of this file.
00001 //===- Dominators.h - Dominator Info Calculation ----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the DominatorTree class, which provides fast and efficient
00011 // dominance queries.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_IR_DOMINATORS_H
00016 #define LLVM_IR_DOMINATORS_H
00017 
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/ADT/DepthFirstIterator.h"
00020 #include "llvm/ADT/GraphTraits.h"
00021 #include "llvm/ADT/SmallPtrSet.h"
00022 #include "llvm/ADT/SmallVector.h"
00023 #include "llvm/IR/BasicBlock.h"
00024 #include "llvm/IR/CFG.h"
00025 #include "llvm/IR/Function.h"
00026 #include "llvm/Pass.h"
00027 #include "llvm/Support/Compiler.h"
00028 #include "llvm/Support/GenericDomTree.h"
00029 #include "llvm/Support/raw_ostream.h"
00030 #include <algorithm>
00031 
00032 namespace llvm {
00033 
00034 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
00035 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
00036 
00037 #define LLVM_COMMA ,
00038 EXTERN_TEMPLATE_INSTANTIATION(void Calculate<Function LLVM_COMMA BasicBlock *>(
00039     DominatorTreeBase<GraphTraits<BasicBlock *>::NodeType> &DT LLVM_COMMA
00040         Function &F));
00041 EXTERN_TEMPLATE_INSTANTIATION(
00042     void Calculate<Function LLVM_COMMA Inverse<BasicBlock *> >(
00043         DominatorTreeBase<GraphTraits<Inverse<BasicBlock *> >::NodeType> &DT
00044             LLVM_COMMA Function &F));
00045 #undef LLVM_COMMA
00046 
00047 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
00048 
00049 class BasicBlockEdge {
00050   const BasicBlock *Start;
00051   const BasicBlock *End;
00052 public:
00053   BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
00054     Start(Start_), End(End_) { }
00055   const BasicBlock *getStart() const {
00056     return Start;
00057   }
00058   const BasicBlock *getEnd() const {
00059     return End;
00060   }
00061   bool isSingleEdge() const;
00062 };
00063 
00064 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
00065 /// normal dominator tree.
00066 class DominatorTree : public DominatorTreeBase<BasicBlock> {
00067 public:
00068   typedef DominatorTreeBase<BasicBlock> Base;
00069 
00070   DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
00071 
00072   /// \brief Returns *false* if the other dominator tree matches this dominator
00073   /// tree.
00074   inline bool compare(const DominatorTree &Other) const {
00075     const DomTreeNode *R = getRootNode();
00076     const DomTreeNode *OtherR = Other.getRootNode();
00077 
00078     if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
00079       return true;
00080 
00081     if (Base::compare(Other))
00082       return true;
00083 
00084     return false;
00085   }
00086 
00087   // Ensure base-class overloads are visible.
00088   using Base::dominates;
00089 
00090   /// \brief Return true if Def dominates a use in User.
00091   ///
00092   /// This performs the special checks necessary if Def and User are in the same
00093   /// basic block. Note that Def doesn't dominate a use in Def itself!
00094   bool dominates(const Instruction *Def, const Use &U) const;
00095   bool dominates(const Instruction *Def, const Instruction *User) const;
00096   bool dominates(const Instruction *Def, const BasicBlock *BB) const;
00097   bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
00098   bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
00099 
00100   // Ensure base class overloads are visible.
00101   using Base::isReachableFromEntry;
00102 
00103   /// \brief Provide an overload for a Use.
00104   bool isReachableFromEntry(const Use &U) const;
00105 
00106   /// \brief Verify the correctness of the domtree by re-computing it.
00107   ///
00108   /// This should only be used for debugging as it aborts the program if the
00109   /// verification fails.
00110   void verifyDomTree() const;
00111 };
00112 
00113 //===-------------------------------------
00114 // DominatorTree GraphTraits specializations so the DominatorTree can be
00115 // iterable by generic graph iterators.
00116 
00117 template <> struct GraphTraits<DomTreeNode*> {
00118   typedef DomTreeNode NodeType;
00119   typedef NodeType::iterator  ChildIteratorType;
00120 
00121   static NodeType *getEntryNode(NodeType *N) {
00122     return N;
00123   }
00124   static inline ChildIteratorType child_begin(NodeType *N) {
00125     return N->begin();
00126   }
00127   static inline ChildIteratorType child_end(NodeType *N) {
00128     return N->end();
00129   }
00130 
00131   typedef df_iterator<DomTreeNode*> nodes_iterator;
00132 
00133   static nodes_iterator nodes_begin(DomTreeNode *N) {
00134     return df_begin(getEntryNode(N));
00135   }
00136 
00137   static nodes_iterator nodes_end(DomTreeNode *N) {
00138     return df_end(getEntryNode(N));
00139   }
00140 };
00141 
00142 template <> struct GraphTraits<DominatorTree*>
00143   : public GraphTraits<DomTreeNode*> {
00144   static NodeType *getEntryNode(DominatorTree *DT) {
00145     return DT->getRootNode();
00146   }
00147 
00148   static nodes_iterator nodes_begin(DominatorTree *N) {
00149     return df_begin(getEntryNode(N));
00150   }
00151 
00152   static nodes_iterator nodes_end(DominatorTree *N) {
00153     return df_end(getEntryNode(N));
00154   }
00155 };
00156 
00157 /// \brief Analysis pass which computes a \c DominatorTree.
00158 class DominatorTreeWrapperPass : public FunctionPass {
00159   DominatorTree DT;
00160 
00161 public:
00162   static char ID;
00163 
00164   DominatorTreeWrapperPass() : FunctionPass(ID) {
00165     initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
00166   }
00167 
00168   DominatorTree &getDomTree() { return DT; }
00169   const DominatorTree &getDomTree() const { return DT; }
00170 
00171   bool runOnFunction(Function &F) override;
00172 
00173   void verifyAnalysis() const override;
00174 
00175   void getAnalysisUsage(AnalysisUsage &AU) const override {
00176     AU.setPreservesAll();
00177   }
00178 
00179   void releaseMemory() override { DT.releaseMemory(); }
00180 
00181   void print(raw_ostream &OS, const Module *M = nullptr) const override;
00182 };
00183 
00184 } // End llvm namespace
00185 
00186 #endif