10b57cec5SDimitry Andric //===-- SystemZTargetTransformInfo.cpp - SystemZ-specific TTI -------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements a TargetTransformInfo analysis pass specific to the 100b57cec5SDimitry Andric // SystemZ target machine. It uses the target's detailed information to provide 110b57cec5SDimitry Andric // more precise answers to certain TTI queries, while letting the target 120b57cec5SDimitry Andric // independent and default TTI implementations handle the rest. 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "SystemZTargetTransformInfo.h" 170b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/BasicTTIImpl.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/CostTable.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 21*0fca6ea1SDimitry Andric #include "llvm/IR/DerivedTypes.h" 220b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 23*0fca6ea1SDimitry Andric #include "llvm/IR/Intrinsics.h" 240b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 25*0fca6ea1SDimitry Andric #include "llvm/Support/MathExtras.h" 26*0fca6ea1SDimitry Andric 270b57cec5SDimitry Andric using namespace llvm; 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric #define DEBUG_TYPE "systemztti" 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 320b57cec5SDimitry Andric // 330b57cec5SDimitry Andric // SystemZ cost model. 340b57cec5SDimitry Andric // 350b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 360b57cec5SDimitry Andric 3781ad6265SDimitry Andric static bool isUsedAsMemCpySource(const Value *V, bool &OtherUse) { 3881ad6265SDimitry Andric bool UsedAsMemCpySource = false; 3981ad6265SDimitry Andric for (const User *U : V->users()) 4081ad6265SDimitry Andric if (const Instruction *User = dyn_cast<Instruction>(U)) { 4181ad6265SDimitry Andric if (isa<BitCastInst>(User) || isa<GetElementPtrInst>(User)) { 4281ad6265SDimitry Andric UsedAsMemCpySource |= isUsedAsMemCpySource(User, OtherUse); 4381ad6265SDimitry Andric continue; 4481ad6265SDimitry Andric } 4581ad6265SDimitry Andric if (const MemCpyInst *Memcpy = dyn_cast<MemCpyInst>(User)) { 4681ad6265SDimitry Andric if (Memcpy->getOperand(1) == V && !Memcpy->isVolatile()) { 4781ad6265SDimitry Andric UsedAsMemCpySource = true; 4881ad6265SDimitry Andric continue; 4981ad6265SDimitry Andric } 5081ad6265SDimitry Andric } 5181ad6265SDimitry Andric OtherUse = true; 5281ad6265SDimitry Andric } 5381ad6265SDimitry Andric return UsedAsMemCpySource; 5481ad6265SDimitry Andric } 5581ad6265SDimitry Andric 5681ad6265SDimitry Andric unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const { 5781ad6265SDimitry Andric unsigned Bonus = 0; 5881ad6265SDimitry Andric 5981ad6265SDimitry Andric // Increase the threshold if an incoming argument is used only as a memcpy 6081ad6265SDimitry Andric // source. 6181ad6265SDimitry Andric if (Function *Callee = CB->getCalledFunction()) 6281ad6265SDimitry Andric for (Argument &Arg : Callee->args()) { 6381ad6265SDimitry Andric bool OtherUse = false; 6481ad6265SDimitry Andric if (isUsedAsMemCpySource(&Arg, OtherUse) && !OtherUse) 6581ad6265SDimitry Andric Bonus += 150; 6681ad6265SDimitry Andric } 6781ad6265SDimitry Andric 6881ad6265SDimitry Andric LLVM_DEBUG(if (Bonus) 6981ad6265SDimitry Andric dbgs() << "++ SZTTI Adding inlining bonus: " << Bonus << "\n";); 7081ad6265SDimitry Andric return Bonus; 7181ad6265SDimitry Andric } 7281ad6265SDimitry Andric 73fe6060f1SDimitry Andric InstructionCost SystemZTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty, 745ffd83dbSDimitry Andric TTI::TargetCostKind CostKind) { 750b57cec5SDimitry Andric assert(Ty->isIntegerTy()); 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric unsigned BitSize = Ty->getPrimitiveSizeInBits(); 780b57cec5SDimitry Andric // There is no cost model for constants with a bit size of 0. Return TCC_Free 790b57cec5SDimitry Andric // here, so that constant hoisting will ignore this constant. 800b57cec5SDimitry Andric if (BitSize == 0) 810b57cec5SDimitry Andric return TTI::TCC_Free; 827a6dacacSDimitry Andric // No cost model for operations on integers larger than 128 bit implemented yet. 837a6dacacSDimitry Andric if ((!ST->hasVector() && BitSize > 64) || BitSize > 128) 840b57cec5SDimitry Andric return TTI::TCC_Free; 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric if (Imm == 0) 870b57cec5SDimitry Andric return TTI::TCC_Free; 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric if (Imm.getBitWidth() <= 64) { 900b57cec5SDimitry Andric // Constants loaded via lgfi. 910b57cec5SDimitry Andric if (isInt<32>(Imm.getSExtValue())) 920b57cec5SDimitry Andric return TTI::TCC_Basic; 930b57cec5SDimitry Andric // Constants loaded via llilf. 940b57cec5SDimitry Andric if (isUInt<32>(Imm.getZExtValue())) 950b57cec5SDimitry Andric return TTI::TCC_Basic; 960b57cec5SDimitry Andric // Constants loaded via llihf: 970b57cec5SDimitry Andric if ((Imm.getZExtValue() & 0xffffffff) == 0) 980b57cec5SDimitry Andric return TTI::TCC_Basic; 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric return 2 * TTI::TCC_Basic; 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric 1037a6dacacSDimitry Andric // i128 immediates loads from Constant Pool 1047a6dacacSDimitry Andric return 2 * TTI::TCC_Basic; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 107fe6060f1SDimitry Andric InstructionCost SystemZTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx, 1085ffd83dbSDimitry Andric const APInt &Imm, Type *Ty, 109e8d8bef9SDimitry Andric TTI::TargetCostKind CostKind, 110e8d8bef9SDimitry Andric Instruction *Inst) { 1110b57cec5SDimitry Andric assert(Ty->isIntegerTy()); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric unsigned BitSize = Ty->getPrimitiveSizeInBits(); 1140b57cec5SDimitry Andric // There is no cost model for constants with a bit size of 0. Return TCC_Free 1150b57cec5SDimitry Andric // here, so that constant hoisting will ignore this constant. 1160b57cec5SDimitry Andric if (BitSize == 0) 1170b57cec5SDimitry Andric return TTI::TCC_Free; 1180b57cec5SDimitry Andric // No cost model for operations on integers larger than 64 bit implemented yet. 1190b57cec5SDimitry Andric if (BitSize > 64) 1200b57cec5SDimitry Andric return TTI::TCC_Free; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric switch (Opcode) { 1230b57cec5SDimitry Andric default: 1240b57cec5SDimitry Andric return TTI::TCC_Free; 1250b57cec5SDimitry Andric case Instruction::GetElementPtr: 1260b57cec5SDimitry Andric // Always hoist the base address of a GetElementPtr. This prevents the 1270b57cec5SDimitry Andric // creation of new constants for every base constant that gets constant 1280b57cec5SDimitry Andric // folded with the offset. 1290b57cec5SDimitry Andric if (Idx == 0) 1300b57cec5SDimitry Andric return 2 * TTI::TCC_Basic; 1310b57cec5SDimitry Andric return TTI::TCC_Free; 1320b57cec5SDimitry Andric case Instruction::Store: 1330b57cec5SDimitry Andric if (Idx == 0 && Imm.getBitWidth() <= 64) { 1340b57cec5SDimitry Andric // Any 8-bit immediate store can by implemented via mvi. 1350b57cec5SDimitry Andric if (BitSize == 8) 1360b57cec5SDimitry Andric return TTI::TCC_Free; 1370b57cec5SDimitry Andric // 16-bit immediate values can be stored via mvhhi/mvhi/mvghi. 1380b57cec5SDimitry Andric if (isInt<16>(Imm.getSExtValue())) 1390b57cec5SDimitry Andric return TTI::TCC_Free; 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric break; 1420b57cec5SDimitry Andric case Instruction::ICmp: 1430b57cec5SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) { 1440b57cec5SDimitry Andric // Comparisons against signed 32-bit immediates implemented via cgfi. 1450b57cec5SDimitry Andric if (isInt<32>(Imm.getSExtValue())) 1460b57cec5SDimitry Andric return TTI::TCC_Free; 1470b57cec5SDimitry Andric // Comparisons against unsigned 32-bit immediates implemented via clgfi. 1480b57cec5SDimitry Andric if (isUInt<32>(Imm.getZExtValue())) 1490b57cec5SDimitry Andric return TTI::TCC_Free; 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric break; 1520b57cec5SDimitry Andric case Instruction::Add: 1530b57cec5SDimitry Andric case Instruction::Sub: 1540b57cec5SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) { 1550b57cec5SDimitry Andric // We use algfi/slgfi to add/subtract 32-bit unsigned immediates. 1560b57cec5SDimitry Andric if (isUInt<32>(Imm.getZExtValue())) 1570b57cec5SDimitry Andric return TTI::TCC_Free; 1580b57cec5SDimitry Andric // Or their negation, by swapping addition vs. subtraction. 1590b57cec5SDimitry Andric if (isUInt<32>(-Imm.getSExtValue())) 1600b57cec5SDimitry Andric return TTI::TCC_Free; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric break; 1630b57cec5SDimitry Andric case Instruction::Mul: 1640b57cec5SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) { 1650b57cec5SDimitry Andric // We use msgfi to multiply by 32-bit signed immediates. 1660b57cec5SDimitry Andric if (isInt<32>(Imm.getSExtValue())) 1670b57cec5SDimitry Andric return TTI::TCC_Free; 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric break; 1700b57cec5SDimitry Andric case Instruction::Or: 1710b57cec5SDimitry Andric case Instruction::Xor: 1720b57cec5SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) { 1730b57cec5SDimitry Andric // Masks supported by oilf/xilf. 1740b57cec5SDimitry Andric if (isUInt<32>(Imm.getZExtValue())) 1750b57cec5SDimitry Andric return TTI::TCC_Free; 1760b57cec5SDimitry Andric // Masks supported by oihf/xihf. 1770b57cec5SDimitry Andric if ((Imm.getZExtValue() & 0xffffffff) == 0) 1780b57cec5SDimitry Andric return TTI::TCC_Free; 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric break; 1810b57cec5SDimitry Andric case Instruction::And: 1820b57cec5SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) { 1830b57cec5SDimitry Andric // Any 32-bit AND operation can by implemented via nilf. 1840b57cec5SDimitry Andric if (BitSize <= 32) 1850b57cec5SDimitry Andric return TTI::TCC_Free; 1860b57cec5SDimitry Andric // 64-bit masks supported by nilf. 1870b57cec5SDimitry Andric if (isUInt<32>(~Imm.getZExtValue())) 1880b57cec5SDimitry Andric return TTI::TCC_Free; 1890b57cec5SDimitry Andric // 64-bit masks supported by nilh. 1900b57cec5SDimitry Andric if ((Imm.getZExtValue() & 0xffffffff) == 0xffffffff) 1910b57cec5SDimitry Andric return TTI::TCC_Free; 1920b57cec5SDimitry Andric // Some 64-bit AND operations can be implemented via risbg. 1930b57cec5SDimitry Andric const SystemZInstrInfo *TII = ST->getInstrInfo(); 1940b57cec5SDimitry Andric unsigned Start, End; 1950b57cec5SDimitry Andric if (TII->isRxSBGMask(Imm.getZExtValue(), BitSize, Start, End)) 1960b57cec5SDimitry Andric return TTI::TCC_Free; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric break; 1990b57cec5SDimitry Andric case Instruction::Shl: 2000b57cec5SDimitry Andric case Instruction::LShr: 2010b57cec5SDimitry Andric case Instruction::AShr: 2020b57cec5SDimitry Andric // Always return TCC_Free for the shift value of a shift instruction. 2030b57cec5SDimitry Andric if (Idx == 1) 2040b57cec5SDimitry Andric return TTI::TCC_Free; 2050b57cec5SDimitry Andric break; 2060b57cec5SDimitry Andric case Instruction::UDiv: 2070b57cec5SDimitry Andric case Instruction::SDiv: 2080b57cec5SDimitry Andric case Instruction::URem: 2090b57cec5SDimitry Andric case Instruction::SRem: 2100b57cec5SDimitry Andric case Instruction::Trunc: 2110b57cec5SDimitry Andric case Instruction::ZExt: 2120b57cec5SDimitry Andric case Instruction::SExt: 2130b57cec5SDimitry Andric case Instruction::IntToPtr: 2140b57cec5SDimitry Andric case Instruction::PtrToInt: 2150b57cec5SDimitry Andric case Instruction::BitCast: 2160b57cec5SDimitry Andric case Instruction::PHI: 2170b57cec5SDimitry Andric case Instruction::Call: 2180b57cec5SDimitry Andric case Instruction::Select: 2190b57cec5SDimitry Andric case Instruction::Ret: 2200b57cec5SDimitry Andric case Instruction::Load: 2210b57cec5SDimitry Andric break; 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric 2245ffd83dbSDimitry Andric return SystemZTTIImpl::getIntImmCost(Imm, Ty, CostKind); 2250b57cec5SDimitry Andric } 2260b57cec5SDimitry Andric 227fe6060f1SDimitry Andric InstructionCost 228fe6060f1SDimitry Andric SystemZTTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, 2295ffd83dbSDimitry Andric const APInt &Imm, Type *Ty, 2305ffd83dbSDimitry Andric TTI::TargetCostKind CostKind) { 2310b57cec5SDimitry Andric assert(Ty->isIntegerTy()); 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric unsigned BitSize = Ty->getPrimitiveSizeInBits(); 2340b57cec5SDimitry Andric // There is no cost model for constants with a bit size of 0. Return TCC_Free 2350b57cec5SDimitry Andric // here, so that constant hoisting will ignore this constant. 2360b57cec5SDimitry Andric if (BitSize == 0) 2370b57cec5SDimitry Andric return TTI::TCC_Free; 2380b57cec5SDimitry Andric // No cost model for operations on integers larger than 64 bit implemented yet. 2390b57cec5SDimitry Andric if (BitSize > 64) 2400b57cec5SDimitry Andric return TTI::TCC_Free; 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric switch (IID) { 2430b57cec5SDimitry Andric default: 2440b57cec5SDimitry Andric return TTI::TCC_Free; 2450b57cec5SDimitry Andric case Intrinsic::sadd_with_overflow: 2460b57cec5SDimitry Andric case Intrinsic::uadd_with_overflow: 2470b57cec5SDimitry Andric case Intrinsic::ssub_with_overflow: 2480b57cec5SDimitry Andric case Intrinsic::usub_with_overflow: 2490b57cec5SDimitry Andric // These get expanded to include a normal addition/subtraction. 2500b57cec5SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) { 2510b57cec5SDimitry Andric if (isUInt<32>(Imm.getZExtValue())) 2520b57cec5SDimitry Andric return TTI::TCC_Free; 2530b57cec5SDimitry Andric if (isUInt<32>(-Imm.getSExtValue())) 2540b57cec5SDimitry Andric return TTI::TCC_Free; 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric break; 2570b57cec5SDimitry Andric case Intrinsic::smul_with_overflow: 2580b57cec5SDimitry Andric case Intrinsic::umul_with_overflow: 2590b57cec5SDimitry Andric // These get expanded to include a normal multiplication. 2600b57cec5SDimitry Andric if (Idx == 1 && Imm.getBitWidth() <= 64) { 2610b57cec5SDimitry Andric if (isInt<32>(Imm.getSExtValue())) 2620b57cec5SDimitry Andric return TTI::TCC_Free; 2630b57cec5SDimitry Andric } 2640b57cec5SDimitry Andric break; 2650b57cec5SDimitry Andric case Intrinsic::experimental_stackmap: 2660b57cec5SDimitry Andric if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 2670b57cec5SDimitry Andric return TTI::TCC_Free; 2680b57cec5SDimitry Andric break; 2690b57cec5SDimitry Andric case Intrinsic::experimental_patchpoint_void: 270*0fca6ea1SDimitry Andric case Intrinsic::experimental_patchpoint: 2710b57cec5SDimitry Andric if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 2720b57cec5SDimitry Andric return TTI::TCC_Free; 2730b57cec5SDimitry Andric break; 2740b57cec5SDimitry Andric } 2755ffd83dbSDimitry Andric return SystemZTTIImpl::getIntImmCost(Imm, Ty, CostKind); 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric TargetTransformInfo::PopcntSupportKind 2790b57cec5SDimitry Andric SystemZTTIImpl::getPopcntSupport(unsigned TyWidth) { 2800b57cec5SDimitry Andric assert(isPowerOf2_32(TyWidth) && "Type width must be power of 2"); 2810b57cec5SDimitry Andric if (ST->hasPopulationCount() && TyWidth <= 64) 2820b57cec5SDimitry Andric return TTI::PSK_FastHardware; 2830b57cec5SDimitry Andric return TTI::PSK_Software; 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric void SystemZTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE, 287349cc55cSDimitry Andric TTI::UnrollingPreferences &UP, 288349cc55cSDimitry Andric OptimizationRemarkEmitter *ORE) { 2890b57cec5SDimitry Andric // Find out if L contains a call, what the machine instruction count 2900b57cec5SDimitry Andric // estimate is, and how many stores there are. 2910b57cec5SDimitry Andric bool HasCall = false; 292fe6060f1SDimitry Andric InstructionCost NumStores = 0; 2930b57cec5SDimitry Andric for (auto &BB : L->blocks()) 2940b57cec5SDimitry Andric for (auto &I : *BB) { 2950b57cec5SDimitry Andric if (isa<CallInst>(&I) || isa<InvokeInst>(&I)) { 2965ffd83dbSDimitry Andric if (const Function *F = cast<CallBase>(I).getCalledFunction()) { 2970b57cec5SDimitry Andric if (isLoweredToCall(F)) 2980b57cec5SDimitry Andric HasCall = true; 2990b57cec5SDimitry Andric if (F->getIntrinsicID() == Intrinsic::memcpy || 3000b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::memset) 3010b57cec5SDimitry Andric NumStores++; 3020b57cec5SDimitry Andric } else { // indirect call. 3030b57cec5SDimitry Andric HasCall = true; 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric if (isa<StoreInst>(&I)) { 3070b57cec5SDimitry Andric Type *MemAccessTy = I.getOperand(0)->getType(); 308bdd1243dSDimitry Andric NumStores += getMemoryOpCost(Instruction::Store, MemAccessTy, 309bdd1243dSDimitry Andric std::nullopt, 0, TTI::TCK_RecipThroughput); 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric // The z13 processor will run out of store tags if too many stores 3140b57cec5SDimitry Andric // are fed into it too quickly. Therefore make sure there are not 3150b57cec5SDimitry Andric // too many stores in the resulting unrolled loop. 316fe6060f1SDimitry Andric unsigned const NumStoresVal = *NumStores.getValue(); 317fe6060f1SDimitry Andric unsigned const Max = (NumStoresVal ? (12 / NumStoresVal) : UINT_MAX); 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric if (HasCall) { 3200b57cec5SDimitry Andric // Only allow full unrolling if loop has any calls. 3210b57cec5SDimitry Andric UP.FullUnrollMaxCount = Max; 3220b57cec5SDimitry Andric UP.MaxCount = 1; 3230b57cec5SDimitry Andric return; 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric UP.MaxCount = Max; 3270b57cec5SDimitry Andric if (UP.MaxCount <= 1) 3280b57cec5SDimitry Andric return; 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric // Allow partial and runtime trip count unrolling. 3310b57cec5SDimitry Andric UP.Partial = UP.Runtime = true; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric UP.PartialThreshold = 75; 3340b57cec5SDimitry Andric UP.DefaultUnrollRuntimeCount = 4; 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric // Allow expensive instructions in the pre-header of the loop. 3370b57cec5SDimitry Andric UP.AllowExpensiveTripCount = true; 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric UP.Force = true; 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 3425ffd83dbSDimitry Andric void SystemZTTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE, 3435ffd83dbSDimitry Andric TTI::PeelingPreferences &PP) { 3445ffd83dbSDimitry Andric BaseT::getPeelingPreferences(L, SE, PP); 3455ffd83dbSDimitry Andric } 3460b57cec5SDimitry Andric 34781ad6265SDimitry Andric bool SystemZTTIImpl::isLSRCostLess(const TargetTransformInfo::LSRCost &C1, 34881ad6265SDimitry Andric const TargetTransformInfo::LSRCost &C2) { 3490b57cec5SDimitry Andric // SystemZ specific: check instruction count (first), and don't care about 3500b57cec5SDimitry Andric // ImmCost, since offsets are checked explicitly. 3510b57cec5SDimitry Andric return std::tie(C1.Insns, C1.NumRegs, C1.AddRecCost, 3520b57cec5SDimitry Andric C1.NumIVMuls, C1.NumBaseAdds, 3530b57cec5SDimitry Andric C1.ScaleCost, C1.SetupCost) < 3540b57cec5SDimitry Andric std::tie(C2.Insns, C2.NumRegs, C2.AddRecCost, 3550b57cec5SDimitry Andric C2.NumIVMuls, C2.NumBaseAdds, 3560b57cec5SDimitry Andric C2.ScaleCost, C2.SetupCost); 3570b57cec5SDimitry Andric } 3580b57cec5SDimitry Andric 3598bcb0991SDimitry Andric unsigned SystemZTTIImpl::getNumberOfRegisters(unsigned ClassID) const { 3608bcb0991SDimitry Andric bool Vector = (ClassID == 1); 3610b57cec5SDimitry Andric if (!Vector) 3620b57cec5SDimitry Andric // Discount the stack pointer. Also leave out %r0, since it can't 3630b57cec5SDimitry Andric // be used in an address. 3640b57cec5SDimitry Andric return 14; 3650b57cec5SDimitry Andric if (ST->hasVector()) 3660b57cec5SDimitry Andric return 32; 3670b57cec5SDimitry Andric return 0; 3680b57cec5SDimitry Andric } 3690b57cec5SDimitry Andric 370fe6060f1SDimitry Andric TypeSize 371fe6060f1SDimitry Andric SystemZTTIImpl::getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const { 372fe6060f1SDimitry Andric switch (K) { 373fe6060f1SDimitry Andric case TargetTransformInfo::RGK_Scalar: 374fe6060f1SDimitry Andric return TypeSize::getFixed(64); 375fe6060f1SDimitry Andric case TargetTransformInfo::RGK_FixedWidthVector: 376fe6060f1SDimitry Andric return TypeSize::getFixed(ST->hasVector() ? 128 : 0); 377fe6060f1SDimitry Andric case TargetTransformInfo::RGK_ScalableVector: 378fe6060f1SDimitry Andric return TypeSize::getScalable(0); 379fe6060f1SDimitry Andric } 380fe6060f1SDimitry Andric 381fe6060f1SDimitry Andric llvm_unreachable("Unsupported register kind"); 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3845ffd83dbSDimitry Andric unsigned SystemZTTIImpl::getMinPrefetchStride(unsigned NumMemAccesses, 3855ffd83dbSDimitry Andric unsigned NumStridedMemAccesses, 3865ffd83dbSDimitry Andric unsigned NumPrefetches, 3875ffd83dbSDimitry Andric bool HasCall) const { 3885ffd83dbSDimitry Andric // Don't prefetch a loop with many far apart accesses. 3895ffd83dbSDimitry Andric if (NumPrefetches > 16) 3905ffd83dbSDimitry Andric return UINT_MAX; 3915ffd83dbSDimitry Andric 3925ffd83dbSDimitry Andric // Emit prefetch instructions for smaller strides in cases where we think 3935ffd83dbSDimitry Andric // the hardware prefetcher might not be able to keep up. 394e8d8bef9SDimitry Andric if (NumStridedMemAccesses > 32 && !HasCall && 395e8d8bef9SDimitry Andric (NumMemAccesses - NumStridedMemAccesses) * 32 <= NumStridedMemAccesses) 3965ffd83dbSDimitry Andric return 1; 3975ffd83dbSDimitry Andric 3985ffd83dbSDimitry Andric return ST->hasMiscellaneousExtensions3() ? 8192 : 2048; 3995ffd83dbSDimitry Andric } 4005ffd83dbSDimitry Andric 4010b57cec5SDimitry Andric bool SystemZTTIImpl::hasDivRemOp(Type *DataType, bool IsSigned) { 4020b57cec5SDimitry Andric EVT VT = TLI->getValueType(DL, DataType); 4030b57cec5SDimitry Andric return (VT.isScalarInteger() && TLI->isTypeLegal(VT)); 4040b57cec5SDimitry Andric } 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric // Return the bit size for the scalar type or vector element 4070b57cec5SDimitry Andric // type. getScalarSizeInBits() returns 0 for a pointer type. 4080b57cec5SDimitry Andric static unsigned getScalarSizeInBits(Type *Ty) { 4090b57cec5SDimitry Andric unsigned Size = 4100b57cec5SDimitry Andric (Ty->isPtrOrPtrVectorTy() ? 64U : Ty->getScalarSizeInBits()); 4110b57cec5SDimitry Andric assert(Size > 0 && "Element must have non-zero size."); 4120b57cec5SDimitry Andric return Size; 4130b57cec5SDimitry Andric } 4140b57cec5SDimitry Andric 4150b57cec5SDimitry Andric // getNumberOfParts() calls getTypeLegalizationCost() which splits the vector 4160b57cec5SDimitry Andric // type until it is legal. This would e.g. return 4 for <6 x i64>, instead of 4170b57cec5SDimitry Andric // 3. 4180b57cec5SDimitry Andric static unsigned getNumVectorRegs(Type *Ty) { 4195ffd83dbSDimitry Andric auto *VTy = cast<FixedVectorType>(Ty); 4205ffd83dbSDimitry Andric unsigned WideBits = getScalarSizeInBits(Ty) * VTy->getNumElements(); 4210b57cec5SDimitry Andric assert(WideBits > 0 && "Could not compute size of vector"); 4220b57cec5SDimitry Andric return ((WideBits % 128U) ? ((WideBits / 128U) + 1) : (WideBits / 128U)); 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric 425fe6060f1SDimitry Andric InstructionCost SystemZTTIImpl::getArithmeticInstrCost( 4265ffd83dbSDimitry Andric unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind, 427bdd1243dSDimitry Andric TTI::OperandValueInfo Op1Info, TTI::OperandValueInfo Op2Info, 428bdd1243dSDimitry Andric ArrayRef<const Value *> Args, 429480093f4SDimitry Andric const Instruction *CxtI) { 4300b57cec5SDimitry Andric 4315ffd83dbSDimitry Andric // TODO: Handle more cost kinds. 4325ffd83dbSDimitry Andric if (CostKind != TTI::TCK_RecipThroughput) 4335ffd83dbSDimitry Andric return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info, 434bdd1243dSDimitry Andric Op2Info, Args, CxtI); 4355ffd83dbSDimitry Andric 4360b57cec5SDimitry Andric // TODO: return a good value for BB-VECTORIZER that includes the 4370b57cec5SDimitry Andric // immediate loads, which we do not want to count for the loop 4380b57cec5SDimitry Andric // vectorizer, since they are hopefully hoisted out of the loop. This 4390b57cec5SDimitry Andric // would require a new parameter 'InLoop', but not sure if constant 4400b57cec5SDimitry Andric // args are common enough to motivate this. 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric unsigned ScalarBits = Ty->getScalarSizeInBits(); 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric // There are thre cases of division and remainder: Dividing with a register 4450b57cec5SDimitry Andric // needs a divide instruction. A divisor which is a power of two constant 4460b57cec5SDimitry Andric // can be implemented with a sequence of shifts. Any other constant needs a 4470b57cec5SDimitry Andric // multiply and shifts. 4480b57cec5SDimitry Andric const unsigned DivInstrCost = 20; 4490b57cec5SDimitry Andric const unsigned DivMulSeqCost = 10; 4500b57cec5SDimitry Andric const unsigned SDivPow2Cost = 4; 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric bool SignedDivRem = 4530b57cec5SDimitry Andric Opcode == Instruction::SDiv || Opcode == Instruction::SRem; 4540b57cec5SDimitry Andric bool UnsignedDivRem = 4550b57cec5SDimitry Andric Opcode == Instruction::UDiv || Opcode == Instruction::URem; 4560b57cec5SDimitry Andric 4570b57cec5SDimitry Andric // Check for a constant divisor. 4580b57cec5SDimitry Andric bool DivRemConst = false; 4590b57cec5SDimitry Andric bool DivRemConstPow2 = false; 4600b57cec5SDimitry Andric if ((SignedDivRem || UnsignedDivRem) && Args.size() == 2) { 4610b57cec5SDimitry Andric if (const Constant *C = dyn_cast<Constant>(Args[1])) { 4620b57cec5SDimitry Andric const ConstantInt *CVal = 4630b57cec5SDimitry Andric (C->getType()->isVectorTy() 4640b57cec5SDimitry Andric ? dyn_cast_or_null<const ConstantInt>(C->getSplatValue()) 4650b57cec5SDimitry Andric : dyn_cast<const ConstantInt>(C)); 466349cc55cSDimitry Andric if (CVal && (CVal->getValue().isPowerOf2() || 467349cc55cSDimitry Andric CVal->getValue().isNegatedPowerOf2())) 4680b57cec5SDimitry Andric DivRemConstPow2 = true; 4690b57cec5SDimitry Andric else 4700b57cec5SDimitry Andric DivRemConst = true; 4710b57cec5SDimitry Andric } 4720b57cec5SDimitry Andric } 4730b57cec5SDimitry Andric 4745ffd83dbSDimitry Andric if (!Ty->isVectorTy()) { 4750b57cec5SDimitry Andric // These FP operations are supported with a dedicated instruction for 4760b57cec5SDimitry Andric // float, double and fp128 (base implementation assumes float generally 4770b57cec5SDimitry Andric // costs 2). 4780b57cec5SDimitry Andric if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub || 4790b57cec5SDimitry Andric Opcode == Instruction::FMul || Opcode == Instruction::FDiv) 4800b57cec5SDimitry Andric return 1; 4810b57cec5SDimitry Andric 4820b57cec5SDimitry Andric // There is no native support for FRem. 4830b57cec5SDimitry Andric if (Opcode == Instruction::FRem) 4840b57cec5SDimitry Andric return LIBCALL_COST; 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric // Give discount for some combined logical operations if supported. 4877a6dacacSDimitry Andric if (Args.size() == 2) { 4880b57cec5SDimitry Andric if (Opcode == Instruction::Xor) { 4890b57cec5SDimitry Andric for (const Value *A : Args) { 4900b57cec5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(A)) 4910b57cec5SDimitry Andric if (I->hasOneUse() && 4927a6dacacSDimitry Andric (I->getOpcode() == Instruction::Or || 4937a6dacacSDimitry Andric I->getOpcode() == Instruction::And || 4940b57cec5SDimitry Andric I->getOpcode() == Instruction::Xor)) 4957a6dacacSDimitry Andric if ((ScalarBits <= 64 && ST->hasMiscellaneousExtensions3()) || 4967a6dacacSDimitry Andric (isInt128InVR(Ty) && 4977a6dacacSDimitry Andric (I->getOpcode() == Instruction::Or || ST->hasVectorEnhancements1()))) 4980b57cec5SDimitry Andric return 0; 4990b57cec5SDimitry Andric } 5000b57cec5SDimitry Andric } 5017a6dacacSDimitry Andric else if (Opcode == Instruction::And || Opcode == Instruction::Or) { 5020b57cec5SDimitry Andric for (const Value *A : Args) { 5030b57cec5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(A)) 5047a6dacacSDimitry Andric if ((I->hasOneUse() && I->getOpcode() == Instruction::Xor) && 5057a6dacacSDimitry Andric ((ScalarBits <= 64 && ST->hasMiscellaneousExtensions3()) || 5067a6dacacSDimitry Andric (isInt128InVR(Ty) && 5077a6dacacSDimitry Andric (Opcode == Instruction::And || ST->hasVectorEnhancements1())))) 5080b57cec5SDimitry Andric return 0; 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric } 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric // Or requires one instruction, although it has custom handling for i64. 5140b57cec5SDimitry Andric if (Opcode == Instruction::Or) 5150b57cec5SDimitry Andric return 1; 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric if (Opcode == Instruction::Xor && ScalarBits == 1) { 5180b57cec5SDimitry Andric if (ST->hasLoadStoreOnCond2()) 5190b57cec5SDimitry Andric return 5; // 2 * (li 0; loc 1); xor 5200b57cec5SDimitry Andric return 7; // 2 * ipm sequences ; xor ; shift ; compare 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric if (DivRemConstPow2) 5240b57cec5SDimitry Andric return (SignedDivRem ? SDivPow2Cost : 1); 5250b57cec5SDimitry Andric if (DivRemConst) 5260b57cec5SDimitry Andric return DivMulSeqCost; 5270b57cec5SDimitry Andric if (SignedDivRem || UnsignedDivRem) 5280b57cec5SDimitry Andric return DivInstrCost; 5290b57cec5SDimitry Andric } 5305ffd83dbSDimitry Andric else if (ST->hasVector()) { 5315ffd83dbSDimitry Andric auto *VTy = cast<FixedVectorType>(Ty); 5325ffd83dbSDimitry Andric unsigned VF = VTy->getNumElements(); 5335ffd83dbSDimitry Andric unsigned NumVectors = getNumVectorRegs(Ty); 5345ffd83dbSDimitry Andric 5355ffd83dbSDimitry Andric // These vector operations are custom handled, but are still supported 5365ffd83dbSDimitry Andric // with one instruction per vector, regardless of element size. 5375ffd83dbSDimitry Andric if (Opcode == Instruction::Shl || Opcode == Instruction::LShr || 5385ffd83dbSDimitry Andric Opcode == Instruction::AShr) { 5395ffd83dbSDimitry Andric return NumVectors; 5405ffd83dbSDimitry Andric } 5415ffd83dbSDimitry Andric 5425ffd83dbSDimitry Andric if (DivRemConstPow2) 5435ffd83dbSDimitry Andric return (NumVectors * (SignedDivRem ? SDivPow2Cost : 1)); 544fe6060f1SDimitry Andric if (DivRemConst) { 545fe6060f1SDimitry Andric SmallVector<Type *> Tys(Args.size(), Ty); 546bdd1243dSDimitry Andric return VF * DivMulSeqCost + 547bdd1243dSDimitry Andric getScalarizationOverhead(VTy, Args, Tys, CostKind); 548fe6060f1SDimitry Andric } 5495ffd83dbSDimitry Andric if ((SignedDivRem || UnsignedDivRem) && VF > 4) 5505ffd83dbSDimitry Andric // Temporary hack: disable high vectorization factors with integer 5515ffd83dbSDimitry Andric // division/remainder, which will get scalarized and handled with 5525ffd83dbSDimitry Andric // GR128 registers. The mischeduler is not clever enough to avoid 5535ffd83dbSDimitry Andric // spilling yet. 5545ffd83dbSDimitry Andric return 1000; 5555ffd83dbSDimitry Andric 5565ffd83dbSDimitry Andric // These FP operations are supported with a single vector instruction for 5575ffd83dbSDimitry Andric // double (base implementation assumes float generally costs 2). For 5585ffd83dbSDimitry Andric // FP128, the scalar cost is 1, and there is no overhead since the values 5595ffd83dbSDimitry Andric // are already in scalar registers. 5605ffd83dbSDimitry Andric if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub || 5615ffd83dbSDimitry Andric Opcode == Instruction::FMul || Opcode == Instruction::FDiv) { 5625ffd83dbSDimitry Andric switch (ScalarBits) { 5635ffd83dbSDimitry Andric case 32: { 5645ffd83dbSDimitry Andric // The vector enhancements facility 1 provides v4f32 instructions. 5655ffd83dbSDimitry Andric if (ST->hasVectorEnhancements1()) 5665ffd83dbSDimitry Andric return NumVectors; 5675ffd83dbSDimitry Andric // Return the cost of multiple scalar invocation plus the cost of 5685ffd83dbSDimitry Andric // inserting and extracting the values. 569fe6060f1SDimitry Andric InstructionCost ScalarCost = 5705ffd83dbSDimitry Andric getArithmeticInstrCost(Opcode, Ty->getScalarType(), CostKind); 571fe6060f1SDimitry Andric SmallVector<Type *> Tys(Args.size(), Ty); 572fe6060f1SDimitry Andric InstructionCost Cost = 573bdd1243dSDimitry Andric (VF * ScalarCost) + 574bdd1243dSDimitry Andric getScalarizationOverhead(VTy, Args, Tys, CostKind); 5755ffd83dbSDimitry Andric // FIXME: VF 2 for these FP operations are currently just as 5765ffd83dbSDimitry Andric // expensive as for VF 4. 5775ffd83dbSDimitry Andric if (VF == 2) 5785ffd83dbSDimitry Andric Cost *= 2; 5795ffd83dbSDimitry Andric return Cost; 5805ffd83dbSDimitry Andric } 5815ffd83dbSDimitry Andric case 64: 5825ffd83dbSDimitry Andric case 128: 5835ffd83dbSDimitry Andric return NumVectors; 5845ffd83dbSDimitry Andric default: 5855ffd83dbSDimitry Andric break; 5865ffd83dbSDimitry Andric } 5875ffd83dbSDimitry Andric } 5885ffd83dbSDimitry Andric 5895ffd83dbSDimitry Andric // There is no native support for FRem. 5905ffd83dbSDimitry Andric if (Opcode == Instruction::FRem) { 591fe6060f1SDimitry Andric SmallVector<Type *> Tys(Args.size(), Ty); 592bdd1243dSDimitry Andric InstructionCost Cost = (VF * LIBCALL_COST) + 593bdd1243dSDimitry Andric getScalarizationOverhead(VTy, Args, Tys, CostKind); 5945ffd83dbSDimitry Andric // FIXME: VF 2 for float is currently just as expensive as for VF 4. 5955ffd83dbSDimitry Andric if (VF == 2 && ScalarBits == 32) 5965ffd83dbSDimitry Andric Cost *= 2; 5975ffd83dbSDimitry Andric return Cost; 5985ffd83dbSDimitry Andric } 5995ffd83dbSDimitry Andric } 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric // Fallback to the default implementation. 6025ffd83dbSDimitry Andric return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info, Op2Info, 603bdd1243dSDimitry Andric Args, CxtI); 6040b57cec5SDimitry Andric } 6050b57cec5SDimitry Andric 606*0fca6ea1SDimitry Andric InstructionCost SystemZTTIImpl::getShuffleCost( 607*0fca6ea1SDimitry Andric TTI::ShuffleKind Kind, VectorType *Tp, ArrayRef<int> Mask, 608*0fca6ea1SDimitry Andric TTI::TargetCostKind CostKind, int Index, VectorType *SubTp, 609*0fca6ea1SDimitry Andric ArrayRef<const Value *> Args, const Instruction *CxtI) { 6105f757f3fSDimitry Andric Kind = improveShuffleKindFromMask(Kind, Mask, Tp, Index, SubTp); 6115ffd83dbSDimitry Andric if (ST->hasVector()) { 6120b57cec5SDimitry Andric unsigned NumVectors = getNumVectorRegs(Tp); 6130b57cec5SDimitry Andric 6140b57cec5SDimitry Andric // TODO: Since fp32 is expanded, the shuffle cost should always be 0. 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric // FP128 values are always in scalar registers, so there is no work 6170b57cec5SDimitry Andric // involved with a shuffle, except for broadcast. In that case register 6180b57cec5SDimitry Andric // moves are done with a single instruction per element. 6190b57cec5SDimitry Andric if (Tp->getScalarType()->isFP128Ty()) 6200b57cec5SDimitry Andric return (Kind == TargetTransformInfo::SK_Broadcast ? NumVectors - 1 : 0); 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric switch (Kind) { 6230b57cec5SDimitry Andric case TargetTransformInfo::SK_ExtractSubvector: 6240b57cec5SDimitry Andric // ExtractSubvector Index indicates start offset. 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric // Extracting a subvector from first index is a noop. 6270b57cec5SDimitry Andric return (Index == 0 ? 0 : NumVectors); 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric case TargetTransformInfo::SK_Broadcast: 6300b57cec5SDimitry Andric // Loop vectorizer calls here to figure out the extra cost of 6310b57cec5SDimitry Andric // broadcasting a loaded value to all elements of a vector. Since vlrep 6320b57cec5SDimitry Andric // loads and replicates with a single instruction, adjust the returned 6330b57cec5SDimitry Andric // value. 6340b57cec5SDimitry Andric return NumVectors - 1; 6350b57cec5SDimitry Andric 6360b57cec5SDimitry Andric default: 6370b57cec5SDimitry Andric 6380b57cec5SDimitry Andric // SystemZ supports single instruction permutation / replication. 6390b57cec5SDimitry Andric return NumVectors; 6400b57cec5SDimitry Andric } 6415ffd83dbSDimitry Andric } 6420b57cec5SDimitry Andric 643bdd1243dSDimitry Andric return BaseT::getShuffleCost(Kind, Tp, Mask, CostKind, Index, SubTp); 6440b57cec5SDimitry Andric } 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric // Return the log2 difference of the element sizes of the two vector types. 6470b57cec5SDimitry Andric static unsigned getElSizeLog2Diff(Type *Ty0, Type *Ty1) { 6480b57cec5SDimitry Andric unsigned Bits0 = Ty0->getScalarSizeInBits(); 6490b57cec5SDimitry Andric unsigned Bits1 = Ty1->getScalarSizeInBits(); 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric if (Bits1 > Bits0) 6520b57cec5SDimitry Andric return (Log2_32(Bits1) - Log2_32(Bits0)); 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric return (Log2_32(Bits0) - Log2_32(Bits1)); 6550b57cec5SDimitry Andric } 6560b57cec5SDimitry Andric 6570b57cec5SDimitry Andric // Return the number of instructions needed to truncate SrcTy to DstTy. 6580b57cec5SDimitry Andric unsigned SystemZTTIImpl:: 6590b57cec5SDimitry Andric getVectorTruncCost(Type *SrcTy, Type *DstTy) { 6600b57cec5SDimitry Andric assert (SrcTy->isVectorTy() && DstTy->isVectorTy()); 661bdd1243dSDimitry Andric assert(SrcTy->getPrimitiveSizeInBits().getFixedValue() > 662bdd1243dSDimitry Andric DstTy->getPrimitiveSizeInBits().getFixedValue() && 6630b57cec5SDimitry Andric "Packing must reduce size of vector type."); 6645ffd83dbSDimitry Andric assert(cast<FixedVectorType>(SrcTy)->getNumElements() == 6655ffd83dbSDimitry Andric cast<FixedVectorType>(DstTy)->getNumElements() && 6660b57cec5SDimitry Andric "Packing should not change number of elements."); 6670b57cec5SDimitry Andric 6680b57cec5SDimitry Andric // TODO: Since fp32 is expanded, the extract cost should always be 0. 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric unsigned NumParts = getNumVectorRegs(SrcTy); 6710b57cec5SDimitry Andric if (NumParts <= 2) 6720b57cec5SDimitry Andric // Up to 2 vector registers can be truncated efficiently with pack or 6730b57cec5SDimitry Andric // permute. The latter requires an immediate mask to be loaded, which 6740b57cec5SDimitry Andric // typically gets hoisted out of a loop. TODO: return a good value for 6750b57cec5SDimitry Andric // BB-VECTORIZER that includes the immediate loads, which we do not want 6760b57cec5SDimitry Andric // to count for the loop vectorizer. 6770b57cec5SDimitry Andric return 1; 6780b57cec5SDimitry Andric 6790b57cec5SDimitry Andric unsigned Cost = 0; 6800b57cec5SDimitry Andric unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy); 6815ffd83dbSDimitry Andric unsigned VF = cast<FixedVectorType>(SrcTy)->getNumElements(); 6820b57cec5SDimitry Andric for (unsigned P = 0; P < Log2Diff; ++P) { 6830b57cec5SDimitry Andric if (NumParts > 1) 6840b57cec5SDimitry Andric NumParts /= 2; 6850b57cec5SDimitry Andric Cost += NumParts; 6860b57cec5SDimitry Andric } 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andric // Currently, a general mix of permutes and pack instructions is output by 6890b57cec5SDimitry Andric // isel, which follow the cost computation above except for this case which 6900b57cec5SDimitry Andric // is one instruction less: 6910b57cec5SDimitry Andric if (VF == 8 && SrcTy->getScalarSizeInBits() == 64 && 6920b57cec5SDimitry Andric DstTy->getScalarSizeInBits() == 8) 6930b57cec5SDimitry Andric Cost--; 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric return Cost; 6960b57cec5SDimitry Andric } 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric // Return the cost of converting a vector bitmask produced by a compare 6990b57cec5SDimitry Andric // (SrcTy), to the type of the select or extend instruction (DstTy). 7000b57cec5SDimitry Andric unsigned SystemZTTIImpl:: 7010b57cec5SDimitry Andric getVectorBitmaskConversionCost(Type *SrcTy, Type *DstTy) { 7020b57cec5SDimitry Andric assert (SrcTy->isVectorTy() && DstTy->isVectorTy() && 7030b57cec5SDimitry Andric "Should only be called with vector types."); 7040b57cec5SDimitry Andric 7050b57cec5SDimitry Andric unsigned PackCost = 0; 7060b57cec5SDimitry Andric unsigned SrcScalarBits = SrcTy->getScalarSizeInBits(); 7070b57cec5SDimitry Andric unsigned DstScalarBits = DstTy->getScalarSizeInBits(); 7080b57cec5SDimitry Andric unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy); 7090b57cec5SDimitry Andric if (SrcScalarBits > DstScalarBits) 7100b57cec5SDimitry Andric // The bitmask will be truncated. 7110b57cec5SDimitry Andric PackCost = getVectorTruncCost(SrcTy, DstTy); 7120b57cec5SDimitry Andric else if (SrcScalarBits < DstScalarBits) { 7130b57cec5SDimitry Andric unsigned DstNumParts = getNumVectorRegs(DstTy); 7140b57cec5SDimitry Andric // Each vector select needs its part of the bitmask unpacked. 7150b57cec5SDimitry Andric PackCost = Log2Diff * DstNumParts; 7160b57cec5SDimitry Andric // Extra cost for moving part of mask before unpacking. 7170b57cec5SDimitry Andric PackCost += DstNumParts - 1; 7180b57cec5SDimitry Andric } 7190b57cec5SDimitry Andric 7200b57cec5SDimitry Andric return PackCost; 7210b57cec5SDimitry Andric } 7220b57cec5SDimitry Andric 7230b57cec5SDimitry Andric // Return the type of the compared operands. This is needed to compute the 7240b57cec5SDimitry Andric // cost for a Select / ZExt or SExt instruction. 7250b57cec5SDimitry Andric static Type *getCmpOpsType(const Instruction *I, unsigned VF = 1) { 7260b57cec5SDimitry Andric Type *OpTy = nullptr; 7270b57cec5SDimitry Andric if (CmpInst *CI = dyn_cast<CmpInst>(I->getOperand(0))) 7280b57cec5SDimitry Andric OpTy = CI->getOperand(0)->getType(); 7290b57cec5SDimitry Andric else if (Instruction *LogicI = dyn_cast<Instruction>(I->getOperand(0))) 7300b57cec5SDimitry Andric if (LogicI->getNumOperands() == 2) 7310b57cec5SDimitry Andric if (CmpInst *CI0 = dyn_cast<CmpInst>(LogicI->getOperand(0))) 7320b57cec5SDimitry Andric if (isa<CmpInst>(LogicI->getOperand(1))) 7330b57cec5SDimitry Andric OpTy = CI0->getOperand(0)->getType(); 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric if (OpTy != nullptr) { 7360b57cec5SDimitry Andric if (VF == 1) { 7370b57cec5SDimitry Andric assert (!OpTy->isVectorTy() && "Expected scalar type"); 7380b57cec5SDimitry Andric return OpTy; 7390b57cec5SDimitry Andric } 7400b57cec5SDimitry Andric // Return the potentially vectorized type based on 'I' and 'VF'. 'I' may 7410b57cec5SDimitry Andric // be either scalar or already vectorized with a same or lesser VF. 7420b57cec5SDimitry Andric Type *ElTy = OpTy->getScalarType(); 7435ffd83dbSDimitry Andric return FixedVectorType::get(ElTy, VF); 7440b57cec5SDimitry Andric } 7450b57cec5SDimitry Andric 7460b57cec5SDimitry Andric return nullptr; 7470b57cec5SDimitry Andric } 7480b57cec5SDimitry Andric 7490b57cec5SDimitry Andric // Get the cost of converting a boolean vector to a vector with same width 7500b57cec5SDimitry Andric // and element size as Dst, plus the cost of zero extending if needed. 7510b57cec5SDimitry Andric unsigned SystemZTTIImpl:: 7520b57cec5SDimitry Andric getBoolVecToIntConversionCost(unsigned Opcode, Type *Dst, 7530b57cec5SDimitry Andric const Instruction *I) { 7545ffd83dbSDimitry Andric auto *DstVTy = cast<FixedVectorType>(Dst); 7555ffd83dbSDimitry Andric unsigned VF = DstVTy->getNumElements(); 7560b57cec5SDimitry Andric unsigned Cost = 0; 7570b57cec5SDimitry Andric // If we know what the widths of the compared operands, get any cost of 7580b57cec5SDimitry Andric // converting it to match Dst. Otherwise assume same widths. 7590b57cec5SDimitry Andric Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr); 7600b57cec5SDimitry Andric if (CmpOpTy != nullptr) 7610b57cec5SDimitry Andric Cost = getVectorBitmaskConversionCost(CmpOpTy, Dst); 7620b57cec5SDimitry Andric if (Opcode == Instruction::ZExt || Opcode == Instruction::UIToFP) 7630b57cec5SDimitry Andric // One 'vn' per dst vector with an immediate mask. 7640b57cec5SDimitry Andric Cost += getNumVectorRegs(Dst); 7650b57cec5SDimitry Andric return Cost; 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric 768fe6060f1SDimitry Andric InstructionCost SystemZTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, 769fe6060f1SDimitry Andric Type *Src, 770e8d8bef9SDimitry Andric TTI::CastContextHint CCH, 7715ffd83dbSDimitry Andric TTI::TargetCostKind CostKind, 7720b57cec5SDimitry Andric const Instruction *I) { 7735ffd83dbSDimitry Andric // FIXME: Can the logic below also be used for these cost kinds? 7745ffd83dbSDimitry Andric if (CostKind == TTI::TCK_CodeSize || CostKind == TTI::TCK_SizeAndLatency) { 775fe6060f1SDimitry Andric auto BaseCost = BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I); 7765ffd83dbSDimitry Andric return BaseCost == 0 ? BaseCost : 1; 7775ffd83dbSDimitry Andric } 7785ffd83dbSDimitry Andric 7790b57cec5SDimitry Andric unsigned DstScalarBits = Dst->getScalarSizeInBits(); 7800b57cec5SDimitry Andric unsigned SrcScalarBits = Src->getScalarSizeInBits(); 7810b57cec5SDimitry Andric 7825ffd83dbSDimitry Andric if (!Src->isVectorTy()) { 7835ffd83dbSDimitry Andric assert (!Dst->isVectorTy()); 7845ffd83dbSDimitry Andric 7855ffd83dbSDimitry Andric if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP) { 7867a6dacacSDimitry Andric if (Src->isIntegerTy(128)) 7877a6dacacSDimitry Andric return LIBCALL_COST; 7885ffd83dbSDimitry Andric if (SrcScalarBits >= 32 || 7895ffd83dbSDimitry Andric (I != nullptr && isa<LoadInst>(I->getOperand(0)))) 7905ffd83dbSDimitry Andric return 1; 7915ffd83dbSDimitry Andric return SrcScalarBits > 1 ? 2 /*i8/i16 extend*/ : 5 /*branch seq.*/; 7925ffd83dbSDimitry Andric } 7935ffd83dbSDimitry Andric 7947a6dacacSDimitry Andric if ((Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI) && 7957a6dacacSDimitry Andric Dst->isIntegerTy(128)) 7967a6dacacSDimitry Andric return LIBCALL_COST; 7977a6dacacSDimitry Andric 7987a6dacacSDimitry Andric if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt)) { 7997a6dacacSDimitry Andric if (Src->isIntegerTy(1)) { 8007a6dacacSDimitry Andric if (DstScalarBits == 128) 8017a6dacacSDimitry Andric return 5 /*branch seq.*/; 8027a6dacacSDimitry Andric 8035ffd83dbSDimitry Andric if (ST->hasLoadStoreOnCond2()) 8045ffd83dbSDimitry Andric return 2; // li 0; loc 1 8055ffd83dbSDimitry Andric 8065ffd83dbSDimitry Andric // This should be extension of a compare i1 result, which is done with 8075ffd83dbSDimitry Andric // ipm and a varying sequence of instructions. 8085ffd83dbSDimitry Andric unsigned Cost = 0; 8095ffd83dbSDimitry Andric if (Opcode == Instruction::SExt) 8105ffd83dbSDimitry Andric Cost = (DstScalarBits < 64 ? 3 : 4); 8115ffd83dbSDimitry Andric if (Opcode == Instruction::ZExt) 8125ffd83dbSDimitry Andric Cost = 3; 8135ffd83dbSDimitry Andric Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I) : nullptr); 8145ffd83dbSDimitry Andric if (CmpOpTy != nullptr && CmpOpTy->isFloatingPointTy()) 8155ffd83dbSDimitry Andric // If operands of an fp-type was compared, this costs +1. 8165ffd83dbSDimitry Andric Cost++; 8175ffd83dbSDimitry Andric return Cost; 8185ffd83dbSDimitry Andric } 8197a6dacacSDimitry Andric else if (isInt128InVR(Dst)) { 8207a6dacacSDimitry Andric // Extensions from GPR to i128 (in VR) typically costs two instructions, 8217a6dacacSDimitry Andric // but a zero-extending load would be just one extra instruction. 8227a6dacacSDimitry Andric if (Opcode == Instruction::ZExt && I != nullptr) 8237a6dacacSDimitry Andric if (LoadInst *Ld = dyn_cast<LoadInst>(I->getOperand(0))) 8247a6dacacSDimitry Andric if (Ld->hasOneUse()) 8257a6dacacSDimitry Andric return 1; 8267a6dacacSDimitry Andric return 2; 8277a6dacacSDimitry Andric } 8287a6dacacSDimitry Andric } 8297a6dacacSDimitry Andric 8307a6dacacSDimitry Andric if (Opcode == Instruction::Trunc && isInt128InVR(Src) && I != nullptr) { 8317a6dacacSDimitry Andric if (LoadInst *Ld = dyn_cast<LoadInst>(I->getOperand(0))) 8327a6dacacSDimitry Andric if (Ld->hasOneUse()) 8337a6dacacSDimitry Andric return 0; // Will be converted to GPR load. 8347a6dacacSDimitry Andric bool OnlyTruncatingStores = true; 8357a6dacacSDimitry Andric for (const User *U : I->users()) 8367a6dacacSDimitry Andric if (!isa<StoreInst>(U)) { 8377a6dacacSDimitry Andric OnlyTruncatingStores = false; 8387a6dacacSDimitry Andric break; 8397a6dacacSDimitry Andric } 8407a6dacacSDimitry Andric if (OnlyTruncatingStores) 8417a6dacacSDimitry Andric return 0; 8427a6dacacSDimitry Andric return 2; // Vector element extraction. 8437a6dacacSDimitry Andric } 8445ffd83dbSDimitry Andric } 8455ffd83dbSDimitry Andric else if (ST->hasVector()) { 846fe6060f1SDimitry Andric // Vector to scalar cast. 8475ffd83dbSDimitry Andric auto *SrcVecTy = cast<FixedVectorType>(Src); 848fe6060f1SDimitry Andric auto *DstVecTy = dyn_cast<FixedVectorType>(Dst); 849fe6060f1SDimitry Andric if (!DstVecTy) { 850fe6060f1SDimitry Andric // TODO: tune vector-to-scalar cast. 851fe6060f1SDimitry Andric return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I); 852fe6060f1SDimitry Andric } 8535ffd83dbSDimitry Andric unsigned VF = SrcVecTy->getNumElements(); 8540b57cec5SDimitry Andric unsigned NumDstVectors = getNumVectorRegs(Dst); 8550b57cec5SDimitry Andric unsigned NumSrcVectors = getNumVectorRegs(Src); 8560b57cec5SDimitry Andric 8570b57cec5SDimitry Andric if (Opcode == Instruction::Trunc) { 8580b57cec5SDimitry Andric if (Src->getScalarSizeInBits() == Dst->getScalarSizeInBits()) 8590b57cec5SDimitry Andric return 0; // Check for NOOP conversions. 8600b57cec5SDimitry Andric return getVectorTruncCost(Src, Dst); 8610b57cec5SDimitry Andric } 8620b57cec5SDimitry Andric 8630b57cec5SDimitry Andric if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) { 8640b57cec5SDimitry Andric if (SrcScalarBits >= 8) { 86581ad6265SDimitry Andric // ZExt will use either a single unpack or a vector permute. 86681ad6265SDimitry Andric if (Opcode == Instruction::ZExt) 86781ad6265SDimitry Andric return NumDstVectors; 86881ad6265SDimitry Andric 86981ad6265SDimitry Andric // SExt will be handled with one unpack per doubling of width. 8700b57cec5SDimitry Andric unsigned NumUnpacks = getElSizeLog2Diff(Src, Dst); 8710b57cec5SDimitry Andric 8720b57cec5SDimitry Andric // For types that spans multiple vector registers, some additional 8730b57cec5SDimitry Andric // instructions are used to setup the unpacking. 8740b57cec5SDimitry Andric unsigned NumSrcVectorOps = 8750b57cec5SDimitry Andric (NumUnpacks > 1 ? (NumDstVectors - NumSrcVectors) 8760b57cec5SDimitry Andric : (NumDstVectors / 2)); 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric return (NumUnpacks * NumDstVectors) + NumSrcVectorOps; 8790b57cec5SDimitry Andric } 8800b57cec5SDimitry Andric else if (SrcScalarBits == 1) 8810b57cec5SDimitry Andric return getBoolVecToIntConversionCost(Opcode, Dst, I); 8820b57cec5SDimitry Andric } 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP || 8850b57cec5SDimitry Andric Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI) { 8860b57cec5SDimitry Andric // TODO: Fix base implementation which could simplify things a bit here 8870b57cec5SDimitry Andric // (seems to miss on differentiating on scalar/vector types). 8880b57cec5SDimitry Andric 8898bcb0991SDimitry Andric // Only 64 bit vector conversions are natively supported before z15. 8900b57cec5SDimitry Andric if (DstScalarBits == 64 || ST->hasVectorEnhancements2()) { 8910b57cec5SDimitry Andric if (SrcScalarBits == DstScalarBits) 8920b57cec5SDimitry Andric return NumDstVectors; 8930b57cec5SDimitry Andric 8940b57cec5SDimitry Andric if (SrcScalarBits == 1) 8950b57cec5SDimitry Andric return getBoolVecToIntConversionCost(Opcode, Dst, I) + NumDstVectors; 8960b57cec5SDimitry Andric } 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric // Return the cost of multiple scalar invocation plus the cost of 8990b57cec5SDimitry Andric // inserting and extracting the values. Base implementation does not 9000b57cec5SDimitry Andric // realize float->int gets scalarized. 901fe6060f1SDimitry Andric InstructionCost ScalarCost = getCastInstrCost( 902e8d8bef9SDimitry Andric Opcode, Dst->getScalarType(), Src->getScalarType(), CCH, CostKind); 903fe6060f1SDimitry Andric InstructionCost TotCost = VF * ScalarCost; 9040b57cec5SDimitry Andric bool NeedsInserts = true, NeedsExtracts = true; 9050b57cec5SDimitry Andric // FP128 registers do not get inserted or extracted. 9060b57cec5SDimitry Andric if (DstScalarBits == 128 && 9070b57cec5SDimitry Andric (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP)) 9080b57cec5SDimitry Andric NeedsInserts = false; 9090b57cec5SDimitry Andric if (SrcScalarBits == 128 && 9100b57cec5SDimitry Andric (Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI)) 9110b57cec5SDimitry Andric NeedsExtracts = false; 9120b57cec5SDimitry Andric 913bdd1243dSDimitry Andric TotCost += getScalarizationOverhead(SrcVecTy, /*Insert*/ false, 914bdd1243dSDimitry Andric NeedsExtracts, CostKind); 915bdd1243dSDimitry Andric TotCost += getScalarizationOverhead(DstVecTy, NeedsInserts, 916bdd1243dSDimitry Andric /*Extract*/ false, CostKind); 9170b57cec5SDimitry Andric 9180b57cec5SDimitry Andric // FIXME: VF 2 for float<->i32 is currently just as expensive as for VF 4. 9190b57cec5SDimitry Andric if (VF == 2 && SrcScalarBits == 32 && DstScalarBits == 32) 9200b57cec5SDimitry Andric TotCost *= 2; 9210b57cec5SDimitry Andric 9220b57cec5SDimitry Andric return TotCost; 9230b57cec5SDimitry Andric } 9240b57cec5SDimitry Andric 9250b57cec5SDimitry Andric if (Opcode == Instruction::FPTrunc) { 9260b57cec5SDimitry Andric if (SrcScalarBits == 128) // fp128 -> double/float + inserts of elements. 9275ffd83dbSDimitry Andric return VF /*ldxbr/lexbr*/ + 928bdd1243dSDimitry Andric getScalarizationOverhead(DstVecTy, /*Insert*/ true, 929bdd1243dSDimitry Andric /*Extract*/ false, CostKind); 9300b57cec5SDimitry Andric else // double -> float 9310b57cec5SDimitry Andric return VF / 2 /*vledb*/ + std::max(1U, VF / 4 /*vperm*/); 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric if (Opcode == Instruction::FPExt) { 9350b57cec5SDimitry Andric if (SrcScalarBits == 32 && DstScalarBits == 64) { 9360b57cec5SDimitry Andric // float -> double is very rare and currently unoptimized. Instead of 9370b57cec5SDimitry Andric // using vldeb, which can do two at a time, all conversions are 9380b57cec5SDimitry Andric // scalarized. 9390b57cec5SDimitry Andric return VF * 2; 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric // -> fp128. VF * lxdb/lxeb + extraction of elements. 942bdd1243dSDimitry Andric return VF + getScalarizationOverhead(SrcVecTy, /*Insert*/ false, 943bdd1243dSDimitry Andric /*Extract*/ true, CostKind); 9440b57cec5SDimitry Andric } 9450b57cec5SDimitry Andric } 9460b57cec5SDimitry Andric 947e8d8bef9SDimitry Andric return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I); 9480b57cec5SDimitry Andric } 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric // Scalar i8 / i16 operations will typically be made after first extending 9510b57cec5SDimitry Andric // the operands to i32. 9520b57cec5SDimitry Andric static unsigned getOperandsExtensionCost(const Instruction *I) { 9530b57cec5SDimitry Andric unsigned ExtCost = 0; 9540b57cec5SDimitry Andric for (Value *Op : I->operands()) 9550b57cec5SDimitry Andric // A load of i8 or i16 sign/zero extends to i32. 9560b57cec5SDimitry Andric if (!isa<LoadInst>(Op) && !isa<ConstantInt>(Op)) 9570b57cec5SDimitry Andric ExtCost++; 9580b57cec5SDimitry Andric 9590b57cec5SDimitry Andric return ExtCost; 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric 962fe6060f1SDimitry Andric InstructionCost SystemZTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 963fe6060f1SDimitry Andric Type *CondTy, 964fe6060f1SDimitry Andric CmpInst::Predicate VecPred, 9655ffd83dbSDimitry Andric TTI::TargetCostKind CostKind, 9665ffd83dbSDimitry Andric const Instruction *I) { 9675ffd83dbSDimitry Andric if (CostKind != TTI::TCK_RecipThroughput) 968e8d8bef9SDimitry Andric return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind); 9695ffd83dbSDimitry Andric 9705ffd83dbSDimitry Andric if (!ValTy->isVectorTy()) { 9715ffd83dbSDimitry Andric switch (Opcode) { 9725ffd83dbSDimitry Andric case Instruction::ICmp: { 9735ffd83dbSDimitry Andric // A loaded value compared with 0 with multiple users becomes Load and 9745ffd83dbSDimitry Andric // Test. The load is then not foldable, so return 0 cost for the ICmp. 9755ffd83dbSDimitry Andric unsigned ScalarBits = ValTy->getScalarSizeInBits(); 9767a6dacacSDimitry Andric if (I != nullptr && (ScalarBits == 32 || ScalarBits == 64)) 9775ffd83dbSDimitry Andric if (LoadInst *Ld = dyn_cast<LoadInst>(I->getOperand(0))) 9785ffd83dbSDimitry Andric if (const ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1))) 9795ffd83dbSDimitry Andric if (!Ld->hasOneUse() && Ld->getParent() == I->getParent() && 980e8d8bef9SDimitry Andric C->isZero()) 9815ffd83dbSDimitry Andric return 0; 9825ffd83dbSDimitry Andric 9835ffd83dbSDimitry Andric unsigned Cost = 1; 9845ffd83dbSDimitry Andric if (ValTy->isIntegerTy() && ValTy->getScalarSizeInBits() <= 16) 9855ffd83dbSDimitry Andric Cost += (I != nullptr ? getOperandsExtensionCost(I) : 2); 9865ffd83dbSDimitry Andric return Cost; 9875ffd83dbSDimitry Andric } 9885ffd83dbSDimitry Andric case Instruction::Select: 9897a6dacacSDimitry Andric if (ValTy->isFloatingPointTy() || isInt128InVR(ValTy)) 9907a6dacacSDimitry Andric return 4; // No LOC for FP / i128 - costs a conditional jump. 9915ffd83dbSDimitry Andric return 1; // Load On Condition / Select Register. 9925ffd83dbSDimitry Andric } 9935ffd83dbSDimitry Andric } 9945ffd83dbSDimitry Andric else if (ST->hasVector()) { 9955ffd83dbSDimitry Andric unsigned VF = cast<FixedVectorType>(ValTy)->getNumElements(); 9960b57cec5SDimitry Andric 9970b57cec5SDimitry Andric // Called with a compare instruction. 9980b57cec5SDimitry Andric if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) { 9990b57cec5SDimitry Andric unsigned PredicateExtraCost = 0; 10000b57cec5SDimitry Andric if (I != nullptr) { 10010b57cec5SDimitry Andric // Some predicates cost one or two extra instructions. 10020b57cec5SDimitry Andric switch (cast<CmpInst>(I)->getPredicate()) { 10030b57cec5SDimitry Andric case CmpInst::Predicate::ICMP_NE: 10040b57cec5SDimitry Andric case CmpInst::Predicate::ICMP_UGE: 10050b57cec5SDimitry Andric case CmpInst::Predicate::ICMP_ULE: 10060b57cec5SDimitry Andric case CmpInst::Predicate::ICMP_SGE: 10070b57cec5SDimitry Andric case CmpInst::Predicate::ICMP_SLE: 10080b57cec5SDimitry Andric PredicateExtraCost = 1; 10090b57cec5SDimitry Andric break; 10100b57cec5SDimitry Andric case CmpInst::Predicate::FCMP_ONE: 10110b57cec5SDimitry Andric case CmpInst::Predicate::FCMP_ORD: 10120b57cec5SDimitry Andric case CmpInst::Predicate::FCMP_UEQ: 10130b57cec5SDimitry Andric case CmpInst::Predicate::FCMP_UNO: 10140b57cec5SDimitry Andric PredicateExtraCost = 2; 10150b57cec5SDimitry Andric break; 10160b57cec5SDimitry Andric default: 10170b57cec5SDimitry Andric break; 10180b57cec5SDimitry Andric } 10190b57cec5SDimitry Andric } 10200b57cec5SDimitry Andric 10210b57cec5SDimitry Andric // Float is handled with 2*vmr[lh]f + 2*vldeb + vfchdb for each pair of 10220b57cec5SDimitry Andric // floats. FIXME: <2 x float> generates same code as <4 x float>. 10230b57cec5SDimitry Andric unsigned CmpCostPerVector = (ValTy->getScalarType()->isFloatTy() ? 10 : 1); 10240b57cec5SDimitry Andric unsigned NumVecs_cmp = getNumVectorRegs(ValTy); 10250b57cec5SDimitry Andric 10260b57cec5SDimitry Andric unsigned Cost = (NumVecs_cmp * (CmpCostPerVector + PredicateExtraCost)); 10270b57cec5SDimitry Andric return Cost; 10280b57cec5SDimitry Andric } 10290b57cec5SDimitry Andric else { // Called with a select instruction. 10300b57cec5SDimitry Andric assert (Opcode == Instruction::Select); 10310b57cec5SDimitry Andric 10320b57cec5SDimitry Andric // We can figure out the extra cost of packing / unpacking if the 10330b57cec5SDimitry Andric // instruction was passed and the compare instruction is found. 10340b57cec5SDimitry Andric unsigned PackCost = 0; 10350b57cec5SDimitry Andric Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr); 10360b57cec5SDimitry Andric if (CmpOpTy != nullptr) 10370b57cec5SDimitry Andric PackCost = 10380b57cec5SDimitry Andric getVectorBitmaskConversionCost(CmpOpTy, ValTy); 10390b57cec5SDimitry Andric 10400b57cec5SDimitry Andric return getNumVectorRegs(ValTy) /*vsel*/ + PackCost; 10410b57cec5SDimitry Andric } 10420b57cec5SDimitry Andric } 10430b57cec5SDimitry Andric 1044e8d8bef9SDimitry Andric return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind); 10450b57cec5SDimitry Andric } 10460b57cec5SDimitry Andric 1047fe6060f1SDimitry Andric InstructionCost SystemZTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, 1048bdd1243dSDimitry Andric TTI::TargetCostKind CostKind, 1049bdd1243dSDimitry Andric unsigned Index, Value *Op0, 1050bdd1243dSDimitry Andric Value *Op1) { 10510b57cec5SDimitry Andric // vlvgp will insert two grs into a vector register, so only count half the 10520b57cec5SDimitry Andric // number of instructions. 10530b57cec5SDimitry Andric if (Opcode == Instruction::InsertElement && Val->isIntOrIntVectorTy(64)) 10540b57cec5SDimitry Andric return ((Index % 2 == 0) ? 1 : 0); 10550b57cec5SDimitry Andric 10560b57cec5SDimitry Andric if (Opcode == Instruction::ExtractElement) { 10570b57cec5SDimitry Andric int Cost = ((getScalarSizeInBits(Val) == 1) ? 2 /*+test-under-mask*/ : 1); 10580b57cec5SDimitry Andric 10590b57cec5SDimitry Andric // Give a slight penalty for moving out of vector pipeline to FXU unit. 10600b57cec5SDimitry Andric if (Index == 0 && Val->isIntOrIntVectorTy()) 10610b57cec5SDimitry Andric Cost += 1; 10620b57cec5SDimitry Andric 10630b57cec5SDimitry Andric return Cost; 10640b57cec5SDimitry Andric } 10650b57cec5SDimitry Andric 1066bdd1243dSDimitry Andric return BaseT::getVectorInstrCost(Opcode, Val, CostKind, Index, Op0, Op1); 10670b57cec5SDimitry Andric } 10680b57cec5SDimitry Andric 10690b57cec5SDimitry Andric // Check if a load may be folded as a memory operand in its user. 10700b57cec5SDimitry Andric bool SystemZTTIImpl:: 10710b57cec5SDimitry Andric isFoldableLoad(const LoadInst *Ld, const Instruction *&FoldedValue) { 10720b57cec5SDimitry Andric if (!Ld->hasOneUse()) 10730b57cec5SDimitry Andric return false; 10740b57cec5SDimitry Andric FoldedValue = Ld; 10750b57cec5SDimitry Andric const Instruction *UserI = cast<Instruction>(*Ld->user_begin()); 10760b57cec5SDimitry Andric unsigned LoadedBits = getScalarSizeInBits(Ld->getType()); 10770b57cec5SDimitry Andric unsigned TruncBits = 0; 10780b57cec5SDimitry Andric unsigned SExtBits = 0; 10790b57cec5SDimitry Andric unsigned ZExtBits = 0; 10800b57cec5SDimitry Andric if (UserI->hasOneUse()) { 10810b57cec5SDimitry Andric unsigned UserBits = UserI->getType()->getScalarSizeInBits(); 10820b57cec5SDimitry Andric if (isa<TruncInst>(UserI)) 10830b57cec5SDimitry Andric TruncBits = UserBits; 10840b57cec5SDimitry Andric else if (isa<SExtInst>(UserI)) 10850b57cec5SDimitry Andric SExtBits = UserBits; 10860b57cec5SDimitry Andric else if (isa<ZExtInst>(UserI)) 10870b57cec5SDimitry Andric ZExtBits = UserBits; 10880b57cec5SDimitry Andric } 10890b57cec5SDimitry Andric if (TruncBits || SExtBits || ZExtBits) { 10900b57cec5SDimitry Andric FoldedValue = UserI; 10910b57cec5SDimitry Andric UserI = cast<Instruction>(*UserI->user_begin()); 10920b57cec5SDimitry Andric // Load (single use) -> trunc/extend (single use) -> UserI 10930b57cec5SDimitry Andric } 10940b57cec5SDimitry Andric if ((UserI->getOpcode() == Instruction::Sub || 10950b57cec5SDimitry Andric UserI->getOpcode() == Instruction::SDiv || 10960b57cec5SDimitry Andric UserI->getOpcode() == Instruction::UDiv) && 10970b57cec5SDimitry Andric UserI->getOperand(1) != FoldedValue) 10980b57cec5SDimitry Andric return false; // Not commutative, only RHS foldable. 10990b57cec5SDimitry Andric // LoadOrTruncBits holds the number of effectively loaded bits, but 0 if an 11000b57cec5SDimitry Andric // extension was made of the load. 11010b57cec5SDimitry Andric unsigned LoadOrTruncBits = 11020b57cec5SDimitry Andric ((SExtBits || ZExtBits) ? 0 : (TruncBits ? TruncBits : LoadedBits)); 11030b57cec5SDimitry Andric switch (UserI->getOpcode()) { 11040b57cec5SDimitry Andric case Instruction::Add: // SE: 16->32, 16/32->64, z14:16->64. ZE: 32->64 11050b57cec5SDimitry Andric case Instruction::Sub: 11060b57cec5SDimitry Andric case Instruction::ICmp: 11070b57cec5SDimitry Andric if (LoadedBits == 32 && ZExtBits == 64) 11080b57cec5SDimitry Andric return true; 1109bdd1243dSDimitry Andric [[fallthrough]]; 11100b57cec5SDimitry Andric case Instruction::Mul: // SE: 16->32, 32->64, z14:16->64 11110b57cec5SDimitry Andric if (UserI->getOpcode() != Instruction::ICmp) { 11120b57cec5SDimitry Andric if (LoadedBits == 16 && 11130b57cec5SDimitry Andric (SExtBits == 32 || 11140b57cec5SDimitry Andric (SExtBits == 64 && ST->hasMiscellaneousExtensions2()))) 11150b57cec5SDimitry Andric return true; 11160b57cec5SDimitry Andric if (LoadOrTruncBits == 16) 11170b57cec5SDimitry Andric return true; 11180b57cec5SDimitry Andric } 1119bdd1243dSDimitry Andric [[fallthrough]]; 11200b57cec5SDimitry Andric case Instruction::SDiv:// SE: 32->64 11210b57cec5SDimitry Andric if (LoadedBits == 32 && SExtBits == 64) 11220b57cec5SDimitry Andric return true; 1123bdd1243dSDimitry Andric [[fallthrough]]; 11240b57cec5SDimitry Andric case Instruction::UDiv: 11250b57cec5SDimitry Andric case Instruction::And: 11260b57cec5SDimitry Andric case Instruction::Or: 11270b57cec5SDimitry Andric case Instruction::Xor: 11280b57cec5SDimitry Andric // This also makes sense for float operations, but disabled for now due 11290b57cec5SDimitry Andric // to regressions. 11300b57cec5SDimitry Andric // case Instruction::FCmp: 11310b57cec5SDimitry Andric // case Instruction::FAdd: 11320b57cec5SDimitry Andric // case Instruction::FSub: 11330b57cec5SDimitry Andric // case Instruction::FMul: 11340b57cec5SDimitry Andric // case Instruction::FDiv: 11350b57cec5SDimitry Andric 11360b57cec5SDimitry Andric // All possible extensions of memory checked above. 11370b57cec5SDimitry Andric 11380b57cec5SDimitry Andric // Comparison between memory and immediate. 11390b57cec5SDimitry Andric if (UserI->getOpcode() == Instruction::ICmp) 11400b57cec5SDimitry Andric if (ConstantInt *CI = dyn_cast<ConstantInt>(UserI->getOperand(1))) 1141e8d8bef9SDimitry Andric if (CI->getValue().isIntN(16)) 11420b57cec5SDimitry Andric return true; 11430b57cec5SDimitry Andric return (LoadOrTruncBits == 32 || LoadOrTruncBits == 64); 11440b57cec5SDimitry Andric break; 11450b57cec5SDimitry Andric } 11460b57cec5SDimitry Andric return false; 11470b57cec5SDimitry Andric } 11480b57cec5SDimitry Andric 11490b57cec5SDimitry Andric static bool isBswapIntrinsicCall(const Value *V) { 11500b57cec5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(V)) 11510b57cec5SDimitry Andric if (auto *CI = dyn_cast<CallInst>(I)) 11520b57cec5SDimitry Andric if (auto *F = CI->getCalledFunction()) 11530b57cec5SDimitry Andric if (F->getIntrinsicID() == Intrinsic::bswap) 11540b57cec5SDimitry Andric return true; 11550b57cec5SDimitry Andric return false; 11560b57cec5SDimitry Andric } 11570b57cec5SDimitry Andric 1158fe6060f1SDimitry Andric InstructionCost SystemZTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, 1159fe6060f1SDimitry Andric MaybeAlign Alignment, 1160fe6060f1SDimitry Andric unsigned AddressSpace, 11615ffd83dbSDimitry Andric TTI::TargetCostKind CostKind, 1162bdd1243dSDimitry Andric TTI::OperandValueInfo OpInfo, 11630b57cec5SDimitry Andric const Instruction *I) { 11640b57cec5SDimitry Andric assert(!Src->isVoidTy() && "Invalid type"); 11650b57cec5SDimitry Andric 11665ffd83dbSDimitry Andric // TODO: Handle other cost kinds. 11675ffd83dbSDimitry Andric if (CostKind != TTI::TCK_RecipThroughput) 11685ffd83dbSDimitry Andric return 1; 11695ffd83dbSDimitry Andric 11700b57cec5SDimitry Andric if (!Src->isVectorTy() && Opcode == Instruction::Load && I != nullptr) { 11710b57cec5SDimitry Andric // Store the load or its truncated or extended value in FoldedValue. 11720b57cec5SDimitry Andric const Instruction *FoldedValue = nullptr; 11730b57cec5SDimitry Andric if (isFoldableLoad(cast<LoadInst>(I), FoldedValue)) { 11740b57cec5SDimitry Andric const Instruction *UserI = cast<Instruction>(*FoldedValue->user_begin()); 11750b57cec5SDimitry Andric assert (UserI->getNumOperands() == 2 && "Expected a binop."); 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric // UserI can't fold two loads, so in that case return 0 cost only 11780b57cec5SDimitry Andric // half of the time. 11790b57cec5SDimitry Andric for (unsigned i = 0; i < 2; ++i) { 11800b57cec5SDimitry Andric if (UserI->getOperand(i) == FoldedValue) 11810b57cec5SDimitry Andric continue; 11820b57cec5SDimitry Andric 11830b57cec5SDimitry Andric if (Instruction *OtherOp = dyn_cast<Instruction>(UserI->getOperand(i))){ 11840b57cec5SDimitry Andric LoadInst *OtherLoad = dyn_cast<LoadInst>(OtherOp); 11850b57cec5SDimitry Andric if (!OtherLoad && 11860b57cec5SDimitry Andric (isa<TruncInst>(OtherOp) || isa<SExtInst>(OtherOp) || 11870b57cec5SDimitry Andric isa<ZExtInst>(OtherOp))) 11880b57cec5SDimitry Andric OtherLoad = dyn_cast<LoadInst>(OtherOp->getOperand(0)); 11890b57cec5SDimitry Andric if (OtherLoad && isFoldableLoad(OtherLoad, FoldedValue/*dummy*/)) 11900b57cec5SDimitry Andric return i == 0; // Both operands foldable. 11910b57cec5SDimitry Andric } 11920b57cec5SDimitry Andric } 11930b57cec5SDimitry Andric 11940b57cec5SDimitry Andric return 0; // Only I is foldable in user. 11950b57cec5SDimitry Andric } 11960b57cec5SDimitry Andric } 11970b57cec5SDimitry Andric 11988a4dda33SDimitry Andric // Type legalization (via getNumberOfParts) can't handle structs 11998a4dda33SDimitry Andric if (TLI->getValueType(DL, Src, true) == MVT::Other) 12008a4dda33SDimitry Andric return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, 12018a4dda33SDimitry Andric CostKind); 12028a4dda33SDimitry Andric 12037a6dacacSDimitry Andric // FP128 is a legal type but kept in a register pair on older CPUs. 12047a6dacacSDimitry Andric if (Src->isFP128Ty() && !ST->hasVectorEnhancements1()) 12057a6dacacSDimitry Andric return 2; 12067a6dacacSDimitry Andric 12070b57cec5SDimitry Andric unsigned NumOps = 12080b57cec5SDimitry Andric (Src->isVectorTy() ? getNumVectorRegs(Src) : getNumberOfParts(Src)); 12090b57cec5SDimitry Andric 12100b57cec5SDimitry Andric // Store/Load reversed saves one instruction. 12110b57cec5SDimitry Andric if (((!Src->isVectorTy() && NumOps == 1) || ST->hasVectorEnhancements2()) && 12120b57cec5SDimitry Andric I != nullptr) { 12130b57cec5SDimitry Andric if (Opcode == Instruction::Load && I->hasOneUse()) { 12140b57cec5SDimitry Andric const Instruction *LdUser = cast<Instruction>(*I->user_begin()); 12150b57cec5SDimitry Andric // In case of load -> bswap -> store, return normal cost for the load. 12160b57cec5SDimitry Andric if (isBswapIntrinsicCall(LdUser) && 12170b57cec5SDimitry Andric (!LdUser->hasOneUse() || !isa<StoreInst>(*LdUser->user_begin()))) 12180b57cec5SDimitry Andric return 0; 12190b57cec5SDimitry Andric } 12200b57cec5SDimitry Andric else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) { 12210b57cec5SDimitry Andric const Value *StoredVal = SI->getValueOperand(); 12220b57cec5SDimitry Andric if (StoredVal->hasOneUse() && isBswapIntrinsicCall(StoredVal)) 12230b57cec5SDimitry Andric return 0; 12240b57cec5SDimitry Andric } 12250b57cec5SDimitry Andric } 12260b57cec5SDimitry Andric 12270b57cec5SDimitry Andric return NumOps; 12280b57cec5SDimitry Andric } 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric // The generic implementation of getInterleavedMemoryOpCost() is based on 12310b57cec5SDimitry Andric // adding costs of the memory operations plus all the extracts and inserts 12320b57cec5SDimitry Andric // needed for using / defining the vector operands. The SystemZ version does 12330b57cec5SDimitry Andric // roughly the same but bases the computations on vector permutations 12340b57cec5SDimitry Andric // instead. 1235fe6060f1SDimitry Andric InstructionCost SystemZTTIImpl::getInterleavedMemoryOpCost( 12365ffd83dbSDimitry Andric unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices, 12375ffd83dbSDimitry Andric Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind, 12385ffd83dbSDimitry Andric bool UseMaskForCond, bool UseMaskForGaps) { 12390b57cec5SDimitry Andric if (UseMaskForCond || UseMaskForGaps) 12400b57cec5SDimitry Andric return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices, 12415ffd83dbSDimitry Andric Alignment, AddressSpace, CostKind, 12420b57cec5SDimitry Andric UseMaskForCond, UseMaskForGaps); 12430b57cec5SDimitry Andric assert(isa<VectorType>(VecTy) && 12440b57cec5SDimitry Andric "Expect a vector type for interleaved memory op"); 12450b57cec5SDimitry Andric 12465ffd83dbSDimitry Andric unsigned NumElts = cast<FixedVectorType>(VecTy)->getNumElements(); 12470b57cec5SDimitry Andric assert(Factor > 1 && NumElts % Factor == 0 && "Invalid interleave factor"); 12480b57cec5SDimitry Andric unsigned VF = NumElts / Factor; 12490b57cec5SDimitry Andric unsigned NumEltsPerVecReg = (128U / getScalarSizeInBits(VecTy)); 12500b57cec5SDimitry Andric unsigned NumVectorMemOps = getNumVectorRegs(VecTy); 12510b57cec5SDimitry Andric unsigned NumPermutes = 0; 12520b57cec5SDimitry Andric 12530b57cec5SDimitry Andric if (Opcode == Instruction::Load) { 12540b57cec5SDimitry Andric // Loading interleave groups may have gaps, which may mean fewer 12550b57cec5SDimitry Andric // loads. Find out how many vectors will be loaded in total, and in how 12560b57cec5SDimitry Andric // many of them each value will be in. 12570b57cec5SDimitry Andric BitVector UsedInsts(NumVectorMemOps, false); 12580b57cec5SDimitry Andric std::vector<BitVector> ValueVecs(Factor, BitVector(NumVectorMemOps, false)); 12590b57cec5SDimitry Andric for (unsigned Index : Indices) 12600b57cec5SDimitry Andric for (unsigned Elt = 0; Elt < VF; ++Elt) { 12610b57cec5SDimitry Andric unsigned Vec = (Index + Elt * Factor) / NumEltsPerVecReg; 12620b57cec5SDimitry Andric UsedInsts.set(Vec); 12630b57cec5SDimitry Andric ValueVecs[Index].set(Vec); 12640b57cec5SDimitry Andric } 12650b57cec5SDimitry Andric NumVectorMemOps = UsedInsts.count(); 12660b57cec5SDimitry Andric 12670b57cec5SDimitry Andric for (unsigned Index : Indices) { 12680b57cec5SDimitry Andric // Estimate that each loaded source vector containing this Index 12690b57cec5SDimitry Andric // requires one operation, except that vperm can handle two input 12700b57cec5SDimitry Andric // registers first time for each dst vector. 12710b57cec5SDimitry Andric unsigned NumSrcVecs = ValueVecs[Index].count(); 1272fe6060f1SDimitry Andric unsigned NumDstVecs = divideCeil(VF * getScalarSizeInBits(VecTy), 128U); 12730b57cec5SDimitry Andric assert (NumSrcVecs >= NumDstVecs && "Expected at least as many sources"); 12740b57cec5SDimitry Andric NumPermutes += std::max(1U, NumSrcVecs - NumDstVecs); 12750b57cec5SDimitry Andric } 12760b57cec5SDimitry Andric } else { 12770b57cec5SDimitry Andric // Estimate the permutes for each stored vector as the smaller of the 12780b57cec5SDimitry Andric // number of elements and the number of source vectors. Subtract one per 12790b57cec5SDimitry Andric // dst vector for vperm (S.A.). 12800b57cec5SDimitry Andric unsigned NumSrcVecs = std::min(NumEltsPerVecReg, Factor); 12810b57cec5SDimitry Andric unsigned NumDstVecs = NumVectorMemOps; 12820b57cec5SDimitry Andric NumPermutes += (NumDstVecs * NumSrcVecs) - NumDstVecs; 12830b57cec5SDimitry Andric } 12840b57cec5SDimitry Andric 12850b57cec5SDimitry Andric // Cost of load/store operations and the permutations needed. 12860b57cec5SDimitry Andric return NumVectorMemOps + NumPermutes; 12870b57cec5SDimitry Andric } 12880b57cec5SDimitry Andric 1289*0fca6ea1SDimitry Andric static int 1290*0fca6ea1SDimitry Andric getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 1291*0fca6ea1SDimitry Andric const SmallVectorImpl<Type *> &ParamTys) { 12920b57cec5SDimitry Andric if (RetTy->isVectorTy() && ID == Intrinsic::bswap) 12930b57cec5SDimitry Andric return getNumVectorRegs(RetTy); // VPERM 1294*0fca6ea1SDimitry Andric 1295*0fca6ea1SDimitry Andric if (ID == Intrinsic::vector_reduce_add) { 1296*0fca6ea1SDimitry Andric // Retrieve number and size of elements for the vector op. 1297*0fca6ea1SDimitry Andric auto *VTy = cast<FixedVectorType>(ParamTys.front()); 1298*0fca6ea1SDimitry Andric unsigned ScalarSize = VTy->getScalarSizeInBits(); 1299*0fca6ea1SDimitry Andric // For scalar sizes >128 bits, we fall back to the generic cost estimate. 1300*0fca6ea1SDimitry Andric if (ScalarSize > SystemZ::VectorBits) 1301*0fca6ea1SDimitry Andric return -1; 1302*0fca6ea1SDimitry Andric // This many vector regs are needed to represent the input elements (V). 1303*0fca6ea1SDimitry Andric unsigned VectorRegsNeeded = getNumVectorRegs(VTy); 1304*0fca6ea1SDimitry Andric // This many instructions are needed for the final sum of vector elems (S). 1305*0fca6ea1SDimitry Andric unsigned LastVectorHandling = (ScalarSize < 32) ? 3 : 2; 1306*0fca6ea1SDimitry Andric // We use vector adds to create a sum vector, which takes 1307*0fca6ea1SDimitry Andric // V/2 + V/4 + ... = V - 1 operations. 1308*0fca6ea1SDimitry Andric // Then, we need S operations to sum up the elements of that sum vector, 1309*0fca6ea1SDimitry Andric // for a total of V + S - 1 operations. 1310*0fca6ea1SDimitry Andric int Cost = VectorRegsNeeded + LastVectorHandling - 1; 1311*0fca6ea1SDimitry Andric return Cost; 1312*0fca6ea1SDimitry Andric } 13130b57cec5SDimitry Andric return -1; 13140b57cec5SDimitry Andric } 13150b57cec5SDimitry Andric 1316fe6060f1SDimitry Andric InstructionCost 1317fe6060f1SDimitry Andric SystemZTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, 13185ffd83dbSDimitry Andric TTI::TargetCostKind CostKind) { 1319*0fca6ea1SDimitry Andric InstructionCost Cost = getVectorIntrinsicInstrCost( 1320*0fca6ea1SDimitry Andric ICA.getID(), ICA.getReturnType(), ICA.getArgTypes()); 13210b57cec5SDimitry Andric if (Cost != -1) 13220b57cec5SDimitry Andric return Cost; 13235ffd83dbSDimitry Andric return BaseT::getIntrinsicInstrCost(ICA, CostKind); 13240b57cec5SDimitry Andric } 1325*0fca6ea1SDimitry Andric 1326*0fca6ea1SDimitry Andric bool SystemZTTIImpl::shouldExpandReduction(const IntrinsicInst *II) const { 1327*0fca6ea1SDimitry Andric // Always expand on Subtargets without vector instructions. 1328*0fca6ea1SDimitry Andric if (!ST->hasVector()) 1329*0fca6ea1SDimitry Andric return true; 1330*0fca6ea1SDimitry Andric 1331*0fca6ea1SDimitry Andric // Whether or not to expand is a per-intrinsic decision. 1332*0fca6ea1SDimitry Andric switch (II->getIntrinsicID()) { 1333*0fca6ea1SDimitry Andric default: 1334*0fca6ea1SDimitry Andric return true; 1335*0fca6ea1SDimitry Andric // Do not expand vector.reduce.add... 1336*0fca6ea1SDimitry Andric case Intrinsic::vector_reduce_add: 1337*0fca6ea1SDimitry Andric auto *VType = cast<FixedVectorType>(II->getOperand(0)->getType()); 1338*0fca6ea1SDimitry Andric // ...unless the scalar size is i64 or larger, 1339*0fca6ea1SDimitry Andric // or the operand vector is not full, since the 1340*0fca6ea1SDimitry Andric // performance benefit is dubious in those cases. 1341*0fca6ea1SDimitry Andric return VType->getScalarSizeInBits() >= 64 || 1342*0fca6ea1SDimitry Andric VType->getPrimitiveSizeInBits() < SystemZ::VectorBits; 1343*0fca6ea1SDimitry Andric } 1344*0fca6ea1SDimitry Andric } 1345