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/Support/GenericDomTree.h"
21 #include "llvm/Support/GenericDomTreeConstruction.h"
23 // FIXME: There is no good reason for the domtree to require a print method
24 // which accepts an LLVM Module, so remove this (and the method's argument that
25 // needs it) when that is fixed.
33 typedef llvm::DomTreeNodeBase<CFGBlock> DomTreeNode;
35 /// \brief Concrete subclass of DominatorTreeBase for Clang
36 /// This class implements the dominators tree functionality given a Clang CFG.
38 class DominatorTree : public ManagedAnalysis {
39 virtual void anchor();
41 llvm::DominatorTreeBase<CFGBlock>* DT;
44 DT = new llvm::DominatorTreeBase<CFGBlock>(false);
51 llvm::DominatorTreeBase<CFGBlock>& getBase() { return *DT; }
53 /// \brief This method returns the root CFGBlock of the dominators tree.
55 inline CFGBlock *getRoot() const {
59 /// \brief This method returns the root DomTreeNode, which is the wrapper
61 inline DomTreeNode *getRootNode() const {
62 return DT->getRootNode();
65 /// \brief This method compares two dominator trees.
66 /// The method returns false if the other dominator tree matches this
67 /// dominator tree, otherwise returns true.
69 inline bool compare(DominatorTree &Other) const {
70 DomTreeNode *R = getRootNode();
71 DomTreeNode *OtherR = Other.getRootNode();
73 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
76 if (DT->compare(Other.getBase()))
82 /// \brief This method builds the dominator tree for a given CFG
83 /// The CFG information is passed via AnalysisDeclContext
85 void buildDominatorTree(AnalysisDeclContext &AC) {
87 DT->recalculate(*cfg);
90 /// \brief This method dumps immediate dominators for each block,
91 /// mainly used for debug purposes.
94 llvm::errs() << "Immediate dominance tree (Node#,IDom#):\n";
95 for (CFG::const_iterator I = cfg->begin(),
96 E = cfg->end(); I != E; ++I) {
97 if(DT->getNode(*I)->getIDom())
98 llvm::errs() << "(" << (*I)->getBlockID()
100 << DT->getNode(*I)->getIDom()->getBlock()->getBlockID()
102 else llvm::errs() << "(" << (*I)->getBlockID()
103 << "," << (*I)->getBlockID() << ")\n";
107 /// \brief This method tests if one CFGBlock dominates the other.
108 /// The method return true if A dominates B, false otherwise.
109 /// Note a block always dominates itself.
111 inline bool dominates(const CFGBlock* A, const CFGBlock* B) const {
112 return DT->dominates(A, B);
115 /// \brief This method tests if one CFGBlock properly dominates the other.
116 /// The method return true if A properly dominates B, false otherwise.
118 bool properlyDominates(const CFGBlock*A, const CFGBlock*B) const {
119 return DT->properlyDominates(A, B);
122 /// \brief This method finds the nearest common dominator CFG block
123 /// for CFG block A and B. If there is no such block then return NULL.
125 inline CFGBlock *findNearestCommonDominator(CFGBlock *A, CFGBlock *B) {
126 return DT->findNearestCommonDominator(A, B);
129 inline const CFGBlock *findNearestCommonDominator(const CFGBlock *A,
131 return DT->findNearestCommonDominator(A, B);
134 /// \brief This method is used to update the dominator
135 /// tree information when a node's immediate dominator changes.
137 inline void changeImmediateDominator(CFGBlock *N, CFGBlock *NewIDom) {
138 DT->changeImmediateDominator(N, NewIDom);
141 /// \brief This method tests if the given CFGBlock can be reachable from root.
142 /// Returns true if reachable, false otherwise.
144 bool isReachableFromEntry(const CFGBlock *A) {
145 return DT->isReachableFromEntry(A);
148 /// \brief This method releases the memory held by the dominator tree.
150 virtual void releaseMemory() {
154 /// \brief This method converts the dominator tree to human readable form.
156 virtual void print(raw_ostream &OS, const llvm::Module* M= nullptr) const {
164 } // end namespace clang
166 //===-------------------------------------
167 /// DominatorTree GraphTraits specialization so the DominatorTree can be
168 /// iterable by generic graph iterators.
171 template <> struct GraphTraits< ::clang::DomTreeNode* > {
172 typedef ::clang::DomTreeNode NodeType;
173 typedef NodeType::iterator ChildIteratorType;
175 static NodeType *getEntryNode(NodeType *N) {
178 static inline ChildIteratorType child_begin(NodeType *N) {
181 static inline ChildIteratorType child_end(NodeType *N) {
185 typedef df_iterator< ::clang::DomTreeNode* > nodes_iterator;
187 static nodes_iterator nodes_begin(::clang::DomTreeNode *N) {
188 return df_begin(getEntryNode(N));
191 static nodes_iterator nodes_end(::clang::DomTreeNode *N) {
192 return df_end(getEntryNode(N));
196 template <> struct GraphTraits< ::clang::DominatorTree* >
197 : public GraphTraits< ::clang::DomTreeNode* > {
198 static NodeType *getEntryNode(::clang::DominatorTree *DT) {
199 return DT->getRootNode();
202 static nodes_iterator nodes_begin(::clang::DominatorTree *N) {
203 return df_begin(getEntryNode(N));
206 static nodes_iterator nodes_end(::clang::DominatorTree *N) {
207 return df_end(getEntryNode(N));
210 } // end namespace llvm