LLVM  14.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 
26 namespace llvm {
27 
28 class RISCVTTIImpl : public 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 public:
41  explicit RISCVTTIImpl(const RISCVTargetMachine *TM, const Function &F)
42  : BaseT(TM, F.getParent()->getDataLayout()), ST(TM->getSubtargetImpl(F)),
43  TLI(ST->getTargetLowering()) {}
44 
45  InstructionCost getIntImmCost(const APInt &Imm, Type *Ty,
47  InstructionCost getIntImmCostInst(unsigned Opcode, unsigned Idx,
48  const APInt &Imm, Type *Ty,
50  Instruction *Inst = nullptr);
52  const APInt &Imm, Type *Ty,
54 
56 
57  bool shouldExpandReduction(const IntrinsicInst *II) const;
58  bool supportsScalableVectors() const { return ST->hasVInstructions(); }
60 
62  switch (K) {
64  return TypeSize::getFixed(ST->getXLen());
66  return TypeSize::getFixed(
67  ST->hasVInstructions() ? ST->getMinRVVVectorSizeInBits() : 0);
69  return TypeSize::getScalable(
70  ST->hasVInstructions() ? RISCV::RVVBitsPerBlock : 0);
71  }
72 
73  llvm_unreachable("Unsupported register kind");
74  }
75 
76  unsigned getMinVectorRegisterBitWidth() const {
77  return ST->hasVInstructions() ? ST->getMinRVVVectorSizeInBits() : 0;
78  }
79 
80  InstructionCost getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
81  const Value *Ptr, bool VariableMask,
82  Align Alignment,
84  const Instruction *I);
85 
86  bool isLegalMaskedLoadStore(Type *DataType, Align Alignment) {
87  if (!ST->hasVInstructions())
88  return false;
89 
90  // Only support fixed vectors if we know the minimum vector size.
91  if (isa<FixedVectorType>(DataType) && ST->getMinRVVVectorSizeInBits() == 0)
92  return false;
93 
94  // Don't allow elements larger than the ELEN.
95  // FIXME: How to limit for scalable vectors?
96  if (isa<FixedVectorType>(DataType) &&
97  DataType->getScalarSizeInBits() > ST->getMaxELENForFixedLengthVectors())
98  return false;
99 
100  if (Alignment <
102  return false;
103 
104  return TLI->isLegalElementTypeForRVV(DataType->getScalarType());
105  }
106 
107  bool isLegalMaskedLoad(Type *DataType, Align Alignment) {
108  return isLegalMaskedLoadStore(DataType, Alignment);
109  }
110  bool isLegalMaskedStore(Type *DataType, Align Alignment) {
111  return isLegalMaskedLoadStore(DataType, Alignment);
112  }
113 
114  bool isLegalMaskedGatherScatter(Type *DataType, Align Alignment) {
115  if (!ST->hasVInstructions())
116  return false;
117 
118  // Only support fixed vectors if we know the minimum vector size.
119  if (isa<FixedVectorType>(DataType) && ST->getMinRVVVectorSizeInBits() == 0)
120  return false;
121 
122  // Don't allow elements larger than the ELEN.
123  // FIXME: How to limit for scalable vectors?
124  if (isa<FixedVectorType>(DataType) &&
125  DataType->getScalarSizeInBits() > ST->getMaxELENForFixedLengthVectors())
126  return false;
127 
128  if (Alignment <
130  return false;
131 
132  return TLI->isLegalElementTypeForRVV(DataType->getScalarType());
133  }
134 
135  bool isLegalMaskedGather(Type *DataType, Align Alignment) {
136  return isLegalMaskedGatherScatter(DataType, Alignment);
137  }
138  bool isLegalMaskedScatter(Type *DataType, Align Alignment) {
139  return isLegalMaskedGatherScatter(DataType, Alignment);
140  }
141 
142  /// \returns How the target needs this vector-predicated operation to be
143  /// transformed.
148  }
149 
151  ElementCount VF) const {
152  if (!ST->hasVInstructions())
153  return false;
154 
155  if (!VF.isScalable())
156  return true;
157 
158  Type *Ty = RdxDesc.getRecurrenceType();
159  if (!TLI->isLegalElementTypeForRVV(Ty))
160  return false;
161 
162  switch (RdxDesc.getRecurrenceKind()) {
163  case RecurKind::Add:
164  case RecurKind::FAdd:
165  case RecurKind::And:
166  case RecurKind::Or:
167  case RecurKind::Xor:
168  case RecurKind::SMin:
169  case RecurKind::SMax:
170  case RecurKind::UMin:
171  case RecurKind::UMax:
172  case RecurKind::FMin:
173  case RecurKind::FMax:
174  return true;
175  default:
176  return false;
177  }
178  }
179 
180  unsigned getMaxInterleaveFactor(unsigned VF) {
181  return ST->getMaxInterleaveFactor();
182  }
183 };
184 
185 } // end namespace llvm
186 
187 #endif // LLVM_LIB_TARGET_RISCV_RISCVTARGETTRANSFORMINFO_H
llvm::InstructionCost
Definition: InstructionCost.h:29
llvm::RISCVTTIImpl::getMaxVScale
Optional< unsigned > getMaxVScale() const
Definition: RISCVTargetTransformInfo.cpp:126
llvm::BasicTTIImplBase< RISCVTTIImpl >::DL
const DataLayout & DL
Definition: TargetTransformInfoImpl.h:39
llvm::TargetTransformInfo::TargetCostKind
TargetCostKind
The kind of cost model.
Definition: TargetTransformInfo.h:212
llvm::RISCVTTIImpl::shouldExpandReduction
bool shouldExpandReduction(const IntrinsicInst *II) const
Definition: RISCVTargetTransformInfo.cpp:112
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::RISCVTTIImpl::getIntImmCostIntrin
InstructionCost getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind)
Definition: RISCVTargetTransformInfo.cpp:99
llvm::RISCVTTIImpl::getPopcntSupport
TargetTransformInfo::PopcntSupportKind getPopcntSupport(unsigned TyWidth)
Definition: RISCVTargetTransformInfo.cpp:107
llvm::RecurKind::Or
@ Or
Bitwise or logical OR of integers.
llvm::RISCVTTIImpl
Definition: RISCVTargetTransformInfo.h:28
llvm::RISCVTTIImpl::RISCVTTIImpl
RISCVTTIImpl(const RISCVTargetMachine *TM, const Function &F)
Definition: RISCVTargetTransformInfo.h:41
llvm::ElementCount
Definition: TypeSize.h:385
llvm::TypeSize::getFixedSize
ScalarTy getFixedSize() const
Definition: TypeSize.h:425
llvm::Function
Definition: Function.h:62
llvm::TargetTransformInfo::PopcntSupportKind
PopcntSupportKind
Flags indicating the kind of support for population count.
Definition: TargetTransformInfo.h:596
llvm::Type::getScalarType
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:308
llvm::DataLayout::getTypeStoreSize
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
Definition: DataLayout.h:471
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition: TargetTransformInfo.h:169
llvm::TargetTransformInfo::RGK_Scalar
@ RGK_Scalar
Definition: TargetTransformInfo.h:911
llvm::RISCVTTIImpl::isLegalMaskedScatter
bool isLegalMaskedScatter(Type *DataType, Align Alignment)
Definition: RISCVTargetTransformInfo.h:138
llvm::RISCVTargetMachine
Definition: RISCVTargetMachine.h:23
llvm::TargetTransformInfo::VPLegalization
Definition: TargetTransformInfo.h:1398
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::RISCVTTIImpl::isLegalMaskedGather
bool isLegalMaskedGather(Type *DataType, Align Alignment)
Definition: RISCVTargetTransformInfo.h:135
llvm::Optional< unsigned >
llvm::RISCVTTIImpl::getIntImmCost
InstructionCost getIntImmCost(const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind)
Definition: RISCVTargetTransformInfo.cpp:18
llvm::RecurKind::SMin
@ SMin
Signed integer min implemented in terms of select(cmp()).
llvm::RISCVTTIImpl::getMinVectorRegisterBitWidth
unsigned getMinVectorRegisterBitWidth() const
Definition: RISCVTargetTransformInfo.h:76
llvm::LinearPolySize::isScalable
bool isScalable() const
Returns whether the size is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:298
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::TargetTransformInfoImplBase::getDataLayout
const DataLayout & getDataLayout() const
Definition: TargetTransformInfoImpl.h:49
llvm::RISCVTargetLowering::isLegalElementTypeForRVV
bool isLegalElementTypeForRVV(Type *ScalarTy) const
Definition: RISCVISelLowering.cpp:1426
llvm::RecurKind::And
@ And
Bitwise or logical AND of integers.
llvm::RecurrenceDescriptor::getRecurrenceType
Type * getRecurrenceType() const
Returns the type of the recurrence.
Definition: IVDescriptors.h:248
llvm::RISCVTTIImpl::supportsScalableVectors
bool supportsScalableVectors() const
Definition: RISCVTargetTransformInfo.h:58
llvm::Instruction
Definition: Instruction.h:45
llvm::Type::getScalarSizeInBits
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition: Type.cpp:191
llvm::RecurrenceDescriptor::getRecurrenceKind
RecurKind getRecurrenceKind() const
Definition: IVDescriptors.h:198
llvm::TargetTransformInfo::RGK_FixedWidthVector
@ RGK_FixedWidthVector
Definition: TargetTransformInfo.h:911
llvm::RISCVTTIImpl::getMaxInterleaveFactor
unsigned getMaxInterleaveFactor(unsigned VF)
Definition: RISCVTargetTransformInfo.h:180
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
IVDescriptors.h
llvm::LinearPolySize< TypeSize >::getFixed
static TypeSize getFixed(ScalarTy MinVal)
Definition: TypeSize.h:283
llvm::RISCVTTIImpl::isLegalMaskedStore
bool isLegalMaskedStore(Type *DataType, Align Alignment)
Definition: RISCVTargetTransformInfo.h:110
llvm::RecurKind::UMin
@ UMin
Unisgned integer min implemented in terms of select(cmp()).
llvm::RISCVTTIImpl::getGatherScatterOpCost
InstructionCost getGatherScatterOpCost(unsigned Opcode, Type *DataTy, const Value *Ptr, bool VariableMask, Align Alignment, TTI::TargetCostKind CostKind, const Instruction *I)
Definition: RISCVTargetTransformInfo.cpp:140
llvm::RISCV::RVVBitsPerBlock
static constexpr unsigned RVVBitsPerBlock
Definition: RISCVISelLowering.h:637
llvm::RISCVTTIImpl::isLegalToVectorizeReduction
bool isLegalToVectorizeReduction(const RecurrenceDescriptor &RdxDesc, ElementCount VF) const
Definition: RISCVTargetTransformInfo.h:150
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::RecurKind::Add
@ Add
Sum of integers.
llvm::BasicTTIImplBase
Base class which can be used to help build a TTI implementation.
Definition: BasicTTIImpl.h:77
llvm::RISCVTTIImpl::isLegalMaskedLoadStore
bool isLegalMaskedLoadStore(Type *DataType, Align Alignment)
Definition: RISCVTargetTransformInfo.h:86
llvm::RISCVTTIImpl::getVPLegalizationStrategy
TargetTransformInfo::VPLegalization getVPLegalizationStrategy(const VPIntrinsic &PI) const
Definition: RISCVTargetTransformInfo.h:145
llvm::RISCVSubtarget
Definition: RISCVSubtarget.h:35
llvm::RISCVTTIImpl::isLegalMaskedGatherScatter
bool isLegalMaskedGatherScatter(Type *DataType, Align Alignment)
Definition: RISCVTargetTransformInfo.h:114
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
llvm::RecurKind::UMax
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
CostKind
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")))
getParent
static const Function * getParent(const Value *V)
Definition: BasicAliasAnalysis.cpp:870
llvm::RecurKind::FMax
@ FMax
FP max implemented in terms of select(cmp()).
llvm::VPIntrinsic
This is the common base class for vector predication intrinsics.
Definition: IntrinsicInst.h:390
llvm::RISCVTTIImpl::getRegisterBitWidth
TypeSize getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const
Definition: RISCVTargetTransformInfo.h:61
VPLegalization
TargetTransformInfo::VPLegalization VPLegalization
Definition: ExpandVectorPredication.cpp:36
llvm::TypeSize
Definition: TypeSize.h:416
Function.h
llvm::LinearPolySize< TypeSize >::getScalable
static TypeSize getScalable(ScalarTy MinVal)
Definition: TypeSize.h:286
llvm::RISCVTargetLowering
Definition: RISCVISelLowering.h:295
llvm::RISCVTTIImpl::isLegalMaskedLoad
bool isLegalMaskedLoad(Type *DataType, Align Alignment)
Definition: RISCVTargetTransformInfo.h:107
llvm::TargetTransformInfo::RGK_ScalableVector
@ RGK_ScalableVector
Definition: TargetTransformInfo.h:911
llvm::IntrinsicInst
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:45
RISCVSubtarget.h
llvm::RecurrenceDescriptor
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
Definition: IVDescriptors.h:73
llvm::RecurKind::FAdd
@ FAdd
Sum of floats.
llvm::TargetTransformInfo::RegisterKind
RegisterKind
Definition: TargetTransformInfo.h:911
llvm::TargetTransformInfo::VPLegalization::Legal
@ Legal
Definition: TargetTransformInfo.h:1401
TargetTransformInfo.h
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::RecurKind::FMin
@ FMin
FP min implemented in terms of select(cmp()).
BasicTTIImpl.h
llvm::RecurKind::SMax
@ SMax
Signed integer max implemented in terms of select(cmp()).
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::RISCVTTIImpl::getIntImmCostInst
InstructionCost getIntImmCostInst(unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind, Instruction *Inst=nullptr)
Definition: RISCVTargetTransformInfo.cpp:33
llvm::RecurKind::Xor
@ Xor
Bitwise or logical XOR of integers.
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
RISCVTargetMachine.h