]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/CodeGen/MachineDominators.h
Merge OpenSSL 1.0.2l.
[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
25 namespace llvm {
26
27 template<>
28 inline void DominatorTreeBase<MachineBasicBlock>::addRoot(MachineBasicBlock* MBB) {
29   this->Roots.push_back(MBB);
30 }
31
32 extern template class DomTreeNodeBase<MachineBasicBlock>;
33 extern template class DominatorTreeBase<MachineBasicBlock>;
34
35 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
36
37 //===-------------------------------------
38 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
39 /// compute a normal dominator tree.
40 ///
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.
44   struct CriticalEdge {
45     MachineBasicBlock *FromBB;
46     MachineBasicBlock *ToBB;
47     MachineBasicBlock *NewBB;
48   };
49
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
55   /// edge splitting.
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;
61
62   /// The DominatorTreeBase that is used to compute a normal dominator tree
63   DominatorTreeBase<MachineBasicBlock>* DT;
64
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.
68   ///
69   /// \post CriticalEdgesToSplit.empty().
70   void applySplitCriticalEdges() const;
71
72 public:
73   static char ID; // Pass ID, replacement for typeid
74
75   MachineDominatorTree();
76
77   ~MachineDominatorTree() override;
78
79   DominatorTreeBase<MachineBasicBlock> &getBase() {
80     applySplitCriticalEdges();
81     return *DT;
82   }
83
84   void getAnalysisUsage(AnalysisUsage &AU) const override;
85
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).
89   ///
90   inline const std::vector<MachineBasicBlock*> &getRoots() const {
91     applySplitCriticalEdges();
92     return DT->getRoots();
93   }
94
95   inline MachineBasicBlock *getRoot() const {
96     applySplitCriticalEdges();
97     return DT->getRoot();
98   }
99
100   inline MachineDomTreeNode *getRootNode() const {
101     applySplitCriticalEdges();
102     return DT->getRootNode();
103   }
104
105   bool runOnMachineFunction(MachineFunction &F) override;
106
107   inline bool dominates(const MachineDomTreeNode* A,
108                         const MachineDomTreeNode* B) const {
109     applySplitCriticalEdges();
110     return DT->dominates(A, B);
111   }
112
113   inline bool dominates(const MachineBasicBlock* A,
114                         const MachineBasicBlock* B) const {
115     applySplitCriticalEdges();
116     return DT->dominates(A, B);
117   }
118
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);
125
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)
129       /*empty*/ ;
130
131     //if(!DT.IsPostDominators) {
132       // A dominates B if it is found first in the basic block.
133       return &*I == A;
134     //} else {
135     //  // A post-dominates B if B is found first in the basic block.
136     //  return &*I == B;
137     //}
138   }
139
140   inline bool properlyDominates(const MachineDomTreeNode* A,
141                                 const MachineDomTreeNode* B) const {
142     applySplitCriticalEdges();
143     return DT->properlyDominates(A, B);
144   }
145
146   inline bool properlyDominates(const MachineBasicBlock* A,
147                                 const MachineBasicBlock* B) const {
148     applySplitCriticalEdges();
149     return DT->properlyDominates(A, B);
150   }
151
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);
158   }
159
160   inline MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
161     applySplitCriticalEdges();
162     return DT->getNode(BB);
163   }
164
165   /// getNode - return the (Post)DominatorTree node for the specified basic
166   /// block.  This is the same as using operator[] on this class.
167   ///
168   inline MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
169     applySplitCriticalEdges();
170     return DT->getNode(BB);
171   }
172
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);
180   }
181
182   /// changeImmediateDominator - This method is used to update the dominator
183   /// tree information when a node's immediate dominator changes.
184   ///
185   inline void changeImmediateDominator(MachineBasicBlock *N,
186                                        MachineBasicBlock* NewIDom) {
187     applySplitCriticalEdges();
188     DT->changeImmediateDominator(N, NewIDom);
189   }
190
191   inline void changeImmediateDominator(MachineDomTreeNode *N,
192                                        MachineDomTreeNode* NewIDom) {
193     applySplitCriticalEdges();
194     DT->changeImmediateDominator(N, NewIDom);
195   }
196
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();
202     DT->eraseNode(BB);
203   }
204
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);
210   }
211
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);
217   }
218
219   void releaseMemory() override;
220
221   void verifyAnalysis() const override;
222
223   void print(raw_ostream &OS, const Module*) const override;
224
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.
230   ///
231   /// \note Do not use this method with regular edges.
232   ///
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;
241     (void)Inserted;
242     assert(Inserted &&
243            "A basic block inserted via edge splitting cannot appear twice");
244     CriticalEdgesToSplit.push_back({FromBB, ToBB, NewBB});
245   }
246
247   /// \brief Returns *false* if the other dominator tree matches this dominator
248   /// tree.
249   inline bool compare(const MachineDominatorTree &Other) const {
250     const MachineDomTreeNode *R = getRootNode();
251     const MachineDomTreeNode *OtherR = Other.getRootNode();
252
253     if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
254       return true;
255
256     if (DT->compare(*Other.DT))
257       return true;
258
259     return false;
260   }
261
262   /// \brief Verify the correctness of the domtree by re-computing it.
263   ///
264   /// This should only be used for debugging as it aborts the program if the
265   /// verification fails.
266   void verifyDomTree() const;
267 };
268
269 //===-------------------------------------
270 /// DominatorTree GraphTraits specialization so the DominatorTree can be
271 /// iterable by generic graph iterators.
272 ///
273
274 template <class Node, class ChildIterator>
275 struct MachineDomTreeGraphTraitsBase {
276   typedef Node *NodeRef;
277   typedef ChildIterator ChildIteratorType;
278
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(); }
282 };
283
284 template <class T> struct GraphTraits;
285
286 template <>
287 struct GraphTraits<MachineDomTreeNode *>
288     : public MachineDomTreeGraphTraitsBase<MachineDomTreeNode,
289                                            MachineDomTreeNode::iterator> {};
290
291 template <>
292 struct GraphTraits<const MachineDomTreeNode *>
293     : public MachineDomTreeGraphTraitsBase<const MachineDomTreeNode,
294                                            MachineDomTreeNode::const_iterator> {
295 };
296
297 template <> struct GraphTraits<MachineDominatorTree*>
298   : public GraphTraits<MachineDomTreeNode *> {
299   static NodeRef getEntryNode(MachineDominatorTree *DT) {
300     return DT->getRootNode();
301   }
302 };
303
304 }
305
306 #endif