1 //==- Dominators.h - Implementation of dominators tree for Clang CFG 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 implements the dominators tree functionality for Clang CFGs.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_DOMINATORS_H
15 #define LLVM_CLANG_DOMINATORS_H
17 #include "clang/Analysis/AnalysisContext.h"
18 #include "clang/Analysis/CFG.h"
19 #include "llvm/ADT/GraphTraits.h"
20 #include "llvm/Analysis/DominatorInternals.h"
21 #include "llvm/Analysis/Dominators.h"
22 #include "llvm/IR/Module.h"
27 typedef llvm::DomTreeNodeBase<CFGBlock> DomTreeNode;
29 /// \brief Concrete subclass of DominatorTreeBase for Clang
30 /// This class implements the dominators tree functionality given a Clang CFG.
32 class DominatorTree : public ManagedAnalysis {
33 virtual void anchor();
35 llvm::DominatorTreeBase<CFGBlock>* DT;
38 DT = new llvm::DominatorTreeBase<CFGBlock>(false);
45 llvm::DominatorTreeBase<CFGBlock>& getBase() { return *DT; }
47 /// \brief This method returns the root CFGBlock of the dominators tree.
49 inline CFGBlock *getRoot() const {
53 /// \brief This method returns the root DomTreeNode, which is the wrapper
55 inline DomTreeNode *getRootNode() const {
56 return DT->getRootNode();
59 /// \brief This method compares two dominator trees.
60 /// The method returns false if the other dominator tree matches this
61 /// dominator tree, otherwise returns true.
63 inline bool compare(DominatorTree &Other) const {
64 DomTreeNode *R = getRootNode();
65 DomTreeNode *OtherR = Other.getRootNode();
67 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
70 if (DT->compare(Other.getBase()))
76 /// \brief This method builds the dominator tree for a given CFG
77 /// The CFG information is passed via AnalysisDeclContext
79 void buildDominatorTree(AnalysisDeclContext &AC) {
81 DT->recalculate(*cfg);
84 /// \brief This method dumps immediate dominators for each block,
85 /// mainly used for debug purposes.
88 llvm::errs() << "Immediate dominance tree (Node#,IDom#):\n";
89 for (CFG::const_iterator I = cfg->begin(),
90 E = cfg->end(); I != E; ++I) {
91 if(DT->getNode(*I)->getIDom())
92 llvm::errs() << "(" << (*I)->getBlockID()
94 << DT->getNode(*I)->getIDom()->getBlock()->getBlockID()
96 else llvm::errs() << "(" << (*I)->getBlockID()
97 << "," << (*I)->getBlockID() << ")\n";
101 /// \brief This method tests if one CFGBlock dominates the other.
102 /// The method return true if A dominates B, false otherwise.
103 /// Note a block always dominates itself.
105 inline bool dominates(const CFGBlock* A, const CFGBlock* B) const {
106 return DT->dominates(A, B);
109 /// \brief This method tests if one CFGBlock properly dominates the other.
110 /// The method return true if A properly dominates B, false otherwise.
112 bool properlyDominates(const CFGBlock*A, const CFGBlock*B) const {
113 return DT->properlyDominates(A, B);
116 /// \brief This method finds the nearest common dominator CFG block
117 /// for CFG block A and B. If there is no such block then return NULL.
119 inline CFGBlock *findNearestCommonDominator(CFGBlock *A, CFGBlock *B) {
120 return DT->findNearestCommonDominator(A, B);
123 inline const CFGBlock *findNearestCommonDominator(const CFGBlock *A,
125 return DT->findNearestCommonDominator(A, B);
128 /// \brief This method is used to update the dominator
129 /// tree information when a node's immediate dominator changes.
131 inline void changeImmediateDominator(CFGBlock *N, CFGBlock *NewIDom) {
132 DT->changeImmediateDominator(N, NewIDom);
135 /// \brief This method tests if the given CFGBlock can be reachable from root.
136 /// Returns true if reachable, false otherwise.
138 bool isReachableFromEntry(const CFGBlock *A) {
139 return DT->isReachableFromEntry(A);
142 /// \brief This method releases the memory held by the dominator tree.
144 virtual void releaseMemory() {
148 /// \brief This method converts the dominator tree to human readable form.
150 virtual void print(raw_ostream &OS, const llvm::Module* M= 0) const {
158 inline void WriteAsOperand(raw_ostream &OS, const CFGBlock *BB,
160 OS << "BB#" << BB->getBlockID();
163 } // end namespace clang
165 //===-------------------------------------
166 /// DominatorTree GraphTraits specialization so the DominatorTree can be
167 /// iterable by generic graph iterators.
170 template <> struct GraphTraits< ::clang::DomTreeNode* > {
171 typedef ::clang::DomTreeNode NodeType;
172 typedef NodeType::iterator ChildIteratorType;
174 static NodeType *getEntryNode(NodeType *N) {
177 static inline ChildIteratorType child_begin(NodeType *N) {
180 static inline ChildIteratorType child_end(NodeType *N) {
184 typedef df_iterator< ::clang::DomTreeNode* > nodes_iterator;
186 static nodes_iterator nodes_begin(::clang::DomTreeNode *N) {
187 return df_begin(getEntryNode(N));
190 static nodes_iterator nodes_end(::clang::DomTreeNode *N) {
191 return df_end(getEntryNode(N));
195 template <> struct GraphTraits< ::clang::DominatorTree* >
196 : public GraphTraits< ::clang::DomTreeNode* > {
197 static NodeType *getEntryNode(::clang::DominatorTree *DT) {
198 return DT->getRootNode();
201 static nodes_iterator nodes_begin(::clang::DominatorTree *N) {
202 return df_begin(getEntryNode(N));
205 static nodes_iterator nodes_end(::clang::DominatorTree *N) {
206 return df_end(getEntryNode(N));
209 } // end namespace llvm