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