LLVM  4.0.0
MachineDominators.h
Go to the documentation of this file.
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"
24 
25 namespace llvm {
26 
27 template<>
29  this->Roots.push_back(MBB);
30 }
31 
32 extern template class DomTreeNodeBase<MachineBasicBlock>;
33 extern template class DominatorTreeBase<MachineBasicBlock>;
34 
36 
37 //===-------------------------------------
38 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
39 /// compute a normal dominator tree.
40 ///
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
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 
76 
77  ~MachineDominatorTree() override;
78 
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.
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 
141  const MachineDomTreeNode* B) const {
142  applySplitCriticalEdges();
143  return DT->properlyDominates(A, B);
144  }
145 
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.
155  MachineBasicBlock *B) {
156  applySplitCriticalEdges();
157  return DT->findNearestCommonDominator(A, B);
158  }
159 
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  ///
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.
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  ///
186  MachineBasicBlock* NewIDom) {
187  applySplitCriticalEdges();
188  DT->changeImmediateDominator(N, NewIDom);
189  }
190 
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.
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.
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>
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 <>
290 
291 template <>
295 };
296 
297 template <> struct GraphTraits<MachineDominatorTree*>
300  return DT->getRootNode();
301  }
302 };
303 
304 }
305 
306 #endif
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
void push_back(const T &Elt)
Definition: SmallVector.h:211
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
bool properlyDominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
MachineDomTreeNode * getRootNode() const
MachineBasicBlock * getRoot() const
DominatorTreeBase< MachineBasicBlock > & getBase()
bool dominates(const MachineInstr *A, const MachineInstr *B) const
bool compare(const MachineDominatorTree &Other) const
Returns false if the other dominator tree matches this dominator tree.
void eraseNode(MachineBasicBlock *BB)
eraseNode - Removes a node from the dominator tree.
void splitBlock(MachineBasicBlock *NewBB)
splitBlock - BB is split and now it has one successor.
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
void addRoot(NodeT *BB)
static NodeRef getEntryNode(NodeRef N)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
MachineDomTreeNode * getNode(MachineBasicBlock *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
#define F(x, y, z)
Definition: MD5.cpp:51
NodeT * getRoot() const
static ChildIteratorType child_end(NodeRef N)
MachineBasicBlock * MBB
Base class for the actual dominator tree node.
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
MachineBasicBlock * findNearestCommonDominator(MachineBasicBlock *A, MachineBasicBlock *B)
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B...
NodeT * getBlock() const
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:131
Core dominator tree base class.
Definition: LoopInfo.h:59
void recordSplitCriticalEdge(MachineBasicBlock *FromBB, MachineBasicBlock *ToBB, MachineBasicBlock *NewBB)
Record that the critical edge (FromBB, ToBB) has been split with NewBB.
MachineDomTreeNode * operator[](MachineBasicBlock *BB) const
bool isReachableFromEntry(const NodeT *A) const
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
const std::vector< NodeT * > & getRoots() const
getRoots - Return the root blocks of the current CFG.
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:36
Represent the analysis usage information of a pass.
NodeT * findNearestCommonDominator(NodeT *A, NodeT *B)
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B...
bool properlyDominates(const MachineBasicBlock *A, const MachineBasicBlock *B) const
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:80
void changeImmediateDominator(MachineDomTreeNode *N, MachineDomTreeNode *NewIDom)
Generic dominator tree construction - This file provides routines to construct immediate dominator in...
static NodeRef getEntryNode(MachineDominatorTree *DT)
static ChildIteratorType child_begin(NodeRef N)
const std::vector< MachineBasicBlock * > & getRoots() const
getRoots - Return the root blocks of the current CFG.
std::vector< DomTreeNodeBase< NodeT > * >::const_iterator const_iterator
bool dominates(const MachineBasicBlock *A, const MachineBasicBlock *B) const
Representation of each machine instruction.
Definition: MachineInstr.h:52
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
void changeImmediateDominator(MachineBasicBlock *N, MachineBasicBlock *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
bool isReachableFromEntry(const MachineBasicBlock *A)
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::vector< DomTreeNodeBase< NodeT > * >::iterator iterator
aarch64 promote const
bool compare(const DominatorTreeBase &Other) const
compare - Return false if the other dominator tree base matches this dominator tree base...
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
This file defines a set of templates that efficiently compute a dominator tree over a generic graph...
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
MachineDomTreeNode * addNewBlock(MachineBasicBlock *BB, MachineBasicBlock *DomBB)
addNewBlock - Add a new node to the dominator tree information.
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
void splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.