1//===- VPlanCFG.h - GraphTraits for VP blocks -------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// Specializations of GraphTraits that allow VPBlockBase graphs to be
9/// treated as proper graphs for generic algorithms;
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLANCFG_H
13#define LLVM_TRANSFORMS_VECTORIZE_VPLANCFG_H
14
15#include "VPlan.h"
16#include "VPlanUtils.h"
17#include "llvm/ADT/DepthFirstIterator.h"
18#include "llvm/ADT/GraphTraits.h"
19#include "llvm/ADT/PostOrderIterator.h"
20#include "llvm/ADT/SmallVector.h"
21
22namespace llvm {
23
24//===----------------------------------------------------------------------===//
25// GraphTraits specializations for VPlan Hierarchical Control-Flow Graphs //
26//===----------------------------------------------------------------------===//
27
28/// Iterator to traverse all successors/predecessors of a VPBlockBase node,
29/// including its hierarchical successors/predecessors:
30///
31/// A
32/// |
33/// +-----+ <- Region R
34/// | b |
35/// | |
36/// | ... |
37/// | |
38/// | e |
39/// +-----+
40/// |
41/// B
42///
43/// Forward == true:
44/// Region blocks themselves traverse only their entries directly.
45/// Region's successor is implictly traversed when processing its exiting
46/// block.
47/// children(A) == {R}
48/// children(R) == {b}
49/// children(e) == {B}
50///
51/// Forward == false:
52/// Region blocks themselves traverse only their exiting blocks directly.
53/// Region's predecessor is implicitly traversed when processing its entry
54/// block.
55/// children(B) == {R}
56/// children(R) == {e}
57/// children(b) == {A}
58///
59/// The scheme described above ensures that all blocks of the region are visited
60/// before continuing traversal outside the region when doing a reverse
61/// post-order traversal of the VPlan.
62template <typename BlockPtrTy, bool Forward = true>
63class VPHierarchicalChildrenIterator
64 : public iterator_facade_base<
65 VPHierarchicalChildrenIterator<BlockPtrTy, Forward>,
66 std::bidirectional_iterator_tag, VPBlockBase> {
67 BlockPtrTy Block;
68 /// Index of the current successor/predecessor. For VPBasicBlock nodes, this
69 /// simply is the index for the successors/predecessors array. For
70 /// VPRegionBlock, EdgeIdx == 0 is used for the region's entry/exiting block,
71 /// and EdgeIdx - 1 are the indices for the successors/predecessors array.
72 size_t EdgeIdx;
73
74 static size_t getNumOutgoingEdges(BlockPtrTy Current) {
75 if constexpr (Forward)
76 return Current->getNumSuccessors();
77 else
78 return Current->getNumPredecessors();
79 }
80
81 static ArrayRef<BlockPtrTy> getOutgoingEdges(BlockPtrTy Current) {
82 if constexpr (Forward)
83 return Current->getSuccessors();
84 else
85 return Current->getPredecessors();
86 }
87
88 static BlockPtrTy getBlockWithOutgoingEdges(BlockPtrTy Current) {
89 while (Current && getNumOutgoingEdges(Current) == 0)
90 Current = Current->getParent();
91 return Current;
92 }
93
94 /// Templated helper to dereference successor/predecessor \p EdgeIdx of \p
95 /// Block. Used by both the const and non-const operator* implementations.
96 template <typename T1> static T1 deref(T1 Block, unsigned EdgeIdx) {
97 if (auto *R = dyn_cast<VPRegionBlock>(Block)) {
98 assert(EdgeIdx == 0);
99 if constexpr (Forward)
100 return R->getEntry();
101 else
102 return R->getExiting();
103 }
104
105 // For exit blocks, use the next parent region with successors.
106 return getOutgoingEdges(Current: getBlockWithOutgoingEdges(Current: Block))[EdgeIdx];
107 }
108
109public:
110 /// Used by iterator_facade_base with bidirectional_iterator_tag.
111 using reference = BlockPtrTy;
112
113 VPHierarchicalChildrenIterator(BlockPtrTy Block, size_t Idx = 0)
114 : Block(Block), EdgeIdx(Idx) {}
115
116 static VPHierarchicalChildrenIterator end(BlockPtrTy Block) {
117 if (auto *R = dyn_cast<VPRegionBlock>(Block)) {
118 // Traverse through the region's entry/exiting (based on Forward) node.
119 return {R, 1};
120 }
121 BlockPtrTy ParentWithOutgoingEdges = getBlockWithOutgoingEdges(Current: Block);
122 unsigned NumOutgoingEdges =
123 ParentWithOutgoingEdges ? getNumOutgoingEdges(Current: ParentWithOutgoingEdges)
124 : 0;
125 return {Block, NumOutgoingEdges};
126 }
127
128 bool operator==(const VPHierarchicalChildrenIterator &R) const {
129 return Block == R.Block && EdgeIdx == R.EdgeIdx;
130 }
131
132 const VPBlockBase *operator*() const { return deref(Block, EdgeIdx); }
133
134 BlockPtrTy operator*() { return deref(Block, EdgeIdx); }
135
136 VPHierarchicalChildrenIterator &operator++() {
137 EdgeIdx++;
138 return *this;
139 }
140
141 VPHierarchicalChildrenIterator &operator--() {
142 EdgeIdx--;
143 return *this;
144 }
145
146 VPHierarchicalChildrenIterator operator++(int X) {
147 VPHierarchicalChildrenIterator Orig = *this;
148 EdgeIdx++;
149 return Orig;
150 }
151};
152
153/// Helper for GraphTraits specialization that traverses through VPRegionBlocks.
154template <typename BlockTy> class VPBlockDeepTraversalWrapper {
155 BlockTy Entry;
156
157public:
158 VPBlockDeepTraversalWrapper(BlockTy Entry) : Entry(Entry) {}
159 BlockTy getEntry() { return Entry; }
160};
161
162/// GraphTraits specialization to recursively traverse VPBlockBase nodes,
163/// including traversing through VPRegionBlocks. Exit blocks of a region
164/// implicitly have their parent region's successors. This ensures all blocks in
165/// a region are visited before any blocks in a successor region when doing a
166/// reverse post-order traversal of the graph.
167template <> struct GraphTraits<VPBlockDeepTraversalWrapper<VPBlockBase *>> {
168 using NodeRef = VPBlockBase *;
169 using ChildIteratorType = VPHierarchicalChildrenIterator<VPBlockBase *>;
170
171 static NodeRef getEntryNode(VPBlockDeepTraversalWrapper<VPBlockBase *> N) {
172 return N.getEntry();
173 }
174
175 static inline ChildIteratorType child_begin(NodeRef N) {
176 return ChildIteratorType(N);
177 }
178
179 static inline ChildIteratorType child_end(NodeRef N) {
180 return ChildIteratorType::end(Block: N);
181 }
182};
183
184template <>
185struct GraphTraits<VPBlockDeepTraversalWrapper<const VPBlockBase *>> {
186 using NodeRef = const VPBlockBase *;
187 using ChildIteratorType = VPHierarchicalChildrenIterator<const VPBlockBase *>;
188
189 static NodeRef
190 getEntryNode(VPBlockDeepTraversalWrapper<const VPBlockBase *> N) {
191 return N.getEntry();
192 }
193
194 static inline ChildIteratorType child_begin(NodeRef N) {
195 return ChildIteratorType(N);
196 }
197
198 static inline ChildIteratorType child_end(NodeRef N) {
199 return ChildIteratorType::end(Block: N);
200 }
201};
202
203/// Helper for GraphTraits specialization that does not traverses through
204/// VPRegionBlocks.
205template <typename BlockTy> class VPBlockShallowTraversalWrapper {
206 BlockTy Entry;
207
208public:
209 VPBlockShallowTraversalWrapper(BlockTy Entry) : Entry(Entry) {}
210 BlockTy getEntry() { return Entry; }
211};
212
213template <> struct GraphTraits<VPBlockShallowTraversalWrapper<VPBlockBase *>> {
214 using NodeRef = VPBlockBase *;
215 using ChildIteratorType = SmallVectorImpl<VPBlockBase *>::iterator;
216
217 static NodeRef getEntryNode(VPBlockShallowTraversalWrapper<VPBlockBase *> N) {
218 return N.getEntry();
219 }
220
221 static inline ChildIteratorType child_begin(NodeRef N) {
222 return N->getSuccessors().begin();
223 }
224
225 static inline ChildIteratorType child_end(NodeRef N) {
226 return N->getSuccessors().end();
227 }
228};
229
230template <>
231struct GraphTraits<VPBlockShallowTraversalWrapper<const VPBlockBase *>> {
232 using NodeRef = const VPBlockBase *;
233 using ChildIteratorType = SmallVectorImpl<VPBlockBase *>::const_iterator;
234
235 static NodeRef
236 getEntryNode(VPBlockShallowTraversalWrapper<const VPBlockBase *> N) {
237 return N.getEntry();
238 }
239
240 static inline ChildIteratorType child_begin(NodeRef N) {
241 return N->getSuccessors().begin();
242 }
243
244 static inline ChildIteratorType child_end(NodeRef N) {
245 return N->getSuccessors().end();
246 }
247};
248
249/// Returns an iterator range to traverse the graph starting at \p G in
250/// depth-first order. The iterator won't traverse through region blocks.
251inline iterator_range<
252 df_iterator<VPBlockShallowTraversalWrapper<VPBlockBase *>>>
253vp_depth_first_shallow(VPBlockBase *G) {
254 return depth_first(G: VPBlockShallowTraversalWrapper<VPBlockBase *>(G));
255}
256inline iterator_range<
257 df_iterator<VPBlockShallowTraversalWrapper<const VPBlockBase *>>>
258vp_depth_first_shallow(const VPBlockBase *G) {
259 return depth_first(G: VPBlockShallowTraversalWrapper<const VPBlockBase *>(G));
260}
261
262/// Returns an iterator range to traverse the graph starting at \p G in
263/// post order. The iterator won't traverse through region blocks.
264inline iterator_range<
265 po_iterator<VPBlockShallowTraversalWrapper<VPBlockBase *>>>
266vp_post_order_shallow(VPBlockBase *G) {
267 return post_order(G: VPBlockShallowTraversalWrapper<VPBlockBase *>(G));
268}
269
270/// Returns an iterator range to traverse the graph starting at \p G in
271/// post order while traversing through region blocks.
272inline iterator_range<po_iterator<VPBlockDeepTraversalWrapper<VPBlockBase *>>>
273vp_post_order_deep(VPBlockBase *G) {
274 return post_order(G: VPBlockDeepTraversalWrapper<VPBlockBase *>(G));
275}
276
277/// Returns an iterator range to traverse the graph starting at \p G in
278/// depth-first order while traversing through region blocks.
279inline iterator_range<df_iterator<VPBlockDeepTraversalWrapper<VPBlockBase *>>>
280vp_depth_first_deep(VPBlockBase *G) {
281 return depth_first(G: VPBlockDeepTraversalWrapper<VPBlockBase *>(G));
282}
283inline iterator_range<
284 df_iterator<VPBlockDeepTraversalWrapper<const VPBlockBase *>>>
285vp_depth_first_deep(const VPBlockBase *G) {
286 return depth_first(G: VPBlockDeepTraversalWrapper<const VPBlockBase *>(G));
287}
288
289// The following set of template specializations implement GraphTraits to treat
290// any VPBlockBase as a node in a graph of VPBlockBases. It's important to note
291// that VPBlockBase traits don't recurse into VPRegioBlocks, i.e., if the
292// VPBlockBase is a VPRegionBlock, this specialization provides access to its
293// successors/predecessors but not to the blocks inside the region.
294
295template <> struct GraphTraits<VPBlockBase *> {
296 using NodeRef = VPBlockBase *;
297 using ChildIteratorType = VPHierarchicalChildrenIterator<VPBlockBase *>;
298
299 static NodeRef getEntryNode(NodeRef N) { return N; }
300
301 static inline ChildIteratorType child_begin(NodeRef N) {
302 return ChildIteratorType(N);
303 }
304
305 static inline ChildIteratorType child_end(NodeRef N) {
306 return ChildIteratorType::end(Block: N);
307 }
308};
309
310template <> struct GraphTraits<const VPBlockBase *> {
311 using NodeRef = const VPBlockBase *;
312 using ChildIteratorType = VPHierarchicalChildrenIterator<const VPBlockBase *>;
313
314 static NodeRef getEntryNode(NodeRef N) { return N; }
315
316 static inline ChildIteratorType child_begin(NodeRef N) {
317 return ChildIteratorType(N);
318 }
319
320 static inline ChildIteratorType child_end(NodeRef N) {
321 return ChildIteratorType::end(Block: N);
322 }
323};
324
325template <> struct GraphTraits<Inverse<VPBlockBase *>> {
326 using NodeRef = VPBlockBase *;
327 using ChildIteratorType =
328 VPHierarchicalChildrenIterator<VPBlockBase *, /*Forward=*/false>;
329
330 static NodeRef getEntryNode(Inverse<NodeRef> B) { return B.Graph; }
331
332 static inline ChildIteratorType child_begin(NodeRef N) {
333 return ChildIteratorType(N);
334 }
335
336 static inline ChildIteratorType child_end(NodeRef N) {
337 return ChildIteratorType::end(Block: N);
338 }
339};
340
341template <> struct GraphTraits<VPlan *> {
342 using GraphRef = VPlan *;
343 using NodeRef = VPBlockBase *;
344 using nodes_iterator = df_iterator<NodeRef>;
345
346 static NodeRef getEntryNode(GraphRef N) { return N->getEntry(); }
347
348 static nodes_iterator nodes_begin(GraphRef N) {
349 return nodes_iterator::begin(G: N->getEntry());
350 }
351
352 static nodes_iterator nodes_end(GraphRef N) {
353 // df_iterator::end() returns an empty iterator so the node used doesn't
354 // matter.
355 return nodes_iterator::end(G: N->getEntry());
356 }
357};
358
359} // namespace llvm
360
361#endif // LLVM_TRANSFORMS_VECTORIZE_VPLANCFG_H
362