LLVM 20.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#include "llvm/IR/IRBuilder.h"
17
18namespace llvm {
19
20class LoopVectorizationLegality;
21class LoopVectorizationCostModel;
22class TargetLibraryInfo;
23
24/// Helper class to create VPRecipies from IR instructions.
26 /// The VPlan new recipes are added to.
27 VPlan &Plan;
28
29 /// The loop that we evaluate.
30 Loop *OrigLoop;
31
32 /// Target Library Info.
33 const TargetLibraryInfo *TLI;
34
35 /// The legality analysis.
37
38 /// The profitablity analysis.
40
42
43 VPBuilder &Builder;
44
45 /// When we if-convert we need to create edge masks. We have to cache values
46 /// so that we don't end up with exponential recursion/IR. Note that
47 /// if-conversion currently takes place during VPlan-construction, so these
48 /// caches are only used at that stage.
49 using EdgeMaskCacheTy =
52 EdgeMaskCacheTy EdgeMaskCache;
53 BlockMaskCacheTy BlockMaskCache;
54
55 // VPlan construction support: Hold a mapping from ingredients to
56 // their recipe.
58
59 /// Cross-iteration reduction & first-order recurrence phis for which we need
60 /// to add the incoming value from the backedge after all recipes have been
61 /// created.
63
64 /// Check if \p I can be widened at the start of \p Range and possibly
65 /// decrease the range such that the returned value holds for the entire \p
66 /// Range. The function should not be called for memory instructions or calls.
67 bool shouldWiden(Instruction *I, VFRange &Range) const;
68
69 /// Check if the load or store instruction \p I should widened for \p
70 /// Range.Start and potentially masked. Such instructions are handled by a
71 /// recipe that takes an additional VPInstruction for the mask.
72 VPWidenMemoryRecipe *tryToWidenMemory(Instruction *I,
74 VFRange &Range);
75
76 /// Check if an induction recipe should be constructed for \p Phi. If so build
77 /// and return it. If not, return null.
78 VPHeaderPHIRecipe *tryToOptimizeInductionPHI(PHINode *Phi,
80 VFRange &Range);
81
82 /// Optimize the special case where the operand of \p I is a constant integer
83 /// induction variable.
85 tryToOptimizeInductionTruncate(TruncInst *I, ArrayRef<VPValue *> Operands,
86 VFRange &Range);
87
88 /// Handle non-loop phi nodes. Return a new VPBlendRecipe otherwise. Currently
89 /// all such phi nodes are turned into a sequence of select instructions as
90 /// the vectorizer currently performs full if-conversion.
92
93 /// Handle call instructions. If \p CI can be widened for \p Range.Start,
94 /// return a new VPWidenCallRecipe. Range.End may be decreased to ensure same
95 /// decision from \p Range.Start to \p Range.End.
97 VFRange &Range);
98
99 /// Check if \p I has an opcode that can be widened and return a VPWidenRecipe
100 /// if it can. The function should only be called if the cost-model indicates
101 /// that widening should be performed.
103 VPBasicBlock *VPBB);
104
105public:
106 VPRecipeBuilder(VPlan &Plan, Loop *OrigLoop, const TargetLibraryInfo *TLI,
110 : Plan(Plan), OrigLoop(OrigLoop), TLI(TLI), Legal(Legal), CM(CM),
111 PSE(PSE), Builder(Builder) {}
112
113 /// Create and return a widened recipe for \p I if one can be created within
114 /// the given VF \p Range.
117 VFRange &Range, VPBasicBlock *VPBB);
118
119 /// Set the recipe created for given ingredient.
121 assert(!Ingredient2Recipe.contains(I) &&
122 "Cannot reset recipe for instruction.");
123 Ingredient2Recipe[I] = R;
124 }
125
126 /// Create the mask for the vector loop header block.
127 void createHeaderMask();
128
129 /// A helper function that computes the predicate of the block BB, assuming
130 /// that the header block of the loop is set to True or the loop mask when
131 /// tail folding.
133
134 /// Returns the *entry* mask for the block \p BB.
136
137 /// Create an edge mask for every destination of cases and/or default.
139
140 /// A helper function that computes the predicate of the edge between SRC
141 /// and DST.
143
144 /// A helper that returns the previously computed predicate of the edge
145 /// between SRC and DST.
146 VPValue *getEdgeMask(BasicBlock *Src, BasicBlock *Dst) const;
147
148 /// Return the recipe created for given ingredient.
150 assert(Ingredient2Recipe.count(I) &&
151 "Recording this ingredients recipe was not requested");
152 assert(Ingredient2Recipe[I] != nullptr &&
153 "Ingredient doesn't have a recipe");
154 return Ingredient2Recipe[I];
155 }
156
157 /// Build a VPReplicationRecipe for \p I. If it is predicated, add the mask as
158 /// last operand. Range.End may be decreased to ensure same recipe behavior
159 /// from \p Range.Start to \p Range.End.
161
162 /// Add the incoming values from the backedge to reduction & first-order
163 /// recurrence cross-iteration phis.
164 void fixHeaderPhis();
165
166 /// Returns a range mapping the values of the range \p Operands to their
167 /// corresponding VPValues.
168 iterator_range<mapped_iterator<Use *, std::function<VPValue *(Value *)>>>
170
172 if (auto *I = dyn_cast<Instruction>(V)) {
173 if (auto *R = Ingredient2Recipe.lookup(I))
174 return R->getVPSingleValue();
175 }
176 return Plan.getOrAddLiveIn(V);
177 }
178};
179} // end namespace llvm
180
181#endif // LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
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
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:151
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:146
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 is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
Multiway switch.
Provides information about what library functions are available for the current target.
This class represents a truncation of integer types.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition: VPlan.h:2986
A recipe for vectorizing a phi-node as a sequence of mask-based select instructions.
Definition: VPlan.h:2041
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:1727
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition: VPlan.h:766
Helper class to create VPRecipies from IR instructions.
VPValue * getVPValueOrAddLiveIn(Value *V, VPlan &Plan)
VPValue * createEdgeMask(BasicBlock *Src, BasicBlock *Dst)
A helper function that computes the predicate of the edge between SRC and DST.
VPReplicateRecipe * handleReplication(Instruction *I, VFRange &Range)
Build a VPReplicationRecipe for I.
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.
VPRecipeBase * tryToCreateWidenRecipe(Instruction *Instr, ArrayRef< VPValue * > Operands, VFRange &Range, VPBasicBlock *VPBB)
Create and return a widened recipe for I if one can be created within the given VF Range.
void createHeaderMask()
Create the mask for the vector loop header block.
VPRecipeBuilder(VPlan &Plan, Loop *OrigLoop, const TargetLibraryInfo *TLI, LoopVectorizationLegality *Legal, LoopVectorizationCostModel &CM, PredicatedScalarEvolution &PSE, VPBuilder &Builder)
void createBlockInMask(BasicBlock *BB)
A helper function that computes the predicate of the block BB, assuming that the header block of the ...
void setRecipe(Instruction *I, VPRecipeBase *R)
Set the recipe created for given ingredient.
VPRecipeBase * getRecipe(Instruction *I)
Return the recipe created for given ingredient.
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition: VPlan.h:2304
A recipe for widening Call instructions.
Definition: VPlan.h:1534
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition: VPlan.h:1784
A common base class for widening memory operations.
Definition: VPlan.h:2461
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition: VPlan.h:1410
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition: VPlan.h:3268
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:3436
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
A range of powers-of-2 vectorization factors with fixed start and adjustable end.
Definition: VPlan.h:100