Add a first attempt at dominator information for MBB's. Use with caution: this has...
[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 void WriteAsOperand(std::ostream &, const MachineBasicBlock*, bool t) {  }
28
29 //===-------------------------------------
30 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
31 /// compute a normal dominator tree.
32 ///
33 class MachineDominatorTree : public MachineFunctionPass {
34 public:
35   static char ID; // Pass ID, replacement for typeid
36   DominatorTreeBase<MachineBasicBlock>* DT;
37   
38   MachineDominatorTree() : MachineFunctionPass(intptr_t(&ID)) {
39     DT = new DominatorTreeBase<MachineBasicBlock>(false);
40   }
41   
42   ~MachineDominatorTree() {
43     DT->releaseMemory();
44     delete DT;
45   }
46   
47   /// getRoots -  Return the root blocks of the current CFG.  This may include
48   /// multiple blocks if we are computing post dominators.  For forward
49   /// dominators, this will always be a single block (the entry node).
50   ///
51   inline const std::vector<MachineBasicBlock*> &getRoots() const {
52     return DT->getRoots();
53   }
54   
55   inline MachineBasicBlock *getRoot() const {
56     return DT->getRoot();
57   }
58   
59   inline MachineDomTreeNode *getRootNode() const {
60     return DT->getRootNode();
61   }
62   
63   virtual bool runOnFunction(Function &F);
64   
65   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
66     AU.setPreservesAll();
67   }
68   
69   inline bool dominates(MachineDomTreeNode* A, MachineDomTreeNode* B) const {
70     return DT->dominates(A, B);
71   }
72   
73   inline bool dominates(MachineBasicBlock* A, MachineBasicBlock* B) const {
74     return DT->dominates(A, B);
75   }
76   
77   // dominates - Return true if A dominates B. This performs the
78   // special checks necessary if A and B are in the same basic block.
79   bool dominates(MachineInstr *A, MachineInstr *B) const {
80     MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
81     if (BBA != BBB) return DT->dominates(BBA, BBB);
82
83     // Loop through the basic block until we find A or B.
84     MachineBasicBlock::iterator I = BBA->begin();
85     for (; &*I != A && &*I != B; ++I) /*empty*/;
86
87     //if(!DT.IsPostDominators) {
88       // A dominates B if it is found first in the basic block.
89       return &*I == A;
90     //} else {
91     //  // A post-dominates B if B is found first in the basic block.
92     //  return &*I == B;
93     //}
94   }
95   
96   inline bool properlyDominates(const MachineDomTreeNode* A,
97                                 MachineDomTreeNode* B) const {
98     return DT->properlyDominates(A, B);
99   }
100   
101   inline bool properlyDominates(MachineBasicBlock* A,
102                                 MachineBasicBlock* B) const {
103     return DT->properlyDominates(A, B);
104   }
105   
106   /// findNearestCommonDominator - Find nearest common dominator basic block
107   /// for basic block A and B. If there is no such block then return NULL.
108   inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
109                                                        MachineBasicBlock *B) {
110     return DT->findNearestCommonDominator(A, B);
111   }
112   
113   inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
114     return DT->getNode(BB);
115   }
116   
117   /// getNode - return the (Post)DominatorTree node for the specified basic
118   /// block.  This is the same as using operator[] on this class.
119   ///
120   inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
121     return DT->getNode(BB);
122   }
123   
124   /// addNewBlock - Add a new node to the dominator tree information.  This
125   /// creates a new node as a child of DomBB dominator node,linking it into 
126   /// the children list of the immediate dominator.
127   inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
128                                          MachineBasicBlock *DomBB) {
129     return DT->addNewBlock(BB, DomBB);
130   }
131   
132   /// changeImmediateDominator - This method is used to update the dominator
133   /// tree information when a node's immediate dominator changes.
134   ///
135   inline void changeImmediateDominator(MachineBasicBlock *N,
136                                        MachineBasicBlock* NewIDom) {
137     DT->changeImmediateDominator(N, NewIDom);
138   }
139   
140   inline void changeImmediateDominator(MachineDomTreeNode *N,
141                                        MachineDomTreeNode* NewIDom) {
142     DT->changeImmediateDominator(N, NewIDom);
143   }
144   
145   /// eraseNode - Removes a node from  the dominator tree. Block must not
146   /// domiante any other blocks. Removes node from its immediate dominator's
147   /// children list. Deletes dominator node associated with basic block BB.
148   inline void eraseNode(MachineBasicBlock *BB) {
149     DT->eraseNode(BB);
150   }
151   
152   /// splitBlock - BB is split and now it has one successor. Update dominator
153   /// tree to reflect this change.
154   inline void splitBlock(MachineBasicBlock* NewBB) {
155     DT->splitBlock(NewBB);
156   }
157   
158   
159   virtual void releaseMemory() { 
160     DT->releaseMemory();
161   }
162   
163   virtual void print(std::ostream &OS, const Module* M= 0) const {
164     DT->print(OS, M);
165   }
166 };
167
168 }
169
170 #endif