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"
29 inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
30 this->Roots.push_back(MBB);
33 extern template class DomTreeNodeBase<MachineBasicBlock>;
34 extern template class DominatorTreeBase<MachineBasicBlock>;
36 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
38 //===-------------------------------------
39 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
40 /// compute a normal dominator tree.
42 class MachineDominatorTree : public MachineFunctionPass {
43 /// \brief Helper structure used to hold all the basic blocks
44 /// involved in the split of a critical edge.
46 MachineBasicBlock *FromBB;
47 MachineBasicBlock *ToBB;
48 MachineBasicBlock *NewBB;
51 /// \brief Pile up all the critical edges to be split.
52 /// The splitting of a critical edge is local and thus, it is possible
53 /// to apply several of those changes at the same time.
54 mutable SmallVector<CriticalEdge, 32> CriticalEdgesToSplit;
55 /// \brief Remember all the basic blocks that are inserted during
57 /// Invariant: NewBBs == all the basic blocks contained in the NewBB
58 /// field of all the elements of CriticalEdgesToSplit.
59 /// I.e., forall elt in CriticalEdgesToSplit, it exists BB in NewBBs
60 /// such as BB == elt.NewBB.
61 mutable SmallSet<MachineBasicBlock *, 32> NewBBs;
63 /// The DominatorTreeBase that is used to compute a normal dominator tree
64 std::unique_ptr<DominatorTreeBase<MachineBasicBlock>> DT;
66 /// \brief Apply all the recorded critical edges to the DT.
67 /// This updates the underlying DT information in a way that uses
68 /// the fast query path of DT as much as possible.
70 /// \post CriticalEdgesToSplit.empty().
71 void applySplitCriticalEdges() const;
74 static char ID; // Pass ID, replacement for typeid
76 MachineDominatorTree();
78 DominatorTreeBase<MachineBasicBlock> &getBase() {
80 DT.reset(new DominatorTreeBase<MachineBasicBlock>(false));
81 applySplitCriticalEdges();
85 void getAnalysisUsage(AnalysisUsage &AU) const override;
87 /// getRoots - Return the root blocks of the current CFG. This may include
88 /// multiple blocks if we are computing post dominators. For forward
89 /// dominators, this will always be a single block (the entry node).
91 inline const std::vector<MachineBasicBlock*> &getRoots() const {
92 applySplitCriticalEdges();
93 return DT->getRoots();
96 inline MachineBasicBlock *getRoot() const {
97 applySplitCriticalEdges();
101 inline MachineDomTreeNode *getRootNode() const {
102 applySplitCriticalEdges();
103 return DT->getRootNode();
106 bool runOnMachineFunction(MachineFunction &F) override;
108 inline bool dominates(const MachineDomTreeNode* A,
109 const MachineDomTreeNode* B) const {
110 applySplitCriticalEdges();
111 return DT->dominates(A, B);
114 inline bool dominates(const MachineBasicBlock* A,
115 const MachineBasicBlock* B) const {
116 applySplitCriticalEdges();
117 return DT->dominates(A, B);
120 // dominates - Return true if A dominates B. This performs the
121 // special checks necessary if A and B are in the same basic block.
122 bool dominates(const MachineInstr *A, const MachineInstr *B) const {
123 applySplitCriticalEdges();
124 const MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
125 if (BBA != BBB) return DT->dominates(BBA, BBB);
127 // Loop through the basic block until we find A or B.
128 MachineBasicBlock::const_iterator I = BBA->begin();
129 for (; &*I != A && &*I != B; ++I)
132 //if(!DT.IsPostDominators) {
133 // A dominates B if it is found first in the basic block.
136 // // A post-dominates B if B is found first in the basic block.
141 inline bool properlyDominates(const MachineDomTreeNode* A,
142 const MachineDomTreeNode* B) const {
143 applySplitCriticalEdges();
144 return DT->properlyDominates(A, B);
147 inline bool properlyDominates(const MachineBasicBlock* A,
148 const MachineBasicBlock* B) const {
149 applySplitCriticalEdges();
150 return DT->properlyDominates(A, B);
153 /// findNearestCommonDominator - Find nearest common dominator basic block
154 /// for basic block A and B. If there is no such block then return NULL.
155 inline MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
156 MachineBasicBlock *B) {
157 applySplitCriticalEdges();
158 return DT->findNearestCommonDominator(A, B);
161 inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
162 applySplitCriticalEdges();
163 return DT->getNode(BB);
166 /// getNode - return the (Post)DominatorTree node for the specified basic
167 /// block. This is the same as using operator[] on this class.
169 inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
170 applySplitCriticalEdges();
171 return DT->getNode(BB);
174 /// addNewBlock - Add a new node to the dominator tree information. This
175 /// creates a new node as a child of DomBB dominator node,linking it into
176 /// the children list of the immediate dominator.
177 inline MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
178 MachineBasicBlock *DomBB) {
179 applySplitCriticalEdges();
180 return DT->addNewBlock(BB, DomBB);
183 /// changeImmediateDominator - This method is used to update the dominator
184 /// tree information when a node's immediate dominator changes.
186 inline void changeImmediateDominator(MachineBasicBlock *N,
187 MachineBasicBlock* NewIDom) {
188 applySplitCriticalEdges();
189 DT->changeImmediateDominator(N, NewIDom);
192 inline void changeImmediateDominator(MachineDomTreeNode *N,
193 MachineDomTreeNode* NewIDom) {
194 applySplitCriticalEdges();
195 DT->changeImmediateDominator(N, NewIDom);
198 /// eraseNode - Removes a node from the dominator tree. Block must not
199 /// dominate any other blocks. Removes node from its immediate dominator's
200 /// children list. Deletes dominator node associated with basic block BB.
201 inline void eraseNode(MachineBasicBlock *BB) {
202 applySplitCriticalEdges();
206 /// splitBlock - BB is split and now it has one successor. Update dominator
207 /// tree to reflect this change.
208 inline void splitBlock(MachineBasicBlock* NewBB) {
209 applySplitCriticalEdges();
210 DT->splitBlock(NewBB);
213 /// isReachableFromEntry - Return true if A is dominated by the entry
214 /// block of the function containing it.
215 bool isReachableFromEntry(const MachineBasicBlock *A) {
216 applySplitCriticalEdges();
217 return DT->isReachableFromEntry(A);
220 void releaseMemory() override;
222 void verifyAnalysis() const override;
224 void print(raw_ostream &OS, const Module*) const override;
226 /// \brief Record that the critical edge (FromBB, ToBB) has been
227 /// split with NewBB.
228 /// This is best to use this method instead of directly update the
229 /// underlying information, because this helps mitigating the
230 /// number of time the DT information is invalidated.
232 /// \note Do not use this method with regular edges.
234 /// \note To benefit from the compile time improvement incurred by this
235 /// method, the users of this method have to limit the queries to the DT
236 /// interface between two edges splitting. In other words, they have to
237 /// pack the splitting of critical edges as much as possible.
238 void recordSplitCriticalEdge(MachineBasicBlock *FromBB,
239 MachineBasicBlock *ToBB,
240 MachineBasicBlock *NewBB) {
241 bool Inserted = NewBBs.insert(NewBB).second;
244 "A basic block inserted via edge splitting cannot appear twice");
245 CriticalEdgesToSplit.push_back({FromBB, ToBB, NewBB});
248 /// \brief Verify the correctness of the domtree by re-computing it.
250 /// This should only be used for debugging as it aborts the program if the
251 /// verification fails.
252 void verifyDomTree() const;
255 //===-------------------------------------
256 /// DominatorTree GraphTraits specialization so the DominatorTree can be
257 /// iterable by generic graph iterators.
260 template <class Node, class ChildIterator>
261 struct MachineDomTreeGraphTraitsBase {
262 typedef Node *NodeRef;
263 typedef ChildIterator ChildIteratorType;
265 static NodeRef getEntryNode(NodeRef N) { return N; }
266 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
267 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
270 template <class T> struct GraphTraits;
273 struct GraphTraits<MachineDomTreeNode *>
274 : public MachineDomTreeGraphTraitsBase<MachineDomTreeNode,
275 MachineDomTreeNode::iterator> {};
278 struct GraphTraits<const MachineDomTreeNode *>
279 : public MachineDomTreeGraphTraitsBase<const MachineDomTreeNode,
280 MachineDomTreeNode::const_iterator> {
283 template <> struct GraphTraits<MachineDominatorTree*>
284 : public GraphTraits<MachineDomTreeNode *> {
285 static NodeRef getEntryNode(MachineDominatorTree *DT) {
286 return DT->getRootNode();