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 /// Helper to dereference successor/predecessor \p EdgeIdx of \p Block.
95 static BlockPtrTy deref(BlockPtrTy Block, unsigned EdgeIdx) {
96 if (auto *R = dyn_cast<VPRegionBlock>(Block)) {
97 assert(EdgeIdx == 0);
98 if constexpr (Forward)
99 return R->getEntry();
100 else
101 return R->getExiting();
102 }
103
104 // For exit blocks, use the next parent region with successors.
105 return getOutgoingEdges(getBlockWithOutgoingEdges(Block))[EdgeIdx];
106 }
107
108public:
109 /// Used by iterator_facade_base with bidirectional_iterator_tag.
110 using reference = BlockPtrTy;
111
112 VPHierarchicalChildrenIterator(BlockPtrTy Block, size_t Idx = 0)
113 : Block(Block), EdgeIdx(Idx) {}
114
115 static VPHierarchicalChildrenIterator end(BlockPtrTy Block) {
116 if (auto *R = dyn_cast<VPRegionBlock>(Block)) {
117 // Traverse through the region's entry/exiting (based on Forward) node.
118 return {R, 1};
119 }
120 BlockPtrTy ParentWithOutgoingEdges = getBlockWithOutgoingEdges(Block);
121 unsigned NumOutgoingEdges =
122 ParentWithOutgoingEdges ? getNumOutgoingEdges(ParentWithOutgoingEdges)
123 : 0;
124 return {Block, NumOutgoingEdges};
125 }
126
128 return Block == R.Block && EdgeIdx == R.EdgeIdx;
129 }
130
131 BlockPtrTy operator*() const { return deref(Block, EdgeIdx); }
132
134 EdgeIdx++;
135 return *this;
136 }
137
139 EdgeIdx--;
140 return *this;
141 }
142
145 EdgeIdx++;
146 return Orig;
147 }
148};
149
150/// Helper for GraphTraits specialization that traverses through VPRegionBlocks.
151template <typename BlockTy> class VPBlockDeepTraversalWrapper {
152 BlockTy Entry;
153
154public:
155 VPBlockDeepTraversalWrapper(BlockTy Entry) : Entry(Entry) {}
156 BlockTy getEntry() { return Entry; }
157};
158
159/// GraphTraits specialization to recursively traverse VPBlockBase nodes,
160/// including traversing through VPRegionBlocks. Exit blocks of a region
161/// implicitly have their parent region's successors. This ensures all blocks in
162/// a region are visited before any blocks in a successor region when doing a
163/// reverse post-order traversal of the graph.
180
181template <>
199
200/// Helper for GraphTraits specialization that does not traverses through
201/// VPRegionBlocks.
202template <typename BlockTy> class VPBlockShallowTraversalWrapper {
203 BlockTy Entry;
204
205public:
207 BlockTy getEntry() { return Entry; }
208};
209
213
217
219 return N->getSuccessors().begin();
220 }
221
223 return N->getSuccessors().end();
224 }
225};
226
227template <>
229 using NodeRef = const VPBlockBase *;
231
232 static NodeRef
236
238 return N->getSuccessors().begin();
239 }
240
242 return N->getSuccessors().end();
243 }
244};
245
246/// Returns an iterator range to traverse the graph starting at \p G in
247/// depth-first order. The iterator won't traverse through region blocks.
248inline iterator_range<
249 df_iterator<VPBlockShallowTraversalWrapper<VPBlockBase *>>>
253inline iterator_range<
254 df_iterator<VPBlockShallowTraversalWrapper<const VPBlockBase *>>>
258
259/// Returns the VPBasicBlocks forming the loop body of a plain (pre-region)
260/// VPlan in reverse post-order starting from \p Header.
263 assert(!Header->getParent() && "Header must not be inside a region");
264 VPBlockBase *Middle = Header->getPredecessors()[1]->getSuccessors()[0];
267 Header);
269 if (VPBB == Middle)
270 break;
271 // Skip exit blocks.
272 if (isa<VPIRBasicBlock>(VPBB)) {
273 assert(is_contained(Header->getPlan()->getExitBlocks(), VPBB) &&
274 "skipped VPIRBBs must be exit blocks");
275 continue;
276 }
277 Result.push_back(VPBB);
278 }
279 return Result;
280}
281
282/// Returns an iterator range to traverse the graph starting at \p G in
283/// depth-first order while traversing through region blocks.
288inline iterator_range<
289 df_iterator<VPBlockDeepTraversalWrapper<const VPBlockBase *>>>
293
294// The following set of template specializations implement GraphTraits to treat
295// any VPBlockBase as a node in a graph of VPBlockBases. It's important to note
296// that VPBlockBase traits don't recurse into VPRegioBlocks, i.e., if the
297// VPBlockBase is a VPRegionBlock, this specialization provides access to its
298// successors/predecessors but not to the blocks inside the region.
299
300template <> struct GraphTraits<VPBlockBase *> {
303
304 static NodeRef getEntryNode(NodeRef N) { return N; }
305
307 return ChildIteratorType(N);
308 }
309
312 }
313};
314
315template <> struct GraphTraits<const VPBlockBase *> {
316 using NodeRef = const VPBlockBase *;
318
319 static NodeRef getEntryNode(NodeRef N) { return N; }
320
322 return ChildIteratorType(N);
323 }
324
327 }
328};
329
330template <> struct GraphTraits<Inverse<VPBlockBase *>> {
333 VPHierarchicalChildrenIterator<VPBlockBase *, /*Forward=*/false>;
334
335 static NodeRef getEntryNode(Inverse<NodeRef> B) { return B.Graph; }
336
338 return ChildIteratorType(N);
339 }
340
343 }
344};
345
346template <> struct GraphTraits<VPlan *> {
347 using GraphRef = VPlan *;
350
351 static NodeRef getEntryNode(GraphRef N) { return N->getEntry(); }
352
354 return nodes_iterator::begin(N->getEntry());
355 }
356
358 // df_iterator::end() returns an empty iterator so the node used doesn't
359 // matter.
360 return nodes_iterator::end(N->getEntry());
361 }
362};
363
364} // namespace llvm
365
366#endif // LLVM_TRANSFORMS_VECTORIZE_VPLANCFG_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
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
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file defines the SmallVector class.
This file contains the declarations of the Vectorization Plan base classes:
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
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:4377
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:94
const VPBlocksTy & getPredecessors() const
Definition VPlan.h:222
Helper for GraphTraits specialization that traverses through VPRegionBlocks.
Definition VPlanCFG.h:151
VPBlockDeepTraversalWrapper(BlockTy Entry)
Definition VPlanCFG.h:155
Helper for GraphTraits specialization that does not traverses through VPRegionBlocks.
Definition VPlanCFG.h:202
VPBlockShallowTraversalWrapper(BlockTy Entry)
Definition VPlanCFG.h:206
static auto blocksAs(T &&Range)
Return an iterator range over Range with each block cast to BlockTy.
Definition VPlanUtils.h:331
Iterator to traverse all successors/predecessors of a VPBlockBase node, including its hierarchical su...
Definition VPlanCFG.h:66
VPHierarchicalChildrenIterator & operator++()
Definition VPlanCFG.h:133
VPHierarchicalChildrenIterator(BlockPtrTy Block, size_t Idx=0)
Definition VPlanCFG.h:112
BlockPtrTy reference
Used by iterator_facade_base with bidirectional_iterator_tag.
Definition VPlanCFG.h:110
static VPHierarchicalChildrenIterator end(BlockPtrTy Block)
Definition VPlanCFG.h:115
VPHierarchicalChildrenIterator operator++(int X)
Definition VPlanCFG.h:143
bool operator==(const VPHierarchicalChildrenIterator &R) const
Definition VPlanCFG.h:127
VPHierarchicalChildrenIterator & operator--()
Definition VPlanCFG.h:138
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4735
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.
SmallVector< VPBasicBlock * > vp_rpo_plain_cfg_loop_body(VPBasicBlock *Header)
Returns the VPBasicBlocks forming the loop body of a plain (pre-region) VPlan in reverse post-order s...
Definition VPlanCFG.h:262
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< 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:250
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:285
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
iterator_range< df_iterator< T > > depth_first(const T &G)
#define N
VPHierarchicalChildrenIterator< VPBlockBase *, false > ChildIteratorType
Definition VPlanCFG.h:332
static NodeRef getEntryNode(Inverse< NodeRef > B)
Definition VPlanCFG.h:335
static ChildIteratorType child_end(NodeRef N)
Definition VPlanCFG.h:341
static ChildIteratorType child_begin(NodeRef N)
Definition VPlanCFG.h:337
VPHierarchicalChildrenIterator< VPBlockBase * > ChildIteratorType
Definition VPlanCFG.h:302
static NodeRef getEntryNode(NodeRef N)
Definition VPlanCFG.h:304
static ChildIteratorType child_begin(NodeRef N)
Definition VPlanCFG.h:306
static ChildIteratorType child_end(NodeRef N)
Definition VPlanCFG.h:310
static NodeRef getEntryNode(VPBlockDeepTraversalWrapper< VPBlockBase * > N)
Definition VPlanCFG.h:168
VPHierarchicalChildrenIterator< VPBlockBase * > ChildIteratorType
Definition VPlanCFG.h:166
VPHierarchicalChildrenIterator< const VPBlockBase * > ChildIteratorType
Definition VPlanCFG.h:184
static NodeRef getEntryNode(VPBlockDeepTraversalWrapper< const VPBlockBase * > N)
Definition VPlanCFG.h:187
static NodeRef getEntryNode(VPBlockShallowTraversalWrapper< VPBlockBase * > N)
Definition VPlanCFG.h:214
SmallVectorImpl< VPBlockBase * >::iterator ChildIteratorType
Definition VPlanCFG.h:212
SmallVectorImpl< VPBlockBase * >::const_iterator ChildIteratorType
Definition VPlanCFG.h:230
static NodeRef getEntryNode(VPBlockShallowTraversalWrapper< const VPBlockBase * > N)
Definition VPlanCFG.h:233
static nodes_iterator nodes_end(GraphRef N)
Definition VPlanCFG.h:357
static NodeRef getEntryNode(GraphRef N)
Definition VPlanCFG.h:351
df_iterator< NodeRef > nodes_iterator
Definition VPlanCFG.h:349
static nodes_iterator nodes_begin(GraphRef N)
Definition VPlanCFG.h:353
VPHierarchicalChildrenIterator< const VPBlockBase * > ChildIteratorType
Definition VPlanCFG.h:317
static ChildIteratorType child_end(NodeRef N)
Definition VPlanCFG.h:325
static ChildIteratorType child_begin(NodeRef N)
Definition VPlanCFG.h:321
static NodeRef getEntryNode(NodeRef N)
Definition VPlanCFG.h:319