xref: /src/contrib/llvm-project/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
15a5ac124SDimitry Andric //===-- SystemZTargetTransformInfo.cpp - SystemZ-specific TTI -------------===//
25a5ac124SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a5ac124SDimitry Andric //
75a5ac124SDimitry Andric //===----------------------------------------------------------------------===//
85a5ac124SDimitry Andric //
95a5ac124SDimitry Andric // This file implements a TargetTransformInfo analysis pass specific to the
105a5ac124SDimitry Andric // SystemZ target machine. It uses the target's detailed information to provide
115a5ac124SDimitry Andric // more precise answers to certain TTI queries, while letting the target
125a5ac124SDimitry Andric // independent and default TTI implementations handle the rest.
135a5ac124SDimitry Andric //
145a5ac124SDimitry Andric //===----------------------------------------------------------------------===//
155a5ac124SDimitry Andric 
165a5ac124SDimitry Andric #include "SystemZTargetTransformInfo.h"
175a5ac124SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
185a5ac124SDimitry Andric #include "llvm/CodeGen/BasicTTIImpl.h"
19044eb2f6SDimitry Andric #include "llvm/CodeGen/CostTable.h"
20044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
21ac9a064cSDimitry Andric #include "llvm/IR/DerivedTypes.h"
225a5ac124SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
23ac9a064cSDimitry Andric #include "llvm/IR/Intrinsics.h"
245a5ac124SDimitry Andric #include "llvm/Support/Debug.h"
25ac9a064cSDimitry Andric #include "llvm/Support/MathExtras.h"
26ac9a064cSDimitry Andric 
275a5ac124SDimitry Andric using namespace llvm;
285a5ac124SDimitry Andric 
295a5ac124SDimitry Andric #define DEBUG_TYPE "systemztti"
305a5ac124SDimitry Andric 
315a5ac124SDimitry Andric //===----------------------------------------------------------------------===//
325a5ac124SDimitry Andric //
335a5ac124SDimitry Andric // SystemZ cost model.
345a5ac124SDimitry Andric //
355a5ac124SDimitry Andric //===----------------------------------------------------------------------===//
365a5ac124SDimitry Andric 
isUsedAsMemCpySource(const Value * V,bool & OtherUse)37145449b1SDimitry Andric static bool isUsedAsMemCpySource(const Value *V, bool &OtherUse) {
38145449b1SDimitry Andric   bool UsedAsMemCpySource = false;
39145449b1SDimitry Andric   for (const User *U : V->users())
40145449b1SDimitry Andric     if (const Instruction *User = dyn_cast<Instruction>(U)) {
41145449b1SDimitry Andric       if (isa<BitCastInst>(User) || isa<GetElementPtrInst>(User)) {
42145449b1SDimitry Andric         UsedAsMemCpySource |= isUsedAsMemCpySource(User, OtherUse);
43145449b1SDimitry Andric         continue;
44145449b1SDimitry Andric       }
45145449b1SDimitry Andric       if (const MemCpyInst *Memcpy = dyn_cast<MemCpyInst>(User)) {
46145449b1SDimitry Andric         if (Memcpy->getOperand(1) == V && !Memcpy->isVolatile()) {
47145449b1SDimitry Andric           UsedAsMemCpySource = true;
48145449b1SDimitry Andric           continue;
49145449b1SDimitry Andric         }
50145449b1SDimitry Andric       }
51145449b1SDimitry Andric       OtherUse = true;
52145449b1SDimitry Andric     }
53145449b1SDimitry Andric   return UsedAsMemCpySource;
54145449b1SDimitry Andric }
55145449b1SDimitry Andric 
adjustInliningThreshold(const CallBase * CB) const56145449b1SDimitry Andric unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const {
57145449b1SDimitry Andric   unsigned Bonus = 0;
58145449b1SDimitry Andric 
59145449b1SDimitry Andric   // Increase the threshold if an incoming argument is used only as a memcpy
60145449b1SDimitry Andric   // source.
61145449b1SDimitry Andric   if (Function *Callee = CB->getCalledFunction())
62145449b1SDimitry Andric     for (Argument &Arg : Callee->args()) {
63145449b1SDimitry Andric       bool OtherUse = false;
64145449b1SDimitry Andric       if (isUsedAsMemCpySource(&Arg, OtherUse) && !OtherUse)
65145449b1SDimitry Andric         Bonus += 150;
66145449b1SDimitry Andric     }
67145449b1SDimitry Andric 
68145449b1SDimitry Andric   LLVM_DEBUG(if (Bonus)
69145449b1SDimitry Andric                dbgs() << "++ SZTTI Adding inlining bonus: " << Bonus << "\n";);
70145449b1SDimitry Andric   return Bonus;
71145449b1SDimitry Andric }
72145449b1SDimitry Andric 
getIntImmCost(const APInt & Imm,Type * Ty,TTI::TargetCostKind CostKind)73344a3780SDimitry Andric InstructionCost SystemZTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty,
74cfca06d7SDimitry Andric                                               TTI::TargetCostKind CostKind) {
755a5ac124SDimitry Andric   assert(Ty->isIntegerTy());
765a5ac124SDimitry Andric 
775a5ac124SDimitry Andric   unsigned BitSize = Ty->getPrimitiveSizeInBits();
785a5ac124SDimitry Andric   // There is no cost model for constants with a bit size of 0. Return TCC_Free
795a5ac124SDimitry Andric   // here, so that constant hoisting will ignore this constant.
805a5ac124SDimitry Andric   if (BitSize == 0)
815a5ac124SDimitry Andric     return TTI::TCC_Free;
824df029ccSDimitry Andric   // No cost model for operations on integers larger than 128 bit implemented yet.
834df029ccSDimitry Andric   if ((!ST->hasVector() && BitSize > 64) || BitSize > 128)
845a5ac124SDimitry Andric     return TTI::TCC_Free;
855a5ac124SDimitry Andric 
865a5ac124SDimitry Andric   if (Imm == 0)
875a5ac124SDimitry Andric     return TTI::TCC_Free;
885a5ac124SDimitry Andric 
895a5ac124SDimitry Andric   if (Imm.getBitWidth() <= 64) {
905a5ac124SDimitry Andric     // Constants loaded via lgfi.
915a5ac124SDimitry Andric     if (isInt<32>(Imm.getSExtValue()))
925a5ac124SDimitry Andric       return TTI::TCC_Basic;
935a5ac124SDimitry Andric     // Constants loaded via llilf.
945a5ac124SDimitry Andric     if (isUInt<32>(Imm.getZExtValue()))
955a5ac124SDimitry Andric       return TTI::TCC_Basic;
965a5ac124SDimitry Andric     // Constants loaded via llihf:
975a5ac124SDimitry Andric     if ((Imm.getZExtValue() & 0xffffffff) == 0)
985a5ac124SDimitry Andric       return TTI::TCC_Basic;
995a5ac124SDimitry Andric 
1005a5ac124SDimitry Andric     return 2 * TTI::TCC_Basic;
1015a5ac124SDimitry Andric   }
1025a5ac124SDimitry Andric 
1034df029ccSDimitry Andric   // i128 immediates loads from Constant Pool
1044df029ccSDimitry Andric   return 2 * TTI::TCC_Basic;
1055a5ac124SDimitry Andric }
1065a5ac124SDimitry Andric 
getIntImmCostInst(unsigned Opcode,unsigned Idx,const APInt & Imm,Type * Ty,TTI::TargetCostKind CostKind,Instruction * Inst)107344a3780SDimitry Andric InstructionCost SystemZTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
108cfca06d7SDimitry Andric                                                   const APInt &Imm, Type *Ty,
109b60736ecSDimitry Andric                                                   TTI::TargetCostKind CostKind,
110b60736ecSDimitry Andric                                                   Instruction *Inst) {
1115a5ac124SDimitry Andric   assert(Ty->isIntegerTy());
1125a5ac124SDimitry Andric 
1135a5ac124SDimitry Andric   unsigned BitSize = Ty->getPrimitiveSizeInBits();
1145a5ac124SDimitry Andric   // There is no cost model for constants with a bit size of 0. Return TCC_Free
1155a5ac124SDimitry Andric   // here, so that constant hoisting will ignore this constant.
1165a5ac124SDimitry Andric   if (BitSize == 0)
1175a5ac124SDimitry Andric     return TTI::TCC_Free;
1185a5ac124SDimitry Andric   // No cost model for operations on integers larger than 64 bit implemented yet.
1195a5ac124SDimitry Andric   if (BitSize > 64)
1205a5ac124SDimitry Andric     return TTI::TCC_Free;
1215a5ac124SDimitry Andric 
1225a5ac124SDimitry Andric   switch (Opcode) {
1235a5ac124SDimitry Andric   default:
1245a5ac124SDimitry Andric     return TTI::TCC_Free;
1255a5ac124SDimitry Andric   case Instruction::GetElementPtr:
1265a5ac124SDimitry Andric     // Always hoist the base address of a GetElementPtr. This prevents the
1275a5ac124SDimitry Andric     // creation of new constants for every base constant that gets constant
1285a5ac124SDimitry Andric     // folded with the offset.
1295a5ac124SDimitry Andric     if (Idx == 0)
1305a5ac124SDimitry Andric       return 2 * TTI::TCC_Basic;
1315a5ac124SDimitry Andric     return TTI::TCC_Free;
1325a5ac124SDimitry Andric   case Instruction::Store:
1335a5ac124SDimitry Andric     if (Idx == 0 && Imm.getBitWidth() <= 64) {
1345a5ac124SDimitry Andric       // Any 8-bit immediate store can by implemented via mvi.
1355a5ac124SDimitry Andric       if (BitSize == 8)
1365a5ac124SDimitry Andric         return TTI::TCC_Free;
1375a5ac124SDimitry Andric       // 16-bit immediate values can be stored via mvhhi/mvhi/mvghi.
1385a5ac124SDimitry Andric       if (isInt<16>(Imm.getSExtValue()))
1395a5ac124SDimitry Andric         return TTI::TCC_Free;
1405a5ac124SDimitry Andric     }
1415a5ac124SDimitry Andric     break;
1425a5ac124SDimitry Andric   case Instruction::ICmp:
1435a5ac124SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
1445a5ac124SDimitry Andric       // Comparisons against signed 32-bit immediates implemented via cgfi.
1455a5ac124SDimitry Andric       if (isInt<32>(Imm.getSExtValue()))
1465a5ac124SDimitry Andric         return TTI::TCC_Free;
1475a5ac124SDimitry Andric       // Comparisons against unsigned 32-bit immediates implemented via clgfi.
1485a5ac124SDimitry Andric       if (isUInt<32>(Imm.getZExtValue()))
1495a5ac124SDimitry Andric         return TTI::TCC_Free;
1505a5ac124SDimitry Andric     }
1515a5ac124SDimitry Andric     break;
1525a5ac124SDimitry Andric   case Instruction::Add:
1535a5ac124SDimitry Andric   case Instruction::Sub:
1545a5ac124SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
1555a5ac124SDimitry Andric       // We use algfi/slgfi to add/subtract 32-bit unsigned immediates.
1565a5ac124SDimitry Andric       if (isUInt<32>(Imm.getZExtValue()))
1575a5ac124SDimitry Andric         return TTI::TCC_Free;
1585a5ac124SDimitry Andric       // Or their negation, by swapping addition vs. subtraction.
1595a5ac124SDimitry Andric       if (isUInt<32>(-Imm.getSExtValue()))
1605a5ac124SDimitry Andric         return TTI::TCC_Free;
1615a5ac124SDimitry Andric     }
1625a5ac124SDimitry Andric     break;
1635a5ac124SDimitry Andric   case Instruction::Mul:
1645a5ac124SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
1655a5ac124SDimitry Andric       // We use msgfi to multiply by 32-bit signed immediates.
1665a5ac124SDimitry Andric       if (isInt<32>(Imm.getSExtValue()))
1675a5ac124SDimitry Andric         return TTI::TCC_Free;
1685a5ac124SDimitry Andric     }
1695a5ac124SDimitry Andric     break;
1705a5ac124SDimitry Andric   case Instruction::Or:
1715a5ac124SDimitry Andric   case Instruction::Xor:
1725a5ac124SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
1735a5ac124SDimitry Andric       // Masks supported by oilf/xilf.
1745a5ac124SDimitry Andric       if (isUInt<32>(Imm.getZExtValue()))
1755a5ac124SDimitry Andric         return TTI::TCC_Free;
1765a5ac124SDimitry Andric       // Masks supported by oihf/xihf.
1775a5ac124SDimitry Andric       if ((Imm.getZExtValue() & 0xffffffff) == 0)
1785a5ac124SDimitry Andric         return TTI::TCC_Free;
1795a5ac124SDimitry Andric     }
1805a5ac124SDimitry Andric     break;
1815a5ac124SDimitry Andric   case Instruction::And:
1825a5ac124SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
1835a5ac124SDimitry Andric       // Any 32-bit AND operation can by implemented via nilf.
1845a5ac124SDimitry Andric       if (BitSize <= 32)
1855a5ac124SDimitry Andric         return TTI::TCC_Free;
1865a5ac124SDimitry Andric       // 64-bit masks supported by nilf.
1875a5ac124SDimitry Andric       if (isUInt<32>(~Imm.getZExtValue()))
1885a5ac124SDimitry Andric         return TTI::TCC_Free;
1895a5ac124SDimitry Andric       // 64-bit masks supported by nilh.
1905a5ac124SDimitry Andric       if ((Imm.getZExtValue() & 0xffffffff) == 0xffffffff)
1915a5ac124SDimitry Andric         return TTI::TCC_Free;
1925a5ac124SDimitry Andric       // Some 64-bit AND operations can be implemented via risbg.
1935a5ac124SDimitry Andric       const SystemZInstrInfo *TII = ST->getInstrInfo();
1945a5ac124SDimitry Andric       unsigned Start, End;
1955a5ac124SDimitry Andric       if (TII->isRxSBGMask(Imm.getZExtValue(), BitSize, Start, End))
1965a5ac124SDimitry Andric         return TTI::TCC_Free;
1975a5ac124SDimitry Andric     }
1985a5ac124SDimitry Andric     break;
1995a5ac124SDimitry Andric   case Instruction::Shl:
2005a5ac124SDimitry Andric   case Instruction::LShr:
2015a5ac124SDimitry Andric   case Instruction::AShr:
2025a5ac124SDimitry Andric     // Always return TCC_Free for the shift value of a shift instruction.
2035a5ac124SDimitry Andric     if (Idx == 1)
2045a5ac124SDimitry Andric       return TTI::TCC_Free;
2055a5ac124SDimitry Andric     break;
2065a5ac124SDimitry Andric   case Instruction::UDiv:
2075a5ac124SDimitry Andric   case Instruction::SDiv:
2085a5ac124SDimitry Andric   case Instruction::URem:
2095a5ac124SDimitry Andric   case Instruction::SRem:
2105a5ac124SDimitry Andric   case Instruction::Trunc:
2115a5ac124SDimitry Andric   case Instruction::ZExt:
2125a5ac124SDimitry Andric   case Instruction::SExt:
2135a5ac124SDimitry Andric   case Instruction::IntToPtr:
2145a5ac124SDimitry Andric   case Instruction::PtrToInt:
2155a5ac124SDimitry Andric   case Instruction::BitCast:
2165a5ac124SDimitry Andric   case Instruction::PHI:
2175a5ac124SDimitry Andric   case Instruction::Call:
2185a5ac124SDimitry Andric   case Instruction::Select:
2195a5ac124SDimitry Andric   case Instruction::Ret:
2205a5ac124SDimitry Andric   case Instruction::Load:
2215a5ac124SDimitry Andric     break;
2225a5ac124SDimitry Andric   }
2235a5ac124SDimitry Andric 
224cfca06d7SDimitry Andric   return SystemZTTIImpl::getIntImmCost(Imm, Ty, CostKind);
2255a5ac124SDimitry Andric }
2265a5ac124SDimitry Andric 
227344a3780SDimitry Andric InstructionCost
getIntImmCostIntrin(Intrinsic::ID IID,unsigned Idx,const APInt & Imm,Type * Ty,TTI::TargetCostKind CostKind)228344a3780SDimitry Andric SystemZTTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
229cfca06d7SDimitry Andric                                     const APInt &Imm, Type *Ty,
230cfca06d7SDimitry Andric                                     TTI::TargetCostKind CostKind) {
2315a5ac124SDimitry Andric   assert(Ty->isIntegerTy());
2325a5ac124SDimitry Andric 
2335a5ac124SDimitry Andric   unsigned BitSize = Ty->getPrimitiveSizeInBits();
2345a5ac124SDimitry Andric   // There is no cost model for constants with a bit size of 0. Return TCC_Free
2355a5ac124SDimitry Andric   // here, so that constant hoisting will ignore this constant.
2365a5ac124SDimitry Andric   if (BitSize == 0)
2375a5ac124SDimitry Andric     return TTI::TCC_Free;
2385a5ac124SDimitry Andric   // No cost model for operations on integers larger than 64 bit implemented yet.
2395a5ac124SDimitry Andric   if (BitSize > 64)
2405a5ac124SDimitry Andric     return TTI::TCC_Free;
2415a5ac124SDimitry Andric 
2425a5ac124SDimitry Andric   switch (IID) {
2435a5ac124SDimitry Andric   default:
2445a5ac124SDimitry Andric     return TTI::TCC_Free;
2455a5ac124SDimitry Andric   case Intrinsic::sadd_with_overflow:
2465a5ac124SDimitry Andric   case Intrinsic::uadd_with_overflow:
2475a5ac124SDimitry Andric   case Intrinsic::ssub_with_overflow:
2485a5ac124SDimitry Andric   case Intrinsic::usub_with_overflow:
2495a5ac124SDimitry Andric     // These get expanded to include a normal addition/subtraction.
2505a5ac124SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
2515a5ac124SDimitry Andric       if (isUInt<32>(Imm.getZExtValue()))
2525a5ac124SDimitry Andric         return TTI::TCC_Free;
2535a5ac124SDimitry Andric       if (isUInt<32>(-Imm.getSExtValue()))
2545a5ac124SDimitry Andric         return TTI::TCC_Free;
2555a5ac124SDimitry Andric     }
2565a5ac124SDimitry Andric     break;
2575a5ac124SDimitry Andric   case Intrinsic::smul_with_overflow:
2585a5ac124SDimitry Andric   case Intrinsic::umul_with_overflow:
2595a5ac124SDimitry Andric     // These get expanded to include a normal multiplication.
2605a5ac124SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
2615a5ac124SDimitry Andric       if (isInt<32>(Imm.getSExtValue()))
2625a5ac124SDimitry Andric         return TTI::TCC_Free;
2635a5ac124SDimitry Andric     }
2645a5ac124SDimitry Andric     break;
2655a5ac124SDimitry Andric   case Intrinsic::experimental_stackmap:
2665a5ac124SDimitry Andric     if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
2675a5ac124SDimitry Andric       return TTI::TCC_Free;
2685a5ac124SDimitry Andric     break;
2695a5ac124SDimitry Andric   case Intrinsic::experimental_patchpoint_void:
270ac9a064cSDimitry Andric   case Intrinsic::experimental_patchpoint:
2715a5ac124SDimitry Andric     if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
2725a5ac124SDimitry Andric       return TTI::TCC_Free;
2735a5ac124SDimitry Andric     break;
2745a5ac124SDimitry Andric   }
275cfca06d7SDimitry Andric   return SystemZTTIImpl::getIntImmCost(Imm, Ty, CostKind);
2765a5ac124SDimitry Andric }
2775a5ac124SDimitry Andric 
2785a5ac124SDimitry Andric TargetTransformInfo::PopcntSupportKind
getPopcntSupport(unsigned TyWidth)2795a5ac124SDimitry Andric SystemZTTIImpl::getPopcntSupport(unsigned TyWidth) {
2805a5ac124SDimitry Andric   assert(isPowerOf2_32(TyWidth) && "Type width must be power of 2");
2815a5ac124SDimitry Andric   if (ST->hasPopulationCount() && TyWidth <= 64)
2825a5ac124SDimitry Andric     return TTI::PSK_FastHardware;
2835a5ac124SDimitry Andric   return TTI::PSK_Software;
2845a5ac124SDimitry Andric }
2855a5ac124SDimitry Andric 
getUnrollingPreferences(Loop * L,ScalarEvolution & SE,TTI::UnrollingPreferences & UP,OptimizationRemarkEmitter * ORE)2869df3605dSDimitry Andric void SystemZTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
287c0981da4SDimitry Andric                                              TTI::UnrollingPreferences &UP,
288c0981da4SDimitry Andric                                              OptimizationRemarkEmitter *ORE) {
289b915e9e0SDimitry Andric   // Find out if L contains a call, what the machine instruction count
290b915e9e0SDimitry Andric   // estimate is, and how many stores there are.
291b915e9e0SDimitry Andric   bool HasCall = false;
292344a3780SDimitry Andric   InstructionCost NumStores = 0;
293b915e9e0SDimitry Andric   for (auto &BB : L->blocks())
294b915e9e0SDimitry Andric     for (auto &I : *BB) {
295b915e9e0SDimitry Andric       if (isa<CallInst>(&I) || isa<InvokeInst>(&I)) {
296cfca06d7SDimitry Andric         if (const Function *F = cast<CallBase>(I).getCalledFunction()) {
297b915e9e0SDimitry Andric           if (isLoweredToCall(F))
298b915e9e0SDimitry Andric             HasCall = true;
299b915e9e0SDimitry Andric           if (F->getIntrinsicID() == Intrinsic::memcpy ||
300b915e9e0SDimitry Andric               F->getIntrinsicID() == Intrinsic::memset)
301b915e9e0SDimitry Andric             NumStores++;
302b915e9e0SDimitry Andric         } else { // indirect call.
303b915e9e0SDimitry Andric           HasCall = true;
304b915e9e0SDimitry Andric         }
305b915e9e0SDimitry Andric       }
306b915e9e0SDimitry Andric       if (isa<StoreInst>(&I)) {
307b915e9e0SDimitry Andric         Type *MemAccessTy = I.getOperand(0)->getType();
308e3b55780SDimitry Andric         NumStores += getMemoryOpCost(Instruction::Store, MemAccessTy,
309e3b55780SDimitry Andric                                      std::nullopt, 0, TTI::TCK_RecipThroughput);
310b915e9e0SDimitry Andric       }
311b915e9e0SDimitry Andric     }
312b915e9e0SDimitry Andric 
313b915e9e0SDimitry Andric   // The z13 processor will run out of store tags if too many stores
314b915e9e0SDimitry Andric   // are fed into it too quickly. Therefore make sure there are not
315b915e9e0SDimitry Andric   // too many stores in the resulting unrolled loop.
316344a3780SDimitry Andric   unsigned const NumStoresVal = *NumStores.getValue();
317344a3780SDimitry Andric   unsigned const Max = (NumStoresVal ? (12 / NumStoresVal) : UINT_MAX);
318b915e9e0SDimitry Andric 
319b915e9e0SDimitry Andric   if (HasCall) {
320b915e9e0SDimitry Andric     // Only allow full unrolling if loop has any calls.
321b915e9e0SDimitry Andric     UP.FullUnrollMaxCount = Max;
322b915e9e0SDimitry Andric     UP.MaxCount = 1;
323b915e9e0SDimitry Andric     return;
324b915e9e0SDimitry Andric   }
325b915e9e0SDimitry Andric 
326b915e9e0SDimitry Andric   UP.MaxCount = Max;
327b915e9e0SDimitry Andric   if (UP.MaxCount <= 1)
328b915e9e0SDimitry Andric     return;
329b915e9e0SDimitry Andric 
330b915e9e0SDimitry Andric   // Allow partial and runtime trip count unrolling.
331b915e9e0SDimitry Andric   UP.Partial = UP.Runtime = true;
332b915e9e0SDimitry Andric 
333b915e9e0SDimitry Andric   UP.PartialThreshold = 75;
334b915e9e0SDimitry Andric   UP.DefaultUnrollRuntimeCount = 4;
335b915e9e0SDimitry Andric 
336b915e9e0SDimitry Andric   // Allow expensive instructions in the pre-header of the loop.
337b915e9e0SDimitry Andric   UP.AllowExpensiveTripCount = true;
338b915e9e0SDimitry Andric 
339b915e9e0SDimitry Andric   UP.Force = true;
340b915e9e0SDimitry Andric }
341b915e9e0SDimitry Andric 
getPeelingPreferences(Loop * L,ScalarEvolution & SE,TTI::PeelingPreferences & PP)342cfca06d7SDimitry Andric void SystemZTTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE,
343cfca06d7SDimitry Andric                                            TTI::PeelingPreferences &PP) {
344cfca06d7SDimitry Andric   BaseT::getPeelingPreferences(L, SE, PP);
345cfca06d7SDimitry Andric }
346044eb2f6SDimitry Andric 
isLSRCostLess(const TargetTransformInfo::LSRCost & C1,const TargetTransformInfo::LSRCost & C2)347145449b1SDimitry Andric bool SystemZTTIImpl::isLSRCostLess(const TargetTransformInfo::LSRCost &C1,
348145449b1SDimitry Andric                                    const TargetTransformInfo::LSRCost &C2) {
349044eb2f6SDimitry Andric   // SystemZ specific: check instruction count (first), and don't care about
350044eb2f6SDimitry Andric   // ImmCost, since offsets are checked explicitly.
351044eb2f6SDimitry Andric   return std::tie(C1.Insns, C1.NumRegs, C1.AddRecCost,
352044eb2f6SDimitry Andric                   C1.NumIVMuls, C1.NumBaseAdds,
353044eb2f6SDimitry Andric                   C1.ScaleCost, C1.SetupCost) <
354044eb2f6SDimitry Andric     std::tie(C2.Insns, C2.NumRegs, C2.AddRecCost,
355044eb2f6SDimitry Andric              C2.NumIVMuls, C2.NumBaseAdds,
356044eb2f6SDimitry Andric              C2.ScaleCost, C2.SetupCost);
357044eb2f6SDimitry Andric }
358044eb2f6SDimitry Andric 
getNumberOfRegisters(unsigned ClassID) const3591d5ae102SDimitry Andric unsigned SystemZTTIImpl::getNumberOfRegisters(unsigned ClassID) const {
3601d5ae102SDimitry Andric   bool Vector = (ClassID == 1);
3615a5ac124SDimitry Andric   if (!Vector)
3625a5ac124SDimitry Andric     // Discount the stack pointer.  Also leave out %r0, since it can't
3635a5ac124SDimitry Andric     // be used in an address.
3645a5ac124SDimitry Andric     return 14;
3655a5ac124SDimitry Andric   if (ST->hasVector())
3665a5ac124SDimitry Andric     return 32;
3675a5ac124SDimitry Andric   return 0;
3685a5ac124SDimitry Andric }
3695a5ac124SDimitry Andric 
370344a3780SDimitry Andric TypeSize
getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const371344a3780SDimitry Andric SystemZTTIImpl::getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const {
372344a3780SDimitry Andric   switch (K) {
373344a3780SDimitry Andric   case TargetTransformInfo::RGK_Scalar:
374344a3780SDimitry Andric     return TypeSize::getFixed(64);
375344a3780SDimitry Andric   case TargetTransformInfo::RGK_FixedWidthVector:
376344a3780SDimitry Andric     return TypeSize::getFixed(ST->hasVector() ? 128 : 0);
377344a3780SDimitry Andric   case TargetTransformInfo::RGK_ScalableVector:
378344a3780SDimitry Andric     return TypeSize::getScalable(0);
379344a3780SDimitry Andric   }
380344a3780SDimitry Andric 
381344a3780SDimitry Andric   llvm_unreachable("Unsupported register kind");
3825a5ac124SDimitry Andric }
3835a5ac124SDimitry Andric 
getMinPrefetchStride(unsigned NumMemAccesses,unsigned NumStridedMemAccesses,unsigned NumPrefetches,bool HasCall) const384cfca06d7SDimitry Andric unsigned SystemZTTIImpl::getMinPrefetchStride(unsigned NumMemAccesses,
385cfca06d7SDimitry Andric                                               unsigned NumStridedMemAccesses,
386cfca06d7SDimitry Andric                                               unsigned NumPrefetches,
387cfca06d7SDimitry Andric                                               bool HasCall) const {
388cfca06d7SDimitry Andric   // Don't prefetch a loop with many far apart accesses.
389cfca06d7SDimitry Andric   if (NumPrefetches > 16)
390cfca06d7SDimitry Andric     return UINT_MAX;
391cfca06d7SDimitry Andric 
392cfca06d7SDimitry Andric   // Emit prefetch instructions for smaller strides in cases where we think
393cfca06d7SDimitry Andric   // the hardware prefetcher might not be able to keep up.
394b60736ecSDimitry Andric   if (NumStridedMemAccesses > 32 && !HasCall &&
395b60736ecSDimitry Andric       (NumMemAccesses - NumStridedMemAccesses) * 32 <= NumStridedMemAccesses)
396cfca06d7SDimitry Andric     return 1;
397cfca06d7SDimitry Andric 
398cfca06d7SDimitry Andric   return ST->hasMiscellaneousExtensions3() ? 8192 : 2048;
399cfca06d7SDimitry Andric }
400cfca06d7SDimitry Andric 
hasDivRemOp(Type * DataType,bool IsSigned)401044eb2f6SDimitry Andric bool SystemZTTIImpl::hasDivRemOp(Type *DataType, bool IsSigned) {
402044eb2f6SDimitry Andric   EVT VT = TLI->getValueType(DL, DataType);
403044eb2f6SDimitry Andric   return (VT.isScalarInteger() && TLI->isTypeLegal(VT));
404044eb2f6SDimitry Andric }
405044eb2f6SDimitry Andric 
406d8e91e46SDimitry Andric // Return the bit size for the scalar type or vector element
407d8e91e46SDimitry Andric // type. getScalarSizeInBits() returns 0 for a pointer type.
getScalarSizeInBits(Type * Ty)408d8e91e46SDimitry Andric static unsigned getScalarSizeInBits(Type *Ty) {
409d8e91e46SDimitry Andric   unsigned Size =
410d8e91e46SDimitry Andric     (Ty->isPtrOrPtrVectorTy() ? 64U : Ty->getScalarSizeInBits());
411d8e91e46SDimitry Andric   assert(Size > 0 && "Element must have non-zero size.");
412d8e91e46SDimitry Andric   return Size;
413d8e91e46SDimitry Andric }
414d8e91e46SDimitry Andric 
415d8e91e46SDimitry Andric // getNumberOfParts() calls getTypeLegalizationCost() which splits the vector
416d8e91e46SDimitry Andric // type until it is legal. This would e.g. return 4 for <6 x i64>, instead of
417d8e91e46SDimitry Andric // 3.
getNumVectorRegs(Type * Ty)418d8e91e46SDimitry Andric static unsigned getNumVectorRegs(Type *Ty) {
419cfca06d7SDimitry Andric   auto *VTy = cast<FixedVectorType>(Ty);
420cfca06d7SDimitry Andric   unsigned WideBits = getScalarSizeInBits(Ty) * VTy->getNumElements();
421d8e91e46SDimitry Andric   assert(WideBits > 0 && "Could not compute size of vector");
422d8e91e46SDimitry Andric   return ((WideBits % 128U) ? ((WideBits / 128U) + 1) : (WideBits / 128U));
423d8e91e46SDimitry Andric }
424d8e91e46SDimitry Andric 
getArithmeticInstrCost(unsigned Opcode,Type * Ty,TTI::TargetCostKind CostKind,TTI::OperandValueInfo Op1Info,TTI::OperandValueInfo Op2Info,ArrayRef<const Value * > Args,const Instruction * CxtI)425344a3780SDimitry Andric InstructionCost SystemZTTIImpl::getArithmeticInstrCost(
426cfca06d7SDimitry Andric     unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
427e3b55780SDimitry Andric     TTI::OperandValueInfo Op1Info, TTI::OperandValueInfo Op2Info,
428e3b55780SDimitry Andric     ArrayRef<const Value *> Args,
429706b4fc4SDimitry Andric     const Instruction *CxtI) {
43071d5a254SDimitry Andric 
431cfca06d7SDimitry Andric   // TODO: Handle more cost kinds.
432cfca06d7SDimitry Andric   if (CostKind != TTI::TCK_RecipThroughput)
433cfca06d7SDimitry Andric     return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info,
434e3b55780SDimitry Andric                                          Op2Info, Args, CxtI);
435cfca06d7SDimitry Andric 
43671d5a254SDimitry Andric   // TODO: return a good value for BB-VECTORIZER that includes the
43771d5a254SDimitry Andric   // immediate loads, which we do not want to count for the loop
43871d5a254SDimitry Andric   // vectorizer, since they are hopefully hoisted out of the loop. This
43971d5a254SDimitry Andric   // would require a new parameter 'InLoop', but not sure if constant
44071d5a254SDimitry Andric   // args are common enough to motivate this.
44171d5a254SDimitry Andric 
44271d5a254SDimitry Andric   unsigned ScalarBits = Ty->getScalarSizeInBits();
44371d5a254SDimitry Andric 
444d8e91e46SDimitry Andric   // There are thre cases of division and remainder: Dividing with a register
445d8e91e46SDimitry Andric   // needs a divide instruction. A divisor which is a power of two constant
446d8e91e46SDimitry Andric   // can be implemented with a sequence of shifts. Any other constant needs a
447d8e91e46SDimitry Andric   // multiply and shifts.
448d8e91e46SDimitry Andric   const unsigned DivInstrCost = 20;
449d8e91e46SDimitry Andric   const unsigned DivMulSeqCost = 10;
450d8e91e46SDimitry Andric   const unsigned SDivPow2Cost = 4;
451d8e91e46SDimitry Andric 
452d8e91e46SDimitry Andric   bool SignedDivRem =
453d8e91e46SDimitry Andric       Opcode == Instruction::SDiv || Opcode == Instruction::SRem;
454d8e91e46SDimitry Andric   bool UnsignedDivRem =
455d8e91e46SDimitry Andric       Opcode == Instruction::UDiv || Opcode == Instruction::URem;
456d8e91e46SDimitry Andric 
457d8e91e46SDimitry Andric   // Check for a constant divisor.
458d8e91e46SDimitry Andric   bool DivRemConst = false;
459d8e91e46SDimitry Andric   bool DivRemConstPow2 = false;
460d8e91e46SDimitry Andric   if ((SignedDivRem || UnsignedDivRem) && Args.size() == 2) {
4617af96fb3SDimitry Andric     if (const Constant *C = dyn_cast<Constant>(Args[1])) {
462d8e91e46SDimitry Andric       const ConstantInt *CVal =
463d8e91e46SDimitry Andric           (C->getType()->isVectorTy()
464d8e91e46SDimitry Andric                ? dyn_cast_or_null<const ConstantInt>(C->getSplatValue())
465d8e91e46SDimitry Andric                : dyn_cast<const ConstantInt>(C));
466c0981da4SDimitry Andric       if (CVal && (CVal->getValue().isPowerOf2() ||
467c0981da4SDimitry Andric                    CVal->getValue().isNegatedPowerOf2()))
468d8e91e46SDimitry Andric         DivRemConstPow2 = true;
4697af96fb3SDimitry Andric       else
470d8e91e46SDimitry Andric         DivRemConst = true;
4717af96fb3SDimitry Andric     }
4727af96fb3SDimitry Andric   }
4737af96fb3SDimitry Andric 
474cfca06d7SDimitry Andric   if (!Ty->isVectorTy()) {
47571d5a254SDimitry Andric     // These FP operations are supported with a dedicated instruction for
47671d5a254SDimitry Andric     // float, double and fp128 (base implementation assumes float generally
47771d5a254SDimitry Andric     // costs 2).
47871d5a254SDimitry Andric     if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub ||
47971d5a254SDimitry Andric         Opcode == Instruction::FMul || Opcode == Instruction::FDiv)
48071d5a254SDimitry Andric       return 1;
48171d5a254SDimitry Andric 
48271d5a254SDimitry Andric     // There is no native support for FRem.
48371d5a254SDimitry Andric     if (Opcode == Instruction::FRem)
48471d5a254SDimitry Andric       return LIBCALL_COST;
48571d5a254SDimitry Andric 
486e6d15924SDimitry Andric     // Give discount for some combined logical operations if supported.
4874df029ccSDimitry Andric     if (Args.size() == 2) {
488e6d15924SDimitry Andric       if (Opcode == Instruction::Xor) {
489e6d15924SDimitry Andric         for (const Value *A : Args) {
490e6d15924SDimitry Andric           if (const Instruction *I = dyn_cast<Instruction>(A))
491e6d15924SDimitry Andric             if (I->hasOneUse() &&
4924df029ccSDimitry Andric                 (I->getOpcode() == Instruction::Or ||
4934df029ccSDimitry Andric                  I->getOpcode() == Instruction::And ||
494e6d15924SDimitry Andric                  I->getOpcode() == Instruction::Xor))
4954df029ccSDimitry Andric               if ((ScalarBits <= 64 && ST->hasMiscellaneousExtensions3()) ||
4964df029ccSDimitry Andric                   (isInt128InVR(Ty) &&
4974df029ccSDimitry Andric                    (I->getOpcode() == Instruction::Or || ST->hasVectorEnhancements1())))
498e6d15924SDimitry Andric                 return 0;
499e6d15924SDimitry Andric         }
500e6d15924SDimitry Andric       }
5014df029ccSDimitry Andric       else if (Opcode == Instruction::And || Opcode == Instruction::Or) {
502e6d15924SDimitry Andric         for (const Value *A : Args) {
503e6d15924SDimitry Andric           if (const Instruction *I = dyn_cast<Instruction>(A))
5044df029ccSDimitry Andric             if ((I->hasOneUse() && I->getOpcode() == Instruction::Xor) &&
5054df029ccSDimitry Andric                 ((ScalarBits <= 64 && ST->hasMiscellaneousExtensions3()) ||
5064df029ccSDimitry Andric                  (isInt128InVR(Ty) &&
5074df029ccSDimitry Andric                   (Opcode == Instruction::And || ST->hasVectorEnhancements1()))))
508e6d15924SDimitry Andric               return 0;
509e6d15924SDimitry Andric         }
510e6d15924SDimitry Andric       }
511e6d15924SDimitry Andric     }
512e6d15924SDimitry Andric 
51371d5a254SDimitry Andric     // Or requires one instruction, although it has custom handling for i64.
51471d5a254SDimitry Andric     if (Opcode == Instruction::Or)
51571d5a254SDimitry Andric       return 1;
51671d5a254SDimitry Andric 
517d8e91e46SDimitry Andric     if (Opcode == Instruction::Xor && ScalarBits == 1) {
518d8e91e46SDimitry Andric       if (ST->hasLoadStoreOnCond2())
519d8e91e46SDimitry Andric         return 5; // 2 * (li 0; loc 1); xor
520d8e91e46SDimitry Andric       return 7; // 2 * ipm sequences ; xor ; shift ; compare
521d8e91e46SDimitry Andric     }
52271d5a254SDimitry Andric 
523d8e91e46SDimitry Andric     if (DivRemConstPow2)
524d8e91e46SDimitry Andric       return (SignedDivRem ? SDivPow2Cost : 1);
525d8e91e46SDimitry Andric     if (DivRemConst)
526d8e91e46SDimitry Andric       return DivMulSeqCost;
527d8e91e46SDimitry Andric     if (SignedDivRem || UnsignedDivRem)
528d8e91e46SDimitry Andric       return DivInstrCost;
52971d5a254SDimitry Andric   }
530cfca06d7SDimitry Andric   else if (ST->hasVector()) {
531cfca06d7SDimitry Andric     auto *VTy = cast<FixedVectorType>(Ty);
532cfca06d7SDimitry Andric     unsigned VF = VTy->getNumElements();
533cfca06d7SDimitry Andric     unsigned NumVectors = getNumVectorRegs(Ty);
534cfca06d7SDimitry Andric 
535cfca06d7SDimitry Andric     // These vector operations are custom handled, but are still supported
536cfca06d7SDimitry Andric     // with one instruction per vector, regardless of element size.
537cfca06d7SDimitry Andric     if (Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
538cfca06d7SDimitry Andric         Opcode == Instruction::AShr) {
539cfca06d7SDimitry Andric       return NumVectors;
540cfca06d7SDimitry Andric     }
541cfca06d7SDimitry Andric 
542cfca06d7SDimitry Andric     if (DivRemConstPow2)
543cfca06d7SDimitry Andric       return (NumVectors * (SignedDivRem ? SDivPow2Cost : 1));
544344a3780SDimitry Andric     if (DivRemConst) {
545344a3780SDimitry Andric       SmallVector<Type *> Tys(Args.size(), Ty);
546e3b55780SDimitry Andric       return VF * DivMulSeqCost +
547e3b55780SDimitry Andric              getScalarizationOverhead(VTy, Args, Tys, CostKind);
548344a3780SDimitry Andric     }
549cfca06d7SDimitry Andric     if ((SignedDivRem || UnsignedDivRem) && VF > 4)
550cfca06d7SDimitry Andric       // Temporary hack: disable high vectorization factors with integer
551cfca06d7SDimitry Andric       // division/remainder, which will get scalarized and handled with
552cfca06d7SDimitry Andric       // GR128 registers. The mischeduler is not clever enough to avoid
553cfca06d7SDimitry Andric       // spilling yet.
554cfca06d7SDimitry Andric       return 1000;
555cfca06d7SDimitry Andric 
556cfca06d7SDimitry Andric     // These FP operations are supported with a single vector instruction for
557cfca06d7SDimitry Andric     // double (base implementation assumes float generally costs 2). For
558cfca06d7SDimitry Andric     // FP128, the scalar cost is 1, and there is no overhead since the values
559cfca06d7SDimitry Andric     // are already in scalar registers.
560cfca06d7SDimitry Andric     if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub ||
561cfca06d7SDimitry Andric         Opcode == Instruction::FMul || Opcode == Instruction::FDiv) {
562cfca06d7SDimitry Andric       switch (ScalarBits) {
563cfca06d7SDimitry Andric       case 32: {
564cfca06d7SDimitry Andric         // The vector enhancements facility 1 provides v4f32 instructions.
565cfca06d7SDimitry Andric         if (ST->hasVectorEnhancements1())
566cfca06d7SDimitry Andric           return NumVectors;
567cfca06d7SDimitry Andric         // Return the cost of multiple scalar invocation plus the cost of
568cfca06d7SDimitry Andric         // inserting and extracting the values.
569344a3780SDimitry Andric         InstructionCost ScalarCost =
570cfca06d7SDimitry Andric             getArithmeticInstrCost(Opcode, Ty->getScalarType(), CostKind);
571344a3780SDimitry Andric         SmallVector<Type *> Tys(Args.size(), Ty);
572344a3780SDimitry Andric         InstructionCost Cost =
573e3b55780SDimitry Andric             (VF * ScalarCost) +
574e3b55780SDimitry Andric             getScalarizationOverhead(VTy, Args, Tys, CostKind);
575cfca06d7SDimitry Andric         // FIXME: VF 2 for these FP operations are currently just as
576cfca06d7SDimitry Andric         // expensive as for VF 4.
577cfca06d7SDimitry Andric         if (VF == 2)
578cfca06d7SDimitry Andric           Cost *= 2;
579cfca06d7SDimitry Andric         return Cost;
580cfca06d7SDimitry Andric       }
581cfca06d7SDimitry Andric       case 64:
582cfca06d7SDimitry Andric       case 128:
583cfca06d7SDimitry Andric         return NumVectors;
584cfca06d7SDimitry Andric       default:
585cfca06d7SDimitry Andric         break;
586cfca06d7SDimitry Andric       }
587cfca06d7SDimitry Andric     }
588cfca06d7SDimitry Andric 
589cfca06d7SDimitry Andric     // There is no native support for FRem.
590cfca06d7SDimitry Andric     if (Opcode == Instruction::FRem) {
591344a3780SDimitry Andric       SmallVector<Type *> Tys(Args.size(), Ty);
592e3b55780SDimitry Andric       InstructionCost Cost = (VF * LIBCALL_COST) +
593e3b55780SDimitry Andric                              getScalarizationOverhead(VTy, Args, Tys, CostKind);
594cfca06d7SDimitry Andric       // FIXME: VF 2 for float is currently just as expensive as for VF 4.
595cfca06d7SDimitry Andric       if (VF == 2 && ScalarBits == 32)
596cfca06d7SDimitry Andric         Cost *= 2;
597cfca06d7SDimitry Andric       return Cost;
598cfca06d7SDimitry Andric     }
599cfca06d7SDimitry Andric   }
60071d5a254SDimitry Andric 
60171d5a254SDimitry Andric   // Fallback to the default implementation.
602cfca06d7SDimitry Andric   return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info, Op2Info,
603e3b55780SDimitry Andric                                        Args, CxtI);
60471d5a254SDimitry Andric }
60571d5a254SDimitry Andric 
getShuffleCost(TTI::ShuffleKind Kind,VectorType * Tp,ArrayRef<int> Mask,TTI::TargetCostKind CostKind,int Index,VectorType * SubTp,ArrayRef<const Value * > Args,const Instruction * CxtI)606ac9a064cSDimitry Andric InstructionCost SystemZTTIImpl::getShuffleCost(
607ac9a064cSDimitry Andric     TTI::ShuffleKind Kind, VectorType *Tp, ArrayRef<int> Mask,
608ac9a064cSDimitry Andric     TTI::TargetCostKind CostKind, int Index, VectorType *SubTp,
609ac9a064cSDimitry Andric     ArrayRef<const Value *> Args, const Instruction *CxtI) {
610b1c73532SDimitry Andric   Kind = improveShuffleKindFromMask(Kind, Mask, Tp, Index, SubTp);
611cfca06d7SDimitry Andric   if (ST->hasVector()) {
612d8e91e46SDimitry Andric     unsigned NumVectors = getNumVectorRegs(Tp);
61371d5a254SDimitry Andric 
61471d5a254SDimitry Andric     // TODO: Since fp32 is expanded, the shuffle cost should always be 0.
61571d5a254SDimitry Andric 
61671d5a254SDimitry Andric     // FP128 values are always in scalar registers, so there is no work
61771d5a254SDimitry Andric     // involved with a shuffle, except for broadcast. In that case register
61871d5a254SDimitry Andric     // moves are done with a single instruction per element.
61971d5a254SDimitry Andric     if (Tp->getScalarType()->isFP128Ty())
62071d5a254SDimitry Andric       return (Kind == TargetTransformInfo::SK_Broadcast ? NumVectors - 1 : 0);
62171d5a254SDimitry Andric 
62271d5a254SDimitry Andric     switch (Kind) {
62371d5a254SDimitry Andric     case  TargetTransformInfo::SK_ExtractSubvector:
62471d5a254SDimitry Andric       // ExtractSubvector Index indicates start offset.
62571d5a254SDimitry Andric 
62671d5a254SDimitry Andric       // Extracting a subvector from first index is a noop.
62771d5a254SDimitry Andric       return (Index == 0 ? 0 : NumVectors);
62871d5a254SDimitry Andric 
62971d5a254SDimitry Andric     case TargetTransformInfo::SK_Broadcast:
63071d5a254SDimitry Andric       // Loop vectorizer calls here to figure out the extra cost of
63171d5a254SDimitry Andric       // broadcasting a loaded value to all elements of a vector. Since vlrep
63271d5a254SDimitry Andric       // loads and replicates with a single instruction, adjust the returned
63371d5a254SDimitry Andric       // value.
63471d5a254SDimitry Andric       return NumVectors - 1;
63571d5a254SDimitry Andric 
63671d5a254SDimitry Andric     default:
63771d5a254SDimitry Andric 
63871d5a254SDimitry Andric       // SystemZ supports single instruction permutation / replication.
63971d5a254SDimitry Andric       return NumVectors;
64071d5a254SDimitry Andric     }
641cfca06d7SDimitry Andric   }
64271d5a254SDimitry Andric 
643e3b55780SDimitry Andric   return BaseT::getShuffleCost(Kind, Tp, Mask, CostKind, Index, SubTp);
64471d5a254SDimitry Andric }
64571d5a254SDimitry Andric 
64671d5a254SDimitry Andric // Return the log2 difference of the element sizes of the two vector types.
getElSizeLog2Diff(Type * Ty0,Type * Ty1)64771d5a254SDimitry Andric static unsigned getElSizeLog2Diff(Type *Ty0, Type *Ty1) {
64871d5a254SDimitry Andric   unsigned Bits0 = Ty0->getScalarSizeInBits();
64971d5a254SDimitry Andric   unsigned Bits1 = Ty1->getScalarSizeInBits();
65071d5a254SDimitry Andric 
65171d5a254SDimitry Andric   if (Bits1 >  Bits0)
65271d5a254SDimitry Andric     return (Log2_32(Bits1) - Log2_32(Bits0));
65371d5a254SDimitry Andric 
65471d5a254SDimitry Andric   return (Log2_32(Bits0) - Log2_32(Bits1));
65571d5a254SDimitry Andric }
65671d5a254SDimitry Andric 
65771d5a254SDimitry Andric // Return the number of instructions needed to truncate SrcTy to DstTy.
65871d5a254SDimitry Andric unsigned SystemZTTIImpl::
getVectorTruncCost(Type * SrcTy,Type * DstTy)65971d5a254SDimitry Andric getVectorTruncCost(Type *SrcTy, Type *DstTy) {
66071d5a254SDimitry Andric   assert (SrcTy->isVectorTy() && DstTy->isVectorTy());
661e3b55780SDimitry Andric   assert(SrcTy->getPrimitiveSizeInBits().getFixedValue() >
662e3b55780SDimitry Andric              DstTy->getPrimitiveSizeInBits().getFixedValue() &&
66371d5a254SDimitry Andric          "Packing must reduce size of vector type.");
664cfca06d7SDimitry Andric   assert(cast<FixedVectorType>(SrcTy)->getNumElements() ==
665cfca06d7SDimitry Andric              cast<FixedVectorType>(DstTy)->getNumElements() &&
66671d5a254SDimitry Andric          "Packing should not change number of elements.");
66771d5a254SDimitry Andric 
66871d5a254SDimitry Andric   // TODO: Since fp32 is expanded, the extract cost should always be 0.
66971d5a254SDimitry Andric 
670d8e91e46SDimitry Andric   unsigned NumParts = getNumVectorRegs(SrcTy);
67171d5a254SDimitry Andric   if (NumParts <= 2)
67271d5a254SDimitry Andric     // Up to 2 vector registers can be truncated efficiently with pack or
67371d5a254SDimitry Andric     // permute. The latter requires an immediate mask to be loaded, which
67471d5a254SDimitry Andric     // typically gets hoisted out of a loop.  TODO: return a good value for
67571d5a254SDimitry Andric     // BB-VECTORIZER that includes the immediate loads, which we do not want
67671d5a254SDimitry Andric     // to count for the loop vectorizer.
67771d5a254SDimitry Andric     return 1;
67871d5a254SDimitry Andric 
67971d5a254SDimitry Andric   unsigned Cost = 0;
68071d5a254SDimitry Andric   unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy);
681cfca06d7SDimitry Andric   unsigned VF = cast<FixedVectorType>(SrcTy)->getNumElements();
68271d5a254SDimitry Andric   for (unsigned P = 0; P < Log2Diff; ++P) {
68371d5a254SDimitry Andric     if (NumParts > 1)
68471d5a254SDimitry Andric       NumParts /= 2;
68571d5a254SDimitry Andric     Cost += NumParts;
68671d5a254SDimitry Andric   }
68771d5a254SDimitry Andric 
68871d5a254SDimitry Andric   // Currently, a general mix of permutes and pack instructions is output by
68971d5a254SDimitry Andric   // isel, which follow the cost computation above except for this case which
69071d5a254SDimitry Andric   // is one instruction less:
69171d5a254SDimitry Andric   if (VF == 8 && SrcTy->getScalarSizeInBits() == 64 &&
69271d5a254SDimitry Andric       DstTy->getScalarSizeInBits() == 8)
69371d5a254SDimitry Andric     Cost--;
69471d5a254SDimitry Andric 
69571d5a254SDimitry Andric   return Cost;
69671d5a254SDimitry Andric }
69771d5a254SDimitry Andric 
69871d5a254SDimitry Andric // Return the cost of converting a vector bitmask produced by a compare
69971d5a254SDimitry Andric // (SrcTy), to the type of the select or extend instruction (DstTy).
70071d5a254SDimitry Andric unsigned SystemZTTIImpl::
getVectorBitmaskConversionCost(Type * SrcTy,Type * DstTy)70171d5a254SDimitry Andric getVectorBitmaskConversionCost(Type *SrcTy, Type *DstTy) {
70271d5a254SDimitry Andric   assert (SrcTy->isVectorTy() && DstTy->isVectorTy() &&
70371d5a254SDimitry Andric           "Should only be called with vector types.");
70471d5a254SDimitry Andric 
70571d5a254SDimitry Andric   unsigned PackCost = 0;
70671d5a254SDimitry Andric   unsigned SrcScalarBits = SrcTy->getScalarSizeInBits();
70771d5a254SDimitry Andric   unsigned DstScalarBits = DstTy->getScalarSizeInBits();
70871d5a254SDimitry Andric   unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy);
70971d5a254SDimitry Andric   if (SrcScalarBits > DstScalarBits)
71071d5a254SDimitry Andric     // The bitmask will be truncated.
71171d5a254SDimitry Andric     PackCost = getVectorTruncCost(SrcTy, DstTy);
71271d5a254SDimitry Andric   else if (SrcScalarBits < DstScalarBits) {
713d8e91e46SDimitry Andric     unsigned DstNumParts = getNumVectorRegs(DstTy);
71471d5a254SDimitry Andric     // Each vector select needs its part of the bitmask unpacked.
71571d5a254SDimitry Andric     PackCost = Log2Diff * DstNumParts;
71671d5a254SDimitry Andric     // Extra cost for moving part of mask before unpacking.
71771d5a254SDimitry Andric     PackCost += DstNumParts - 1;
71871d5a254SDimitry Andric   }
71971d5a254SDimitry Andric 
72071d5a254SDimitry Andric   return PackCost;
72171d5a254SDimitry Andric }
72271d5a254SDimitry Andric 
72371d5a254SDimitry Andric // Return the type of the compared operands. This is needed to compute the
72471d5a254SDimitry Andric // cost for a Select / ZExt or SExt instruction.
getCmpOpsType(const Instruction * I,unsigned VF=1)72571d5a254SDimitry Andric static Type *getCmpOpsType(const Instruction *I, unsigned VF = 1) {
72671d5a254SDimitry Andric   Type *OpTy = nullptr;
72771d5a254SDimitry Andric   if (CmpInst *CI = dyn_cast<CmpInst>(I->getOperand(0)))
72871d5a254SDimitry Andric     OpTy = CI->getOperand(0)->getType();
72971d5a254SDimitry Andric   else if (Instruction *LogicI = dyn_cast<Instruction>(I->getOperand(0)))
730148779dfSDimitry Andric     if (LogicI->getNumOperands() == 2)
73171d5a254SDimitry Andric       if (CmpInst *CI0 = dyn_cast<CmpInst>(LogicI->getOperand(0)))
73271d5a254SDimitry Andric         if (isa<CmpInst>(LogicI->getOperand(1)))
73371d5a254SDimitry Andric           OpTy = CI0->getOperand(0)->getType();
73471d5a254SDimitry Andric 
73571d5a254SDimitry Andric   if (OpTy != nullptr) {
73671d5a254SDimitry Andric     if (VF == 1) {
73771d5a254SDimitry Andric       assert (!OpTy->isVectorTy() && "Expected scalar type");
73871d5a254SDimitry Andric       return OpTy;
73971d5a254SDimitry Andric     }
74071d5a254SDimitry Andric     // Return the potentially vectorized type based on 'I' and 'VF'.  'I' may
74171d5a254SDimitry Andric     // be either scalar or already vectorized with a same or lesser VF.
74271d5a254SDimitry Andric     Type *ElTy = OpTy->getScalarType();
743cfca06d7SDimitry Andric     return FixedVectorType::get(ElTy, VF);
74471d5a254SDimitry Andric   }
74571d5a254SDimitry Andric 
74671d5a254SDimitry Andric   return nullptr;
74771d5a254SDimitry Andric }
74871d5a254SDimitry Andric 
749d8e91e46SDimitry Andric // Get the cost of converting a boolean vector to a vector with same width
750d8e91e46SDimitry Andric // and element size as Dst, plus the cost of zero extending if needed.
751d8e91e46SDimitry Andric unsigned SystemZTTIImpl::
getBoolVecToIntConversionCost(unsigned Opcode,Type * Dst,const Instruction * I)752d8e91e46SDimitry Andric getBoolVecToIntConversionCost(unsigned Opcode, Type *Dst,
753d8e91e46SDimitry Andric                               const Instruction *I) {
754cfca06d7SDimitry Andric   auto *DstVTy = cast<FixedVectorType>(Dst);
755cfca06d7SDimitry Andric   unsigned VF = DstVTy->getNumElements();
756d8e91e46SDimitry Andric   unsigned Cost = 0;
757d8e91e46SDimitry Andric   // If we know what the widths of the compared operands, get any cost of
758d8e91e46SDimitry Andric   // converting it to match Dst. Otherwise assume same widths.
759d8e91e46SDimitry Andric   Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr);
760d8e91e46SDimitry Andric   if (CmpOpTy != nullptr)
761d8e91e46SDimitry Andric     Cost = getVectorBitmaskConversionCost(CmpOpTy, Dst);
762d8e91e46SDimitry Andric   if (Opcode == Instruction::ZExt || Opcode == Instruction::UIToFP)
763d8e91e46SDimitry Andric     // One 'vn' per dst vector with an immediate mask.
764d8e91e46SDimitry Andric     Cost += getNumVectorRegs(Dst);
765d8e91e46SDimitry Andric   return Cost;
766d8e91e46SDimitry Andric }
767d8e91e46SDimitry Andric 
getCastInstrCost(unsigned Opcode,Type * Dst,Type * Src,TTI::CastContextHint CCH,TTI::TargetCostKind CostKind,const Instruction * I)768344a3780SDimitry Andric InstructionCost SystemZTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
769344a3780SDimitry Andric                                                  Type *Src,
770b60736ecSDimitry Andric                                                  TTI::CastContextHint CCH,
771cfca06d7SDimitry Andric                                                  TTI::TargetCostKind CostKind,
77271d5a254SDimitry Andric                                                  const Instruction *I) {
773cfca06d7SDimitry Andric   // FIXME: Can the logic below also be used for these cost kinds?
774cfca06d7SDimitry Andric   if (CostKind == TTI::TCK_CodeSize || CostKind == TTI::TCK_SizeAndLatency) {
775344a3780SDimitry Andric     auto BaseCost = BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
776cfca06d7SDimitry Andric     return BaseCost == 0 ? BaseCost : 1;
777cfca06d7SDimitry Andric   }
778cfca06d7SDimitry Andric 
77971d5a254SDimitry Andric   unsigned DstScalarBits = Dst->getScalarSizeInBits();
78071d5a254SDimitry Andric   unsigned SrcScalarBits = Src->getScalarSizeInBits();
78171d5a254SDimitry Andric 
782cfca06d7SDimitry Andric   if (!Src->isVectorTy()) {
783cfca06d7SDimitry Andric     assert (!Dst->isVectorTy());
784cfca06d7SDimitry Andric 
785cfca06d7SDimitry Andric     if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP) {
7864df029ccSDimitry Andric       if (Src->isIntegerTy(128))
7874df029ccSDimitry Andric         return LIBCALL_COST;
788cfca06d7SDimitry Andric       if (SrcScalarBits >= 32 ||
789cfca06d7SDimitry Andric           (I != nullptr && isa<LoadInst>(I->getOperand(0))))
790cfca06d7SDimitry Andric         return 1;
791cfca06d7SDimitry Andric       return SrcScalarBits > 1 ? 2 /*i8/i16 extend*/ : 5 /*branch seq.*/;
792cfca06d7SDimitry Andric     }
793cfca06d7SDimitry Andric 
7944df029ccSDimitry Andric     if ((Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI) &&
7954df029ccSDimitry Andric         Dst->isIntegerTy(128))
7964df029ccSDimitry Andric       return LIBCALL_COST;
7974df029ccSDimitry Andric 
7984df029ccSDimitry Andric     if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt)) {
7994df029ccSDimitry Andric       if (Src->isIntegerTy(1)) {
8004df029ccSDimitry Andric         if (DstScalarBits == 128)
8014df029ccSDimitry Andric           return 5 /*branch seq.*/;
8024df029ccSDimitry Andric 
803cfca06d7SDimitry Andric         if (ST->hasLoadStoreOnCond2())
804cfca06d7SDimitry Andric           return 2; // li 0; loc 1
805cfca06d7SDimitry Andric 
806cfca06d7SDimitry Andric         // This should be extension of a compare i1 result, which is done with
807cfca06d7SDimitry Andric         // ipm and a varying sequence of instructions.
808cfca06d7SDimitry Andric         unsigned Cost = 0;
809cfca06d7SDimitry Andric         if (Opcode == Instruction::SExt)
810cfca06d7SDimitry Andric           Cost = (DstScalarBits < 64 ? 3 : 4);
811cfca06d7SDimitry Andric         if (Opcode == Instruction::ZExt)
812cfca06d7SDimitry Andric           Cost = 3;
813cfca06d7SDimitry Andric         Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I) : nullptr);
814cfca06d7SDimitry Andric         if (CmpOpTy != nullptr && CmpOpTy->isFloatingPointTy())
815cfca06d7SDimitry Andric           // If operands of an fp-type was compared, this costs +1.
816cfca06d7SDimitry Andric           Cost++;
817cfca06d7SDimitry Andric         return Cost;
818cfca06d7SDimitry Andric       }
8194df029ccSDimitry Andric       else if (isInt128InVR(Dst)) {
8204df029ccSDimitry Andric         // Extensions from GPR to i128 (in VR) typically costs two instructions,
8214df029ccSDimitry Andric         // but a zero-extending load would be just one extra instruction.
8224df029ccSDimitry Andric         if (Opcode == Instruction::ZExt && I != nullptr)
8234df029ccSDimitry Andric           if (LoadInst *Ld = dyn_cast<LoadInst>(I->getOperand(0)))
8244df029ccSDimitry Andric             if (Ld->hasOneUse())
8254df029ccSDimitry Andric               return 1;
8264df029ccSDimitry Andric         return 2;
8274df029ccSDimitry Andric       }
8284df029ccSDimitry Andric     }
8294df029ccSDimitry Andric 
8304df029ccSDimitry Andric     if (Opcode == Instruction::Trunc && isInt128InVR(Src) && I != nullptr) {
8314df029ccSDimitry Andric       if (LoadInst *Ld = dyn_cast<LoadInst>(I->getOperand(0)))
8324df029ccSDimitry Andric         if (Ld->hasOneUse())
8334df029ccSDimitry Andric           return 0;  // Will be converted to GPR load.
8344df029ccSDimitry Andric       bool OnlyTruncatingStores = true;
8354df029ccSDimitry Andric       for (const User *U : I->users())
8364df029ccSDimitry Andric         if (!isa<StoreInst>(U)) {
8374df029ccSDimitry Andric           OnlyTruncatingStores = false;
8384df029ccSDimitry Andric           break;
8394df029ccSDimitry Andric         }
8404df029ccSDimitry Andric       if (OnlyTruncatingStores)
8414df029ccSDimitry Andric         return 0;
8424df029ccSDimitry Andric       return 2; // Vector element extraction.
8434df029ccSDimitry Andric     }
844cfca06d7SDimitry Andric   }
845cfca06d7SDimitry Andric   else if (ST->hasVector()) {
846344a3780SDimitry Andric     // Vector to scalar cast.
847cfca06d7SDimitry Andric     auto *SrcVecTy = cast<FixedVectorType>(Src);
848344a3780SDimitry Andric     auto *DstVecTy = dyn_cast<FixedVectorType>(Dst);
849344a3780SDimitry Andric     if (!DstVecTy) {
850344a3780SDimitry Andric       // TODO: tune vector-to-scalar cast.
851344a3780SDimitry Andric       return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
852344a3780SDimitry Andric     }
853cfca06d7SDimitry Andric     unsigned VF = SrcVecTy->getNumElements();
854d8e91e46SDimitry Andric     unsigned NumDstVectors = getNumVectorRegs(Dst);
855d8e91e46SDimitry Andric     unsigned NumSrcVectors = getNumVectorRegs(Src);
85671d5a254SDimitry Andric 
85771d5a254SDimitry Andric     if (Opcode == Instruction::Trunc) {
85871d5a254SDimitry Andric       if (Src->getScalarSizeInBits() == Dst->getScalarSizeInBits())
85971d5a254SDimitry Andric         return 0; // Check for NOOP conversions.
86071d5a254SDimitry Andric       return getVectorTruncCost(Src, Dst);
86171d5a254SDimitry Andric     }
86271d5a254SDimitry Andric 
86371d5a254SDimitry Andric     if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) {
86471d5a254SDimitry Andric       if (SrcScalarBits >= 8) {
865145449b1SDimitry Andric         // ZExt will use either a single unpack or a vector permute.
866145449b1SDimitry Andric         if (Opcode == Instruction::ZExt)
867145449b1SDimitry Andric           return NumDstVectors;
868145449b1SDimitry Andric 
869145449b1SDimitry Andric         // SExt will be handled with one unpack per doubling of width.
87071d5a254SDimitry Andric         unsigned NumUnpacks = getElSizeLog2Diff(Src, Dst);
87171d5a254SDimitry Andric 
87271d5a254SDimitry Andric         // For types that spans multiple vector registers, some additional
87371d5a254SDimitry Andric         // instructions are used to setup the unpacking.
87471d5a254SDimitry Andric         unsigned NumSrcVectorOps =
87571d5a254SDimitry Andric           (NumUnpacks > 1 ? (NumDstVectors - NumSrcVectors)
87671d5a254SDimitry Andric                           : (NumDstVectors / 2));
87771d5a254SDimitry Andric 
87871d5a254SDimitry Andric         return (NumUnpacks * NumDstVectors) + NumSrcVectorOps;
87971d5a254SDimitry Andric       }
880d8e91e46SDimitry Andric       else if (SrcScalarBits == 1)
881d8e91e46SDimitry Andric         return getBoolVecToIntConversionCost(Opcode, Dst, I);
88271d5a254SDimitry Andric     }
88371d5a254SDimitry Andric 
88471d5a254SDimitry Andric     if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP ||
88571d5a254SDimitry Andric         Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI) {
88671d5a254SDimitry Andric       // TODO: Fix base implementation which could simplify things a bit here
88771d5a254SDimitry Andric       // (seems to miss on differentiating on scalar/vector types).
88871d5a254SDimitry Andric 
8891d5ae102SDimitry Andric       // Only 64 bit vector conversions are natively supported before z15.
890e6d15924SDimitry Andric       if (DstScalarBits == 64 || ST->hasVectorEnhancements2()) {
891e6d15924SDimitry Andric         if (SrcScalarBits == DstScalarBits)
89271d5a254SDimitry Andric           return NumDstVectors;
89371d5a254SDimitry Andric 
894d8e91e46SDimitry Andric         if (SrcScalarBits == 1)
895d8e91e46SDimitry Andric           return getBoolVecToIntConversionCost(Opcode, Dst, I) + NumDstVectors;
896d8e91e46SDimitry Andric       }
897d8e91e46SDimitry Andric 
89871d5a254SDimitry Andric       // Return the cost of multiple scalar invocation plus the cost of
89971d5a254SDimitry Andric       // inserting and extracting the values. Base implementation does not
90071d5a254SDimitry Andric       // realize float->int gets scalarized.
901344a3780SDimitry Andric       InstructionCost ScalarCost = getCastInstrCost(
902b60736ecSDimitry Andric           Opcode, Dst->getScalarType(), Src->getScalarType(), CCH, CostKind);
903344a3780SDimitry Andric       InstructionCost TotCost = VF * ScalarCost;
90471d5a254SDimitry Andric       bool NeedsInserts = true, NeedsExtracts = true;
90571d5a254SDimitry Andric       // FP128 registers do not get inserted or extracted.
90671d5a254SDimitry Andric       if (DstScalarBits == 128 &&
90771d5a254SDimitry Andric           (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP))
90871d5a254SDimitry Andric         NeedsInserts = false;
90971d5a254SDimitry Andric       if (SrcScalarBits == 128 &&
91071d5a254SDimitry Andric           (Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI))
91171d5a254SDimitry Andric         NeedsExtracts = false;
91271d5a254SDimitry Andric 
913e3b55780SDimitry Andric       TotCost += getScalarizationOverhead(SrcVecTy, /*Insert*/ false,
914e3b55780SDimitry Andric                                           NeedsExtracts, CostKind);
915e3b55780SDimitry Andric       TotCost += getScalarizationOverhead(DstVecTy, NeedsInserts,
916e3b55780SDimitry Andric                                           /*Extract*/ false, CostKind);
91771d5a254SDimitry Andric 
91871d5a254SDimitry Andric       // FIXME: VF 2 for float<->i32 is currently just as expensive as for VF 4.
91971d5a254SDimitry Andric       if (VF == 2 && SrcScalarBits == 32 && DstScalarBits == 32)
92071d5a254SDimitry Andric         TotCost *= 2;
92171d5a254SDimitry Andric 
92271d5a254SDimitry Andric       return TotCost;
92371d5a254SDimitry Andric     }
92471d5a254SDimitry Andric 
92571d5a254SDimitry Andric     if (Opcode == Instruction::FPTrunc) {
92671d5a254SDimitry Andric       if (SrcScalarBits == 128)  // fp128 -> double/float + inserts of elements.
927cfca06d7SDimitry Andric         return VF /*ldxbr/lexbr*/ +
928e3b55780SDimitry Andric                getScalarizationOverhead(DstVecTy, /*Insert*/ true,
929e3b55780SDimitry Andric                                         /*Extract*/ false, CostKind);
93071d5a254SDimitry Andric       else // double -> float
93171d5a254SDimitry Andric         return VF / 2 /*vledb*/ + std::max(1U, VF / 4 /*vperm*/);
93271d5a254SDimitry Andric     }
93371d5a254SDimitry Andric 
93471d5a254SDimitry Andric     if (Opcode == Instruction::FPExt) {
93571d5a254SDimitry Andric       if (SrcScalarBits == 32 && DstScalarBits == 64) {
93671d5a254SDimitry Andric         // float -> double is very rare and currently unoptimized. Instead of
93771d5a254SDimitry Andric         // using vldeb, which can do two at a time, all conversions are
93871d5a254SDimitry Andric         // scalarized.
93971d5a254SDimitry Andric         return VF * 2;
94071d5a254SDimitry Andric       }
94171d5a254SDimitry Andric       // -> fp128.  VF * lxdb/lxeb + extraction of elements.
942e3b55780SDimitry Andric       return VF + getScalarizationOverhead(SrcVecTy, /*Insert*/ false,
943e3b55780SDimitry Andric                                            /*Extract*/ true, CostKind);
94471d5a254SDimitry Andric     }
94571d5a254SDimitry Andric   }
94671d5a254SDimitry Andric 
947b60736ecSDimitry Andric   return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
94871d5a254SDimitry Andric }
94971d5a254SDimitry Andric 
950d8e91e46SDimitry Andric // Scalar i8 / i16 operations will typically be made after first extending
951d8e91e46SDimitry Andric // the operands to i32.
getOperandsExtensionCost(const Instruction * I)952d8e91e46SDimitry Andric static unsigned getOperandsExtensionCost(const Instruction *I) {
953d8e91e46SDimitry Andric   unsigned ExtCost = 0;
954d8e91e46SDimitry Andric   for (Value *Op : I->operands())
955d8e91e46SDimitry Andric     // A load of i8 or i16 sign/zero extends to i32.
956d8e91e46SDimitry Andric     if (!isa<LoadInst>(Op) && !isa<ConstantInt>(Op))
957d8e91e46SDimitry Andric       ExtCost++;
958d8e91e46SDimitry Andric 
959d8e91e46SDimitry Andric   return ExtCost;
960d8e91e46SDimitry Andric }
961d8e91e46SDimitry Andric 
getCmpSelInstrCost(unsigned Opcode,Type * ValTy,Type * CondTy,CmpInst::Predicate VecPred,TTI::TargetCostKind CostKind,const Instruction * I)962344a3780SDimitry Andric InstructionCost SystemZTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
963344a3780SDimitry Andric                                                    Type *CondTy,
964344a3780SDimitry Andric                                                    CmpInst::Predicate VecPred,
965cfca06d7SDimitry Andric                                                    TTI::TargetCostKind CostKind,
966cfca06d7SDimitry Andric                                                    const Instruction *I) {
967cfca06d7SDimitry Andric   if (CostKind != TTI::TCK_RecipThroughput)
968b60736ecSDimitry Andric     return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind);
969cfca06d7SDimitry Andric 
970cfca06d7SDimitry Andric   if (!ValTy->isVectorTy()) {
971cfca06d7SDimitry Andric     switch (Opcode) {
972cfca06d7SDimitry Andric     case Instruction::ICmp: {
973cfca06d7SDimitry Andric       // A loaded value compared with 0 with multiple users becomes Load and
974cfca06d7SDimitry Andric       // Test. The load is then not foldable, so return 0 cost for the ICmp.
975cfca06d7SDimitry Andric       unsigned ScalarBits = ValTy->getScalarSizeInBits();
9764df029ccSDimitry Andric       if (I != nullptr && (ScalarBits == 32 || ScalarBits == 64))
977cfca06d7SDimitry Andric         if (LoadInst *Ld = dyn_cast<LoadInst>(I->getOperand(0)))
978cfca06d7SDimitry Andric           if (const ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)))
979cfca06d7SDimitry Andric             if (!Ld->hasOneUse() && Ld->getParent() == I->getParent() &&
980b60736ecSDimitry Andric                 C->isZero())
981cfca06d7SDimitry Andric               return 0;
982cfca06d7SDimitry Andric 
983cfca06d7SDimitry Andric       unsigned Cost = 1;
984cfca06d7SDimitry Andric       if (ValTy->isIntegerTy() && ValTy->getScalarSizeInBits() <= 16)
985cfca06d7SDimitry Andric         Cost += (I != nullptr ? getOperandsExtensionCost(I) : 2);
986cfca06d7SDimitry Andric       return Cost;
987cfca06d7SDimitry Andric     }
988cfca06d7SDimitry Andric     case Instruction::Select:
9894df029ccSDimitry Andric       if (ValTy->isFloatingPointTy() || isInt128InVR(ValTy))
9904df029ccSDimitry Andric         return 4; // No LOC for FP / i128 - costs a conditional jump.
991cfca06d7SDimitry Andric       return 1; // Load On Condition / Select Register.
992cfca06d7SDimitry Andric     }
993cfca06d7SDimitry Andric   }
994cfca06d7SDimitry Andric   else if (ST->hasVector()) {
995cfca06d7SDimitry Andric     unsigned VF = cast<FixedVectorType>(ValTy)->getNumElements();
99671d5a254SDimitry Andric 
99771d5a254SDimitry Andric     // Called with a compare instruction.
99871d5a254SDimitry Andric     if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) {
99971d5a254SDimitry Andric       unsigned PredicateExtraCost = 0;
100071d5a254SDimitry Andric       if (I != nullptr) {
100171d5a254SDimitry Andric         // Some predicates cost one or two extra instructions.
1002eb11fae6SDimitry Andric         switch (cast<CmpInst>(I)->getPredicate()) {
100371d5a254SDimitry Andric         case CmpInst::Predicate::ICMP_NE:
100471d5a254SDimitry Andric         case CmpInst::Predicate::ICMP_UGE:
100571d5a254SDimitry Andric         case CmpInst::Predicate::ICMP_ULE:
100671d5a254SDimitry Andric         case CmpInst::Predicate::ICMP_SGE:
100771d5a254SDimitry Andric         case CmpInst::Predicate::ICMP_SLE:
100871d5a254SDimitry Andric           PredicateExtraCost = 1;
100971d5a254SDimitry Andric           break;
101071d5a254SDimitry Andric         case CmpInst::Predicate::FCMP_ONE:
101171d5a254SDimitry Andric         case CmpInst::Predicate::FCMP_ORD:
101271d5a254SDimitry Andric         case CmpInst::Predicate::FCMP_UEQ:
101371d5a254SDimitry Andric         case CmpInst::Predicate::FCMP_UNO:
101471d5a254SDimitry Andric           PredicateExtraCost = 2;
101571d5a254SDimitry Andric           break;
101671d5a254SDimitry Andric         default:
101771d5a254SDimitry Andric           break;
101871d5a254SDimitry Andric         }
101971d5a254SDimitry Andric       }
102071d5a254SDimitry Andric 
102171d5a254SDimitry Andric       // Float is handled with 2*vmr[lh]f + 2*vldeb + vfchdb for each pair of
102271d5a254SDimitry Andric       // floats.  FIXME: <2 x float> generates same code as <4 x float>.
102371d5a254SDimitry Andric       unsigned CmpCostPerVector = (ValTy->getScalarType()->isFloatTy() ? 10 : 1);
1024d8e91e46SDimitry Andric       unsigned NumVecs_cmp = getNumVectorRegs(ValTy);
102571d5a254SDimitry Andric 
102671d5a254SDimitry Andric       unsigned Cost = (NumVecs_cmp * (CmpCostPerVector + PredicateExtraCost));
102771d5a254SDimitry Andric       return Cost;
102871d5a254SDimitry Andric     }
102971d5a254SDimitry Andric     else { // Called with a select instruction.
103071d5a254SDimitry Andric       assert (Opcode == Instruction::Select);
103171d5a254SDimitry Andric 
103271d5a254SDimitry Andric       // We can figure out the extra cost of packing / unpacking if the
103371d5a254SDimitry Andric       // instruction was passed and the compare instruction is found.
103471d5a254SDimitry Andric       unsigned PackCost = 0;
103571d5a254SDimitry Andric       Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr);
103671d5a254SDimitry Andric       if (CmpOpTy != nullptr)
103771d5a254SDimitry Andric         PackCost =
103871d5a254SDimitry Andric           getVectorBitmaskConversionCost(CmpOpTy, ValTy);
103971d5a254SDimitry Andric 
1040d8e91e46SDimitry Andric       return getNumVectorRegs(ValTy) /*vsel*/ + PackCost;
104171d5a254SDimitry Andric     }
104271d5a254SDimitry Andric   }
1043d8e91e46SDimitry Andric 
1044b60736ecSDimitry Andric   return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind);
104571d5a254SDimitry Andric }
104671d5a254SDimitry Andric 
getVectorInstrCost(unsigned Opcode,Type * Val,TTI::TargetCostKind CostKind,unsigned Index,Value * Op0,Value * Op1)1047344a3780SDimitry Andric InstructionCost SystemZTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
1048e3b55780SDimitry Andric                                                    TTI::TargetCostKind CostKind,
1049e3b55780SDimitry Andric                                                    unsigned Index, Value *Op0,
1050e3b55780SDimitry Andric                                                    Value *Op1) {
105171d5a254SDimitry Andric   // vlvgp will insert two grs into a vector register, so only count half the
105271d5a254SDimitry Andric   // number of instructions.
1053ca089b24SDimitry Andric   if (Opcode == Instruction::InsertElement && Val->isIntOrIntVectorTy(64))
105471d5a254SDimitry Andric     return ((Index % 2 == 0) ? 1 : 0);
105571d5a254SDimitry Andric 
105671d5a254SDimitry Andric   if (Opcode == Instruction::ExtractElement) {
1057d8e91e46SDimitry Andric     int Cost = ((getScalarSizeInBits(Val) == 1) ? 2 /*+test-under-mask*/ : 1);
105871d5a254SDimitry Andric 
105971d5a254SDimitry Andric     // Give a slight penalty for moving out of vector pipeline to FXU unit.
1060ca089b24SDimitry Andric     if (Index == 0 && Val->isIntOrIntVectorTy())
106171d5a254SDimitry Andric       Cost += 1;
106271d5a254SDimitry Andric 
106371d5a254SDimitry Andric     return Cost;
106471d5a254SDimitry Andric   }
106571d5a254SDimitry Andric 
1066e3b55780SDimitry Andric   return BaseT::getVectorInstrCost(Opcode, Val, CostKind, Index, Op0, Op1);
106771d5a254SDimitry Andric }
106871d5a254SDimitry Andric 
1069d8e91e46SDimitry Andric // Check if a load may be folded as a memory operand in its user.
1070d8e91e46SDimitry Andric bool SystemZTTIImpl::
isFoldableLoad(const LoadInst * Ld,const Instruction * & FoldedValue)1071d8e91e46SDimitry Andric isFoldableLoad(const LoadInst *Ld, const Instruction *&FoldedValue) {
1072d8e91e46SDimitry Andric   if (!Ld->hasOneUse())
1073d8e91e46SDimitry Andric     return false;
1074d8e91e46SDimitry Andric   FoldedValue = Ld;
1075d8e91e46SDimitry Andric   const Instruction *UserI = cast<Instruction>(*Ld->user_begin());
1076d8e91e46SDimitry Andric   unsigned LoadedBits = getScalarSizeInBits(Ld->getType());
1077d8e91e46SDimitry Andric   unsigned TruncBits = 0;
1078d8e91e46SDimitry Andric   unsigned SExtBits = 0;
1079d8e91e46SDimitry Andric   unsigned ZExtBits = 0;
1080d8e91e46SDimitry Andric   if (UserI->hasOneUse()) {
1081d8e91e46SDimitry Andric     unsigned UserBits = UserI->getType()->getScalarSizeInBits();
1082d8e91e46SDimitry Andric     if (isa<TruncInst>(UserI))
1083d8e91e46SDimitry Andric       TruncBits = UserBits;
1084d8e91e46SDimitry Andric     else if (isa<SExtInst>(UserI))
1085d8e91e46SDimitry Andric       SExtBits = UserBits;
1086d8e91e46SDimitry Andric     else if (isa<ZExtInst>(UserI))
1087d8e91e46SDimitry Andric       ZExtBits = UserBits;
1088d8e91e46SDimitry Andric   }
1089d8e91e46SDimitry Andric   if (TruncBits || SExtBits || ZExtBits) {
1090d8e91e46SDimitry Andric     FoldedValue = UserI;
1091d8e91e46SDimitry Andric     UserI = cast<Instruction>(*UserI->user_begin());
1092d8e91e46SDimitry Andric     // Load (single use) -> trunc/extend (single use) -> UserI
1093d8e91e46SDimitry Andric   }
1094d8e91e46SDimitry Andric   if ((UserI->getOpcode() == Instruction::Sub ||
1095d8e91e46SDimitry Andric        UserI->getOpcode() == Instruction::SDiv ||
1096d8e91e46SDimitry Andric        UserI->getOpcode() == Instruction::UDiv) &&
1097d8e91e46SDimitry Andric       UserI->getOperand(1) != FoldedValue)
1098d8e91e46SDimitry Andric     return false; // Not commutative, only RHS foldable.
1099d8e91e46SDimitry Andric   // LoadOrTruncBits holds the number of effectively loaded bits, but 0 if an
1100d8e91e46SDimitry Andric   // extension was made of the load.
1101d8e91e46SDimitry Andric   unsigned LoadOrTruncBits =
1102d8e91e46SDimitry Andric       ((SExtBits || ZExtBits) ? 0 : (TruncBits ? TruncBits : LoadedBits));
110371d5a254SDimitry Andric   switch (UserI->getOpcode()) {
1104d8e91e46SDimitry Andric   case Instruction::Add: // SE: 16->32, 16/32->64, z14:16->64. ZE: 32->64
110571d5a254SDimitry Andric   case Instruction::Sub:
1106d8e91e46SDimitry Andric   case Instruction::ICmp:
1107d8e91e46SDimitry Andric     if (LoadedBits == 32 && ZExtBits == 64)
1108d8e91e46SDimitry Andric       return true;
1109e3b55780SDimitry Andric     [[fallthrough]];
1110d8e91e46SDimitry Andric   case Instruction::Mul: // SE: 16->32, 32->64, z14:16->64
1111d8e91e46SDimitry Andric     if (UserI->getOpcode() != Instruction::ICmp) {
1112d8e91e46SDimitry Andric       if (LoadedBits == 16 &&
1113d8e91e46SDimitry Andric           (SExtBits == 32 ||
1114d8e91e46SDimitry Andric            (SExtBits == 64 && ST->hasMiscellaneousExtensions2())))
1115d8e91e46SDimitry Andric         return true;
1116d8e91e46SDimitry Andric       if (LoadOrTruncBits == 16)
1117d8e91e46SDimitry Andric         return true;
1118d8e91e46SDimitry Andric     }
1119e3b55780SDimitry Andric     [[fallthrough]];
1120d8e91e46SDimitry Andric   case Instruction::SDiv:// SE: 32->64
1121d8e91e46SDimitry Andric     if (LoadedBits == 32 && SExtBits == 64)
1122d8e91e46SDimitry Andric       return true;
1123e3b55780SDimitry Andric     [[fallthrough]];
112471d5a254SDimitry Andric   case Instruction::UDiv:
112571d5a254SDimitry Andric   case Instruction::And:
112671d5a254SDimitry Andric   case Instruction::Or:
112771d5a254SDimitry Andric   case Instruction::Xor:
112871d5a254SDimitry Andric     // This also makes sense for float operations, but disabled for now due
112971d5a254SDimitry Andric     // to regressions.
113071d5a254SDimitry Andric     // case Instruction::FCmp:
113171d5a254SDimitry Andric     // case Instruction::FAdd:
113271d5a254SDimitry Andric     // case Instruction::FSub:
113371d5a254SDimitry Andric     // case Instruction::FMul:
113471d5a254SDimitry Andric     // case Instruction::FDiv:
1135d8e91e46SDimitry Andric 
1136d8e91e46SDimitry Andric     // All possible extensions of memory checked above.
1137d8e91e46SDimitry Andric 
1138d8e91e46SDimitry Andric     // Comparison between memory and immediate.
1139d8e91e46SDimitry Andric     if (UserI->getOpcode() == Instruction::ICmp)
1140d8e91e46SDimitry Andric       if (ConstantInt *CI = dyn_cast<ConstantInt>(UserI->getOperand(1)))
1141b60736ecSDimitry Andric         if (CI->getValue().isIntN(16))
1142d8e91e46SDimitry Andric           return true;
1143d8e91e46SDimitry Andric     return (LoadOrTruncBits == 32 || LoadOrTruncBits == 64);
114471d5a254SDimitry Andric     break;
114571d5a254SDimitry Andric   }
1146d8e91e46SDimitry Andric   return false;
1147d8e91e46SDimitry Andric }
114871d5a254SDimitry Andric 
isBswapIntrinsicCall(const Value * V)1149d8e91e46SDimitry Andric static bool isBswapIntrinsicCall(const Value *V) {
1150d8e91e46SDimitry Andric   if (const Instruction *I = dyn_cast<Instruction>(V))
1151d8e91e46SDimitry Andric     if (auto *CI = dyn_cast<CallInst>(I))
1152d8e91e46SDimitry Andric       if (auto *F = CI->getCalledFunction())
1153d8e91e46SDimitry Andric         if (F->getIntrinsicID() == Intrinsic::bswap)
1154d8e91e46SDimitry Andric           return true;
1155d8e91e46SDimitry Andric   return false;
1156d8e91e46SDimitry Andric }
1157d8e91e46SDimitry Andric 
getMemoryOpCost(unsigned Opcode,Type * Src,MaybeAlign Alignment,unsigned AddressSpace,TTI::TargetCostKind CostKind,TTI::OperandValueInfo OpInfo,const Instruction * I)1158344a3780SDimitry Andric InstructionCost SystemZTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
1159344a3780SDimitry Andric                                                 MaybeAlign Alignment,
1160344a3780SDimitry Andric                                                 unsigned AddressSpace,
1161cfca06d7SDimitry Andric                                                 TTI::TargetCostKind CostKind,
1162e3b55780SDimitry Andric                                                 TTI::OperandValueInfo OpInfo,
1163d8e91e46SDimitry Andric                                                 const Instruction *I) {
1164d8e91e46SDimitry Andric   assert(!Src->isVoidTy() && "Invalid type");
1165d8e91e46SDimitry Andric 
1166cfca06d7SDimitry Andric   // TODO: Handle other cost kinds.
1167cfca06d7SDimitry Andric   if (CostKind != TTI::TCK_RecipThroughput)
1168cfca06d7SDimitry Andric     return 1;
1169cfca06d7SDimitry Andric 
1170d8e91e46SDimitry Andric   if (!Src->isVectorTy() && Opcode == Instruction::Load && I != nullptr) {
1171d8e91e46SDimitry Andric     // Store the load or its truncated or extended value in FoldedValue.
1172d8e91e46SDimitry Andric     const Instruction *FoldedValue = nullptr;
1173d8e91e46SDimitry Andric     if (isFoldableLoad(cast<LoadInst>(I), FoldedValue)) {
1174d8e91e46SDimitry Andric       const Instruction *UserI = cast<Instruction>(*FoldedValue->user_begin());
1175d8e91e46SDimitry Andric       assert (UserI->getNumOperands() == 2 && "Expected a binop.");
117671d5a254SDimitry Andric 
117771d5a254SDimitry Andric       // UserI can't fold two loads, so in that case return 0 cost only
117871d5a254SDimitry Andric       // half of the time.
117971d5a254SDimitry Andric       for (unsigned i = 0; i < 2; ++i) {
1180d8e91e46SDimitry Andric         if (UserI->getOperand(i) == FoldedValue)
118171d5a254SDimitry Andric           continue;
1182d8e91e46SDimitry Andric 
1183d8e91e46SDimitry Andric         if (Instruction *OtherOp = dyn_cast<Instruction>(UserI->getOperand(i))){
1184d8e91e46SDimitry Andric           LoadInst *OtherLoad = dyn_cast<LoadInst>(OtherOp);
1185d8e91e46SDimitry Andric           if (!OtherLoad &&
1186d8e91e46SDimitry Andric               (isa<TruncInst>(OtherOp) || isa<SExtInst>(OtherOp) ||
1187d8e91e46SDimitry Andric                isa<ZExtInst>(OtherOp)))
1188d8e91e46SDimitry Andric             OtherLoad = dyn_cast<LoadInst>(OtherOp->getOperand(0));
1189d8e91e46SDimitry Andric           if (OtherLoad && isFoldableLoad(OtherLoad, FoldedValue/*dummy*/))
1190d8e91e46SDimitry Andric             return i == 0; // Both operands foldable.
119171d5a254SDimitry Andric         }
119271d5a254SDimitry Andric       }
119371d5a254SDimitry Andric 
1194d8e91e46SDimitry Andric       return 0; // Only I is foldable in user.
1195d8e91e46SDimitry Andric     }
1196d8e91e46SDimitry Andric   }
1197d8e91e46SDimitry Andric 
1198b1c73532SDimitry Andric   // Type legalization (via getNumberOfParts) can't handle structs
1199b1c73532SDimitry Andric   if (TLI->getValueType(DL, Src, true) == MVT::Other)
1200b1c73532SDimitry Andric     return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace,
1201b1c73532SDimitry Andric                                   CostKind);
1202b1c73532SDimitry Andric 
12034df029ccSDimitry Andric   // FP128 is a legal type but kept in a register pair on older CPUs.
12044df029ccSDimitry Andric   if (Src->isFP128Ty() && !ST->hasVectorEnhancements1())
12054df029ccSDimitry Andric     return 2;
12064df029ccSDimitry Andric 
1207d8e91e46SDimitry Andric   unsigned NumOps =
1208d8e91e46SDimitry Andric     (Src->isVectorTy() ? getNumVectorRegs(Src) : getNumberOfParts(Src));
1209d8e91e46SDimitry Andric 
1210d8e91e46SDimitry Andric   // Store/Load reversed saves one instruction.
1211e6d15924SDimitry Andric   if (((!Src->isVectorTy() && NumOps == 1) || ST->hasVectorEnhancements2()) &&
1212e6d15924SDimitry Andric       I != nullptr) {
1213d8e91e46SDimitry Andric     if (Opcode == Instruction::Load && I->hasOneUse()) {
1214d8e91e46SDimitry Andric       const Instruction *LdUser = cast<Instruction>(*I->user_begin());
1215d8e91e46SDimitry Andric       // In case of load -> bswap -> store, return normal cost for the load.
1216d8e91e46SDimitry Andric       if (isBswapIntrinsicCall(LdUser) &&
1217d8e91e46SDimitry Andric           (!LdUser->hasOneUse() || !isa<StoreInst>(*LdUser->user_begin())))
1218d8e91e46SDimitry Andric         return 0;
1219d8e91e46SDimitry Andric     }
1220d8e91e46SDimitry Andric     else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
1221d8e91e46SDimitry Andric       const Value *StoredVal = SI->getValueOperand();
1222d8e91e46SDimitry Andric       if (StoredVal->hasOneUse() && isBswapIntrinsicCall(StoredVal))
122371d5a254SDimitry Andric         return 0;
122471d5a254SDimitry Andric     }
122571d5a254SDimitry Andric   }
122671d5a254SDimitry Andric 
122771d5a254SDimitry Andric   return  NumOps;
122871d5a254SDimitry Andric }
122971d5a254SDimitry Andric 
1230d8e91e46SDimitry Andric // The generic implementation of getInterleavedMemoryOpCost() is based on
1231d8e91e46SDimitry Andric // adding costs of the memory operations plus all the extracts and inserts
1232d8e91e46SDimitry Andric // needed for using / defining the vector operands. The SystemZ version does
1233d8e91e46SDimitry Andric // roughly the same but bases the computations on vector permutations
1234d8e91e46SDimitry Andric // instead.
getInterleavedMemoryOpCost(unsigned Opcode,Type * VecTy,unsigned Factor,ArrayRef<unsigned> Indices,Align Alignment,unsigned AddressSpace,TTI::TargetCostKind CostKind,bool UseMaskForCond,bool UseMaskForGaps)1235344a3780SDimitry Andric InstructionCost SystemZTTIImpl::getInterleavedMemoryOpCost(
1236cfca06d7SDimitry Andric     unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
1237cfca06d7SDimitry Andric     Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind,
1238cfca06d7SDimitry Andric     bool UseMaskForCond, bool UseMaskForGaps) {
1239d8e91e46SDimitry Andric   if (UseMaskForCond || UseMaskForGaps)
1240d8e91e46SDimitry Andric     return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
1241cfca06d7SDimitry Andric                                              Alignment, AddressSpace, CostKind,
1242d8e91e46SDimitry Andric                                              UseMaskForCond, UseMaskForGaps);
124371d5a254SDimitry Andric   assert(isa<VectorType>(VecTy) &&
124471d5a254SDimitry Andric          "Expect a vector type for interleaved memory op");
124571d5a254SDimitry Andric 
1246cfca06d7SDimitry Andric   unsigned NumElts = cast<FixedVectorType>(VecTy)->getNumElements();
1247d8e91e46SDimitry Andric   assert(Factor > 1 && NumElts % Factor == 0 && "Invalid interleave factor");
1248d8e91e46SDimitry Andric   unsigned VF = NumElts / Factor;
1249d8e91e46SDimitry Andric   unsigned NumEltsPerVecReg = (128U / getScalarSizeInBits(VecTy));
1250d8e91e46SDimitry Andric   unsigned NumVectorMemOps = getNumVectorRegs(VecTy);
1251d8e91e46SDimitry Andric   unsigned NumPermutes = 0;
125271d5a254SDimitry Andric 
1253d8e91e46SDimitry Andric   if (Opcode == Instruction::Load) {
1254d8e91e46SDimitry Andric     // Loading interleave groups may have gaps, which may mean fewer
1255d8e91e46SDimitry Andric     // loads. Find out how many vectors will be loaded in total, and in how
1256d8e91e46SDimitry Andric     // many of them each value will be in.
1257d8e91e46SDimitry Andric     BitVector UsedInsts(NumVectorMemOps, false);
1258d8e91e46SDimitry Andric     std::vector<BitVector> ValueVecs(Factor, BitVector(NumVectorMemOps, false));
1259d8e91e46SDimitry Andric     for (unsigned Index : Indices)
1260d8e91e46SDimitry Andric       for (unsigned Elt = 0; Elt < VF; ++Elt) {
1261d8e91e46SDimitry Andric         unsigned Vec = (Index + Elt * Factor) / NumEltsPerVecReg;
1262d8e91e46SDimitry Andric         UsedInsts.set(Vec);
1263d8e91e46SDimitry Andric         ValueVecs[Index].set(Vec);
1264d8e91e46SDimitry Andric       }
1265d8e91e46SDimitry Andric     NumVectorMemOps = UsedInsts.count();
126671d5a254SDimitry Andric 
1267d8e91e46SDimitry Andric     for (unsigned Index : Indices) {
1268d8e91e46SDimitry Andric       // Estimate that each loaded source vector containing this Index
1269d8e91e46SDimitry Andric       // requires one operation, except that vperm can handle two input
1270d8e91e46SDimitry Andric       // registers first time for each dst vector.
1271d8e91e46SDimitry Andric       unsigned NumSrcVecs = ValueVecs[Index].count();
1272344a3780SDimitry Andric       unsigned NumDstVecs = divideCeil(VF * getScalarSizeInBits(VecTy), 128U);
1273d8e91e46SDimitry Andric       assert (NumSrcVecs >= NumDstVecs && "Expected at least as many sources");
1274d8e91e46SDimitry Andric       NumPermutes += std::max(1U, NumSrcVecs - NumDstVecs);
1275d8e91e46SDimitry Andric     }
1276d8e91e46SDimitry Andric   } else {
1277d8e91e46SDimitry Andric     // Estimate the permutes for each stored vector as the smaller of the
1278d8e91e46SDimitry Andric     // number of elements and the number of source vectors. Subtract one per
1279d8e91e46SDimitry Andric     // dst vector for vperm (S.A.).
1280d8e91e46SDimitry Andric     unsigned NumSrcVecs = std::min(NumEltsPerVecReg, Factor);
1281d8e91e46SDimitry Andric     unsigned NumDstVecs = NumVectorMemOps;
1282d8e91e46SDimitry Andric     NumPermutes += (NumDstVecs * NumSrcVecs) - NumDstVecs;
1283d8e91e46SDimitry Andric   }
128471d5a254SDimitry Andric 
128571d5a254SDimitry Andric   // Cost of load/store operations and the permutations needed.
1286d8e91e46SDimitry Andric   return NumVectorMemOps + NumPermutes;
1287d8e91e46SDimitry Andric }
1288d8e91e46SDimitry Andric 
1289ac9a064cSDimitry Andric static int
getVectorIntrinsicInstrCost(Intrinsic::ID ID,Type * RetTy,const SmallVectorImpl<Type * > & ParamTys)1290ac9a064cSDimitry Andric getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
1291ac9a064cSDimitry Andric                             const SmallVectorImpl<Type *> &ParamTys) {
1292d8e91e46SDimitry Andric   if (RetTy->isVectorTy() && ID == Intrinsic::bswap)
1293d8e91e46SDimitry Andric     return getNumVectorRegs(RetTy); // VPERM
1294ac9a064cSDimitry Andric 
1295ac9a064cSDimitry Andric   if (ID == Intrinsic::vector_reduce_add) {
1296ac9a064cSDimitry Andric     // Retrieve number and size of elements for the vector op.
1297ac9a064cSDimitry Andric     auto *VTy = cast<FixedVectorType>(ParamTys.front());
1298ac9a064cSDimitry Andric     unsigned ScalarSize = VTy->getScalarSizeInBits();
1299ac9a064cSDimitry Andric     // For scalar sizes >128 bits, we fall back to the generic cost estimate.
1300ac9a064cSDimitry Andric     if (ScalarSize > SystemZ::VectorBits)
1301ac9a064cSDimitry Andric       return -1;
1302ac9a064cSDimitry Andric     // This many vector regs are needed to represent the input elements (V).
1303ac9a064cSDimitry Andric     unsigned VectorRegsNeeded = getNumVectorRegs(VTy);
1304ac9a064cSDimitry Andric     // This many instructions are needed for the final sum of vector elems (S).
1305ac9a064cSDimitry Andric     unsigned LastVectorHandling = (ScalarSize < 32) ? 3 : 2;
1306ac9a064cSDimitry Andric     // We use vector adds to create a sum vector, which takes
1307ac9a064cSDimitry Andric     // V/2 + V/4 + ... = V - 1 operations.
1308ac9a064cSDimitry Andric     // Then, we need S operations to sum up the elements of that sum vector,
1309ac9a064cSDimitry Andric     // for a total of V + S - 1 operations.
1310ac9a064cSDimitry Andric     int Cost = VectorRegsNeeded + LastVectorHandling - 1;
1311ac9a064cSDimitry Andric     return Cost;
1312ac9a064cSDimitry Andric   }
1313d8e91e46SDimitry Andric   return -1;
1314d8e91e46SDimitry Andric }
1315d8e91e46SDimitry Andric 
1316344a3780SDimitry Andric InstructionCost
getIntrinsicInstrCost(const IntrinsicCostAttributes & ICA,TTI::TargetCostKind CostKind)1317344a3780SDimitry Andric SystemZTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
1318cfca06d7SDimitry Andric                                       TTI::TargetCostKind CostKind) {
1319ac9a064cSDimitry Andric   InstructionCost Cost = getVectorIntrinsicInstrCost(
1320ac9a064cSDimitry Andric       ICA.getID(), ICA.getReturnType(), ICA.getArgTypes());
1321d8e91e46SDimitry Andric   if (Cost != -1)
1322d8e91e46SDimitry Andric     return Cost;
1323cfca06d7SDimitry Andric   return BaseT::getIntrinsicInstrCost(ICA, CostKind);
132471d5a254SDimitry Andric }
1325ac9a064cSDimitry Andric 
shouldExpandReduction(const IntrinsicInst * II) const1326ac9a064cSDimitry Andric bool SystemZTTIImpl::shouldExpandReduction(const IntrinsicInst *II) const {
1327ac9a064cSDimitry Andric   // Always expand on Subtargets without vector instructions.
1328ac9a064cSDimitry Andric   if (!ST->hasVector())
1329ac9a064cSDimitry Andric     return true;
1330ac9a064cSDimitry Andric 
1331ac9a064cSDimitry Andric   // Whether or not to expand is a per-intrinsic decision.
1332ac9a064cSDimitry Andric   switch (II->getIntrinsicID()) {
1333ac9a064cSDimitry Andric   default:
1334ac9a064cSDimitry Andric     return true;
1335ac9a064cSDimitry Andric   // Do not expand vector.reduce.add...
1336ac9a064cSDimitry Andric   case Intrinsic::vector_reduce_add:
1337ac9a064cSDimitry Andric     auto *VType = cast<FixedVectorType>(II->getOperand(0)->getType());
1338ac9a064cSDimitry Andric     // ...unless the scalar size is i64 or larger,
1339ac9a064cSDimitry Andric     // or the operand vector is not full, since the
1340ac9a064cSDimitry Andric     // performance benefit is dubious in those cases.
1341ac9a064cSDimitry Andric     return VType->getScalarSizeInBits() >= 64 ||
1342ac9a064cSDimitry Andric            VType->getPrimitiveSizeInBits() < SystemZ::VectorBits;
1343ac9a064cSDimitry Andric   }
1344ac9a064cSDimitry Andric }
1345