]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/CodeGen/MachineDominators.h
Merge lldb trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / CodeGen / MachineDominators.h
1 //=- llvm/CodeGen/MachineDominators.h - Machine Dom Calculation --*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines classes mirroring those in llvm/Analysis/Dominators.h,
11 // but for target-specific code rather than target-independent IR.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_MACHINEDOMINATORS_H
16 #define LLVM_CODEGEN_MACHINEDOMINATORS_H
17
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"
24 #include <memory>
25
26 namespace llvm {
27
28 template<>
29 inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
30   this->Roots.push_back(MBB);
31 }
32
33 extern template class DomTreeNodeBase<MachineBasicBlock>;
34 extern template class DominatorTreeBase<MachineBasicBlock>;
35
36 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
37
38 //===-------------------------------------
39 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
40 /// compute a normal dominator tree.
41 ///
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.
45   struct CriticalEdge {
46     MachineBasicBlock *FromBB;
47     MachineBasicBlock *ToBB;
48     MachineBasicBlock *NewBB;
49   };
50
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
56   /// edge splitting.
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;
62
63   /// The DominatorTreeBase that is used to compute a normal dominator tree
64   std::unique_ptr<DominatorTreeBase<MachineBasicBlock>> DT;
65
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.
69   ///
70   /// \post CriticalEdgesToSplit.empty().
71   void applySplitCriticalEdges() const;
72
73 public:
74   static char ID; // Pass ID, replacement for typeid
75
76   MachineDominatorTree();
77
78   DominatorTreeBase<MachineBasicBlock> &getBase() {
79     if (!DT)
80       DT.reset(new DominatorTreeBase<MachineBasicBlock>(false));
81     applySplitCriticalEdges();
82     return *DT;
83   }
84
85   void getAnalysisUsage(AnalysisUsage &AU) const override;
86
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).
90   ///
91   inline const std::vector<MachineBasicBlock*> &getRoots() const {
92     applySplitCriticalEdges();
93     return DT->getRoots();
94   }
95
96   inline MachineBasicBlock *getRoot() const {
97     applySplitCriticalEdges();
98     return DT->getRoot();
99   }
100
101   inline MachineDomTreeNode *getRootNode() const {
102     applySplitCriticalEdges();
103     return DT->getRootNode();
104   }
105
106   bool runOnMachineFunction(MachineFunction &F) override;
107
108   inline bool dominates(const MachineDomTreeNode* A,
109                         const MachineDomTreeNode* B) const {
110     applySplitCriticalEdges();
111     return DT->dominates(A, B);
112   }
113
114   inline bool dominates(const MachineBasicBlock* A,
115                         const MachineBasicBlock* B) const {
116     applySplitCriticalEdges();
117     return DT->dominates(A, B);
118   }
119
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);
126
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)
130       /*empty*/ ;
131
132     //if(!DT.IsPostDominators) {
133       // A dominates B if it is found first in the basic block.
134       return &*I == A;
135     //} else {
136     //  // A post-dominates B if B is found first in the basic block.
137     //  return &*I == B;
138     //}
139   }
140
141   inline bool properlyDominates(const MachineDomTreeNode* A,
142                                 const MachineDomTreeNode* B) const {
143     applySplitCriticalEdges();
144     return DT->properlyDominates(A, B);
145   }
146
147   inline bool properlyDominates(const MachineBasicBlock* A,
148                                 const MachineBasicBlock* B) const {
149     applySplitCriticalEdges();
150     return DT->properlyDominates(A, B);
151   }
152
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);
159   }
160
161   inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
162     applySplitCriticalEdges();
163     return DT->getNode(BB);
164   }
165
166   /// getNode - return the (Post)DominatorTree node for the specified basic
167   /// block.  This is the same as using operator[] on this class.
168   ///
169   inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
170     applySplitCriticalEdges();
171     return DT->getNode(BB);
172   }
173
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);
181   }
182
183   /// changeImmediateDominator - This method is used to update the dominator
184   /// tree information when a node's immediate dominator changes.
185   ///
186   inline void changeImmediateDominator(MachineBasicBlock *N,
187                                        MachineBasicBlock* NewIDom) {
188     applySplitCriticalEdges();
189     DT->changeImmediateDominator(N, NewIDom);
190   }
191
192   inline void changeImmediateDominator(MachineDomTreeNode *N,
193                                        MachineDomTreeNode* NewIDom) {
194     applySplitCriticalEdges();
195     DT->changeImmediateDominator(N, NewIDom);
196   }
197
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();
203     DT->eraseNode(BB);
204   }
205
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);
211   }
212
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);
218   }
219
220   void releaseMemory() override;
221
222   void verifyAnalysis() const override;
223
224   void print(raw_ostream &OS, const Module*) const override;
225
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.
231   ///
232   /// \note Do not use this method with regular edges.
233   ///
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;
242     (void)Inserted;
243     assert(Inserted &&
244            "A basic block inserted via edge splitting cannot appear twice");
245     CriticalEdgesToSplit.push_back({FromBB, ToBB, NewBB});
246   }
247
248   /// \brief Verify the correctness of the domtree by re-computing it.
249   ///
250   /// This should only be used for debugging as it aborts the program if the
251   /// verification fails.
252   void verifyDomTree() const;
253 };
254
255 //===-------------------------------------
256 /// DominatorTree GraphTraits specialization so the DominatorTree can be
257 /// iterable by generic graph iterators.
258 ///
259
260 template <class Node, class ChildIterator>
261 struct MachineDomTreeGraphTraitsBase {
262   typedef Node *NodeRef;
263   typedef ChildIterator ChildIteratorType;
264
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(); }
268 };
269
270 template <class T> struct GraphTraits;
271
272 template <>
273 struct GraphTraits<MachineDomTreeNode *>
274     : public MachineDomTreeGraphTraitsBase<MachineDomTreeNode,
275                                            MachineDomTreeNode::iterator> {};
276
277 template <>
278 struct GraphTraits<const MachineDomTreeNode *>
279     : public MachineDomTreeGraphTraitsBase<const MachineDomTreeNode,
280                                            MachineDomTreeNode::const_iterator> {
281 };
282
283 template <> struct GraphTraits<MachineDominatorTree*>
284   : public GraphTraits<MachineDomTreeNode *> {
285   static NodeRef getEntryNode(MachineDominatorTree *DT) {
286     return DT->getRootNode();
287   }
288 };
289
290 }
291
292 #endif