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