1//===-- LoongArchTargetTransformInfo.cpp - LoongArch specific TTI ---------===//
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 implements a TargetTransformInfo analysis pass specific to the
10/// LoongArch 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#include "LoongArchTargetTransformInfo.h"
17#include "llvm/Analysis/TargetTransformInfo.h"
18#include "llvm/CodeGen/CostTable.h"
19#include "llvm/CodeGen/ISDOpcodes.h"
20#include "llvm/CodeGenTypes/MachineValueType.h"
21#include "llvm/IR/InstrTypes.h"
22#include "llvm/IR/Intrinsics.h"
23#include "llvm/Support/InstructionCost.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "loongarchtti"
28
29using CostKindTblEntry = CostTblEntryT<CostKindCosts>;
30
31TypeSize LoongArchTTIImpl::getRegisterBitWidth(
32 TargetTransformInfo::RegisterKind K) const {
33 TypeSize DefSize = TargetTransformInfoImplBase::getRegisterBitWidth(K);
34 switch (K) {
35 case TargetTransformInfo::RGK_Scalar:
36 return TypeSize::getFixed(ExactSize: ST->is64Bit() ? 64 : 32);
37 case TargetTransformInfo::RGK_FixedWidthVector:
38 if (ST->hasExtLASX())
39 return TypeSize::getFixed(ExactSize: 256);
40 if (ST->hasExtLSX())
41 return TypeSize::getFixed(ExactSize: 128);
42 [[fallthrough]];
43 case TargetTransformInfo::RGK_ScalableVector:
44 return DefSize;
45 }
46
47 llvm_unreachable("Unsupported register kind");
48}
49
50unsigned LoongArchTTIImpl::getNumberOfRegisters(unsigned ClassID) const {
51 switch (ClassID) {
52 case LoongArchRegisterClass::GPRRC:
53 // 30 = 32 GPRs - r0 (zero register) - r21 (non-allocatable)
54 return 30;
55 case LoongArchRegisterClass::FPRRC:
56 return ST->hasBasicF() ? 32 : 0;
57 case LoongArchRegisterClass::VRRC:
58 return ST->hasExtLSX() ? 32 : 0;
59 }
60 llvm_unreachable("unknown register class");
61}
62
63unsigned LoongArchTTIImpl::getRegisterClassForType(bool Vector,
64 Type *Ty) const {
65 if (Vector)
66 return LoongArchRegisterClass::VRRC;
67 if (!Ty)
68 return LoongArchRegisterClass::GPRRC;
69
70 Type *ScalarTy = Ty->getScalarType();
71 if ((ScalarTy->isFloatTy() && ST->hasBasicF()) ||
72 (ScalarTy->isDoubleTy() && ST->hasBasicD())) {
73 return LoongArchRegisterClass::FPRRC;
74 }
75
76 return LoongArchRegisterClass::GPRRC;
77}
78
79unsigned
80LoongArchTTIImpl::getMaxInterleaveFactor(ElementCount VF,
81 bool HasUnorderedReductions) const {
82 return ST->getMaxInterleaveFactor();
83}
84
85const char *LoongArchTTIImpl::getRegisterClassName(unsigned ClassID) const {
86 switch (ClassID) {
87 case LoongArchRegisterClass::GPRRC:
88 return "LoongArch::GPRRC";
89 case LoongArchRegisterClass::FPRRC:
90 return "LoongArch::FPRRC";
91 case LoongArchRegisterClass::VRRC:
92 return "LoongArch::VRRC";
93 }
94 llvm_unreachable("unknown register class");
95}
96
97TargetTransformInfo::PopcntSupportKind
98LoongArchTTIImpl::getPopcntSupport(unsigned TyWidth) const {
99 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
100 return ST->hasExtLSX() ? TTI::PSK_FastHardware : TTI::PSK_Software;
101}
102
103unsigned LoongArchTTIImpl::getCacheLineSize() const { return 64; }
104
105bool LoongArchTTIImpl::shouldExpandReduction(const IntrinsicInst *II) const {
106 switch (II->getIntrinsicID()) {
107 default:
108 return true;
109 case Intrinsic::vector_reduce_add:
110 case Intrinsic::vector_reduce_and:
111 case Intrinsic::vector_reduce_or:
112 case Intrinsic::vector_reduce_smax:
113 case Intrinsic::vector_reduce_smin:
114 case Intrinsic::vector_reduce_umax:
115 case Intrinsic::vector_reduce_umin:
116 case Intrinsic::vector_reduce_xor:
117 return false;
118 }
119}
120
121InstructionCost LoongArchTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
122 Align Alignment,
123 unsigned AddressSpace,
124 TTI::TargetCostKind CostKind,
125 TTI::OperandValueInfo OpInfo,
126 const Instruction *I) const {
127 // Vectors narrower than 128 bits are widened, and on LA64 loading or storing
128 // one takes a single instruction: FLD.{S/D} or VLDREPL.{B/H} for loads, and
129 // VSTELM.{B/H/W/D} for stores. The generic model costs them as scalarized.
130 // TODO: LA32 splits 64-bit loads and doesn't use VSTELM.{W/D} for stores.
131 auto *VTy = dyn_cast<FixedVectorType>(Val: Src);
132 if (CostKind == TTI::TCK_RecipThroughput && ST->is64Bit() &&
133 ST->hasExtLSX() && VTy && isPowerOf2_32(Value: VTy->getNumElements())) {
134 unsigned Size = VTy->getPrimitiveSizeInBits().getFixedValue();
135 EVT VT = getTLI()->getValueType(DL, Ty: Src);
136 if (Size >= 16 && Size < 128 &&
137 getTLI()->getTypeAction(Context&: Src->getContext(), VT) ==
138 TargetLowering::TypeWidenVector &&
139 getTLI()->allowsMemoryAccessForAlignment(Context&: Src->getContext(), DL, VT,
140 AddrSpace: AddressSpace, Alignment))
141 return 1;
142 }
143
144 return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, CostKind,
145 OpInfo, I);
146}
147
148LoongArchTTIImpl::TTI::MemCmpExpansionOptions
149LoongArchTTIImpl::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
150 TTI::MemCmpExpansionOptions Options;
151
152 if (!ST->hasUAL())
153 return Options;
154
155 Options.MaxNumLoads = TLI->getMaxExpandSizeMemcmp(OptSize);
156 Options.NumLoadsPerBlock = IsZeroCmp ? Options.MaxNumLoads : 1;
157 Options.AllowOverlappingLoads = true;
158
159 // TODO: Support for vectors.
160 if (ST->is64Bit()) {
161 Options.LoadSizes = {8, 4, 2, 1};
162 Options.AllowedTailExpansions = {3, 5, 6};
163 } else {
164 Options.LoadSizes = {4, 2, 1};
165 Options.AllowedTailExpansions = {3};
166 }
167
168 return Options;
169}
170
171InstructionCost LoongArchTTIImpl::getArithmeticInstrCost(
172 unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
173 TTI::OperandValueInfo Op1Info, TTI::OperandValueInfo Op2Info,
174 ArrayRef<const Value *> Args, const Instruction *CxtI) const {
175
176 std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Ty);
177 int ISD = TLI->InstructionOpcodeToISD(Opcode);
178
179 // Vector multiply by pow2 will be simplified to shifts.
180 // Vector multiply by -pow2 will be simplified to shifts/negates.
181 if (ISD == ISD::MUL && Op2Info.isConstant() &&
182 (Op2Info.isPowerOf2() || Op2Info.isNegatedPowerOf2())) {
183 InstructionCost Cost =
184 getArithmeticInstrCost(Opcode: Instruction::Shl, Ty, CostKind,
185 Op1Info: Op1Info.getNoProps(), Op2Info: Op2Info.getNoProps());
186 if (Op2Info.isNegatedPowerOf2())
187 Cost += getArithmeticInstrCost(Opcode: Instruction::Sub, Ty, CostKind);
188 return Cost;
189 }
190
191 // On LoongArch, vector signed division by constants power-of-two are
192 // normally expanded to the sequence SRA + SRL + ADD + SRA.
193 // The OperandValue properties may not be the same as that of the previous
194 // operation; conservatively assume OP_None.
195 if ((ISD == ISD::SDIV || ISD == ISD::SREM) && Op2Info.isConstant() &&
196 Op2Info.isPowerOf2()) {
197 InstructionCost Cost =
198 2 * getArithmeticInstrCost(Opcode: Instruction::AShr, Ty, CostKind,
199 Op1Info: Op1Info.getNoProps(), Op2Info: Op2Info.getNoProps());
200 Cost += getArithmeticInstrCost(Opcode: Instruction::LShr, Ty, CostKind,
201 Op1Info: Op1Info.getNoProps(), Op2Info: Op2Info.getNoProps());
202 Cost += getArithmeticInstrCost(Opcode: Instruction::Add, Ty, CostKind,
203 Op1Info: Op1Info.getNoProps(), Op2Info: Op2Info.getNoProps());
204
205 if (ISD == ISD::SREM) {
206 // For SREM: (X % C) is the equivalent of (X - (X/C)*C)
207 Cost +=
208 getArithmeticInstrCost(Opcode: Instruction::Mul, Ty, CostKind,
209 Op1Info: Op1Info.getNoProps(), Op2Info: Op2Info.getNoProps());
210 Cost +=
211 getArithmeticInstrCost(Opcode: Instruction::Sub, Ty, CostKind,
212 Op1Info: Op1Info.getNoProps(), Op2Info: Op2Info.getNoProps());
213 }
214
215 return Cost;
216 }
217 // Vector unsigned division/remainder will be simplified to shifts/masks.
218 if ((ISD == ISD::UDIV || ISD == ISD::UREM) && Op2Info.isConstant() &&
219 Op2Info.isPowerOf2()) {
220 if (ISD == ISD::UDIV)
221 return getArithmeticInstrCost(Opcode: Instruction::LShr, Ty, CostKind,
222 Op1Info: Op1Info.getNoProps(), Op2Info: Op2Info.getNoProps());
223 // UREM
224 return getArithmeticInstrCost(Opcode: Instruction::And, Ty, CostKind,
225 Op1Info: Op1Info.getNoProps(), Op2Info: Op2Info.getNoProps());
226 }
227
228 static const CostKindTblEntry LSXCostTable[]{
229 {.ISD: ISD::ADD, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vaddi.b/vadd.b
230 {.ISD: ISD::ADD, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vaddi.h/vadd.h
231 {.ISD: ISD::ADD, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vaddi.w/vadd.w
232 {.ISD: ISD::ADD, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vaddi.d/vadd.d
233
234 {.ISD: ISD::SUB, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsubi.b/vsub.b
235 {.ISD: ISD::SUB, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsubi.h/vsub.h
236 {.ISD: ISD::SUB, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsubi.w/vsub.w
237 {.ISD: ISD::SUB, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsubi.d/vsub.d
238
239 {.ISD: ISD::MUL, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 4}}, // vmul.b
240 {.ISD: ISD::MUL, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 4}}, // vmul.h
241 {.ISD: ISD::MUL, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 4}}, // vmul.w
242 {.ISD: ISD::MUL, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 4}}, // vmul.d
243
244 {.ISD: ISD::SDIV, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 62, .LatencyCost: 29}}, // vdiv.b
245 {.ISD: ISD::SDIV, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 34, .LatencyCost: 17}}, // vdiv.h
246 {.ISD: ISD::SDIV, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 22, .LatencyCost: 11}}, // vdiv.w
247 {.ISD: ISD::SDIV, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 16, .LatencyCost: 8}}, // vdiv.d
248
249 {.ISD: ISD::UDIV, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 66, .LatencyCost: 29}}, // vdiv.bu
250 {.ISD: ISD::UDIV, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 36, .LatencyCost: 17}}, // vdiv.hu
251 {.ISD: ISD::UDIV, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 22, .LatencyCost: 11}}, // vdiv.wu
252 {.ISD: ISD::UDIV, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 16, .LatencyCost: 8}}, // vdiv.du
253
254 {.ISD: ISD::SREM, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 76, .LatencyCost: 38}}, // vmod.b
255 {.ISD: ISD::SREM, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 44, .LatencyCost: 24}}, // vmod.h
256 {.ISD: ISD::SREM, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 27, .LatencyCost: 17}}, // vmod.w
257 {.ISD: ISD::SREM, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 19, .LatencyCost: 14}}, // vmod.d
258
259 {.ISD: ISD::UREM, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 80, .LatencyCost: 38}}, // vmod.bu
260 {.ISD: ISD::UREM, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 44, .LatencyCost: 24}}, // vmod.hu
261 {.ISD: ISD::UREM, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 28, .LatencyCost: 17}}, // vmod.wu
262 {.ISD: ISD::UREM, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 19, .LatencyCost: 14}}, // vmod.du
263
264 {.ISD: ISD::SHL, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vslli.b/vsll.b
265 {.ISD: ISD::SHL, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vslli.h/vsll.h
266 {.ISD: ISD::SHL, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vslli.w/vsll.w
267 {.ISD: ISD::SHL, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vslli.d/vsll.d
268
269 {.ISD: ISD::SRL, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsrli.b/vsrl.b
270 {.ISD: ISD::SRL, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsrli.h/vsrl.h
271 {.ISD: ISD::SRL, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsrli.w/vsrl.w
272 {.ISD: ISD::SRL, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsrli.d/vsrl.d
273
274 {.ISD: ISD::SRA, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsrai.b/vsra.b
275 {.ISD: ISD::SRA, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsrai.h/vsra.h
276 {.ISD: ISD::SRA, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsrai.w/vsra.w
277 {.ISD: ISD::SRA, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vsrai.d/vsra.d
278
279 {.ISD: ISD::AND, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vand.b/vand.v
280 {.ISD: ISD::AND, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vand.v
281 {.ISD: ISD::AND, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vand.v
282 {.ISD: ISD::AND, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vand.v
283
284 {.ISD: ISD::OR, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vori.b/vor.v
285 {.ISD: ISD::OR, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vor.v
286 {.ISD: ISD::OR, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vor.v
287 {.ISD: ISD::OR, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vor.v
288
289 {.ISD: ISD::XOR, .Type: MVT::v16i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vxori.b/vxor.v
290 {.ISD: ISD::XOR, .Type: MVT::v8i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vxor.v
291 {.ISD: ISD::XOR, .Type: MVT::v4i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vxor.v
292 {.ISD: ISD::XOR, .Type: MVT::v2i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // vxor.v
293
294 {.ISD: ISD::FADD, .Type: MVT::v4f32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // vfadd.s
295 {.ISD: ISD::FADD, .Type: MVT::v2f64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // vfadd.d
296
297 {.ISD: ISD::FSUB, .Type: MVT::v4f32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // vfsub.s
298 {.ISD: ISD::FSUB, .Type: MVT::v2f64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // vfsub.d
299
300 {.ISD: ISD::FMUL, .Type: MVT::v4f32, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 5}}, // vfmul.s
301 {.ISD: ISD::FMUL, .Type: MVT::v2f64, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 5}}, // vfmul.d
302
303 {.ISD: ISD::FDIV, .Type: MVT::v4f32, .Cost: {.RecipThroughputCost: 22, .LatencyCost: 11}}, // vfdiv.s
304 {.ISD: ISD::FDIV, .Type: MVT::v2f64, .Cost: {.RecipThroughputCost: 16, .LatencyCost: 8}}, // vfdiv.d
305 };
306
307 if (ST->hasExtLSX()) {
308 if (const auto *Entry = CostTableLookup(Table: LSXCostTable, ISD, Ty: LT.second))
309 if (auto KindCost = Entry->Cost[CostKind])
310 return LT.first * *KindCost;
311 }
312
313 static const CostKindTblEntry LASXUniformConstCostTable[]{
314 {.ISD: ISD::ADD, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvaddi.b/xvadd.b
315 {.ISD: ISD::ADD, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvaddi.h/xvadd.h
316 {.ISD: ISD::ADD, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvaddi.w/xvadd.w
317 {.ISD: ISD::ADD, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvaddi.d/xvadd.d
318
319 {.ISD: ISD::SUB, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsubi.b/xvsub.b
320 {.ISD: ISD::SUB, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsubi.h/xvsub.h
321 {.ISD: ISD::SUB, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsubi.w/xvsub.w
322 {.ISD: ISD::SUB, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsubi.d/xvsub.d
323
324 {.ISD: ISD::MUL, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 4}}, // xvmul.b
325 {.ISD: ISD::MUL, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 4}}, // xvmul.h
326 {.ISD: ISD::MUL, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 4}}, // xvmul.w
327 {.ISD: ISD::MUL, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 4}}, // xvmul.d
328
329 {.ISD: ISD::SDIV, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 62, .LatencyCost: 29}}, // xvdiv.b
330 {.ISD: ISD::SDIV, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 34, .LatencyCost: 17}}, // xvdiv.h
331 {.ISD: ISD::SDIV, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 22, .LatencyCost: 11}}, // xvdiv.w
332 {.ISD: ISD::SDIV, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 16, .LatencyCost: 8}}, // xvdiv.d
333
334 {.ISD: ISD::UDIV, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 66, .LatencyCost: 29}}, // xvdiv.bu
335 {.ISD: ISD::UDIV, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 36, .LatencyCost: 17}}, // xvdiv.hu
336 {.ISD: ISD::UDIV, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 22, .LatencyCost: 11}}, // xvdiv.wu
337 {.ISD: ISD::UDIV, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 16, .LatencyCost: 8}}, // xvdiv.du
338
339 {.ISD: ISD::SREM, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 76, .LatencyCost: 38}}, // xvmod.b
340 {.ISD: ISD::SREM, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 44, .LatencyCost: 24}}, // xvmod.h
341 {.ISD: ISD::SREM, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 28, .LatencyCost: 17}}, // xvmod.w
342 {.ISD: ISD::SREM, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 19, .LatencyCost: 14}}, // xvmod.d
343
344 {.ISD: ISD::UREM, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 76, .LatencyCost: 38}}, // xvmod.bu
345 {.ISD: ISD::UREM, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 43, .LatencyCost: 24}}, // xvmod.hu
346 {.ISD: ISD::UREM, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 28, .LatencyCost: 17}}, // xvmod.wu
347 {.ISD: ISD::UREM, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 19, .LatencyCost: 14}}, // xvmod.du
348
349 {.ISD: ISD::SHL, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvslli.b/xvsll.b
350 {.ISD: ISD::SHL, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvslli.h/xvsll.h
351 {.ISD: ISD::SHL, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvslli.w/xvsll.w
352 {.ISD: ISD::SHL, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvslli.d/xvsll.d
353
354 {.ISD: ISD::SRL, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsrli.b/xvsrl.b
355 {.ISD: ISD::SRL, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsrli.h/xvsrl.h
356 {.ISD: ISD::SRL, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsrli.w/xvsrl.w
357 {.ISD: ISD::SRL, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsrli.d/xvsrl.d
358
359 {.ISD: ISD::SRA, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsrai.b/xvsra.b
360 {.ISD: ISD::SRA, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsrai.h/xvsra.h
361 {.ISD: ISD::SRA, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsrai.w/xvsra.w
362 {.ISD: ISD::SRA, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvsrai.d/xvsra.d
363
364 {.ISD: ISD::AND, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvandi.b/xvand.v
365 {.ISD: ISD::AND, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvand.v
366 {.ISD: ISD::AND, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvand.v
367 {.ISD: ISD::AND, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvand.v
368
369 {.ISD: ISD::OR, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvori.b/xvor.v
370 {.ISD: ISD::OR, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvor.v
371 {.ISD: ISD::OR, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvor.v
372 {.ISD: ISD::OR, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvor.v
373
374 {.ISD: ISD::XOR, .Type: MVT::v32i8, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvxori.b/xvxor.v
375 {.ISD: ISD::XOR, .Type: MVT::v16i16, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvxor.v
376 {.ISD: ISD::XOR, .Type: MVT::v8i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvxor.v
377 {.ISD: ISD::XOR, .Type: MVT::v4i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xvxor.v
378
379 {.ISD: ISD::FADD, .Type: MVT::v8f32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // xvfadd.s
380 {.ISD: ISD::FADD, .Type: MVT::v4f64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // xvfadd.d
381
382 {.ISD: ISD::FSUB, .Type: MVT::v8f32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // xvfsub.s
383 {.ISD: ISD::FSUB, .Type: MVT::v4f64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // xvfsub.d
384
385 {.ISD: ISD::FMUL, .Type: MVT::v8f32, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 5}}, // xvfmul.s
386 {.ISD: ISD::FMUL, .Type: MVT::v4f64, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 5}}, // xvfmul.d
387
388 {.ISD: ISD::FDIV, .Type: MVT::v8f32, .Cost: {.RecipThroughputCost: 22, .LatencyCost: 11}}, // xvfdiv.s
389 {.ISD: ISD::FDIV, .Type: MVT::v4f64, .Cost: {.RecipThroughputCost: 16, .LatencyCost: 8}}, // xvfdiv.d
390 };
391
392 if (ST->hasExtLASX()) {
393 if (const auto *Entry =
394 CostTableLookup(Table: LASXUniformConstCostTable, ISD, Ty: LT.second))
395 if (auto KindCost = Entry->Cost[CostKind])
396 return LT.first * *KindCost;
397 }
398
399 static const CostKindTblEntry LA64CostTable[]{
400 {.ISD: ISD::ADD, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // addi.d/add.d
401 {.ISD: ISD::SUB, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // subi.d/sub.d
402 {.ISD: ISD::MUL, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 4}}, // mul.d
403
404 {.ISD: ISD::SDIV, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 26, .LatencyCost: 18}}, // div.d
405 {.ISD: ISD::UDIV, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 26, .LatencyCost: 18}}, // div.du
406 {.ISD: ISD::SREM, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 26, .LatencyCost: 18}}, // mod.d
407 {.ISD: ISD::UREM, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 26, .LatencyCost: 18}}, // mod.du
408
409 {.ISD: ISD::SHL, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // slli.d/sll.d
410 {.ISD: ISD::SRL, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // srli.d/srl.d
411 {.ISD: ISD::SRA, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // srai.d/sra.d
412
413 {.ISD: ISD::AND, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // andi.d/and.d
414 {.ISD: ISD::OR, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // ori.d/or.d
415 {.ISD: ISD::XOR, .Type: MVT::i64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xori.d/xor.d
416
417 {.ISD: ISD::FADD, .Type: MVT::f64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // fadd.d
418 {.ISD: ISD::FSUB, .Type: MVT::f64, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // fsub.d
419 {.ISD: ISD::FMUL, .Type: MVT::f64, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 5}}, // fmul.d
420 {.ISD: ISD::FDIV, .Type: MVT::f64, .Cost: {.RecipThroughputCost: 9, .LatencyCost: 12}}, // fdiv.d
421 };
422
423 if (ST->is64Bit()) {
424 if (const auto *Entry = CostTableLookup(Table: LA64CostTable, ISD, Ty: LT.second))
425 if (auto KindCost = Entry->Cost[CostKind])
426 return LT.first * *KindCost;
427 }
428
429 static const CostKindTblEntry LA32CostTable[]{
430 {.ISD: ISD::ADD, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // addi.w/add.w
431 {.ISD: ISD::SUB, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // subi.w/sub.w
432 {.ISD: ISD::MUL, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 4}}, // mul.w
433
434 {.ISD: ISD::SDIV, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 24, .LatencyCost: 11}}, // div.w
435 {.ISD: ISD::UDIV, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 24, .LatencyCost: 12}}, // div.wu
436 {.ISD: ISD::SREM, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 24, .LatencyCost: 11}}, // mod.w
437 {.ISD: ISD::UREM, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 24, .LatencyCost: 12}}, // mod.wu
438
439 {.ISD: ISD::SHL, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // slli.w/sll.w
440 {.ISD: ISD::SRL, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // srli.w/srl.w
441 {.ISD: ISD::SRA, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // srai.w/sra.w
442
443 {.ISD: ISD::AND, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // andi.w/and.w
444 {.ISD: ISD::OR, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // ori.w/or.w
445 {.ISD: ISD::XOR, .Type: MVT::i32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 1}}, // xori.w/xor.w
446
447 {.ISD: ISD::FADD, .Type: MVT::f32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // fadd.s
448 {.ISD: ISD::FSUB, .Type: MVT::f32, .Cost: {.RecipThroughputCost: 1, .LatencyCost: 3}}, // fsub.s
449 {.ISD: ISD::FMUL, .Type: MVT::f32, .Cost: {.RecipThroughputCost: 2, .LatencyCost: 5}}, // fmul.s
450 {.ISD: ISD::FDIV, .Type: MVT::f32, .Cost: {.RecipThroughputCost: 8, .LatencyCost: 9}}, // fdiv.s
451 };
452
453 if (const auto *Entry = CostTableLookup(Table: LA32CostTable, ISD, Ty: LT.second))
454 if (auto KindCost = Entry->Cost[CostKind])
455 return LT.first * *KindCost;
456
457 // Fallback to the default implementation.
458 return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info: Op1Info, Opd2Info: Op2Info,
459 Args, CtxI: CxtI);
460}
461
462// Returns true if Ty is a fixed vector type held in a single LSX/LASX register.
463static bool isSingleVRegType(const LoongArchSubtarget *ST, Type *Ty) {
464 auto *VTy = dyn_cast<FixedVectorType>(Val: Ty);
465 if (!VTy)
466 return false;
467 unsigned Bits = VTy->getPrimitiveSizeInBits().getFixedValue();
468 return (Bits == 128 && ST->hasExtLSX()) || (Bits == 256 && ST->hasExtLASX());
469}
470
471InstructionCost LoongArchTTIImpl::getVectorInstrCost(
472 unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
473 const Value *Op0, const Value *Op1, TTI::VectorInstrContext VIC) const {
474 // Element 0 of a floating-point vector is the aliased FPR, so extracting it
475 // is free.
476 if (Opcode == Instruction::ExtractElement && Index == 0 &&
477 Val->getScalarType()->isFloatingPointTy() && isSingleVRegType(ST, Ty: Val))
478 return 0;
479 return BaseT::getVectorInstrCost(Opcode, Val, CostKind, Index, Op0, Op1, VIC);
480}
481
482InstructionCost LoongArchTTIImpl::getShuffleCost(
483 TTI::ShuffleKind Kind, VectorType *DstTy, VectorType *SrcTy,
484 TTI::TargetCostKind CostKind, ArrayRef<int> Mask, int Index,
485 VectorType *SubTp, ArrayRef<const Value *> Args, const Instruction *CxtI,
486 TTI::VectorInstrContext VIC) const {
487 if (isSingleVRegType(ST, Ty: SrcTy) && DstTy == SrcTy) {
488 Kind = improveShuffleKindFromMask(Kind, Mask, SrcTy, Index, SubTy&: SubTp);
489 unsigned EltBits = SrcTy->getScalarSizeInBits();
490 bool Is128 = SrcTy->getPrimitiveSizeInBits().getFixedValue() == 128;
491 switch (Kind) {
492 case TTI::SK_Broadcast:
493 // [x]vreplvei.{b/h/w/d}, xvreplve0.{b/h/w/d}, [x]vreplgr2vr.{b/h/w/d}.
494 return 1;
495 case TTI::SK_Reverse:
496 case TTI::SK_PermuteSingleSrc:
497 // vshuf4i.{w/d} handles any single-source permutation of 32/64-bit
498 // elements within a 128-bit register.
499 if (Is128 && EltBits >= 32)
500 return 1;
501 break;
502 default:
503 break;
504 }
505 }
506 return BaseT::getShuffleCost(Kind, DstTy, SrcTy, CostKind, Mask, Index, SubTp,
507 Args, CtxI: CxtI, VIC);
508}
509