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/CodeGen/MachineFunctionPass.h"
00019 #include "llvm/Analysis/Dominators.h"
00020 #include "llvm/Analysis/DominatorInternals.h"
00021 
00022 namespace llvm {
00023 
00024 inline void WriteAsOperand(std::ostream &, const MachineBasicBlock*, bool t) {  }
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(MachineDomTreeNode* A, MachineDomTreeNode* B) const {
00072     return DT->dominates(A, B);
00073   }
00074   
00075   inline bool dominates(MachineBasicBlock* A, MachineBasicBlock* B) const {
00076     return DT->dominates(A, B);
00077   }
00078   
00079   // dominates - Return true if A dominates B. This performs the
00080   // special checks necessary if A and B are in the same basic block.
00081   bool dominates(MachineInstr *A, MachineInstr *B) const {
00082     MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
00083     if (BBA != BBB) return DT->dominates(BBA, BBB);
00084 
00085     // Loop through the basic block until we find A or B.
00086     MachineBasicBlock::iterator I = BBA->begin();
00087     for (; &*I != A && &*I != B; ++I) /*empty*/;
00088 
00089     //if(!DT.IsPostDominators) {
00090       // A dominates B if it is found first in the basic block.
00091       return &*I == A;
00092     //} else {
00093     //  // A post-dominates B if B is found first in the basic block.
00094     //  return &*I == B;
00095     //}
00096   }
00097   
00098   inline bool properlyDominates(const MachineDomTreeNode* A,
00099                                 MachineDomTreeNode* B) const {
00100     return DT->properlyDominates(A, B);
00101   }
00102   
00103   inline bool properlyDominates(MachineBasicBlock* A,
00104                                 MachineBasicBlock* B) const {
00105     return DT->properlyDominates(A, B);
00106   }
00107   
00108   /// findNearestCommonDominator - Find nearest common dominator basic block
00109   /// for basic block A and B. If there is no such block then return NULL.
00110   inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
00111                                                        MachineBasicBlock *B) {
00112     return DT->findNearestCommonDominator(A, B);
00113   }
00114   
00115   inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
00116     return DT->getNode(BB);
00117   }
00118   
00119   /// getNode - return the (Post)DominatorTree node for the specified basic
00120   /// block.  This is the same as using operator[] on this class.
00121   ///
00122   inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
00123     return DT->getNode(BB);
00124   }
00125   
00126   /// addNewBlock - Add a new node to the dominator tree information.  This
00127   /// creates a new node as a child of DomBB dominator node,linking it into 
00128   /// the children list of the immediate dominator.
00129   inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
00130                                          MachineBasicBlock *DomBB) {
00131     return DT->addNewBlock(BB, DomBB);
00132   }
00133   
00134   /// changeImmediateDominator - This method is used to update the dominator
00135   /// tree information when a node's immediate dominator changes.
00136   ///
00137   inline void changeImmediateDominator(MachineBasicBlock *N,
00138                                        MachineBasicBlock* NewIDom) {
00139     DT->changeImmediateDominator(N, NewIDom);
00140   }
00141   
00142   inline void changeImmediateDominator(MachineDomTreeNode *N,
00143                                        MachineDomTreeNode* NewIDom) {
00144     DT->changeImmediateDominator(N, NewIDom);
00145   }
00146   
00147   /// eraseNode - Removes a node from  the dominator tree. Block must not
00148   /// domiante any other blocks. Removes node from its immediate dominator's
00149   /// children list. Deletes dominator node associated with basic block BB.
00150   inline void eraseNode(MachineBasicBlock *BB) {
00151     DT->eraseNode(BB);
00152   }
00153   
00154   /// splitBlock - BB is split and now it has one successor. Update dominator
00155   /// tree to reflect this change.
00156   inline void splitBlock(MachineBasicBlock* NewBB) {
00157     DT->splitBlock(NewBB);
00158   }
00159   
00160   
00161   virtual void releaseMemory();
00162   
00163   virtual void print(std::ostream &OS, const Module* M= 0) const {
00164     DT->print(OS, M);
00165   }
00166 };
00167 
00168 //===-------------------------------------
00169 /// DominatorTree GraphTraits specialization so the DominatorTree can be
00170 /// iterable by generic graph iterators.
00171 ///
00172 
00173 template<class T> struct GraphTraits;
00174 
00175 template <> struct GraphTraits<MachineDomTreeNode *> {
00176   typedef MachineDomTreeNode NodeType;
00177   typedef NodeType::iterator  ChildIteratorType;
00178   
00179   static NodeType *getEntryNode(NodeType *N) {
00180     return N;
00181   }
00182   static inline ChildIteratorType child_begin(NodeType* N) {
00183     return N->begin();
00184   }
00185   static inline ChildIteratorType child_end(NodeType* N) {
00186     return N->end();
00187   }
00188 };
00189 
00190 template <> struct GraphTraits<MachineDominatorTree*>
00191   : public GraphTraits<MachineDomTreeNode *> {
00192   static NodeType *getEntryNode(MachineDominatorTree *DT) {
00193     return DT->getRootNode();
00194   }
00195 };
00196 
00197 }
00198 
00199 #endif