Add GraphTraits specializations for machine dominators.
[oota-llvm.git] / include / llvm / CodeGen / MachineDominators.h
1 //=- llvm/CodeGen/MachineDominators.h - Machine Dom Calculation --*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Owen Anderson and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines classes mirroring those in llvm/Analysis/Dominators.h,
11 // but for target-specific code rather than target-independent IR.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_MACHINEDOMINATORS_H
16 #define LLVM_CODEGEN_MACHINEDOMINATORS_H
17
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/Analysis/DominatorInternals.h"
24 #include "llvm/ADT/GraphTraits.h"
25
26 namespace llvm {
27
28 inline void WriteAsOperand(std::ostream &, const MachineBasicBlock*, bool t) {  }
29
30 template<>
31 inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
32   this->Roots.push_back(MBB);
33 }
34
35 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
36 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<MachineBasicBlock>);
37
38 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
39
40 //===-------------------------------------
41 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
42 /// compute a normal dominator tree.
43 ///
44 class MachineDominatorTree : public MachineFunctionPass {
45 public:
46   static char ID; // Pass ID, replacement for typeid
47   DominatorTreeBase<MachineBasicBlock>* DT;
48   
49   MachineDominatorTree() : MachineFunctionPass(intptr_t(&ID)) {
50     DT = new DominatorTreeBase<MachineBasicBlock>(false);
51   }
52   
53   ~MachineDominatorTree() {
54     DT->releaseMemory();
55     delete DT;
56   }
57   
58   DominatorTreeBase<MachineBasicBlock>& getBase() { return *DT; }
59   
60   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61     AU.setPreservesAll();
62     MachineFunctionPass::getAnalysisUsage(AU);
63   }
64   
65   /// getRoots -  Return the root blocks of the current CFG.  This may include
66   /// multiple blocks if we are computing post dominators.  For forward
67   /// dominators, this will always be a single block (the entry node).
68   ///
69   inline const std::vector<MachineBasicBlock*> &getRoots() const {
70     return DT->getRoots();
71   }
72   
73   inline MachineBasicBlock *getRoot() const {
74     return DT->getRoot();
75   }
76   
77   inline MachineDomTreeNode *getRootNode() const {
78     return DT->getRootNode();
79   }
80   
81   virtual bool runOnMachineFunction(MachineFunction &F) {
82     DT->recalculate(F);
83     
84     return false;
85   }
86   
87   inline bool dominates(MachineDomTreeNode* A, MachineDomTreeNode* B) const {
88     return DT->dominates(A, B);
89   }
90   
91   inline bool dominates(MachineBasicBlock* A, MachineBasicBlock* B) const {
92     return DT->dominates(A, B);
93   }
94   
95   // dominates - Return true if A dominates B. This performs the
96   // special checks necessary if A and B are in the same basic block.
97   bool dominates(MachineInstr *A, MachineInstr *B) const {
98     MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
99     if (BBA != BBB) return DT->dominates(BBA, BBB);
100
101     // Loop through the basic block until we find A or B.
102     MachineBasicBlock::iterator I = BBA->begin();
103     for (; &*I != A && &*I != B; ++I) /*empty*/;
104
105     //if(!DT.IsPostDominators) {
106       // A dominates B if it is found first in the basic block.
107       return &*I == A;
108     //} else {
109     //  // A post-dominates B if B is found first in the basic block.
110     //  return &*I == B;
111     //}
112   }
113   
114   inline bool properlyDominates(const MachineDomTreeNode* A,
115                                 MachineDomTreeNode* B) const {
116     return DT->properlyDominates(A, B);
117   }
118   
119   inline bool properlyDominates(MachineBasicBlock* A,
120                                 MachineBasicBlock* B) const {
121     return DT->properlyDominates(A, B);
122   }
123   
124   /// findNearestCommonDominator - Find nearest common dominator basic block
125   /// for basic block A and B. If there is no such block then return NULL.
126   inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
127                                                        MachineBasicBlock *B) {
128     return DT->findNearestCommonDominator(A, B);
129   }
130   
131   inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
132     return DT->getNode(BB);
133   }
134   
135   /// getNode - return the (Post)DominatorTree node for the specified basic
136   /// block.  This is the same as using operator[] on this class.
137   ///
138   inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
139     return DT->getNode(BB);
140   }
141   
142   /// addNewBlock - Add a new node to the dominator tree information.  This
143   /// creates a new node as a child of DomBB dominator node,linking it into 
144   /// the children list of the immediate dominator.
145   inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
146                                          MachineBasicBlock *DomBB) {
147     return DT->addNewBlock(BB, DomBB);
148   }
149   
150   /// changeImmediateDominator - This method is used to update the dominator
151   /// tree information when a node's immediate dominator changes.
152   ///
153   inline void changeImmediateDominator(MachineBasicBlock *N,
154                                        MachineBasicBlock* NewIDom) {
155     DT->changeImmediateDominator(N, NewIDom);
156   }
157   
158   inline void changeImmediateDominator(MachineDomTreeNode *N,
159                                        MachineDomTreeNode* NewIDom) {
160     DT->changeImmediateDominator(N, NewIDom);
161   }
162   
163   /// eraseNode - Removes a node from  the dominator tree. Block must not
164   /// domiante any other blocks. Removes node from its immediate dominator's
165   /// children list. Deletes dominator node associated with basic block BB.
166   inline void eraseNode(MachineBasicBlock *BB) {
167     DT->eraseNode(BB);
168   }
169   
170   /// splitBlock - BB is split and now it has one successor. Update dominator
171   /// tree to reflect this change.
172   inline void splitBlock(MachineBasicBlock* NewBB) {
173     DT->splitBlock(NewBB);
174   }
175   
176   
177   virtual void releaseMemory() { 
178     DT->releaseMemory();
179   }
180   
181   virtual void print(std::ostream &OS, const Module* M= 0) const {
182     DT->print(OS, M);
183   }
184 };
185
186 //===-------------------------------------
187 /// DominatorTree GraphTraits specialization so the DominatorTree can be
188 /// iterable by generic graph iterators.
189 ///
190 template <> struct GraphTraits<MachineDomTreeNode *> {
191   typedef MachineDomTreeNode NodeType;
192   typedef NodeType::iterator  ChildIteratorType;
193   
194   static NodeType *getEntryNode(NodeType *N) {
195     return N;
196   }
197   static inline ChildIteratorType child_begin(NodeType* N) {
198     return N->begin();
199   }
200   static inline ChildIteratorType child_end(NodeType* N) {
201     return N->end();
202   }
203 };
204
205 template <> struct GraphTraits<MachineDominatorTree*>
206   : public GraphTraits<MachineDomTreeNode *> {
207   static NodeType *getEntryNode(MachineDominatorTree *DT) {
208     return DT->getRootNode();
209   }
210 };
211
212 }
213
214 #endif