1 //=- llvm/CodeGen/MachineDominators.h - Machine Dom Calculation --*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
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.
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/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"
28 inline void WriteAsOperand(std::ostream &, const MachineBasicBlock*, bool t) { }
31 inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
32 this->Roots.push_back(MBB);
35 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
36 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<MachineBasicBlock>);
38 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
40 //===-------------------------------------
41 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
42 /// compute a normal dominator tree.
44 class MachineDominatorTree : public MachineFunctionPass {
46 static char ID; // Pass ID, replacement for typeid
47 DominatorTreeBase<MachineBasicBlock>* DT;
49 MachineDominatorTree() : MachineFunctionPass(intptr_t(&ID)) {
50 DT = new DominatorTreeBase<MachineBasicBlock>(false);
53 ~MachineDominatorTree() {
58 DominatorTreeBase<MachineBasicBlock>& getBase() { return *DT; }
60 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
62 MachineFunctionPass::getAnalysisUsage(AU);
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).
69 inline const std::vector<MachineBasicBlock*> &getRoots() const {
70 return DT->getRoots();
73 inline MachineBasicBlock *getRoot() const {
77 inline MachineDomTreeNode *getRootNode() const {
78 return DT->getRootNode();
81 virtual bool runOnMachineFunction(MachineFunction &F) {
87 inline bool dominates(MachineDomTreeNode* A, MachineDomTreeNode* B) const {
88 return DT->dominates(A, B);
91 inline bool dominates(MachineBasicBlock* A, MachineBasicBlock* B) const {
92 return DT->dominates(A, B);
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);
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*/;
105 //if(!DT.IsPostDominators) {
106 // A dominates B if it is found first in the basic block.
109 // // A post-dominates B if B is found first in the basic block.
114 inline bool properlyDominates(const MachineDomTreeNode* A,
115 MachineDomTreeNode* B) const {
116 return DT->properlyDominates(A, B);
119 inline bool properlyDominates(MachineBasicBlock* A,
120 MachineBasicBlock* B) const {
121 return DT->properlyDominates(A, B);
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);
131 inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
132 return DT->getNode(BB);
135 /// getNode - return the (Post)DominatorTree node for the specified basic
136 /// block. This is the same as using operator[] on this class.
138 inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
139 return DT->getNode(BB);
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);
150 /// changeImmediateDominator - This method is used to update the dominator
151 /// tree information when a node's immediate dominator changes.
153 inline void changeImmediateDominator(MachineBasicBlock *N,
154 MachineBasicBlock* NewIDom) {
155 DT->changeImmediateDominator(N, NewIDom);
158 inline void changeImmediateDominator(MachineDomTreeNode *N,
159 MachineDomTreeNode* NewIDom) {
160 DT->changeImmediateDominator(N, NewIDom);
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) {
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);
177 virtual void releaseMemory() {
181 virtual void print(std::ostream &OS, const Module* M= 0) const {
186 //===-------------------------------------
187 /// DominatorTree GraphTraits specialization so the DominatorTree can be
188 /// iterable by generic graph iterators.
190 template <> struct GraphTraits<MachineDomTreeNode *> {
191 typedef MachineDomTreeNode NodeType;
192 typedef NodeType::iterator ChildIteratorType;
194 static NodeType *getEntryNode(NodeType *N) {
197 static inline ChildIteratorType child_begin(NodeType* N) {
200 static inline ChildIteratorType child_end(NodeType* N) {
205 template <> struct GraphTraits<MachineDominatorTree*>
206 : public GraphTraits<MachineDomTreeNode *> {
207 static NodeType *getEntryNode(MachineDominatorTree *DT) {
208 return DT->getRootNode();