1 //=- llvm/CodeGen/MachineDominators.h - Machine Dom 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 classes mirroring those in llvm/Analysis/Dominators.h,
11 // but for target-specific code rather than target-independent IR.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_MACHINEDOMINATORS_H
16 #define LLVM_CODEGEN_MACHINEDOMINATORS_H
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/Analysis/Dominators.h"
22 #include "llvm/Analysis/DominatorInternals.h"
26 inline void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t) { }
29 inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
30 this->Roots.push_back(MBB);
33 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
34 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<MachineBasicBlock>);
36 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
38 //===-------------------------------------
39 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
40 /// compute a normal dominator tree.
42 class MachineDominatorTree : public MachineFunctionPass {
44 static char ID; // Pass ID, replacement for typeid
45 DominatorTreeBase<MachineBasicBlock>* DT;
47 MachineDominatorTree();
49 ~MachineDominatorTree();
51 DominatorTreeBase<MachineBasicBlock>& getBase() { return *DT; }
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
55 /// getRoots - Return the root blocks of the current CFG. This may include
56 /// multiple blocks if we are computing post dominators. For forward
57 /// dominators, this will always be a single block (the entry node).
59 inline const std::vector<MachineBasicBlock*> &getRoots() const {
60 return DT->getRoots();
63 inline MachineBasicBlock *getRoot() const {
67 inline MachineDomTreeNode *getRootNode() const {
68 return DT->getRootNode();
71 virtual bool runOnMachineFunction(MachineFunction &F);
73 inline bool dominates(MachineDomTreeNode* A, MachineDomTreeNode* B) const {
74 return DT->dominates(A, B);
77 inline bool dominates(MachineBasicBlock* A, MachineBasicBlock* B) const {
78 return DT->dominates(A, B);
81 // dominates - Return true if A dominates B. This performs the
82 // special checks necessary if A and B are in the same basic block.
83 bool dominates(MachineInstr *A, MachineInstr *B) const {
84 MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
85 if (BBA != BBB) return DT->dominates(BBA, BBB);
87 // Loop through the basic block until we find A or B.
88 MachineBasicBlock::iterator I = BBA->begin();
89 for (; &*I != A && &*I != B; ++I) /*empty*/;
91 //if(!DT.IsPostDominators) {
92 // A dominates B if it is found first in the basic block.
95 // // A post-dominates B if B is found first in the basic block.
100 inline bool properlyDominates(const MachineDomTreeNode* A,
101 MachineDomTreeNode* B) const {
102 return DT->properlyDominates(A, B);
105 inline bool properlyDominates(MachineBasicBlock* A,
106 MachineBasicBlock* B) const {
107 return DT->properlyDominates(A, B);
110 /// findNearestCommonDominator - Find nearest common dominator basic block
111 /// for basic block A and B. If there is no such block then return NULL.
112 inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
113 MachineBasicBlock *B) {
114 return DT->findNearestCommonDominator(A, B);
117 inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
118 return DT->getNode(BB);
121 /// getNode - return the (Post)DominatorTree node for the specified basic
122 /// block. This is the same as using operator[] on this class.
124 inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
125 return DT->getNode(BB);
128 /// addNewBlock - Add a new node to the dominator tree information. This
129 /// creates a new node as a child of DomBB dominator node,linking it into
130 /// the children list of the immediate dominator.
131 inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
132 MachineBasicBlock *DomBB) {
133 return DT->addNewBlock(BB, DomBB);
136 /// changeImmediateDominator - This method is used to update the dominator
137 /// tree information when a node's immediate dominator changes.
139 inline void changeImmediateDominator(MachineBasicBlock *N,
140 MachineBasicBlock* NewIDom) {
141 DT->changeImmediateDominator(N, NewIDom);
144 inline void changeImmediateDominator(MachineDomTreeNode *N,
145 MachineDomTreeNode* NewIDom) {
146 DT->changeImmediateDominator(N, NewIDom);
149 /// eraseNode - Removes a node from the dominator tree. Block must not
150 /// domiante any other blocks. Removes node from its immediate dominator's
151 /// children list. Deletes dominator node associated with basic block BB.
152 inline void eraseNode(MachineBasicBlock *BB) {
156 /// splitBlock - BB is split and now it has one successor. Update dominator
157 /// tree to reflect this change.
158 inline void splitBlock(MachineBasicBlock* NewBB) {
159 DT->splitBlock(NewBB);
163 virtual void releaseMemory();
165 virtual void print(raw_ostream &OS, const Module*) const;
168 //===-------------------------------------
169 /// DominatorTree GraphTraits specialization so the DominatorTree can be
170 /// iterable by generic graph iterators.
173 template<class T> struct GraphTraits;
175 template <> struct GraphTraits<MachineDomTreeNode *> {
176 typedef MachineDomTreeNode NodeType;
177 typedef NodeType::iterator ChildIteratorType;
179 static NodeType *getEntryNode(NodeType *N) {
182 static inline ChildIteratorType child_begin(NodeType* N) {
185 static inline ChildIteratorType child_end(NodeType* N) {
190 template <> struct GraphTraits<MachineDominatorTree*>
191 : public GraphTraits<MachineDomTreeNode *> {
192 static NodeType *getEntryNode(MachineDominatorTree *DT) {
193 return DT->getRootNode();