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 "AArch64TargetTransformInfo.h" 10 #include "AArch64ExpandImm.h" 11 #include "MCTargetDesc/AArch64AddressingModes.h" 12 #include "llvm/Analysis/IVDescriptors.h" 13 #include "llvm/Analysis/LoopInfo.h" 14 #include "llvm/Analysis/TargetTransformInfo.h" 15 #include "llvm/CodeGen/BasicTTIImpl.h" 16 #include "llvm/CodeGen/CostTable.h" 17 #include "llvm/CodeGen/TargetLowering.h" 18 #include "llvm/IR/Intrinsics.h" 19 #include "llvm/IR/IntrinsicInst.h" 20 #include "llvm/IR/IntrinsicsAArch64.h" 21 #include "llvm/IR/PatternMatch.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/Transforms/InstCombine/InstCombiner.h" 24 #include <algorithm> 25 using namespace llvm; 26 using namespace llvm::PatternMatch; 27 28 #define DEBUG_TYPE "aarch64tti" 29 30 static cl::opt<bool> EnableFalkorHWPFUnrollFix("enable-falkor-hwpf-unroll-fix", 31 cl::init(true), cl::Hidden); 32 33 bool AArch64TTIImpl::areInlineCompatible(const Function *Caller, 34 const Function *Callee) const { 35 const TargetMachine &TM = getTLI()->getTargetMachine(); 36 37 const FeatureBitset &CallerBits = 38 TM.getSubtargetImpl(*Caller)->getFeatureBits(); 39 const FeatureBitset &CalleeBits = 40 TM.getSubtargetImpl(*Callee)->getFeatureBits(); 41 42 // Inline a callee if its target-features are a subset of the callers 43 // target-features. 44 return (CallerBits & CalleeBits) == CalleeBits; 45 } 46 47 /// Calculate the cost of materializing a 64-bit value. This helper 48 /// method might only calculate a fraction of a larger immediate. Therefore it 49 /// is valid to return a cost of ZERO. 50 InstructionCost AArch64TTIImpl::getIntImmCost(int64_t Val) { 51 // Check if the immediate can be encoded within an instruction. 52 if (Val == 0 || AArch64_AM::isLogicalImmediate(Val, 64)) 53 return 0; 54 55 if (Val < 0) 56 Val = ~Val; 57 58 // Calculate how many moves we will need to materialize this constant. 59 SmallVector<AArch64_IMM::ImmInsnModel, 4> Insn; 60 AArch64_IMM::expandMOVImm(Val, 64, Insn); 61 return Insn.size(); 62 } 63 64 /// Calculate the cost of materializing the given constant. 65 InstructionCost AArch64TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty, 66 TTI::TargetCostKind CostKind) { 67 assert(Ty->isIntegerTy()); 68 69 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 70 if (BitSize == 0) 71 return ~0U; 72 73 // Sign-extend all constants to a multiple of 64-bit. 74 APInt ImmVal = Imm; 75 if (BitSize & 0x3f) 76 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU); 77 78 // Split the constant into 64-bit chunks and calculate the cost for each 79 // chunk. 80 InstructionCost Cost = 0; 81 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) { 82 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64); 83 int64_t Val = Tmp.getSExtValue(); 84 Cost += getIntImmCost(Val); 85 } 86 // We need at least one instruction to materialze the constant. 87 return std::max<InstructionCost>(1, Cost); 88 } 89 90 InstructionCost AArch64TTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx, 91 const APInt &Imm, Type *Ty, 92 TTI::TargetCostKind CostKind, 93 Instruction *Inst) { 94 assert(Ty->isIntegerTy()); 95 96 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 97 // There is no cost model for constants with a bit size of 0. Return TCC_Free 98 // here, so that constant hoisting will ignore this constant. 99 if (BitSize == 0) 100 return TTI::TCC_Free; 101 102 unsigned ImmIdx = ~0U; 103 switch (Opcode) { 104 default: 105 return TTI::TCC_Free; 106 case Instruction::GetElementPtr: 107 // Always hoist the base address of a GetElementPtr. 108 if (Idx == 0) 109 return 2 * TTI::TCC_Basic; 110 return TTI::TCC_Free; 111 case Instruction::Store: 112 ImmIdx = 0; 113 break; 114 case Instruction::Add: 115 case Instruction::Sub: 116 case Instruction::Mul: 117 case Instruction::UDiv: 118 case Instruction::SDiv: 119 case Instruction::URem: 120 case Instruction::SRem: 121 case Instruction::And: 122 case Instruction::Or: 123 case Instruction::Xor: 124 case Instruction::ICmp: 125 ImmIdx = 1; 126 break; 127 // Always return TCC_Free for the shift value of a shift instruction. 128 case Instruction::Shl: 129 case Instruction::LShr: 130 case Instruction::AShr: 131 if (Idx == 1) 132 return TTI::TCC_Free; 133 break; 134 case Instruction::Trunc: 135 case Instruction::ZExt: 136 case Instruction::SExt: 137 case Instruction::IntToPtr: 138 case Instruction::PtrToInt: 139 case Instruction::BitCast: 140 case Instruction::PHI: 141 case Instruction::Call: 142 case Instruction::Select: 143 case Instruction::Ret: 144 case Instruction::Load: 145 break; 146 } 147 148 if (Idx == ImmIdx) { 149 int NumConstants = (BitSize + 63) / 64; 150 InstructionCost Cost = AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind); 151 return (Cost <= NumConstants * TTI::TCC_Basic) 152 ? static_cast<int>(TTI::TCC_Free) 153 : Cost; 154 } 155 return AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind); 156 } 157 158 InstructionCost 159 AArch64TTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, 160 const APInt &Imm, Type *Ty, 161 TTI::TargetCostKind CostKind) { 162 assert(Ty->isIntegerTy()); 163 164 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 165 // There is no cost model for constants with a bit size of 0. Return TCC_Free 166 // here, so that constant hoisting will ignore this constant. 167 if (BitSize == 0) 168 return TTI::TCC_Free; 169 170 // Most (all?) AArch64 intrinsics do not support folding immediates into the 171 // selected instruction, so we compute the materialization cost for the 172 // immediate directly. 173 if (IID >= Intrinsic::aarch64_addg && IID <= Intrinsic::aarch64_udiv) 174 return AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind); 175 176 switch (IID) { 177 default: 178 return TTI::TCC_Free; 179 case Intrinsic::sadd_with_overflow: 180 case Intrinsic::uadd_with_overflow: 181 case Intrinsic::ssub_with_overflow: 182 case Intrinsic::usub_with_overflow: 183 case Intrinsic::smul_with_overflow: 184 case Intrinsic::umul_with_overflow: 185 if (Idx == 1) { 186 int NumConstants = (BitSize + 63) / 64; 187 InstructionCost Cost = AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind); 188 return (Cost <= NumConstants * TTI::TCC_Basic) 189 ? static_cast<int>(TTI::TCC_Free) 190 : Cost; 191 } 192 break; 193 case Intrinsic::experimental_stackmap: 194 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 195 return TTI::TCC_Free; 196 break; 197 case Intrinsic::experimental_patchpoint_void: 198 case Intrinsic::experimental_patchpoint_i64: 199 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 200 return TTI::TCC_Free; 201 break; 202 case Intrinsic::experimental_gc_statepoint: 203 if ((Idx < 5) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 204 return TTI::TCC_Free; 205 break; 206 } 207 return AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind); 208 } 209 210 TargetTransformInfo::PopcntSupportKind 211 AArch64TTIImpl::getPopcntSupport(unsigned TyWidth) { 212 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2"); 213 if (TyWidth == 32 || TyWidth == 64) 214 return TTI::PSK_FastHardware; 215 // TODO: AArch64TargetLowering::LowerCTPOP() supports 128bit popcount. 216 return TTI::PSK_Software; 217 } 218 219 InstructionCost 220 AArch64TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, 221 TTI::TargetCostKind CostKind) { 222 auto *RetTy = ICA.getReturnType(); 223 switch (ICA.getID()) { 224 case Intrinsic::umin: 225 case Intrinsic::umax: 226 case Intrinsic::smin: 227 case Intrinsic::smax: { 228 static const auto ValidMinMaxTys = {MVT::v8i8, MVT::v16i8, MVT::v4i16, 229 MVT::v8i16, MVT::v2i32, MVT::v4i32}; 230 auto LT = TLI->getTypeLegalizationCost(DL, RetTy); 231 // v2i64 types get converted to cmp+bif hence the cost of 2 232 if (LT.second == MVT::v2i64) 233 return LT.first * 2; 234 if (any_of(ValidMinMaxTys, [<](MVT M) { return M == LT.second; })) 235 return LT.first; 236 break; 237 } 238 case Intrinsic::sadd_sat: 239 case Intrinsic::ssub_sat: 240 case Intrinsic::uadd_sat: 241 case Intrinsic::usub_sat: { 242 static const auto ValidSatTys = {MVT::v8i8, MVT::v16i8, MVT::v4i16, 243 MVT::v8i16, MVT::v2i32, MVT::v4i32, 244 MVT::v2i64}; 245 auto LT = TLI->getTypeLegalizationCost(DL, RetTy); 246 // This is a base cost of 1 for the vadd, plus 3 extract shifts if we 247 // need to extend the type, as it uses shr(qadd(shl, shl)). 248 unsigned Instrs = 249 LT.second.getScalarSizeInBits() == RetTy->getScalarSizeInBits() ? 1 : 4; 250 if (any_of(ValidSatTys, [<](MVT M) { return M == LT.second; })) 251 return LT.first * Instrs; 252 break; 253 } 254 case Intrinsic::abs: { 255 static const auto ValidAbsTys = {MVT::v8i8, MVT::v16i8, MVT::v4i16, 256 MVT::v8i16, MVT::v2i32, MVT::v4i32, 257 MVT::v2i64}; 258 auto LT = TLI->getTypeLegalizationCost(DL, RetTy); 259 if (any_of(ValidAbsTys, [<](MVT M) { return M == LT.second; })) 260 return LT.first; 261 break; 262 } 263 case Intrinsic::experimental_stepvector: { 264 InstructionCost Cost = 1; // Cost of the `index' instruction 265 auto LT = TLI->getTypeLegalizationCost(DL, RetTy); 266 // Legalisation of illegal vectors involves an `index' instruction plus 267 // (LT.first - 1) vector adds. 268 if (LT.first > 1) { 269 Type *LegalVTy = EVT(LT.second).getTypeForEVT(RetTy->getContext()); 270 InstructionCost AddCost = 271 getArithmeticInstrCost(Instruction::Add, LegalVTy, CostKind); 272 Cost += AddCost * (LT.first - 1); 273 } 274 return Cost; 275 } 276 case Intrinsic::bitreverse: { 277 static const CostTblEntry BitreverseTbl[] = { 278 {Intrinsic::bitreverse, MVT::i32, 1}, 279 {Intrinsic::bitreverse, MVT::i64, 1}, 280 {Intrinsic::bitreverse, MVT::v8i8, 1}, 281 {Intrinsic::bitreverse, MVT::v16i8, 1}, 282 {Intrinsic::bitreverse, MVT::v4i16, 2}, 283 {Intrinsic::bitreverse, MVT::v8i16, 2}, 284 {Intrinsic::bitreverse, MVT::v2i32, 2}, 285 {Intrinsic::bitreverse, MVT::v4i32, 2}, 286 {Intrinsic::bitreverse, MVT::v1i64, 2}, 287 {Intrinsic::bitreverse, MVT::v2i64, 2}, 288 }; 289 const auto LegalisationCost = TLI->getTypeLegalizationCost(DL, RetTy); 290 const auto *Entry = 291 CostTableLookup(BitreverseTbl, ICA.getID(), LegalisationCost.second); 292 if (Entry) { 293 // Cost Model is using the legal type(i32) that i8 and i16 will be 294 // converted to +1 so that we match the actual lowering cost 295 if (TLI->getValueType(DL, RetTy, true) == MVT::i8 || 296 TLI->getValueType(DL, RetTy, true) == MVT::i16) 297 return LegalisationCost.first * Entry->Cost + 1; 298 299 return LegalisationCost.first * Entry->Cost; 300 } 301 break; 302 } 303 case Intrinsic::ctpop: { 304 static const CostTblEntry CtpopCostTbl[] = { 305 {ISD::CTPOP, MVT::v2i64, 4}, 306 {ISD::CTPOP, MVT::v4i32, 3}, 307 {ISD::CTPOP, MVT::v8i16, 2}, 308 {ISD::CTPOP, MVT::v16i8, 1}, 309 {ISD::CTPOP, MVT::i64, 4}, 310 {ISD::CTPOP, MVT::v2i32, 3}, 311 {ISD::CTPOP, MVT::v4i16, 2}, 312 {ISD::CTPOP, MVT::v8i8, 1}, 313 {ISD::CTPOP, MVT::i32, 5}, 314 }; 315 auto LT = TLI->getTypeLegalizationCost(DL, RetTy); 316 MVT MTy = LT.second; 317 if (const auto *Entry = CostTableLookup(CtpopCostTbl, ISD::CTPOP, MTy)) { 318 // Extra cost of +1 when illegal vector types are legalized by promoting 319 // the integer type. 320 int ExtraCost = MTy.isVector() && MTy.getScalarSizeInBits() != 321 RetTy->getScalarSizeInBits() 322 ? 1 323 : 0; 324 return LT.first * Entry->Cost + ExtraCost; 325 } 326 break; 327 } 328 default: 329 break; 330 } 331 return BaseT::getIntrinsicInstrCost(ICA, CostKind); 332 } 333 334 /// The function will remove redundant reinterprets casting in the presence 335 /// of the control flow 336 static Optional<Instruction *> processPhiNode(InstCombiner &IC, 337 IntrinsicInst &II) { 338 SmallVector<Instruction *, 32> Worklist; 339 auto RequiredType = II.getType(); 340 341 auto *PN = dyn_cast<PHINode>(II.getArgOperand(0)); 342 assert(PN && "Expected Phi Node!"); 343 344 // Don't create a new Phi unless we can remove the old one. 345 if (!PN->hasOneUse()) 346 return None; 347 348 for (Value *IncValPhi : PN->incoming_values()) { 349 auto *Reinterpret = dyn_cast<IntrinsicInst>(IncValPhi); 350 if (!Reinterpret || 351 Reinterpret->getIntrinsicID() != 352 Intrinsic::aarch64_sve_convert_to_svbool || 353 RequiredType != Reinterpret->getArgOperand(0)->getType()) 354 return None; 355 } 356 357 // Create the new Phi 358 LLVMContext &Ctx = PN->getContext(); 359 IRBuilder<> Builder(Ctx); 360 Builder.SetInsertPoint(PN); 361 PHINode *NPN = Builder.CreatePHI(RequiredType, PN->getNumIncomingValues()); 362 Worklist.push_back(PN); 363 364 for (unsigned I = 0; I < PN->getNumIncomingValues(); I++) { 365 auto *Reinterpret = cast<Instruction>(PN->getIncomingValue(I)); 366 NPN->addIncoming(Reinterpret->getOperand(0), PN->getIncomingBlock(I)); 367 Worklist.push_back(Reinterpret); 368 } 369 370 // Cleanup Phi Node and reinterprets 371 return IC.replaceInstUsesWith(II, NPN); 372 } 373 374 static Optional<Instruction *> instCombineConvertFromSVBool(InstCombiner &IC, 375 IntrinsicInst &II) { 376 // If the reinterpret instruction operand is a PHI Node 377 if (isa<PHINode>(II.getArgOperand(0))) 378 return processPhiNode(IC, II); 379 380 SmallVector<Instruction *, 32> CandidatesForRemoval; 381 Value *Cursor = II.getOperand(0), *EarliestReplacement = nullptr; 382 383 const auto *IVTy = cast<VectorType>(II.getType()); 384 385 // Walk the chain of conversions. 386 while (Cursor) { 387 // If the type of the cursor has fewer lanes than the final result, zeroing 388 // must take place, which breaks the equivalence chain. 389 const auto *CursorVTy = cast<VectorType>(Cursor->getType()); 390 if (CursorVTy->getElementCount().getKnownMinValue() < 391 IVTy->getElementCount().getKnownMinValue()) 392 break; 393 394 // If the cursor has the same type as I, it is a viable replacement. 395 if (Cursor->getType() == IVTy) 396 EarliestReplacement = Cursor; 397 398 auto *IntrinsicCursor = dyn_cast<IntrinsicInst>(Cursor); 399 400 // If this is not an SVE conversion intrinsic, this is the end of the chain. 401 if (!IntrinsicCursor || !(IntrinsicCursor->getIntrinsicID() == 402 Intrinsic::aarch64_sve_convert_to_svbool || 403 IntrinsicCursor->getIntrinsicID() == 404 Intrinsic::aarch64_sve_convert_from_svbool)) 405 break; 406 407 CandidatesForRemoval.insert(CandidatesForRemoval.begin(), IntrinsicCursor); 408 Cursor = IntrinsicCursor->getOperand(0); 409 } 410 411 // If no viable replacement in the conversion chain was found, there is 412 // nothing to do. 413 if (!EarliestReplacement) 414 return None; 415 416 return IC.replaceInstUsesWith(II, EarliestReplacement); 417 } 418 419 static Optional<Instruction *> instCombineSVEDup(InstCombiner &IC, 420 IntrinsicInst &II) { 421 IntrinsicInst *Pg = dyn_cast<IntrinsicInst>(II.getArgOperand(1)); 422 if (!Pg) 423 return None; 424 425 if (Pg->getIntrinsicID() != Intrinsic::aarch64_sve_ptrue) 426 return None; 427 428 const auto PTruePattern = 429 cast<ConstantInt>(Pg->getOperand(0))->getZExtValue(); 430 if (PTruePattern != AArch64SVEPredPattern::vl1) 431 return None; 432 433 // The intrinsic is inserting into lane zero so use an insert instead. 434 auto *IdxTy = Type::getInt64Ty(II.getContext()); 435 auto *Insert = InsertElementInst::Create( 436 II.getArgOperand(0), II.getArgOperand(2), ConstantInt::get(IdxTy, 0)); 437 Insert->insertBefore(&II); 438 Insert->takeName(&II); 439 440 return IC.replaceInstUsesWith(II, Insert); 441 } 442 443 static Optional<Instruction *> instCombineSVEDupX(InstCombiner &IC, 444 IntrinsicInst &II) { 445 // Replace DupX with a regular IR splat. 446 IRBuilder<> Builder(II.getContext()); 447 Builder.SetInsertPoint(&II); 448 auto *RetTy = cast<ScalableVectorType>(II.getType()); 449 Value *Splat = 450 Builder.CreateVectorSplat(RetTy->getElementCount(), II.getArgOperand(0)); 451 Splat->takeName(&II); 452 return IC.replaceInstUsesWith(II, Splat); 453 } 454 455 static Optional<Instruction *> instCombineSVECmpNE(InstCombiner &IC, 456 IntrinsicInst &II) { 457 LLVMContext &Ctx = II.getContext(); 458 IRBuilder<> Builder(Ctx); 459 Builder.SetInsertPoint(&II); 460 461 // Check that the predicate is all active 462 auto *Pg = dyn_cast<IntrinsicInst>(II.getArgOperand(0)); 463 if (!Pg || Pg->getIntrinsicID() != Intrinsic::aarch64_sve_ptrue) 464 return None; 465 466 const auto PTruePattern = 467 cast<ConstantInt>(Pg->getOperand(0))->getZExtValue(); 468 if (PTruePattern != AArch64SVEPredPattern::all) 469 return None; 470 471 // Check that we have a compare of zero.. 472 auto *SplatValue = 473 dyn_cast_or_null<ConstantInt>(getSplatValue(II.getArgOperand(2))); 474 if (!SplatValue || !SplatValue->isZero()) 475 return None; 476 477 // ..against a dupq 478 auto *DupQLane = dyn_cast<IntrinsicInst>(II.getArgOperand(1)); 479 if (!DupQLane || 480 DupQLane->getIntrinsicID() != Intrinsic::aarch64_sve_dupq_lane) 481 return None; 482 483 // Where the dupq is a lane 0 replicate of a vector insert 484 if (!cast<ConstantInt>(DupQLane->getArgOperand(1))->isZero()) 485 return None; 486 487 auto *VecIns = dyn_cast<IntrinsicInst>(DupQLane->getArgOperand(0)); 488 if (!VecIns || 489 VecIns->getIntrinsicID() != Intrinsic::experimental_vector_insert) 490 return None; 491 492 // Where the vector insert is a fixed constant vector insert into undef at 493 // index zero 494 if (!isa<UndefValue>(VecIns->getArgOperand(0))) 495 return None; 496 497 if (!cast<ConstantInt>(VecIns->getArgOperand(2))->isZero()) 498 return None; 499 500 auto *ConstVec = dyn_cast<Constant>(VecIns->getArgOperand(1)); 501 if (!ConstVec) 502 return None; 503 504 auto *VecTy = dyn_cast<FixedVectorType>(ConstVec->getType()); 505 auto *OutTy = dyn_cast<ScalableVectorType>(II.getType()); 506 if (!VecTy || !OutTy || VecTy->getNumElements() != OutTy->getMinNumElements()) 507 return None; 508 509 unsigned NumElts = VecTy->getNumElements(); 510 unsigned PredicateBits = 0; 511 512 // Expand intrinsic operands to a 16-bit byte level predicate 513 for (unsigned I = 0; I < NumElts; ++I) { 514 auto *Arg = dyn_cast<ConstantInt>(ConstVec->getAggregateElement(I)); 515 if (!Arg) 516 return None; 517 if (!Arg->isZero()) 518 PredicateBits |= 1 << (I * (16 / NumElts)); 519 } 520 521 // If all bits are zero bail early with an empty predicate 522 if (PredicateBits == 0) { 523 auto *PFalse = Constant::getNullValue(II.getType()); 524 PFalse->takeName(&II); 525 return IC.replaceInstUsesWith(II, PFalse); 526 } 527 528 // Calculate largest predicate type used (where byte predicate is largest) 529 unsigned Mask = 8; 530 for (unsigned I = 0; I < 16; ++I) 531 if ((PredicateBits & (1 << I)) != 0) 532 Mask |= (I % 8); 533 534 unsigned PredSize = Mask & -Mask; 535 auto *PredType = ScalableVectorType::get( 536 Type::getInt1Ty(Ctx), AArch64::SVEBitsPerBlock / (PredSize * 8)); 537 538 // Ensure all relevant bits are set 539 for (unsigned I = 0; I < 16; I += PredSize) 540 if ((PredicateBits & (1 << I)) == 0) 541 return None; 542 543 auto *PTruePat = 544 ConstantInt::get(Type::getInt32Ty(Ctx), AArch64SVEPredPattern::all); 545 auto *PTrue = Builder.CreateIntrinsic(Intrinsic::aarch64_sve_ptrue, 546 {PredType}, {PTruePat}); 547 auto *ConvertToSVBool = Builder.CreateIntrinsic( 548 Intrinsic::aarch64_sve_convert_to_svbool, {PredType}, {PTrue}); 549 auto *ConvertFromSVBool = 550 Builder.CreateIntrinsic(Intrinsic::aarch64_sve_convert_from_svbool, 551 {II.getType()}, {ConvertToSVBool}); 552 553 ConvertFromSVBool->takeName(&II); 554 return IC.replaceInstUsesWith(II, ConvertFromSVBool); 555 } 556 557 static Optional<Instruction *> instCombineSVELast(InstCombiner &IC, 558 IntrinsicInst &II) { 559 IRBuilder<> Builder(II.getContext()); 560 Builder.SetInsertPoint(&II); 561 Value *Pg = II.getArgOperand(0); 562 Value *Vec = II.getArgOperand(1); 563 auto IntrinsicID = II.getIntrinsicID(); 564 bool IsAfter = IntrinsicID == Intrinsic::aarch64_sve_lasta; 565 566 // lastX(splat(X)) --> X 567 if (auto *SplatVal = getSplatValue(Vec)) 568 return IC.replaceInstUsesWith(II, SplatVal); 569 570 // If x and/or y is a splat value then: 571 // lastX (binop (x, y)) --> binop(lastX(x), lastX(y)) 572 Value *LHS, *RHS; 573 if (match(Vec, m_OneUse(m_BinOp(m_Value(LHS), m_Value(RHS))))) { 574 if (isSplatValue(LHS) || isSplatValue(RHS)) { 575 auto *OldBinOp = cast<BinaryOperator>(Vec); 576 auto OpC = OldBinOp->getOpcode(); 577 auto *NewLHS = 578 Builder.CreateIntrinsic(IntrinsicID, {Vec->getType()}, {Pg, LHS}); 579 auto *NewRHS = 580 Builder.CreateIntrinsic(IntrinsicID, {Vec->getType()}, {Pg, RHS}); 581 auto *NewBinOp = BinaryOperator::CreateWithCopiedFlags( 582 OpC, NewLHS, NewRHS, OldBinOp, OldBinOp->getName(), &II); 583 return IC.replaceInstUsesWith(II, NewBinOp); 584 } 585 } 586 587 auto *C = dyn_cast<Constant>(Pg); 588 if (IsAfter && C && C->isNullValue()) { 589 // The intrinsic is extracting lane 0 so use an extract instead. 590 auto *IdxTy = Type::getInt64Ty(II.getContext()); 591 auto *Extract = ExtractElementInst::Create(Vec, ConstantInt::get(IdxTy, 0)); 592 Extract->insertBefore(&II); 593 Extract->takeName(&II); 594 return IC.replaceInstUsesWith(II, Extract); 595 } 596 597 auto *IntrPG = dyn_cast<IntrinsicInst>(Pg); 598 if (!IntrPG) 599 return None; 600 601 if (IntrPG->getIntrinsicID() != Intrinsic::aarch64_sve_ptrue) 602 return None; 603 604 const auto PTruePattern = 605 cast<ConstantInt>(IntrPG->getOperand(0))->getZExtValue(); 606 607 // Can the intrinsic's predicate be converted to a known constant index? 608 unsigned MinNumElts = getNumElementsFromSVEPredPattern(PTruePattern); 609 if (!MinNumElts) 610 return None; 611 612 unsigned Idx = MinNumElts - 1; 613 // Increment the index if extracting the element after the last active 614 // predicate element. 615 if (IsAfter) 616 ++Idx; 617 618 // Ignore extracts whose index is larger than the known minimum vector 619 // length. NOTE: This is an artificial constraint where we prefer to 620 // maintain what the user asked for until an alternative is proven faster. 621 auto *PgVTy = cast<ScalableVectorType>(Pg->getType()); 622 if (Idx >= PgVTy->getMinNumElements()) 623 return None; 624 625 // The intrinsic is extracting a fixed lane so use an extract instead. 626 auto *IdxTy = Type::getInt64Ty(II.getContext()); 627 auto *Extract = ExtractElementInst::Create(Vec, ConstantInt::get(IdxTy, Idx)); 628 Extract->insertBefore(&II); 629 Extract->takeName(&II); 630 return IC.replaceInstUsesWith(II, Extract); 631 } 632 633 static Optional<Instruction *> instCombineRDFFR(InstCombiner &IC, 634 IntrinsicInst &II) { 635 LLVMContext &Ctx = II.getContext(); 636 IRBuilder<> Builder(Ctx); 637 Builder.SetInsertPoint(&II); 638 // Replace rdffr with predicated rdffr.z intrinsic, so that optimizePTestInstr 639 // can work with RDFFR_PP for ptest elimination. 640 auto *AllPat = 641 ConstantInt::get(Type::getInt32Ty(Ctx), AArch64SVEPredPattern::all); 642 auto *PTrue = Builder.CreateIntrinsic(Intrinsic::aarch64_sve_ptrue, 643 {II.getType()}, {AllPat}); 644 auto *RDFFR = 645 Builder.CreateIntrinsic(Intrinsic::aarch64_sve_rdffr_z, {}, {PTrue}); 646 RDFFR->takeName(&II); 647 return IC.replaceInstUsesWith(II, RDFFR); 648 } 649 650 static Optional<Instruction *> 651 instCombineSVECntElts(InstCombiner &IC, IntrinsicInst &II, unsigned NumElts) { 652 const auto Pattern = cast<ConstantInt>(II.getArgOperand(0))->getZExtValue(); 653 654 if (Pattern == AArch64SVEPredPattern::all) { 655 LLVMContext &Ctx = II.getContext(); 656 IRBuilder<> Builder(Ctx); 657 Builder.SetInsertPoint(&II); 658 659 Constant *StepVal = ConstantInt::get(II.getType(), NumElts); 660 auto *VScale = Builder.CreateVScale(StepVal); 661 VScale->takeName(&II); 662 return IC.replaceInstUsesWith(II, VScale); 663 } 664 665 unsigned MinNumElts = getNumElementsFromSVEPredPattern(Pattern); 666 667 return MinNumElts && NumElts >= MinNumElts 668 ? Optional<Instruction *>(IC.replaceInstUsesWith( 669 II, ConstantInt::get(II.getType(), MinNumElts))) 670 : None; 671 } 672 673 static Optional<Instruction *> instCombineSVEPTest(InstCombiner &IC, 674 IntrinsicInst &II) { 675 IntrinsicInst *Op1 = dyn_cast<IntrinsicInst>(II.getArgOperand(0)); 676 IntrinsicInst *Op2 = dyn_cast<IntrinsicInst>(II.getArgOperand(1)); 677 678 if (Op1 && Op2 && 679 Op1->getIntrinsicID() == Intrinsic::aarch64_sve_convert_to_svbool && 680 Op2->getIntrinsicID() == Intrinsic::aarch64_sve_convert_to_svbool && 681 Op1->getArgOperand(0)->getType() == Op2->getArgOperand(0)->getType()) { 682 683 IRBuilder<> Builder(II.getContext()); 684 Builder.SetInsertPoint(&II); 685 686 Value *Ops[] = {Op1->getArgOperand(0), Op2->getArgOperand(0)}; 687 Type *Tys[] = {Op1->getArgOperand(0)->getType()}; 688 689 auto *PTest = Builder.CreateIntrinsic(II.getIntrinsicID(), Tys, Ops); 690 691 PTest->takeName(&II); 692 return IC.replaceInstUsesWith(II, PTest); 693 } 694 695 return None; 696 } 697 698 static Optional<Instruction *> instCombineSVEVectorFMLA(InstCombiner &IC, 699 IntrinsicInst &II) { 700 // fold (fadd p a (fmul p b c)) -> (fma p a b c) 701 Value *P = II.getOperand(0); 702 Value *A = II.getOperand(1); 703 auto FMul = II.getOperand(2); 704 Value *B, *C; 705 if (!match(FMul, m_Intrinsic<Intrinsic::aarch64_sve_fmul>( 706 m_Specific(P), m_Value(B), m_Value(C)))) 707 return None; 708 709 if (!FMul->hasOneUse()) 710 return None; 711 712 llvm::FastMathFlags FAddFlags = II.getFastMathFlags(); 713 // Stop the combine when the flags on the inputs differ in case dropping flags 714 // would lead to us missing out on more beneficial optimizations. 715 if (FAddFlags != cast<CallInst>(FMul)->getFastMathFlags()) 716 return None; 717 if (!FAddFlags.allowContract()) 718 return None; 719 720 IRBuilder<> Builder(II.getContext()); 721 Builder.SetInsertPoint(&II); 722 auto FMLA = Builder.CreateIntrinsic(Intrinsic::aarch64_sve_fmla, 723 {II.getType()}, {P, A, B, C}, &II); 724 FMLA->setFastMathFlags(FAddFlags); 725 return IC.replaceInstUsesWith(II, FMLA); 726 } 727 728 static Instruction::BinaryOps intrinsicIDToBinOpCode(unsigned Intrinsic) { 729 switch (Intrinsic) { 730 case Intrinsic::aarch64_sve_fmul: 731 return Instruction::BinaryOps::FMul; 732 case Intrinsic::aarch64_sve_fadd: 733 return Instruction::BinaryOps::FAdd; 734 case Intrinsic::aarch64_sve_fsub: 735 return Instruction::BinaryOps::FSub; 736 default: 737 return Instruction::BinaryOpsEnd; 738 } 739 } 740 741 static Optional<Instruction *> instCombineSVEVectorBinOp(InstCombiner &IC, 742 IntrinsicInst &II) { 743 auto *OpPredicate = II.getOperand(0); 744 auto BinOpCode = intrinsicIDToBinOpCode(II.getIntrinsicID()); 745 if (BinOpCode == Instruction::BinaryOpsEnd || 746 !match(OpPredicate, m_Intrinsic<Intrinsic::aarch64_sve_ptrue>( 747 m_ConstantInt<AArch64SVEPredPattern::all>()))) 748 return None; 749 IRBuilder<> Builder(II.getContext()); 750 Builder.SetInsertPoint(&II); 751 Builder.setFastMathFlags(II.getFastMathFlags()); 752 auto BinOp = 753 Builder.CreateBinOp(BinOpCode, II.getOperand(1), II.getOperand(2)); 754 return IC.replaceInstUsesWith(II, BinOp); 755 } 756 757 static Optional<Instruction *> instCombineSVEVectorFAdd(InstCombiner &IC, 758 IntrinsicInst &II) { 759 if (auto FMLA = instCombineSVEVectorFMLA(IC, II)) 760 return FMLA; 761 return instCombineSVEVectorBinOp(IC, II); 762 } 763 764 static Optional<Instruction *> instCombineSVEVectorMul(InstCombiner &IC, 765 IntrinsicInst &II) { 766 auto *OpPredicate = II.getOperand(0); 767 auto *OpMultiplicand = II.getOperand(1); 768 auto *OpMultiplier = II.getOperand(2); 769 770 IRBuilder<> Builder(II.getContext()); 771 Builder.SetInsertPoint(&II); 772 773 // Return true if a given instruction is a unit splat value, false otherwise. 774 auto IsUnitSplat = [](auto *I) { 775 auto *SplatValue = getSplatValue(I); 776 if (!SplatValue) 777 return false; 778 return match(SplatValue, m_FPOne()) || match(SplatValue, m_One()); 779 }; 780 781 // Return true if a given instruction is an aarch64_sve_dup intrinsic call 782 // with a unit splat value, false otherwise. 783 auto IsUnitDup = [](auto *I) { 784 auto *IntrI = dyn_cast<IntrinsicInst>(I); 785 if (!IntrI || IntrI->getIntrinsicID() != Intrinsic::aarch64_sve_dup) 786 return false; 787 788 auto *SplatValue = IntrI->getOperand(2); 789 return match(SplatValue, m_FPOne()) || match(SplatValue, m_One()); 790 }; 791 792 // The OpMultiplier variable should always point to the dup (if any), so 793 // swap if necessary. 794 if (IsUnitDup(OpMultiplicand) || IsUnitSplat(OpMultiplicand)) 795 std::swap(OpMultiplier, OpMultiplicand); 796 797 if (IsUnitSplat(OpMultiplier)) { 798 // [f]mul pg (dupx 1) %n => %n 799 OpMultiplicand->takeName(&II); 800 return IC.replaceInstUsesWith(II, OpMultiplicand); 801 } else if (IsUnitDup(OpMultiplier)) { 802 // [f]mul pg (dup pg 1) %n => %n 803 auto *DupInst = cast<IntrinsicInst>(OpMultiplier); 804 auto *DupPg = DupInst->getOperand(1); 805 // TODO: this is naive. The optimization is still valid if DupPg 806 // 'encompasses' OpPredicate, not only if they're the same predicate. 807 if (OpPredicate == DupPg) { 808 OpMultiplicand->takeName(&II); 809 return IC.replaceInstUsesWith(II, OpMultiplicand); 810 } 811 } 812 813 return instCombineSVEVectorBinOp(IC, II); 814 } 815 816 static Optional<Instruction *> instCombineSVEUnpack(InstCombiner &IC, 817 IntrinsicInst &II) { 818 IRBuilder<> Builder(II.getContext()); 819 Builder.SetInsertPoint(&II); 820 Value *UnpackArg = II.getArgOperand(0); 821 auto *RetTy = cast<ScalableVectorType>(II.getType()); 822 bool IsSigned = II.getIntrinsicID() == Intrinsic::aarch64_sve_sunpkhi || 823 II.getIntrinsicID() == Intrinsic::aarch64_sve_sunpklo; 824 825 // Hi = uunpkhi(splat(X)) --> Hi = splat(extend(X)) 826 // Lo = uunpklo(splat(X)) --> Lo = splat(extend(X)) 827 if (auto *ScalarArg = getSplatValue(UnpackArg)) { 828 ScalarArg = 829 Builder.CreateIntCast(ScalarArg, RetTy->getScalarType(), IsSigned); 830 Value *NewVal = 831 Builder.CreateVectorSplat(RetTy->getElementCount(), ScalarArg); 832 NewVal->takeName(&II); 833 return IC.replaceInstUsesWith(II, NewVal); 834 } 835 836 return None; 837 } 838 static Optional<Instruction *> instCombineSVETBL(InstCombiner &IC, 839 IntrinsicInst &II) { 840 auto *OpVal = II.getOperand(0); 841 auto *OpIndices = II.getOperand(1); 842 VectorType *VTy = cast<VectorType>(II.getType()); 843 844 // Check whether OpIndices is a constant splat value < minimal element count 845 // of result. 846 auto *SplatValue = dyn_cast_or_null<ConstantInt>(getSplatValue(OpIndices)); 847 if (!SplatValue || 848 SplatValue->getValue().uge(VTy->getElementCount().getKnownMinValue())) 849 return None; 850 851 // Convert sve_tbl(OpVal sve_dup_x(SplatValue)) to 852 // splat_vector(extractelement(OpVal, SplatValue)) for further optimization. 853 IRBuilder<> Builder(II.getContext()); 854 Builder.SetInsertPoint(&II); 855 auto *Extract = Builder.CreateExtractElement(OpVal, SplatValue); 856 auto *VectorSplat = 857 Builder.CreateVectorSplat(VTy->getElementCount(), Extract); 858 859 VectorSplat->takeName(&II); 860 return IC.replaceInstUsesWith(II, VectorSplat); 861 } 862 863 static Optional<Instruction *> instCombineSVETupleGet(InstCombiner &IC, 864 IntrinsicInst &II) { 865 // Try to remove sequences of tuple get/set. 866 Value *SetTuple, *SetIndex, *SetValue; 867 auto *GetTuple = II.getArgOperand(0); 868 auto *GetIndex = II.getArgOperand(1); 869 // Check that we have tuple_get(GetTuple, GetIndex) where GetTuple is a 870 // call to tuple_set i.e. tuple_set(SetTuple, SetIndex, SetValue). 871 // Make sure that the types of the current intrinsic and SetValue match 872 // in order to safely remove the sequence. 873 if (!match(GetTuple, 874 m_Intrinsic<Intrinsic::aarch64_sve_tuple_set>( 875 m_Value(SetTuple), m_Value(SetIndex), m_Value(SetValue))) || 876 SetValue->getType() != II.getType()) 877 return None; 878 // Case where we get the same index right after setting it. 879 // tuple_get(tuple_set(SetTuple, SetIndex, SetValue), GetIndex) --> SetValue 880 if (GetIndex == SetIndex) 881 return IC.replaceInstUsesWith(II, SetValue); 882 // If we are getting a different index than what was set in the tuple_set 883 // intrinsic. We can just set the input tuple to the one up in the chain. 884 // tuple_get(tuple_set(SetTuple, SetIndex, SetValue), GetIndex) 885 // --> tuple_get(SetTuple, GetIndex) 886 return IC.replaceOperand(II, 0, SetTuple); 887 } 888 889 static Optional<Instruction *> instCombineSVEZip(InstCombiner &IC, 890 IntrinsicInst &II) { 891 // zip1(uzp1(A, B), uzp2(A, B)) --> A 892 // zip2(uzp1(A, B), uzp2(A, B)) --> B 893 Value *A, *B; 894 if (match(II.getArgOperand(0), 895 m_Intrinsic<Intrinsic::aarch64_sve_uzp1>(m_Value(A), m_Value(B))) && 896 match(II.getArgOperand(1), m_Intrinsic<Intrinsic::aarch64_sve_uzp2>( 897 m_Specific(A), m_Specific(B)))) 898 return IC.replaceInstUsesWith( 899 II, (II.getIntrinsicID() == Intrinsic::aarch64_sve_zip1 ? A : B)); 900 901 return None; 902 } 903 904 static Optional<Instruction *> instCombineLD1GatherIndex(InstCombiner &IC, 905 IntrinsicInst &II) { 906 Value *Mask = II.getOperand(0); 907 Value *BasePtr = II.getOperand(1); 908 Value *Index = II.getOperand(2); 909 Type *Ty = II.getType(); 910 Type *BasePtrTy = BasePtr->getType(); 911 Value *PassThru = ConstantAggregateZero::get(Ty); 912 913 // Contiguous gather => masked load. 914 // (sve.ld1.gather.index Mask BasePtr (sve.index IndexBase 1)) 915 // => (masked.load (gep BasePtr IndexBase) Align Mask zeroinitializer) 916 Value *IndexBase; 917 if (match(Index, m_Intrinsic<Intrinsic::aarch64_sve_index>( 918 m_Value(IndexBase), m_SpecificInt(1)))) { 919 IRBuilder<> Builder(II.getContext()); 920 Builder.SetInsertPoint(&II); 921 922 Align Alignment = 923 BasePtr->getPointerAlignment(II.getModule()->getDataLayout()); 924 925 Type *VecPtrTy = PointerType::getUnqual(Ty); 926 Value *Ptr = Builder.CreateGEP(BasePtrTy->getPointerElementType(), BasePtr, 927 IndexBase); 928 Ptr = Builder.CreateBitCast(Ptr, VecPtrTy); 929 CallInst *MaskedLoad = 930 Builder.CreateMaskedLoad(Ty, Ptr, Alignment, Mask, PassThru); 931 MaskedLoad->takeName(&II); 932 return IC.replaceInstUsesWith(II, MaskedLoad); 933 } 934 935 return None; 936 } 937 938 static Optional<Instruction *> instCombineST1ScatterIndex(InstCombiner &IC, 939 IntrinsicInst &II) { 940 Value *Val = II.getOperand(0); 941 Value *Mask = II.getOperand(1); 942 Value *BasePtr = II.getOperand(2); 943 Value *Index = II.getOperand(3); 944 Type *Ty = Val->getType(); 945 Type *BasePtrTy = BasePtr->getType(); 946 947 // Contiguous scatter => masked store. 948 // (sve.ld1.scatter.index Value Mask BasePtr (sve.index IndexBase 1)) 949 // => (masked.store Value (gep BasePtr IndexBase) Align Mask) 950 Value *IndexBase; 951 if (match(Index, m_Intrinsic<Intrinsic::aarch64_sve_index>( 952 m_Value(IndexBase), m_SpecificInt(1)))) { 953 IRBuilder<> Builder(II.getContext()); 954 Builder.SetInsertPoint(&II); 955 956 Align Alignment = 957 BasePtr->getPointerAlignment(II.getModule()->getDataLayout()); 958 959 Value *Ptr = Builder.CreateGEP(BasePtrTy->getPointerElementType(), BasePtr, 960 IndexBase); 961 Type *VecPtrTy = PointerType::getUnqual(Ty); 962 Ptr = Builder.CreateBitCast(Ptr, VecPtrTy); 963 964 (void)Builder.CreateMaskedStore(Val, Ptr, Alignment, Mask); 965 966 return IC.eraseInstFromFunction(II); 967 } 968 969 return None; 970 } 971 972 Optional<Instruction *> 973 AArch64TTIImpl::instCombineIntrinsic(InstCombiner &IC, 974 IntrinsicInst &II) const { 975 Intrinsic::ID IID = II.getIntrinsicID(); 976 switch (IID) { 977 default: 978 break; 979 case Intrinsic::aarch64_sve_convert_from_svbool: 980 return instCombineConvertFromSVBool(IC, II); 981 case Intrinsic::aarch64_sve_dup: 982 return instCombineSVEDup(IC, II); 983 case Intrinsic::aarch64_sve_dup_x: 984 return instCombineSVEDupX(IC, II); 985 case Intrinsic::aarch64_sve_cmpne: 986 case Intrinsic::aarch64_sve_cmpne_wide: 987 return instCombineSVECmpNE(IC, II); 988 case Intrinsic::aarch64_sve_rdffr: 989 return instCombineRDFFR(IC, II); 990 case Intrinsic::aarch64_sve_lasta: 991 case Intrinsic::aarch64_sve_lastb: 992 return instCombineSVELast(IC, II); 993 case Intrinsic::aarch64_sve_cntd: 994 return instCombineSVECntElts(IC, II, 2); 995 case Intrinsic::aarch64_sve_cntw: 996 return instCombineSVECntElts(IC, II, 4); 997 case Intrinsic::aarch64_sve_cnth: 998 return instCombineSVECntElts(IC, II, 8); 999 case Intrinsic::aarch64_sve_cntb: 1000 return instCombineSVECntElts(IC, II, 16); 1001 case Intrinsic::aarch64_sve_ptest_any: 1002 case Intrinsic::aarch64_sve_ptest_first: 1003 case Intrinsic::aarch64_sve_ptest_last: 1004 return instCombineSVEPTest(IC, II); 1005 case Intrinsic::aarch64_sve_mul: 1006 case Intrinsic::aarch64_sve_fmul: 1007 return instCombineSVEVectorMul(IC, II); 1008 case Intrinsic::aarch64_sve_fadd: 1009 return instCombineSVEVectorFAdd(IC, II); 1010 case Intrinsic::aarch64_sve_fsub: 1011 return instCombineSVEVectorBinOp(IC, II); 1012 case Intrinsic::aarch64_sve_tbl: 1013 return instCombineSVETBL(IC, II); 1014 case Intrinsic::aarch64_sve_uunpkhi: 1015 case Intrinsic::aarch64_sve_uunpklo: 1016 case Intrinsic::aarch64_sve_sunpkhi: 1017 case Intrinsic::aarch64_sve_sunpklo: 1018 return instCombineSVEUnpack(IC, II); 1019 case Intrinsic::aarch64_sve_tuple_get: 1020 return instCombineSVETupleGet(IC, II); 1021 case Intrinsic::aarch64_sve_zip1: 1022 case Intrinsic::aarch64_sve_zip2: 1023 return instCombineSVEZip(IC, II); 1024 case Intrinsic::aarch64_sve_ld1_gather_index: 1025 return instCombineLD1GatherIndex(IC, II); 1026 case Intrinsic::aarch64_sve_st1_scatter_index: 1027 return instCombineST1ScatterIndex(IC, II); 1028 } 1029 1030 return None; 1031 } 1032 1033 bool AArch64TTIImpl::isWideningInstruction(Type *DstTy, unsigned Opcode, 1034 ArrayRef<const Value *> Args) { 1035 1036 // A helper that returns a vector type from the given type. The number of 1037 // elements in type Ty determine the vector width. 1038 auto toVectorTy = [&](Type *ArgTy) { 1039 return VectorType::get(ArgTy->getScalarType(), 1040 cast<VectorType>(DstTy)->getElementCount()); 1041 }; 1042 1043 // Exit early if DstTy is not a vector type whose elements are at least 1044 // 16-bits wide. 1045 if (!DstTy->isVectorTy() || DstTy->getScalarSizeInBits() < 16) 1046 return false; 1047 1048 // Determine if the operation has a widening variant. We consider both the 1049 // "long" (e.g., usubl) and "wide" (e.g., usubw) versions of the 1050 // instructions. 1051 // 1052 // TODO: Add additional widening operations (e.g., mul, shl, etc.) once we 1053 // verify that their extending operands are eliminated during code 1054 // generation. 1055 switch (Opcode) { 1056 case Instruction::Add: // UADDL(2), SADDL(2), UADDW(2), SADDW(2). 1057 case Instruction::Sub: // USUBL(2), SSUBL(2), USUBW(2), SSUBW(2). 1058 break; 1059 default: 1060 return false; 1061 } 1062 1063 // To be a widening instruction (either the "wide" or "long" versions), the 1064 // second operand must be a sign- or zero extend having a single user. We 1065 // only consider extends having a single user because they may otherwise not 1066 // be eliminated. 1067 if (Args.size() != 2 || 1068 (!isa<SExtInst>(Args[1]) && !isa<ZExtInst>(Args[1])) || 1069 !Args[1]->hasOneUse()) 1070 return false; 1071 auto *Extend = cast<CastInst>(Args[1]); 1072 1073 // Legalize the destination type and ensure it can be used in a widening 1074 // operation. 1075 auto DstTyL = TLI->getTypeLegalizationCost(DL, DstTy); 1076 unsigned DstElTySize = DstTyL.second.getScalarSizeInBits(); 1077 if (!DstTyL.second.isVector() || DstElTySize != DstTy->getScalarSizeInBits()) 1078 return false; 1079 1080 // Legalize the source type and ensure it can be used in a widening 1081 // operation. 1082 auto *SrcTy = toVectorTy(Extend->getSrcTy()); 1083 auto SrcTyL = TLI->getTypeLegalizationCost(DL, SrcTy); 1084 unsigned SrcElTySize = SrcTyL.second.getScalarSizeInBits(); 1085 if (!SrcTyL.second.isVector() || SrcElTySize != SrcTy->getScalarSizeInBits()) 1086 return false; 1087 1088 // Get the total number of vector elements in the legalized types. 1089 InstructionCost NumDstEls = 1090 DstTyL.first * DstTyL.second.getVectorMinNumElements(); 1091 InstructionCost NumSrcEls = 1092 SrcTyL.first * SrcTyL.second.getVectorMinNumElements(); 1093 1094 // Return true if the legalized types have the same number of vector elements 1095 // and the destination element type size is twice that of the source type. 1096 return NumDstEls == NumSrcEls && 2 * SrcElTySize == DstElTySize; 1097 } 1098 1099 InstructionCost AArch64TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, 1100 Type *Src, 1101 TTI::CastContextHint CCH, 1102 TTI::TargetCostKind CostKind, 1103 const Instruction *I) { 1104 int ISD = TLI->InstructionOpcodeToISD(Opcode); 1105 assert(ISD && "Invalid opcode"); 1106 1107 // If the cast is observable, and it is used by a widening instruction (e.g., 1108 // uaddl, saddw, etc.), it may be free. 1109 if (I && I->hasOneUse()) { 1110 auto *SingleUser = cast<Instruction>(*I->user_begin()); 1111 SmallVector<const Value *, 4> Operands(SingleUser->operand_values()); 1112 if (isWideningInstruction(Dst, SingleUser->getOpcode(), Operands)) { 1113 // If the cast is the second operand, it is free. We will generate either 1114 // a "wide" or "long" version of the widening instruction. 1115 if (I == SingleUser->getOperand(1)) 1116 return 0; 1117 // If the cast is not the second operand, it will be free if it looks the 1118 // same as the second operand. In this case, we will generate a "long" 1119 // version of the widening instruction. 1120 if (auto *Cast = dyn_cast<CastInst>(SingleUser->getOperand(1))) 1121 if (I->getOpcode() == unsigned(Cast->getOpcode()) && 1122 cast<CastInst>(I)->getSrcTy() == Cast->getSrcTy()) 1123 return 0; 1124 } 1125 } 1126 1127 // TODO: Allow non-throughput costs that aren't binary. 1128 auto AdjustCost = [&CostKind](InstructionCost Cost) -> InstructionCost { 1129 if (CostKind != TTI::TCK_RecipThroughput) 1130 return Cost == 0 ? 0 : 1; 1131 return Cost; 1132 }; 1133 1134 EVT SrcTy = TLI->getValueType(DL, Src); 1135 EVT DstTy = TLI->getValueType(DL, Dst); 1136 1137 if (!SrcTy.isSimple() || !DstTy.isSimple()) 1138 return AdjustCost( 1139 BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I)); 1140 1141 static const TypeConversionCostTblEntry 1142 ConversionTbl[] = { 1143 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 }, 1144 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 0 }, 1145 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 }, 1146 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 6 }, 1147 1148 // Truncations on nxvmiN 1149 { ISD::TRUNCATE, MVT::nxv2i1, MVT::nxv2i16, 1 }, 1150 { ISD::TRUNCATE, MVT::nxv2i1, MVT::nxv2i32, 1 }, 1151 { ISD::TRUNCATE, MVT::nxv2i1, MVT::nxv2i64, 1 }, 1152 { ISD::TRUNCATE, MVT::nxv4i1, MVT::nxv4i16, 1 }, 1153 { ISD::TRUNCATE, MVT::nxv4i1, MVT::nxv4i32, 1 }, 1154 { ISD::TRUNCATE, MVT::nxv4i1, MVT::nxv4i64, 2 }, 1155 { ISD::TRUNCATE, MVT::nxv8i1, MVT::nxv8i16, 1 }, 1156 { ISD::TRUNCATE, MVT::nxv8i1, MVT::nxv8i32, 3 }, 1157 { ISD::TRUNCATE, MVT::nxv8i1, MVT::nxv8i64, 5 }, 1158 { ISD::TRUNCATE, MVT::nxv16i1, MVT::nxv16i8, 1 }, 1159 { ISD::TRUNCATE, MVT::nxv2i16, MVT::nxv2i32, 1 }, 1160 { ISD::TRUNCATE, MVT::nxv2i32, MVT::nxv2i64, 1 }, 1161 { ISD::TRUNCATE, MVT::nxv4i16, MVT::nxv4i32, 1 }, 1162 { ISD::TRUNCATE, MVT::nxv4i32, MVT::nxv4i64, 2 }, 1163 { ISD::TRUNCATE, MVT::nxv8i16, MVT::nxv8i32, 3 }, 1164 { ISD::TRUNCATE, MVT::nxv8i32, MVT::nxv8i64, 6 }, 1165 1166 // The number of shll instructions for the extension. 1167 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 1168 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 1169 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 2 }, 1170 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 2 }, 1171 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, 1172 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, 1173 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 2 }, 1174 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 2 }, 1175 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i8, 7 }, 1176 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i8, 7 }, 1177 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 6 }, 1178 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 6 }, 1179 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 2 }, 1180 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 2 }, 1181 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 6 }, 1182 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 6 }, 1183 1184 // LowerVectorINT_TO_FP: 1185 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 }, 1186 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, 1187 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 }, 1188 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 1 }, 1189 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, 1190 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 }, 1191 1192 // Complex: to v2f32 1193 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 }, 1194 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i16, 3 }, 1195 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i64, 2 }, 1196 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i8, 3 }, 1197 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i16, 3 }, 1198 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 2 }, 1199 1200 // Complex: to v4f32 1201 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 4 }, 1202 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 }, 1203 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 }, 1204 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 }, 1205 1206 // Complex: to v8f32 1207 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 10 }, 1208 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 }, 1209 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 10 }, 1210 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 4 }, 1211 1212 // Complex: to v16f32 1213 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 21 }, 1214 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i8, 21 }, 1215 1216 // Complex: to v2f64 1217 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 }, 1218 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i16, 4 }, 1219 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 }, 1220 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 4 }, 1221 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 4 }, 1222 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2 }, 1223 1224 1225 // LowerVectorFP_TO_INT 1226 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f32, 1 }, 1227 { ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f32, 1 }, 1228 { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f64, 1 }, 1229 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f32, 1 }, 1230 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 }, 1231 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f64, 1 }, 1232 1233 // Complex, from v2f32: legal type is v2i32 (no cost) or v2i64 (1 ext). 1234 { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f32, 2 }, 1235 { ISD::FP_TO_SINT, MVT::v2i16, MVT::v2f32, 1 }, 1236 { ISD::FP_TO_SINT, MVT::v2i8, MVT::v2f32, 1 }, 1237 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f32, 2 }, 1238 { ISD::FP_TO_UINT, MVT::v2i16, MVT::v2f32, 1 }, 1239 { ISD::FP_TO_UINT, MVT::v2i8, MVT::v2f32, 1 }, 1240 1241 // Complex, from v4f32: legal type is v4i16, 1 narrowing => ~2 1242 { ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f32, 2 }, 1243 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 2 }, 1244 { ISD::FP_TO_UINT, MVT::v4i16, MVT::v4f32, 2 }, 1245 { ISD::FP_TO_UINT, MVT::v4i8, MVT::v4f32, 2 }, 1246 1247 // Complex, from nxv2f32. 1248 { ISD::FP_TO_SINT, MVT::nxv2i64, MVT::nxv2f32, 1 }, 1249 { ISD::FP_TO_SINT, MVT::nxv2i32, MVT::nxv2f32, 1 }, 1250 { ISD::FP_TO_SINT, MVT::nxv2i16, MVT::nxv2f32, 1 }, 1251 { ISD::FP_TO_SINT, MVT::nxv2i8, MVT::nxv2f32, 1 }, 1252 { ISD::FP_TO_UINT, MVT::nxv2i64, MVT::nxv2f32, 1 }, 1253 { ISD::FP_TO_UINT, MVT::nxv2i32, MVT::nxv2f32, 1 }, 1254 { ISD::FP_TO_UINT, MVT::nxv2i16, MVT::nxv2f32, 1 }, 1255 { ISD::FP_TO_UINT, MVT::nxv2i8, MVT::nxv2f32, 1 }, 1256 1257 // Complex, from v2f64: legal type is v2i32, 1 narrowing => ~2. 1258 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 2 }, 1259 { ISD::FP_TO_SINT, MVT::v2i16, MVT::v2f64, 2 }, 1260 { ISD::FP_TO_SINT, MVT::v2i8, MVT::v2f64, 2 }, 1261 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f64, 2 }, 1262 { ISD::FP_TO_UINT, MVT::v2i16, MVT::v2f64, 2 }, 1263 { ISD::FP_TO_UINT, MVT::v2i8, MVT::v2f64, 2 }, 1264 1265 // Complex, from nxv2f64. 1266 { ISD::FP_TO_SINT, MVT::nxv2i64, MVT::nxv2f64, 1 }, 1267 { ISD::FP_TO_SINT, MVT::nxv2i32, MVT::nxv2f64, 1 }, 1268 { ISD::FP_TO_SINT, MVT::nxv2i16, MVT::nxv2f64, 1 }, 1269 { ISD::FP_TO_SINT, MVT::nxv2i8, MVT::nxv2f64, 1 }, 1270 { ISD::FP_TO_UINT, MVT::nxv2i64, MVT::nxv2f64, 1 }, 1271 { ISD::FP_TO_UINT, MVT::nxv2i32, MVT::nxv2f64, 1 }, 1272 { ISD::FP_TO_UINT, MVT::nxv2i16, MVT::nxv2f64, 1 }, 1273 { ISD::FP_TO_UINT, MVT::nxv2i8, MVT::nxv2f64, 1 }, 1274 1275 // Complex, from nxv4f32. 1276 { ISD::FP_TO_SINT, MVT::nxv4i64, MVT::nxv4f32, 4 }, 1277 { ISD::FP_TO_SINT, MVT::nxv4i32, MVT::nxv4f32, 1 }, 1278 { ISD::FP_TO_SINT, MVT::nxv4i16, MVT::nxv4f32, 1 }, 1279 { ISD::FP_TO_SINT, MVT::nxv4i8, MVT::nxv4f32, 1 }, 1280 { ISD::FP_TO_UINT, MVT::nxv4i64, MVT::nxv4f32, 4 }, 1281 { ISD::FP_TO_UINT, MVT::nxv4i32, MVT::nxv4f32, 1 }, 1282 { ISD::FP_TO_UINT, MVT::nxv4i16, MVT::nxv4f32, 1 }, 1283 { ISD::FP_TO_UINT, MVT::nxv4i8, MVT::nxv4f32, 1 }, 1284 1285 // Complex, from nxv8f64. Illegal -> illegal conversions not required. 1286 { ISD::FP_TO_SINT, MVT::nxv8i16, MVT::nxv8f64, 7 }, 1287 { ISD::FP_TO_SINT, MVT::nxv8i8, MVT::nxv8f64, 7 }, 1288 { ISD::FP_TO_UINT, MVT::nxv8i16, MVT::nxv8f64, 7 }, 1289 { ISD::FP_TO_UINT, MVT::nxv8i8, MVT::nxv8f64, 7 }, 1290 1291 // Complex, from nxv4f64. Illegal -> illegal conversions not required. 1292 { ISD::FP_TO_SINT, MVT::nxv4i32, MVT::nxv4f64, 3 }, 1293 { ISD::FP_TO_SINT, MVT::nxv4i16, MVT::nxv4f64, 3 }, 1294 { ISD::FP_TO_SINT, MVT::nxv4i8, MVT::nxv4f64, 3 }, 1295 { ISD::FP_TO_UINT, MVT::nxv4i32, MVT::nxv4f64, 3 }, 1296 { ISD::FP_TO_UINT, MVT::nxv4i16, MVT::nxv4f64, 3 }, 1297 { ISD::FP_TO_UINT, MVT::nxv4i8, MVT::nxv4f64, 3 }, 1298 1299 // Complex, from nxv8f32. Illegal -> illegal conversions not required. 1300 { ISD::FP_TO_SINT, MVT::nxv8i16, MVT::nxv8f32, 3 }, 1301 { ISD::FP_TO_SINT, MVT::nxv8i8, MVT::nxv8f32, 3 }, 1302 { ISD::FP_TO_UINT, MVT::nxv8i16, MVT::nxv8f32, 3 }, 1303 { ISD::FP_TO_UINT, MVT::nxv8i8, MVT::nxv8f32, 3 }, 1304 1305 // Complex, from nxv8f16. 1306 { ISD::FP_TO_SINT, MVT::nxv8i64, MVT::nxv8f16, 10 }, 1307 { ISD::FP_TO_SINT, MVT::nxv8i32, MVT::nxv8f16, 4 }, 1308 { ISD::FP_TO_SINT, MVT::nxv8i16, MVT::nxv8f16, 1 }, 1309 { ISD::FP_TO_SINT, MVT::nxv8i8, MVT::nxv8f16, 1 }, 1310 { ISD::FP_TO_UINT, MVT::nxv8i64, MVT::nxv8f16, 10 }, 1311 { ISD::FP_TO_UINT, MVT::nxv8i32, MVT::nxv8f16, 4 }, 1312 { ISD::FP_TO_UINT, MVT::nxv8i16, MVT::nxv8f16, 1 }, 1313 { ISD::FP_TO_UINT, MVT::nxv8i8, MVT::nxv8f16, 1 }, 1314 1315 // Complex, from nxv4f16. 1316 { ISD::FP_TO_SINT, MVT::nxv4i64, MVT::nxv4f16, 4 }, 1317 { ISD::FP_TO_SINT, MVT::nxv4i32, MVT::nxv4f16, 1 }, 1318 { ISD::FP_TO_SINT, MVT::nxv4i16, MVT::nxv4f16, 1 }, 1319 { ISD::FP_TO_SINT, MVT::nxv4i8, MVT::nxv4f16, 1 }, 1320 { ISD::FP_TO_UINT, MVT::nxv4i64, MVT::nxv4f16, 4 }, 1321 { ISD::FP_TO_UINT, MVT::nxv4i32, MVT::nxv4f16, 1 }, 1322 { ISD::FP_TO_UINT, MVT::nxv4i16, MVT::nxv4f16, 1 }, 1323 { ISD::FP_TO_UINT, MVT::nxv4i8, MVT::nxv4f16, 1 }, 1324 1325 // Complex, from nxv2f16. 1326 { ISD::FP_TO_SINT, MVT::nxv2i64, MVT::nxv2f16, 1 }, 1327 { ISD::FP_TO_SINT, MVT::nxv2i32, MVT::nxv2f16, 1 }, 1328 { ISD::FP_TO_SINT, MVT::nxv2i16, MVT::nxv2f16, 1 }, 1329 { ISD::FP_TO_SINT, MVT::nxv2i8, MVT::nxv2f16, 1 }, 1330 { ISD::FP_TO_UINT, MVT::nxv2i64, MVT::nxv2f16, 1 }, 1331 { ISD::FP_TO_UINT, MVT::nxv2i32, MVT::nxv2f16, 1 }, 1332 { ISD::FP_TO_UINT, MVT::nxv2i16, MVT::nxv2f16, 1 }, 1333 { ISD::FP_TO_UINT, MVT::nxv2i8, MVT::nxv2f16, 1 }, 1334 1335 // Truncate from nxvmf32 to nxvmf16. 1336 { ISD::FP_ROUND, MVT::nxv2f16, MVT::nxv2f32, 1 }, 1337 { ISD::FP_ROUND, MVT::nxv4f16, MVT::nxv4f32, 1 }, 1338 { ISD::FP_ROUND, MVT::nxv8f16, MVT::nxv8f32, 3 }, 1339 1340 // Truncate from nxvmf64 to nxvmf16. 1341 { ISD::FP_ROUND, MVT::nxv2f16, MVT::nxv2f64, 1 }, 1342 { ISD::FP_ROUND, MVT::nxv4f16, MVT::nxv4f64, 3 }, 1343 { ISD::FP_ROUND, MVT::nxv8f16, MVT::nxv8f64, 7 }, 1344 1345 // Truncate from nxvmf64 to nxvmf32. 1346 { ISD::FP_ROUND, MVT::nxv2f32, MVT::nxv2f64, 1 }, 1347 { ISD::FP_ROUND, MVT::nxv4f32, MVT::nxv4f64, 3 }, 1348 { ISD::FP_ROUND, MVT::nxv8f32, MVT::nxv8f64, 6 }, 1349 1350 // Extend from nxvmf16 to nxvmf32. 1351 { ISD::FP_EXTEND, MVT::nxv2f32, MVT::nxv2f16, 1}, 1352 { ISD::FP_EXTEND, MVT::nxv4f32, MVT::nxv4f16, 1}, 1353 { ISD::FP_EXTEND, MVT::nxv8f32, MVT::nxv8f16, 2}, 1354 1355 // Extend from nxvmf16 to nxvmf64. 1356 { ISD::FP_EXTEND, MVT::nxv2f64, MVT::nxv2f16, 1}, 1357 { ISD::FP_EXTEND, MVT::nxv4f64, MVT::nxv4f16, 2}, 1358 { ISD::FP_EXTEND, MVT::nxv8f64, MVT::nxv8f16, 4}, 1359 1360 // Extend from nxvmf32 to nxvmf64. 1361 { ISD::FP_EXTEND, MVT::nxv2f64, MVT::nxv2f32, 1}, 1362 { ISD::FP_EXTEND, MVT::nxv4f64, MVT::nxv4f32, 2}, 1363 { ISD::FP_EXTEND, MVT::nxv8f64, MVT::nxv8f32, 6}, 1364 1365 }; 1366 1367 if (const auto *Entry = ConvertCostTableLookup(ConversionTbl, ISD, 1368 DstTy.getSimpleVT(), 1369 SrcTy.getSimpleVT())) 1370 return AdjustCost(Entry->Cost); 1371 1372 return AdjustCost( 1373 BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I)); 1374 } 1375 1376 InstructionCost AArch64TTIImpl::getExtractWithExtendCost(unsigned Opcode, 1377 Type *Dst, 1378 VectorType *VecTy, 1379 unsigned Index) { 1380 1381 // Make sure we were given a valid extend opcode. 1382 assert((Opcode == Instruction::SExt || Opcode == Instruction::ZExt) && 1383 "Invalid opcode"); 1384 1385 // We are extending an element we extract from a vector, so the source type 1386 // of the extend is the element type of the vector. 1387 auto *Src = VecTy->getElementType(); 1388 1389 // Sign- and zero-extends are for integer types only. 1390 assert(isa<IntegerType>(Dst) && isa<IntegerType>(Src) && "Invalid type"); 1391 1392 // Get the cost for the extract. We compute the cost (if any) for the extend 1393 // below. 1394 InstructionCost Cost = 1395 getVectorInstrCost(Instruction::ExtractElement, VecTy, Index); 1396 1397 // Legalize the types. 1398 auto VecLT = TLI->getTypeLegalizationCost(DL, VecTy); 1399 auto DstVT = TLI->getValueType(DL, Dst); 1400 auto SrcVT = TLI->getValueType(DL, Src); 1401 TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput; 1402 1403 // If the resulting type is still a vector and the destination type is legal, 1404 // we may get the extension for free. If not, get the default cost for the 1405 // extend. 1406 if (!VecLT.second.isVector() || !TLI->isTypeLegal(DstVT)) 1407 return Cost + getCastInstrCost(Opcode, Dst, Src, TTI::CastContextHint::None, 1408 CostKind); 1409 1410 // The destination type should be larger than the element type. If not, get 1411 // the default cost for the extend. 1412 if (DstVT.getFixedSizeInBits() < SrcVT.getFixedSizeInBits()) 1413 return Cost + getCastInstrCost(Opcode, Dst, Src, TTI::CastContextHint::None, 1414 CostKind); 1415 1416 switch (Opcode) { 1417 default: 1418 llvm_unreachable("Opcode should be either SExt or ZExt"); 1419 1420 // For sign-extends, we only need a smov, which performs the extension 1421 // automatically. 1422 case Instruction::SExt: 1423 return Cost; 1424 1425 // For zero-extends, the extend is performed automatically by a umov unless 1426 // the destination type is i64 and the element type is i8 or i16. 1427 case Instruction::ZExt: 1428 if (DstVT.getSizeInBits() != 64u || SrcVT.getSizeInBits() == 32u) 1429 return Cost; 1430 } 1431 1432 // If we are unable to perform the extend for free, get the default cost. 1433 return Cost + getCastInstrCost(Opcode, Dst, Src, TTI::CastContextHint::None, 1434 CostKind); 1435 } 1436 1437 InstructionCost AArch64TTIImpl::getCFInstrCost(unsigned Opcode, 1438 TTI::TargetCostKind CostKind, 1439 const Instruction *I) { 1440 if (CostKind != TTI::TCK_RecipThroughput) 1441 return Opcode == Instruction::PHI ? 0 : 1; 1442 assert(CostKind == TTI::TCK_RecipThroughput && "unexpected CostKind"); 1443 // Branches are assumed to be predicted. 1444 return 0; 1445 } 1446 1447 InstructionCost AArch64TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, 1448 unsigned Index) { 1449 assert(Val->isVectorTy() && "This must be a vector type"); 1450 1451 if (Index != -1U) { 1452 // Legalize the type. 1453 std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Val); 1454 1455 // This type is legalized to a scalar type. 1456 if (!LT.second.isVector()) 1457 return 0; 1458 1459 // The type may be split. Normalize the index to the new type. 1460 unsigned Width = LT.second.getVectorNumElements(); 1461 Index = Index % Width; 1462 1463 // The element at index zero is already inside the vector. 1464 if (Index == 0) 1465 return 0; 1466 } 1467 1468 // All other insert/extracts cost this much. 1469 return ST->getVectorInsertExtractBaseCost(); 1470 } 1471 1472 InstructionCost AArch64TTIImpl::getArithmeticInstrCost( 1473 unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind, 1474 TTI::OperandValueKind Opd1Info, TTI::OperandValueKind Opd2Info, 1475 TTI::OperandValueProperties Opd1PropInfo, 1476 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args, 1477 const Instruction *CxtI) { 1478 // TODO: Handle more cost kinds. 1479 if (CostKind != TTI::TCK_RecipThroughput) 1480 return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info, 1481 Opd2Info, Opd1PropInfo, 1482 Opd2PropInfo, Args, CxtI); 1483 1484 // Legalize the type. 1485 std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty); 1486 1487 // If the instruction is a widening instruction (e.g., uaddl, saddw, etc.), 1488 // add in the widening overhead specified by the sub-target. Since the 1489 // extends feeding widening instructions are performed automatically, they 1490 // aren't present in the generated code and have a zero cost. By adding a 1491 // widening overhead here, we attach the total cost of the combined operation 1492 // to the widening instruction. 1493 InstructionCost Cost = 0; 1494 if (isWideningInstruction(Ty, Opcode, Args)) 1495 Cost += ST->getWideningBaseCost(); 1496 1497 int ISD = TLI->InstructionOpcodeToISD(Opcode); 1498 1499 switch (ISD) { 1500 default: 1501 return Cost + BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info, 1502 Opd2Info, 1503 Opd1PropInfo, Opd2PropInfo); 1504 case ISD::SDIV: 1505 if (Opd2Info == TargetTransformInfo::OK_UniformConstantValue && 1506 Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) { 1507 // On AArch64, scalar signed division by constants power-of-two are 1508 // normally expanded to the sequence ADD + CMP + SELECT + SRA. 1509 // The OperandValue properties many not be same as that of previous 1510 // operation; conservatively assume OP_None. 1511 Cost += getArithmeticInstrCost(Instruction::Add, Ty, CostKind, 1512 Opd1Info, Opd2Info, 1513 TargetTransformInfo::OP_None, 1514 TargetTransformInfo::OP_None); 1515 Cost += getArithmeticInstrCost(Instruction::Sub, Ty, CostKind, 1516 Opd1Info, Opd2Info, 1517 TargetTransformInfo::OP_None, 1518 TargetTransformInfo::OP_None); 1519 Cost += getArithmeticInstrCost(Instruction::Select, Ty, CostKind, 1520 Opd1Info, Opd2Info, 1521 TargetTransformInfo::OP_None, 1522 TargetTransformInfo::OP_None); 1523 Cost += getArithmeticInstrCost(Instruction::AShr, Ty, CostKind, 1524 Opd1Info, Opd2Info, 1525 TargetTransformInfo::OP_None, 1526 TargetTransformInfo::OP_None); 1527 return Cost; 1528 } 1529 LLVM_FALLTHROUGH; 1530 case ISD::UDIV: 1531 if (Opd2Info == TargetTransformInfo::OK_UniformConstantValue) { 1532 auto VT = TLI->getValueType(DL, Ty); 1533 if (TLI->isOperationLegalOrCustom(ISD::MULHU, VT)) { 1534 // Vector signed division by constant are expanded to the 1535 // sequence MULHS + ADD/SUB + SRA + SRL + ADD, and unsigned division 1536 // to MULHS + SUB + SRL + ADD + SRL. 1537 InstructionCost MulCost = getArithmeticInstrCost( 1538 Instruction::Mul, Ty, CostKind, Opd1Info, Opd2Info, 1539 TargetTransformInfo::OP_None, TargetTransformInfo::OP_None); 1540 InstructionCost AddCost = getArithmeticInstrCost( 1541 Instruction::Add, Ty, CostKind, Opd1Info, Opd2Info, 1542 TargetTransformInfo::OP_None, TargetTransformInfo::OP_None); 1543 InstructionCost ShrCost = getArithmeticInstrCost( 1544 Instruction::AShr, Ty, CostKind, Opd1Info, Opd2Info, 1545 TargetTransformInfo::OP_None, TargetTransformInfo::OP_None); 1546 return MulCost * 2 + AddCost * 2 + ShrCost * 2 + 1; 1547 } 1548 } 1549 1550 Cost += BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info, 1551 Opd2Info, 1552 Opd1PropInfo, Opd2PropInfo); 1553 if (Ty->isVectorTy()) { 1554 // On AArch64, vector divisions are not supported natively and are 1555 // expanded into scalar divisions of each pair of elements. 1556 Cost += getArithmeticInstrCost(Instruction::ExtractElement, Ty, CostKind, 1557 Opd1Info, Opd2Info, Opd1PropInfo, 1558 Opd2PropInfo); 1559 Cost += getArithmeticInstrCost(Instruction::InsertElement, Ty, CostKind, 1560 Opd1Info, Opd2Info, Opd1PropInfo, 1561 Opd2PropInfo); 1562 // TODO: if one of the arguments is scalar, then it's not necessary to 1563 // double the cost of handling the vector elements. 1564 Cost += Cost; 1565 } 1566 return Cost; 1567 1568 case ISD::MUL: 1569 if (LT.second != MVT::v2i64) 1570 return (Cost + 1) * LT.first; 1571 // Since we do not have a MUL.2d instruction, a mul <2 x i64> is expensive 1572 // as elements are extracted from the vectors and the muls scalarized. 1573 // As getScalarizationOverhead is a bit too pessimistic, we estimate the 1574 // cost for a i64 vector directly here, which is: 1575 // - four i64 extracts, 1576 // - two i64 inserts, and 1577 // - two muls. 1578 // So, for a v2i64 with LT.First = 1 the cost is 8, and for a v4i64 with 1579 // LT.first = 2 the cost is 16. 1580 return LT.first * 8; 1581 case ISD::ADD: 1582 case ISD::XOR: 1583 case ISD::OR: 1584 case ISD::AND: 1585 // These nodes are marked as 'custom' for combining purposes only. 1586 // We know that they are legal. See LowerAdd in ISelLowering. 1587 return (Cost + 1) * LT.first; 1588 1589 case ISD::FADD: 1590 case ISD::FSUB: 1591 case ISD::FMUL: 1592 case ISD::FDIV: 1593 case ISD::FNEG: 1594 // These nodes are marked as 'custom' just to lower them to SVE. 1595 // We know said lowering will incur no additional cost. 1596 if (!Ty->getScalarType()->isFP128Ty()) 1597 return (Cost + 2) * LT.first; 1598 1599 return Cost + BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info, 1600 Opd2Info, 1601 Opd1PropInfo, Opd2PropInfo); 1602 } 1603 } 1604 1605 InstructionCost AArch64TTIImpl::getAddressComputationCost(Type *Ty, 1606 ScalarEvolution *SE, 1607 const SCEV *Ptr) { 1608 // Address computations in vectorized code with non-consecutive addresses will 1609 // likely result in more instructions compared to scalar code where the 1610 // computation can more often be merged into the index mode. The resulting 1611 // extra micro-ops can significantly decrease throughput. 1612 unsigned NumVectorInstToHideOverhead = 10; 1613 int MaxMergeDistance = 64; 1614 1615 if (Ty->isVectorTy() && SE && 1616 !BaseT::isConstantStridedAccessLessThan(SE, Ptr, MaxMergeDistance + 1)) 1617 return NumVectorInstToHideOverhead; 1618 1619 // In many cases the address computation is not merged into the instruction 1620 // addressing mode. 1621 return 1; 1622 } 1623 1624 InstructionCost AArch64TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 1625 Type *CondTy, 1626 CmpInst::Predicate VecPred, 1627 TTI::TargetCostKind CostKind, 1628 const Instruction *I) { 1629 // TODO: Handle other cost kinds. 1630 if (CostKind != TTI::TCK_RecipThroughput) 1631 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind, 1632 I); 1633 1634 int ISD = TLI->InstructionOpcodeToISD(Opcode); 1635 // We don't lower some vector selects well that are wider than the register 1636 // width. 1637 if (isa<FixedVectorType>(ValTy) && ISD == ISD::SELECT) { 1638 // We would need this many instructions to hide the scalarization happening. 1639 const int AmortizationCost = 20; 1640 1641 // If VecPred is not set, check if we can get a predicate from the context 1642 // instruction, if its type matches the requested ValTy. 1643 if (VecPred == CmpInst::BAD_ICMP_PREDICATE && I && I->getType() == ValTy) { 1644 CmpInst::Predicate CurrentPred; 1645 if (match(I, m_Select(m_Cmp(CurrentPred, m_Value(), m_Value()), m_Value(), 1646 m_Value()))) 1647 VecPred = CurrentPred; 1648 } 1649 // Check if we have a compare/select chain that can be lowered using CMxx & 1650 // BFI pair. 1651 if (CmpInst::isIntPredicate(VecPred)) { 1652 static const auto ValidMinMaxTys = {MVT::v8i8, MVT::v16i8, MVT::v4i16, 1653 MVT::v8i16, MVT::v2i32, MVT::v4i32, 1654 MVT::v2i64}; 1655 auto LT = TLI->getTypeLegalizationCost(DL, ValTy); 1656 if (any_of(ValidMinMaxTys, [<](MVT M) { return M == LT.second; })) 1657 return LT.first; 1658 } 1659 1660 static const TypeConversionCostTblEntry 1661 VectorSelectTbl[] = { 1662 { ISD::SELECT, MVT::v16i1, MVT::v16i16, 16 }, 1663 { ISD::SELECT, MVT::v8i1, MVT::v8i32, 8 }, 1664 { ISD::SELECT, MVT::v16i1, MVT::v16i32, 16 }, 1665 { ISD::SELECT, MVT::v4i1, MVT::v4i64, 4 * AmortizationCost }, 1666 { ISD::SELECT, MVT::v8i1, MVT::v8i64, 8 * AmortizationCost }, 1667 { ISD::SELECT, MVT::v16i1, MVT::v16i64, 16 * AmortizationCost } 1668 }; 1669 1670 EVT SelCondTy = TLI->getValueType(DL, CondTy); 1671 EVT SelValTy = TLI->getValueType(DL, ValTy); 1672 if (SelCondTy.isSimple() && SelValTy.isSimple()) { 1673 if (const auto *Entry = ConvertCostTableLookup(VectorSelectTbl, ISD, 1674 SelCondTy.getSimpleVT(), 1675 SelValTy.getSimpleVT())) 1676 return Entry->Cost; 1677 } 1678 } 1679 // The base case handles scalable vectors fine for now, since it treats the 1680 // cost as 1 * legalization cost. 1681 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind, I); 1682 } 1683 1684 AArch64TTIImpl::TTI::MemCmpExpansionOptions 1685 AArch64TTIImpl::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const { 1686 TTI::MemCmpExpansionOptions Options; 1687 if (ST->requiresStrictAlign()) { 1688 // TODO: Add cost modeling for strict align. Misaligned loads expand to 1689 // a bunch of instructions when strict align is enabled. 1690 return Options; 1691 } 1692 Options.AllowOverlappingLoads = true; 1693 Options.MaxNumLoads = TLI->getMaxExpandSizeMemcmp(OptSize); 1694 Options.NumLoadsPerBlock = Options.MaxNumLoads; 1695 // TODO: Though vector loads usually perform well on AArch64, in some targets 1696 // they may wake up the FP unit, which raises the power consumption. Perhaps 1697 // they could be used with no holds barred (-O3). 1698 Options.LoadSizes = {8, 4, 2, 1}; 1699 return Options; 1700 } 1701 1702 InstructionCost 1703 AArch64TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *Src, 1704 Align Alignment, unsigned AddressSpace, 1705 TTI::TargetCostKind CostKind) { 1706 if (!isa<ScalableVectorType>(Src)) 1707 return BaseT::getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace, 1708 CostKind); 1709 auto LT = TLI->getTypeLegalizationCost(DL, Src); 1710 if (!LT.first.isValid()) 1711 return InstructionCost::getInvalid(); 1712 1713 // The code-generator is currently not able to handle scalable vectors 1714 // of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting 1715 // it. This change will be removed when code-generation for these types is 1716 // sufficiently reliable. 1717 if (cast<VectorType>(Src)->getElementCount() == ElementCount::getScalable(1)) 1718 return InstructionCost::getInvalid(); 1719 1720 return LT.first * 2; 1721 } 1722 1723 InstructionCost AArch64TTIImpl::getGatherScatterOpCost( 1724 unsigned Opcode, Type *DataTy, const Value *Ptr, bool VariableMask, 1725 Align Alignment, TTI::TargetCostKind CostKind, const Instruction *I) { 1726 if (useNeonVector(DataTy)) 1727 return BaseT::getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask, 1728 Alignment, CostKind, I); 1729 auto *VT = cast<VectorType>(DataTy); 1730 auto LT = TLI->getTypeLegalizationCost(DL, DataTy); 1731 if (!LT.first.isValid()) 1732 return InstructionCost::getInvalid(); 1733 1734 // The code-generator is currently not able to handle scalable vectors 1735 // of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting 1736 // it. This change will be removed when code-generation for these types is 1737 // sufficiently reliable. 1738 if (cast<VectorType>(DataTy)->getElementCount() == 1739 ElementCount::getScalable(1)) 1740 return InstructionCost::getInvalid(); 1741 1742 ElementCount LegalVF = LT.second.getVectorElementCount(); 1743 InstructionCost MemOpCost = 1744 getMemoryOpCost(Opcode, VT->getElementType(), Alignment, 0, CostKind, I); 1745 return LT.first * MemOpCost * getMaxNumElements(LegalVF); 1746 } 1747 1748 bool AArch64TTIImpl::useNeonVector(const Type *Ty) const { 1749 return isa<FixedVectorType>(Ty) && !ST->useSVEForFixedLengthVectors(); 1750 } 1751 1752 InstructionCost AArch64TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Ty, 1753 MaybeAlign Alignment, 1754 unsigned AddressSpace, 1755 TTI::TargetCostKind CostKind, 1756 const Instruction *I) { 1757 EVT VT = TLI->getValueType(DL, Ty, true); 1758 // Type legalization can't handle structs 1759 if (VT == MVT::Other) 1760 return BaseT::getMemoryOpCost(Opcode, Ty, Alignment, AddressSpace, 1761 CostKind); 1762 1763 auto LT = TLI->getTypeLegalizationCost(DL, Ty); 1764 if (!LT.first.isValid()) 1765 return InstructionCost::getInvalid(); 1766 1767 // The code-generator is currently not able to handle scalable vectors 1768 // of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting 1769 // it. This change will be removed when code-generation for these types is 1770 // sufficiently reliable. 1771 if (auto *VTy = dyn_cast<ScalableVectorType>(Ty)) 1772 if (VTy->getElementCount() == ElementCount::getScalable(1)) 1773 return InstructionCost::getInvalid(); 1774 1775 // TODO: consider latency as well for TCK_SizeAndLatency. 1776 if (CostKind == TTI::TCK_CodeSize || CostKind == TTI::TCK_SizeAndLatency) 1777 return LT.first; 1778 1779 if (CostKind != TTI::TCK_RecipThroughput) 1780 return 1; 1781 1782 if (ST->isMisaligned128StoreSlow() && Opcode == Instruction::Store && 1783 LT.second.is128BitVector() && (!Alignment || *Alignment < Align(16))) { 1784 // Unaligned stores are extremely inefficient. We don't split all 1785 // unaligned 128-bit stores because the negative impact that has shown in 1786 // practice on inlined block copy code. 1787 // We make such stores expensive so that we will only vectorize if there 1788 // are 6 other instructions getting vectorized. 1789 const int AmortizationCost = 6; 1790 1791 return LT.first * 2 * AmortizationCost; 1792 } 1793 1794 // Check truncating stores and extending loads. 1795 if (useNeonVector(Ty) && 1796 Ty->getScalarSizeInBits() != LT.second.getScalarSizeInBits()) { 1797 // v4i8 types are lowered to scalar a load/store and sshll/xtn. 1798 if (VT == MVT::v4i8) 1799 return 2; 1800 // Otherwise we need to scalarize. 1801 return cast<FixedVectorType>(Ty)->getNumElements() * 2; 1802 } 1803 1804 return LT.first; 1805 } 1806 1807 InstructionCost AArch64TTIImpl::getInterleavedMemoryOpCost( 1808 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices, 1809 Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind, 1810 bool UseMaskForCond, bool UseMaskForGaps) { 1811 assert(Factor >= 2 && "Invalid interleave factor"); 1812 auto *VecVTy = cast<FixedVectorType>(VecTy); 1813 1814 if (!UseMaskForCond && !UseMaskForGaps && 1815 Factor <= TLI->getMaxSupportedInterleaveFactor()) { 1816 unsigned NumElts = VecVTy->getNumElements(); 1817 auto *SubVecTy = 1818 FixedVectorType::get(VecTy->getScalarType(), NumElts / Factor); 1819 1820 // ldN/stN only support legal vector types of size 64 or 128 in bits. 1821 // Accesses having vector types that are a multiple of 128 bits can be 1822 // matched to more than one ldN/stN instruction. 1823 bool UseScalable; 1824 if (NumElts % Factor == 0 && 1825 TLI->isLegalInterleavedAccessType(SubVecTy, DL, UseScalable)) 1826 return Factor * TLI->getNumInterleavedAccesses(SubVecTy, DL, UseScalable); 1827 } 1828 1829 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices, 1830 Alignment, AddressSpace, CostKind, 1831 UseMaskForCond, UseMaskForGaps); 1832 } 1833 1834 InstructionCost 1835 AArch64TTIImpl::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { 1836 InstructionCost Cost = 0; 1837 TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput; 1838 for (auto *I : Tys) { 1839 if (!I->isVectorTy()) 1840 continue; 1841 if (I->getScalarSizeInBits() * cast<FixedVectorType>(I)->getNumElements() == 1842 128) 1843 Cost += getMemoryOpCost(Instruction::Store, I, Align(128), 0, CostKind) + 1844 getMemoryOpCost(Instruction::Load, I, Align(128), 0, CostKind); 1845 } 1846 return Cost; 1847 } 1848 1849 unsigned AArch64TTIImpl::getMaxInterleaveFactor(unsigned VF) { 1850 return ST->getMaxInterleaveFactor(); 1851 } 1852 1853 // For Falkor, we want to avoid having too many strided loads in a loop since 1854 // that can exhaust the HW prefetcher resources. We adjust the unroller 1855 // MaxCount preference below to attempt to ensure unrolling doesn't create too 1856 // many strided loads. 1857 static void 1858 getFalkorUnrollingPreferences(Loop *L, ScalarEvolution &SE, 1859 TargetTransformInfo::UnrollingPreferences &UP) { 1860 enum { MaxStridedLoads = 7 }; 1861 auto countStridedLoads = [](Loop *L, ScalarEvolution &SE) { 1862 int StridedLoads = 0; 1863 // FIXME? We could make this more precise by looking at the CFG and 1864 // e.g. not counting loads in each side of an if-then-else diamond. 1865 for (const auto BB : L->blocks()) { 1866 for (auto &I : *BB) { 1867 LoadInst *LMemI = dyn_cast<LoadInst>(&I); 1868 if (!LMemI) 1869 continue; 1870 1871 Value *PtrValue = LMemI->getPointerOperand(); 1872 if (L->isLoopInvariant(PtrValue)) 1873 continue; 1874 1875 const SCEV *LSCEV = SE.getSCEV(PtrValue); 1876 const SCEVAddRecExpr *LSCEVAddRec = dyn_cast<SCEVAddRecExpr>(LSCEV); 1877 if (!LSCEVAddRec || !LSCEVAddRec->isAffine()) 1878 continue; 1879 1880 // FIXME? We could take pairing of unrolled load copies into account 1881 // by looking at the AddRec, but we would probably have to limit this 1882 // to loops with no stores or other memory optimization barriers. 1883 ++StridedLoads; 1884 // We've seen enough strided loads that seeing more won't make a 1885 // difference. 1886 if (StridedLoads > MaxStridedLoads / 2) 1887 return StridedLoads; 1888 } 1889 } 1890 return StridedLoads; 1891 }; 1892 1893 int StridedLoads = countStridedLoads(L, SE); 1894 LLVM_DEBUG(dbgs() << "falkor-hwpf: detected " << StridedLoads 1895 << " strided loads\n"); 1896 // Pick the largest power of 2 unroll count that won't result in too many 1897 // strided loads. 1898 if (StridedLoads) { 1899 UP.MaxCount = 1 << Log2_32(MaxStridedLoads / StridedLoads); 1900 LLVM_DEBUG(dbgs() << "falkor-hwpf: setting unroll MaxCount to " 1901 << UP.MaxCount << '\n'); 1902 } 1903 } 1904 1905 void AArch64TTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE, 1906 TTI::UnrollingPreferences &UP, 1907 OptimizationRemarkEmitter *ORE) { 1908 // Enable partial unrolling and runtime unrolling. 1909 BaseT::getUnrollingPreferences(L, SE, UP, ORE); 1910 1911 UP.UpperBound = true; 1912 1913 // For inner loop, it is more likely to be a hot one, and the runtime check 1914 // can be promoted out from LICM pass, so the overhead is less, let's try 1915 // a larger threshold to unroll more loops. 1916 if (L->getLoopDepth() > 1) 1917 UP.PartialThreshold *= 2; 1918 1919 // Disable partial & runtime unrolling on -Os. 1920 UP.PartialOptSizeThreshold = 0; 1921 1922 if (ST->getProcFamily() == AArch64Subtarget::Falkor && 1923 EnableFalkorHWPFUnrollFix) 1924 getFalkorUnrollingPreferences(L, SE, UP); 1925 1926 // Scan the loop: don't unroll loops with calls as this could prevent 1927 // inlining. Don't unroll vector loops either, as they don't benefit much from 1928 // unrolling. 1929 for (auto *BB : L->getBlocks()) { 1930 for (auto &I : *BB) { 1931 // Don't unroll vectorised loop. 1932 if (I.getType()->isVectorTy()) 1933 return; 1934 1935 if (isa<CallInst>(I) || isa<InvokeInst>(I)) { 1936 if (const Function *F = cast<CallBase>(I).getCalledFunction()) { 1937 if (!isLoweredToCall(F)) 1938 continue; 1939 } 1940 return; 1941 } 1942 } 1943 } 1944 1945 // Enable runtime unrolling for in-order models 1946 // If mcpu is omitted, getProcFamily() returns AArch64Subtarget::Others, so by 1947 // checking for that case, we can ensure that the default behaviour is 1948 // unchanged 1949 if (ST->getProcFamily() != AArch64Subtarget::Others && 1950 !ST->getSchedModel().isOutOfOrder()) { 1951 UP.Runtime = true; 1952 UP.Partial = true; 1953 UP.UnrollRemainder = true; 1954 UP.DefaultUnrollRuntimeCount = 4; 1955 1956 UP.UnrollAndJam = true; 1957 UP.UnrollAndJamInnerLoopThreshold = 60; 1958 } 1959 } 1960 1961 void AArch64TTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE, 1962 TTI::PeelingPreferences &PP) { 1963 BaseT::getPeelingPreferences(L, SE, PP); 1964 } 1965 1966 Value *AArch64TTIImpl::getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst, 1967 Type *ExpectedType) { 1968 switch (Inst->getIntrinsicID()) { 1969 default: 1970 return nullptr; 1971 case Intrinsic::aarch64_neon_st2: 1972 case Intrinsic::aarch64_neon_st3: 1973 case Intrinsic::aarch64_neon_st4: { 1974 // Create a struct type 1975 StructType *ST = dyn_cast<StructType>(ExpectedType); 1976 if (!ST) 1977 return nullptr; 1978 unsigned NumElts = Inst->arg_size() - 1; 1979 if (ST->getNumElements() != NumElts) 1980 return nullptr; 1981 for (unsigned i = 0, e = NumElts; i != e; ++i) { 1982 if (Inst->getArgOperand(i)->getType() != ST->getElementType(i)) 1983 return nullptr; 1984 } 1985 Value *Res = UndefValue::get(ExpectedType); 1986 IRBuilder<> Builder(Inst); 1987 for (unsigned i = 0, e = NumElts; i != e; ++i) { 1988 Value *L = Inst->getArgOperand(i); 1989 Res = Builder.CreateInsertValue(Res, L, i); 1990 } 1991 return Res; 1992 } 1993 case Intrinsic::aarch64_neon_ld2: 1994 case Intrinsic::aarch64_neon_ld3: 1995 case Intrinsic::aarch64_neon_ld4: 1996 if (Inst->getType() == ExpectedType) 1997 return Inst; 1998 return nullptr; 1999 } 2000 } 2001 2002 bool AArch64TTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst, 2003 MemIntrinsicInfo &Info) { 2004 switch (Inst->getIntrinsicID()) { 2005 default: 2006 break; 2007 case Intrinsic::aarch64_neon_ld2: 2008 case Intrinsic::aarch64_neon_ld3: 2009 case Intrinsic::aarch64_neon_ld4: 2010 Info.ReadMem = true; 2011 Info.WriteMem = false; 2012 Info.PtrVal = Inst->getArgOperand(0); 2013 break; 2014 case Intrinsic::aarch64_neon_st2: 2015 case Intrinsic::aarch64_neon_st3: 2016 case Intrinsic::aarch64_neon_st4: 2017 Info.ReadMem = false; 2018 Info.WriteMem = true; 2019 Info.PtrVal = Inst->getArgOperand(Inst->arg_size() - 1); 2020 break; 2021 } 2022 2023 switch (Inst->getIntrinsicID()) { 2024 default: 2025 return false; 2026 case Intrinsic::aarch64_neon_ld2: 2027 case Intrinsic::aarch64_neon_st2: 2028 Info.MatchingId = VECTOR_LDST_TWO_ELEMENTS; 2029 break; 2030 case Intrinsic::aarch64_neon_ld3: 2031 case Intrinsic::aarch64_neon_st3: 2032 Info.MatchingId = VECTOR_LDST_THREE_ELEMENTS; 2033 break; 2034 case Intrinsic::aarch64_neon_ld4: 2035 case Intrinsic::aarch64_neon_st4: 2036 Info.MatchingId = VECTOR_LDST_FOUR_ELEMENTS; 2037 break; 2038 } 2039 return true; 2040 } 2041 2042 /// See if \p I should be considered for address type promotion. We check if \p 2043 /// I is a sext with right type and used in memory accesses. If it used in a 2044 /// "complex" getelementptr, we allow it to be promoted without finding other 2045 /// sext instructions that sign extended the same initial value. A getelementptr 2046 /// is considered as "complex" if it has more than 2 operands. 2047 bool AArch64TTIImpl::shouldConsiderAddressTypePromotion( 2048 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) { 2049 bool Considerable = false; 2050 AllowPromotionWithoutCommonHeader = false; 2051 if (!isa<SExtInst>(&I)) 2052 return false; 2053 Type *ConsideredSExtType = 2054 Type::getInt64Ty(I.getParent()->getParent()->getContext()); 2055 if (I.getType() != ConsideredSExtType) 2056 return false; 2057 // See if the sext is the one with the right type and used in at least one 2058 // GetElementPtrInst. 2059 for (const User *U : I.users()) { 2060 if (const GetElementPtrInst *GEPInst = dyn_cast<GetElementPtrInst>(U)) { 2061 Considerable = true; 2062 // A getelementptr is considered as "complex" if it has more than 2 2063 // operands. We will promote a SExt used in such complex GEP as we 2064 // expect some computation to be merged if they are done on 64 bits. 2065 if (GEPInst->getNumOperands() > 2) { 2066 AllowPromotionWithoutCommonHeader = true; 2067 break; 2068 } 2069 } 2070 } 2071 return Considerable; 2072 } 2073 2074 bool AArch64TTIImpl::isLegalToVectorizeReduction( 2075 const RecurrenceDescriptor &RdxDesc, ElementCount VF) const { 2076 if (!VF.isScalable()) 2077 return true; 2078 2079 Type *Ty = RdxDesc.getRecurrenceType(); 2080 if (Ty->isBFloatTy() || !isElementTypeLegalForScalableVector(Ty)) 2081 return false; 2082 2083 switch (RdxDesc.getRecurrenceKind()) { 2084 case RecurKind::Add: 2085 case RecurKind::FAdd: 2086 case RecurKind::And: 2087 case RecurKind::Or: 2088 case RecurKind::Xor: 2089 case RecurKind::SMin: 2090 case RecurKind::SMax: 2091 case RecurKind::UMin: 2092 case RecurKind::UMax: 2093 case RecurKind::FMin: 2094 case RecurKind::FMax: 2095 case RecurKind::SelectICmp: 2096 case RecurKind::SelectFCmp: 2097 return true; 2098 default: 2099 return false; 2100 } 2101 } 2102 2103 InstructionCost 2104 AArch64TTIImpl::getMinMaxReductionCost(VectorType *Ty, VectorType *CondTy, 2105 bool IsUnsigned, 2106 TTI::TargetCostKind CostKind) { 2107 std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty); 2108 2109 if (LT.second.getScalarType() == MVT::f16 && !ST->hasFullFP16()) 2110 return BaseT::getMinMaxReductionCost(Ty, CondTy, IsUnsigned, CostKind); 2111 2112 assert((isa<ScalableVectorType>(Ty) == isa<ScalableVectorType>(CondTy)) && 2113 "Both vector needs to be equally scalable"); 2114 2115 InstructionCost LegalizationCost = 0; 2116 if (LT.first > 1) { 2117 Type *LegalVTy = EVT(LT.second).getTypeForEVT(Ty->getContext()); 2118 unsigned MinMaxOpcode = 2119 Ty->isFPOrFPVectorTy() 2120 ? Intrinsic::maxnum 2121 : (IsUnsigned ? Intrinsic::umin : Intrinsic::smin); 2122 IntrinsicCostAttributes Attrs(MinMaxOpcode, LegalVTy, {LegalVTy, LegalVTy}); 2123 LegalizationCost = getIntrinsicInstrCost(Attrs, CostKind) * (LT.first - 1); 2124 } 2125 2126 return LegalizationCost + /*Cost of horizontal reduction*/ 2; 2127 } 2128 2129 InstructionCost AArch64TTIImpl::getArithmeticReductionCostSVE( 2130 unsigned Opcode, VectorType *ValTy, TTI::TargetCostKind CostKind) { 2131 std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); 2132 InstructionCost LegalizationCost = 0; 2133 if (LT.first > 1) { 2134 Type *LegalVTy = EVT(LT.second).getTypeForEVT(ValTy->getContext()); 2135 LegalizationCost = getArithmeticInstrCost(Opcode, LegalVTy, CostKind); 2136 LegalizationCost *= LT.first - 1; 2137 } 2138 2139 int ISD = TLI->InstructionOpcodeToISD(Opcode); 2140 assert(ISD && "Invalid opcode"); 2141 // Add the final reduction cost for the legal horizontal reduction 2142 switch (ISD) { 2143 case ISD::ADD: 2144 case ISD::AND: 2145 case ISD::OR: 2146 case ISD::XOR: 2147 case ISD::FADD: 2148 return LegalizationCost + 2; 2149 default: 2150 return InstructionCost::getInvalid(); 2151 } 2152 } 2153 2154 InstructionCost 2155 AArch64TTIImpl::getArithmeticReductionCost(unsigned Opcode, VectorType *ValTy, 2156 Optional<FastMathFlags> FMF, 2157 TTI::TargetCostKind CostKind) { 2158 if (TTI::requiresOrderedReduction(FMF)) { 2159 if (auto *FixedVTy = dyn_cast<FixedVectorType>(ValTy)) { 2160 InstructionCost BaseCost = 2161 BaseT::getArithmeticReductionCost(Opcode, ValTy, FMF, CostKind); 2162 // Add on extra cost to reflect the extra overhead on some CPUs. We still 2163 // end up vectorizing for more computationally intensive loops. 2164 return BaseCost + FixedVTy->getNumElements(); 2165 } 2166 2167 if (Opcode != Instruction::FAdd) 2168 return InstructionCost::getInvalid(); 2169 2170 auto *VTy = cast<ScalableVectorType>(ValTy); 2171 InstructionCost Cost = 2172 getArithmeticInstrCost(Opcode, VTy->getScalarType(), CostKind); 2173 Cost *= getMaxNumElements(VTy->getElementCount()); 2174 return Cost; 2175 } 2176 2177 if (isa<ScalableVectorType>(ValTy)) 2178 return getArithmeticReductionCostSVE(Opcode, ValTy, CostKind); 2179 2180 std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); 2181 MVT MTy = LT.second; 2182 int ISD = TLI->InstructionOpcodeToISD(Opcode); 2183 assert(ISD && "Invalid opcode"); 2184 2185 // Horizontal adds can use the 'addv' instruction. We model the cost of these 2186 // instructions as twice a normal vector add, plus 1 for each legalization 2187 // step (LT.first). This is the only arithmetic vector reduction operation for 2188 // which we have an instruction. 2189 // OR, XOR and AND costs should match the codegen from: 2190 // OR: llvm/test/CodeGen/AArch64/reduce-or.ll 2191 // XOR: llvm/test/CodeGen/AArch64/reduce-xor.ll 2192 // AND: llvm/test/CodeGen/AArch64/reduce-and.ll 2193 static const CostTblEntry CostTblNoPairwise[]{ 2194 {ISD::ADD, MVT::v8i8, 2}, 2195 {ISD::ADD, MVT::v16i8, 2}, 2196 {ISD::ADD, MVT::v4i16, 2}, 2197 {ISD::ADD, MVT::v8i16, 2}, 2198 {ISD::ADD, MVT::v4i32, 2}, 2199 {ISD::OR, MVT::v8i8, 15}, 2200 {ISD::OR, MVT::v16i8, 17}, 2201 {ISD::OR, MVT::v4i16, 7}, 2202 {ISD::OR, MVT::v8i16, 9}, 2203 {ISD::OR, MVT::v2i32, 3}, 2204 {ISD::OR, MVT::v4i32, 5}, 2205 {ISD::OR, MVT::v2i64, 3}, 2206 {ISD::XOR, MVT::v8i8, 15}, 2207 {ISD::XOR, MVT::v16i8, 17}, 2208 {ISD::XOR, MVT::v4i16, 7}, 2209 {ISD::XOR, MVT::v8i16, 9}, 2210 {ISD::XOR, MVT::v2i32, 3}, 2211 {ISD::XOR, MVT::v4i32, 5}, 2212 {ISD::XOR, MVT::v2i64, 3}, 2213 {ISD::AND, MVT::v8i8, 15}, 2214 {ISD::AND, MVT::v16i8, 17}, 2215 {ISD::AND, MVT::v4i16, 7}, 2216 {ISD::AND, MVT::v8i16, 9}, 2217 {ISD::AND, MVT::v2i32, 3}, 2218 {ISD::AND, MVT::v4i32, 5}, 2219 {ISD::AND, MVT::v2i64, 3}, 2220 }; 2221 switch (ISD) { 2222 default: 2223 break; 2224 case ISD::ADD: 2225 if (const auto *Entry = CostTableLookup(CostTblNoPairwise, ISD, MTy)) 2226 return (LT.first - 1) + Entry->Cost; 2227 break; 2228 case ISD::XOR: 2229 case ISD::AND: 2230 case ISD::OR: 2231 const auto *Entry = CostTableLookup(CostTblNoPairwise, ISD, MTy); 2232 if (!Entry) 2233 break; 2234 auto *ValVTy = cast<FixedVectorType>(ValTy); 2235 if (!ValVTy->getElementType()->isIntegerTy(1) && 2236 MTy.getVectorNumElements() <= ValVTy->getNumElements() && 2237 isPowerOf2_32(ValVTy->getNumElements())) { 2238 InstructionCost ExtraCost = 0; 2239 if (LT.first != 1) { 2240 // Type needs to be split, so there is an extra cost of LT.first - 1 2241 // arithmetic ops. 2242 auto *Ty = FixedVectorType::get(ValTy->getElementType(), 2243 MTy.getVectorNumElements()); 2244 ExtraCost = getArithmeticInstrCost(Opcode, Ty, CostKind); 2245 ExtraCost *= LT.first - 1; 2246 } 2247 return Entry->Cost + ExtraCost; 2248 } 2249 break; 2250 } 2251 return BaseT::getArithmeticReductionCost(Opcode, ValTy, FMF, CostKind); 2252 } 2253 2254 InstructionCost AArch64TTIImpl::getSpliceCost(VectorType *Tp, int Index) { 2255 static const CostTblEntry ShuffleTbl[] = { 2256 { TTI::SK_Splice, MVT::nxv16i8, 1 }, 2257 { TTI::SK_Splice, MVT::nxv8i16, 1 }, 2258 { TTI::SK_Splice, MVT::nxv4i32, 1 }, 2259 { TTI::SK_Splice, MVT::nxv2i64, 1 }, 2260 { TTI::SK_Splice, MVT::nxv2f16, 1 }, 2261 { TTI::SK_Splice, MVT::nxv4f16, 1 }, 2262 { TTI::SK_Splice, MVT::nxv8f16, 1 }, 2263 { TTI::SK_Splice, MVT::nxv2bf16, 1 }, 2264 { TTI::SK_Splice, MVT::nxv4bf16, 1 }, 2265 { TTI::SK_Splice, MVT::nxv8bf16, 1 }, 2266 { TTI::SK_Splice, MVT::nxv2f32, 1 }, 2267 { TTI::SK_Splice, MVT::nxv4f32, 1 }, 2268 { TTI::SK_Splice, MVT::nxv2f64, 1 }, 2269 }; 2270 2271 std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); 2272 Type *LegalVTy = EVT(LT.second).getTypeForEVT(Tp->getContext()); 2273 TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput; 2274 EVT PromotedVT = LT.second.getScalarType() == MVT::i1 2275 ? TLI->getPromotedVTForPredicate(EVT(LT.second)) 2276 : LT.second; 2277 Type *PromotedVTy = EVT(PromotedVT).getTypeForEVT(Tp->getContext()); 2278 InstructionCost LegalizationCost = 0; 2279 if (Index < 0) { 2280 LegalizationCost = 2281 getCmpSelInstrCost(Instruction::ICmp, PromotedVTy, PromotedVTy, 2282 CmpInst::BAD_ICMP_PREDICATE, CostKind) + 2283 getCmpSelInstrCost(Instruction::Select, PromotedVTy, LegalVTy, 2284 CmpInst::BAD_ICMP_PREDICATE, CostKind); 2285 } 2286 2287 // Predicated splice are promoted when lowering. See AArch64ISelLowering.cpp 2288 // Cost performed on a promoted type. 2289 if (LT.second.getScalarType() == MVT::i1) { 2290 LegalizationCost += 2291 getCastInstrCost(Instruction::ZExt, PromotedVTy, LegalVTy, 2292 TTI::CastContextHint::None, CostKind) + 2293 getCastInstrCost(Instruction::Trunc, LegalVTy, PromotedVTy, 2294 TTI::CastContextHint::None, CostKind); 2295 } 2296 const auto *Entry = 2297 CostTableLookup(ShuffleTbl, TTI::SK_Splice, PromotedVT.getSimpleVT()); 2298 assert(Entry && "Illegal Type for Splice"); 2299 LegalizationCost += Entry->Cost; 2300 return LegalizationCost * LT.first; 2301 } 2302 2303 InstructionCost AArch64TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, 2304 VectorType *Tp, 2305 ArrayRef<int> Mask, int Index, 2306 VectorType *SubTp) { 2307 Kind = improveShuffleKindFromMask(Kind, Mask); 2308 if (Kind == TTI::SK_Broadcast || Kind == TTI::SK_Transpose || 2309 Kind == TTI::SK_Select || Kind == TTI::SK_PermuteSingleSrc || 2310 Kind == TTI::SK_Reverse) { 2311 static const CostTblEntry ShuffleTbl[] = { 2312 // Broadcast shuffle kinds can be performed with 'dup'. 2313 { TTI::SK_Broadcast, MVT::v8i8, 1 }, 2314 { TTI::SK_Broadcast, MVT::v16i8, 1 }, 2315 { TTI::SK_Broadcast, MVT::v4i16, 1 }, 2316 { TTI::SK_Broadcast, MVT::v8i16, 1 }, 2317 { TTI::SK_Broadcast, MVT::v2i32, 1 }, 2318 { TTI::SK_Broadcast, MVT::v4i32, 1 }, 2319 { TTI::SK_Broadcast, MVT::v2i64, 1 }, 2320 { TTI::SK_Broadcast, MVT::v2f32, 1 }, 2321 { TTI::SK_Broadcast, MVT::v4f32, 1 }, 2322 { TTI::SK_Broadcast, MVT::v2f64, 1 }, 2323 // Transpose shuffle kinds can be performed with 'trn1/trn2' and 2324 // 'zip1/zip2' instructions. 2325 { TTI::SK_Transpose, MVT::v8i8, 1 }, 2326 { TTI::SK_Transpose, MVT::v16i8, 1 }, 2327 { TTI::SK_Transpose, MVT::v4i16, 1 }, 2328 { TTI::SK_Transpose, MVT::v8i16, 1 }, 2329 { TTI::SK_Transpose, MVT::v2i32, 1 }, 2330 { TTI::SK_Transpose, MVT::v4i32, 1 }, 2331 { TTI::SK_Transpose, MVT::v2i64, 1 }, 2332 { TTI::SK_Transpose, MVT::v2f32, 1 }, 2333 { TTI::SK_Transpose, MVT::v4f32, 1 }, 2334 { TTI::SK_Transpose, MVT::v2f64, 1 }, 2335 // Select shuffle kinds. 2336 // TODO: handle vXi8/vXi16. 2337 { TTI::SK_Select, MVT::v2i32, 1 }, // mov. 2338 { TTI::SK_Select, MVT::v4i32, 2 }, // rev+trn (or similar). 2339 { TTI::SK_Select, MVT::v2i64, 1 }, // mov. 2340 { TTI::SK_Select, MVT::v2f32, 1 }, // mov. 2341 { TTI::SK_Select, MVT::v4f32, 2 }, // rev+trn (or similar). 2342 { TTI::SK_Select, MVT::v2f64, 1 }, // mov. 2343 // PermuteSingleSrc shuffle kinds. 2344 { TTI::SK_PermuteSingleSrc, MVT::v2i32, 1 }, // mov. 2345 { TTI::SK_PermuteSingleSrc, MVT::v4i32, 3 }, // perfectshuffle worst case. 2346 { TTI::SK_PermuteSingleSrc, MVT::v2i64, 1 }, // mov. 2347 { TTI::SK_PermuteSingleSrc, MVT::v2f32, 1 }, // mov. 2348 { TTI::SK_PermuteSingleSrc, MVT::v4f32, 3 }, // perfectshuffle worst case. 2349 { TTI::SK_PermuteSingleSrc, MVT::v2f64, 1 }, // mov. 2350 { TTI::SK_PermuteSingleSrc, MVT::v4i16, 3 }, // perfectshuffle worst case. 2351 { TTI::SK_PermuteSingleSrc, MVT::v4f16, 3 }, // perfectshuffle worst case. 2352 { TTI::SK_PermuteSingleSrc, MVT::v4bf16, 3 }, // perfectshuffle worst case. 2353 { TTI::SK_PermuteSingleSrc, MVT::v8i16, 8 }, // constpool + load + tbl 2354 { TTI::SK_PermuteSingleSrc, MVT::v8f16, 8 }, // constpool + load + tbl 2355 { TTI::SK_PermuteSingleSrc, MVT::v8bf16, 8 }, // constpool + load + tbl 2356 { TTI::SK_PermuteSingleSrc, MVT::v8i8, 8 }, // constpool + load + tbl 2357 { TTI::SK_PermuteSingleSrc, MVT::v16i8, 8 }, // constpool + load + tbl 2358 // Reverse can be lowered with `rev`. 2359 { TTI::SK_Reverse, MVT::v2i32, 1 }, // mov. 2360 { TTI::SK_Reverse, MVT::v4i32, 2 }, // REV64; EXT 2361 { TTI::SK_Reverse, MVT::v2i64, 1 }, // mov. 2362 { TTI::SK_Reverse, MVT::v2f32, 1 }, // mov. 2363 { TTI::SK_Reverse, MVT::v4f32, 2 }, // REV64; EXT 2364 { TTI::SK_Reverse, MVT::v2f64, 1 }, // mov. 2365 // Broadcast shuffle kinds for scalable vectors 2366 { TTI::SK_Broadcast, MVT::nxv16i8, 1 }, 2367 { TTI::SK_Broadcast, MVT::nxv8i16, 1 }, 2368 { TTI::SK_Broadcast, MVT::nxv4i32, 1 }, 2369 { TTI::SK_Broadcast, MVT::nxv2i64, 1 }, 2370 { TTI::SK_Broadcast, MVT::nxv2f16, 1 }, 2371 { TTI::SK_Broadcast, MVT::nxv4f16, 1 }, 2372 { TTI::SK_Broadcast, MVT::nxv8f16, 1 }, 2373 { TTI::SK_Broadcast, MVT::nxv2bf16, 1 }, 2374 { TTI::SK_Broadcast, MVT::nxv4bf16, 1 }, 2375 { TTI::SK_Broadcast, MVT::nxv8bf16, 1 }, 2376 { TTI::SK_Broadcast, MVT::nxv2f32, 1 }, 2377 { TTI::SK_Broadcast, MVT::nxv4f32, 1 }, 2378 { TTI::SK_Broadcast, MVT::nxv2f64, 1 }, 2379 { TTI::SK_Broadcast, MVT::nxv16i1, 1 }, 2380 { TTI::SK_Broadcast, MVT::nxv8i1, 1 }, 2381 { TTI::SK_Broadcast, MVT::nxv4i1, 1 }, 2382 { TTI::SK_Broadcast, MVT::nxv2i1, 1 }, 2383 // Handle the cases for vector.reverse with scalable vectors 2384 { TTI::SK_Reverse, MVT::nxv16i8, 1 }, 2385 { TTI::SK_Reverse, MVT::nxv8i16, 1 }, 2386 { TTI::SK_Reverse, MVT::nxv4i32, 1 }, 2387 { TTI::SK_Reverse, MVT::nxv2i64, 1 }, 2388 { TTI::SK_Reverse, MVT::nxv2f16, 1 }, 2389 { TTI::SK_Reverse, MVT::nxv4f16, 1 }, 2390 { TTI::SK_Reverse, MVT::nxv8f16, 1 }, 2391 { TTI::SK_Reverse, MVT::nxv2bf16, 1 }, 2392 { TTI::SK_Reverse, MVT::nxv4bf16, 1 }, 2393 { TTI::SK_Reverse, MVT::nxv8bf16, 1 }, 2394 { TTI::SK_Reverse, MVT::nxv2f32, 1 }, 2395 { TTI::SK_Reverse, MVT::nxv4f32, 1 }, 2396 { TTI::SK_Reverse, MVT::nxv2f64, 1 }, 2397 { TTI::SK_Reverse, MVT::nxv16i1, 1 }, 2398 { TTI::SK_Reverse, MVT::nxv8i1, 1 }, 2399 { TTI::SK_Reverse, MVT::nxv4i1, 1 }, 2400 { TTI::SK_Reverse, MVT::nxv2i1, 1 }, 2401 }; 2402 std::pair<InstructionCost, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); 2403 if (const auto *Entry = CostTableLookup(ShuffleTbl, Kind, LT.second)) 2404 return LT.first * Entry->Cost; 2405 } 2406 if (Kind == TTI::SK_Splice && isa<ScalableVectorType>(Tp)) 2407 return getSpliceCost(Tp, Index); 2408 return BaseT::getShuffleCost(Kind, Tp, Mask, Index, SubTp); 2409 } 2410