LLVM 18.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
25
26/// Helper class to create VPRecipies from IR instructions.
28 /// The loop that we evaluate.
29 Loop *OrigLoop;
30
31 /// Target Library Info.
32 const TargetLibraryInfo *TLI;
33
34 /// The legality analysis.
36
37 /// The profitablity analysis.
39
41
42 VPBuilder &Builder;
43
44 /// When we if-convert we need to create edge masks. We have to cache values
45 /// so that we don't end up with exponential recursion/IR. Note that
46 /// if-conversion currently takes place during VPlan-construction, so these
47 /// caches are only used at that stage.
48 using EdgeMaskCacheTy =
51 EdgeMaskCacheTy EdgeMaskCache;
52 BlockMaskCacheTy BlockMaskCache;
53
54 // VPlan-VPlan transformations support: Hold a mapping from ingredients to
55 // their recipe. To save on memory, only do so for selected ingredients,
56 // marked by having a nullptr entry in this map.
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.
73 VFRange &Range, VPlanPtr &Plan);
74
75 /// Check if an induction recipe should be constructed for \p Phi. If so build
76 /// and return it. If not, return null.
77 VPRecipeBase *tryToOptimizeInductionPHI(PHINode *Phi,
79 VPlan &Plan, VFRange &Range);
80
81 /// Optimize the special case where the operand of \p I is a constant integer
82 /// induction variable.
84 tryToOptimizeInductionTruncate(TruncInst *I, ArrayRef<VPValue *> Operands,
85 VFRange &Range, VPlan &Plan);
86
87 /// Handle non-loop phi nodes. Return a VPValue, if all incoming values match
88 /// or a new VPBlendRecipe otherwise. Currently all such phi nodes are turned
89 /// into a sequence of select instructions as the vectorizer currently
90 /// performs full if-conversion.
92 VPlanPtr &Plan);
93
94 /// Handle call instructions. If \p CI can be widened for \p Range.Start,
95 /// return a new VPWidenCallRecipe. Range.End may be decreased to ensure same
96 /// decision from \p Range.Start to \p Range.End.
98 VFRange &Range, VPlanPtr &Plan);
99
100 /// Check if \p I has an opcode that can be widened and return a VPWidenRecipe
101 /// if it can. The function should only be called if the cost-model indicates
102 /// that widening should be performed.
104 VPBasicBlock *VPBB, VPlanPtr &Plan);
105
106 /// Return a VPRecipeOrValueTy with VPRecipeBase * being set. This can be used to force the use as VPRecipeBase* for recipe sub-types that also inherit from VPValue.
107 VPRecipeOrVPValueTy toVPRecipeResult(VPRecipeBase *R) const { return R; }
108
109public:
114 : OrigLoop(OrigLoop), TLI(TLI), Legal(Legal), CM(CM), PSE(PSE),
115 Builder(Builder) {}
116
117 /// Check if an existing VPValue can be used for \p Instr or a recipe can be
118 /// create for \p I withing the given VF \p Range. If an existing VPValue can
119 /// be used or if a recipe can be created, return it. Otherwise return a
120 /// VPRecipeOrVPValueTy with nullptr.
123 VFRange &Range, VPBasicBlock *VPBB,
124 VPlanPtr &Plan);
125
126 /// Set the recipe created for given ingredient. This operation is a no-op for
127 /// ingredients that were not marked using a nullptr entry in the map.
129 if (!Ingredient2Recipe.count(I))
130 return;
131 assert(Ingredient2Recipe[I] == nullptr &&
132 "Recipe already set for ingredient");
133 Ingredient2Recipe[I] = R;
134 }
135
136 /// Create the mask for the vector loop header block.
137 void createHeaderMask(VPlan &Plan);
138
139 /// A helper function that computes the predicate of the block BB, assuming
140 /// that the header block of the loop is set to True or the loop mask when
141 /// tail folding. It returns the *entry* mask for the block BB.
143
144 /// A helper function that computes the predicate of the edge between SRC
145 /// and DST.
147
148 /// Mark given ingredient for recording its recipe once one is created for
149 /// it.
151 assert((!Ingredient2Recipe.count(I) || Ingredient2Recipe[I] == nullptr) &&
152 "Recipe already set for ingredient");
153 Ingredient2Recipe[I] = nullptr;
154 }
155
156 /// Return the recipe created for given ingredient.
158 assert(Ingredient2Recipe.count(I) &&
159 "Recording this ingredients recipe was not requested");
160 assert(Ingredient2Recipe[I] != nullptr &&
161 "Ingredient doesn't have a recipe");
162 return Ingredient2Recipe[I];
163 }
164
165 /// Build a VPReplicationRecipe for \p I. If it is predicated, add the mask as
166 /// last operand. Range.End may be decreased to ensure same recipe behavior
167 /// from \p Range.Start to \p Range.End.
169 VPlan &Plan);
170
171 /// Add the incoming values from the backedge to reduction & first-order
172 /// recurrence cross-iteration phis.
173 void fixHeaderPhis();
174};
175} // end namespace llvm
176
177#endif // LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
assume Assume Builder
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
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:56
This class represents a function call, abstracting a target machine's calling convention.
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
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:47
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
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:1200
Provides information about what library functions are available for the current target.
This class represents a truncation of integer types.
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition: VPlan.h:2253
VPlan-based builder utility analogous to IRBuilder.
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition: VPlan.h:707
Helper class to create VPRecipies from IR instructions.
void createHeaderMask(VPlan &Plan)
Create the mask for the vector loop header block.
VPRecipeBuilder(Loop *OrigLoop, const TargetLibraryInfo *TLI, LoopVectorizationLegality *Legal, LoopVectorizationCostModel &CM, PredicatedScalarEvolution &PSE, VPBuilder &Builder)
VPRecipeOrVPValueTy handleReplication(Instruction *I, VFRange &Range, VPlan &Plan)
Build a VPReplicationRecipe for I.
VPValue * createBlockInMask(BasicBlock *BB, VPlan &Plan)
A helper function that computes the predicate of the block BB, assuming that the header block of the ...
void fixHeaderPhis()
Add the incoming values from the backedge to reduction & first-order recurrence cross-iteration phis.
VPValue * createEdgeMask(BasicBlock *Src, BasicBlock *Dst, VPlan &Plan)
A helper function that computes the predicate of the edge between SRC and DST.
void recordRecipeOf(Instruction *I)
Mark given ingredient for recording its recipe once one is created for it.
void setRecipe(Instruction *I, VPRecipeBase *R)
Set the recipe created for given ingredient.
VPRecipeOrVPValueTy tryToCreateWidenRecipe(Instruction *Instr, ArrayRef< VPValue * > Operands, VFRange &Range, VPBasicBlock *VPBB, VPlanPtr &Plan)
Check if an existing VPValue can be used for Instr or a recipe can be create for I withing the given ...
VPRecipeBase * getRecipe(Instruction *I)
Return the recipe created for given ingredient.
A recipe for widening Call instructions.
Definition: VPlan.h:1216
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition: VPlan.h:1393
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition: VPlan.h:2474
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< VPlan > VPlanPtr
Definition: VPlan.h:131
A range of powers-of-2 vectorization factors with fixed start and adjustable end.
Definition: VPlan.h:84