LLVM API Documentation
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/ADT/SmallSet.h" 00019 #include "llvm/CodeGen/MachineBasicBlock.h" 00020 #include "llvm/CodeGen/MachineFunction.h" 00021 #include "llvm/CodeGen/MachineFunctionPass.h" 00022 #include "llvm/Support/GenericDomTree.h" 00023 #include "llvm/Support/GenericDomTreeConstruction.h" 00024 00025 namespace llvm { 00026 00027 template<> 00028 inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) { 00029 this->Roots.push_back(MBB); 00030 } 00031 00032 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>); 00033 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<MachineBasicBlock>); 00034 00035 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode; 00036 00037 //===------------------------------------- 00038 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to 00039 /// compute a normal dominator tree. 00040 /// 00041 class MachineDominatorTree : public MachineFunctionPass { 00042 /// \brief Helper structure used to hold all the basic blocks 00043 /// involved in the split of a critical edge. 00044 struct CriticalEdge { 00045 MachineBasicBlock *FromBB; 00046 MachineBasicBlock *ToBB; 00047 MachineBasicBlock *NewBB; 00048 CriticalEdge(MachineBasicBlock *FromBB, MachineBasicBlock *ToBB, 00049 MachineBasicBlock *NewBB) 00050 : FromBB(FromBB), ToBB(ToBB), NewBB(NewBB) {} 00051 }; 00052 00053 /// \brief Pile up all the critical edges to be split. 00054 /// The splitting of a critical edge is local and thus, it is possible 00055 /// to apply several of those changes at the same time. 00056 mutable SmallVector<CriticalEdge, 32> CriticalEdgesToSplit; 00057 /// \brief Remember all the basic blocks that are inserted during 00058 /// edge splitting. 00059 /// Invariant: NewBBs == all the basic blocks contained in the NewBB 00060 /// field of all the elements of CriticalEdgesToSplit. 00061 /// I.e., forall elt in CriticalEdgesToSplit, it exists BB in NewBBs 00062 /// such as BB == elt.NewBB. 00063 mutable SmallSet<MachineBasicBlock *, 32> NewBBs; 00064 00065 /// \brief Apply all the recorded critical edges to the DT. 00066 /// This updates the underlying DT information in a way that uses 00067 /// the fast query path of DT as much as possible. 00068 /// 00069 /// \post CriticalEdgesToSplit.empty(). 00070 void applySplitCriticalEdges() const { 00071 // Bail out early if there is nothing to do. 00072 if (CriticalEdgesToSplit.empty()) 00073 return; 00074 00075 // For each element in CriticalEdgesToSplit, remember whether or 00076 // not element is the new immediate domminator of its successor. 00077 // The mapping is done by index, i.e., the information for the ith 00078 // element of CriticalEdgesToSplit is the ith element of IsNewIDom. 00079 SmallVector<bool, 32> IsNewIDom; 00080 IsNewIDom.resize(CriticalEdgesToSplit.size()); 00081 size_t Idx = 0; 00082 00083 // Collect all the dominance properties info, before invalidating 00084 // the underlying DT. 00085 for (CriticalEdge &Edge : CriticalEdgesToSplit) { 00086 // Update dominator information. 00087 MachineBasicBlock *Succ = Edge.ToBB; 00088 MachineDomTreeNode *SucccDTNode = DT->getNode(Succ); 00089 00090 IsNewIDom[Idx] = true; 00091 for (MachineBasicBlock *PredBB : Succ->predecessors()) { 00092 if (PredBB == Edge.NewBB) 00093 continue; 00094 // If we are in this situation: 00095 // FromBB1 FromBB2 00096 // + + 00097 // + + + + 00098 // + + + + 00099 // ... Split1 Split2 ... 00100 // + + 00101 // + + 00102 // + 00103 // Succ 00104 // Instead of checking the domiance property with Split2, we 00105 // check it with FromBB2 since Split2 is still unknown of the 00106 // underlying DT structure. 00107 if (NewBBs.count(PredBB)) { 00108 assert(PredBB->pred_size() == 1 && "A basic block resulting from a " 00109 "critical edge split has more " 00110 "than one predecessor!"); 00111 PredBB = *PredBB->pred_begin(); 00112 } 00113 if (!DT->dominates(SucccDTNode, DT->getNode(PredBB))) { 00114 IsNewIDom[Idx] = false; 00115 break; 00116 } 00117 } 00118 ++Idx; 00119 } 00120 00121 // Now, update DT with the collected dominance properties info. 00122 Idx = 0; 00123 for (CriticalEdge &Edge : CriticalEdgesToSplit) { 00124 // We know FromBB dominates NewBB. 00125 MachineDomTreeNode *NewDTNode = DT->addNewBlock(Edge.NewBB, Edge.FromBB); 00126 MachineDomTreeNode *SucccDTNode = DT->getNode(Edge.ToBB); 00127 00128 // If all the other predecessors of "Succ" are dominated by "Succ" itself 00129 // then the new block is the new immediate dominator of "Succ". Otherwise, 00130 // the new block doesn't dominate anything. 00131 if (IsNewIDom[Idx]) 00132 DT->changeImmediateDominator(SucccDTNode, NewDTNode); 00133 ++Idx; 00134 } 00135 NewBBs.clear(); 00136 CriticalEdgesToSplit.clear(); 00137 } 00138 00139 public: 00140 static char ID; // Pass ID, replacement for typeid 00141 DominatorTreeBase<MachineBasicBlock>* DT; 00142 00143 MachineDominatorTree(); 00144 00145 ~MachineDominatorTree(); 00146 00147 DominatorTreeBase<MachineBasicBlock> &getBase() { 00148 applySplitCriticalEdges(); 00149 return *DT; 00150 } 00151 00152 void getAnalysisUsage(AnalysisUsage &AU) const override; 00153 00154 /// getRoots - Return the root blocks of the current CFG. This may include 00155 /// multiple blocks if we are computing post dominators. For forward 00156 /// dominators, this will always be a single block (the entry node). 00157 /// 00158 inline const std::vector<MachineBasicBlock*> &getRoots() const { 00159 applySplitCriticalEdges(); 00160 return DT->getRoots(); 00161 } 00162 00163 inline MachineBasicBlock *getRoot() const { 00164 applySplitCriticalEdges(); 00165 return DT->getRoot(); 00166 } 00167 00168 inline MachineDomTreeNode *getRootNode() const { 00169 applySplitCriticalEdges(); 00170 return DT->getRootNode(); 00171 } 00172 00173 bool runOnMachineFunction(MachineFunction &F) override; 00174 00175 inline bool dominates(const MachineDomTreeNode* A, 00176 const MachineDomTreeNode* B) const { 00177 applySplitCriticalEdges(); 00178 return DT->dominates(A, B); 00179 } 00180 00181 inline bool dominates(const MachineBasicBlock* A, 00182 const MachineBasicBlock* B) const { 00183 applySplitCriticalEdges(); 00184 return DT->dominates(A, B); 00185 } 00186 00187 // dominates - Return true if A dominates B. This performs the 00188 // special checks necessary if A and B are in the same basic block. 00189 bool dominates(const MachineInstr *A, const MachineInstr *B) const { 00190 applySplitCriticalEdges(); 00191 const MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent(); 00192 if (BBA != BBB) return DT->dominates(BBA, BBB); 00193 00194 // Loop through the basic block until we find A or B. 00195 MachineBasicBlock::const_iterator I = BBA->begin(); 00196 for (; &*I != A && &*I != B; ++I) 00197 /*empty*/ ; 00198 00199 //if(!DT.IsPostDominators) { 00200 // A dominates B if it is found first in the basic block. 00201 return &*I == A; 00202 //} else { 00203 // // A post-dominates B if B is found first in the basic block. 00204 // return &*I == B; 00205 //} 00206 } 00207 00208 inline bool properlyDominates(const MachineDomTreeNode* A, 00209 const MachineDomTreeNode* B) const { 00210 applySplitCriticalEdges(); 00211 return DT->properlyDominates(A, B); 00212 } 00213 00214 inline bool properlyDominates(const MachineBasicBlock* A, 00215 const MachineBasicBlock* B) const { 00216 applySplitCriticalEdges(); 00217 return DT->properlyDominates(A, B); 00218 } 00219 00220 /// findNearestCommonDominator - Find nearest common dominator basic block 00221 /// for basic block A and B. If there is no such block then return NULL. 00222 inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A, 00223 MachineBasicBlock *B) { 00224 applySplitCriticalEdges(); 00225 return DT->findNearestCommonDominator(A, B); 00226 } 00227 00228 inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const { 00229 applySplitCriticalEdges(); 00230 return DT->getNode(BB); 00231 } 00232 00233 /// getNode - return the (Post)DominatorTree node for the specified basic 00234 /// block. This is the same as using operator[] on this class. 00235 /// 00236 inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const { 00237 applySplitCriticalEdges(); 00238 return DT->getNode(BB); 00239 } 00240 00241 /// addNewBlock - Add a new node to the dominator tree information. This 00242 /// creates a new node as a child of DomBB dominator node,linking it into 00243 /// the children list of the immediate dominator. 00244 inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB, 00245 MachineBasicBlock *DomBB) { 00246 applySplitCriticalEdges(); 00247 return DT->addNewBlock(BB, DomBB); 00248 } 00249 00250 /// changeImmediateDominator - This method is used to update the dominator 00251 /// tree information when a node's immediate dominator changes. 00252 /// 00253 inline void changeImmediateDominator(MachineBasicBlock *N, 00254 MachineBasicBlock* NewIDom) { 00255 applySplitCriticalEdges(); 00256 DT->changeImmediateDominator(N, NewIDom); 00257 } 00258 00259 inline void changeImmediateDominator(MachineDomTreeNode *N, 00260 MachineDomTreeNode* NewIDom) { 00261 applySplitCriticalEdges(); 00262 DT->changeImmediateDominator(N, NewIDom); 00263 } 00264 00265 /// eraseNode - Removes a node from the dominator tree. Block must not 00266 /// dominate any other blocks. Removes node from its immediate dominator's 00267 /// children list. Deletes dominator node associated with basic block BB. 00268 inline void eraseNode(MachineBasicBlock *BB) { 00269 applySplitCriticalEdges(); 00270 DT->eraseNode(BB); 00271 } 00272 00273 /// splitBlock - BB is split and now it has one successor. Update dominator 00274 /// tree to reflect this change. 00275 inline void splitBlock(MachineBasicBlock* NewBB) { 00276 applySplitCriticalEdges(); 00277 DT->splitBlock(NewBB); 00278 } 00279 00280 /// isReachableFromEntry - Return true if A is dominated by the entry 00281 /// block of the function containing it. 00282 bool isReachableFromEntry(const MachineBasicBlock *A) { 00283 applySplitCriticalEdges(); 00284 return DT->isReachableFromEntry(A); 00285 } 00286 00287 void releaseMemory() override; 00288 00289 void print(raw_ostream &OS, const Module*) const override; 00290 00291 /// \brief Record that the critical edge (FromBB, ToBB) has been 00292 /// split with NewBB. 00293 /// This is best to use this method instead of directly update the 00294 /// underlying information, because this helps mitigating the 00295 /// number of time the DT information is invalidated. 00296 /// 00297 /// \note Do not use this method with regular edges. 00298 /// 00299 /// \note To benefit from the compile time improvement incurred by this 00300 /// method, the users of this method have to limit the queries to the DT 00301 /// interface between two edges splitting. In other words, they have to 00302 /// pack the splitting of critical edges as much as possible. 00303 void recordSplitCriticalEdge(MachineBasicBlock *FromBB, 00304 MachineBasicBlock *ToBB, 00305 MachineBasicBlock *NewBB) { 00306 bool Inserted = NewBBs.insert(NewBB); 00307 (void)Inserted; 00308 assert(Inserted && 00309 "A basic block inserted via edge splitting cannot appear twice"); 00310 CriticalEdgesToSplit.push_back(CriticalEdge(FromBB, ToBB, NewBB)); 00311 } 00312 }; 00313 00314 //===------------------------------------- 00315 /// DominatorTree GraphTraits specialization so the DominatorTree can be 00316 /// iterable by generic graph iterators. 00317 /// 00318 00319 template<class T> struct GraphTraits; 00320 00321 template <> struct GraphTraits<MachineDomTreeNode *> { 00322 typedef MachineDomTreeNode NodeType; 00323 typedef NodeType::iterator ChildIteratorType; 00324 00325 static NodeType *getEntryNode(NodeType *N) { 00326 return N; 00327 } 00328 static inline ChildIteratorType child_begin(NodeType* N) { 00329 return N->begin(); 00330 } 00331 static inline ChildIteratorType child_end(NodeType* N) { 00332 return N->end(); 00333 } 00334 }; 00335 00336 template <> struct GraphTraits<MachineDominatorTree*> 00337 : public GraphTraits<MachineDomTreeNode *> { 00338 static NodeType *getEntryNode(MachineDominatorTree *DT) { 00339 return DT->getRootNode(); 00340 } 00341 }; 00342 00343 } 00344 00345 #endif