1 //===-- SystemZTargetTransformInfo.cpp - SystemZ-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 // This file implements a TargetTransformInfo analysis pass specific to the 10 // SystemZ target machine. It uses the target's detailed information to provide 11 // more precise answers to certain TTI queries, while letting the target 12 // independent and default TTI implementations handle the rest. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "SystemZTargetTransformInfo.h" 17 #include "llvm/Analysis/TargetTransformInfo.h" 18 #include "llvm/CodeGen/BasicTTIImpl.h" 19 #include "llvm/CodeGen/CostTable.h" 20 #include "llvm/CodeGen/TargetLowering.h" 21 #include "llvm/IR/IntrinsicInst.h" 22 #include "llvm/Support/Debug.h" 23 using namespace llvm; 24 25 #define DEBUG_TYPE "systemztti" 26 27 //===----------------------------------------------------------------------===// 28 // 29 // SystemZ cost model. 30 // 31 //===----------------------------------------------------------------------===// 32 33 int SystemZTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) { 34 assert(Ty->isIntegerTy()); 35 36 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 37 // There is no cost model for constants with a bit size of 0. Return TCC_Free 38 // here, so that constant hoisting will ignore this constant. 39 if (BitSize == 0) 40 return TTI::TCC_Free; 41 // No cost model for operations on integers larger than 64 bit implemented yet. 42 if (BitSize > 64) 43 return TTI::TCC_Free; 44 45 if (Imm == 0) 46 return TTI::TCC_Free; 47 48 if (Imm.getBitWidth() <= 64) { 49 // Constants loaded via lgfi. 50 if (isInt<32>(Imm.getSExtValue())) 51 return TTI::TCC_Basic; 52 // Constants loaded via llilf. 53 if (isUInt<32>(Imm.getZExtValue())) 54 return TTI::TCC_Basic; 55 // Constants loaded via llihf: 56 if ((Imm.getZExtValue() & 0xffffffff) == 0) 57 return TTI::TCC_Basic; 58 59 return 2 * TTI::TCC_Basic; 60 } 61 62 return 4 * TTI::TCC_Basic; 63 } 64 65 int SystemZTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx, 66 const APInt &Imm, Type *Ty) { 67 assert(Ty->isIntegerTy()); 68 69 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 70 // There is no cost model for constants with a bit size of 0. Return TCC_Free 71 // here, so that constant hoisting will ignore this constant. 72 if (BitSize == 0) 73 return TTI::TCC_Free; 74 // No cost model for operations on integers larger than 64 bit implemented yet. 75 if (BitSize > 64) 76 return TTI::TCC_Free; 77 78 switch (Opcode) { 79 default: 80 return TTI::TCC_Free; 81 case Instruction::GetElementPtr: 82 // Always hoist the base address of a GetElementPtr. This prevents the 83 // creation of new constants for every base constant that gets constant 84 // folded with the offset. 85 if (Idx == 0) 86 return 2 * TTI::TCC_Basic; 87 return TTI::TCC_Free; 88 case Instruction::Store: 89 if (Idx == 0 && Imm.getBitWidth() <= 64) { 90 // Any 8-bit immediate store can by implemented via mvi. 91 if (BitSize == 8) 92 return TTI::TCC_Free; 93 // 16-bit immediate values can be stored via mvhhi/mvhi/mvghi. 94 if (isInt<16>(Imm.getSExtValue())) 95 return TTI::TCC_Free; 96 } 97 break; 98 case Instruction::ICmp: 99 if (Idx == 1 && Imm.getBitWidth() <= 64) { 100 // Comparisons against signed 32-bit immediates implemented via cgfi. 101 if (isInt<32>(Imm.getSExtValue())) 102 return TTI::TCC_Free; 103 // Comparisons against unsigned 32-bit immediates implemented via clgfi. 104 if (isUInt<32>(Imm.getZExtValue())) 105 return TTI::TCC_Free; 106 } 107 break; 108 case Instruction::Add: 109 case Instruction::Sub: 110 if (Idx == 1 && Imm.getBitWidth() <= 64) { 111 // We use algfi/slgfi to add/subtract 32-bit unsigned immediates. 112 if (isUInt<32>(Imm.getZExtValue())) 113 return TTI::TCC_Free; 114 // Or their negation, by swapping addition vs. subtraction. 115 if (isUInt<32>(-Imm.getSExtValue())) 116 return TTI::TCC_Free; 117 } 118 break; 119 case Instruction::Mul: 120 if (Idx == 1 && Imm.getBitWidth() <= 64) { 121 // We use msgfi to multiply by 32-bit signed immediates. 122 if (isInt<32>(Imm.getSExtValue())) 123 return TTI::TCC_Free; 124 } 125 break; 126 case Instruction::Or: 127 case Instruction::Xor: 128 if (Idx == 1 && Imm.getBitWidth() <= 64) { 129 // Masks supported by oilf/xilf. 130 if (isUInt<32>(Imm.getZExtValue())) 131 return TTI::TCC_Free; 132 // Masks supported by oihf/xihf. 133 if ((Imm.getZExtValue() & 0xffffffff) == 0) 134 return TTI::TCC_Free; 135 } 136 break; 137 case Instruction::And: 138 if (Idx == 1 && Imm.getBitWidth() <= 64) { 139 // Any 32-bit AND operation can by implemented via nilf. 140 if (BitSize <= 32) 141 return TTI::TCC_Free; 142 // 64-bit masks supported by nilf. 143 if (isUInt<32>(~Imm.getZExtValue())) 144 return TTI::TCC_Free; 145 // 64-bit masks supported by nilh. 146 if ((Imm.getZExtValue() & 0xffffffff) == 0xffffffff) 147 return TTI::TCC_Free; 148 // Some 64-bit AND operations can be implemented via risbg. 149 const SystemZInstrInfo *TII = ST->getInstrInfo(); 150 unsigned Start, End; 151 if (TII->isRxSBGMask(Imm.getZExtValue(), BitSize, Start, End)) 152 return TTI::TCC_Free; 153 } 154 break; 155 case Instruction::Shl: 156 case Instruction::LShr: 157 case Instruction::AShr: 158 // Always return TCC_Free for the shift value of a shift instruction. 159 if (Idx == 1) 160 return TTI::TCC_Free; 161 break; 162 case Instruction::UDiv: 163 case Instruction::SDiv: 164 case Instruction::URem: 165 case Instruction::SRem: 166 case Instruction::Trunc: 167 case Instruction::ZExt: 168 case Instruction::SExt: 169 case Instruction::IntToPtr: 170 case Instruction::PtrToInt: 171 case Instruction::BitCast: 172 case Instruction::PHI: 173 case Instruction::Call: 174 case Instruction::Select: 175 case Instruction::Ret: 176 case Instruction::Load: 177 break; 178 } 179 180 return SystemZTTIImpl::getIntImmCost(Imm, Ty); 181 } 182 183 int SystemZTTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, 184 const APInt &Imm, Type *Ty) { 185 assert(Ty->isIntegerTy()); 186 187 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 188 // There is no cost model for constants with a bit size of 0. Return TCC_Free 189 // here, so that constant hoisting will ignore this constant. 190 if (BitSize == 0) 191 return TTI::TCC_Free; 192 // No cost model for operations on integers larger than 64 bit implemented yet. 193 if (BitSize > 64) 194 return TTI::TCC_Free; 195 196 switch (IID) { 197 default: 198 return TTI::TCC_Free; 199 case Intrinsic::sadd_with_overflow: 200 case Intrinsic::uadd_with_overflow: 201 case Intrinsic::ssub_with_overflow: 202 case Intrinsic::usub_with_overflow: 203 // These get expanded to include a normal addition/subtraction. 204 if (Idx == 1 && Imm.getBitWidth() <= 64) { 205 if (isUInt<32>(Imm.getZExtValue())) 206 return TTI::TCC_Free; 207 if (isUInt<32>(-Imm.getSExtValue())) 208 return TTI::TCC_Free; 209 } 210 break; 211 case Intrinsic::smul_with_overflow: 212 case Intrinsic::umul_with_overflow: 213 // These get expanded to include a normal multiplication. 214 if (Idx == 1 && Imm.getBitWidth() <= 64) { 215 if (isInt<32>(Imm.getSExtValue())) 216 return TTI::TCC_Free; 217 } 218 break; 219 case Intrinsic::experimental_stackmap: 220 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 221 return TTI::TCC_Free; 222 break; 223 case Intrinsic::experimental_patchpoint_void: 224 case Intrinsic::experimental_patchpoint_i64: 225 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 226 return TTI::TCC_Free; 227 break; 228 } 229 return SystemZTTIImpl::getIntImmCost(Imm, Ty); 230 } 231 232 TargetTransformInfo::PopcntSupportKind 233 SystemZTTIImpl::getPopcntSupport(unsigned TyWidth) { 234 assert(isPowerOf2_32(TyWidth) && "Type width must be power of 2"); 235 if (ST->hasPopulationCount() && TyWidth <= 64) 236 return TTI::PSK_FastHardware; 237 return TTI::PSK_Software; 238 } 239 240 void SystemZTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE, 241 TTI::UnrollingPreferences &UP) { 242 // Find out if L contains a call, what the machine instruction count 243 // estimate is, and how many stores there are. 244 bool HasCall = false; 245 unsigned NumStores = 0; 246 for (auto &BB : L->blocks()) 247 for (auto &I : *BB) { 248 if (isa<CallInst>(&I) || isa<InvokeInst>(&I)) { 249 ImmutableCallSite CS(&I); 250 if (const Function *F = CS.getCalledFunction()) { 251 if (isLoweredToCall(F)) 252 HasCall = true; 253 if (F->getIntrinsicID() == Intrinsic::memcpy || 254 F->getIntrinsicID() == Intrinsic::memset) 255 NumStores++; 256 } else { // indirect call. 257 HasCall = true; 258 } 259 } 260 if (isa<StoreInst>(&I)) { 261 Type *MemAccessTy = I.getOperand(0)->getType(); 262 NumStores += getMemoryOpCost(Instruction::Store, MemAccessTy, None, 0); 263 } 264 } 265 266 // The z13 processor will run out of store tags if too many stores 267 // are fed into it too quickly. Therefore make sure there are not 268 // too many stores in the resulting unrolled loop. 269 unsigned const Max = (NumStores ? (12 / NumStores) : UINT_MAX); 270 271 if (HasCall) { 272 // Only allow full unrolling if loop has any calls. 273 UP.FullUnrollMaxCount = Max; 274 UP.MaxCount = 1; 275 return; 276 } 277 278 UP.MaxCount = Max; 279 if (UP.MaxCount <= 1) 280 return; 281 282 // Allow partial and runtime trip count unrolling. 283 UP.Partial = UP.Runtime = true; 284 285 UP.PartialThreshold = 75; 286 UP.DefaultUnrollRuntimeCount = 4; 287 288 // Allow expensive instructions in the pre-header of the loop. 289 UP.AllowExpensiveTripCount = true; 290 291 UP.Force = true; 292 } 293 294 295 bool SystemZTTIImpl::isLSRCostLess(TargetTransformInfo::LSRCost &C1, 296 TargetTransformInfo::LSRCost &C2) { 297 // SystemZ specific: check instruction count (first), and don't care about 298 // ImmCost, since offsets are checked explicitly. 299 return std::tie(C1.Insns, C1.NumRegs, C1.AddRecCost, 300 C1.NumIVMuls, C1.NumBaseAdds, 301 C1.ScaleCost, C1.SetupCost) < 302 std::tie(C2.Insns, C2.NumRegs, C2.AddRecCost, 303 C2.NumIVMuls, C2.NumBaseAdds, 304 C2.ScaleCost, C2.SetupCost); 305 } 306 307 unsigned SystemZTTIImpl::getNumberOfRegisters(unsigned ClassID) const { 308 bool Vector = (ClassID == 1); 309 if (!Vector) 310 // Discount the stack pointer. Also leave out %r0, since it can't 311 // be used in an address. 312 return 14; 313 if (ST->hasVector()) 314 return 32; 315 return 0; 316 } 317 318 unsigned SystemZTTIImpl::getRegisterBitWidth(bool Vector) const { 319 if (!Vector) 320 return 64; 321 if (ST->hasVector()) 322 return 128; 323 return 0; 324 } 325 326 unsigned SystemZTTIImpl::getMinPrefetchStride(unsigned NumMemAccesses, 327 unsigned NumStridedMemAccesses, 328 unsigned NumPrefetches, 329 bool HasCall) const { 330 // Don't prefetch a loop with many far apart accesses. 331 if (NumPrefetches > 16) 332 return UINT_MAX; 333 334 // Emit prefetch instructions for smaller strides in cases where we think 335 // the hardware prefetcher might not be able to keep up. 336 if (NumStridedMemAccesses > 32 && 337 NumStridedMemAccesses == NumMemAccesses && !HasCall) 338 return 1; 339 340 return ST->hasMiscellaneousExtensions3() ? 8192 : 2048; 341 } 342 343 bool SystemZTTIImpl::hasDivRemOp(Type *DataType, bool IsSigned) { 344 EVT VT = TLI->getValueType(DL, DataType); 345 return (VT.isScalarInteger() && TLI->isTypeLegal(VT)); 346 } 347 348 // Return the bit size for the scalar type or vector element 349 // type. getScalarSizeInBits() returns 0 for a pointer type. 350 static unsigned getScalarSizeInBits(Type *Ty) { 351 unsigned Size = 352 (Ty->isPtrOrPtrVectorTy() ? 64U : Ty->getScalarSizeInBits()); 353 assert(Size > 0 && "Element must have non-zero size."); 354 return Size; 355 } 356 357 // getNumberOfParts() calls getTypeLegalizationCost() which splits the vector 358 // type until it is legal. This would e.g. return 4 for <6 x i64>, instead of 359 // 3. 360 static unsigned getNumVectorRegs(Type *Ty) { 361 assert(Ty->isVectorTy() && "Expected vector type"); 362 unsigned WideBits = 363 getScalarSizeInBits(Ty) * cast<VectorType>(Ty)->getNumElements(); 364 assert(WideBits > 0 && "Could not compute size of vector"); 365 return ((WideBits % 128U) ? ((WideBits / 128U) + 1) : (WideBits / 128U)); 366 } 367 368 int SystemZTTIImpl::getArithmeticInstrCost( 369 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info, 370 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo, 371 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args, 372 const Instruction *CxtI) { 373 374 // TODO: return a good value for BB-VECTORIZER that includes the 375 // immediate loads, which we do not want to count for the loop 376 // vectorizer, since they are hopefully hoisted out of the loop. This 377 // would require a new parameter 'InLoop', but not sure if constant 378 // args are common enough to motivate this. 379 380 unsigned ScalarBits = Ty->getScalarSizeInBits(); 381 382 // There are thre cases of division and remainder: Dividing with a register 383 // needs a divide instruction. A divisor which is a power of two constant 384 // can be implemented with a sequence of shifts. Any other constant needs a 385 // multiply and shifts. 386 const unsigned DivInstrCost = 20; 387 const unsigned DivMulSeqCost = 10; 388 const unsigned SDivPow2Cost = 4; 389 390 bool SignedDivRem = 391 Opcode == Instruction::SDiv || Opcode == Instruction::SRem; 392 bool UnsignedDivRem = 393 Opcode == Instruction::UDiv || Opcode == Instruction::URem; 394 395 // Check for a constant divisor. 396 bool DivRemConst = false; 397 bool DivRemConstPow2 = false; 398 if ((SignedDivRem || UnsignedDivRem) && Args.size() == 2) { 399 if (const Constant *C = dyn_cast<Constant>(Args[1])) { 400 const ConstantInt *CVal = 401 (C->getType()->isVectorTy() 402 ? dyn_cast_or_null<const ConstantInt>(C->getSplatValue()) 403 : dyn_cast<const ConstantInt>(C)); 404 if (CVal != nullptr && 405 (CVal->getValue().isPowerOf2() || (-CVal->getValue()).isPowerOf2())) 406 DivRemConstPow2 = true; 407 else 408 DivRemConst = true; 409 } 410 } 411 412 if (!Ty->isVectorTy()) { 413 // These FP operations are supported with a dedicated instruction for 414 // float, double and fp128 (base implementation assumes float generally 415 // costs 2). 416 if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub || 417 Opcode == Instruction::FMul || Opcode == Instruction::FDiv) 418 return 1; 419 420 // There is no native support for FRem. 421 if (Opcode == Instruction::FRem) 422 return LIBCALL_COST; 423 424 // Give discount for some combined logical operations if supported. 425 if (Args.size() == 2 && ST->hasMiscellaneousExtensions3()) { 426 if (Opcode == Instruction::Xor) { 427 for (const Value *A : Args) { 428 if (const Instruction *I = dyn_cast<Instruction>(A)) 429 if (I->hasOneUse() && 430 (I->getOpcode() == Instruction::And || 431 I->getOpcode() == Instruction::Or || 432 I->getOpcode() == Instruction::Xor)) 433 return 0; 434 } 435 } 436 else if (Opcode == Instruction::Or || Opcode == Instruction::And) { 437 for (const Value *A : Args) { 438 if (const Instruction *I = dyn_cast<Instruction>(A)) 439 if (I->hasOneUse() && I->getOpcode() == Instruction::Xor) 440 return 0; 441 } 442 } 443 } 444 445 // Or requires one instruction, although it has custom handling for i64. 446 if (Opcode == Instruction::Or) 447 return 1; 448 449 if (Opcode == Instruction::Xor && ScalarBits == 1) { 450 if (ST->hasLoadStoreOnCond2()) 451 return 5; // 2 * (li 0; loc 1); xor 452 return 7; // 2 * ipm sequences ; xor ; shift ; compare 453 } 454 455 if (DivRemConstPow2) 456 return (SignedDivRem ? SDivPow2Cost : 1); 457 if (DivRemConst) 458 return DivMulSeqCost; 459 if (SignedDivRem || UnsignedDivRem) 460 return DivInstrCost; 461 } 462 else if (ST->hasVector()) { 463 unsigned VF = cast<VectorType>(Ty)->getNumElements(); 464 unsigned NumVectors = getNumVectorRegs(Ty); 465 466 // These vector operations are custom handled, but are still supported 467 // with one instruction per vector, regardless of element size. 468 if (Opcode == Instruction::Shl || Opcode == Instruction::LShr || 469 Opcode == Instruction::AShr) { 470 return NumVectors; 471 } 472 473 if (DivRemConstPow2) 474 return (NumVectors * (SignedDivRem ? SDivPow2Cost : 1)); 475 if (DivRemConst) 476 return VF * DivMulSeqCost + getScalarizationOverhead(Ty, Args); 477 if ((SignedDivRem || UnsignedDivRem) && VF > 4) 478 // Temporary hack: disable high vectorization factors with integer 479 // division/remainder, which will get scalarized and handled with 480 // GR128 registers. The mischeduler is not clever enough to avoid 481 // spilling yet. 482 return 1000; 483 484 // These FP operations are supported with a single vector instruction for 485 // double (base implementation assumes float generally costs 2). For 486 // FP128, the scalar cost is 1, and there is no overhead since the values 487 // are already in scalar registers. 488 if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub || 489 Opcode == Instruction::FMul || Opcode == Instruction::FDiv) { 490 switch (ScalarBits) { 491 case 32: { 492 // The vector enhancements facility 1 provides v4f32 instructions. 493 if (ST->hasVectorEnhancements1()) 494 return NumVectors; 495 // Return the cost of multiple scalar invocation plus the cost of 496 // inserting and extracting the values. 497 unsigned ScalarCost = 498 getArithmeticInstrCost(Opcode, Ty->getScalarType()); 499 unsigned Cost = (VF * ScalarCost) + getScalarizationOverhead(Ty, Args); 500 // FIXME: VF 2 for these FP operations are currently just as 501 // expensive as for VF 4. 502 if (VF == 2) 503 Cost *= 2; 504 return Cost; 505 } 506 case 64: 507 case 128: 508 return NumVectors; 509 default: 510 break; 511 } 512 } 513 514 // There is no native support for FRem. 515 if (Opcode == Instruction::FRem) { 516 unsigned Cost = (VF * LIBCALL_COST) + getScalarizationOverhead(Ty, Args); 517 // FIXME: VF 2 for float is currently just as expensive as for VF 4. 518 if (VF == 2 && ScalarBits == 32) 519 Cost *= 2; 520 return Cost; 521 } 522 } 523 524 // Fallback to the default implementation. 525 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info, 526 Opd1PropInfo, Opd2PropInfo, Args, CxtI); 527 } 528 529 int SystemZTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, VectorType *Tp, 530 int Index, VectorType *SubTp) { 531 if (ST->hasVector()) { 532 unsigned NumVectors = getNumVectorRegs(Tp); 533 534 // TODO: Since fp32 is expanded, the shuffle cost should always be 0. 535 536 // FP128 values are always in scalar registers, so there is no work 537 // involved with a shuffle, except for broadcast. In that case register 538 // moves are done with a single instruction per element. 539 if (Tp->getScalarType()->isFP128Ty()) 540 return (Kind == TargetTransformInfo::SK_Broadcast ? NumVectors - 1 : 0); 541 542 switch (Kind) { 543 case TargetTransformInfo::SK_ExtractSubvector: 544 // ExtractSubvector Index indicates start offset. 545 546 // Extracting a subvector from first index is a noop. 547 return (Index == 0 ? 0 : NumVectors); 548 549 case TargetTransformInfo::SK_Broadcast: 550 // Loop vectorizer calls here to figure out the extra cost of 551 // broadcasting a loaded value to all elements of a vector. Since vlrep 552 // loads and replicates with a single instruction, adjust the returned 553 // value. 554 return NumVectors - 1; 555 556 default: 557 558 // SystemZ supports single instruction permutation / replication. 559 return NumVectors; 560 } 561 } 562 563 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); 564 } 565 566 // Return the log2 difference of the element sizes of the two vector types. 567 static unsigned getElSizeLog2Diff(Type *Ty0, Type *Ty1) { 568 unsigned Bits0 = Ty0->getScalarSizeInBits(); 569 unsigned Bits1 = Ty1->getScalarSizeInBits(); 570 571 if (Bits1 > Bits0) 572 return (Log2_32(Bits1) - Log2_32(Bits0)); 573 574 return (Log2_32(Bits0) - Log2_32(Bits1)); 575 } 576 577 // Return the number of instructions needed to truncate SrcTy to DstTy. 578 unsigned SystemZTTIImpl:: 579 getVectorTruncCost(Type *SrcTy, Type *DstTy) { 580 assert (SrcTy->isVectorTy() && DstTy->isVectorTy()); 581 assert (SrcTy->getPrimitiveSizeInBits() > DstTy->getPrimitiveSizeInBits() && 582 "Packing must reduce size of vector type."); 583 assert(cast<VectorType>(SrcTy)->getNumElements() == 584 cast<VectorType>(DstTy)->getNumElements() && 585 "Packing should not change number of elements."); 586 587 // TODO: Since fp32 is expanded, the extract cost should always be 0. 588 589 unsigned NumParts = getNumVectorRegs(SrcTy); 590 if (NumParts <= 2) 591 // Up to 2 vector registers can be truncated efficiently with pack or 592 // permute. The latter requires an immediate mask to be loaded, which 593 // typically gets hoisted out of a loop. TODO: return a good value for 594 // BB-VECTORIZER that includes the immediate loads, which we do not want 595 // to count for the loop vectorizer. 596 return 1; 597 598 unsigned Cost = 0; 599 unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy); 600 unsigned VF = cast<VectorType>(SrcTy)->getNumElements(); 601 for (unsigned P = 0; P < Log2Diff; ++P) { 602 if (NumParts > 1) 603 NumParts /= 2; 604 Cost += NumParts; 605 } 606 607 // Currently, a general mix of permutes and pack instructions is output by 608 // isel, which follow the cost computation above except for this case which 609 // is one instruction less: 610 if (VF == 8 && SrcTy->getScalarSizeInBits() == 64 && 611 DstTy->getScalarSizeInBits() == 8) 612 Cost--; 613 614 return Cost; 615 } 616 617 // Return the cost of converting a vector bitmask produced by a compare 618 // (SrcTy), to the type of the select or extend instruction (DstTy). 619 unsigned SystemZTTIImpl:: 620 getVectorBitmaskConversionCost(Type *SrcTy, Type *DstTy) { 621 assert (SrcTy->isVectorTy() && DstTy->isVectorTy() && 622 "Should only be called with vector types."); 623 624 unsigned PackCost = 0; 625 unsigned SrcScalarBits = SrcTy->getScalarSizeInBits(); 626 unsigned DstScalarBits = DstTy->getScalarSizeInBits(); 627 unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy); 628 if (SrcScalarBits > DstScalarBits) 629 // The bitmask will be truncated. 630 PackCost = getVectorTruncCost(SrcTy, DstTy); 631 else if (SrcScalarBits < DstScalarBits) { 632 unsigned DstNumParts = getNumVectorRegs(DstTy); 633 // Each vector select needs its part of the bitmask unpacked. 634 PackCost = Log2Diff * DstNumParts; 635 // Extra cost for moving part of mask before unpacking. 636 PackCost += DstNumParts - 1; 637 } 638 639 return PackCost; 640 } 641 642 // Return the type of the compared operands. This is needed to compute the 643 // cost for a Select / ZExt or SExt instruction. 644 static Type *getCmpOpsType(const Instruction *I, unsigned VF = 1) { 645 Type *OpTy = nullptr; 646 if (CmpInst *CI = dyn_cast<CmpInst>(I->getOperand(0))) 647 OpTy = CI->getOperand(0)->getType(); 648 else if (Instruction *LogicI = dyn_cast<Instruction>(I->getOperand(0))) 649 if (LogicI->getNumOperands() == 2) 650 if (CmpInst *CI0 = dyn_cast<CmpInst>(LogicI->getOperand(0))) 651 if (isa<CmpInst>(LogicI->getOperand(1))) 652 OpTy = CI0->getOperand(0)->getType(); 653 654 if (OpTy != nullptr) { 655 if (VF == 1) { 656 assert (!OpTy->isVectorTy() && "Expected scalar type"); 657 return OpTy; 658 } 659 // Return the potentially vectorized type based on 'I' and 'VF'. 'I' may 660 // be either scalar or already vectorized with a same or lesser VF. 661 Type *ElTy = OpTy->getScalarType(); 662 return VectorType::get(ElTy, VF); 663 } 664 665 return nullptr; 666 } 667 668 // Get the cost of converting a boolean vector to a vector with same width 669 // and element size as Dst, plus the cost of zero extending if needed. 670 unsigned SystemZTTIImpl:: 671 getBoolVecToIntConversionCost(unsigned Opcode, Type *Dst, 672 const Instruction *I) { 673 assert (Dst->isVectorTy()); 674 unsigned VF = cast<VectorType>(Dst)->getNumElements(); 675 unsigned Cost = 0; 676 // If we know what the widths of the compared operands, get any cost of 677 // converting it to match Dst. Otherwise assume same widths. 678 Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr); 679 if (CmpOpTy != nullptr) 680 Cost = getVectorBitmaskConversionCost(CmpOpTy, Dst); 681 if (Opcode == Instruction::ZExt || Opcode == Instruction::UIToFP) 682 // One 'vn' per dst vector with an immediate mask. 683 Cost += getNumVectorRegs(Dst); 684 return Cost; 685 } 686 687 int SystemZTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src, 688 const Instruction *I) { 689 unsigned DstScalarBits = Dst->getScalarSizeInBits(); 690 unsigned SrcScalarBits = Src->getScalarSizeInBits(); 691 692 if (!Src->isVectorTy()) { 693 assert (!Dst->isVectorTy()); 694 695 if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP) { 696 if (SrcScalarBits >= 32 || 697 (I != nullptr && isa<LoadInst>(I->getOperand(0)))) 698 return 1; 699 return SrcScalarBits > 1 ? 2 /*i8/i16 extend*/ : 5 /*branch seq.*/; 700 } 701 702 if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) && 703 Src->isIntegerTy(1)) { 704 if (ST->hasLoadStoreOnCond2()) 705 return 2; // li 0; loc 1 706 707 // This should be extension of a compare i1 result, which is done with 708 // ipm and a varying sequence of instructions. 709 unsigned Cost = 0; 710 if (Opcode == Instruction::SExt) 711 Cost = (DstScalarBits < 64 ? 3 : 4); 712 if (Opcode == Instruction::ZExt) 713 Cost = 3; 714 Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I) : nullptr); 715 if (CmpOpTy != nullptr && CmpOpTy->isFloatingPointTy()) 716 // If operands of an fp-type was compared, this costs +1. 717 Cost++; 718 return Cost; 719 } 720 } 721 else if (ST->hasVector()) { 722 assert (Dst->isVectorTy()); 723 unsigned VF = cast<VectorType>(Src)->getNumElements(); 724 unsigned NumDstVectors = getNumVectorRegs(Dst); 725 unsigned NumSrcVectors = getNumVectorRegs(Src); 726 727 if (Opcode == Instruction::Trunc) { 728 if (Src->getScalarSizeInBits() == Dst->getScalarSizeInBits()) 729 return 0; // Check for NOOP conversions. 730 return getVectorTruncCost(Src, Dst); 731 } 732 733 if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) { 734 if (SrcScalarBits >= 8) { 735 // ZExt/SExt will be handled with one unpack per doubling of width. 736 unsigned NumUnpacks = getElSizeLog2Diff(Src, Dst); 737 738 // For types that spans multiple vector registers, some additional 739 // instructions are used to setup the unpacking. 740 unsigned NumSrcVectorOps = 741 (NumUnpacks > 1 ? (NumDstVectors - NumSrcVectors) 742 : (NumDstVectors / 2)); 743 744 return (NumUnpacks * NumDstVectors) + NumSrcVectorOps; 745 } 746 else if (SrcScalarBits == 1) 747 return getBoolVecToIntConversionCost(Opcode, Dst, I); 748 } 749 750 if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP || 751 Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI) { 752 // TODO: Fix base implementation which could simplify things a bit here 753 // (seems to miss on differentiating on scalar/vector types). 754 755 // Only 64 bit vector conversions are natively supported before z15. 756 if (DstScalarBits == 64 || ST->hasVectorEnhancements2()) { 757 if (SrcScalarBits == DstScalarBits) 758 return NumDstVectors; 759 760 if (SrcScalarBits == 1) 761 return getBoolVecToIntConversionCost(Opcode, Dst, I) + NumDstVectors; 762 } 763 764 // Return the cost of multiple scalar invocation plus the cost of 765 // inserting and extracting the values. Base implementation does not 766 // realize float->int gets scalarized. 767 unsigned ScalarCost = getCastInstrCost(Opcode, Dst->getScalarType(), 768 Src->getScalarType()); 769 unsigned TotCost = VF * ScalarCost; 770 bool NeedsInserts = true, NeedsExtracts = true; 771 // FP128 registers do not get inserted or extracted. 772 if (DstScalarBits == 128 && 773 (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP)) 774 NeedsInserts = false; 775 if (SrcScalarBits == 128 && 776 (Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI)) 777 NeedsExtracts = false; 778 779 TotCost += getScalarizationOverhead(Src, false, NeedsExtracts); 780 TotCost += getScalarizationOverhead(Dst, NeedsInserts, false); 781 782 // FIXME: VF 2 for float<->i32 is currently just as expensive as for VF 4. 783 if (VF == 2 && SrcScalarBits == 32 && DstScalarBits == 32) 784 TotCost *= 2; 785 786 return TotCost; 787 } 788 789 if (Opcode == Instruction::FPTrunc) { 790 if (SrcScalarBits == 128) // fp128 -> double/float + inserts of elements. 791 return VF /*ldxbr/lexbr*/ + getScalarizationOverhead(Dst, true, false); 792 else // double -> float 793 return VF / 2 /*vledb*/ + std::max(1U, VF / 4 /*vperm*/); 794 } 795 796 if (Opcode == Instruction::FPExt) { 797 if (SrcScalarBits == 32 && DstScalarBits == 64) { 798 // float -> double is very rare and currently unoptimized. Instead of 799 // using vldeb, which can do two at a time, all conversions are 800 // scalarized. 801 return VF * 2; 802 } 803 // -> fp128. VF * lxdb/lxeb + extraction of elements. 804 return VF + getScalarizationOverhead(Src, false, true); 805 } 806 } 807 808 return BaseT::getCastInstrCost(Opcode, Dst, Src, I); 809 } 810 811 // Scalar i8 / i16 operations will typically be made after first extending 812 // the operands to i32. 813 static unsigned getOperandsExtensionCost(const Instruction *I) { 814 unsigned ExtCost = 0; 815 for (Value *Op : I->operands()) 816 // A load of i8 or i16 sign/zero extends to i32. 817 if (!isa<LoadInst>(Op) && !isa<ConstantInt>(Op)) 818 ExtCost++; 819 820 return ExtCost; 821 } 822 823 int SystemZTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 824 Type *CondTy, const Instruction *I) { 825 if (!ValTy->isVectorTy()) { 826 switch (Opcode) { 827 case Instruction::ICmp: { 828 // A loaded value compared with 0 with multiple users becomes Load and 829 // Test. The load is then not foldable, so return 0 cost for the ICmp. 830 unsigned ScalarBits = ValTy->getScalarSizeInBits(); 831 if (I != nullptr && ScalarBits >= 32) 832 if (LoadInst *Ld = dyn_cast<LoadInst>(I->getOperand(0))) 833 if (const ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1))) 834 if (!Ld->hasOneUse() && Ld->getParent() == I->getParent() && 835 C->getZExtValue() == 0) 836 return 0; 837 838 unsigned Cost = 1; 839 if (ValTy->isIntegerTy() && ValTy->getScalarSizeInBits() <= 16) 840 Cost += (I != nullptr ? getOperandsExtensionCost(I) : 2); 841 return Cost; 842 } 843 case Instruction::Select: 844 if (ValTy->isFloatingPointTy()) 845 return 4; // No load on condition for FP - costs a conditional jump. 846 return 1; // Load On Condition / Select Register. 847 } 848 } 849 else if (ST->hasVector()) { 850 unsigned VF = cast<VectorType>(ValTy)->getNumElements(); 851 852 // Called with a compare instruction. 853 if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) { 854 unsigned PredicateExtraCost = 0; 855 if (I != nullptr) { 856 // Some predicates cost one or two extra instructions. 857 switch (cast<CmpInst>(I)->getPredicate()) { 858 case CmpInst::Predicate::ICMP_NE: 859 case CmpInst::Predicate::ICMP_UGE: 860 case CmpInst::Predicate::ICMP_ULE: 861 case CmpInst::Predicate::ICMP_SGE: 862 case CmpInst::Predicate::ICMP_SLE: 863 PredicateExtraCost = 1; 864 break; 865 case CmpInst::Predicate::FCMP_ONE: 866 case CmpInst::Predicate::FCMP_ORD: 867 case CmpInst::Predicate::FCMP_UEQ: 868 case CmpInst::Predicate::FCMP_UNO: 869 PredicateExtraCost = 2; 870 break; 871 default: 872 break; 873 } 874 } 875 876 // Float is handled with 2*vmr[lh]f + 2*vldeb + vfchdb for each pair of 877 // floats. FIXME: <2 x float> generates same code as <4 x float>. 878 unsigned CmpCostPerVector = (ValTy->getScalarType()->isFloatTy() ? 10 : 1); 879 unsigned NumVecs_cmp = getNumVectorRegs(ValTy); 880 881 unsigned Cost = (NumVecs_cmp * (CmpCostPerVector + PredicateExtraCost)); 882 return Cost; 883 } 884 else { // Called with a select instruction. 885 assert (Opcode == Instruction::Select); 886 887 // We can figure out the extra cost of packing / unpacking if the 888 // instruction was passed and the compare instruction is found. 889 unsigned PackCost = 0; 890 Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr); 891 if (CmpOpTy != nullptr) 892 PackCost = 893 getVectorBitmaskConversionCost(CmpOpTy, ValTy); 894 895 return getNumVectorRegs(ValTy) /*vsel*/ + PackCost; 896 } 897 } 898 899 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, nullptr); 900 } 901 902 int SystemZTTIImpl:: 903 getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) { 904 // vlvgp will insert two grs into a vector register, so only count half the 905 // number of instructions. 906 if (Opcode == Instruction::InsertElement && Val->isIntOrIntVectorTy(64)) 907 return ((Index % 2 == 0) ? 1 : 0); 908 909 if (Opcode == Instruction::ExtractElement) { 910 int Cost = ((getScalarSizeInBits(Val) == 1) ? 2 /*+test-under-mask*/ : 1); 911 912 // Give a slight penalty for moving out of vector pipeline to FXU unit. 913 if (Index == 0 && Val->isIntOrIntVectorTy()) 914 Cost += 1; 915 916 return Cost; 917 } 918 919 return BaseT::getVectorInstrCost(Opcode, Val, Index); 920 } 921 922 // Check if a load may be folded as a memory operand in its user. 923 bool SystemZTTIImpl:: 924 isFoldableLoad(const LoadInst *Ld, const Instruction *&FoldedValue) { 925 if (!Ld->hasOneUse()) 926 return false; 927 FoldedValue = Ld; 928 const Instruction *UserI = cast<Instruction>(*Ld->user_begin()); 929 unsigned LoadedBits = getScalarSizeInBits(Ld->getType()); 930 unsigned TruncBits = 0; 931 unsigned SExtBits = 0; 932 unsigned ZExtBits = 0; 933 if (UserI->hasOneUse()) { 934 unsigned UserBits = UserI->getType()->getScalarSizeInBits(); 935 if (isa<TruncInst>(UserI)) 936 TruncBits = UserBits; 937 else if (isa<SExtInst>(UserI)) 938 SExtBits = UserBits; 939 else if (isa<ZExtInst>(UserI)) 940 ZExtBits = UserBits; 941 } 942 if (TruncBits || SExtBits || ZExtBits) { 943 FoldedValue = UserI; 944 UserI = cast<Instruction>(*UserI->user_begin()); 945 // Load (single use) -> trunc/extend (single use) -> UserI 946 } 947 if ((UserI->getOpcode() == Instruction::Sub || 948 UserI->getOpcode() == Instruction::SDiv || 949 UserI->getOpcode() == Instruction::UDiv) && 950 UserI->getOperand(1) != FoldedValue) 951 return false; // Not commutative, only RHS foldable. 952 // LoadOrTruncBits holds the number of effectively loaded bits, but 0 if an 953 // extension was made of the load. 954 unsigned LoadOrTruncBits = 955 ((SExtBits || ZExtBits) ? 0 : (TruncBits ? TruncBits : LoadedBits)); 956 switch (UserI->getOpcode()) { 957 case Instruction::Add: // SE: 16->32, 16/32->64, z14:16->64. ZE: 32->64 958 case Instruction::Sub: 959 case Instruction::ICmp: 960 if (LoadedBits == 32 && ZExtBits == 64) 961 return true; 962 LLVM_FALLTHROUGH; 963 case Instruction::Mul: // SE: 16->32, 32->64, z14:16->64 964 if (UserI->getOpcode() != Instruction::ICmp) { 965 if (LoadedBits == 16 && 966 (SExtBits == 32 || 967 (SExtBits == 64 && ST->hasMiscellaneousExtensions2()))) 968 return true; 969 if (LoadOrTruncBits == 16) 970 return true; 971 } 972 LLVM_FALLTHROUGH; 973 case Instruction::SDiv:// SE: 32->64 974 if (LoadedBits == 32 && SExtBits == 64) 975 return true; 976 LLVM_FALLTHROUGH; 977 case Instruction::UDiv: 978 case Instruction::And: 979 case Instruction::Or: 980 case Instruction::Xor: 981 // This also makes sense for float operations, but disabled for now due 982 // to regressions. 983 // case Instruction::FCmp: 984 // case Instruction::FAdd: 985 // case Instruction::FSub: 986 // case Instruction::FMul: 987 // case Instruction::FDiv: 988 989 // All possible extensions of memory checked above. 990 991 // Comparison between memory and immediate. 992 if (UserI->getOpcode() == Instruction::ICmp) 993 if (ConstantInt *CI = dyn_cast<ConstantInt>(UserI->getOperand(1))) 994 if (isUInt<16>(CI->getZExtValue())) 995 return true; 996 return (LoadOrTruncBits == 32 || LoadOrTruncBits == 64); 997 break; 998 } 999 return false; 1000 } 1001 1002 static bool isBswapIntrinsicCall(const Value *V) { 1003 if (const Instruction *I = dyn_cast<Instruction>(V)) 1004 if (auto *CI = dyn_cast<CallInst>(I)) 1005 if (auto *F = CI->getCalledFunction()) 1006 if (F->getIntrinsicID() == Intrinsic::bswap) 1007 return true; 1008 return false; 1009 } 1010 1011 int SystemZTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, 1012 MaybeAlign Alignment, unsigned AddressSpace, 1013 const Instruction *I) { 1014 assert(!Src->isVoidTy() && "Invalid type"); 1015 1016 if (!Src->isVectorTy() && Opcode == Instruction::Load && I != nullptr) { 1017 // Store the load or its truncated or extended value in FoldedValue. 1018 const Instruction *FoldedValue = nullptr; 1019 if (isFoldableLoad(cast<LoadInst>(I), FoldedValue)) { 1020 const Instruction *UserI = cast<Instruction>(*FoldedValue->user_begin()); 1021 assert (UserI->getNumOperands() == 2 && "Expected a binop."); 1022 1023 // UserI can't fold two loads, so in that case return 0 cost only 1024 // half of the time. 1025 for (unsigned i = 0; i < 2; ++i) { 1026 if (UserI->getOperand(i) == FoldedValue) 1027 continue; 1028 1029 if (Instruction *OtherOp = dyn_cast<Instruction>(UserI->getOperand(i))){ 1030 LoadInst *OtherLoad = dyn_cast<LoadInst>(OtherOp); 1031 if (!OtherLoad && 1032 (isa<TruncInst>(OtherOp) || isa<SExtInst>(OtherOp) || 1033 isa<ZExtInst>(OtherOp))) 1034 OtherLoad = dyn_cast<LoadInst>(OtherOp->getOperand(0)); 1035 if (OtherLoad && isFoldableLoad(OtherLoad, FoldedValue/*dummy*/)) 1036 return i == 0; // Both operands foldable. 1037 } 1038 } 1039 1040 return 0; // Only I is foldable in user. 1041 } 1042 } 1043 1044 unsigned NumOps = 1045 (Src->isVectorTy() ? getNumVectorRegs(Src) : getNumberOfParts(Src)); 1046 1047 // Store/Load reversed saves one instruction. 1048 if (((!Src->isVectorTy() && NumOps == 1) || ST->hasVectorEnhancements2()) && 1049 I != nullptr) { 1050 if (Opcode == Instruction::Load && I->hasOneUse()) { 1051 const Instruction *LdUser = cast<Instruction>(*I->user_begin()); 1052 // In case of load -> bswap -> store, return normal cost for the load. 1053 if (isBswapIntrinsicCall(LdUser) && 1054 (!LdUser->hasOneUse() || !isa<StoreInst>(*LdUser->user_begin()))) 1055 return 0; 1056 } 1057 else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) { 1058 const Value *StoredVal = SI->getValueOperand(); 1059 if (StoredVal->hasOneUse() && isBswapIntrinsicCall(StoredVal)) 1060 return 0; 1061 } 1062 } 1063 1064 if (Src->getScalarSizeInBits() == 128) 1065 // 128 bit scalars are held in a pair of two 64 bit registers. 1066 NumOps *= 2; 1067 1068 return NumOps; 1069 } 1070 1071 // The generic implementation of getInterleavedMemoryOpCost() is based on 1072 // adding costs of the memory operations plus all the extracts and inserts 1073 // needed for using / defining the vector operands. The SystemZ version does 1074 // roughly the same but bases the computations on vector permutations 1075 // instead. 1076 int SystemZTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy, 1077 unsigned Factor, 1078 ArrayRef<unsigned> Indices, 1079 unsigned Alignment, 1080 unsigned AddressSpace, 1081 bool UseMaskForCond, 1082 bool UseMaskForGaps) { 1083 if (UseMaskForCond || UseMaskForGaps) 1084 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices, 1085 Alignment, AddressSpace, 1086 UseMaskForCond, UseMaskForGaps); 1087 assert(isa<VectorType>(VecTy) && 1088 "Expect a vector type for interleaved memory op"); 1089 1090 // Return the ceiling of dividing A by B. 1091 auto ceil = [](unsigned A, unsigned B) { return (A + B - 1) / B; }; 1092 1093 unsigned NumElts = cast<VectorType>(VecTy)->getNumElements(); 1094 assert(Factor > 1 && NumElts % Factor == 0 && "Invalid interleave factor"); 1095 unsigned VF = NumElts / Factor; 1096 unsigned NumEltsPerVecReg = (128U / getScalarSizeInBits(VecTy)); 1097 unsigned NumVectorMemOps = getNumVectorRegs(VecTy); 1098 unsigned NumPermutes = 0; 1099 1100 if (Opcode == Instruction::Load) { 1101 // Loading interleave groups may have gaps, which may mean fewer 1102 // loads. Find out how many vectors will be loaded in total, and in how 1103 // many of them each value will be in. 1104 BitVector UsedInsts(NumVectorMemOps, false); 1105 std::vector<BitVector> ValueVecs(Factor, BitVector(NumVectorMemOps, false)); 1106 for (unsigned Index : Indices) 1107 for (unsigned Elt = 0; Elt < VF; ++Elt) { 1108 unsigned Vec = (Index + Elt * Factor) / NumEltsPerVecReg; 1109 UsedInsts.set(Vec); 1110 ValueVecs[Index].set(Vec); 1111 } 1112 NumVectorMemOps = UsedInsts.count(); 1113 1114 for (unsigned Index : Indices) { 1115 // Estimate that each loaded source vector containing this Index 1116 // requires one operation, except that vperm can handle two input 1117 // registers first time for each dst vector. 1118 unsigned NumSrcVecs = ValueVecs[Index].count(); 1119 unsigned NumDstVecs = ceil(VF * getScalarSizeInBits(VecTy), 128U); 1120 assert (NumSrcVecs >= NumDstVecs && "Expected at least as many sources"); 1121 NumPermutes += std::max(1U, NumSrcVecs - NumDstVecs); 1122 } 1123 } else { 1124 // Estimate the permutes for each stored vector as the smaller of the 1125 // number of elements and the number of source vectors. Subtract one per 1126 // dst vector for vperm (S.A.). 1127 unsigned NumSrcVecs = std::min(NumEltsPerVecReg, Factor); 1128 unsigned NumDstVecs = NumVectorMemOps; 1129 assert (NumSrcVecs > 1 && "Expected at least two source vectors."); 1130 NumPermutes += (NumDstVecs * NumSrcVecs) - NumDstVecs; 1131 } 1132 1133 // Cost of load/store operations and the permutations needed. 1134 return NumVectorMemOps + NumPermutes; 1135 } 1136 1137 static int getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy) { 1138 if (RetTy->isVectorTy() && ID == Intrinsic::bswap) 1139 return getNumVectorRegs(RetTy); // VPERM 1140 return -1; 1141 } 1142 1143 int SystemZTTIImpl::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 1144 ArrayRef<Value *> Args, 1145 FastMathFlags FMF, unsigned VF, 1146 const Instruction *I) { 1147 int Cost = getVectorIntrinsicInstrCost(ID, RetTy); 1148 if (Cost != -1) 1149 return Cost; 1150 return BaseT::getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF, I); 1151 } 1152 1153 int SystemZTTIImpl::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 1154 ArrayRef<Type *> Tys, 1155 FastMathFlags FMF, 1156 unsigned ScalarizationCostPassed, 1157 const Instruction *I) { 1158 int Cost = getVectorIntrinsicInstrCost(ID, RetTy); 1159 if (Cost != -1) 1160 return Cost; 1161 return BaseT::getIntrinsicInstrCost(ID, RetTy, Tys, FMF, 1162 ScalarizationCostPassed, I); 1163 } 1164