LLVM API Documentation

MachineDominators.h
Go to the documentation of this file.
00001 //=- llvm/CodeGen/MachineDominators.h - Machine Dom Calculation --*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines classes mirroring those in llvm/Analysis/Dominators.h,
00011 // but for target-specific code rather than target-independent IR.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CODEGEN_MACHINEDOMINATORS_H
00016 #define LLVM_CODEGEN_MACHINEDOMINATORS_H
00017 
00018 #include "llvm/Analysis/DominatorInternals.h"
00019 #include "llvm/Analysis/Dominators.h"
00020 #include "llvm/CodeGen/MachineBasicBlock.h"
00021 #include "llvm/CodeGen/MachineFunction.h"
00022 #include "llvm/CodeGen/MachineFunctionPass.h"
00023 
00024 namespace llvm {
00025 
00026 template<>
00027 inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
00028   this->Roots.push_back(MBB);
00029 }
00030 
00031 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
00032 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<MachineBasicBlock>);
00033 
00034 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
00035 
00036 //===-------------------------------------
00037 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
00038 /// compute a normal dominator tree.
00039 ///
00040 class MachineDominatorTree : public MachineFunctionPass {
00041 public:
00042   static char ID; // Pass ID, replacement for typeid
00043   DominatorTreeBase<MachineBasicBlock>* DT;
00044 
00045   MachineDominatorTree();
00046 
00047   ~MachineDominatorTree();
00048 
00049   DominatorTreeBase<MachineBasicBlock>& getBase() { return *DT; }
00050 
00051   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
00052 
00053   /// getRoots -  Return the root blocks of the current CFG.  This may include
00054   /// multiple blocks if we are computing post dominators.  For forward
00055   /// dominators, this will always be a single block (the entry node).
00056   ///
00057   inline const std::vector<MachineBasicBlock*> &getRoots() const {
00058     return DT->getRoots();
00059   }
00060 
00061   inline MachineBasicBlock *getRoot() const {
00062     return DT->getRoot();
00063   }
00064 
00065   inline MachineDomTreeNode *getRootNode() const {
00066     return DT->getRootNode();
00067   }
00068 
00069   virtual bool runOnMachineFunction(MachineFunction &F);
00070 
00071   inline bool dominates(const MachineDomTreeNode* A,
00072                         const MachineDomTreeNode* B) const {
00073     return DT->dominates(A, B);
00074   }
00075 
00076   inline bool dominates(const MachineBasicBlock* A,
00077                         const MachineBasicBlock* B) const {
00078     return DT->dominates(A, B);
00079   }
00080 
00081   // dominates - Return true if A dominates B. This performs the
00082   // special checks necessary if A and B are in the same basic block.
00083   bool dominates(const MachineInstr *A, const MachineInstr *B) const {
00084     const MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
00085     if (BBA != BBB) return DT->dominates(BBA, BBB);
00086 
00087     // Loop through the basic block until we find A or B.
00088     MachineBasicBlock::const_iterator I = BBA->begin();
00089     for (; &*I != A && &*I != B; ++I)
00090       /*empty*/ ;
00091 
00092     //if(!DT.IsPostDominators) {
00093       // A dominates B if it is found first in the basic block.
00094       return &*I == A;
00095     //} else {
00096     //  // A post-dominates B if B is found first in the basic block.
00097     //  return &*I == B;
00098     //}
00099   }
00100 
00101   inline bool properlyDominates(const MachineDomTreeNode* A,
00102                                 const MachineDomTreeNode* B) const {
00103     return DT->properlyDominates(A, B);
00104   }
00105 
00106   inline bool properlyDominates(const MachineBasicBlock* A,
00107                                 const MachineBasicBlock* B) const {
00108     return DT->properlyDominates(A, B);
00109   }
00110 
00111   /// findNearestCommonDominator - Find nearest common dominator basic block
00112   /// for basic block A and B. If there is no such block then return NULL.
00113   inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
00114                                                        MachineBasicBlock *B) {
00115     return DT->findNearestCommonDominator(A, B);
00116   }
00117 
00118   inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
00119     return DT->getNode(BB);
00120   }
00121 
00122   /// getNode - return the (Post)DominatorTree node for the specified basic
00123   /// block.  This is the same as using operator[] on this class.
00124   ///
00125   inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
00126     return DT->getNode(BB);
00127   }
00128 
00129   /// addNewBlock - Add a new node to the dominator tree information.  This
00130   /// creates a new node as a child of DomBB dominator node,linking it into
00131   /// the children list of the immediate dominator.
00132   inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
00133                                          MachineBasicBlock *DomBB) {
00134     return DT->addNewBlock(BB, DomBB);
00135   }
00136 
00137   /// changeImmediateDominator - This method is used to update the dominator
00138   /// tree information when a node's immediate dominator changes.
00139   ///
00140   inline void changeImmediateDominator(MachineBasicBlock *N,
00141                                        MachineBasicBlock* NewIDom) {
00142     DT->changeImmediateDominator(N, NewIDom);
00143   }
00144 
00145   inline void changeImmediateDominator(MachineDomTreeNode *N,
00146                                        MachineDomTreeNode* NewIDom) {
00147     DT->changeImmediateDominator(N, NewIDom);
00148   }
00149 
00150   /// eraseNode - Removes a node from  the dominator tree. Block must not
00151   /// dominate any other blocks. Removes node from its immediate dominator's
00152   /// children list. Deletes dominator node associated with basic block BB.
00153   inline void eraseNode(MachineBasicBlock *BB) {
00154     DT->eraseNode(BB);
00155   }
00156 
00157   /// splitBlock - BB is split and now it has one successor. Update dominator
00158   /// tree to reflect this change.
00159   inline void splitBlock(MachineBasicBlock* NewBB) {
00160     DT->splitBlock(NewBB);
00161   }
00162 
00163   /// isReachableFromEntry - Return true if A is dominated by the entry
00164   /// block of the function containing it.
00165   bool isReachableFromEntry(const MachineBasicBlock *A) {
00166     return DT->isReachableFromEntry(A);
00167   }
00168 
00169   virtual void releaseMemory();
00170 
00171   virtual void print(raw_ostream &OS, const Module*) const;
00172 };
00173 
00174 //===-------------------------------------
00175 /// DominatorTree GraphTraits specialization so the DominatorTree can be
00176 /// iterable by generic graph iterators.
00177 ///
00178 
00179 template<class T> struct GraphTraits;
00180 
00181 template <> struct GraphTraits<MachineDomTreeNode *> {
00182   typedef MachineDomTreeNode NodeType;
00183   typedef NodeType::iterator  ChildIteratorType;
00184 
00185   static NodeType *getEntryNode(NodeType *N) {
00186     return N;
00187   }
00188   static inline ChildIteratorType child_begin(NodeType* N) {
00189     return N->begin();
00190   }
00191   static inline ChildIteratorType child_end(NodeType* N) {
00192     return N->end();
00193   }
00194 };
00195 
00196 template <> struct GraphTraits<MachineDominatorTree*>
00197   : public GraphTraits<MachineDomTreeNode *> {
00198   static NodeType *getEntryNode(MachineDominatorTree *DT) {
00199     return DT->getRootNode();
00200   }
00201 };
00202 
00203 }
00204 
00205 #endif