LLVM 24.0.0git
LoongArchTargetTransformInfo.cpp
Go to the documentation of this file.
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
21#include "llvm/IR/InstrTypes.h"
22#include "llvm/IR/Intrinsics.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "loongarchtti"
28
30
34 switch (K) {
36 return TypeSize::getFixed(ST->is64Bit() ? 64 : 32);
38 if (ST->hasExtLASX())
39 return TypeSize::getFixed(256);
40 if (ST->hasExtLSX())
41 return TypeSize::getFixed(128);
42 [[fallthrough]];
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
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
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
97unsigned LoongArchTTIImpl::getCacheLineSize() const { return 64; }
98
100 switch (II->getIntrinsicID()) {
101 default:
102 return true;
103 case Intrinsic::vector_reduce_add:
104 case Intrinsic::vector_reduce_and:
105 case Intrinsic::vector_reduce_or:
106 case Intrinsic::vector_reduce_smax:
107 case Intrinsic::vector_reduce_smin:
108 case Intrinsic::vector_reduce_umax:
109 case Intrinsic::vector_reduce_umin:
110 case Intrinsic::vector_reduce_xor:
111 return false;
112 }
113}
114
116 Align Alignment,
117 unsigned AddressSpace,
118 TTI::TargetCostKind CostKind,
120 const Instruction *I) const {
121 // Vectors narrower than 128 bits are widened, and on LA64 loading or storing
122 // one takes a single instruction: FLD.{S/D} or VLDREPL.{B/H} for loads, and
123 // VSTELM.{B/H/W/D} for stores. The generic model costs them as scalarized.
124 // TODO: LA32 splits 64-bit loads and doesn't use VSTELM.{W/D} for stores.
125 auto *VTy = dyn_cast<FixedVectorType>(Src);
126 if (CostKind == TTI::TCK_RecipThroughput && ST->is64Bit() &&
127 ST->hasExtLSX() && VTy && isPowerOf2_32(VTy->getNumElements())) {
128 unsigned Size = VTy->getPrimitiveSizeInBits().getFixedValue();
129 EVT VT = getTLI()->getValueType(DL, Src);
130 if (Size >= 16 && Size < 128 &&
131 getTLI()->getTypeAction(Src->getContext(), VT) ==
133 getTLI()->allowsMemoryAccessForAlignment(Src->getContext(), DL, VT,
134 AddressSpace, Alignment))
135 return 1;
136 }
137
138 return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, CostKind,
139 OpInfo, I);
140}
141
143LoongArchTTIImpl::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
145
146 if (!ST->hasUAL())
147 return Options;
148
149 Options.MaxNumLoads = TLI->getMaxExpandSizeMemcmp(OptSize);
150 Options.NumLoadsPerBlock = IsZeroCmp ? Options.MaxNumLoads : 1;
151 Options.AllowOverlappingLoads = true;
152
153 // TODO: Support for vectors.
154 if (ST->is64Bit()) {
155 Options.LoadSizes = {8, 4, 2, 1};
156 Options.AllowedTailExpansions = {3, 5, 6};
157 } else {
158 Options.LoadSizes = {4, 2, 1};
159 Options.AllowedTailExpansions = {3};
160 }
161
162 return Options;
163}
164
166 unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
168 ArrayRef<const Value *> Args, const Instruction *CxtI,
169 const TargetLibraryInfo *TLibInfo, ArrayRef<const Value *> Scalars) const {
170
171 std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Ty);
172 int ISD = TLI->InstructionOpcodeToISD(Opcode);
173
174 // Vector multiply by pow2 will be simplified to shifts.
175 // Vector multiply by -pow2 will be simplified to shifts/negates.
176 if (ISD == ISD::MUL && Op2Info.isConstant() &&
177 (Op2Info.isPowerOf2() || Op2Info.isNegatedPowerOf2())) {
179 getArithmeticInstrCost(Instruction::Shl, Ty, CostKind,
180 Op1Info.getNoProps(), Op2Info.getNoProps());
181 if (Op2Info.isNegatedPowerOf2())
182 Cost += getArithmeticInstrCost(Instruction::Sub, Ty, CostKind);
183 return Cost;
184 }
185
186 // On LoongArch, vector signed division by constants power-of-two are
187 // normally expanded to the sequence SRA + SRL + ADD + SRA.
188 // The OperandValue properties may not be the same as that of the previous
189 // operation; conservatively assume OP_None.
190 if ((ISD == ISD::SDIV || ISD == ISD::SREM) && Op2Info.isConstant() &&
191 Op2Info.isPowerOf2()) {
193 2 * getArithmeticInstrCost(Instruction::AShr, Ty, CostKind,
194 Op1Info.getNoProps(), Op2Info.getNoProps());
195 Cost += getArithmeticInstrCost(Instruction::LShr, Ty, CostKind,
196 Op1Info.getNoProps(), Op2Info.getNoProps());
197 Cost += getArithmeticInstrCost(Instruction::Add, Ty, CostKind,
198 Op1Info.getNoProps(), Op2Info.getNoProps());
199
200 if (ISD == ISD::SREM) {
201 // For SREM: (X % C) is the equivalent of (X - (X/C)*C)
202 Cost +=
203 getArithmeticInstrCost(Instruction::Mul, Ty, CostKind,
204 Op1Info.getNoProps(), Op2Info.getNoProps());
205 Cost +=
206 getArithmeticInstrCost(Instruction::Sub, Ty, CostKind,
207 Op1Info.getNoProps(), Op2Info.getNoProps());
208 }
209
210 return Cost;
211 }
212 // Vector unsigned division/remainder will be simplified to shifts/masks.
213 if ((ISD == ISD::UDIV || ISD == ISD::UREM) && Op2Info.isConstant() &&
214 Op2Info.isPowerOf2()) {
215 if (ISD == ISD::UDIV)
216 return getArithmeticInstrCost(Instruction::LShr, Ty, CostKind,
217 Op1Info.getNoProps(), Op2Info.getNoProps());
218 // UREM
219 return getArithmeticInstrCost(Instruction::And, Ty, CostKind,
220 Op1Info.getNoProps(), Op2Info.getNoProps());
221 }
222
223 static const CostKindTblEntry LSXCostTable[]{
224 {ISD::ADD, MVT::v16i8, {1, 1}}, // vaddi.b/vadd.b
225 {ISD::ADD, MVT::v8i16, {1, 1}}, // vaddi.h/vadd.h
226 {ISD::ADD, MVT::v4i32, {1, 1}}, // vaddi.w/vadd.w
227 {ISD::ADD, MVT::v2i64, {1, 1}}, // vaddi.d/vadd.d
228
229 {ISD::SUB, MVT::v16i8, {1, 1}}, // vsubi.b/vsub.b
230 {ISD::SUB, MVT::v8i16, {1, 1}}, // vsubi.h/vsub.h
231 {ISD::SUB, MVT::v4i32, {1, 1}}, // vsubi.w/vsub.w
232 {ISD::SUB, MVT::v2i64, {1, 1}}, // vsubi.d/vsub.d
233
234 {ISD::MUL, MVT::v16i8, {2, 4}}, // vmul.b
235 {ISD::MUL, MVT::v8i16, {2, 4}}, // vmul.h
236 {ISD::MUL, MVT::v4i32, {2, 4}}, // vmul.w
237 {ISD::MUL, MVT::v2i64, {2, 4}}, // vmul.d
238
239 {ISD::SDIV, MVT::v16i8, {62, 29}}, // vdiv.b
240 {ISD::SDIV, MVT::v8i16, {34, 17}}, // vdiv.h
241 {ISD::SDIV, MVT::v4i32, {22, 11}}, // vdiv.w
242 {ISD::SDIV, MVT::v2i64, {16, 8}}, // vdiv.d
243
244 {ISD::UDIV, MVT::v16i8, {66, 29}}, // vdiv.bu
245 {ISD::UDIV, MVT::v8i16, {36, 17}}, // vdiv.hu
246 {ISD::UDIV, MVT::v4i32, {22, 11}}, // vdiv.wu
247 {ISD::UDIV, MVT::v2i64, {16, 8}}, // vdiv.du
248
249 {ISD::SREM, MVT::v16i8, {76, 38}}, // vmod.b
250 {ISD::SREM, MVT::v8i16, {44, 24}}, // vmod.h
251 {ISD::SREM, MVT::v4i32, {27, 17}}, // vmod.w
252 {ISD::SREM, MVT::v2i64, {19, 14}}, // vmod.d
253
254 {ISD::UREM, MVT::v16i8, {80, 38}}, // vmod.bu
255 {ISD::UREM, MVT::v8i16, {44, 24}}, // vmod.hu
256 {ISD::UREM, MVT::v4i32, {28, 17}}, // vmod.wu
257 {ISD::UREM, MVT::v2i64, {19, 14}}, // vmod.du
258
259 {ISD::SHL, MVT::v16i8, {1, 1}}, // vslli.b/vsll.b
260 {ISD::SHL, MVT::v8i16, {1, 1}}, // vslli.h/vsll.h
261 {ISD::SHL, MVT::v4i32, {1, 1}}, // vslli.w/vsll.w
262 {ISD::SHL, MVT::v2i64, {1, 1}}, // vslli.d/vsll.d
263
264 {ISD::SRL, MVT::v16i8, {1, 1}}, // vsrli.b/vsrl.b
265 {ISD::SRL, MVT::v8i16, {1, 1}}, // vsrli.h/vsrl.h
266 {ISD::SRL, MVT::v4i32, {1, 1}}, // vsrli.w/vsrl.w
267 {ISD::SRL, MVT::v2i64, {1, 1}}, // vsrli.d/vsrl.d
268
269 {ISD::SRA, MVT::v16i8, {1, 1}}, // vsrai.b/vsra.b
270 {ISD::SRA, MVT::v8i16, {1, 1}}, // vsrai.h/vsra.h
271 {ISD::SRA, MVT::v4i32, {1, 1}}, // vsrai.w/vsra.w
272 {ISD::SRA, MVT::v2i64, {1, 1}}, // vsrai.d/vsra.d
273
274 {ISD::AND, MVT::v16i8, {1, 1}}, // vand.b/vand.v
275 {ISD::AND, MVT::v8i16, {1, 1}}, // vand.v
276 {ISD::AND, MVT::v4i32, {1, 1}}, // vand.v
277 {ISD::AND, MVT::v2i64, {1, 1}}, // vand.v
278
279 {ISD::OR, MVT::v16i8, {1, 1}}, // vori.b/vor.v
280 {ISD::OR, MVT::v8i16, {1, 1}}, // vor.v
281 {ISD::OR, MVT::v4i32, {1, 1}}, // vor.v
282 {ISD::OR, MVT::v2i64, {1, 1}}, // vor.v
283
284 {ISD::XOR, MVT::v16i8, {1, 1}}, // vxori.b/vxor.v
285 {ISD::XOR, MVT::v8i16, {1, 1}}, // vxor.v
286 {ISD::XOR, MVT::v4i32, {1, 1}}, // vxor.v
287 {ISD::XOR, MVT::v2i64, {1, 1}}, // vxor.v
288
289 {ISD::FADD, MVT::v4f32, {1, 3}}, // vfadd.s
290 {ISD::FADD, MVT::v2f64, {1, 3}}, // vfadd.d
291
292 {ISD::FSUB, MVT::v4f32, {1, 3}}, // vfsub.s
293 {ISD::FSUB, MVT::v2f64, {1, 3}}, // vfsub.d
294
295 {ISD::FMUL, MVT::v4f32, {2, 5}}, // vfmul.s
296 {ISD::FMUL, MVT::v2f64, {2, 5}}, // vfmul.d
297
298 {ISD::FDIV, MVT::v4f32, {22, 11}}, // vfdiv.s
299 {ISD::FDIV, MVT::v2f64, {16, 8}}, // vfdiv.d
300 };
301
302 if (ST->hasExtLSX()) {
303 if (const auto *Entry = CostTableLookup(LSXCostTable, ISD, LT.second))
304 if (auto KindCost = Entry->Cost[CostKind])
305 return LT.first * *KindCost;
306 }
307
308 static const CostKindTblEntry LASXUniformConstCostTable[]{
309 {ISD::ADD, MVT::v32i8, {1, 1}}, // xvaddi.b/xvadd.b
310 {ISD::ADD, MVT::v16i16, {1, 1}}, // xvaddi.h/xvadd.h
311 {ISD::ADD, MVT::v8i32, {1, 1}}, // xvaddi.w/xvadd.w
312 {ISD::ADD, MVT::v4i64, {1, 1}}, // xvaddi.d/xvadd.d
313
314 {ISD::SUB, MVT::v32i8, {1, 1}}, // xvsubi.b/xvsub.b
315 {ISD::SUB, MVT::v16i16, {1, 1}}, // xvsubi.h/xvsub.h
316 {ISD::SUB, MVT::v8i32, {1, 1}}, // xvsubi.w/xvsub.w
317 {ISD::SUB, MVT::v4i64, {1, 1}}, // xvsubi.d/xvsub.d
318
319 {ISD::MUL, MVT::v32i8, {2, 4}}, // xvmul.b
320 {ISD::MUL, MVT::v16i16, {2, 4}}, // xvmul.h
321 {ISD::MUL, MVT::v8i32, {2, 4}}, // xvmul.w
322 {ISD::MUL, MVT::v4i64, {2, 4}}, // xvmul.d
323
324 {ISD::SDIV, MVT::v32i8, {62, 29}}, // xvdiv.b
325 {ISD::SDIV, MVT::v16i16, {34, 17}}, // xvdiv.h
326 {ISD::SDIV, MVT::v8i32, {22, 11}}, // xvdiv.w
327 {ISD::SDIV, MVT::v4i64, {16, 8}}, // xvdiv.d
328
329 {ISD::UDIV, MVT::v32i8, {66, 29}}, // xvdiv.bu
330 {ISD::UDIV, MVT::v16i16, {36, 17}}, // xvdiv.hu
331 {ISD::UDIV, MVT::v8i32, {22, 11}}, // xvdiv.wu
332 {ISD::UDIV, MVT::v4i64, {16, 8}}, // xvdiv.du
333
334 {ISD::SREM, MVT::v32i8, {76, 38}}, // xvmod.b
335 {ISD::SREM, MVT::v16i16, {44, 24}}, // xvmod.h
336 {ISD::SREM, MVT::v8i32, {28, 17}}, // xvmod.w
337 {ISD::SREM, MVT::v4i64, {19, 14}}, // xvmod.d
338
339 {ISD::UREM, MVT::v32i8, {76, 38}}, // xvmod.bu
340 {ISD::UREM, MVT::v16i16, {43, 24}}, // xvmod.hu
341 {ISD::UREM, MVT::v8i32, {28, 17}}, // xvmod.wu
342 {ISD::UREM, MVT::v4i64, {19, 14}}, // xvmod.du
343
344 {ISD::SHL, MVT::v32i8, {1, 1}}, // xvslli.b/xvsll.b
345 {ISD::SHL, MVT::v16i16, {1, 1}}, // xvslli.h/xvsll.h
346 {ISD::SHL, MVT::v8i32, {1, 1}}, // xvslli.w/xvsll.w
347 {ISD::SHL, MVT::v4i64, {1, 1}}, // xvslli.d/xvsll.d
348
349 {ISD::SRL, MVT::v32i8, {1, 1}}, // xvsrli.b/xvsrl.b
350 {ISD::SRL, MVT::v16i16, {1, 1}}, // xvsrli.h/xvsrl.h
351 {ISD::SRL, MVT::v8i32, {1, 1}}, // xvsrli.w/xvsrl.w
352 {ISD::SRL, MVT::v4i64, {1, 1}}, // xvsrli.d/xvsrl.d
353
354 {ISD::SRA, MVT::v32i8, {1, 1}}, // xvsrai.b/xvsra.b
355 {ISD::SRA, MVT::v16i16, {1, 1}}, // xvsrai.h/xvsra.h
356 {ISD::SRA, MVT::v8i32, {1, 1}}, // xvsrai.w/xvsra.w
357 {ISD::SRA, MVT::v4i64, {1, 1}}, // xvsrai.d/xvsra.d
358
359 {ISD::AND, MVT::v32i8, {1, 1}}, // xvandi.b/xvand.v
360 {ISD::AND, MVT::v16i16, {1, 1}}, // xvand.v
361 {ISD::AND, MVT::v8i32, {1, 1}}, // xvand.v
362 {ISD::AND, MVT::v4i64, {1, 1}}, // xvand.v
363
364 {ISD::OR, MVT::v32i8, {1, 1}}, // xvori.b/xvor.v
365 {ISD::OR, MVT::v16i16, {1, 1}}, // xvor.v
366 {ISD::OR, MVT::v8i32, {1, 1}}, // xvor.v
367 {ISD::OR, MVT::v4i64, {1, 1}}, // xvor.v
368
369 {ISD::XOR, MVT::v32i8, {1, 1}}, // xvxori.b/xvxor.v
370 {ISD::XOR, MVT::v16i16, {1, 1}}, // xvxor.v
371 {ISD::XOR, MVT::v8i32, {1, 1}}, // xvxor.v
372 {ISD::XOR, MVT::v4i64, {1, 1}}, // xvxor.v
373
374 {ISD::FADD, MVT::v8f32, {1, 3}}, // xvfadd.s
375 {ISD::FADD, MVT::v4f64, {1, 3}}, // xvfadd.d
376
377 {ISD::FSUB, MVT::v8f32, {1, 3}}, // xvfsub.s
378 {ISD::FSUB, MVT::v4f64, {1, 3}}, // xvfsub.d
379
380 {ISD::FMUL, MVT::v8f32, {2, 5}}, // xvfmul.s
381 {ISD::FMUL, MVT::v4f64, {2, 5}}, // xvfmul.d
382
383 {ISD::FDIV, MVT::v8f32, {22, 11}}, // xvfdiv.s
384 {ISD::FDIV, MVT::v4f64, {16, 8}}, // xvfdiv.d
385 };
386
387 if (ST->hasExtLASX()) {
388 if (const auto *Entry =
389 CostTableLookup(LASXUniformConstCostTable, ISD, LT.second))
390 if (auto KindCost = Entry->Cost[CostKind])
391 return LT.first * *KindCost;
392 }
393
394 static const CostKindTblEntry LA64CostTable[]{
395 {ISD::ADD, MVT::i64, {1, 1}}, // addi.d/add.d
396 {ISD::SUB, MVT::i64, {1, 1}}, // subi.d/sub.d
397 {ISD::MUL, MVT::i64, {2, 4}}, // mul.d
398
399 {ISD::SDIV, MVT::i64, {26, 18}}, // div.d
400 {ISD::UDIV, MVT::i64, {26, 18}}, // div.du
401 {ISD::SREM, MVT::i64, {26, 18}}, // mod.d
402 {ISD::UREM, MVT::i64, {26, 18}}, // mod.du
403
404 {ISD::SHL, MVT::i64, {1, 1}}, // slli.d/sll.d
405 {ISD::SRL, MVT::i64, {1, 1}}, // srli.d/srl.d
406 {ISD::SRA, MVT::i64, {1, 1}}, // srai.d/sra.d
407
408 {ISD::AND, MVT::i64, {1, 1}}, // andi.d/and.d
409 {ISD::OR, MVT::i64, {1, 1}}, // ori.d/or.d
410 {ISD::XOR, MVT::i64, {1, 1}}, // xori.d/xor.d
411
412 {ISD::FADD, MVT::f64, {1, 3}}, // fadd.d
413 {ISD::FSUB, MVT::f64, {1, 3}}, // fsub.d
414 {ISD::FMUL, MVT::f64, {2, 5}}, // fmul.d
415 {ISD::FDIV, MVT::f64, {9, 12}}, // fdiv.d
416 };
417
418 if (ST->is64Bit()) {
419 if (const auto *Entry = CostTableLookup(LA64CostTable, ISD, LT.second))
420 if (auto KindCost = Entry->Cost[CostKind])
421 return LT.first * *KindCost;
422 }
423
424 static const CostKindTblEntry LA32CostTable[]{
425 {ISD::ADD, MVT::i32, {1, 1}}, // addi.w/add.w
426 {ISD::SUB, MVT::i32, {1, 1}}, // subi.w/sub.w
427 {ISD::MUL, MVT::i32, {2, 4}}, // mul.w
428
429 {ISD::SDIV, MVT::i32, {24, 11}}, // div.w
430 {ISD::UDIV, MVT::i32, {24, 12}}, // div.wu
431 {ISD::SREM, MVT::i32, {24, 11}}, // mod.w
432 {ISD::UREM, MVT::i32, {24, 12}}, // mod.wu
433
434 {ISD::SHL, MVT::i32, {1, 1}}, // slli.w/sll.w
435 {ISD::SRL, MVT::i32, {1, 1}}, // srli.w/srl.w
436 {ISD::SRA, MVT::i32, {1, 1}}, // srai.w/sra.w
437
438 {ISD::AND, MVT::i32, {1, 1}}, // andi.w/and.w
439 {ISD::OR, MVT::i32, {1, 1}}, // ori.w/or.w
440 {ISD::XOR, MVT::i32, {1, 1}}, // xori.w/xor.w
441
442 {ISD::FADD, MVT::f32, {1, 3}}, // fadd.s
443 {ISD::FSUB, MVT::f32, {1, 3}}, // fsub.s
444 {ISD::FMUL, MVT::f32, {2, 5}}, // fmul.s
445 {ISD::FDIV, MVT::f32, {8, 9}}, // fdiv.s
446 };
447
448 if (const auto *Entry = CostTableLookup(LA32CostTable, ISD, LT.second))
449 if (auto KindCost = Entry->Cost[CostKind])
450 return LT.first * *KindCost;
451
452 // Fallback to the default implementation.
453 return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info, Op2Info,
454 Args, CxtI, TLibInfo, Scalars);
455}
456
457// Returns true if Ty is a fixed vector type held in a single LSX/LASX register.
458static bool isSingleVRegType(const LoongArchSubtarget *ST, Type *Ty) {
459 auto *VTy = dyn_cast<FixedVectorType>(Ty);
460 if (!VTy)
461 return false;
462 unsigned Bits = VTy->getPrimitiveSizeInBits().getFixedValue();
463 return (Bits == 128 && ST->hasExtLSX()) || (Bits == 256 && ST->hasExtLASX());
464}
465
467 unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
468 const Value *Op0, const Value *Op1, TTI::VectorInstrContext VIC) const {
469 // Element 0 of a floating-point vector is the aliased FPR, so extracting it
470 // is free.
471 if (Opcode == Instruction::ExtractElement && Index == 0 &&
473 return 0;
474 return BaseT::getVectorInstrCost(Opcode, Val, CostKind, Index, Op0, Op1, VIC);
475}
476
478 TTI::ShuffleKind Kind, VectorType *DstTy, VectorType *SrcTy,
479 TTI::TargetCostKind CostKind, ArrayRef<int> Mask, int Index,
480 VectorType *SubTp, ArrayRef<const Value *> Args, const Instruction *CxtI,
481 TTI::VectorInstrContext VIC) const {
482 if (isSingleVRegType(ST, SrcTy) && DstTy == SrcTy) {
483 Kind = improveShuffleKindFromMask(Kind, Mask, SrcTy, Index, SubTp);
484 unsigned EltBits = SrcTy->getScalarSizeInBits();
485 bool Is128 = SrcTy->getPrimitiveSizeInBits().getFixedValue() == 128;
486 switch (Kind) {
488 // [x]vreplvei.{b/h/w/d}, xvreplve0.{b/h/w/d}, [x]vreplgr2vr.{b/h/w/d}.
489 return 1;
490 case TTI::SK_Reverse:
492 // vshuf4i.{w/d} handles any single-source permutation of 32/64-bit
493 // elements within a 128-bit register.
494 if (Is128 && EltBits >= 32)
495 return 1;
496 break;
497 default:
498 break;
499 }
500 }
501 return BaseT::getShuffleCost(Kind, DstTy, SrcTy, CostKind, Mask, Index, SubTp,
502 Args, CxtI, VIC);
503}
504
506 unsigned Opcode, Type *InputTypeA, Type *InputTypeB, Type *AccumType,
508 TTI::PartialReductionExtendKind OpBExtend, std::optional<unsigned> BinOp,
509 TTI::TargetCostKind CostKind, std::optional<FastMathFlags> FMF) const {
511
512 if (CostKind != TTI::TCK_RecipThroughput)
513 return Invalid;
514
515 if (Opcode != Instruction::Add)
516 return Invalid;
517
518 // LoongArch only supports fixed-length vector now.
519 if (!ST->hasExtLSX() || VF.isScalable())
520 return Invalid;
521
522 // We only support multiply binary operations for now, and for muls we
523 // require the types being extended to be the same.
524 if (InputTypeA != InputTypeB)
525 return Invalid;
526
527 if (!BinOp || *BinOp != Instruction::Mul)
528 return Invalid;
529
530 // Must be sext or zext, different extend type is allowed for SUMLA.
531 if (OpAExtend == TTI::PR_None || OpBExtend == TTI::PR_None)
532 return Invalid;
533
534 unsigned Ratio =
535 AccumType->getScalarSizeInBits() / InputTypeA->getScalarSizeInBits();
536 if (VF.getKnownMinValue() <= Ratio)
537 return Invalid;
538
539 VectorType *InputVectorType = VectorType::get(InputTypeA, VF);
540 VectorType *AccumVectorType =
541 VectorType::get(AccumType, VF.divideCoefficientBy(Ratio));
542 // We don't yet support all kinds of legalization.
543 auto TC = TLI->getTypeConversion(AccumVectorType->getContext(),
544 EVT::getEVT(AccumVectorType));
545 switch (TC.first) {
546 default:
547 return Invalid;
551 // The legalised type (e.g. after splitting) must be legal too.
552 if (TLI->getTypeAction(AccumVectorType->getContext(), TC.second) !=
554 return Invalid;
555 break;
556 }
557
558 std::pair<InstructionCost, MVT> AccumLT =
559 getTypeLegalizationCost(AccumVectorType);
560 std::pair<InstructionCost, MVT> InputLT =
561 getTypeLegalizationCost(InputVectorType);
562
563 // A legal i8->i32 partial reduction will be lowered into:
564 // vmulwev.h.b[u] + vmulwod.h.b[u] + 2 x vhaddw.w[u].h[u] + 2 x vadd.w
565 // The estimate recip-throughput is 10.
566 InstructionCost Cost = InputLT.first * TTI::TCC_Basic * 10;
567 unsigned Bits = AccumLT.second.getSizeInBits();
568 if (AccumLT.second.getScalarType() == MVT::i32 &&
569 InputLT.second.getScalarType() == MVT::i8)
570 if ((ST->hasExtLASX() && Bits <= 256) || (ST->hasExtLSX() && Bits <= 128))
571 return Cost;
572
573 return BaseT::getPartialReductionCost(Opcode, InputTypeA, InputTypeB,
574 AccumType, VF, OpAExtend, OpBExtend,
575 BinOp, CostKind, FMF);
576}
Cost tables and simple lookup functions.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
static LVOptions Options
Definition LVOptions.cpp:25
CostTblEntryT< CostKindCosts > CostKindTblEntry
static bool isSingleVRegType(const LoongArchSubtarget *ST, Type *Ty)
This file a TargetTransformInfoImplBase conforming object specific to the LoongArch target machine.
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
This pass exposes codegen information to IR-level passes.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
TTI::ShuffleKind improveShuffleKindFromMask(TTI::ShuffleKind Kind, ArrayRef< int > Mask, VectorType *SrcTy, int &Index, VectorType *&SubTy) const
InstructionCost getShuffleCost(TTI::ShuffleKind Kind, VectorType *DstTy, VectorType *SrcTy, TTI::TargetCostKind CostKind, ArrayRef< int > Mask, int Index, VectorType *SubTp, ArrayRef< const Value * > Args={}, const Instruction *CtxI=nullptr, TTI::VectorInstrContext VIC=TTI::VectorInstrContext::None) const override
std::pair< InstructionCost, MVT > getTypeLegalizationCost(Type *Ty) const
InstructionCost getPartialReductionCost(unsigned Opcode, Type *InputTypeA, Type *InputTypeB, Type *AccumType, ElementCount VF, TTI::PartialReductionExtendKind OpAExtend, TTI::PartialReductionExtendKind OpBExtend, std::optional< unsigned > BinOp, TTI::TargetCostKind CostKind, std::optional< FastMathFlags > FMF) const override
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index, const Value *Op0, const Value *Op1, TTI::VectorInstrContext VIC=TTI::VectorInstrContext::None) const override
InstructionCost getMemoryOpCost(unsigned Opcode, Type *Src, Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind, TTI::OperandValueInfo OpInfo={TTI::OK_AnyValue, TTI::OP_None}, const Instruction *I=nullptr) const override
InstructionCost getArithmeticInstrCost(unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind, TTI::OperandValueInfo Opd1Info={TTI::OK_AnyValue, TTI::OP_None}, TTI::OperandValueInfo Opd2Info={TTI::OK_AnyValue, TTI::OP_None}, ArrayRef< const Value * > Args={}, const Instruction *CtxI=nullptr, const TargetLibraryInfo *TLibInfo=nullptr, ArrayRef< const Value * > Scalars={}) const override
static InstructionCost getInvalid(CostType Val=0)
A wrapper class for inspecting calls to intrinsic functions.
unsigned getCacheLineSize() const override
unsigned getMaxInterleaveFactor(ElementCount VF, bool HasUnorderedReductions) const override
bool shouldExpandReduction(const IntrinsicInst *II) const override
unsigned getRegisterClassForType(bool Vector, Type *Ty=nullptr) const override
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={}, const Instruction *CxtI=nullptr, const TargetLibraryInfo *TLibInfo=nullptr, ArrayRef< const Value * > Scalars={}) const override
TTI::MemCmpExpansionOptions enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const override
InstructionCost getMemoryOpCost(unsigned Opcode, Type *Src, Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind, TTI::OperandValueInfo OpInfo={TTI::OK_AnyValue, TTI::OP_None}, const Instruction *I=nullptr) const override
const char * getRegisterClassName(unsigned ClassID) const override
InstructionCost getPartialReductionCost(unsigned Opcode, Type *InputTypeA, Type *InputTypeB, Type *AccumType, ElementCount VF, TTI::PartialReductionExtendKind OpAExtend, TTI::PartialReductionExtendKind OpBExtend, std::optional< unsigned > BinOp, TTI::TargetCostKind CostKind, std::optional< FastMathFlags > FMF) const override
unsigned getNumberOfRegisters(unsigned ClassID) const override
InstructionCost getShuffleCost(TTI::ShuffleKind Kind, VectorType *DstTy, VectorType *SrcTy, TTI::TargetCostKind CostKind, ArrayRef< int > Mask, int Index, VectorType *SubTp, ArrayRef< const Value * > Args={}, const Instruction *CxtI=nullptr, TTI::VectorInstrContext VIC=TTI::VectorInstrContext::None) const override
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index, const Value *Op0, const Value *Op1, TTI::VectorInstrContext VIC=TTI::VectorInstrContext::None) const override
TypeSize getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const override
Provides information about what library functions are available for the current target.
virtual TypeSize getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const
TargetCostKind
The kind of cost model.
@ TCK_RecipThroughput
Reciprocal throughput.
llvm::VectorInstrContext VectorInstrContext
@ TCC_Basic
The cost of a typical 'add' instruction.
ShuffleKind
The various kinds of shuffle patterns for vector queries.
@ SK_PermuteSingleSrc
Shuffle elements of single source vector with any shuffle mask.
@ SK_Broadcast
Broadcast element 0 to all other elements.
@ SK_Reverse
Reverse the order of the vector.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:352
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.h:354
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:370
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
LLVM Value Representation.
Definition Value.h:75
Base class of all SIMD vector types.
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:253
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:26
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:266
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:420
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:793
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:763
This is an optimization pass for GlobalISel generic memory operations.
const CostTblEntryT< CostType > * CostTableLookup(ArrayRef< CostTblEntryT< CostType > > Tbl, int ISD, MVT Ty)
Find in cost table.
Definition CostTable.h:36
InstructionCost Cost
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Cost Table Entry.
Definition CostTable.h:26
Extended Value Type.
Definition ValueTypes.h:35
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Returns options for expansion of memcmp. IsZeroCmp is.