LLVM 22.0.0git
VPlanUtils.h
Go to the documentation of this file.
1//===- VPlanUtils.h - VPlan-related utilities -------------------*- 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
9#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLANUTILS_H
10#define LLVM_TRANSFORMS_VECTORIZE_VPLANUTILS_H
11
12#include "VPlan.h"
13
14namespace llvm {
15class ScalarEvolution;
16class SCEV;
17} // namespace llvm
18
19namespace llvm {
20
21namespace vputils {
22/// Returns true if only the first lane of \p Def is used.
23bool onlyFirstLaneUsed(const VPValue *Def);
24
25/// Returns true if only the first part of \p Def is used.
26bool onlyFirstPartUsed(const VPValue *Def);
27
28/// Returns true if only scalar values of \p Def are used by all users.
29bool onlyScalarValuesUsed(const VPValue *Def);
30
31/// Get or create a VPValue that corresponds to the expansion of \p Expr. If \p
32/// Expr is a SCEVConstant or SCEVUnknown, return a VPValue wrapping the live-in
33/// value. Otherwise return a VPExpandSCEVRecipe to expand \p Expr. If \p Plan's
34/// pre-header already contains a recipe expanding \p Expr, return it. If not,
35/// create a new one.
37
38/// Return the SCEV expression for \p V. Returns SCEVCouldNotCompute if no
39/// SCEV expression could be constructed.
41 const Loop *L = nullptr);
42
43/// Returns true if \p VPV is a single scalar, either because it produces the
44/// same value for all lanes or only has its first lane used.
45bool isSingleScalar(const VPValue *VPV);
46
47/// Return true if \p V is a header mask in \p Plan.
48bool isHeaderMask(const VPValue *V, const VPlan &Plan);
49
50/// Checks if \p V is uniform across all VF lanes and UF parts. It is considered
51/// as such if it is either loop invariant (defined outside the vector region)
52/// or its operand is known to be uniform across all VFs and UFs (e.g.
53/// VPDerivedIV or VPCanonicalIVPHI).
55
56/// Returns the header block of the first, top-level loop, or null if none
57/// exist.
59
60/// Get the VF scaling factor applied to the recipe's output, if the recipe has
61/// one.
63
64/// Returns the VPValue representing the uncountable exit comparison used by
65/// AnyOf if the recipes it depends on can be traced back to live-ins and
66/// the addresses (in GEP/PtrAdd form) of any (non-masked) load used in
67/// generating the values for the comparison. The recipes are stored in
68/// \p Recipes, and recipes forming an address for a load are also added to
69/// \p GEPs.
70std::optional<VPValue *>
74} // namespace vputils
75
76//===----------------------------------------------------------------------===//
77// Utilities for modifying predecessors and successors of VPlan blocks.
78//===----------------------------------------------------------------------===//
79
80/// Class that provides utilities for VPBlockBases in VPlan.
82public:
83 VPBlockUtils() = delete;
84
85 /// Insert disconnected VPBlockBase \p NewBlock after \p BlockPtr. Add \p
86 /// NewBlock as successor of \p BlockPtr and \p BlockPtr as predecessor of \p
87 /// NewBlock, and propagate \p BlockPtr parent to \p NewBlock. \p BlockPtr's
88 /// successors are moved from \p BlockPtr to \p NewBlock. \p NewBlock must
89 /// have neither successors nor predecessors.
90 static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
91 assert(NewBlock->getSuccessors().empty() &&
92 NewBlock->getPredecessors().empty() &&
93 "Can't insert new block with predecessors or successors.");
94 NewBlock->setParent(BlockPtr->getParent());
95 SmallVector<VPBlockBase *> Succs(BlockPtr->successors());
96 for (VPBlockBase *Succ : Succs) {
97 Succ->replacePredecessor(BlockPtr, NewBlock);
98 NewBlock->appendSuccessor(Succ);
99 }
100 BlockPtr->clearSuccessors();
101 connectBlocks(BlockPtr, NewBlock);
102 }
103
104 /// Insert disconnected block \p NewBlock before \p Blockptr. First
105 /// disconnects all predecessors of \p BlockPtr and connects them to \p
106 /// NewBlock. Add \p NewBlock as predecessor of \p BlockPtr and \p BlockPtr as
107 /// successor of \p NewBlock.
108 static void insertBlockBefore(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
109 assert(NewBlock->getSuccessors().empty() &&
110 NewBlock->getPredecessors().empty() &&
111 "Can't insert new block with predecessors or successors.");
112 NewBlock->setParent(BlockPtr->getParent());
113 for (VPBlockBase *Pred : to_vector(BlockPtr->predecessors())) {
114 Pred->replaceSuccessor(BlockPtr, NewBlock);
115 NewBlock->appendPredecessor(Pred);
116 }
117 BlockPtr->clearPredecessors();
118 connectBlocks(NewBlock, BlockPtr);
119 }
120
121 /// Insert disconnected VPBlockBases \p IfTrue and \p IfFalse after \p
122 /// BlockPtr. Add \p IfTrue and \p IfFalse as succesors of \p BlockPtr and \p
123 /// BlockPtr as predecessor of \p IfTrue and \p IfFalse. Propagate \p BlockPtr
124 /// parent to \p IfTrue and \p IfFalse. \p BlockPtr must have no successors
125 /// and \p IfTrue and \p IfFalse must have neither successors nor
126 /// predecessors.
127 static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
128 VPBlockBase *BlockPtr) {
129 assert(IfTrue->getSuccessors().empty() &&
130 "Can't insert IfTrue with successors.");
131 assert(IfFalse->getSuccessors().empty() &&
132 "Can't insert IfFalse with successors.");
133 BlockPtr->setTwoSuccessors(IfTrue, IfFalse);
134 IfTrue->setPredecessors({BlockPtr});
135 IfFalse->setPredecessors({BlockPtr});
136 IfTrue->setParent(BlockPtr->getParent());
137 IfFalse->setParent(BlockPtr->getParent());
138 }
139
140 /// Connect VPBlockBases \p From and \p To bi-directionally. If \p PredIdx is
141 /// -1, append \p From to the predecessors of \p To, otherwise set \p To's
142 /// predecessor at \p PredIdx to \p From. If \p SuccIdx is -1, append \p To to
143 /// the successors of \p From, otherwise set \p From's successor at \p SuccIdx
144 /// to \p To. Both VPBlockBases must have the same parent, which can be null.
145 /// Both VPBlockBases can be already connected to other VPBlockBases.
146 static void connectBlocks(VPBlockBase *From, VPBlockBase *To,
147 unsigned PredIdx = -1u, unsigned SuccIdx = -1u) {
148 assert((From->getParent() == To->getParent()) &&
149 "Can't connect two block with different parents");
150 assert((SuccIdx != -1u || From->getNumSuccessors() < 2) &&
151 "Blocks can't have more than two successors.");
152 if (SuccIdx == -1u)
153 From->appendSuccessor(To);
154 else
155 From->getSuccessors()[SuccIdx] = To;
156
157 if (PredIdx == -1u)
158 To->appendPredecessor(From);
159 else
160 To->getPredecessors()[PredIdx] = From;
161 }
162
163 /// Disconnect VPBlockBases \p From and \p To bi-directionally. Remove \p To
164 /// from the successors of \p From and \p From from the predecessors of \p To.
165 static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To) {
166 assert(To && "Successor to disconnect is null.");
167 From->removeSuccessor(To);
168 To->removePredecessor(From);
169 }
170
171 /// Reassociate all the blocks connected to \p Old so that they now point to
172 /// \p New.
173 static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New) {
174 for (auto *Pred : to_vector(Old->getPredecessors()))
175 Pred->replaceSuccessor(Old, New);
176 for (auto *Succ : to_vector(Old->getSuccessors()))
177 Succ->replacePredecessor(Old, New);
178 New->setPredecessors(Old->getPredecessors());
179 New->setSuccessors(Old->getSuccessors());
180 Old->clearPredecessors();
181 Old->clearSuccessors();
182 }
183
184 /// Return an iterator range over \p Range which only includes \p BlockTy
185 /// blocks. The accesses are casted to \p BlockTy.
186 template <typename BlockTy, typename T>
187 static auto blocksOnly(const T &Range) {
188 // Create BaseTy with correct const-ness based on BlockTy.
189 using BaseTy = std::conditional_t<std::is_const<BlockTy>::value,
190 const VPBlockBase, VPBlockBase>;
191
192 // We need to first create an iterator range over (const) BlocktTy & instead
193 // of (const) BlockTy * for filter_range to work properly.
194 auto Mapped =
195 map_range(Range, [](BaseTy *Block) -> BaseTy & { return *Block; });
197 Mapped, [](BaseTy &Block) { return isa<BlockTy>(&Block); });
198 return map_range(Filter, [](BaseTy &Block) -> BlockTy * {
199 return cast<BlockTy>(&Block);
200 });
201 }
202
203 /// Inserts \p BlockPtr on the edge between \p From and \p To. That is, update
204 /// \p From's successor to \p To to point to \p BlockPtr and \p To's
205 /// predecessor from \p From to \p BlockPtr. \p From and \p To are added to \p
206 /// BlockPtr's predecessors and successors respectively. There must be a
207 /// single edge between \p From and \p To.
208 static void insertOnEdge(VPBlockBase *From, VPBlockBase *To,
209 VPBlockBase *BlockPtr) {
210 unsigned SuccIdx = From->getIndexForSuccessor(To);
211 unsigned PredIx = To->getIndexForPredecessor(From);
212 VPBlockUtils::connectBlocks(From, BlockPtr, -1, SuccIdx);
213 VPBlockUtils::connectBlocks(BlockPtr, To, PredIx, -1);
214 }
215
216 /// Returns true if \p VPB is a loop header, based on regions or \p VPDT in
217 /// their absence.
218 static bool isHeader(const VPBlockBase *VPB, const VPDominatorTree &VPDT);
219
220 /// Returns true if \p VPB is a loop latch, using isHeader().
221 static bool isLatch(const VPBlockBase *VPB, const VPDominatorTree &VPDT);
222};
223
224} // namespace llvm
225
226#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
std::pair< BasicBlock *, unsigned > BlockTy
A pair of (basic block, score).
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
This file contains the declarations of the Vectorization Plan base classes:
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
This class represents an analyzed expression in the program.
The main scalar evolution driver.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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:3821
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:80
VPRegionBlock * getParent()
Definition VPlan.h:172
iterator_range< VPBlockBase ** > predecessors()
Definition VPlan.h:201
size_t getNumSuccessors() const
Definition VPlan.h:218
iterator_range< VPBlockBase ** > successors()
Definition VPlan.h:200
unsigned getIndexForSuccessor(const VPBlockBase *Succ) const
Returns the index for Succ in the blocks successor list.
Definition VPlan.h:334
void setPredecessors(ArrayRef< VPBlockBase * > NewPreds)
Set each VPBasicBlock in NewPreds as predecessor of this VPBlockBase.
Definition VPlan.h:290
unsigned getIndexForPredecessor(const VPBlockBase *Pred) const
Returns the index for Pred in the blocks predecessors list.
Definition VPlan.h:327
const VPBlocksTy & getPredecessors() const
Definition VPlan.h:203
void clearSuccessors()
Remove all the successors of this block.
Definition VPlan.h:309
void setTwoSuccessors(VPBlockBase *IfTrue, VPBlockBase *IfFalse)
Set two given VPBlockBases IfTrue and IfFalse to be the two successors of this VPBlockBase.
Definition VPlan.h:281
void clearPredecessors()
Remove all the predecessor of this block.
Definition VPlan.h:306
void setParent(VPRegionBlock *P)
Definition VPlan.h:183
const VPBlocksTy & getSuccessors() const
Definition VPlan.h:197
static auto blocksOnly(const T &Range)
Return an iterator range over Range which only includes BlockTy blocks.
Definition VPlanUtils.h:187
static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr)
Insert disconnected VPBlockBase NewBlock after BlockPtr.
Definition VPlanUtils.h:90
static void insertOnEdge(VPBlockBase *From, VPBlockBase *To, VPBlockBase *BlockPtr)
Inserts BlockPtr on the edge between From and To.
Definition VPlanUtils.h:208
static bool isLatch(const VPBlockBase *VPB, const VPDominatorTree &VPDT)
Returns true if VPB is a loop latch, using isHeader().
static bool isHeader(const VPBlockBase *VPB, const VPDominatorTree &VPDT)
Returns true if VPB is a loop header, based on regions or VPDT in their absence.
static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse, VPBlockBase *BlockPtr)
Insert disconnected VPBlockBases IfTrue and IfFalse after BlockPtr.
Definition VPlanUtils.h:127
static void connectBlocks(VPBlockBase *From, VPBlockBase *To, unsigned PredIdx=-1u, unsigned SuccIdx=-1u)
Connect VPBlockBases From and To bi-directionally.
Definition VPlanUtils.h:146
static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To)
Disconnect VPBlockBases From and To bi-directionally.
Definition VPlanUtils.h:165
static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New)
Reassociate all the blocks connected to Old so that they now point to New.
Definition VPlanUtils.h:173
static void insertBlockBefore(VPBlockBase *NewBlock, VPBlockBase *BlockPtr)
Insert disconnected block NewBlock before Blockptr.
Definition VPlanUtils.h:108
Template specialization of the standard LLVM dominator tree utility for VPBlockBases.
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:386
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Definition VPlanValue.h:48
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4139
bool isSingleScalar(const VPValue *VPV)
Returns true if VPV is a single scalar, either because it produces the same value for all lanes or on...
bool isUniformAcrossVFsAndUFs(VPValue *V)
Checks if V is uniform across all VF lanes and UF parts.
VPValue * getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr)
Get or create a VPValue that corresponds to the expansion of Expr.
VPBasicBlock * getFirstLoopHeader(VPlan &Plan, VPDominatorTree &VPDT)
Returns the header block of the first, top-level loop, or null if none exist.
bool onlyFirstPartUsed(const VPValue *Def)
Returns true if only the first part of Def is used.
bool onlyFirstLaneUsed(const VPValue *Def)
Returns true if only the first lane of Def is used.
bool onlyScalarValuesUsed(const VPValue *Def)
Returns true if only scalar values of Def are used by all users.
unsigned getVFScaleFactor(VPRecipeBase *R)
Get the VF scaling factor applied to the recipe's output, if the recipe has one.
bool isHeaderMask(const VPValue *V, const VPlan &Plan)
Return true if V is a header mask in Plan.
std::optional< VPValue * > getRecipesForUncountableExit(VPlan &Plan, SmallVectorImpl< VPRecipeBase * > &Recipes, SmallVectorImpl< VPRecipeBase * > &GEPs)
Returns the VPValue representing the uncountable exit comparison used by AnyOf if the recipes it depe...
const SCEV * getSCEVExprForVPValue(const VPValue *V, ScalarEvolution &SE, const Loop *L=nullptr)
Return the SCEV expression for V.
This is an optimization pass for GlobalISel generic memory operations.
auto map_range(ContainerTy &&C, FuncTy F)
Definition STLExtras.h:364
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition STLExtras.h:550
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
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559