llvm.org GIT mirror llvm / 8be61a8
Modify df_iterator to support post-order actions Summary: This makes a change to the state used to maintain visited information for depth first iterator. We know assume a method "completed(...)" which is called after all children of a node have been visited. In all existing cases, this method does nothing so this patch has no functional changes. It will however allow a client to distinguish back from cross edges in a DFS tree. Reviewers: nadav, mehdi_amini, dberlin Subscribers: MatzeB, mzolotukhin, twoh, freik, llvm-commits Differential Revision: https://reviews.llvm.org/D25191 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283391 91177308-0d34-0410-b5e6-96231b3b80d8 David Callahan 3 years ago
14 changed file(s) with 48 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
5757 SetType &Visited;
5858 };
5959
60 // The visited stated for the iteration is a simple set augmented with
61 // one more method, completed, which is invoked when all children of a
62 // node have been processed. It is intended to distinguish of back and
63 // cross edges in the spanning tree but is not used in the common case.
64 template
65 struct df_iterator_default_set : public llvm::SmallPtrSet {
66 typedef llvm::SmallPtrSet BaseSet;
67 typedef typename BaseSet::iterator iterator;
68 std::pair insert(NodeRef N) { return BaseSet::insert(N) ; }
69 template
70 void insert(IterT Begin, IterT End) { BaseSet::insert(Begin,End); }
71
72 void completed(NodeRef) { }
73 };
74
6075 // Generic Depth First Iterator
6176 template
6277 class SetType =
63 llvm::SmallPtrSet::NodeRef, 8>,
78 df_iterator_default_set::NodeRef>,
6479 bool ExtStorage = false, class GT = GraphTraits>
6580 class df_iterator
6681 : public std::iterator,
88103 }
89104 inline df_iterator(NodeRef Node, SetType &S)
90105 : df_iterator_storage(S) {
91 if (!S.count(Node)) {
106 if (this->Visited.insert(Node).second)
92107 VisitStack.push_back(StackElement(Node, None));
93 this->Visited.insert(Node);
94 }
95108 }
96109 inline df_iterator(SetType &S)
97110 : df_iterator_storage(S) {
118131 return;
119132 }
120133 }
121
134 this->Visited.completed(Node);
135
122136 // Oops, ran out of successors... go up a level on the stack.
123137 VisitStack.pop_back();
124138 } while (!VisitStack.empty());
234248
235249 // Provide global definitions of inverse depth first iterators...
236250 template
237 class SetTy = llvm::SmallPtrSet::NodeRef, 8>,
251 class SetTy =
252 df_iterator_default_set::NodeRef>,
238253 bool External = false>
239254 struct idf_iterator : public df_iterator, SetTy, External> {
240255 idf_iterator(const df_iterator, SetTy, External> &V)
227227 // Setup for using a depth-first iterator to visit every block in the loop.
228228 SmallVector ExitBBs;
229229 getExitBlocks(ExitBBs);
230 llvm::SmallPtrSet> VisitSet;
230 df_iterator_default_set> VisitSet;
231231 VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
232 df_ext_iteratorllvm::SmallPtrSet >
232 df_ext_iteratordf_iterator_default_set>
233233 BI = df_ext_begin(getHeader(), VisitSet),
234234 BE = df_ext_end(getHeader(), VisitSet);
235235
625625 /// are direct children of this Region. It does not iterate over any
626626 /// RegionNodes that are also element of a subregion of this Region.
627627 //@{
628 typedef df_iterator, false,
629 GraphTraits> element_iterator;
630
631 typedef df_iterator,
632 false,
633 GraphTraits> const_element_iterator;
628 typedef df_iterator,
629 false, GraphTraits>
630 element_iterator;
631
632 typedef df_iterator
633 df_iterator_default_set, false,
634 GraphTraits>
635 const_element_iterator;
634636
635637 element_iterator element_begin();
636638 element_iterator element_end();
293293 template <> \
294294 struct GraphTraits> \
295295 : public GraphTraits> { \
296 typedef df_iteratorSmallPtrSet, false, \
296 typedef df_iteratordf_iterator_default_set, false, \
297297 GraphTraits>> \
298298 nodes_iterator; \
299299 static NodeRef getEntryNode(RegionT *R) { \
315315
316316 template <> struct GraphTraits
317317 : public GraphTraits > {
318 typedef df_iteratorSmallPtrSet>, false,
318 typedef df_iteratordf_iterator_default_set>, false,
319319 GraphTraits>>
320320 nodes_iterator;
321321
332332
333333 template <> struct GraphTraits
334334 : public GraphTraits {
335 typedef df_iteratorSmallPtrSet>, false,
335 typedef df_iteratordf_iterator_default_set>, false,
336336 GraphTraits>>
337337 nodes_iterator;
338338
141141
142142 template <> struct GraphTraits
143143 : public GraphTraits > {
144 typedef df_iteratorSmallPtrSet>, false,
144 typedef df_iteratordf_iterator_default_set>, false,
145145 GraphTraits>>
146146 nodes_iterator;
147147
158158
159159 template <> struct GraphTraits
160160 : public GraphTraits {
161 typedef df_iteratorSmallPtrSet>, false,
161 typedef df_iteratordf_iterator_default_set>, false,
162162 GraphTraits>>
163163 nodes_iterator;
164164
156156 template struct DomTreeGraphTraitsBase {
157157 typedef Node *NodeRef;
158158 typedef ChildIterator ChildIteratorType;
159 typedef df_iteratorSmallPtrSet>> nodes_iterator;
159 typedef df_iteratordf_iterator_default_set>> nodes_iterator;
160160
161161 static NodeRef getEntryNode(NodeRef N) { return N; }
162162 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
598598 // Find all blocks that are reachable from KillMBB without leaving VNI's live
599599 // range. It is possible that KillMBB itself is reachable, so start a DFS
600600 // from each successor.
601 typedef SmallPtrSet9> VisitedTy;
601 typedef df_iterator_default_set9> VisitedTy;
602602 VisitedTy Visited;
603603 for (MachineBasicBlock::succ_iterator
604604 SuccI = KillMBB->succ_begin(), SuccE = KillMBB->succ_end();
642642 // register before its uses due to dominance properties of SSA (except for PHI
643643 // nodes, which are treated as a special case).
644644 MachineBasicBlock *Entry = &MF->front();
645 SmallPtrSet Visited;
645 df_iterator_default_set Visited;
646646
647647 for (MachineBasicBlock *MBB : depth_first_ext(Entry, Visited)) {
648648 runOnBlock(MBB, NumRegs);
20132013
20142014 SmallVector SPState;
20152015 SPState.resize(MF->getNumBlockIDs());
2016 SmallPtrSet> Reachable;
2016 df_iterator_default_set> Reachable;
20172017
20182018 // Visit the MBBs in DFS order.
20192019 for (df_ext_iterator
2020 SmallPtrSet> >
2020 df_iterator_default_set> >
20212021 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
20222022 DFI != DFE; ++DFI) {
20232023 const MachineBasicBlock *MBB = *DFI;
10071007 // Store SPAdj at exit of a basic block.
10081008 SmallVector SPState;
10091009 SPState.resize(Fn.getNumBlockIDs());
1010 SmallPtrSet> Reachable;
1010 df_iterator_default_set> Reachable;
10111011
10121012 // Iterate over the reachable blocks in DFS order.
10131013 for (auto DFI = df_ext_begin(&Fn, Reachable), DFE = df_ext_end(&Fn, Reachable);
3939 using namespace llvm;
4040
4141 static bool eliminateUnreachableBlock(Function &F) {
42 SmallPtrSet> Reachable;
42 df_iterator_default_set> Reachable;
4343
4444 // Mark all reachable blocks.
4545 for (BasicBlock *BB : depth_first_ext(&F, Reachable))
129129 }
130130
131131 bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
132 SmallPtrSet> Reachable;
132 df_iterator_default_set> Reachable;
133133 bool ModifiedPHI = false;
134134
135135 MMI = getAnalysisIfAvailable();
325325
326326 // Process the function in depth first order so that we process at least one
327327 // of the predecessors for every reachable block in the function.
328 SmallPtrSet> Processed;
328 df_iterator_default_set> Processed;
329329 MachineBasicBlock *Entry = &MF.front();
330330
331331 bool Changed = false;
599599
600600 // Because there could be several/many load instructions, remember which
601601 // blocks we know to be transparent to the load.
602 SmallPtrSet TranspBlocks;
602 df_iterator_default_set TranspBlocks;
603603
604604 for (LoadInst *Load : Loads) {
605605 // Check to see if the load is invalidated from the start of the block to
2626 }
2727
2828 size_t count(const T &Item) const { return S.count(Item); }
29
30 void completed(T) { }
2931 };
3032
3133 template class df_iterator_storage, true> {