1 //=- llvm/CodeGen/MachineDominators.h - Machine Dom Calculation --*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/ADT/SmallSet.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/Support/GenericDomTree.h"
23 #include "llvm/Support/GenericDomTreeConstruction.h"
28 inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
29 this->Roots.push_back(MBB);
32 extern template class DomTreeNodeBase<MachineBasicBlock>;
33 extern template class DominatorTreeBase<MachineBasicBlock>;
35 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
37 //===-------------------------------------
38 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
39 /// compute a normal dominator tree.
41 class MachineDominatorTree : public MachineFunctionPass {
42 /// \brief Helper structure used to hold all the basic blocks
43 /// involved in the split of a critical edge.
45 MachineBasicBlock *FromBB;
46 MachineBasicBlock *ToBB;
47 MachineBasicBlock *NewBB;
50 /// \brief Pile up all the critical edges to be split.
51 /// The splitting of a critical edge is local and thus, it is possible
52 /// to apply several of those changes at the same time.
53 mutable SmallVector<CriticalEdge, 32> CriticalEdgesToSplit;
54 /// \brief Remember all the basic blocks that are inserted during
56 /// Invariant: NewBBs == all the basic blocks contained in the NewBB
57 /// field of all the elements of CriticalEdgesToSplit.
58 /// I.e., forall elt in CriticalEdgesToSplit, it exists BB in NewBBs
59 /// such as BB == elt.NewBB.
60 mutable SmallSet<MachineBasicBlock *, 32> NewBBs;
62 /// The DominatorTreeBase that is used to compute a normal dominator tree
63 DominatorTreeBase<MachineBasicBlock>* DT;
65 /// \brief Apply all the recorded critical edges to the DT.
66 /// This updates the underlying DT information in a way that uses
67 /// the fast query path of DT as much as possible.
69 /// \post CriticalEdgesToSplit.empty().
70 void applySplitCriticalEdges() const;
73 static char ID; // Pass ID, replacement for typeid
75 MachineDominatorTree();
77 ~MachineDominatorTree() override;
79 DominatorTreeBase<MachineBasicBlock> &getBase() {
80 applySplitCriticalEdges();
84 void getAnalysisUsage(AnalysisUsage &AU) const override;
86 /// getRoots - Return the root blocks of the current CFG. This may include
87 /// multiple blocks if we are computing post dominators. For forward
88 /// dominators, this will always be a single block (the entry node).
90 inline const std::vector<MachineBasicBlock*> &getRoots() const {
91 applySplitCriticalEdges();
92 return DT->getRoots();
95 inline MachineBasicBlock *getRoot() const {
96 applySplitCriticalEdges();
100 inline MachineDomTreeNode *getRootNode() const {
101 applySplitCriticalEdges();
102 return DT->getRootNode();
105 bool runOnMachineFunction(MachineFunction &F) override;
107 inline bool dominates(const MachineDomTreeNode* A,
108 const MachineDomTreeNode* B) const {
109 applySplitCriticalEdges();
110 return DT->dominates(A, B);
113 inline bool dominates(const MachineBasicBlock* A,
114 const MachineBasicBlock* B) const {
115 applySplitCriticalEdges();
116 return DT->dominates(A, B);
119 // dominates - Return true if A dominates B. This performs the
120 // special checks necessary if A and B are in the same basic block.
121 bool dominates(const MachineInstr *A, const MachineInstr *B) const {
122 applySplitCriticalEdges();
123 const MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
124 if (BBA != BBB) return DT->dominates(BBA, BBB);
126 // Loop through the basic block until we find A or B.
127 MachineBasicBlock::const_iterator I = BBA->begin();
128 for (; &*I != A && &*I != B; ++I)
131 //if(!DT.IsPostDominators) {
132 // A dominates B if it is found first in the basic block.
135 // // A post-dominates B if B is found first in the basic block.
140 inline bool properlyDominates(const MachineDomTreeNode* A,
141 const MachineDomTreeNode* B) const {
142 applySplitCriticalEdges();
143 return DT->properlyDominates(A, B);
146 inline bool properlyDominates(const MachineBasicBlock* A,
147 const MachineBasicBlock* B) const {
148 applySplitCriticalEdges();
149 return DT->properlyDominates(A, B);
152 /// findNearestCommonDominator - Find nearest common dominator basic block
153 /// for basic block A and B. If there is no such block then return NULL.
154 inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
155 MachineBasicBlock *B) {
156 applySplitCriticalEdges();
157 return DT->findNearestCommonDominator(A, B);
160 inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
161 applySplitCriticalEdges();
162 return DT->getNode(BB);
165 /// getNode - return the (Post)DominatorTree node for the specified basic
166 /// block. This is the same as using operator[] on this class.
168 inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
169 applySplitCriticalEdges();
170 return DT->getNode(BB);
173 /// addNewBlock - Add a new node to the dominator tree information. This
174 /// creates a new node as a child of DomBB dominator node,linking it into
175 /// the children list of the immediate dominator.
176 inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
177 MachineBasicBlock *DomBB) {
178 applySplitCriticalEdges();
179 return DT->addNewBlock(BB, DomBB);
182 /// changeImmediateDominator - This method is used to update the dominator
183 /// tree information when a node's immediate dominator changes.
185 inline void changeImmediateDominator(MachineBasicBlock *N,
186 MachineBasicBlock* NewIDom) {
187 applySplitCriticalEdges();
188 DT->changeImmediateDominator(N, NewIDom);
191 inline void changeImmediateDominator(MachineDomTreeNode *N,
192 MachineDomTreeNode* NewIDom) {
193 applySplitCriticalEdges();
194 DT->changeImmediateDominator(N, NewIDom);
197 /// eraseNode - Removes a node from the dominator tree. Block must not
198 /// dominate any other blocks. Removes node from its immediate dominator's
199 /// children list. Deletes dominator node associated with basic block BB.
200 inline void eraseNode(MachineBasicBlock *BB) {
201 applySplitCriticalEdges();
205 /// splitBlock - BB is split and now it has one successor. Update dominator
206 /// tree to reflect this change.
207 inline void splitBlock(MachineBasicBlock* NewBB) {
208 applySplitCriticalEdges();
209 DT->splitBlock(NewBB);
212 /// isReachableFromEntry - Return true if A is dominated by the entry
213 /// block of the function containing it.
214 bool isReachableFromEntry(const MachineBasicBlock *A) {
215 applySplitCriticalEdges();
216 return DT->isReachableFromEntry(A);
219 void releaseMemory() override;
221 void verifyAnalysis() const override;
223 void print(raw_ostream &OS, const Module*) const override;
225 /// \brief Record that the critical edge (FromBB, ToBB) has been
226 /// split with NewBB.
227 /// This is best to use this method instead of directly update the
228 /// underlying information, because this helps mitigating the
229 /// number of time the DT information is invalidated.
231 /// \note Do not use this method with regular edges.
233 /// \note To benefit from the compile time improvement incurred by this
234 /// method, the users of this method have to limit the queries to the DT
235 /// interface between two edges splitting. In other words, they have to
236 /// pack the splitting of critical edges as much as possible.
237 void recordSplitCriticalEdge(MachineBasicBlock *FromBB,
238 MachineBasicBlock *ToBB,
239 MachineBasicBlock *NewBB) {
240 bool Inserted = NewBBs.insert(NewBB).second;
243 "A basic block inserted via edge splitting cannot appear twice");
244 CriticalEdgesToSplit.push_back({FromBB, ToBB, NewBB});
247 /// \brief Returns *false* if the other dominator tree matches this dominator
249 inline bool compare(const MachineDominatorTree &Other) const {
250 const MachineDomTreeNode *R = getRootNode();
251 const MachineDomTreeNode *OtherR = Other.getRootNode();
253 if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
256 if (DT->compare(*Other.DT))
262 /// \brief Verify the correctness of the domtree by re-computing it.
264 /// This should only be used for debugging as it aborts the program if the
265 /// verification fails.
266 void verifyDomTree() const;
269 //===-------------------------------------
270 /// DominatorTree GraphTraits specialization so the DominatorTree can be
271 /// iterable by generic graph iterators.
274 template <class Node, class ChildIterator>
275 struct MachineDomTreeGraphTraitsBase {
276 typedef Node *NodeRef;
277 typedef ChildIterator ChildIteratorType;
279 static NodeRef getEntryNode(NodeRef N) { return N; }
280 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
281 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
284 template <class T> struct GraphTraits;
287 struct GraphTraits<MachineDomTreeNode *>
288 : public MachineDomTreeGraphTraitsBase<MachineDomTreeNode,
289 MachineDomTreeNode::iterator> {};
292 struct GraphTraits<const MachineDomTreeNode *>
293 : public MachineDomTreeGraphTraitsBase<const MachineDomTreeNode,
294 MachineDomTreeNode::const_iterator> {
297 template <> struct GraphTraits<MachineDominatorTree*>
298 : public GraphTraits<MachineDomTreeNode *> {
299 static NodeRef getEntryNode(MachineDominatorTree *DT) {
300 return DT->getRootNode();