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 TargetTransformInfoImplBase 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"
21#include "llvm/Analysis/TargetTransformInfo.h"
22#include "llvm/CodeGen/BasicTTIImpl.h"
23#include "llvm/IR/Function.h"
24#include <optional>
25
26namespace llvm {
27
28class RISCVTTIImpl final : public BasicTTIImplBase<RISCVTTIImpl> {
29 using BaseT = BasicTTIImplBase<RISCVTTIImpl>;
30 using TTI = TargetTransformInfo;
31
32 friend BaseT;
33
34 const RISCVSubtarget *ST;
35 const RISCVTargetLowering *TLI;
36
37 const RISCVSubtarget *getST() const { return ST; }
38 const RISCVTargetLowering *getTLI() const { return TLI; }
39
40 /// This function returns an estimate for VL to be used in VL based terms
41 /// of the cost model. For fixed length vectors, this is simply the
42 /// vector length. For scalable vectors, we return results consistent
43 /// with getVScaleForTuning under the assumption that clients are also
44 /// using that when comparing costs between scalar and vector representation.
45 /// This does unfortunately mean that we can both undershoot and overshot
46 /// the true cost significantly if getVScaleForTuning is wildly off for the
47 /// actual target hardware.
48 unsigned getEstimatedVLFor(VectorType *Ty) const;
49
50 /// This function calculates the costs for one or more RVV opcodes based
51 /// on the vtype and the cost kind.
52 /// \param Opcodes A list of opcodes of the RVV instruction to evaluate.
53 /// \param VT The MVT of vtype associated with the RVV instructions.
54 /// For widening/narrowing instructions where the result and source types
55 /// differ, it is important to check the spec to determine whether the vtype
56 /// refers to the result or source type.
57 /// \param CostKind The type of cost to compute.
58 InstructionCost getRISCVInstructionCost(ArrayRef<unsigned> OpCodes, MVT VT,
59 TTI::TargetCostKind CostKind) const;
60
61 // Return the cost of generating a PC relative address
62 InstructionCost
63 getStaticDataAddrGenerationCost(const TTI::TargetCostKind CostKind) const;
64
65 /// Return the cost of accessing a constant pool entry of the specified
66 /// type.
67 InstructionCost getConstantPoolLoadCost(Type *Ty,
68 TTI::TargetCostKind CostKind) const;
69
70 /// If this shuffle can be lowered as a masked slide pair (at worst),
71 /// return a cost for it.
72 InstructionCost getSlideCost(FixedVectorType *Tp, ArrayRef<int> Mask,
73 TTI::TargetCostKind CostKind) const;
74
75 /// Return the type used to cost vzip.vv, whose LMUL represents the
76 /// interleaved destination EMUL. Return an invalid MVT if illegal.
77 MVT getZvzipVZIPCostVT(MVT InterleavedVT) const;
78
79 /// Return the type used to cost vunzipe.v/vunzipo.v, whose LMUL represents
80 /// the interleaved source EMUL. Return an invalid MVT if illegal.
81 MVT getZvzipVUNZIPCostVT(MVT InterleavedVT) const;
82
83 /// If a fixed-length shuffle can be lowered to a vzip.vv instruction,
84 /// return its cost.
85 InstructionCost getVZIPCost(TTI::ShuffleKind Kind, VectorType *DstTy,
86 VectorType *SrcTy, ArrayRef<int> Mask,
87 TTI::TargetCostKind CostKind) const;
88
89public:
90 explicit RISCVTTIImpl(const RISCVTargetMachine *TM, const Function &F)
91 : BaseT(TM, F.getDataLayout()), ST(TM->getSubtargetImpl(F)),
92 TLI(ST->getTargetLowering()) {}
93
94 /// Return the cost of materializing an immediate for a value operand of
95 /// a store instruction.
96 InstructionCost getStoreImmCost(Type *VecTy, TTI::OperandValueInfo OpInfo,
97 TTI::TargetCostKind CostKind) const;
98
99 InstructionCost getIntImmCost(const APInt &Imm, Type *Ty,
100 TTI::TargetCostKind CostKind) const override;
101 InstructionCost getIntImmCostInst(unsigned Opcode, unsigned Idx,
102 const APInt &Imm, Type *Ty,
103 TTI::TargetCostKind CostKind,
104 Instruction *Inst = nullptr) const override;
105 bool preferLoopExitWithEqualityComparison(
106 const ICmpInst &Cond, const APInt &PostIncExitValue) const override;
107 InstructionCost
108 getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
109 Type *Ty, TTI::TargetCostKind CostKind) const override;
110
111 /// \name EVL Support for predicated vectorization.
112 /// Whether the target supports the %evl parameter of VP intrinsic efficiently
113 /// in hardware. (see LLVM Language Reference - "Vector Predication
114 /// Intrinsics",
115 /// https://llvm.org/docs/LangRef.html#vector-predication-intrinsics and
116 /// "IR-level VP intrinsics",
117 /// https://llvm.org/docs/Proposals/VectorPredication.html#ir-level-vp-intrinsics).
118 bool hasActiveVectorLength() const override;
119
120 TargetTransformInfo::PopcntSupportKind
121 getPopcntSupport(unsigned TyWidth) const override;
122
123 InstructionCost getPartialReductionCost(
124 unsigned Opcode, Type *InputTypeA, Type *InputTypeB, Type *AccumType,
125 ElementCount VF, TTI::PartialReductionExtendKind OpAExtend,
126 TTI::PartialReductionExtendKind OpBExtend, std::optional<unsigned> BinOp,
127 TTI::TargetCostKind CostKind,
128 std::optional<FastMathFlags> FMF) const override;
129
130 bool shouldExpandReduction(const IntrinsicInst *II) const override;
131 bool supportsScalableVectors() const override {
132 // VLEN=32 support is incomplete.
133 return ST->hasVInstructions() &&
134 (ST->getRealMinVLen() >= RISCV::RVVBitsPerBlock);
135 }
136 bool enableOrderedReductions() const override { return true; }
137 bool enableScalableVectorization() const override {
138 return ST->hasVInstructions();
139 }
140 bool preferTailFoldingOverEpilogue(TailFoldingInfo *TFI) const override {
141 return ST->hasVInstructions();
142 }
143 TailFoldingStyle getPreferredTailFoldingStyle() const override {
144 return ST->hasVInstructions() ? TailFoldingStyle::DataWithEVL
145 : TailFoldingStyle::None;
146 }
147 std::optional<unsigned> getVScaleForTuning() const override;
148
149 TypeSize
150 getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const override;
151
152 unsigned getRegUsageForType(Type *Ty) const override;
153
154 unsigned getMaximumVF(unsigned ElemWidth, unsigned Opcode) const override;
155
156 bool preferAlternateOpcodeVectorization() const override;
157
158 bool preferEpilogueVectorization(ElementCount Iters) const override {
159 // Epilogue vectorization is usually unprofitable - tail folding or
160 // a smaller VF would have been better. This a blunt hammer - we
161 // should re-examine this once vectorization is better tuned.
162 return false;
163 }
164
165 bool shouldConsiderVectorizationRegPressure() const override { return true; }
166
167 InstructionCost
168 getMemIntrinsicInstrCost(const MemIntrinsicCostAttributes &MICA,
169 TTI::TargetCostKind CostKind) const override;
170
171 InstructionCost getMaskedMemoryOpCost(const MemIntrinsicCostAttributes &MICA,
172 TTI::TargetCostKind CostKind) const;
173
174 InstructionCost
175 getPointersChainCost(ArrayRef<const Value *> Ptrs, const Value *Base,
176 const TTI::PointersChainInfo &Info, Type *AccessTy,
177 const TTI::TargetCostKind CostKind) const override;
178
179 void getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
180 TTI::UnrollingPreferences &UP,
181 OptimizationRemarkEmitter *ORE) const override;
182
183 void getPeelingPreferences(Loop *L, ScalarEvolution &SE,
184 TTI::PeelingPreferences &PP) const override;
185
186 bool getTgtMemIntrinsic(IntrinsicInst *Inst,
187 MemIntrinsicInfo &Info) const override;
188
189 unsigned getMinVectorRegisterBitWidth() const override {
190 return ST->useRVVForFixedLengthVectors() ? 16 : 0;
191 }
192
193 InstructionCost
194 getShuffleCost(TTI::ShuffleKind Kind, VectorType *DstTy, VectorType *SrcTy,
195 TTI::TargetCostKind CostKind, ArrayRef<int> Mask, int Index,
196 VectorType *SubTp, ArrayRef<const Value *> Args = {},
197 const Instruction *CtxI = nullptr,
198 TTI::VectorInstrContext VIC =
199 TTI::VectorInstrContext::None) const override;
200
201 InstructionCost
202 getScalarizationOverhead(VectorType *Ty, const APInt &DemandedElts,
203 bool Insert, bool Extract,
204 TTI::TargetCostKind CostKind,
205 bool ForPoisonSrc = true, ArrayRef<Value *> VL = {},
206 TTI::VectorInstrContext VIC =
207 TTI::VectorInstrContext::None) const override;
208
209 InstructionCost
210 getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
211 TTI::TargetCostKind CostKind) const override;
212
213 InstructionCost
214 getAddressComputationCost(Type *PTy, ScalarEvolution *SE, const SCEV *Ptr,
215 TTI::TargetCostKind CostKind) const override;
216
217 InstructionCost getInterleavedMemoryOpCost(
218 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
219 Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind,
220 bool UseMaskForCond = false, bool UseMaskForGaps = false) const override;
221
222 InstructionCost getGatherScatterOpCost(const MemIntrinsicCostAttributes &MICA,
223 TTI::TargetCostKind CostKind) const;
224
225 InstructionCost
226 getExpandCompressMemoryOpCost(const MemIntrinsicCostAttributes &MICA,
227 TTI::TargetCostKind CostKind) const;
228
229 InstructionCost getStridedMemoryOpCost(const MemIntrinsicCostAttributes &MICA,
230 TTI::TargetCostKind CostKind) const;
231
232 InstructionCost
233 getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const override;
234
235 InstructionCost
236 getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
237 TTI::CastContextHint CCH, TTI::TargetCostKind CostKind,
238 const Instruction *I = nullptr) const override;
239
240 InstructionCost
241 getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty, FastMathFlags FMF,
242 TTI::TargetCostKind CostKind) const override;
243
244 std::optional<InstructionCost> getCombinedArithmeticInstructionCost(
245 unsigned ISDOpcode, Type *Ty, TTI::TargetCostKind CostKind,
246 TTI::OperandValueInfo Opd1Info, TTI::OperandValueInfo Opd2Info,
247 ArrayRef<const Value *> Args, const Instruction *CtxI) const;
248
249 InstructionCost
250 getArithmeticReductionCost(unsigned Opcode, VectorType *Ty,
251 std::optional<FastMathFlags> FMF,
252 TTI::TargetCostKind CostKind) const override;
253
254 InstructionCost
255 getExtendedReductionCost(unsigned Opcode, bool IsUnsigned, Type *ResTy,
256 VectorType *ValTy, std::optional<FastMathFlags> FMF,
257 TTI::TargetCostKind CostKind) const override;
258
259 InstructionCost getMemoryOpCost(
260 unsigned Opcode, Type *Src, Align Alignment, unsigned AddressSpace,
261 TTI::TargetCostKind CostKind,
262 TTI::OperandValueInfo OpdInfo = {.Kind: TTI::OK_AnyValue, .Properties: TTI::OP_None},
263 const Instruction *I = nullptr) const override;
264
265 InstructionCost getCmpSelInstrCost(
266 unsigned Opcode, Type *ValTy, Type *CondTy, CmpInst::Predicate VecPred,
267 TTI::TargetCostKind CostKind,
268 TTI::OperandValueInfo Op1Info = {.Kind: TTI::OK_AnyValue, .Properties: TTI::OP_None},
269 TTI::OperandValueInfo Op2Info = {.Kind: TTI::OK_AnyValue, .Properties: TTI::OP_None},
270 const Instruction *I = nullptr) const override;
271
272 InstructionCost getCFInstrCost(unsigned Opcode, TTI::TargetCostKind CostKind,
273 const Instruction *I = nullptr) const override;
274
275 using BaseT::getVectorInstrCost;
276 InstructionCost
277 getVectorInstrCost(unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind,
278 unsigned Index, const Value *Op0, const Value *Op1,
279 TTI::VectorInstrContext VIC =
280 TTI::VectorInstrContext::None) const override;
281
282 InstructionCost
283 getIndexedVectorInstrCostFromEnd(unsigned Opcode, Type *Val,
284 TTI::TargetCostKind CostKind,
285 unsigned Index) const override;
286
287 InstructionCost getArithmeticInstrCost(
288 unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
289 TTI::OperandValueInfo Op1Info = {.Kind: TTI::OK_AnyValue, .Properties: TTI::OP_None},
290 TTI::OperandValueInfo Op2Info = {.Kind: TTI::OK_AnyValue, .Properties: TTI::OP_None},
291 ArrayRef<const Value *> Args = {},
292 const Instruction *CtxI = nullptr) const override;
293
294 bool isElementTypeLegalForScalableVector(Type *Ty) const override {
295 return TLI->isLegalElementTypeForRVV(ScalarTy: TLI->getValueType(DL, Ty));
296 }
297
298 bool isLegalMaskedLoadStore(Type *DataType, Align Alignment) const {
299 if (!ST->hasVInstructions())
300 return false;
301
302 EVT DataTypeVT = TLI->getValueType(DL, Ty: DataType);
303
304 // Only support fixed vectors if we know the minimum vector size.
305 if (DataTypeVT.isFixedLengthVector() && !ST->useRVVForFixedLengthVectors())
306 return false;
307
308 EVT ElemType = DataTypeVT.getScalarType();
309 if (!ST->enableUnalignedVectorMem() && Alignment < ElemType.getStoreSize())
310 return false;
311
312 return TLI->isLegalElementTypeForRVV(ScalarTy: ElemType);
313 }
314
315 bool isLegalMaskedLoad(Type *DataType, Align Alignment,
316 unsigned /*AddressSpace*/,
317 TTI::MaskKind /*MaskKind*/) const override {
318 return isLegalMaskedLoadStore(DataType, Alignment);
319 }
320 bool isLegalMaskedStore(Type *DataType, Align Alignment,
321 unsigned /*AddressSpace*/,
322 TTI::MaskKind /*MaskKind*/) const override {
323 return isLegalMaskedLoadStore(DataType, Alignment);
324 }
325
326 bool isLegalMaskedGatherScatter(Type *DataType, Align Alignment) const {
327 if (!ST->hasVInstructions())
328 return false;
329
330 EVT DataTypeVT = TLI->getValueType(DL, Ty: DataType);
331
332 // Only support fixed vectors if we know the minimum vector size.
333 if (DataTypeVT.isFixedLengthVector() && !ST->useRVVForFixedLengthVectors())
334 return false;
335
336 // We also need to check if the vector of address is valid.
337 EVT PointerTypeVT = EVT(TLI->getPointerTy(DL));
338 if (DataTypeVT.isScalableVector() &&
339 !TLI->isLegalElementTypeForRVV(ScalarTy: PointerTypeVT))
340 return false;
341
342 EVT ElemType = DataTypeVT.getScalarType();
343 if (!ST->enableUnalignedVectorMem() && Alignment < ElemType.getStoreSize())
344 return false;
345
346 return TLI->isLegalElementTypeForRVV(ScalarTy: ElemType);
347 }
348
349 bool isLegalMaskedGather(Type *DataType, Align Alignment) const override {
350 return isLegalMaskedGatherScatter(DataType, Alignment);
351 }
352 bool isLegalMaskedScatter(Type *DataType, Align Alignment) const override {
353 return isLegalMaskedGatherScatter(DataType, Alignment);
354 }
355
356 bool forceScalarizeMaskedGather(VectorType *VTy,
357 Align Alignment) const override {
358 // Scalarize masked gather for RV64 if EEW=64 indices aren't supported.
359 return ST->is64Bit() && !ST->hasVInstructionsI64();
360 }
361
362 bool forceScalarizeMaskedScatter(VectorType *VTy,
363 Align Alignment) const override {
364 // Scalarize masked scatter for RV64 if EEW=64 indices aren't supported.
365 return ST->is64Bit() && !ST->hasVInstructionsI64();
366 }
367
368 bool isLegalStridedLoadStore(Type *DataType, Align Alignment) const override {
369 EVT DataTypeVT = TLI->getValueType(DL, Ty: DataType);
370 return TLI->isLegalStridedLoadStore(DataType: DataTypeVT, Alignment);
371 }
372
373 bool isLegalInterleavedAccessType(VectorType *VTy, unsigned Factor,
374 Align Alignment,
375 unsigned AddrSpace) const override {
376 return TLI->isLegalInterleavedAccessType(VTy, Factor, Alignment, AddrSpace,
377 DL);
378 }
379
380 bool isLegalMaskedExpandLoad(Type *DataType, Align Alignment) const override;
381
382 bool isLegalMaskedCompressStore(Type *DataTy, Align Alignment) const override;
383
384 bool isLegalBroadcastLoad(Type *ElementTy,
385 ElementCount NumElements) const override;
386
387 /// \returns How the target needs this vector-predicated operation to be
388 /// transformed.
389 TargetTransformInfo::VPLegalization
390 getVPLegalizationStrategy(const VPIntrinsic &PI) const override {
391 using VPLegalization = TargetTransformInfo::VPLegalization;
392 static const Intrinsic::ID Supported[] = {
393 Intrinsic::experimental_vp_strided_load,
394 Intrinsic::experimental_vp_strided_store,
395 Intrinsic::experimental_vp_reverse,
396 Intrinsic::experimental_vp_splice,
397 Intrinsic::vp_cttz_elts,
398 Intrinsic::vp_gather,
399 Intrinsic::vp_load,
400 Intrinsic::vp_load_ff,
401 Intrinsic::vp_merge,
402 Intrinsic::vp_reduce_add,
403 Intrinsic::vp_reduce_and,
404 Intrinsic::vp_reduce_fadd,
405 Intrinsic::vp_reduce_fmax,
406 Intrinsic::vp_reduce_fmaximum,
407 Intrinsic::vp_reduce_fmin,
408 Intrinsic::vp_reduce_fminimum,
409 Intrinsic::vp_reduce_fmul,
410 Intrinsic::vp_reduce_mul,
411 Intrinsic::vp_reduce_or,
412 Intrinsic::vp_reduce_smax,
413 Intrinsic::vp_reduce_smin,
414 Intrinsic::vp_reduce_umax,
415 Intrinsic::vp_reduce_umin,
416 Intrinsic::vp_reduce_xor,
417 Intrinsic::vp_scatter,
418 Intrinsic::vp_sdiv,
419 Intrinsic::vp_srem,
420 Intrinsic::vp_store,
421 Intrinsic::vp_udiv,
422 Intrinsic::vp_urem};
423 if (!ST->hasVInstructions() ||
424 (PI.getIntrinsicID() == Intrinsic::vp_reduce_mul &&
425 cast<VectorType>(Val: PI.getArgOperand(i: 1)->getType())
426 ->getElementType()
427 ->getIntegerBitWidth() != 1) ||
428 !is_contained(Range: Supported, Element: PI.getIntrinsicID()))
429 return VPLegalization(VPLegalization::Discard, VPLegalization::Convert);
430 return VPLegalization(VPLegalization::Legal, VPLegalization::Legal);
431 }
432
433 bool isLegalToVectorizeReduction(const RecurrenceDescriptor &RdxDesc,
434 ElementCount VF) const override {
435 if (!VF.isScalable())
436 return true;
437
438 Type *Ty = RdxDesc.getRecurrenceType();
439 if (!TLI->isLegalElementTypeForRVV(ScalarTy: TLI->getValueType(DL, Ty)))
440 return false;
441
442 switch (RdxDesc.getRecurrenceKind()) {
443 case RecurKind::Add:
444 case RecurKind::Sub:
445 case RecurKind::AddChainWithSubs:
446 case RecurKind::And:
447 case RecurKind::Or:
448 case RecurKind::Xor:
449 case RecurKind::SMin:
450 case RecurKind::SMax:
451 case RecurKind::UMin:
452 case RecurKind::UMax:
453 case RecurKind::FMin:
454 case RecurKind::FMax:
455 case RecurKind::FindIV:
456 case RecurKind::FindLast:
457 return true;
458 case RecurKind::AnyOf:
459 case RecurKind::FAdd:
460 case RecurKind::FSub:
461 case RecurKind::FMulAdd:
462 // We can't promote f16/bf16 fadd reductions and scalable vectors can't be
463 // expanded.
464 if (Ty->isBFloatTy() || (Ty->isHalfTy() && !ST->hasVInstructionsF16()))
465 return false;
466 return true;
467 case RecurKind::Mul:
468 case RecurKind::FMul:
469 case RecurKind::FMinNum:
470 case RecurKind::FMaxNum:
471 case RecurKind::FMinimum:
472 case RecurKind::FMaximum:
473 case RecurKind::FMinimumNum:
474 case RecurKind::FMaximumNum:
475 case RecurKind::FAddChainWithSubs:
476 return false;
477 default:
478 llvm_unreachable("Unknown reduction kind.");
479 }
480 }
481
482 unsigned getMaxInterleaveFactor(ElementCount VF,
483 bool HasUnorderedReductions) const override {
484 // Don't interleave if the loop has been vectorized with scalable vectors.
485 if (VF.isScalable())
486 return 1;
487 // If the loop will not be vectorized, don't interleave the loop.
488 // Let regular unroll to unroll the loop.
489 return VF.isScalar() ? 1 : ST->getMaxInterleaveFactor();
490 }
491
492 bool enableInterleavedAccessVectorization() const override { return true; }
493
494 bool enableMaskedInterleavedAccessVectorization() const override {
495 return ST->hasVInstructions();
496 }
497
498 unsigned getMinTripCountTailFoldingThreshold() const override;
499
500 enum RISCVRegisterClass { GPRRC, FPRRC, VRRC };
501 unsigned getNumberOfRegisters(unsigned ClassID) const override {
502 switch (ClassID) {
503 case RISCVRegisterClass::GPRRC:
504 // 31 = 32 GPR - x0 (zero register)
505 // FIXME: Should we exclude fixed registers like SP, TP or GP?
506 return 31;
507 case RISCVRegisterClass::FPRRC:
508 if (ST->hasStdExtF())
509 return 32;
510 return 0;
511 case RISCVRegisterClass::VRRC:
512 // Although there are 32 vector registers, v0 is special in that it is the
513 // only register that can be used to hold a mask.
514 // FIXME: Should we conservatively return 31 as the number of usable
515 // vector registers?
516 return ST->hasVInstructions() ? 32 : 0;
517 }
518 llvm_unreachable("unknown register class");
519 }
520
521 TTI::AddressingModeKind
522 getPreferredAddressingMode(const Loop *L, ScalarEvolution *SE) const override;
523
524 unsigned getRegisterClassForType(bool Vector,
525 Type *Ty = nullptr) const override {
526 if (Vector)
527 return RISCVRegisterClass::VRRC;
528 if (!Ty)
529 return RISCVRegisterClass::GPRRC;
530
531 Type *ScalarTy = Ty->getScalarType();
532 if ((ScalarTy->isHalfTy() && ST->hasStdExtZfhmin()) ||
533 (ScalarTy->isFloatTy() && ST->hasStdExtF()) ||
534 (ScalarTy->isDoubleTy() && ST->hasStdExtD())) {
535 return RISCVRegisterClass::FPRRC;
536 }
537
538 return RISCVRegisterClass::GPRRC;
539 }
540
541 const char *getRegisterClassName(unsigned ClassID) const override {
542 switch (ClassID) {
543 case RISCVRegisterClass::GPRRC:
544 return "RISCV::GPRRC";
545 case RISCVRegisterClass::FPRRC:
546 return "RISCV::FPRRC";
547 case RISCVRegisterClass::VRRC:
548 return "RISCV::VRRC";
549 }
550 llvm_unreachable("unknown register class");
551 }
552
553 InstructionCost
554 getRegisterClassSpillCost(unsigned ClassID,
555 TTI::TargetCostKind CostKind) const override {
556 // Unlike scalar spills, spilling a vector register requires materializing
557 // the address of the spill slot in addition to the store itself, as there
558 // is no vector store with a frame index addressing mode.
559 if (ClassID == RISCVRegisterClass::VRRC)
560 return 2 * TTI::TCC_Basic;
561 return TTI::TCC_Basic;
562 }
563
564 InstructionCost
565 getRegisterClassReloadCost(unsigned ClassID,
566 TTI::TargetCostKind CostKind) const override {
567 if (ClassID == RISCVRegisterClass::VRRC)
568 return 2 * TTI::TCC_Basic;
569 return TTI::TCC_Basic;
570 }
571
572 bool isLSRCostLess(const TargetTransformInfo::LSRCost &C1,
573 const TargetTransformInfo::LSRCost &C2) const override;
574
575 bool shouldConsiderAddressTypePromotion(
576 const Instruction &I,
577 bool &AllowPromotionWithoutCommonHeader) const override;
578 std::optional<unsigned> getMinPageSize() const override { return 4096; }
579 /// Return true if the (vector) instruction I will be lowered to an
580 /// instruction with a scalar splat operand for the given Operand number.
581 bool canSplatOperand(Instruction *I, int Operand) const;
582 /// Return true if a vector instruction will lower to a target instruction
583 /// able to splat the given operand.
584 bool canSplatOperand(unsigned Opcode, int Operand,
585 Intrinsic::ID IntrinsicID) const;
586
587 TargetTransformInfo::VectorInstrContext getBuildVectorContextHint(
588 ArrayRef<int> Mask, ArrayRef<Value *> Scalars,
589 function_ref<
590 bool(SmallVectorImpl<TargetTransformInfo::BuildVectorUseOp> &)>
591 GatherUseOps) const override;
592
593 bool isProfitableToSinkOperands(Instruction *I,
594 SmallVectorImpl<Use *> &Ops) const override;
595
596 TTI::MemCmpExpansionOptions
597 enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const override;
598
599 bool enableSelectOptimize() const override {
600 return ST->enableSelectOptimize();
601 }
602
603 bool shouldTreatInstructionLikeSelect(const Instruction *I) const override;
604
605 bool
606 shouldCopyAttributeWhenOutliningFrom(const Function *Caller,
607 const Attribute &Attr) const override;
608
609 std::optional<Instruction *>
610 instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const override;
611};
612
613} // end namespace llvm
614
615#endif // LLVM_LIB_TARGET_RISCV_RISCVTARGETTRANSFORMINFO_H
616