LLVM 23.0.0git
VPlanCFG.h
Go to the documentation of this file.
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"
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>
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(getBlockWithOutgoingEdges(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) {}
116 : Block(Other.Block), EdgeIdx(Other.EdgeIdx) {}
117
120 Block = R.Block;
121 EdgeIdx = R.EdgeIdx;
122 return *this;
123 }
124
125 static VPHierarchicalChildrenIterator end(BlockPtrTy Block) {
126 if (auto *R = dyn_cast<VPRegionBlock>(Block)) {
127 // Traverse through the region's entry/exiting (based on Forward) node.
128 return {R, 1};
129 }
130 BlockPtrTy ParentWithOutgoingEdges = getBlockWithOutgoingEdges(Block);
131 unsigned NumOutgoingEdges =
132 ParentWithOutgoingEdges ? getNumOutgoingEdges(ParentWithOutgoingEdges)
133 : 0;
134 return {Block, NumOutgoingEdges};
135 }
136
138 return Block == R.Block && EdgeIdx == R.EdgeIdx;
139 }
140
141 const VPBlockBase *operator*() const { return deref(Block, EdgeIdx); }
142
143 BlockPtrTy operator*() { return deref(Block, EdgeIdx); }
144
146 EdgeIdx++;
147 return *this;
148 }
149
151 EdgeIdx--;
152 return *this;
153 }
154
157 EdgeIdx++;
158 return Orig;
159 }
160};
161
162/// Helper for GraphTraits specialization that traverses through VPRegionBlocks.
163template <typename BlockTy> class VPBlockDeepTraversalWrapper {
164 BlockTy Entry;
165
166public:
167 VPBlockDeepTraversalWrapper(BlockTy Entry) : Entry(Entry) {}
168 BlockTy getEntry() { return Entry; }
169};
170
171/// GraphTraits specialization to recursively traverse VPBlockBase nodes,
172/// including traversing through VPRegionBlocks. Exit blocks of a region
173/// implicitly have their parent region's successors. This ensures all blocks in
174/// a region are visited before any blocks in a successor region when doing a
175/// reverse post-order traversal of the graph.
192
193template <>
211
212/// Helper for GraphTraits specialization that does not traverses through
213/// VPRegionBlocks.
214template <typename BlockTy> class VPBlockShallowTraversalWrapper {
215 BlockTy Entry;
216
217public:
219 BlockTy getEntry() { return Entry; }
220};
221
225
229
231 return N->getSuccessors().begin();
232 }
233
235 return N->getSuccessors().end();
236 }
237};
238
239template <>
241 using NodeRef = const VPBlockBase *;
243
244 static NodeRef
248
250 return N->getSuccessors().begin();
251 }
252
254 return N->getSuccessors().end();
255 }
256};
257
258/// Returns an iterator range to traverse the graph starting at \p G in
259/// depth-first order. The iterator won't traverse through region blocks.
260inline iterator_range<
261 df_iterator<VPBlockShallowTraversalWrapper<VPBlockBase *>>>
265inline iterator_range<
266 df_iterator<VPBlockShallowTraversalWrapper<const VPBlockBase *>>>
270
271/// Returns an iterator range to traverse the graph starting at \p G in
272/// post order. The iterator won't traverse through region blocks.
273inline iterator_range<
274 po_iterator<VPBlockShallowTraversalWrapper<VPBlockBase *>>>
278
279/// Returns an iterator range to traverse the graph starting at \p G in
280/// post order while traversing through region blocks.
285
286/// Returns an iterator range to traverse the graph starting at \p G in
287/// depth-first order while traversing through region blocks.
292inline iterator_range<
293 df_iterator<VPBlockDeepTraversalWrapper<const VPBlockBase *>>>
297
298// The following set of template specializations implement GraphTraits to treat
299// any VPBlockBase as a node in a graph of VPBlockBases. It's important to note
300// that VPBlockBase traits don't recurse into VPRegioBlocks, i.e., if the
301// VPBlockBase is a VPRegionBlock, this specialization provides access to its
302// successors/predecessors but not to the blocks inside the region.
303
304template <> struct GraphTraits<VPBlockBase *> {
307
308 static NodeRef getEntryNode(NodeRef N) { return N; }
309
311 return ChildIteratorType(N);
312 }
313
316 }
317};
318
319template <> struct GraphTraits<const VPBlockBase *> {
320 using NodeRef = const VPBlockBase *;
322
323 static NodeRef getEntryNode(NodeRef N) { return N; }
324
326 return ChildIteratorType(N);
327 }
328
331 }
332};
333
334template <> struct GraphTraits<Inverse<VPBlockBase *>> {
337 VPHierarchicalChildrenIterator<VPBlockBase *, /*Forward=*/false>;
338
339 static NodeRef getEntryNode(Inverse<NodeRef> B) { return B.Graph; }
340
342 return ChildIteratorType(N);
343 }
344
347 }
348};
349
350template <> struct GraphTraits<VPlan *> {
351 using GraphRef = VPlan *;
354
355 static NodeRef getEntryNode(GraphRef N) { return N->getEntry(); }
356
358 return nodes_iterator::begin(N->getEntry());
359 }
360
362 // df_iterator::end() returns an empty iterator so the node used doesn't
363 // matter.
364 return nodes_iterator::end(N->getEntry());
365 }
366};
367
368} // namespace llvm
369
370#endif // LLVM_TRANSFORMS_VECTORIZE_VPLANCFG_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
This file defines the little GraphTraits<X> template class that should be specialized by classes that...
std::pair< BasicBlock *, unsigned > BlockTy
A pair of (basic block, score).
#define G(x, y, z)
Definition MD5.cpp:55
#define T1
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file defines the SmallVector class.
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This file contains the declarations of the Vectorization Plan base classes:
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
typename SuperClass::const_iterator const_iterator
typename SuperClass::iterator iterator
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:81
Helper for GraphTraits specialization that traverses through VPRegionBlocks.
Definition VPlanCFG.h:163
VPBlockDeepTraversalWrapper(BlockTy Entry)
Definition VPlanCFG.h:167
Helper for GraphTraits specialization that does not traverses through VPRegionBlocks.
Definition VPlanCFG.h:214
VPBlockShallowTraversalWrapper(BlockTy Entry)
Definition VPlanCFG.h:218
Iterator to traverse all successors/predecessors of a VPBlockBase node, including its hierarchical su...
Definition VPlanCFG.h:66
VPHierarchicalChildrenIterator & operator=(const VPHierarchicalChildrenIterator &R)
Definition VPlanCFG.h:119
const VPBlockBase * operator*() const
Definition VPlanCFG.h:141
VPHierarchicalChildrenIterator & operator++()
Definition VPlanCFG.h:145
VPHierarchicalChildrenIterator(BlockPtrTy Block, size_t Idx=0)
Definition VPlanCFG.h:113
BlockPtrTy reference
Used by iterator_facade_base with bidirectional_iterator_tag.
Definition VPlanCFG.h:111
static VPHierarchicalChildrenIterator end(BlockPtrTy Block)
Definition VPlanCFG.h:125
VPHierarchicalChildrenIterator operator++(int X)
Definition VPlanCFG.h:155
VPHierarchicalChildrenIterator(const VPHierarchicalChildrenIterator &Other)
Definition VPlanCFG.h:115
bool operator==(const VPHierarchicalChildrenIterator &R) const
Definition VPlanCFG.h:137
VPHierarchicalChildrenIterator & operator--()
Definition VPlanCFG.h:150
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4500
static df_iterator begin(const NodeRef &G)
static df_iterator end(const NodeRef &G)
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterator.h:80
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< po_iterator< T > > post_order(const T &G)
iterator_range< df_iterator< VPBlockShallowTraversalWrapper< VPBlockBase * > > > vp_depth_first_shallow(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in depth-first order.
Definition VPlanCFG.h:262
iterator_range< df_iterator< VPBlockDeepTraversalWrapper< VPBlockBase * > > > vp_depth_first_deep(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in depth-first order while traversing t...
Definition VPlanCFG.h:289
iterator_range< po_iterator< VPBlockDeepTraversalWrapper< VPBlockBase * > > > vp_post_order_deep(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in post order while traversing through ...
Definition VPlanCFG.h:282
iterator_range< po_iterator< VPBlockShallowTraversalWrapper< VPBlockBase * > > > vp_post_order_shallow(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in post order.
Definition VPlanCFG.h:275
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
@ Other
Any other memory.
Definition ModRef.h:68
iterator_range< df_iterator< T > > depth_first(const T &G)
#define N
VPHierarchicalChildrenIterator< VPBlockBase *, false > ChildIteratorType
Definition VPlanCFG.h:336
static NodeRef getEntryNode(Inverse< NodeRef > B)
Definition VPlanCFG.h:339
static ChildIteratorType child_end(NodeRef N)
Definition VPlanCFG.h:345
static ChildIteratorType child_begin(NodeRef N)
Definition VPlanCFG.h:341
VPHierarchicalChildrenIterator< VPBlockBase * > ChildIteratorType
Definition VPlanCFG.h:306
static NodeRef getEntryNode(NodeRef N)
Definition VPlanCFG.h:308
static ChildIteratorType child_begin(NodeRef N)
Definition VPlanCFG.h:310
static ChildIteratorType child_end(NodeRef N)
Definition VPlanCFG.h:314
static NodeRef getEntryNode(VPBlockDeepTraversalWrapper< VPBlockBase * > N)
Definition VPlanCFG.h:180
VPHierarchicalChildrenIterator< VPBlockBase * > ChildIteratorType
Definition VPlanCFG.h:178
VPHierarchicalChildrenIterator< const VPBlockBase * > ChildIteratorType
Definition VPlanCFG.h:196
static NodeRef getEntryNode(VPBlockDeepTraversalWrapper< const VPBlockBase * > N)
Definition VPlanCFG.h:199
static NodeRef getEntryNode(VPBlockShallowTraversalWrapper< VPBlockBase * > N)
Definition VPlanCFG.h:226
SmallVectorImpl< VPBlockBase * >::iterator ChildIteratorType
Definition VPlanCFG.h:224
SmallVectorImpl< VPBlockBase * >::const_iterator ChildIteratorType
Definition VPlanCFG.h:242
static NodeRef getEntryNode(VPBlockShallowTraversalWrapper< const VPBlockBase * > N)
Definition VPlanCFG.h:245
static nodes_iterator nodes_end(GraphRef N)
Definition VPlanCFG.h:361
static NodeRef getEntryNode(GraphRef N)
Definition VPlanCFG.h:355
df_iterator< NodeRef > nodes_iterator
Definition VPlanCFG.h:353
static nodes_iterator nodes_begin(GraphRef N)
Definition VPlanCFG.h:357
VPHierarchicalChildrenIterator< const VPBlockBase * > ChildIteratorType
Definition VPlanCFG.h:321
static ChildIteratorType child_end(NodeRef N)
Definition VPlanCFG.h:329
static ChildIteratorType child_begin(NodeRef N)
Definition VPlanCFG.h:325
static NodeRef getEntryNode(NodeRef N)
Definition VPlanCFG.h:323