LLVM 21.0.0git
VPRecipeBuilder.h
Go to the documentation of this file.
1//===- VPRecipeBuilder.h - Helper class to build recipes --------*- 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_VPRECIPEBUILDER_H
10#define LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
11
13#include "VPlan.h"
14#include "llvm/ADT/DenseMap.h"
17#include "llvm/IR/IRBuilder.h"
18
19namespace llvm {
20
21class LoopVectorizationLegality;
22class LoopVectorizationCostModel;
23class TargetLibraryInfo;
24class TargetTransformInfo;
25struct HistogramInfo;
26struct VFRange;
27
28/// A chain of instructions that form a partial reduction.
29/// Designed to match: reduction_bin_op (bin_op (extend (A), (extend (B))),
30/// accumulator).
35 }
36 /// The top-level binary operation that forms the reduction to a scalar
37 /// after the loop body.
39 /// The extension of each of the inner binary operation's operands.
42
43 /// The binary operation using the extends that is then reduced.
45};
46
47/// Helper class to create VPRecipies from IR instructions.
49 /// The VPlan new recipes are added to.
50 VPlan &Plan;
51
52 /// The loop that we evaluate.
53 Loop *OrigLoop;
54
55 /// Target Library Info.
56 const TargetLibraryInfo *TLI;
57
58 // Target Transform Info.
60
61 /// The legality analysis.
63
64 /// The profitablity analysis.
66
68
69 VPBuilder &Builder;
70
71 /// When we if-convert we need to create edge masks. We have to cache values
72 /// so that we don't end up with exponential recursion/IR. Note that
73 /// if-conversion currently takes place during VPlan-construction, so these
74 /// caches are only used at that stage.
75 using EdgeMaskCacheTy =
78 EdgeMaskCacheTy EdgeMaskCache;
79 BlockMaskCacheTy BlockMaskCache;
80
81 // VPlan construction support: Hold a mapping from ingredients to
82 // their recipe.
84
85 /// Cross-iteration reduction & first-order recurrence phis for which we need
86 /// to add the incoming value from the backedge after all recipes have been
87 /// created.
89
90 /// A mapping of partial reduction exit instructions to their scaling factor.
92
93 /// Check if \p I can be widened at the start of \p Range and possibly
94 /// decrease the range such that the returned value holds for the entire \p
95 /// Range. The function should not be called for memory instructions or calls.
96 bool shouldWiden(Instruction *I, VFRange &Range) const;
97
98 /// Check if the load or store instruction \p I should widened for \p
99 /// Range.Start and potentially masked. Such instructions are handled by a
100 /// recipe that takes an additional VPInstruction for the mask.
101 VPWidenMemoryRecipe *tryToWidenMemory(Instruction *I,
103 VFRange &Range);
104
105 /// Check if an induction recipe should be constructed for \p Phi. If so build
106 /// and return it. If not, return null.
107 VPHeaderPHIRecipe *tryToOptimizeInductionPHI(PHINode *Phi,
109 VFRange &Range);
110
111 /// Optimize the special case where the operand of \p I is a constant integer
112 /// induction variable.
114 tryToOptimizeInductionTruncate(TruncInst *I, ArrayRef<VPValue *> Operands,
115 VFRange &Range);
116
117 /// Handle non-loop phi nodes. Return a new VPBlendRecipe otherwise. Currently
118 /// all such phi nodes are turned into a sequence of select instructions as
119 /// the vectorizer currently performs full if-conversion.
121
122 /// Handle call instructions. If \p CI can be widened for \p Range.Start,
123 /// return a new VPWidenCallRecipe or VPWidenIntrinsicRecipe. Range.End may be
124 /// decreased to ensure same decision from \p Range.Start to \p Range.End.
126 VFRange &Range);
127
128 /// Check if \p I has an opcode that can be widened and return a VPWidenRecipe
129 /// if it can. The function should only be called if the cost-model indicates
130 /// that widening should be performed.
132
133 /// Makes Histogram count operations safe for vectorization, by emitting a
134 /// llvm.experimental.vector.histogram.add intrinsic in place of the
135 /// Load + Add|Sub + Store operations that perform the histogram in the
136 /// original scalar loop.
137 VPHistogramRecipe *tryToWidenHistogram(const HistogramInfo *HI,
139
140 /// Examines reduction operations to see if the target can use a cheaper
141 /// operation with a wider per-iteration input VF and narrower PHI VF.
142 /// Each element within Chains is a pair with a struct containing reduction
143 /// information and the scaling factor between the number of elements in
144 /// the input and output.
145 /// Recursively calls itself to identify chained scaled reductions.
146 /// Returns true if this invocation added an entry to Chains, otherwise false.
147 /// i.e. returns false in the case that a subcall adds an entry to Chains,
148 /// but the top-level call does not.
149 bool getScaledReductions(
150 Instruction *PHI, Instruction *RdxExitInstr, VFRange &Range,
151 SmallVectorImpl<std::pair<PartialReductionChain, unsigned>> &Chains);
152
153public:
154 VPRecipeBuilder(VPlan &Plan, Loop *OrigLoop, const TargetLibraryInfo *TLI,
159 : Plan(Plan), OrigLoop(OrigLoop), TLI(TLI), TTI(TTI), Legal(Legal),
160 CM(CM), PSE(PSE), Builder(Builder) {}
161
162 std::optional<unsigned> getScalingForReduction(const Instruction *ExitInst) {
163 auto It = ScaledReductionMap.find(ExitInst);
164 return It == ScaledReductionMap.end() ? std::nullopt
165 : std::make_optional(It->second);
166 }
167
168 /// Find all possible partial reductions in the loop and track all of those
169 /// that are valid so recipes can be formed later.
171
172 /// Create and return a widened recipe for \p I if one can be created within
173 /// the given VF \p Range.
176 VFRange &Range);
177
178 /// Create and return a partial reduction recipe for a reduction instruction
179 /// along with binary operation and reduction phi operands.
182
183 /// Set the recipe created for given ingredient.
185 assert(!Ingredient2Recipe.contains(I) &&
186 "Cannot reset recipe for instruction.");
187 Ingredient2Recipe[I] = R;
188 }
189
190 /// Create the mask for the vector loop header block.
191 void createHeaderMask();
192
193 /// A helper function that computes the predicate of the block BB, assuming
194 /// that the header block of the loop is set to True or the loop mask when
195 /// tail folding.
197
198 /// Returns the *entry* mask for the block \p BB.
200
201 /// Create an edge mask for every destination of cases and/or default.
203
204 /// A helper function that computes the predicate of the edge between SRC
205 /// and DST.
207
208 /// A helper that returns the previously computed predicate of the edge
209 /// between SRC and DST.
210 VPValue *getEdgeMask(BasicBlock *Src, BasicBlock *Dst) const;
211
212 /// Return the recipe created for given ingredient.
214 assert(Ingredient2Recipe.count(I) &&
215 "Recording this ingredients recipe was not requested");
216 assert(Ingredient2Recipe[I] != nullptr &&
217 "Ingredient doesn't have a recipe");
218 return Ingredient2Recipe[I];
219 }
220
221 /// Build a VPReplicationRecipe for \p I using \p Operands. If it is
222 /// predicated, add the mask as last operand. Range.End may be decreased to
223 /// ensure same recipe behavior from \p Range.Start to \p Range.End.
226 VFRange &Range);
227
228 /// Add the incoming values from the backedge to reduction & first-order
229 /// recurrence cross-iteration phis.
230 void fixHeaderPhis();
231
232 /// Returns a range mapping the values of the range \p Operands to their
233 /// corresponding VPValues.
234 iterator_range<mapped_iterator<Use *, std::function<VPValue *(Value *)>>>
236
238 if (auto *I = dyn_cast<Instruction>(V)) {
239 if (auto *R = Ingredient2Recipe.lookup(I))
240 return R->getVPSingleValue();
241 }
242 return Plan.getOrAddLiveIn(V);
243 }
244};
245} // end namespace llvm
246
247#endif // LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
Rewrite undef for PHI
This file defines the DenseMap class.
This file provides a LoopVectorizationPlanner class.
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
This file defines the PointerUnion class, which is a discriminated union of pointer types.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
This class represents a function call, abstracting a target machine's calling convention.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:194
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:152
iterator end()
Definition: DenseMap.h:84
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:147
LoopVectorizationCostModel - estimates the expected speedups due to vectorization.
LoopVectorizationLegality checks if it is legal to vectorize a loop, and to what vectorization factor...
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:39
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
Multiway switch.
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
This class represents a truncation of integer types.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
A recipe for vectorizing a phi-node as a sequence of mask-based select instructions.
Definition: VPlan.h:2160
VPlan-based builder utility analogous to IRBuilder.
A pure virtual base class for all recipes modeling header phis, including phis for first order recurr...
Definition: VPlan.h:1689
A recipe representing a sequence of load -> update -> store as part of a histogram operation.
Definition: VPlan.h:1439
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition: VPlan.h:366
Helper class to create VPRecipies from IR instructions.
VPRecipeBase * tryToCreatePartialReduction(Instruction *Reduction, ArrayRef< VPValue * > Operands)
Create and return a partial reduction recipe for a reduction instruction along with binary operation ...
VPValue * createEdgeMask(BasicBlock *Src, BasicBlock *Dst)
A helper function that computes the predicate of the edge between SRC and DST.
VPRecipeBase * tryToCreateWidenRecipe(Instruction *Instr, ArrayRef< VPValue * > Operands, VFRange &Range)
Create and return a widened recipe for I if one can be created within the given VF Range.
void createSwitchEdgeMasks(SwitchInst *SI)
Create an edge mask for every destination of cases and/or default.
VPValue * getBlockInMask(BasicBlock *BB) const
Returns the entry mask for the block BB.
VPValue * getEdgeMask(BasicBlock *Src, BasicBlock *Dst) const
A helper that returns the previously computed predicate of the edge between SRC and DST.
iterator_range< mapped_iterator< Use *, std::function< VPValue *(Value *)> > > mapToVPValues(User::op_range Operands)
Returns a range mapping the values of the range Operands to their corresponding VPValues.
void fixHeaderPhis()
Add the incoming values from the backedge to reduction & first-order recurrence cross-iteration phis.
VPValue * getVPValueOrAddLiveIn(Value *V)
void createHeaderMask()
Create the mask for the vector loop header block.
std::optional< unsigned > getScalingForReduction(const Instruction *ExitInst)
void createBlockInMask(BasicBlock *BB)
A helper function that computes the predicate of the block BB, assuming that the header block of the ...
void collectScaledReductions(VFRange &Range)
Find all possible partial reductions in the loop and track all of those that are valid so recipes can...
void setRecipe(Instruction *I, VPRecipeBase *R)
Set the recipe created for given ingredient.
VPReplicateRecipe * handleReplication(Instruction *I, ArrayRef< VPValue * > Operands, VFRange &Range)
Build a VPReplicationRecipe for I using Operands.
VPRecipeBase * getRecipe(Instruction *I)
Return the recipe created for given ingredient.
VPRecipeBuilder(VPlan &Plan, Loop *OrigLoop, const TargetLibraryInfo *TLI, const TargetTransformInfo *TTI, LoopVectorizationLegality *Legal, LoopVectorizationCostModel &CM, PredicatedScalarEvolution &PSE, VPBuilder &Builder)
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition: VPlan.h:2443
VPSingleDef is a base class for recipes for modeling a sequence of one or more output IR that define ...
Definition: VPlan.h:493
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition: VPlan.h:1804
A common base class for widening memory operations.
Definition: VPlan.h:2616
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition: VPlan.h:1093
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition: VPlan.h:3478
VPValue * getOrAddLiveIn(Value *V)
Gets the live-in VPValue for V or adds a new live-in (if none exists yet) for V.
Definition: VPlan.h:3710
LLVM Value Representation.
Definition: Value.h:74
A range adaptor for a pair of iterators.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
This holds details about a histogram operation – a load -> update -> store sequence where each lane i...
A chain of instructions that form a partial reduction.
Instruction * BinOp
The binary operation using the extends that is then reduced.
PartialReductionChain(Instruction *Reduction, Instruction *ExtendA, Instruction *ExtendB, Instruction *BinOp)
Instruction * Reduction
The top-level binary operation that forms the reduction to a scalar after the loop body.
Instruction * ExtendA
The extension of each of the inner binary operation's operands.
A range of powers-of-2 vectorization factors with fixed start and adjustable end.
Definition: VPlanHelpers.h:62