LLVM 24.0.0git
SLPCostAnalysis.h
Go to the documentation of this file.
1//===- SLPCostAnalysis.h - SLP Vectorizer free cost helpers ----*- 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// Internal header used by SLPVectorizer.cpp. It declares free cost helpers
10// that do not depend on BoUpSLP or any other SLP-private type. The bulk of
11// the SLP cost model still lives in SLPVectorizer.cpp because it references
12// BoUpSLP internals.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPCOSTANALYSIS_H
17#define LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPCOSTANALYSIS_H
18
19#include "SLPUtils.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/DenseSet.h"
27#include "llvm/IR/Intrinsics.h"
29
30#include <optional>
31#include <tuple>
32#include <utility>
33
34namespace llvm {
35class APInt;
36class BasicBlock;
37class CallInst;
38class DataLayout;
39class FastMathFlags;
40class FixedVectorType;
41class Function;
42class Instruction;
44class Type;
45class User;
46class Value;
47class VectorType;
48enum class RecurKind;
49} // namespace llvm
50
51namespace llvm::slpvectorizer {
52
53/// Returns the cost of the shuffle instructions with the given \p Kind, vector
54/// type \p Tp and optional \p Mask. Adds SLP-specific cost estimation for
55/// insert subvector pattern.
57getShuffleCost(const TargetTransformInfo &TTI,
58 TargetTransformInfo::ShuffleKind Kind, VectorType *Tp,
60 ArrayRef<int> Mask = {}, int Index = 0,
61 VectorType *SubTp = nullptr, ArrayRef<const Value *> Args = {},
63 TargetTransformInfo::VectorInstrContext::None);
64
65/// Calculate the scalar and the vector costs from vectorizing set of GEPs.
66std::pair<InstructionCost, InstructionCost>
67getGEPCosts(const TargetTransformInfo &TTI, ArrayRef<Value *> Ptrs,
68 Value *BasePtr, unsigned Opcode,
69 const TargetTransformInfo::TargetCostKind CostKind, Type *ScalarTy,
70 VectorType *VecTy);
71
72/// Returns the cost of a BlendedLoadVectorize node loading \p VecTy: two masked
73/// loads (one per candidate base), a xor to negate the false-lane mask and a
74/// select. The blend mask is a separate operand node, so its cost is counted
75/// there, not here.
77getBlendedLoadCost(const TargetTransformInfo &TTI, Type *VecTy, Align Alignment,
78 unsigned AddressSpace,
80
81/// Returns the cost of the cast between the widened strided access type and
82/// the entry vector type.
84getWidenedStridedCastCost(const TargetTransformInfo &TTI, Type *SrcTy,
85 Type *DstTy, const DataLayout &DL,
88
89/// For a non-power-of-2 \p NumElts-wide integer div/rem \p Opcode, checks if
90/// padding to a full register and using the masked div/rem intrinsic is
91/// cheaper than the direct vector op. Returns the cost of the masked
92/// alternative, or an invalid cost if it is not applicable or not cheaper.
94getMaskedDivRemCost(const TargetTransformInfo &TTI, bool ReVec, unsigned Opcode,
95 Type *ScalarTy, unsigned NumElts,
97 FixedVectorType **PaddedTy = nullptr);
98
99/// Returns the cost of the booleanized logical and/or reduction of a vector
100/// of type \p VecTy with the i1 root \p Root, emitted as the wide reduction
101/// plus the result trunc.
103getBoolReduxWideRdxCost(const TargetTransformInfo &TTI, RecurKind RdxKind,
104 FixedVectorType *VecTy, const Value *Root,
105 FastMathFlags FMF,
107
108/// Returns the cost of the booleanized logical and/or reduction of a vector
109/// of type \p VecTy with the i1 root \p Root, emitted as trunc+bitcast+cmp,
110/// estimated in the context of the replaced cast chain \p ChainInsts.
112getBoolReduxBitcastCmpCost(const TargetTransformInfo &TTI, RecurKind RdxKind,
113 FixedVectorType *VecTy, const Value *Root,
114 ArrayRef<Instruction *> ChainInsts,
116
117/// Returns the cost of the boolean bitmask reduction of a vector of boolean
118/// leaves of type \p NarrowScalarTy, emitted as [and] + [lane permutation
119/// \p PermMask] + zero test + bitcast [+ zext] to \p WideTy. \p Root is the
120/// reduction root, used as the context of the emitted instructions.
122getBoolBitmaskCost(const TargetTransformInfo &TTI, bool NeedMask,
123 Type *NarrowScalarTy, Type *WideTy, unsigned VF,
124 ArrayRef<int> PermMask, const Value *Root,
126
127/// Returns the cost of the per-lane operations on the narrowed leaves
128/// \p NarrowedLeafShifts: the shl in the wide vector type if any leaf is
129/// shifted and the and in the narrow vector type if any leaf is masked.
131 const TargetTransformInfo &TTI,
132 const SmallDenseMap<Value *, NarrowedLeafInfo> &NarrowedLeafShifts,
133 VectorType *NarrowVecTy, VectorType *WideVecTy, const Instruction *CtxI,
135
136/// This is similar to TargetTransformInfo::getScalarizationOverhead, but if
137/// ScalarTy is a FixedVectorType, a vector will be inserted or extracted
138/// instead of a scalar.
140getScalarizationOverhead(const TargetTransformInfo &TTI, bool ReVec,
141 Type *ScalarTy, VectorType *Ty,
142 const APInt &DemandedElts, bool Insert, bool Extract,
144 bool ForPoisonSrc = true, ArrayRef<Value *> VL = {},
146 TargetTransformInfo::VectorInstrContext::None);
147
148/// This is similar to TargetTransformInfo::getVectorInstrCost, but if ScalarTy
149/// is a FixedVectorType, a vector will be extracted instead of a scalar.
151getVectorInstrCost(const TargetTransformInfo &TTI, bool ReVec, Type *ScalarTy,
152 unsigned Opcode, Type *Val,
154 unsigned Index, Value *Scalar,
155 ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx,
157
158/// This is similar to TargetTransformInfo::getExtractWithExtendCost, but if Dst
159/// is a FixedVectorType, a vector will be extracted instead of a scalar.
161getExtractWithExtendCost(const TargetTransformInfo &TTI, bool ReVec,
162 unsigned Opcode, Type *Dst, VectorType *VecTy,
163 unsigned Index,
165
166/// Returns the cost of the bitfield packing of \p SrcTy into \p ResultTy,
167/// picking the cheapest shift width. The packing is a trunc, an lshr, a byte
168/// shuffle and a bitcast. \p ZExtSrcWidth is the source width of the lanes if
169/// they are a plain zext (0 otherwise), so compacting them back to it is free.
170/// \p CCH is the context of the pack's source operand.
171InstructionCost getBitPackCost(const TargetTransformInfo &TTI,
172 FixedVectorType *SrcTy, Type *ResultTy,
173 const BitPackInfo &Info, unsigned ZExtSrcWidth,
176 const TargetLibraryInfo *TLI,
177 const Instruction *CtxI, unsigned &ShiftWidth);
178
179/// i1 reductions can be emitted as the plain target reduction or in the
180/// bitcast-based form (bitcast to a scalar integer type plus a compare for
181/// and/or, plus ctpop for add). Returns the cost of the cheaper form and
182/// whether it is the bitcast-based one. Ties keep the historically default
183/// form: plain for and/or, bitcast-based for add.
184std::pair<InstructionCost, bool>
185getI1ReductionCost(RecurKind Kind, const TargetTransformInfo &TTI,
186 FixedVectorType *VectorTy, Type *ScalarTy,
189
190/// Builds the arguments types vector for the given call instruction with the
191/// given \p ID for the specified vector factor.
193 const Intrinsic::ID ID,
194 const unsigned VF, unsigned MinBW,
195 const TargetTransformInfo &TTI);
196
197/// Calculates the costs of vectorized intrinsic (if possible) and vectorized
198/// function (if possible) calls. Returns invalid cost for the corresponding
199/// calls, if they cannot be vectorized/will be scalarized.
200std::pair<InstructionCost, InstructionCost>
201getVectorCallCosts(CallInst *CI, Type *VecTy, const TargetTransformInfo &TTI,
202 const TargetLibraryInfo &TLI, ArrayRef<Type *> ArgTys,
204
205/// Memoizes opcode / intrinsic / callee kinds already ruled out as seeds.
209 // Keyed on the callee alone: a Function's signature is fixed, so its type
210 // never varies between calls.
212};
213
214/// Returns true if \p I is an expensive scalar op whose vector form is cheaper
215/// per lane (e.g. fdiv, frem, fsqrt). If \p MaxVecRegSize is non-zero, the
216/// widest vector fitting into it is checked too.
217bool isPoorThroughputOp(bool ReVec, Instruction *I,
219 const TargetLibraryInfo &TLI,
222 unsigned MaxVecRegSize = 0);
223
224/// \returns true if \p Op is a contractable single-use fmul, which the backend
225/// fuses with its fadd/fsub user \p I: \p Op is in the block of \p I or the
226/// target sinks it there.
228 const TargetTransformInfo &TTI);
229
230/// \returns true if more values with the scalar type \p Ty than registers are
231/// live at some point of the block \p BB. The blocks larger than the scheduling
232/// budget \p ScheduleBudget are skipped to save compile time.
233bool hasHighRegisterPressure(unsigned ScheduleBudget, const BasicBlock &BB,
234 Type *Ty, const TargetTransformInfo &TTI);
235
236/// Selects the gathers of \p GatheredScalars, which fully reverse the bytes of
237/// a scalar and are emitted as a byte swap of the scalar, if it is not more
238/// expensive than the permutation of the bytes. The permutation of the scalar
239/// extracted from a vector folds the extraction, the byte swap does not, so the
240/// byte swaps of the scalars extracted from the same vector are estimated
241/// together with all these extractions. \p GetVectorizedSource returns the key
242/// and the type of the vector the vectorized scalar is extracted from, and its
243/// lane. \returns the mask of the selected gathers.
245 ArrayRef<SmallVector<Value *>> GatheredScalars, const DataLayout &DL,
246 const TargetTransformInfo &TTI, bool ReVec,
248 function_ref<std::optional<
249 std::tuple<const void *, FixedVectorType *, unsigned>>(Value *)>
250 GetVectorizedSource);
251
252} // namespace llvm::slpvectorizer
253
254#endif // LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPCOSTANALYSIS_H
const TargetLowering & TLI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
#define I(x, y, z)
Definition MD5.cpp:57
This file implements the SmallBitVector class.
This file defines the SmallVector class.
This pass exposes codegen information to IR-level passes.
Class for arbitrary precision integers.
Definition APInt.h:78
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
This class represents a function call, abstracting a target machine's calling convention.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
Class to represent fixed width SIMD vectors.
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition DenseSet.h:295
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.
TargetCostKind
The kind of cost model.
llvm::VectorInstrContext VectorInstrContext
ShuffleKind
The various kinds of shuffle patterns for vector queries.
CastContextHint
Represents a hint about the context in which a cast is used.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
Base class of all SIMD vector types.
An efficient, type-erasing, non-owning reference to a callable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
A private "module" namespace for types and utilities used by this pass.
std::pair< InstructionCost, bool > getI1ReductionCost(RecurKind Kind, const TargetTransformInfo &TTI, FixedVectorType *VectorTy, Type *ScalarTy, TTI::CastContextHint Ctx, TTI::TargetCostKind CostKind)
i1 reductions can be emitted as the plain target reduction or in the bitcast-based form (bitcast to a...
std::pair< InstructionCost, InstructionCost > getVectorCallCosts(CallInst *CI, Type *VecTy, const TargetTransformInfo &TTI, const TargetLibraryInfo &TLI, ArrayRef< Type * > ArgTys, const TTI::TargetCostKind CostKind)
Calculates the costs of vectorized intrinsic (if possible) and vectorized function (if possible) call...
InstructionCost getShuffleCost(const TargetTransformInfo &TTI, TTI::ShuffleKind Kind, VectorType *Tp, const TTI::TargetCostKind CostKind, ArrayRef< int > Mask, int Index, VectorType *SubTp, ArrayRef< const Value * > Args, TTI::VectorInstrContext VIC)
Returns the cost of the shuffle instructions with the given Kind, vector type Tp and optional Mask.
InstructionCost getNarrowedLeafOpsCost(const TargetTransformInfo &TTI, const SmallDenseMap< Value *, NarrowedLeafInfo > &NarrowedLeafShifts, VectorType *NarrowVecTy, VectorType *WideVecTy, const Instruction *CtxI, const TTI::TargetCostKind CostKind)
Returns the cost of the per-lane operations on the narrowed leaves NarrowedLeafShifts: the shl in the...
InstructionCost getBoolReduxBitcastCmpCost(const TargetTransformInfo &TTI, RecurKind RdxKind, FixedVectorType *VecTy, const Value *Root, ArrayRef< Instruction * > ChainInsts, const TTI::TargetCostKind CostKind)
Returns the cost of the booleanized logical and/or reduction of a vector of type VecTy with the i1 ro...
std::pair< InstructionCost, InstructionCost > getGEPCosts(const TargetTransformInfo &TTI, ArrayRef< Value * > Ptrs, Value *BasePtr, unsigned Opcode, const TTI::TargetCostKind CostKind, Type *ScalarTy, VectorType *VecTy)
Calculate the scalar and the vector costs from vectorizing set of GEPs.
SmallVector< Type * > buildIntrinsicArgTypes(const CallInst *CI, const Intrinsic::ID ID, const unsigned VF, unsigned MinBW, const TargetTransformInfo &TTI)
Builds the arguments types vector for the given call instruction with the given ID for the specified ...
bool isPoorThroughputOp(bool ReVec, Instruction *I, const TargetTransformInfo &TTI, const TargetLibraryInfo &TLI, PoorThroughputOpCache &Cache, const TTI::TargetCostKind CostKind, unsigned MaxVecRegSize)
Returns true if I is an expensive scalar op whose vector form is cheaper per lane (e....
InstructionCost getBlendedLoadCost(const TargetTransformInfo &TTI, Type *VecTy, Align Alignment, unsigned AddressSpace, const TTI::TargetCostKind CostKind)
Returns the cost of a BlendedLoadVectorize node loading VecTy: two masked loads (one per candidate ba...
InstructionCost getBoolBitmaskCost(const TargetTransformInfo &TTI, bool NeedMask, Type *NarrowScalarTy, Type *WideTy, unsigned VF, ArrayRef< int > PermMask, const Value *Root, const TTI::TargetCostKind CostKind)
Returns the cost of the boolean bitmask reduction of a vector of boolean leaves of type NarrowScalarT...
bool isFusableFMulOperand(Value *Op, Instruction *I, const TargetTransformInfo &TTI)
InstructionCost getBoolReduxWideRdxCost(const TargetTransformInfo &TTI, RecurKind RdxKind, FixedVectorType *VecTy, const Value *Root, FastMathFlags FMF, const TTI::TargetCostKind CostKind)
Returns the cost of the booleanized logical and/or reduction of a vector of type VecTy with the i1 ro...
InstructionCost getWidenedStridedCastCost(const TargetTransformInfo &TTI, Type *SrcTy, Type *DstTy, const DataLayout &DL, TTI::CastContextHint CCH, TTI::TargetCostKind CostKind)
Returns the cost of the cast between the widened strided access type and the entry vector type.
InstructionCost getScalarizationOverhead(const TargetTransformInfo &TTI, bool ReVec, Type *ScalarTy, VectorType *Ty, const APInt &DemandedElts, bool Insert, bool Extract, const TTI::TargetCostKind CostKind, bool ForPoisonSrc, ArrayRef< Value * > VL, TTI::VectorInstrContext VIC)
This is similar to TargetTransformInfo::getScalarizationOverhead, but if ScalarTy is a FixedVectorTyp...
bool hasHighRegisterPressure(unsigned ScheduleBudget, const BasicBlock &BB, Type *Ty, const TargetTransformInfo &TTI)
SmallBitVector selectBSwapGathers(ArrayRef< SmallVector< Value * > > GatheredScalars, const DataLayout &DL, const TargetTransformInfo &TTI, bool ReVec, TargetTransformInfo::TargetCostKind CostKind, function_ref< std::optional< std::tuple< const void *, FixedVectorType *, unsigned > >(Value *)> GetVectorizedSource)
Selects the gathers of GatheredScalars, which fully reverse the bytes of a scalar and are emitted as ...
InstructionCost getExtractWithExtendCost(const TargetTransformInfo &TTI, bool ReVec, unsigned Opcode, Type *Dst, VectorType *VecTy, unsigned Index, const TTI::TargetCostKind CostKind)
This is similar to TargetTransformInfo::getExtractWithExtendCost, but if Dst is a FixedVectorType,...
InstructionCost getVectorInstrCost(const TargetTransformInfo &TTI, bool ReVec, Type *ScalarTy, unsigned Opcode, Type *Val, const TTI::TargetCostKind CostKind, unsigned Index, Value *Scalar, ArrayRef< std::tuple< Value *, User *, int > > ScalarUserAndIdx, TTI::VectorInstrContext VIC)
This is similar to TargetTransformInfo::getVectorInstrCost, but if ScalarTy is a FixedVectorType,...
InstructionCost getMaskedDivRemCost(const TargetTransformInfo &TTI, bool ReVec, unsigned Opcode, Type *ScalarTy, unsigned NumElts, const TTI::TargetCostKind CostKind, FixedVectorType **PaddedTy)
For a non-power-of-2 NumElts-wide integer div/rem Opcode, checks if padding to a full register and us...
InstructionCost getBitPackCost(const TargetTransformInfo &TTI, FixedVectorType *SrcTy, Type *ResultTy, const BitPackInfo &Info, unsigned ZExtSrcWidth, TTI::CastContextHint CCH, TTI::TargetCostKind CostKind, const TargetLibraryInfo *TLI, const Instruction *CtxI, unsigned &ShiftWidth)
Returns the cost of the bitfield packing of SrcTy into ResultTy, picking the cheapest shift width.
This is an optimization pass for GlobalISel generic memory operations.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
TargetTransformInfo TTI
RecurKind
These are the kinds of recurrences that we support.
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
Description of a bitfield packing of vector lanes into a scalar value: every lane contributes a disjo...
Definition SLPUtils.h:499
Memoizes opcode / intrinsic / callee kinds already ruled out as seeds.
SmallDenseSet< const Function * > CheapCallees
SmallDenseSet< std::pair< Intrinsic::ID, unsigned > > CheapIntrinsics
SmallDenseSet< std::pair< unsigned, unsigned > > CheapOpcodes