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"
24
25#include <tuple>
26#include <utility>
27
28namespace llvm {
29class APInt;
30class DataLayout;
31class FastMathFlags;
32class FixedVectorType;
33class Instruction;
35class Type;
36class User;
37class Value;
38class VectorType;
39enum class RecurKind;
40} // namespace llvm
41
42namespace llvm::slpvectorizer {
43
44/// Returns the cost of the shuffle instructions with the given \p Kind, vector
45/// type \p Tp and optional \p Mask. Adds SLP-specific cost estimation for
46/// insert subvector pattern.
48getShuffleCost(const TargetTransformInfo &TTI,
49 TargetTransformInfo::ShuffleKind Kind, VectorType *Tp,
51 ArrayRef<int> Mask = {}, int Index = 0,
52 VectorType *SubTp = nullptr, ArrayRef<const Value *> Args = {},
54 TargetTransformInfo::VectorInstrContext::None);
55
56/// Calculate the scalar and the vector costs from vectorizing set of GEPs.
57std::pair<InstructionCost, InstructionCost>
58getGEPCosts(const TargetTransformInfo &TTI, ArrayRef<Value *> Ptrs,
59 Value *BasePtr, unsigned Opcode,
61 VectorType *VecTy);
62
63/// Returns the cost of a BlendedLoadVectorize node loading \p VecTy: two masked
64/// loads (one per candidate base), a xor to negate the false-lane mask and a
65/// select. The blend mask is a separate operand node, so its cost is counted
66/// there, not here.
68getBlendedLoadCost(const TargetTransformInfo &TTI, Type *VecTy, Align Alignment,
69 unsigned AddressSpace,
71
72/// Returns the cost of the cast between the widened strided access type and
73/// the entry vector type.
75getWidenedStridedCastCost(const TargetTransformInfo &TTI, Type *SrcTy,
76 Type *DstTy, const DataLayout &DL,
79
80/// For a non-power-of-2 \p NumElts-wide integer div/rem \p Opcode, checks if
81/// padding to a full register and using the masked div/rem intrinsic is
82/// cheaper than the direct vector op. Returns the cost of the masked
83/// alternative, or an invalid cost if it is not applicable or not cheaper.
85getMaskedDivRemCost(const TargetTransformInfo &TTI, bool ReVec, unsigned Opcode,
86 Type *ScalarTy, unsigned NumElts,
88 FixedVectorType **PaddedTy = nullptr);
89
90/// Returns the cost of the booleanized logical and/or reduction of a vector
91/// of type \p VecTy with the i1 root \p Root, emitted as the wide reduction
92/// plus the result trunc.
94getBoolReduxWideRdxCost(const TargetTransformInfo &TTI, RecurKind RdxKind,
95 FixedVectorType *VecTy, const Value *Root,
96 FastMathFlags FMF,
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 trunc+bitcast+cmp,
101/// estimated in the context of the replaced cast chain \p ChainInsts.
103getBoolReduxBitcastCmpCost(const TargetTransformInfo &TTI, RecurKind RdxKind,
104 FixedVectorType *VecTy, const Value *Root,
105 ArrayRef<Instruction *> ChainInsts,
107
108/// Returns the cost of the boolean bitmask reduction of a vector of boolean
109/// leaves of type \p NarrowScalarTy, emitted as [and] + [lane permutation
110/// \p PermMask] + zero test + bitcast [+ zext] to \p WideTy. \p Root is the
111/// reduction root, used as the context of the emitted instructions.
113getBoolBitmaskCost(const TargetTransformInfo &TTI, bool NeedMask,
114 Type *NarrowScalarTy, Type *WideTy, unsigned VF,
115 ArrayRef<int> PermMask, const Value *Root,
117
118/// Returns the cost of the per-lane operations on the narrowed leaves
119/// \p NarrowedLeafShifts: the shl in the wide vector type if any leaf is
120/// shifted and the and in the narrow vector type if any leaf is masked.
122 const TargetTransformInfo &TTI,
123 const SmallDenseMap<Value *, NarrowedLeafInfo> &NarrowedLeafShifts,
124 VectorType *NarrowVecTy, VectorType *WideVecTy, const Instruction *CtxI,
126
127/// This is similar to TargetTransformInfo::getScalarizationOverhead, but if
128/// ScalarTy is a FixedVectorType, a vector will be inserted or extracted
129/// instead of a scalar.
131getScalarizationOverhead(const TargetTransformInfo &TTI, bool ReVec,
132 Type *ScalarTy, VectorType *Ty,
133 const APInt &DemandedElts, bool Insert, bool Extract,
135 bool ForPoisonSrc = true, ArrayRef<Value *> VL = {},
137 TargetTransformInfo::VectorInstrContext::None);
138
139/// This is similar to TargetTransformInfo::getVectorInstrCost, but if ScalarTy
140/// is a FixedVectorType, a vector will be extracted instead of a scalar.
142getVectorInstrCost(const TargetTransformInfo &TTI, bool ReVec, Type *ScalarTy,
143 unsigned Opcode, Type *Val,
145 unsigned Index, Value *Scalar,
146 ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx,
148
149/// This is similar to TargetTransformInfo::getExtractWithExtendCost, but if Dst
150/// is a FixedVectorType, a vector will be extracted instead of a scalar.
152getExtractWithExtendCost(const TargetTransformInfo &TTI, bool ReVec,
153 unsigned Opcode, Type *Dst, VectorType *VecTy,
154 unsigned Index,
156
157/// Returns the cost of the bitfield packing of \p SrcTy into \p ResultTy,
158/// picking the cheapest shift width. The packing is a trunc, an lshr, a byte
159/// shuffle and a bitcast. \p ZExtSrcWidth is the source width of the lanes if
160/// they are a plain zext (0 otherwise), so compacting them back to it is free.
161/// \p CCH is the context of the pack's source operand.
162InstructionCost getBitPackCost(const TargetTransformInfo &TTI,
163 FixedVectorType *SrcTy, Type *ResultTy,
164 const BitPackInfo &Info, unsigned ZExtSrcWidth,
167 const TargetLibraryInfo *TLI,
168 const Instruction *CtxI, unsigned &ShiftWidth);
169
170/// i1 reductions can be emitted as the plain target reduction or in the
171/// bitcast-based form (bitcast to a scalar integer type plus a compare for
172/// and/or, plus ctpop for add). Returns the cost of the cheaper form and
173/// whether it is the bitcast-based one. Ties keep the historically default
174/// form: plain for and/or, bitcast-based for add.
175std::pair<InstructionCost, bool>
176getI1ReductionCost(RecurKind Kind, const TargetTransformInfo &TTI,
177 FixedVectorType *VectorTy, Type *ScalarTy,
180
181} // namespace llvm::slpvectorizer
182
183#endif // LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPCOSTANALYSIS_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static cl::opt< OutputCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(OutputCostKind::RecipThroughput), cl::values(clEnumValN(OutputCostKind::RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(OutputCostKind::Latency, "latency", "Instruction latency"), clEnumValN(OutputCostKind::CodeSize, "code-size", "Code size"), clEnumValN(OutputCostKind::SizeAndLatency, "size-latency", "Code size and latency"), clEnumValN(OutputCostKind::All, "all", "Print all cost kinds")))
This file defines the DenseMap class.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
This pass exposes codegen information to IR-level passes.
Class for arbitrary precision integers.
Definition APInt.h:78
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.
Provides information about what library functions are available for the current target.
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.
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...
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.
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...
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...
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
TargetTransformInfo TTI
RecurKind
These are the kinds of recurrences that we support.
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:482