LLVM 22.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"
16
17namespace llvm {
18
23struct HistogramInfo;
24struct VFRange;
25
26/// A chain of instructions that form a partial reduction.
27/// Designed to match either:
28/// reduction_bin_op (extend (A), accumulator), or
29/// reduction_bin_op (bin_op (extend (A), (extend (B))), accumulator).
35 /// The top-level binary operation that forms the reduction to a scalar
36 /// after the loop body.
38 /// The extension of each of the inner binary operation's operands.
41
42 /// The user of the extends that is then reduced.
44};
45
46/// Helper class to create VPRecipies from IR instructions.
48 /// The VPlan new recipes are added to.
49 VPlan &Plan;
50
51 /// The loop that we evaluate.
52 Loop *OrigLoop;
53
54 /// Target Library Info.
55 const TargetLibraryInfo *TLI;
56
57 // Target Transform Info.
58 const TargetTransformInfo *TTI;
59
60 /// The legality analysis.
62
63 /// The profitablity analysis.
65
66 VPBuilder &Builder;
67
68 /// The mask of each VPBB, generated earlier and used for predicating recipes
69 /// in VPBB.
70 /// TODO: remove by applying predication when generating the masks.
72
73 // VPlan construction support: Hold a mapping from ingredients to
74 // their recipe.
76
77 /// Cross-iteration reduction & first-order recurrence phis for which we need
78 /// to add the incoming value from the backedge after all recipes have been
79 /// created.
81
82 /// A mapping of partial reduction exit instructions to their scaling factor.
84
85 /// Check if \p I can be widened at the start of \p Range and possibly
86 /// decrease the range such that the returned value holds for the entire \p
87 /// Range. The function should not be called for memory instructions or calls.
88 bool shouldWiden(Instruction *I, VFRange &Range) const;
89
90 /// Check if the load or store instruction \p VPI should widened for \p
91 /// Range.Start and potentially masked. Such instructions are handled by a
92 /// recipe that takes an additional VPInstruction for the mask.
93 VPWidenMemoryRecipe *tryToWidenMemory(VPInstruction *VPI, VFRange &Range);
94
95 /// Optimize the special case where the operand of \p VPI is a constant
96 /// integer induction variable.
98 tryToOptimizeInductionTruncate(VPInstruction *VPI, VFRange &Range);
99
100 /// Handle call instructions. If \p VPI can be widened for \p Range.Start,
101 /// return a new VPWidenCallRecipe or VPWidenIntrinsicRecipe. Range.End may be
102 /// decreased to ensure same decision from \p Range.Start to \p Range.End.
103 VPSingleDefRecipe *tryToWidenCall(VPInstruction *VPI, VFRange &Range);
104
105 /// Check if \p VPI has an opcode that can be widened and return a
106 /// VPWidenRecipe if it can. The function should only be called if the
107 /// cost-model indicates that widening should be performed.
108 VPWidenRecipe *tryToWiden(VPInstruction *VPI);
109
110 /// Makes Histogram count operations safe for vectorization, by emitting a
111 /// llvm.experimental.vector.histogram.add intrinsic in place of the
112 /// Load + Add|Sub + Store operations that perform the histogram in the
113 /// original scalar loop.
114 VPHistogramRecipe *tryToWidenHistogram(const HistogramInfo *HI,
115 VPInstruction *VPI);
116
117 /// Examines reduction operations to see if the target can use a cheaper
118 /// operation with a wider per-iteration input VF and narrower PHI VF.
119 /// Each element within Chains is a pair with a struct containing reduction
120 /// information and the scaling factor between the number of elements in
121 /// the input and output.
122 /// Recursively calls itself to identify chained scaled reductions.
123 /// Returns true if this invocation added an entry to Chains, otherwise false.
124 /// i.e. returns false in the case that a subcall adds an entry to Chains,
125 /// but the top-level call does not.
126 bool getScaledReductions(
127 Instruction *PHI, Instruction *RdxExitInstr, VFRange &Range,
128 SmallVectorImpl<std::pair<PartialReductionChain, unsigned>> &Chains);
129
130public:
131 VPRecipeBuilder(VPlan &Plan, Loop *OrigLoop, const TargetLibraryInfo *TLI,
132 const TargetTransformInfo *TTI,
136 : Plan(Plan), OrigLoop(OrigLoop), TLI(TLI), TTI(TTI), Legal(Legal),
137 CM(CM), Builder(Builder), BlockMaskCache(BlockMaskCache) {}
138
139 std::optional<unsigned> getScalingForReduction(const Instruction *ExitInst) {
140 auto It = ScaledReductionMap.find(ExitInst);
141 return It == ScaledReductionMap.end() ? std::nullopt
142 : std::make_optional(It->second);
143 }
144
145 /// Find all possible partial reductions in the loop and track all of those
146 /// that are valid so recipes can be formed later.
148
149 /// Create and return a widened recipe for a non-phi recipe \p R if one can be
150 /// created within the given VF \p Range.
152 VFRange &Range);
153
154 /// Create and return a partial reduction recipe for a reduction instruction
155 /// along with binary operation and reduction phi operands.
157 unsigned ScaleFactor);
158
159 /// Set the recipe created for given ingredient.
161 assert(!Ingredient2Recipe.contains(I) &&
162 "Cannot reset recipe for instruction.");
163 Ingredient2Recipe[I] = R;
164 }
165
166 /// Returns the *entry* mask for block \p VPBB or null if the mask is
167 /// all-true.
169 return BlockMaskCache.lookup(VPBB);
170 }
171
172 /// Return the recipe created for given ingredient.
174 assert(Ingredient2Recipe.count(I) &&
175 "Recording this ingredients recipe was not requested");
176 assert(Ingredient2Recipe[I] != nullptr &&
177 "Ingredient doesn't have a recipe");
178 return Ingredient2Recipe[I];
179 }
180
181 /// Build a VPReplicationRecipe for \p VPI. If it is predicated, add the mask
182 /// as last operand. Range.End may be decreased to ensure same recipe behavior
183 /// from \p Range.Start to \p Range.End.
185
187 if (auto *I = dyn_cast<Instruction>(V)) {
188 if (auto *R = Ingredient2Recipe.lookup(I))
189 return R->getVPSingleValue();
190 }
191 return Plan.getOrAddLiveIn(V);
192 }
193
195 for (auto &[_, V] : BlockMaskCache) {
196 if (auto *New = Old2New.lookup(V)) {
197 V->replaceAllUsesWith(New);
198 V = New;
199 }
200 }
201 }
202};
203} // end namespace llvm
204
205#endif // LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
This file defines the DenseMap class.
#define _
This file provides a LoopVectorizationPlanner class.
#define I(x, y, z)
Definition MD5.cpp:57
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
This file contains the declarations of the Vectorization Plan base classes:
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:205
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:40
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.
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.
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:3980
VPlan-based builder utility analogous to IRBuilder.
A recipe representing a sequence of load -> update -> store as part of a histogram operation.
Definition VPlan.h:1759
This is a concrete Recipe that models a single VPlan-level instruction.
Definition VPlan.h:1036
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:387
VPValue * getBlockInMask(VPBasicBlock *VPBB) const
Returns the entry mask for block VPBB or null if the mask is all-true.
VPRecipeBase * tryToCreateWidenNonPhiRecipe(VPSingleDefRecipe *R, VFRange &Range)
Create and return a widened recipe for a non-phi recipe R if one can be created within the given VF R...
VPValue * getVPValueOrAddLiveIn(Value *V)
VPRecipeBase * tryToCreatePartialReduction(VPInstruction *Reduction, unsigned ScaleFactor)
Create and return a partial reduction recipe for a reduction instruction along with binary operation ...
std::optional< unsigned > getScalingForReduction(const Instruction *ExitInst)
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.
VPRecipeBuilder(VPlan &Plan, Loop *OrigLoop, const TargetLibraryInfo *TLI, const TargetTransformInfo *TTI, LoopVectorizationLegality *Legal, LoopVectorizationCostModel &CM, VPBuilder &Builder, DenseMap< VPBasicBlock *, VPValue * > &BlockMaskCache)
VPReplicateRecipe * handleReplication(VPInstruction *VPI, VFRange &Range)
Build a VPReplicationRecipe for VPI.
VPRecipeBase * getRecipe(Instruction *I)
Return the recipe created for given ingredient.
void updateBlockMaskCache(DenseMap< VPValue *, VPValue * > &Old2New)
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition VPlan.h:2951
VPSingleDef is a base class for recipes for modeling a sequence of one or more output IR that define ...
Definition VPlan.h:531
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Definition VPlanValue.h:46
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition VPlan.h:2197
A common base class for widening memory operations.
Definition VPlan.h:3262
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition VPlan.h:1514
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4298
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
This holds details about a histogram operation – a load -> update -> store sequence where each lane i...
PartialReductionChain(Instruction *Reduction, Instruction *ExtendA, Instruction *ExtendB, Instruction *ExtendUser)
Instruction * ExtendUser
The user of the extends that is then reduced.
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.