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"
27 inline void WriteAsOperand(std::ostream &, const MachineBasicBlock*, bool t) { }
30 inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
31 this->Roots.push_back(MBB);
34 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
35 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<MachineBasicBlock>);
37 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
39 //===-------------------------------------
40 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
41 /// compute a normal dominator tree.
43 class MachineDominatorTree : public MachineFunctionPass {
45 static char ID; // Pass ID, replacement for typeid
46 DominatorTreeBase<MachineBasicBlock>* DT;
48 MachineDominatorTree() : MachineFunctionPass(intptr_t(&ID)) {
49 DT = new DominatorTreeBase<MachineBasicBlock>(false);
52 ~MachineDominatorTree() {
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 MachineFunctionPass::getAnalysisUsage(AU);
62 /// getRoots - Return the root blocks of the current CFG. This may include
63 /// multiple blocks if we are computing post dominators. For forward
64 /// dominators, this will always be a single block (the entry node).
66 inline const std::vector<MachineBasicBlock*> &getRoots() const {
67 return DT->getRoots();
70 inline MachineBasicBlock *getRoot() const {
74 inline MachineDomTreeNode *getRootNode() const {
75 return DT->getRootNode();
78 virtual bool runOnMachineFunction(MachineFunction &F) {
84 inline bool dominates(MachineDomTreeNode* A, MachineDomTreeNode* B) const {
85 return DT->dominates(A, B);
88 inline bool dominates(MachineBasicBlock* A, MachineBasicBlock* B) const {
89 return DT->dominates(A, B);
92 // dominates - Return true if A dominates B. This performs the
93 // special checks necessary if A and B are in the same basic block.
94 bool dominates(MachineInstr *A, MachineInstr *B) const {
95 MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
96 if (BBA != BBB) return DT->dominates(BBA, BBB);
98 // Loop through the basic block until we find A or B.
99 MachineBasicBlock::iterator I = BBA->begin();
100 for (; &*I != A && &*I != B; ++I) /*empty*/;
102 //if(!DT.IsPostDominators) {
103 // A dominates B if it is found first in the basic block.
106 // // A post-dominates B if B is found first in the basic block.
111 inline bool properlyDominates(const MachineDomTreeNode* A,
112 MachineDomTreeNode* B) const {
113 return DT->properlyDominates(A, B);
116 inline bool properlyDominates(MachineBasicBlock* A,
117 MachineBasicBlock* B) const {
118 return DT->properlyDominates(A, B);
121 /// findNearestCommonDominator - Find nearest common dominator basic block
122 /// for basic block A and B. If there is no such block then return NULL.
123 inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
124 MachineBasicBlock *B) {
125 return DT->findNearestCommonDominator(A, B);
128 inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
129 return DT->getNode(BB);
132 /// getNode - return the (Post)DominatorTree node for the specified basic
133 /// block. This is the same as using operator[] on this class.
135 inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
136 return DT->getNode(BB);
139 /// addNewBlock - Add a new node to the dominator tree information. This
140 /// creates a new node as a child of DomBB dominator node,linking it into
141 /// the children list of the immediate dominator.
142 inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
143 MachineBasicBlock *DomBB) {
144 return DT->addNewBlock(BB, DomBB);
147 /// changeImmediateDominator - This method is used to update the dominator
148 /// tree information when a node's immediate dominator changes.
150 inline void changeImmediateDominator(MachineBasicBlock *N,
151 MachineBasicBlock* NewIDom) {
152 DT->changeImmediateDominator(N, NewIDom);
155 inline void changeImmediateDominator(MachineDomTreeNode *N,
156 MachineDomTreeNode* NewIDom) {
157 DT->changeImmediateDominator(N, NewIDom);
160 /// eraseNode - Removes a node from the dominator tree. Block must not
161 /// domiante any other blocks. Removes node from its immediate dominator's
162 /// children list. Deletes dominator node associated with basic block BB.
163 inline void eraseNode(MachineBasicBlock *BB) {
167 /// splitBlock - BB is split and now it has one successor. Update dominator
168 /// tree to reflect this change.
169 inline void splitBlock(MachineBasicBlock* NewBB) {
170 DT->splitBlock(NewBB);
174 virtual void releaseMemory() {
178 virtual void print(std::ostream &OS, const Module* M= 0) const {