1 //===-- AArch64TargetTransformInfo.cpp - AArch64 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 
9 #include "AArch64ExpandImm.h"
10 #include "AArch64TargetTransformInfo.h"
11 #include "MCTargetDesc/AArch64AddressingModes.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/Analysis/TargetTransformInfo.h"
14 #include "llvm/CodeGen/BasicTTIImpl.h"
15 #include "llvm/CodeGen/CostTable.h"
16 #include "llvm/CodeGen/TargetLowering.h"
17 #include "llvm/IR/IntrinsicInst.h"
18 #include "llvm/IR/IntrinsicsAArch64.h"
19 #include "llvm/Support/Debug.h"
20 #include <algorithm>
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "aarch64tti"
24 
25 static cl::opt<bool> EnableFalkorHWPFUnrollFix("enable-falkor-hwpf-unroll-fix",
26                                                cl::init(true), cl::Hidden);
27 
28 bool AArch64TTIImpl::areInlineCompatible(const Function *Caller,
29                                          const Function *Callee) const {
30   const TargetMachine &TM = getTLI()->getTargetMachine();
31 
32   const FeatureBitset &CallerBits =
33       TM.getSubtargetImpl(*Caller)->getFeatureBits();
34   const FeatureBitset &CalleeBits =
35       TM.getSubtargetImpl(*Callee)->getFeatureBits();
36 
37   // Inline a callee if its target-features are a subset of the callers
38   // target-features.
39   return (CallerBits & CalleeBits) == CalleeBits;
40 }
41 
42 /// Calculate the cost of materializing a 64-bit value. This helper
43 /// method might only calculate a fraction of a larger immediate. Therefore it
44 /// is valid to return a cost of ZERO.
45 int AArch64TTIImpl::getIntImmCost(int64_t Val) {
46   // Check if the immediate can be encoded within an instruction.
47   if (Val == 0 || AArch64_AM::isLogicalImmediate(Val, 64))
48     return 0;
49 
50   if (Val < 0)
51     Val = ~Val;
52 
53   // Calculate how many moves we will need to materialize this constant.
54   SmallVector<AArch64_IMM::ImmInsnModel, 4> Insn;
55   AArch64_IMM::expandMOVImm(Val, 64, Insn);
56   return Insn.size();
57 }
58 
59 /// Calculate the cost of materializing the given constant.
60 int AArch64TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty,
61                                   TTI::TargetCostKind CostKind) {
62   assert(Ty->isIntegerTy());
63 
64   unsigned BitSize = Ty->getPrimitiveSizeInBits();
65   if (BitSize == 0)
66     return ~0U;
67 
68   // Sign-extend all constants to a multiple of 64-bit.
69   APInt ImmVal = Imm;
70   if (BitSize & 0x3f)
71     ImmVal = Imm.sext((BitSize + 63) & ~0x3fU);
72 
73   // Split the constant into 64-bit chunks and calculate the cost for each
74   // chunk.
75   int Cost = 0;
76   for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
77     APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
78     int64_t Val = Tmp.getSExtValue();
79     Cost += getIntImmCost(Val);
80   }
81   // We need at least one instruction to materialze the constant.
82   return std::max(1, Cost);
83 }
84 
85 int AArch64TTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
86                                       const APInt &Imm, Type *Ty,
87                                       TTI::TargetCostKind CostKind) {
88   assert(Ty->isIntegerTy());
89 
90   unsigned BitSize = Ty->getPrimitiveSizeInBits();
91   // There is no cost model for constants with a bit size of 0. Return TCC_Free
92   // here, so that constant hoisting will ignore this constant.
93   if (BitSize == 0)
94     return TTI::TCC_Free;
95 
96   unsigned ImmIdx = ~0U;
97   switch (Opcode) {
98   default:
99     return TTI::TCC_Free;
100   case Instruction::GetElementPtr:
101     // Always hoist the base address of a GetElementPtr.
102     if (Idx == 0)
103       return 2 * TTI::TCC_Basic;
104     return TTI::TCC_Free;
105   case Instruction::Store:
106     ImmIdx = 0;
107     break;
108   case Instruction::Add:
109   case Instruction::Sub:
110   case Instruction::Mul:
111   case Instruction::UDiv:
112   case Instruction::SDiv:
113   case Instruction::URem:
114   case Instruction::SRem:
115   case Instruction::And:
116   case Instruction::Or:
117   case Instruction::Xor:
118   case Instruction::ICmp:
119     ImmIdx = 1;
120     break;
121   // Always return TCC_Free for the shift value of a shift instruction.
122   case Instruction::Shl:
123   case Instruction::LShr:
124   case Instruction::AShr:
125     if (Idx == 1)
126       return TTI::TCC_Free;
127     break;
128   case Instruction::Trunc:
129   case Instruction::ZExt:
130   case Instruction::SExt:
131   case Instruction::IntToPtr:
132   case Instruction::PtrToInt:
133   case Instruction::BitCast:
134   case Instruction::PHI:
135   case Instruction::Call:
136   case Instruction::Select:
137   case Instruction::Ret:
138   case Instruction::Load:
139     break;
140   }
141 
142   if (Idx == ImmIdx) {
143     int NumConstants = (BitSize + 63) / 64;
144     int Cost = AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
145     return (Cost <= NumConstants * TTI::TCC_Basic)
146                ? static_cast<int>(TTI::TCC_Free)
147                : Cost;
148   }
149   return AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
150 }
151 
152 int AArch64TTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
153                                         const APInt &Imm, Type *Ty,
154                                         TTI::TargetCostKind CostKind) {
155   assert(Ty->isIntegerTy());
156 
157   unsigned BitSize = Ty->getPrimitiveSizeInBits();
158   // There is no cost model for constants with a bit size of 0. Return TCC_Free
159   // here, so that constant hoisting will ignore this constant.
160   if (BitSize == 0)
161     return TTI::TCC_Free;
162 
163   // Most (all?) AArch64 intrinsics do not support folding immediates into the
164   // selected instruction, so we compute the materialization cost for the
165   // immediate directly.
166   if (IID >= Intrinsic::aarch64_addg && IID <= Intrinsic::aarch64_udiv)
167     return AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
168 
169   switch (IID) {
170   default:
171     return TTI::TCC_Free;
172   case Intrinsic::sadd_with_overflow:
173   case Intrinsic::uadd_with_overflow:
174   case Intrinsic::ssub_with_overflow:
175   case Intrinsic::usub_with_overflow:
176   case Intrinsic::smul_with_overflow:
177   case Intrinsic::umul_with_overflow:
178     if (Idx == 1) {
179       int NumConstants = (BitSize + 63) / 64;
180       int Cost = AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
181       return (Cost <= NumConstants * TTI::TCC_Basic)
182                  ? static_cast<int>(TTI::TCC_Free)
183                  : Cost;
184     }
185     break;
186   case Intrinsic::experimental_stackmap:
187     if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
188       return TTI::TCC_Free;
189     break;
190   case Intrinsic::experimental_patchpoint_void:
191   case Intrinsic::experimental_patchpoint_i64:
192     if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
193       return TTI::TCC_Free;
194     break;
195   }
196   return AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
197 }
198 
199 TargetTransformInfo::PopcntSupportKind
200 AArch64TTIImpl::getPopcntSupport(unsigned TyWidth) {
201   assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
202   if (TyWidth == 32 || TyWidth == 64)
203     return TTI::PSK_FastHardware;
204   // TODO: AArch64TargetLowering::LowerCTPOP() supports 128bit popcount.
205   return TTI::PSK_Software;
206 }
207 
208 bool AArch64TTIImpl::isWideningInstruction(Type *DstTy, unsigned Opcode,
209                                            ArrayRef<const Value *> Args) {
210 
211   // A helper that returns a vector type from the given type. The number of
212   // elements in type Ty determine the vector width.
213   auto toVectorTy = [&](Type *ArgTy) {
214     return VectorType::get(ArgTy->getScalarType(),
215                            cast<VectorType>(DstTy)->getNumElements());
216   };
217 
218   // Exit early if DstTy is not a vector type whose elements are at least
219   // 16-bits wide.
220   if (!DstTy->isVectorTy() || DstTy->getScalarSizeInBits() < 16)
221     return false;
222 
223   // Determine if the operation has a widening variant. We consider both the
224   // "long" (e.g., usubl) and "wide" (e.g., usubw) versions of the
225   // instructions.
226   //
227   // TODO: Add additional widening operations (e.g., mul, shl, etc.) once we
228   //       verify that their extending operands are eliminated during code
229   //       generation.
230   switch (Opcode) {
231   case Instruction::Add: // UADDL(2), SADDL(2), UADDW(2), SADDW(2).
232   case Instruction::Sub: // USUBL(2), SSUBL(2), USUBW(2), SSUBW(2).
233     break;
234   default:
235     return false;
236   }
237 
238   // To be a widening instruction (either the "wide" or "long" versions), the
239   // second operand must be a sign- or zero extend having a single user. We
240   // only consider extends having a single user because they may otherwise not
241   // be eliminated.
242   if (Args.size() != 2 ||
243       (!isa<SExtInst>(Args[1]) && !isa<ZExtInst>(Args[1])) ||
244       !Args[1]->hasOneUse())
245     return false;
246   auto *Extend = cast<CastInst>(Args[1]);
247 
248   // Legalize the destination type and ensure it can be used in a widening
249   // operation.
250   auto DstTyL = TLI->getTypeLegalizationCost(DL, DstTy);
251   unsigned DstElTySize = DstTyL.second.getScalarSizeInBits();
252   if (!DstTyL.second.isVector() || DstElTySize != DstTy->getScalarSizeInBits())
253     return false;
254 
255   // Legalize the source type and ensure it can be used in a widening
256   // operation.
257   Type *SrcTy = toVectorTy(Extend->getSrcTy());
258   auto SrcTyL = TLI->getTypeLegalizationCost(DL, SrcTy);
259   unsigned SrcElTySize = SrcTyL.second.getScalarSizeInBits();
260   if (!SrcTyL.second.isVector() || SrcElTySize != SrcTy->getScalarSizeInBits())
261     return false;
262 
263   // Get the total number of vector elements in the legalized types.
264   unsigned NumDstEls = DstTyL.first * DstTyL.second.getVectorNumElements();
265   unsigned NumSrcEls = SrcTyL.first * SrcTyL.second.getVectorNumElements();
266 
267   // Return true if the legalized types have the same number of vector elements
268   // and the destination element type size is twice that of the source type.
269   return NumDstEls == NumSrcEls && 2 * SrcElTySize == DstElTySize;
270 }
271 
272 int AArch64TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
273                                      TTI::TargetCostKind CostKind,
274                                      const Instruction *I) {
275   int ISD = TLI->InstructionOpcodeToISD(Opcode);
276   assert(ISD && "Invalid opcode");
277 
278   // If the cast is observable, and it is used by a widening instruction (e.g.,
279   // uaddl, saddw, etc.), it may be free.
280   if (I && I->hasOneUse()) {
281     auto *SingleUser = cast<Instruction>(*I->user_begin());
282     SmallVector<const Value *, 4> Operands(SingleUser->operand_values());
283     if (isWideningInstruction(Dst, SingleUser->getOpcode(), Operands)) {
284       // If the cast is the second operand, it is free. We will generate either
285       // a "wide" or "long" version of the widening instruction.
286       if (I == SingleUser->getOperand(1))
287         return 0;
288       // If the cast is not the second operand, it will be free if it looks the
289       // same as the second operand. In this case, we will generate a "long"
290       // version of the widening instruction.
291       if (auto *Cast = dyn_cast<CastInst>(SingleUser->getOperand(1)))
292         if (I->getOpcode() == unsigned(Cast->getOpcode()) &&
293             cast<CastInst>(I)->getSrcTy() == Cast->getSrcTy())
294           return 0;
295     }
296   }
297 
298   EVT SrcTy = TLI->getValueType(DL, Src);
299   EVT DstTy = TLI->getValueType(DL, Dst);
300 
301   if (!SrcTy.isSimple() || !DstTy.isSimple())
302     return BaseT::getCastInstrCost(Opcode, Dst, Src, CostKind, I);
303 
304   static const TypeConversionCostTblEntry
305   ConversionTbl[] = {
306     { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32,  1 },
307     { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64,  0 },
308     { ISD::TRUNCATE, MVT::v8i8,  MVT::v8i32,  3 },
309     { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 6 },
310 
311     // The number of shll instructions for the extension.
312     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i16, 3 },
313     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i16, 3 },
314     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i32, 2 },
315     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i32, 2 },
316     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i8,  3 },
317     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i8,  3 },
318     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i16, 2 },
319     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i16, 2 },
320     { ISD::SIGN_EXTEND, MVT::v8i64,  MVT::v8i8,  7 },
321     { ISD::ZERO_EXTEND, MVT::v8i64,  MVT::v8i8,  7 },
322     { ISD::SIGN_EXTEND, MVT::v8i64,  MVT::v8i16, 6 },
323     { ISD::ZERO_EXTEND, MVT::v8i64,  MVT::v8i16, 6 },
324     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 2 },
325     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 2 },
326     { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
327     { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 6 },
328 
329     // LowerVectorINT_TO_FP:
330     { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
331     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
332     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 },
333     { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 },
334     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
335     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 },
336 
337     // Complex: to v2f32
338     { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i8,  3 },
339     { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i16, 3 },
340     { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i64, 2 },
341     { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i8,  3 },
342     { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i16, 3 },
343     { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 2 },
344 
345     // Complex: to v4f32
346     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8,  4 },
347     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
348     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8,  3 },
349     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
350 
351     // Complex: to v8f32
352     { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8,  10 },
353     { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
354     { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8,  10 },
355     { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 },
356 
357     // Complex: to v16f32
358     { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 21 },
359     { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i8, 21 },
360 
361     // Complex: to v2f64
362     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i8,  4 },
363     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i16, 4 },
364     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
365     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8,  4 },
366     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 4 },
367     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 },
368 
369 
370     // LowerVectorFP_TO_INT
371     { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f32, 1 },
372     { ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f32, 1 },
373     { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f64, 1 },
374     { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f32, 1 },
375     { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 },
376     { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f64, 1 },
377 
378     // Complex, from v2f32: legal type is v2i32 (no cost) or v2i64 (1 ext).
379     { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f32, 2 },
380     { ISD::FP_TO_SINT, MVT::v2i16, MVT::v2f32, 1 },
381     { ISD::FP_TO_SINT, MVT::v2i8,  MVT::v2f32, 1 },
382     { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f32, 2 },
383     { ISD::FP_TO_UINT, MVT::v2i16, MVT::v2f32, 1 },
384     { ISD::FP_TO_UINT, MVT::v2i8,  MVT::v2f32, 1 },
385 
386     // Complex, from v4f32: legal type is v4i16, 1 narrowing => ~2
387     { ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f32, 2 },
388     { ISD::FP_TO_SINT, MVT::v4i8,  MVT::v4f32, 2 },
389     { ISD::FP_TO_UINT, MVT::v4i16, MVT::v4f32, 2 },
390     { ISD::FP_TO_UINT, MVT::v4i8,  MVT::v4f32, 2 },
391 
392     // Complex, from v2f64: legal type is v2i32, 1 narrowing => ~2.
393     { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 2 },
394     { ISD::FP_TO_SINT, MVT::v2i16, MVT::v2f64, 2 },
395     { ISD::FP_TO_SINT, MVT::v2i8,  MVT::v2f64, 2 },
396     { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f64, 2 },
397     { ISD::FP_TO_UINT, MVT::v2i16, MVT::v2f64, 2 },
398     { ISD::FP_TO_UINT, MVT::v2i8,  MVT::v2f64, 2 },
399   };
400 
401   if (const auto *Entry = ConvertCostTableLookup(ConversionTbl, ISD,
402                                                  DstTy.getSimpleVT(),
403                                                  SrcTy.getSimpleVT()))
404     return Entry->Cost;
405 
406   return BaseT::getCastInstrCost(Opcode, Dst, Src, CostKind, I);
407 }
408 
409 int AArch64TTIImpl::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
410                                              VectorType *VecTy,
411                                              unsigned Index) {
412 
413   // Make sure we were given a valid extend opcode.
414   assert((Opcode == Instruction::SExt || Opcode == Instruction::ZExt) &&
415          "Invalid opcode");
416 
417   // We are extending an element we extract from a vector, so the source type
418   // of the extend is the element type of the vector.
419   auto *Src = VecTy->getElementType();
420 
421   // Sign- and zero-extends are for integer types only.
422   assert(isa<IntegerType>(Dst) && isa<IntegerType>(Src) && "Invalid type");
423 
424   // Get the cost for the extract. We compute the cost (if any) for the extend
425   // below.
426   auto Cost = getVectorInstrCost(Instruction::ExtractElement, VecTy, Index);
427 
428   // Legalize the types.
429   auto VecLT = TLI->getTypeLegalizationCost(DL, VecTy);
430   auto DstVT = TLI->getValueType(DL, Dst);
431   auto SrcVT = TLI->getValueType(DL, Src);
432   TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
433 
434   // If the resulting type is still a vector and the destination type is legal,
435   // we may get the extension for free. If not, get the default cost for the
436   // extend.
437   if (!VecLT.second.isVector() || !TLI->isTypeLegal(DstVT))
438     return Cost + getCastInstrCost(Opcode, Dst, Src, CostKind);
439 
440   // The destination type should be larger than the element type. If not, get
441   // the default cost for the extend.
442   if (DstVT.getSizeInBits() < SrcVT.getSizeInBits())
443     return Cost + getCastInstrCost(Opcode, Dst, Src, CostKind);
444 
445   switch (Opcode) {
446   default:
447     llvm_unreachable("Opcode should be either SExt or ZExt");
448 
449   // For sign-extends, we only need a smov, which performs the extension
450   // automatically.
451   case Instruction::SExt:
452     return Cost;
453 
454   // For zero-extends, the extend is performed automatically by a umov unless
455   // the destination type is i64 and the element type is i8 or i16.
456   case Instruction::ZExt:
457     if (DstVT.getSizeInBits() != 64u || SrcVT.getSizeInBits() == 32u)
458       return Cost;
459   }
460 
461   // If we are unable to perform the extend for free, get the default cost.
462   return Cost + getCastInstrCost(Opcode, Dst, Src, CostKind);
463 }
464 
465 int AArch64TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
466                                        unsigned Index) {
467   assert(Val->isVectorTy() && "This must be a vector type");
468 
469   if (Index != -1U) {
470     // Legalize the type.
471     std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val);
472 
473     // This type is legalized to a scalar type.
474     if (!LT.second.isVector())
475       return 0;
476 
477     // The type may be split. Normalize the index to the new type.
478     unsigned Width = LT.second.getVectorNumElements();
479     Index = Index % Width;
480 
481     // The element at index zero is already inside the vector.
482     if (Index == 0)
483       return 0;
484   }
485 
486   // All other insert/extracts cost this much.
487   return ST->getVectorInsertExtractBaseCost();
488 }
489 
490 int AArch64TTIImpl::getArithmeticInstrCost(
491     unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
492     TTI::OperandValueKind Opd1Info,
493     TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
494     TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args,
495     const Instruction *CxtI) {
496   // Legalize the type.
497   std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
498 
499   // If the instruction is a widening instruction (e.g., uaddl, saddw, etc.),
500   // add in the widening overhead specified by the sub-target. Since the
501   // extends feeding widening instructions are performed automatically, they
502   // aren't present in the generated code and have a zero cost. By adding a
503   // widening overhead here, we attach the total cost of the combined operation
504   // to the widening instruction.
505   int Cost = 0;
506   if (isWideningInstruction(Ty, Opcode, Args))
507     Cost += ST->getWideningBaseCost();
508 
509   int ISD = TLI->InstructionOpcodeToISD(Opcode);
510 
511   switch (ISD) {
512   default:
513     return Cost + BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info,
514                                                 Opd2Info,
515                                                 Opd1PropInfo, Opd2PropInfo);
516   case ISD::SDIV:
517     if (Opd2Info == TargetTransformInfo::OK_UniformConstantValue &&
518         Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) {
519       // On AArch64, scalar signed division by constants power-of-two are
520       // normally expanded to the sequence ADD + CMP + SELECT + SRA.
521       // The OperandValue properties many not be same as that of previous
522       // operation; conservatively assume OP_None.
523       Cost += getArithmeticInstrCost(Instruction::Add, Ty, CostKind,
524                                      Opd1Info, Opd2Info,
525                                      TargetTransformInfo::OP_None,
526                                      TargetTransformInfo::OP_None);
527       Cost += getArithmeticInstrCost(Instruction::Sub, Ty, CostKind,
528                                      Opd1Info, Opd2Info,
529                                      TargetTransformInfo::OP_None,
530                                      TargetTransformInfo::OP_None);
531       Cost += getArithmeticInstrCost(Instruction::Select, Ty, CostKind,
532                                      Opd1Info, Opd2Info,
533                                      TargetTransformInfo::OP_None,
534                                      TargetTransformInfo::OP_None);
535       Cost += getArithmeticInstrCost(Instruction::AShr, Ty, CostKind,
536                                      Opd1Info, Opd2Info,
537                                      TargetTransformInfo::OP_None,
538                                      TargetTransformInfo::OP_None);
539       return Cost;
540     }
541     LLVM_FALLTHROUGH;
542   case ISD::UDIV:
543     if (Opd2Info == TargetTransformInfo::OK_UniformConstantValue) {
544       auto VT = TLI->getValueType(DL, Ty);
545       if (TLI->isOperationLegalOrCustom(ISD::MULHU, VT)) {
546         // Vector signed division by constant are expanded to the
547         // sequence MULHS + ADD/SUB + SRA + SRL + ADD, and unsigned division
548         // to MULHS + SUB + SRL + ADD + SRL.
549         int MulCost = getArithmeticInstrCost(Instruction::Mul, Ty, CostKind,
550                                              Opd1Info, Opd2Info,
551                                              TargetTransformInfo::OP_None,
552                                              TargetTransformInfo::OP_None);
553         int AddCost = getArithmeticInstrCost(Instruction::Add, Ty, CostKind,
554                                              Opd1Info, Opd2Info,
555                                              TargetTransformInfo::OP_None,
556                                              TargetTransformInfo::OP_None);
557         int ShrCost = getArithmeticInstrCost(Instruction::AShr, Ty, CostKind,
558                                              Opd1Info, Opd2Info,
559                                              TargetTransformInfo::OP_None,
560                                              TargetTransformInfo::OP_None);
561         return MulCost * 2 + AddCost * 2 + ShrCost * 2 + 1;
562       }
563     }
564 
565     Cost += BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info,
566                                           Opd2Info,
567                                           Opd1PropInfo, Opd2PropInfo);
568     if (Ty->isVectorTy()) {
569       // On AArch64, vector divisions are not supported natively and are
570       // expanded into scalar divisions of each pair of elements.
571       Cost += getArithmeticInstrCost(Instruction::ExtractElement, Ty, CostKind,
572                                      Opd1Info, Opd2Info, Opd1PropInfo,
573                                      Opd2PropInfo);
574       Cost += getArithmeticInstrCost(Instruction::InsertElement, Ty, CostKind,
575                                      Opd1Info, Opd2Info, Opd1PropInfo,
576                                      Opd2PropInfo);
577       // TODO: if one of the arguments is scalar, then it's not necessary to
578       // double the cost of handling the vector elements.
579       Cost += Cost;
580     }
581     return Cost;
582 
583   case ISD::ADD:
584   case ISD::MUL:
585   case ISD::XOR:
586   case ISD::OR:
587   case ISD::AND:
588     // These nodes are marked as 'custom' for combining purposes only.
589     // We know that they are legal. See LowerAdd in ISelLowering.
590     return (Cost + 1) * LT.first;
591   }
592 }
593 
594 int AArch64TTIImpl::getAddressComputationCost(Type *Ty, ScalarEvolution *SE,
595                                               const SCEV *Ptr) {
596   // Address computations in vectorized code with non-consecutive addresses will
597   // likely result in more instructions compared to scalar code where the
598   // computation can more often be merged into the index mode. The resulting
599   // extra micro-ops can significantly decrease throughput.
600   unsigned NumVectorInstToHideOverhead = 10;
601   int MaxMergeDistance = 64;
602 
603   if (Ty->isVectorTy() && SE &&
604       !BaseT::isConstantStridedAccessLessThan(SE, Ptr, MaxMergeDistance + 1))
605     return NumVectorInstToHideOverhead;
606 
607   // In many cases the address computation is not merged into the instruction
608   // addressing mode.
609   return 1;
610 }
611 
612 int AArch64TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
613                                        Type *CondTy,
614                                        TTI::TargetCostKind CostKind,
615                                        const Instruction *I) {
616 
617   int ISD = TLI->InstructionOpcodeToISD(Opcode);
618   // We don't lower some vector selects well that are wider than the register
619   // width.
620   if (ValTy->isVectorTy() && ISD == ISD::SELECT) {
621     // We would need this many instructions to hide the scalarization happening.
622     const int AmortizationCost = 20;
623     static const TypeConversionCostTblEntry
624     VectorSelectTbl[] = {
625       { ISD::SELECT, MVT::v16i1, MVT::v16i16, 16 },
626       { ISD::SELECT, MVT::v8i1, MVT::v8i32, 8 },
627       { ISD::SELECT, MVT::v16i1, MVT::v16i32, 16 },
628       { ISD::SELECT, MVT::v4i1, MVT::v4i64, 4 * AmortizationCost },
629       { ISD::SELECT, MVT::v8i1, MVT::v8i64, 8 * AmortizationCost },
630       { ISD::SELECT, MVT::v16i1, MVT::v16i64, 16 * AmortizationCost }
631     };
632 
633     EVT SelCondTy = TLI->getValueType(DL, CondTy);
634     EVT SelValTy = TLI->getValueType(DL, ValTy);
635     if (SelCondTy.isSimple() && SelValTy.isSimple()) {
636       if (const auto *Entry = ConvertCostTableLookup(VectorSelectTbl, ISD,
637                                                      SelCondTy.getSimpleVT(),
638                                                      SelValTy.getSimpleVT()))
639         return Entry->Cost;
640     }
641   }
642   return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, CostKind, I);
643 }
644 
645 AArch64TTIImpl::TTI::MemCmpExpansionOptions
646 AArch64TTIImpl::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
647   TTI::MemCmpExpansionOptions Options;
648   if (ST->requiresStrictAlign()) {
649     // TODO: Add cost modeling for strict align. Misaligned loads expand to
650     // a bunch of instructions when strict align is enabled.
651     return Options;
652   }
653   Options.AllowOverlappingLoads = true;
654   Options.MaxNumLoads = TLI->getMaxExpandSizeMemcmp(OptSize);
655   Options.NumLoadsPerBlock = Options.MaxNumLoads;
656   // TODO: Though vector loads usually perform well on AArch64, in some targets
657   // they may wake up the FP unit, which raises the power consumption.  Perhaps
658   // they could be used with no holds barred (-O3).
659   Options.LoadSizes = {8, 4, 2, 1};
660   return Options;
661 }
662 
663 int AArch64TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Ty,
664                                     MaybeAlign Alignment, unsigned AddressSpace,
665                                     TTI::TargetCostKind CostKind,
666                                     const Instruction *I) {
667   auto LT = TLI->getTypeLegalizationCost(DL, Ty);
668 
669   if (ST->isMisaligned128StoreSlow() && Opcode == Instruction::Store &&
670       LT.second.is128BitVector() && (!Alignment || *Alignment < Align(16))) {
671     // Unaligned stores are extremely inefficient. We don't split all
672     // unaligned 128-bit stores because the negative impact that has shown in
673     // practice on inlined block copy code.
674     // We make such stores expensive so that we will only vectorize if there
675     // are 6 other instructions getting vectorized.
676     const int AmortizationCost = 6;
677 
678     return LT.first * 2 * AmortizationCost;
679   }
680 
681   if (Ty->isVectorTy() &&
682       cast<VectorType>(Ty)->getElementType()->isIntegerTy(8)) {
683     unsigned ProfitableNumElements;
684     if (Opcode == Instruction::Store)
685       // We use a custom trunc store lowering so v.4b should be profitable.
686       ProfitableNumElements = 4;
687     else
688       // We scalarize the loads because there is not v.4b register and we
689       // have to promote the elements to v.2.
690       ProfitableNumElements = 8;
691 
692     if (cast<VectorType>(Ty)->getNumElements() < ProfitableNumElements) {
693       unsigned NumVecElts = cast<VectorType>(Ty)->getNumElements();
694       unsigned NumVectorizableInstsToAmortize = NumVecElts * 2;
695       // We generate 2 instructions per vector element.
696       return NumVectorizableInstsToAmortize * NumVecElts * 2;
697     }
698   }
699 
700   return LT.first;
701 }
702 
703 int AArch64TTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
704                                                unsigned Factor,
705                                                ArrayRef<unsigned> Indices,
706                                                unsigned Alignment,
707                                                unsigned AddressSpace,
708                                                TTI::TargetCostKind CostKind,
709                                                bool UseMaskForCond,
710                                                bool UseMaskForGaps) {
711   assert(Factor >= 2 && "Invalid interleave factor");
712   auto *VecVTy = cast<VectorType>(VecTy);
713 
714   if (!UseMaskForCond && !UseMaskForGaps &&
715       Factor <= TLI->getMaxSupportedInterleaveFactor()) {
716     unsigned NumElts = VecVTy->getNumElements();
717     auto *SubVecTy = VectorType::get(VecTy->getScalarType(), NumElts / Factor);
718 
719     // ldN/stN only support legal vector types of size 64 or 128 in bits.
720     // Accesses having vector types that are a multiple of 128 bits can be
721     // matched to more than one ldN/stN instruction.
722     if (NumElts % Factor == 0 &&
723         TLI->isLegalInterleavedAccessType(SubVecTy, DL))
724       return Factor * TLI->getNumInterleavedAccesses(SubVecTy, DL);
725   }
726 
727   return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
728                                            Alignment, AddressSpace, CostKind,
729                                            UseMaskForCond, UseMaskForGaps);
730 }
731 
732 int AArch64TTIImpl::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) {
733   int Cost = 0;
734   TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
735   for (auto *I : Tys) {
736     if (!I->isVectorTy())
737       continue;
738     if (I->getScalarSizeInBits() * cast<VectorType>(I)->getNumElements() == 128)
739       Cost += getMemoryOpCost(Instruction::Store, I, Align(128), 0, CostKind) +
740               getMemoryOpCost(Instruction::Load, I, Align(128), 0, CostKind);
741   }
742   return Cost;
743 }
744 
745 unsigned AArch64TTIImpl::getMaxInterleaveFactor(unsigned VF) {
746   return ST->getMaxInterleaveFactor();
747 }
748 
749 // For Falkor, we want to avoid having too many strided loads in a loop since
750 // that can exhaust the HW prefetcher resources.  We adjust the unroller
751 // MaxCount preference below to attempt to ensure unrolling doesn't create too
752 // many strided loads.
753 static void
754 getFalkorUnrollingPreferences(Loop *L, ScalarEvolution &SE,
755                               TargetTransformInfo::UnrollingPreferences &UP) {
756   enum { MaxStridedLoads = 7 };
757   auto countStridedLoads = [](Loop *L, ScalarEvolution &SE) {
758     int StridedLoads = 0;
759     // FIXME? We could make this more precise by looking at the CFG and
760     // e.g. not counting loads in each side of an if-then-else diamond.
761     for (const auto BB : L->blocks()) {
762       for (auto &I : *BB) {
763         LoadInst *LMemI = dyn_cast<LoadInst>(&I);
764         if (!LMemI)
765           continue;
766 
767         Value *PtrValue = LMemI->getPointerOperand();
768         if (L->isLoopInvariant(PtrValue))
769           continue;
770 
771         const SCEV *LSCEV = SE.getSCEV(PtrValue);
772         const SCEVAddRecExpr *LSCEVAddRec = dyn_cast<SCEVAddRecExpr>(LSCEV);
773         if (!LSCEVAddRec || !LSCEVAddRec->isAffine())
774           continue;
775 
776         // FIXME? We could take pairing of unrolled load copies into account
777         // by looking at the AddRec, but we would probably have to limit this
778         // to loops with no stores or other memory optimization barriers.
779         ++StridedLoads;
780         // We've seen enough strided loads that seeing more won't make a
781         // difference.
782         if (StridedLoads > MaxStridedLoads / 2)
783           return StridedLoads;
784       }
785     }
786     return StridedLoads;
787   };
788 
789   int StridedLoads = countStridedLoads(L, SE);
790   LLVM_DEBUG(dbgs() << "falkor-hwpf: detected " << StridedLoads
791                     << " strided loads\n");
792   // Pick the largest power of 2 unroll count that won't result in too many
793   // strided loads.
794   if (StridedLoads) {
795     UP.MaxCount = 1 << Log2_32(MaxStridedLoads / StridedLoads);
796     LLVM_DEBUG(dbgs() << "falkor-hwpf: setting unroll MaxCount to "
797                       << UP.MaxCount << '\n');
798   }
799 }
800 
801 void AArch64TTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
802                                              TTI::UnrollingPreferences &UP) {
803   // Enable partial unrolling and runtime unrolling.
804   BaseT::getUnrollingPreferences(L, SE, UP);
805 
806   // For inner loop, it is more likely to be a hot one, and the runtime check
807   // can be promoted out from LICM pass, so the overhead is less, let's try
808   // a larger threshold to unroll more loops.
809   if (L->getLoopDepth() > 1)
810     UP.PartialThreshold *= 2;
811 
812   // Disable partial & runtime unrolling on -Os.
813   UP.PartialOptSizeThreshold = 0;
814 
815   if (ST->getProcFamily() == AArch64Subtarget::Falkor &&
816       EnableFalkorHWPFUnrollFix)
817     getFalkorUnrollingPreferences(L, SE, UP);
818 }
819 
820 Value *AArch64TTIImpl::getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
821                                                          Type *ExpectedType) {
822   switch (Inst->getIntrinsicID()) {
823   default:
824     return nullptr;
825   case Intrinsic::aarch64_neon_st2:
826   case Intrinsic::aarch64_neon_st3:
827   case Intrinsic::aarch64_neon_st4: {
828     // Create a struct type
829     StructType *ST = dyn_cast<StructType>(ExpectedType);
830     if (!ST)
831       return nullptr;
832     unsigned NumElts = Inst->getNumArgOperands() - 1;
833     if (ST->getNumElements() != NumElts)
834       return nullptr;
835     for (unsigned i = 0, e = NumElts; i != e; ++i) {
836       if (Inst->getArgOperand(i)->getType() != ST->getElementType(i))
837         return nullptr;
838     }
839     Value *Res = UndefValue::get(ExpectedType);
840     IRBuilder<> Builder(Inst);
841     for (unsigned i = 0, e = NumElts; i != e; ++i) {
842       Value *L = Inst->getArgOperand(i);
843       Res = Builder.CreateInsertValue(Res, L, i);
844     }
845     return Res;
846   }
847   case Intrinsic::aarch64_neon_ld2:
848   case Intrinsic::aarch64_neon_ld3:
849   case Intrinsic::aarch64_neon_ld4:
850     if (Inst->getType() == ExpectedType)
851       return Inst;
852     return nullptr;
853   }
854 }
855 
856 bool AArch64TTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst,
857                                         MemIntrinsicInfo &Info) {
858   switch (Inst->getIntrinsicID()) {
859   default:
860     break;
861   case Intrinsic::aarch64_neon_ld2:
862   case Intrinsic::aarch64_neon_ld3:
863   case Intrinsic::aarch64_neon_ld4:
864     Info.ReadMem = true;
865     Info.WriteMem = false;
866     Info.PtrVal = Inst->getArgOperand(0);
867     break;
868   case Intrinsic::aarch64_neon_st2:
869   case Intrinsic::aarch64_neon_st3:
870   case Intrinsic::aarch64_neon_st4:
871     Info.ReadMem = false;
872     Info.WriteMem = true;
873     Info.PtrVal = Inst->getArgOperand(Inst->getNumArgOperands() - 1);
874     break;
875   }
876 
877   switch (Inst->getIntrinsicID()) {
878   default:
879     return false;
880   case Intrinsic::aarch64_neon_ld2:
881   case Intrinsic::aarch64_neon_st2:
882     Info.MatchingId = VECTOR_LDST_TWO_ELEMENTS;
883     break;
884   case Intrinsic::aarch64_neon_ld3:
885   case Intrinsic::aarch64_neon_st3:
886     Info.MatchingId = VECTOR_LDST_THREE_ELEMENTS;
887     break;
888   case Intrinsic::aarch64_neon_ld4:
889   case Intrinsic::aarch64_neon_st4:
890     Info.MatchingId = VECTOR_LDST_FOUR_ELEMENTS;
891     break;
892   }
893   return true;
894 }
895 
896 /// See if \p I should be considered for address type promotion. We check if \p
897 /// I is a sext with right type and used in memory accesses. If it used in a
898 /// "complex" getelementptr, we allow it to be promoted without finding other
899 /// sext instructions that sign extended the same initial value. A getelementptr
900 /// is considered as "complex" if it has more than 2 operands.
901 bool AArch64TTIImpl::shouldConsiderAddressTypePromotion(
902     const Instruction &I, bool &AllowPromotionWithoutCommonHeader) {
903   bool Considerable = false;
904   AllowPromotionWithoutCommonHeader = false;
905   if (!isa<SExtInst>(&I))
906     return false;
907   Type *ConsideredSExtType =
908       Type::getInt64Ty(I.getParent()->getParent()->getContext());
909   if (I.getType() != ConsideredSExtType)
910     return false;
911   // See if the sext is the one with the right type and used in at least one
912   // GetElementPtrInst.
913   for (const User *U : I.users()) {
914     if (const GetElementPtrInst *GEPInst = dyn_cast<GetElementPtrInst>(U)) {
915       Considerable = true;
916       // A getelementptr is considered as "complex" if it has more than 2
917       // operands. We will promote a SExt used in such complex GEP as we
918       // expect some computation to be merged if they are done on 64 bits.
919       if (GEPInst->getNumOperands() > 2) {
920         AllowPromotionWithoutCommonHeader = true;
921         break;
922       }
923     }
924   }
925   return Considerable;
926 }
927 
928 bool AArch64TTIImpl::useReductionIntrinsic(unsigned Opcode, Type *Ty,
929                                            TTI::ReductionFlags Flags) const {
930   auto *VTy = cast<VectorType>(Ty);
931   unsigned ScalarBits = Ty->getScalarSizeInBits();
932   switch (Opcode) {
933   case Instruction::FAdd:
934   case Instruction::FMul:
935   case Instruction::And:
936   case Instruction::Or:
937   case Instruction::Xor:
938   case Instruction::Mul:
939     return false;
940   case Instruction::Add:
941     return ScalarBits * VTy->getNumElements() >= 128;
942   case Instruction::ICmp:
943     return (ScalarBits < 64) && (ScalarBits * VTy->getNumElements() >= 128);
944   case Instruction::FCmp:
945     return Flags.NoNaN;
946   default:
947     llvm_unreachable("Unhandled reduction opcode");
948   }
949   return false;
950 }
951 
952 int AArch64TTIImpl::getArithmeticReductionCost(unsigned Opcode,
953                                                VectorType *ValTy,
954                                                bool IsPairwiseForm,
955                                                TTI::TargetCostKind CostKind) {
956 
957   if (IsPairwiseForm)
958     return BaseT::getArithmeticReductionCost(Opcode, ValTy, IsPairwiseForm,
959                                              CostKind);
960 
961   std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
962   MVT MTy = LT.second;
963   int ISD = TLI->InstructionOpcodeToISD(Opcode);
964   assert(ISD && "Invalid opcode");
965 
966   // Horizontal adds can use the 'addv' instruction. We model the cost of these
967   // instructions as normal vector adds. This is the only arithmetic vector
968   // reduction operation for which we have an instruction.
969   static const CostTblEntry CostTblNoPairwise[]{
970       {ISD::ADD, MVT::v8i8,  1},
971       {ISD::ADD, MVT::v16i8, 1},
972       {ISD::ADD, MVT::v4i16, 1},
973       {ISD::ADD, MVT::v8i16, 1},
974       {ISD::ADD, MVT::v4i32, 1},
975   };
976 
977   if (const auto *Entry = CostTableLookup(CostTblNoPairwise, ISD, MTy))
978     return LT.first * Entry->Cost;
979 
980   return BaseT::getArithmeticReductionCost(Opcode, ValTy, IsPairwiseForm,
981                                            CostKind);
982 }
983 
984 int AArch64TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, VectorType *Tp,
985                                    int Index, VectorType *SubTp) {
986   if (Kind == TTI::SK_Broadcast || Kind == TTI::SK_Transpose ||
987       Kind == TTI::SK_Select || Kind == TTI::SK_PermuteSingleSrc) {
988     static const CostTblEntry ShuffleTbl[] = {
989       // Broadcast shuffle kinds can be performed with 'dup'.
990       { TTI::SK_Broadcast, MVT::v8i8,  1 },
991       { TTI::SK_Broadcast, MVT::v16i8, 1 },
992       { TTI::SK_Broadcast, MVT::v4i16, 1 },
993       { TTI::SK_Broadcast, MVT::v8i16, 1 },
994       { TTI::SK_Broadcast, MVT::v2i32, 1 },
995       { TTI::SK_Broadcast, MVT::v4i32, 1 },
996       { TTI::SK_Broadcast, MVT::v2i64, 1 },
997       { TTI::SK_Broadcast, MVT::v2f32, 1 },
998       { TTI::SK_Broadcast, MVT::v4f32, 1 },
999       { TTI::SK_Broadcast, MVT::v2f64, 1 },
1000       // Transpose shuffle kinds can be performed with 'trn1/trn2' and
1001       // 'zip1/zip2' instructions.
1002       { TTI::SK_Transpose, MVT::v8i8,  1 },
1003       { TTI::SK_Transpose, MVT::v16i8, 1 },
1004       { TTI::SK_Transpose, MVT::v4i16, 1 },
1005       { TTI::SK_Transpose, MVT::v8i16, 1 },
1006       { TTI::SK_Transpose, MVT::v2i32, 1 },
1007       { TTI::SK_Transpose, MVT::v4i32, 1 },
1008       { TTI::SK_Transpose, MVT::v2i64, 1 },
1009       { TTI::SK_Transpose, MVT::v2f32, 1 },
1010       { TTI::SK_Transpose, MVT::v4f32, 1 },
1011       { TTI::SK_Transpose, MVT::v2f64, 1 },
1012       // Select shuffle kinds.
1013       // TODO: handle vXi8/vXi16.
1014       { TTI::SK_Select, MVT::v2i32, 1 }, // mov.
1015       { TTI::SK_Select, MVT::v4i32, 2 }, // rev+trn (or similar).
1016       { TTI::SK_Select, MVT::v2i64, 1 }, // mov.
1017       { TTI::SK_Select, MVT::v2f32, 1 }, // mov.
1018       { TTI::SK_Select, MVT::v4f32, 2 }, // rev+trn (or similar).
1019       { TTI::SK_Select, MVT::v2f64, 1 }, // mov.
1020       // PermuteSingleSrc shuffle kinds.
1021       // TODO: handle vXi8/vXi16.
1022       { TTI::SK_PermuteSingleSrc, MVT::v2i32, 1 }, // mov.
1023       { TTI::SK_PermuteSingleSrc, MVT::v4i32, 3 }, // perfectshuffle worst case.
1024       { TTI::SK_PermuteSingleSrc, MVT::v2i64, 1 }, // mov.
1025       { TTI::SK_PermuteSingleSrc, MVT::v2f32, 1 }, // mov.
1026       { TTI::SK_PermuteSingleSrc, MVT::v4f32, 3 }, // perfectshuffle worst case.
1027       { TTI::SK_PermuteSingleSrc, MVT::v2f64, 1 }, // mov.
1028     };
1029     std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
1030     if (const auto *Entry = CostTableLookup(ShuffleTbl, Kind, LT.second))
1031       return LT.first * Entry->Cost;
1032   }
1033 
1034   return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
1035 }
1036