LLVM 18.0.0git
RISCVTargetTransformInfo.h
Go to the documentation of this file.
1//===- RISCVTargetTransformInfo.h - RISC-V specific TTI ---------*- 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/// \file
9/// This file defines a TargetTransformInfo::Concept conforming object specific
10/// to the RISC-V target machine. It uses the target's detailed information to
11/// provide more precise answers to certain TTI queries, while letting the
12/// target independent and default TTI implementations handle the rest.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_LIB_TARGET_RISCV_RISCVTARGETTRANSFORMINFO_H
17#define LLVM_LIB_TARGET_RISCV_RISCVTARGETTRANSFORMINFO_H
18
19#include "RISCVSubtarget.h"
20#include "RISCVTargetMachine.h"
24#include "llvm/IR/Function.h"
25#include <optional>
26
27namespace llvm {
28
29class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
32
33 friend BaseT;
34
35 const RISCVSubtarget *ST;
36 const RISCVTargetLowering *TLI;
37
38 const RISCVSubtarget *getST() const { return ST; }
39 const RISCVTargetLowering *getTLI() const { return TLI; }
40
41 /// This function returns an estimate for VL to be used in VL based terms
42 /// of the cost model. For fixed length vectors, this is simply the
43 /// vector length. For scalable vectors, we return results consistent
44 /// with getVScaleForTuning under the assumption that clients are also
45 /// using that when comparing costs between scalar and vector representation.
46 /// This does unfortunately mean that we can both undershoot and overshot
47 /// the true cost significantly if getVScaleForTuning is wildly off for the
48 /// actual target hardware.
49 unsigned getEstimatedVLFor(VectorType *Ty);
50
51 /// Return the cost of accessing a constant pool entry of the specified
52 /// type.
53 InstructionCost getConstantPoolLoadCost(Type *Ty,
55public:
56 explicit RISCVTTIImpl(const RISCVTargetMachine *TM, const Function &F)
57 : BaseT(TM, F.getParent()->getDataLayout()), ST(TM->getSubtargetImpl(F)),
58 TLI(ST->getTargetLowering()) {}
59
60 /// Return the cost of materializing an immediate for a value operand of
61 /// a store instruction.
64
67 InstructionCost getIntImmCostInst(unsigned Opcode, unsigned Idx,
68 const APInt &Imm, Type *Ty,
70 Instruction *Inst = nullptr);
72 const APInt &Imm, Type *Ty,
74
76
77 bool shouldExpandReduction(const IntrinsicInst *II) const;
78 bool supportsScalableVectors() const { return ST->hasVInstructions(); }
79 bool enableOrderedReductions() const { return true; }
80 bool enableScalableVectorization() const { return ST->hasVInstructions(); }
82 getPreferredTailFoldingStyle(bool IVUpdateMayOverflow) const {
83 return ST->hasVInstructions() ? TailFoldingStyle::Data
85 }
86 std::optional<unsigned> getMaxVScale() const;
87 std::optional<unsigned> getVScaleForTuning() const;
88
90
91 unsigned getRegUsageForType(Type *Ty);
92
93 unsigned getMaximumVF(unsigned ElemWidth, unsigned Opcode) const;
94
96 // Epilogue vectorization is usually unprofitable - tail folding or
97 // a smaller VF would have been better. This a blunt hammer - we
98 // should re-examine this once vectorization is better tuned.
99 return false;
100 }
101
102 InstructionCost getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
103 Align Alignment, unsigned AddressSpace,
105
107 const Value *Base,
109 Type *AccessTy,
111
115
118
120 return ST->useRVVForFixedLengthVectors() ? 16 : 0;
121 }
122
124 ArrayRef<int> Mask,
126 VectorType *SubTp,
127 ArrayRef<const Value *> Args = std::nullopt);
128
131
133 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
134 Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind,
135 bool UseMaskForCond = false, bool UseMaskForGaps = false);
136
137 InstructionCost getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
138 const Value *Ptr, bool VariableMask,
139 Align Alignment,
141 const Instruction *I);
142
143 InstructionCost getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
146 const Instruction *I = nullptr);
147
149 FastMathFlags FMF,
151
153 std::optional<FastMathFlags> FMF,
155
156 InstructionCost getExtendedReductionCost(unsigned Opcode, bool IsUnsigned,
157 Type *ResTy, VectorType *ValTy,
158 FastMathFlags FMF,
160
162 getMemoryOpCost(unsigned Opcode, Type *Src, MaybeAlign Alignment,
165 const Instruction *I = nullptr);
166
167 InstructionCost getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
168 CmpInst::Predicate VecPred,
170 const Instruction *I = nullptr);
171
172 InstructionCost getCFInstrCost(unsigned Opcode, TTI::TargetCostKind CostKind,
173 const Instruction *I = nullptr);
174
176 InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val,
178 unsigned Index, Value *Op0, Value *Op1);
179
180 InstructionCost getArithmeticInstrCost(
181 unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
182 TTI::OperandValueInfo Op1Info = {TTI::OK_AnyValue, TTI::OP_None},
183 TTI::OperandValueInfo Op2Info = {TTI::OK_AnyValue, TTI::OP_None},
184 ArrayRef<const Value *> Args = ArrayRef<const Value *>(),
185 const Instruction *CxtI = nullptr);
186
188 return TLI->isLegalElementTypeForRVV(TLI->getValueType(DL, Ty));
189 }
190
191 bool isLegalMaskedLoadStore(Type *DataType, Align Alignment) {
192 if (!ST->hasVInstructions())
193 return false;
194
195 EVT DataTypeVT = TLI->getValueType(DL, DataType);
196
197 // Only support fixed vectors if we know the minimum vector size.
198 if (DataTypeVT.isFixedLengthVector() && !ST->useRVVForFixedLengthVectors())
199 return false;
200
201 EVT ElemType = DataTypeVT.getScalarType();
202 if (!ST->enableUnalignedVectorMem() && Alignment < ElemType.getStoreSize())
203 return false;
204
205 return TLI->isLegalElementTypeForRVV(ElemType);
206
207 }
208
209 bool isLegalMaskedLoad(Type *DataType, Align Alignment) {
210 return isLegalMaskedLoadStore(DataType, Alignment);
211 }
212 bool isLegalMaskedStore(Type *DataType, Align Alignment) {
213 return isLegalMaskedLoadStore(DataType, Alignment);
214 }
215
216 bool isLegalMaskedGatherScatter(Type *DataType, Align Alignment) {
217 if (!ST->hasVInstructions())
218 return false;
219
220 EVT DataTypeVT = TLI->getValueType(DL, DataType);
221
222 // Only support fixed vectors if we know the minimum vector size.
223 if (DataTypeVT.isFixedLengthVector() && !ST->useRVVForFixedLengthVectors())
224 return false;
225
226 EVT ElemType = DataTypeVT.getScalarType();
227 if (!ST->enableUnalignedVectorMem() && Alignment < ElemType.getStoreSize())
228 return false;
229
230 return TLI->isLegalElementTypeForRVV(ElemType);
231 }
232
233 bool isLegalMaskedGather(Type *DataType, Align Alignment) {
234 return isLegalMaskedGatherScatter(DataType, Alignment);
235 }
236 bool isLegalMaskedScatter(Type *DataType, Align Alignment) {
237 return isLegalMaskedGatherScatter(DataType, Alignment);
238 }
239
241 // Scalarize masked gather for RV64 if EEW=64 indices aren't supported.
242 return ST->is64Bit() && !ST->hasVInstructionsI64();
243 }
244
246 // Scalarize masked scatter for RV64 if EEW=64 indices aren't supported.
247 return ST->is64Bit() && !ST->hasVInstructionsI64();
248 }
249
251 return TLI->isVScaleKnownToBeAPowerOfTwo();
252 }
253
254 /// \returns How the target needs this vector-predicated operation to be
255 /// transformed.
259 if (!ST->hasVInstructions() ||
260 (PI.getIntrinsicID() == Intrinsic::vp_reduce_mul &&
261 cast<VectorType>(PI.getArgOperand(1)->getType())
262 ->getElementType()
263 ->getIntegerBitWidth() != 1))
266 }
267
269 ElementCount VF) const {
270 if (!VF.isScalable())
271 return true;
272
273 Type *Ty = RdxDesc.getRecurrenceType();
274 if (!TLI->isLegalElementTypeForRVV(TLI->getValueType(DL, Ty)))
275 return false;
276
277 switch (RdxDesc.getRecurrenceKind()) {
278 case RecurKind::Add:
279 case RecurKind::FAdd:
280 case RecurKind::And:
281 case RecurKind::Or:
282 case RecurKind::Xor:
283 case RecurKind::SMin:
284 case RecurKind::SMax:
285 case RecurKind::UMin:
286 case RecurKind::UMax:
287 case RecurKind::FMin:
288 case RecurKind::FMax:
292 return true;
293 default:
294 return false;
295 }
296 }
297
299 // Don't interleave if the loop has been vectorized with scalable vectors.
300 if (VF.isScalable())
301 return 1;
302 // If the loop will not be vectorized, don't interleave the loop.
303 // Let regular unroll to unroll the loop.
304 return VF.isScalar() ? 1 : ST->getMaxInterleaveFactor();
305 }
306
308
310 unsigned getNumberOfRegisters(unsigned ClassID) const {
311 switch (ClassID) {
313 // 31 = 32 GPR - x0 (zero register)
314 // FIXME: Should we exclude fixed registers like SP, TP or GP?
315 return 31;
317 if (ST->hasStdExtF())
318 return 32;
319 return 0;
321 // Although there are 32 vector registers, v0 is special in that it is the
322 // only register that can be used to hold a mask.
323 // FIXME: Should we conservatively return 31 as the number of usable
324 // vector registers?
325 return ST->hasVInstructions() ? 32 : 0;
326 }
327 llvm_unreachable("unknown register class");
328 }
329
330 unsigned getRegisterClassForType(bool Vector, Type *Ty = nullptr) const {
331 if (Vector)
333 if (!Ty)
335
336 Type *ScalarTy = Ty->getScalarType();
337 if ((ScalarTy->isHalfTy() && ST->hasStdExtZfhOrZfhmin()) ||
338 (ScalarTy->isFloatTy() && ST->hasStdExtF()) ||
339 (ScalarTy->isDoubleTy() && ST->hasStdExtD())) {
341 }
342
344 }
345
346 const char *getRegisterClassName(unsigned ClassID) const {
347 switch (ClassID) {
349 return "RISCV::GPRRC";
351 return "RISCV::FPRRC";
353 return "RISCV::VRRC";
354 }
355 llvm_unreachable("unknown register class");
356 }
357
360};
361
362} // end namespace llvm
363
364#endif // LLVM_LIB_TARGET_RISCV_RISCVTARGETTRANSFORMINFO_H
static const Function * getParent(const Value *V)
This file provides a helper that implements much of the TTI interface in terms of the target-independ...
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static cl::opt< TargetTransformInfo::TargetCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(TargetTransformInfo::TCK_RecipThroughput), cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(TargetTransformInfo::TCK_Latency, "latency", "Instruction latency"), clEnumValN(TargetTransformInfo::TCK_CodeSize, "code-size", "Code size"), clEnumValN(TargetTransformInfo::TCK_SizeAndLatency, "size-latency", "Code size and latency")))
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
TargetTransformInfo::VPLegalization VPLegalization
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
const char LLVMTargetMachineRef TM
This pass exposes codegen information to IR-level passes.
Class for arbitrary precision integers.
Definition: APInt.h:76
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Base class which can be used to help build a TTI implementation.
Definition: BasicTTIImpl.h:79
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index, Value *Op0, Value *Op1)
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1357
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:711
constexpr bool isScalar() const
Exactly one element.
Definition: TypeSize.h:302
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:54
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:47
The optimization diagnostic interface.
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index, Value *Op0, Value *Op1)
const char * getRegisterClassName(unsigned ClassID) const
InstructionCost getGatherScatterOpCost(unsigned Opcode, Type *DataTy, const Value *Ptr, bool VariableMask, Align Alignment, TTI::TargetCostKind CostKind, const Instruction *I)
void getPeelingPreferences(Loop *L, ScalarEvolution &SE, TTI::PeelingPreferences &PP)
InstructionCost getShuffleCost(TTI::ShuffleKind Kind, VectorType *Tp, ArrayRef< int > Mask, TTI::TargetCostKind CostKind, int Index, VectorType *SubTp, ArrayRef< const Value * > Args=std::nullopt)
bool isLSRCostLess(const TargetTransformInfo::LSRCost &C1, const TargetTransformInfo::LSRCost &C2)
InstructionCost getArithmeticReductionCost(unsigned Opcode, VectorType *Ty, std::optional< FastMathFlags > FMF, TTI::TargetCostKind CostKind)
bool isLegalMaskedGatherScatter(Type *DataType, Align Alignment)
InstructionCost getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind)
unsigned getMaximumVF(unsigned ElemWidth, unsigned Opcode) const
TailFoldingStyle getPreferredTailFoldingStyle(bool IVUpdateMayOverflow) const
TargetTransformInfo::VPLegalization getVPLegalizationStrategy(const VPIntrinsic &PI) const
bool isLegalMaskedStore(Type *DataType, Align Alignment)
InstructionCost getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src, TTI::CastContextHint CCH, TTI::TargetCostKind CostKind, const Instruction *I=nullptr)
unsigned getNumberOfRegisters(unsigned ClassID) const
bool isElementTypeLegalForScalableVector(Type *Ty) const
InstructionCost getArithmeticInstrCost(unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind, TTI::OperandValueInfo Op1Info={TTI::OK_AnyValue, TTI::OP_None}, TTI::OperandValueInfo Op2Info={TTI::OK_AnyValue, TTI::OP_None}, ArrayRef< const Value * > Args=ArrayRef< const Value * >(), const Instruction *CxtI=nullptr)
unsigned getMaxInterleaveFactor(ElementCount VF)
InstructionCost getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty, FastMathFlags FMF, TTI::TargetCostKind CostKind)
bool isLegalMaskedLoadStore(Type *DataType, Align Alignment)
InstructionCost getIntImmCostInst(unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind, Instruction *Inst=nullptr)
bool enableScalableVectorization() const
bool preferEpilogueVectorization() const
bool forceScalarizeMaskedGather(VectorType *VTy, Align Alignment)
bool isVScaleKnownToBeAPowerOfTwo() const
InstructionCost getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, TTI::TargetCostKind CostKind)
bool isLegalMaskedLoad(Type *DataType, Align Alignment)
bool supportsScalableVectors() const
std::optional< unsigned > getVScaleForTuning() const
InstructionCost getExtendedReductionCost(unsigned Opcode, bool IsUnsigned, Type *ResTy, VectorType *ValTy, FastMathFlags FMF, TTI::TargetCostKind CostKind)
InstructionCost getMaskedMemoryOpCost(unsigned Opcode, Type *Src, Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind)
std::optional< unsigned > getMaxVScale() const
void getUnrollingPreferences(Loop *L, ScalarEvolution &SE, TTI::UnrollingPreferences &UP, OptimizationRemarkEmitter *ORE)
InstructionCost getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy, CmpInst::Predicate VecPred, TTI::TargetCostKind CostKind, const Instruction *I=nullptr)
RISCVTTIImpl(const RISCVTargetMachine *TM, const Function &F)
InstructionCost getPointersChainCost(ArrayRef< const Value * > Ptrs, const Value *Base, const TTI::PointersChainInfo &Info, Type *AccessTy, TTI::TargetCostKind CostKind)
TargetTransformInfo::PopcntSupportKind getPopcntSupport(unsigned TyWidth)
bool shouldExpandReduction(const IntrinsicInst *II) const
InstructionCost getStoreImmCost(Type *VecTy, TTI::OperandValueInfo OpInfo, TTI::TargetCostKind CostKind)
Return the cost of materializing an immediate for a value operand of a store instruction.
InstructionCost getCFInstrCost(unsigned Opcode, TTI::TargetCostKind CostKind, const Instruction *I=nullptr)
unsigned getRegUsageForType(Type *Ty)
bool forceScalarizeMaskedScatter(VectorType *VTy, Align Alignment)
bool isLegalMaskedGather(Type *DataType, Align Alignment)
InstructionCost getMemoryOpCost(unsigned Opcode, Type *Src, MaybeAlign Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind, TTI::OperandValueInfo OpdInfo={TTI::OK_AnyValue, TTI::OP_None}, const Instruction *I=nullptr)
bool enableOrderedReductions() const
bool isLegalToVectorizeReduction(const RecurrenceDescriptor &RdxDesc, ElementCount VF) const
bool isLegalMaskedScatter(Type *DataType, Align Alignment)
unsigned getRegisterClassForType(bool Vector, Type *Ty=nullptr) const
TypeSize getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const
InstructionCost getIntImmCost(const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind)
unsigned getMinVectorRegisterBitWidth() const
InstructionCost getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef< unsigned > Indices, Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind, bool UseMaskForCond=false, bool UseMaskForGaps=false)
bool isLegalElementTypeForRVV(EVT ScalarTy) const
bool isVScaleKnownToBeAPowerOfTwo() const override
Return true only if vscale must be a power of two.
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
Definition: IVDescriptors.h:72
Type * getRecurrenceType() const
Returns the type of the recurrence.
RecurKind getRecurrenceKind() const
The main scalar evolution driver.
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
const DataLayout & getDataLayout() const
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
TargetCostKind
The kind of cost model.
PopcntSupportKind
Flags indicating the kind of support for population count.
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:45
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:154
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:143
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:157
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:348
This is the common base class for vector predication intrinsics.
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
Base class of all SIMD vector types.
Definition: DerivedTypes.h:400
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:166
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
AddressSpace
Definition: NVPTXBaseInfo.h:21
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ FAnyOf
Any_of reduction with select(fcmp(),x,y) where one of (x,y) is loop invariant, and both x and y are i...
@ Or
Bitwise or logical OR of integers.
@ Xor
Bitwise or logical XOR of integers.
@ FMax
FP max implemented in terms of select(cmp()).
@ FMulAdd
Sum of float products with llvm.fmuladd(a * b + sum).
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ And
Bitwise or logical AND of integers.
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ FMin
FP min implemented in terms of select(cmp()).
@ Add
Sum of integers.
@ FAdd
Sum of floats.
@ IAnyOf
Any_of reduction with select(icmp(),x,y) where one of (x,y) is loop invariant, and both x and y are i...
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
@ DataWithoutLaneMask
Same as Data, but avoids using the get.active.lane.mask intrinsic to calculate the mask and instead i...
@ Data
Use predicate only to mask operations on data in the loop.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Extended Value Type.
Definition: ValueTypes.h:34
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:373
bool isFixedLengthVector() const
Definition: ValueTypes.h:170
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:306
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
Describe known properties for a set of pointers.
Parameters that control the generic loop unrolling transformation.