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"
210b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
220b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
230b57cec5SDimitry Andric using namespace llvm;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric #define DEBUG_TYPE "systemztti"
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
280b57cec5SDimitry Andric //
290b57cec5SDimitry Andric // SystemZ cost model.
300b57cec5SDimitry Andric //
310b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
320b57cec5SDimitry Andric 
getIntImmCost(const APInt & Imm,Type * Ty,TTI::TargetCostKind CostKind)33*5f7ddb14SDimitry Andric InstructionCost SystemZTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty,
345ffd83dbSDimitry Andric                                               TTI::TargetCostKind CostKind) {
350b57cec5SDimitry Andric   assert(Ty->isIntegerTy());
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric   unsigned BitSize = Ty->getPrimitiveSizeInBits();
380b57cec5SDimitry Andric   // There is no cost model for constants with a bit size of 0. Return TCC_Free
390b57cec5SDimitry Andric   // here, so that constant hoisting will ignore this constant.
400b57cec5SDimitry Andric   if (BitSize == 0)
410b57cec5SDimitry Andric     return TTI::TCC_Free;
420b57cec5SDimitry Andric   // No cost model for operations on integers larger than 64 bit implemented yet.
430b57cec5SDimitry Andric   if (BitSize > 64)
440b57cec5SDimitry Andric     return TTI::TCC_Free;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   if (Imm == 0)
470b57cec5SDimitry Andric     return TTI::TCC_Free;
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   if (Imm.getBitWidth() <= 64) {
500b57cec5SDimitry Andric     // Constants loaded via lgfi.
510b57cec5SDimitry Andric     if (isInt<32>(Imm.getSExtValue()))
520b57cec5SDimitry Andric       return TTI::TCC_Basic;
530b57cec5SDimitry Andric     // Constants loaded via llilf.
540b57cec5SDimitry Andric     if (isUInt<32>(Imm.getZExtValue()))
550b57cec5SDimitry Andric       return TTI::TCC_Basic;
560b57cec5SDimitry Andric     // Constants loaded via llihf:
570b57cec5SDimitry Andric     if ((Imm.getZExtValue() & 0xffffffff) == 0)
580b57cec5SDimitry Andric       return TTI::TCC_Basic;
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric     return 2 * TTI::TCC_Basic;
610b57cec5SDimitry Andric   }
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   return 4 * TTI::TCC_Basic;
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
getIntImmCostInst(unsigned Opcode,unsigned Idx,const APInt & Imm,Type * Ty,TTI::TargetCostKind CostKind,Instruction * Inst)66*5f7ddb14SDimitry Andric InstructionCost SystemZTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
675ffd83dbSDimitry Andric                                                   const APInt &Imm, Type *Ty,
68af732203SDimitry Andric                                                   TTI::TargetCostKind CostKind,
69af732203SDimitry Andric                                                   Instruction *Inst) {
700b57cec5SDimitry Andric   assert(Ty->isIntegerTy());
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   unsigned BitSize = Ty->getPrimitiveSizeInBits();
730b57cec5SDimitry Andric   // There is no cost model for constants with a bit size of 0. Return TCC_Free
740b57cec5SDimitry Andric   // here, so that constant hoisting will ignore this constant.
750b57cec5SDimitry Andric   if (BitSize == 0)
760b57cec5SDimitry Andric     return TTI::TCC_Free;
770b57cec5SDimitry Andric   // No cost model for operations on integers larger than 64 bit implemented yet.
780b57cec5SDimitry Andric   if (BitSize > 64)
790b57cec5SDimitry Andric     return TTI::TCC_Free;
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   switch (Opcode) {
820b57cec5SDimitry Andric   default:
830b57cec5SDimitry Andric     return TTI::TCC_Free;
840b57cec5SDimitry Andric   case Instruction::GetElementPtr:
850b57cec5SDimitry Andric     // Always hoist the base address of a GetElementPtr. This prevents the
860b57cec5SDimitry Andric     // creation of new constants for every base constant that gets constant
870b57cec5SDimitry Andric     // folded with the offset.
880b57cec5SDimitry Andric     if (Idx == 0)
890b57cec5SDimitry Andric       return 2 * TTI::TCC_Basic;
900b57cec5SDimitry Andric     return TTI::TCC_Free;
910b57cec5SDimitry Andric   case Instruction::Store:
920b57cec5SDimitry Andric     if (Idx == 0 && Imm.getBitWidth() <= 64) {
930b57cec5SDimitry Andric       // Any 8-bit immediate store can by implemented via mvi.
940b57cec5SDimitry Andric       if (BitSize == 8)
950b57cec5SDimitry Andric         return TTI::TCC_Free;
960b57cec5SDimitry Andric       // 16-bit immediate values can be stored via mvhhi/mvhi/mvghi.
970b57cec5SDimitry Andric       if (isInt<16>(Imm.getSExtValue()))
980b57cec5SDimitry Andric         return TTI::TCC_Free;
990b57cec5SDimitry Andric     }
1000b57cec5SDimitry Andric     break;
1010b57cec5SDimitry Andric   case Instruction::ICmp:
1020b57cec5SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
1030b57cec5SDimitry Andric       // Comparisons against signed 32-bit immediates implemented via cgfi.
1040b57cec5SDimitry Andric       if (isInt<32>(Imm.getSExtValue()))
1050b57cec5SDimitry Andric         return TTI::TCC_Free;
1060b57cec5SDimitry Andric       // Comparisons against unsigned 32-bit immediates implemented via clgfi.
1070b57cec5SDimitry Andric       if (isUInt<32>(Imm.getZExtValue()))
1080b57cec5SDimitry Andric         return TTI::TCC_Free;
1090b57cec5SDimitry Andric     }
1100b57cec5SDimitry Andric     break;
1110b57cec5SDimitry Andric   case Instruction::Add:
1120b57cec5SDimitry Andric   case Instruction::Sub:
1130b57cec5SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
1140b57cec5SDimitry Andric       // We use algfi/slgfi to add/subtract 32-bit unsigned immediates.
1150b57cec5SDimitry Andric       if (isUInt<32>(Imm.getZExtValue()))
1160b57cec5SDimitry Andric         return TTI::TCC_Free;
1170b57cec5SDimitry Andric       // Or their negation, by swapping addition vs. subtraction.
1180b57cec5SDimitry Andric       if (isUInt<32>(-Imm.getSExtValue()))
1190b57cec5SDimitry Andric         return TTI::TCC_Free;
1200b57cec5SDimitry Andric     }
1210b57cec5SDimitry Andric     break;
1220b57cec5SDimitry Andric   case Instruction::Mul:
1230b57cec5SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
1240b57cec5SDimitry Andric       // We use msgfi to multiply by 32-bit signed immediates.
1250b57cec5SDimitry Andric       if (isInt<32>(Imm.getSExtValue()))
1260b57cec5SDimitry Andric         return TTI::TCC_Free;
1270b57cec5SDimitry Andric     }
1280b57cec5SDimitry Andric     break;
1290b57cec5SDimitry Andric   case Instruction::Or:
1300b57cec5SDimitry Andric   case Instruction::Xor:
1310b57cec5SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
1320b57cec5SDimitry Andric       // Masks supported by oilf/xilf.
1330b57cec5SDimitry Andric       if (isUInt<32>(Imm.getZExtValue()))
1340b57cec5SDimitry Andric         return TTI::TCC_Free;
1350b57cec5SDimitry Andric       // Masks supported by oihf/xihf.
1360b57cec5SDimitry Andric       if ((Imm.getZExtValue() & 0xffffffff) == 0)
1370b57cec5SDimitry Andric         return TTI::TCC_Free;
1380b57cec5SDimitry Andric     }
1390b57cec5SDimitry Andric     break;
1400b57cec5SDimitry Andric   case Instruction::And:
1410b57cec5SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
1420b57cec5SDimitry Andric       // Any 32-bit AND operation can by implemented via nilf.
1430b57cec5SDimitry Andric       if (BitSize <= 32)
1440b57cec5SDimitry Andric         return TTI::TCC_Free;
1450b57cec5SDimitry Andric       // 64-bit masks supported by nilf.
1460b57cec5SDimitry Andric       if (isUInt<32>(~Imm.getZExtValue()))
1470b57cec5SDimitry Andric         return TTI::TCC_Free;
1480b57cec5SDimitry Andric       // 64-bit masks supported by nilh.
1490b57cec5SDimitry Andric       if ((Imm.getZExtValue() & 0xffffffff) == 0xffffffff)
1500b57cec5SDimitry Andric         return TTI::TCC_Free;
1510b57cec5SDimitry Andric       // Some 64-bit AND operations can be implemented via risbg.
1520b57cec5SDimitry Andric       const SystemZInstrInfo *TII = ST->getInstrInfo();
1530b57cec5SDimitry Andric       unsigned Start, End;
1540b57cec5SDimitry Andric       if (TII->isRxSBGMask(Imm.getZExtValue(), BitSize, Start, End))
1550b57cec5SDimitry Andric         return TTI::TCC_Free;
1560b57cec5SDimitry Andric     }
1570b57cec5SDimitry Andric     break;
1580b57cec5SDimitry Andric   case Instruction::Shl:
1590b57cec5SDimitry Andric   case Instruction::LShr:
1600b57cec5SDimitry Andric   case Instruction::AShr:
1610b57cec5SDimitry Andric     // Always return TCC_Free for the shift value of a shift instruction.
1620b57cec5SDimitry Andric     if (Idx == 1)
1630b57cec5SDimitry Andric       return TTI::TCC_Free;
1640b57cec5SDimitry Andric     break;
1650b57cec5SDimitry Andric   case Instruction::UDiv:
1660b57cec5SDimitry Andric   case Instruction::SDiv:
1670b57cec5SDimitry Andric   case Instruction::URem:
1680b57cec5SDimitry Andric   case Instruction::SRem:
1690b57cec5SDimitry Andric   case Instruction::Trunc:
1700b57cec5SDimitry Andric   case Instruction::ZExt:
1710b57cec5SDimitry Andric   case Instruction::SExt:
1720b57cec5SDimitry Andric   case Instruction::IntToPtr:
1730b57cec5SDimitry Andric   case Instruction::PtrToInt:
1740b57cec5SDimitry Andric   case Instruction::BitCast:
1750b57cec5SDimitry Andric   case Instruction::PHI:
1760b57cec5SDimitry Andric   case Instruction::Call:
1770b57cec5SDimitry Andric   case Instruction::Select:
1780b57cec5SDimitry Andric   case Instruction::Ret:
1790b57cec5SDimitry Andric   case Instruction::Load:
1800b57cec5SDimitry Andric     break;
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric 
1835ffd83dbSDimitry Andric   return SystemZTTIImpl::getIntImmCost(Imm, Ty, CostKind);
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric 
186*5f7ddb14SDimitry Andric InstructionCost
getIntImmCostIntrin(Intrinsic::ID IID,unsigned Idx,const APInt & Imm,Type * Ty,TTI::TargetCostKind CostKind)187*5f7ddb14SDimitry Andric SystemZTTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
1885ffd83dbSDimitry Andric                                     const APInt &Imm, Type *Ty,
1895ffd83dbSDimitry Andric                                     TTI::TargetCostKind CostKind) {
1900b57cec5SDimitry Andric   assert(Ty->isIntegerTy());
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   unsigned BitSize = Ty->getPrimitiveSizeInBits();
1930b57cec5SDimitry Andric   // There is no cost model for constants with a bit size of 0. Return TCC_Free
1940b57cec5SDimitry Andric   // here, so that constant hoisting will ignore this constant.
1950b57cec5SDimitry Andric   if (BitSize == 0)
1960b57cec5SDimitry Andric     return TTI::TCC_Free;
1970b57cec5SDimitry Andric   // No cost model for operations on integers larger than 64 bit implemented yet.
1980b57cec5SDimitry Andric   if (BitSize > 64)
1990b57cec5SDimitry Andric     return TTI::TCC_Free;
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   switch (IID) {
2020b57cec5SDimitry Andric   default:
2030b57cec5SDimitry Andric     return TTI::TCC_Free;
2040b57cec5SDimitry Andric   case Intrinsic::sadd_with_overflow:
2050b57cec5SDimitry Andric   case Intrinsic::uadd_with_overflow:
2060b57cec5SDimitry Andric   case Intrinsic::ssub_with_overflow:
2070b57cec5SDimitry Andric   case Intrinsic::usub_with_overflow:
2080b57cec5SDimitry Andric     // These get expanded to include a normal addition/subtraction.
2090b57cec5SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
2100b57cec5SDimitry Andric       if (isUInt<32>(Imm.getZExtValue()))
2110b57cec5SDimitry Andric         return TTI::TCC_Free;
2120b57cec5SDimitry Andric       if (isUInt<32>(-Imm.getSExtValue()))
2130b57cec5SDimitry Andric         return TTI::TCC_Free;
2140b57cec5SDimitry Andric     }
2150b57cec5SDimitry Andric     break;
2160b57cec5SDimitry Andric   case Intrinsic::smul_with_overflow:
2170b57cec5SDimitry Andric   case Intrinsic::umul_with_overflow:
2180b57cec5SDimitry Andric     // These get expanded to include a normal multiplication.
2190b57cec5SDimitry Andric     if (Idx == 1 && Imm.getBitWidth() <= 64) {
2200b57cec5SDimitry Andric       if (isInt<32>(Imm.getSExtValue()))
2210b57cec5SDimitry Andric         return TTI::TCC_Free;
2220b57cec5SDimitry Andric     }
2230b57cec5SDimitry Andric     break;
2240b57cec5SDimitry Andric   case Intrinsic::experimental_stackmap:
2250b57cec5SDimitry Andric     if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
2260b57cec5SDimitry Andric       return TTI::TCC_Free;
2270b57cec5SDimitry Andric     break;
2280b57cec5SDimitry Andric   case Intrinsic::experimental_patchpoint_void:
2290b57cec5SDimitry Andric   case Intrinsic::experimental_patchpoint_i64:
2300b57cec5SDimitry Andric     if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
2310b57cec5SDimitry Andric       return TTI::TCC_Free;
2320b57cec5SDimitry Andric     break;
2330b57cec5SDimitry Andric   }
2345ffd83dbSDimitry Andric   return SystemZTTIImpl::getIntImmCost(Imm, Ty, CostKind);
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric TargetTransformInfo::PopcntSupportKind
getPopcntSupport(unsigned TyWidth)2380b57cec5SDimitry Andric SystemZTTIImpl::getPopcntSupport(unsigned TyWidth) {
2390b57cec5SDimitry Andric   assert(isPowerOf2_32(TyWidth) && "Type width must be power of 2");
2400b57cec5SDimitry Andric   if (ST->hasPopulationCount() && TyWidth <= 64)
2410b57cec5SDimitry Andric     return TTI::PSK_FastHardware;
2420b57cec5SDimitry Andric   return TTI::PSK_Software;
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
getUnrollingPreferences(Loop * L,ScalarEvolution & SE,TTI::UnrollingPreferences & UP)2450b57cec5SDimitry Andric void SystemZTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
2460b57cec5SDimitry Andric                                              TTI::UnrollingPreferences &UP) {
2470b57cec5SDimitry Andric   // Find out if L contains a call, what the machine instruction count
2480b57cec5SDimitry Andric   // estimate is, and how many stores there are.
2490b57cec5SDimitry Andric   bool HasCall = false;
250*5f7ddb14SDimitry Andric   InstructionCost NumStores = 0;
2510b57cec5SDimitry Andric   for (auto &BB : L->blocks())
2520b57cec5SDimitry Andric     for (auto &I : *BB) {
2530b57cec5SDimitry Andric       if (isa<CallInst>(&I) || isa<InvokeInst>(&I)) {
2545ffd83dbSDimitry Andric         if (const Function *F = cast<CallBase>(I).getCalledFunction()) {
2550b57cec5SDimitry Andric           if (isLoweredToCall(F))
2560b57cec5SDimitry Andric             HasCall = true;
2570b57cec5SDimitry Andric           if (F->getIntrinsicID() == Intrinsic::memcpy ||
2580b57cec5SDimitry Andric               F->getIntrinsicID() == Intrinsic::memset)
2590b57cec5SDimitry Andric             NumStores++;
2600b57cec5SDimitry Andric         } else { // indirect call.
2610b57cec5SDimitry Andric           HasCall = true;
2620b57cec5SDimitry Andric         }
2630b57cec5SDimitry Andric       }
2640b57cec5SDimitry Andric       if (isa<StoreInst>(&I)) {
2650b57cec5SDimitry Andric         Type *MemAccessTy = I.getOperand(0)->getType();
2665ffd83dbSDimitry Andric         NumStores += getMemoryOpCost(Instruction::Store, MemAccessTy, None, 0,
2675ffd83dbSDimitry Andric                                      TTI::TCK_RecipThroughput);
2680b57cec5SDimitry Andric       }
2690b57cec5SDimitry Andric     }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   // The z13 processor will run out of store tags if too many stores
2720b57cec5SDimitry Andric   // are fed into it too quickly. Therefore make sure there are not
2730b57cec5SDimitry Andric   // too many stores in the resulting unrolled loop.
274*5f7ddb14SDimitry Andric   unsigned const NumStoresVal = *NumStores.getValue();
275*5f7ddb14SDimitry Andric   unsigned const Max = (NumStoresVal ? (12 / NumStoresVal) : UINT_MAX);
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   if (HasCall) {
2780b57cec5SDimitry Andric     // Only allow full unrolling if loop has any calls.
2790b57cec5SDimitry Andric     UP.FullUnrollMaxCount = Max;
2800b57cec5SDimitry Andric     UP.MaxCount = 1;
2810b57cec5SDimitry Andric     return;
2820b57cec5SDimitry Andric   }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   UP.MaxCount = Max;
2850b57cec5SDimitry Andric   if (UP.MaxCount <= 1)
2860b57cec5SDimitry Andric     return;
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   // Allow partial and runtime trip count unrolling.
2890b57cec5SDimitry Andric   UP.Partial = UP.Runtime = true;
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   UP.PartialThreshold = 75;
2920b57cec5SDimitry Andric   UP.DefaultUnrollRuntimeCount = 4;
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric   // Allow expensive instructions in the pre-header of the loop.
2950b57cec5SDimitry Andric   UP.AllowExpensiveTripCount = true;
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric   UP.Force = true;
2980b57cec5SDimitry Andric }
2990b57cec5SDimitry Andric 
getPeelingPreferences(Loop * L,ScalarEvolution & SE,TTI::PeelingPreferences & PP)3005ffd83dbSDimitry Andric void SystemZTTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE,
3015ffd83dbSDimitry Andric                                            TTI::PeelingPreferences &PP) {
3025ffd83dbSDimitry Andric   BaseT::getPeelingPreferences(L, SE, PP);
3035ffd83dbSDimitry Andric }
3040b57cec5SDimitry Andric 
isLSRCostLess(TargetTransformInfo::LSRCost & C1,TargetTransformInfo::LSRCost & C2)3050b57cec5SDimitry Andric bool SystemZTTIImpl::isLSRCostLess(TargetTransformInfo::LSRCost &C1,
3060b57cec5SDimitry Andric                                    TargetTransformInfo::LSRCost &C2) {
3070b57cec5SDimitry Andric   // SystemZ specific: check instruction count (first), and don't care about
3080b57cec5SDimitry Andric   // ImmCost, since offsets are checked explicitly.
3090b57cec5SDimitry Andric   return std::tie(C1.Insns, C1.NumRegs, C1.AddRecCost,
3100b57cec5SDimitry Andric                   C1.NumIVMuls, C1.NumBaseAdds,
3110b57cec5SDimitry Andric                   C1.ScaleCost, C1.SetupCost) <
3120b57cec5SDimitry Andric     std::tie(C2.Insns, C2.NumRegs, C2.AddRecCost,
3130b57cec5SDimitry Andric              C2.NumIVMuls, C2.NumBaseAdds,
3140b57cec5SDimitry Andric              C2.ScaleCost, C2.SetupCost);
3150b57cec5SDimitry Andric }
3160b57cec5SDimitry Andric 
getNumberOfRegisters(unsigned ClassID) const3178bcb0991SDimitry Andric unsigned SystemZTTIImpl::getNumberOfRegisters(unsigned ClassID) const {
3188bcb0991SDimitry Andric   bool Vector = (ClassID == 1);
3190b57cec5SDimitry Andric   if (!Vector)
3200b57cec5SDimitry Andric     // Discount the stack pointer.  Also leave out %r0, since it can't
3210b57cec5SDimitry Andric     // be used in an address.
3220b57cec5SDimitry Andric     return 14;
3230b57cec5SDimitry Andric   if (ST->hasVector())
3240b57cec5SDimitry Andric     return 32;
3250b57cec5SDimitry Andric   return 0;
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric 
328*5f7ddb14SDimitry Andric TypeSize
getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const329*5f7ddb14SDimitry Andric SystemZTTIImpl::getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const {
330*5f7ddb14SDimitry Andric   switch (K) {
331*5f7ddb14SDimitry Andric   case TargetTransformInfo::RGK_Scalar:
332*5f7ddb14SDimitry Andric     return TypeSize::getFixed(64);
333*5f7ddb14SDimitry Andric   case TargetTransformInfo::RGK_FixedWidthVector:
334*5f7ddb14SDimitry Andric     return TypeSize::getFixed(ST->hasVector() ? 128 : 0);
335*5f7ddb14SDimitry Andric   case TargetTransformInfo::RGK_ScalableVector:
336*5f7ddb14SDimitry Andric     return TypeSize::getScalable(0);
337*5f7ddb14SDimitry Andric   }
338*5f7ddb14SDimitry Andric 
339*5f7ddb14SDimitry Andric   llvm_unreachable("Unsupported register kind");
3400b57cec5SDimitry Andric }
3410b57cec5SDimitry Andric 
getMinPrefetchStride(unsigned NumMemAccesses,unsigned NumStridedMemAccesses,unsigned NumPrefetches,bool HasCall) const3425ffd83dbSDimitry Andric unsigned SystemZTTIImpl::getMinPrefetchStride(unsigned NumMemAccesses,
3435ffd83dbSDimitry Andric                                               unsigned NumStridedMemAccesses,
3445ffd83dbSDimitry Andric                                               unsigned NumPrefetches,
3455ffd83dbSDimitry Andric                                               bool HasCall) const {
3465ffd83dbSDimitry Andric   // Don't prefetch a loop with many far apart accesses.
3475ffd83dbSDimitry Andric   if (NumPrefetches > 16)
3485ffd83dbSDimitry Andric     return UINT_MAX;
3495ffd83dbSDimitry Andric 
3505ffd83dbSDimitry Andric   // Emit prefetch instructions for smaller strides in cases where we think
3515ffd83dbSDimitry Andric   // the hardware prefetcher might not be able to keep up.
352af732203SDimitry Andric   if (NumStridedMemAccesses > 32 && !HasCall &&
353af732203SDimitry Andric       (NumMemAccesses - NumStridedMemAccesses) * 32 <= NumStridedMemAccesses)
3545ffd83dbSDimitry Andric     return 1;
3555ffd83dbSDimitry Andric 
3565ffd83dbSDimitry Andric   return ST->hasMiscellaneousExtensions3() ? 8192 : 2048;
3575ffd83dbSDimitry Andric }
3585ffd83dbSDimitry Andric 
hasDivRemOp(Type * DataType,bool IsSigned)3590b57cec5SDimitry Andric bool SystemZTTIImpl::hasDivRemOp(Type *DataType, bool IsSigned) {
3600b57cec5SDimitry Andric   EVT VT = TLI->getValueType(DL, DataType);
3610b57cec5SDimitry Andric   return (VT.isScalarInteger() && TLI->isTypeLegal(VT));
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric // Return the bit size for the scalar type or vector element
3650b57cec5SDimitry Andric // type. getScalarSizeInBits() returns 0 for a pointer type.
getScalarSizeInBits(Type * Ty)3660b57cec5SDimitry Andric static unsigned getScalarSizeInBits(Type *Ty) {
3670b57cec5SDimitry Andric   unsigned Size =
3680b57cec5SDimitry Andric     (Ty->isPtrOrPtrVectorTy() ? 64U : Ty->getScalarSizeInBits());
3690b57cec5SDimitry Andric   assert(Size > 0 && "Element must have non-zero size.");
3700b57cec5SDimitry Andric   return Size;
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric // getNumberOfParts() calls getTypeLegalizationCost() which splits the vector
3740b57cec5SDimitry Andric // type until it is legal. This would e.g. return 4 for <6 x i64>, instead of
3750b57cec5SDimitry Andric // 3.
getNumVectorRegs(Type * Ty)3760b57cec5SDimitry Andric static unsigned getNumVectorRegs(Type *Ty) {
3775ffd83dbSDimitry Andric   auto *VTy = cast<FixedVectorType>(Ty);
3785ffd83dbSDimitry Andric   unsigned WideBits = getScalarSizeInBits(Ty) * VTy->getNumElements();
3790b57cec5SDimitry Andric   assert(WideBits > 0 && "Could not compute size of vector");
3800b57cec5SDimitry Andric   return ((WideBits % 128U) ? ((WideBits / 128U) + 1) : (WideBits / 128U));
3810b57cec5SDimitry Andric }
3820b57cec5SDimitry Andric 
getArithmeticInstrCost(unsigned Opcode,Type * Ty,TTI::TargetCostKind CostKind,TTI::OperandValueKind Op1Info,TTI::OperandValueKind Op2Info,TTI::OperandValueProperties Opd1PropInfo,TTI::OperandValueProperties Opd2PropInfo,ArrayRef<const Value * > Args,const Instruction * CxtI)383*5f7ddb14SDimitry Andric InstructionCost SystemZTTIImpl::getArithmeticInstrCost(
3845ffd83dbSDimitry Andric     unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
385*5f7ddb14SDimitry Andric     TTI::OperandValueKind Op1Info, TTI::OperandValueKind Op2Info,
386*5f7ddb14SDimitry Andric     TTI::OperandValueProperties Opd1PropInfo,
387480093f4SDimitry Andric     TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args,
388480093f4SDimitry Andric     const Instruction *CxtI) {
3890b57cec5SDimitry Andric 
3905ffd83dbSDimitry Andric   // TODO: Handle more cost kinds.
3915ffd83dbSDimitry Andric   if (CostKind != TTI::TCK_RecipThroughput)
3925ffd83dbSDimitry Andric     return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info,
3935ffd83dbSDimitry Andric                                          Op2Info, Opd1PropInfo,
3945ffd83dbSDimitry Andric                                          Opd2PropInfo, Args, CxtI);
3955ffd83dbSDimitry Andric 
3960b57cec5SDimitry Andric   // TODO: return a good value for BB-VECTORIZER that includes the
3970b57cec5SDimitry Andric   // immediate loads, which we do not want to count for the loop
3980b57cec5SDimitry Andric   // vectorizer, since they are hopefully hoisted out of the loop. This
3990b57cec5SDimitry Andric   // would require a new parameter 'InLoop', but not sure if constant
4000b57cec5SDimitry Andric   // args are common enough to motivate this.
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric   unsigned ScalarBits = Ty->getScalarSizeInBits();
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric   // There are thre cases of division and remainder: Dividing with a register
4050b57cec5SDimitry Andric   // needs a divide instruction. A divisor which is a power of two constant
4060b57cec5SDimitry Andric   // can be implemented with a sequence of shifts. Any other constant needs a
4070b57cec5SDimitry Andric   // multiply and shifts.
4080b57cec5SDimitry Andric   const unsigned DivInstrCost = 20;
4090b57cec5SDimitry Andric   const unsigned DivMulSeqCost = 10;
4100b57cec5SDimitry Andric   const unsigned SDivPow2Cost = 4;
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric   bool SignedDivRem =
4130b57cec5SDimitry Andric       Opcode == Instruction::SDiv || Opcode == Instruction::SRem;
4140b57cec5SDimitry Andric   bool UnsignedDivRem =
4150b57cec5SDimitry Andric       Opcode == Instruction::UDiv || Opcode == Instruction::URem;
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric   // Check for a constant divisor.
4180b57cec5SDimitry Andric   bool DivRemConst = false;
4190b57cec5SDimitry Andric   bool DivRemConstPow2 = false;
4200b57cec5SDimitry Andric   if ((SignedDivRem || UnsignedDivRem) && Args.size() == 2) {
4210b57cec5SDimitry Andric     if (const Constant *C = dyn_cast<Constant>(Args[1])) {
4220b57cec5SDimitry Andric       const ConstantInt *CVal =
4230b57cec5SDimitry Andric           (C->getType()->isVectorTy()
4240b57cec5SDimitry Andric                ? dyn_cast_or_null<const ConstantInt>(C->getSplatValue())
4250b57cec5SDimitry Andric                : dyn_cast<const ConstantInt>(C));
4260b57cec5SDimitry Andric       if (CVal != nullptr &&
4270b57cec5SDimitry Andric           (CVal->getValue().isPowerOf2() || (-CVal->getValue()).isPowerOf2()))
4280b57cec5SDimitry Andric         DivRemConstPow2 = true;
4290b57cec5SDimitry Andric       else
4300b57cec5SDimitry Andric         DivRemConst = true;
4310b57cec5SDimitry Andric     }
4320b57cec5SDimitry Andric   }
4330b57cec5SDimitry Andric 
4345ffd83dbSDimitry Andric   if (!Ty->isVectorTy()) {
4350b57cec5SDimitry Andric     // These FP operations are supported with a dedicated instruction for
4360b57cec5SDimitry Andric     // float, double and fp128 (base implementation assumes float generally
4370b57cec5SDimitry Andric     // costs 2).
4380b57cec5SDimitry Andric     if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub ||
4390b57cec5SDimitry Andric         Opcode == Instruction::FMul || Opcode == Instruction::FDiv)
4400b57cec5SDimitry Andric       return 1;
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric     // There is no native support for FRem.
4430b57cec5SDimitry Andric     if (Opcode == Instruction::FRem)
4440b57cec5SDimitry Andric       return LIBCALL_COST;
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric     // Give discount for some combined logical operations if supported.
4470b57cec5SDimitry Andric     if (Args.size() == 2 && ST->hasMiscellaneousExtensions3()) {
4480b57cec5SDimitry Andric       if (Opcode == Instruction::Xor) {
4490b57cec5SDimitry Andric         for (const Value *A : Args) {
4500b57cec5SDimitry Andric           if (const Instruction *I = dyn_cast<Instruction>(A))
4510b57cec5SDimitry Andric             if (I->hasOneUse() &&
4520b57cec5SDimitry Andric                 (I->getOpcode() == Instruction::And ||
4530b57cec5SDimitry Andric                  I->getOpcode() == Instruction::Or ||
4540b57cec5SDimitry Andric                  I->getOpcode() == Instruction::Xor))
4550b57cec5SDimitry Andric               return 0;
4560b57cec5SDimitry Andric         }
4570b57cec5SDimitry Andric       }
4580b57cec5SDimitry Andric       else if (Opcode == Instruction::Or || Opcode == Instruction::And) {
4590b57cec5SDimitry Andric         for (const Value *A : Args) {
4600b57cec5SDimitry Andric           if (const Instruction *I = dyn_cast<Instruction>(A))
4610b57cec5SDimitry Andric             if (I->hasOneUse() && I->getOpcode() == Instruction::Xor)
4620b57cec5SDimitry Andric               return 0;
4630b57cec5SDimitry Andric         }
4640b57cec5SDimitry Andric       }
4650b57cec5SDimitry Andric     }
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric     // Or requires one instruction, although it has custom handling for i64.
4680b57cec5SDimitry Andric     if (Opcode == Instruction::Or)
4690b57cec5SDimitry Andric       return 1;
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric     if (Opcode == Instruction::Xor && ScalarBits == 1) {
4720b57cec5SDimitry Andric       if (ST->hasLoadStoreOnCond2())
4730b57cec5SDimitry Andric         return 5; // 2 * (li 0; loc 1); xor
4740b57cec5SDimitry Andric       return 7; // 2 * ipm sequences ; xor ; shift ; compare
4750b57cec5SDimitry Andric     }
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric     if (DivRemConstPow2)
4780b57cec5SDimitry Andric       return (SignedDivRem ? SDivPow2Cost : 1);
4790b57cec5SDimitry Andric     if (DivRemConst)
4800b57cec5SDimitry Andric       return DivMulSeqCost;
4810b57cec5SDimitry Andric     if (SignedDivRem || UnsignedDivRem)
4820b57cec5SDimitry Andric       return DivInstrCost;
4830b57cec5SDimitry Andric   }
4845ffd83dbSDimitry Andric   else if (ST->hasVector()) {
4855ffd83dbSDimitry Andric     auto *VTy = cast<FixedVectorType>(Ty);
4865ffd83dbSDimitry Andric     unsigned VF = VTy->getNumElements();
4875ffd83dbSDimitry Andric     unsigned NumVectors = getNumVectorRegs(Ty);
4885ffd83dbSDimitry Andric 
4895ffd83dbSDimitry Andric     // These vector operations are custom handled, but are still supported
4905ffd83dbSDimitry Andric     // with one instruction per vector, regardless of element size.
4915ffd83dbSDimitry Andric     if (Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
4925ffd83dbSDimitry Andric         Opcode == Instruction::AShr) {
4935ffd83dbSDimitry Andric       return NumVectors;
4945ffd83dbSDimitry Andric     }
4955ffd83dbSDimitry Andric 
4965ffd83dbSDimitry Andric     if (DivRemConstPow2)
4975ffd83dbSDimitry Andric       return (NumVectors * (SignedDivRem ? SDivPow2Cost : 1));
498*5f7ddb14SDimitry Andric     if (DivRemConst) {
499*5f7ddb14SDimitry Andric       SmallVector<Type *> Tys(Args.size(), Ty);
500*5f7ddb14SDimitry Andric       return VF * DivMulSeqCost + getScalarizationOverhead(VTy, Args, Tys);
501*5f7ddb14SDimitry Andric     }
5025ffd83dbSDimitry Andric     if ((SignedDivRem || UnsignedDivRem) && VF > 4)
5035ffd83dbSDimitry Andric       // Temporary hack: disable high vectorization factors with integer
5045ffd83dbSDimitry Andric       // division/remainder, which will get scalarized and handled with
5055ffd83dbSDimitry Andric       // GR128 registers. The mischeduler is not clever enough to avoid
5065ffd83dbSDimitry Andric       // spilling yet.
5075ffd83dbSDimitry Andric       return 1000;
5085ffd83dbSDimitry Andric 
5095ffd83dbSDimitry Andric     // These FP operations are supported with a single vector instruction for
5105ffd83dbSDimitry Andric     // double (base implementation assumes float generally costs 2). For
5115ffd83dbSDimitry Andric     // FP128, the scalar cost is 1, and there is no overhead since the values
5125ffd83dbSDimitry Andric     // are already in scalar registers.
5135ffd83dbSDimitry Andric     if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub ||
5145ffd83dbSDimitry Andric         Opcode == Instruction::FMul || Opcode == Instruction::FDiv) {
5155ffd83dbSDimitry Andric       switch (ScalarBits) {
5165ffd83dbSDimitry Andric       case 32: {
5175ffd83dbSDimitry Andric         // The vector enhancements facility 1 provides v4f32 instructions.
5185ffd83dbSDimitry Andric         if (ST->hasVectorEnhancements1())
5195ffd83dbSDimitry Andric           return NumVectors;
5205ffd83dbSDimitry Andric         // Return the cost of multiple scalar invocation plus the cost of
5215ffd83dbSDimitry Andric         // inserting and extracting the values.
522*5f7ddb14SDimitry Andric         InstructionCost ScalarCost =
5235ffd83dbSDimitry Andric             getArithmeticInstrCost(Opcode, Ty->getScalarType(), CostKind);
524*5f7ddb14SDimitry Andric         SmallVector<Type *> Tys(Args.size(), Ty);
525*5f7ddb14SDimitry Andric         InstructionCost Cost =
526*5f7ddb14SDimitry Andric             (VF * ScalarCost) + getScalarizationOverhead(VTy, Args, Tys);
5275ffd83dbSDimitry Andric         // FIXME: VF 2 for these FP operations are currently just as
5285ffd83dbSDimitry Andric         // expensive as for VF 4.
5295ffd83dbSDimitry Andric         if (VF == 2)
5305ffd83dbSDimitry Andric           Cost *= 2;
5315ffd83dbSDimitry Andric         return Cost;
5325ffd83dbSDimitry Andric       }
5335ffd83dbSDimitry Andric       case 64:
5345ffd83dbSDimitry Andric       case 128:
5355ffd83dbSDimitry Andric         return NumVectors;
5365ffd83dbSDimitry Andric       default:
5375ffd83dbSDimitry Andric         break;
5385ffd83dbSDimitry Andric       }
5395ffd83dbSDimitry Andric     }
5405ffd83dbSDimitry Andric 
5415ffd83dbSDimitry Andric     // There is no native support for FRem.
5425ffd83dbSDimitry Andric     if (Opcode == Instruction::FRem) {
543*5f7ddb14SDimitry Andric       SmallVector<Type *> Tys(Args.size(), Ty);
544*5f7ddb14SDimitry Andric       InstructionCost Cost =
545*5f7ddb14SDimitry Andric           (VF * LIBCALL_COST) + getScalarizationOverhead(VTy, Args, Tys);
5465ffd83dbSDimitry Andric       // FIXME: VF 2 for float is currently just as expensive as for VF 4.
5475ffd83dbSDimitry Andric       if (VF == 2 && ScalarBits == 32)
5485ffd83dbSDimitry Andric         Cost *= 2;
5495ffd83dbSDimitry Andric       return Cost;
5505ffd83dbSDimitry Andric     }
5515ffd83dbSDimitry Andric   }
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric   // Fallback to the default implementation.
5545ffd83dbSDimitry Andric   return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info, Op2Info,
555480093f4SDimitry Andric                                        Opd1PropInfo, Opd2PropInfo, Args, CxtI);
5560b57cec5SDimitry Andric }
5570b57cec5SDimitry Andric 
getShuffleCost(TTI::ShuffleKind Kind,VectorType * Tp,ArrayRef<int> Mask,int Index,VectorType * SubTp)558*5f7ddb14SDimitry Andric InstructionCost SystemZTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
559*5f7ddb14SDimitry Andric                                                VectorType *Tp,
560*5f7ddb14SDimitry Andric                                                ArrayRef<int> Mask, int Index,
561*5f7ddb14SDimitry Andric                                                VectorType *SubTp) {
562*5f7ddb14SDimitry Andric   Kind = improveShuffleKindFromMask(Kind, Mask);
5635ffd83dbSDimitry Andric   if (ST->hasVector()) {
5640b57cec5SDimitry Andric     unsigned NumVectors = getNumVectorRegs(Tp);
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric     // TODO: Since fp32 is expanded, the shuffle cost should always be 0.
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric     // FP128 values are always in scalar registers, so there is no work
5690b57cec5SDimitry Andric     // involved with a shuffle, except for broadcast. In that case register
5700b57cec5SDimitry Andric     // moves are done with a single instruction per element.
5710b57cec5SDimitry Andric     if (Tp->getScalarType()->isFP128Ty())
5720b57cec5SDimitry Andric       return (Kind == TargetTransformInfo::SK_Broadcast ? NumVectors - 1 : 0);
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric     switch (Kind) {
5750b57cec5SDimitry Andric     case  TargetTransformInfo::SK_ExtractSubvector:
5760b57cec5SDimitry Andric       // ExtractSubvector Index indicates start offset.
5770b57cec5SDimitry Andric 
5780b57cec5SDimitry Andric       // Extracting a subvector from first index is a noop.
5790b57cec5SDimitry Andric       return (Index == 0 ? 0 : NumVectors);
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric     case TargetTransformInfo::SK_Broadcast:
5820b57cec5SDimitry Andric       // Loop vectorizer calls here to figure out the extra cost of
5830b57cec5SDimitry Andric       // broadcasting a loaded value to all elements of a vector. Since vlrep
5840b57cec5SDimitry Andric       // loads and replicates with a single instruction, adjust the returned
5850b57cec5SDimitry Andric       // value.
5860b57cec5SDimitry Andric       return NumVectors - 1;
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric     default:
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric       // SystemZ supports single instruction permutation / replication.
5910b57cec5SDimitry Andric       return NumVectors;
5920b57cec5SDimitry Andric     }
5935ffd83dbSDimitry Andric   }
5940b57cec5SDimitry Andric 
595*5f7ddb14SDimitry Andric   return BaseT::getShuffleCost(Kind, Tp, Mask, Index, SubTp);
5960b57cec5SDimitry Andric }
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric // Return the log2 difference of the element sizes of the two vector types.
getElSizeLog2Diff(Type * Ty0,Type * Ty1)5990b57cec5SDimitry Andric static unsigned getElSizeLog2Diff(Type *Ty0, Type *Ty1) {
6000b57cec5SDimitry Andric   unsigned Bits0 = Ty0->getScalarSizeInBits();
6010b57cec5SDimitry Andric   unsigned Bits1 = Ty1->getScalarSizeInBits();
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric   if (Bits1 >  Bits0)
6040b57cec5SDimitry Andric     return (Log2_32(Bits1) - Log2_32(Bits0));
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric   return (Log2_32(Bits0) - Log2_32(Bits1));
6070b57cec5SDimitry Andric }
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric // Return the number of instructions needed to truncate SrcTy to DstTy.
6100b57cec5SDimitry Andric unsigned SystemZTTIImpl::
getVectorTruncCost(Type * SrcTy,Type * DstTy)6110b57cec5SDimitry Andric getVectorTruncCost(Type *SrcTy, Type *DstTy) {
6120b57cec5SDimitry Andric   assert (SrcTy->isVectorTy() && DstTy->isVectorTy());
613af732203SDimitry Andric   assert(SrcTy->getPrimitiveSizeInBits().getFixedSize() >
614af732203SDimitry Andric              DstTy->getPrimitiveSizeInBits().getFixedSize() &&
6150b57cec5SDimitry Andric          "Packing must reduce size of vector type.");
6165ffd83dbSDimitry Andric   assert(cast<FixedVectorType>(SrcTy)->getNumElements() ==
6175ffd83dbSDimitry Andric              cast<FixedVectorType>(DstTy)->getNumElements() &&
6180b57cec5SDimitry Andric          "Packing should not change number of elements.");
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric   // TODO: Since fp32 is expanded, the extract cost should always be 0.
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric   unsigned NumParts = getNumVectorRegs(SrcTy);
6230b57cec5SDimitry Andric   if (NumParts <= 2)
6240b57cec5SDimitry Andric     // Up to 2 vector registers can be truncated efficiently with pack or
6250b57cec5SDimitry Andric     // permute. The latter requires an immediate mask to be loaded, which
6260b57cec5SDimitry Andric     // typically gets hoisted out of a loop.  TODO: return a good value for
6270b57cec5SDimitry Andric     // BB-VECTORIZER that includes the immediate loads, which we do not want
6280b57cec5SDimitry Andric     // to count for the loop vectorizer.
6290b57cec5SDimitry Andric     return 1;
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric   unsigned Cost = 0;
6320b57cec5SDimitry Andric   unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy);
6335ffd83dbSDimitry Andric   unsigned VF = cast<FixedVectorType>(SrcTy)->getNumElements();
6340b57cec5SDimitry Andric   for (unsigned P = 0; P < Log2Diff; ++P) {
6350b57cec5SDimitry Andric     if (NumParts > 1)
6360b57cec5SDimitry Andric       NumParts /= 2;
6370b57cec5SDimitry Andric     Cost += NumParts;
6380b57cec5SDimitry Andric   }
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric   // Currently, a general mix of permutes and pack instructions is output by
6410b57cec5SDimitry Andric   // isel, which follow the cost computation above except for this case which
6420b57cec5SDimitry Andric   // is one instruction less:
6430b57cec5SDimitry Andric   if (VF == 8 && SrcTy->getScalarSizeInBits() == 64 &&
6440b57cec5SDimitry Andric       DstTy->getScalarSizeInBits() == 8)
6450b57cec5SDimitry Andric     Cost--;
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric   return Cost;
6480b57cec5SDimitry Andric }
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric // Return the cost of converting a vector bitmask produced by a compare
6510b57cec5SDimitry Andric // (SrcTy), to the type of the select or extend instruction (DstTy).
6520b57cec5SDimitry Andric unsigned SystemZTTIImpl::
getVectorBitmaskConversionCost(Type * SrcTy,Type * DstTy)6530b57cec5SDimitry Andric getVectorBitmaskConversionCost(Type *SrcTy, Type *DstTy) {
6540b57cec5SDimitry Andric   assert (SrcTy->isVectorTy() && DstTy->isVectorTy() &&
6550b57cec5SDimitry Andric           "Should only be called with vector types.");
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric   unsigned PackCost = 0;
6580b57cec5SDimitry Andric   unsigned SrcScalarBits = SrcTy->getScalarSizeInBits();
6590b57cec5SDimitry Andric   unsigned DstScalarBits = DstTy->getScalarSizeInBits();
6600b57cec5SDimitry Andric   unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy);
6610b57cec5SDimitry Andric   if (SrcScalarBits > DstScalarBits)
6620b57cec5SDimitry Andric     // The bitmask will be truncated.
6630b57cec5SDimitry Andric     PackCost = getVectorTruncCost(SrcTy, DstTy);
6640b57cec5SDimitry Andric   else if (SrcScalarBits < DstScalarBits) {
6650b57cec5SDimitry Andric     unsigned DstNumParts = getNumVectorRegs(DstTy);
6660b57cec5SDimitry Andric     // Each vector select needs its part of the bitmask unpacked.
6670b57cec5SDimitry Andric     PackCost = Log2Diff * DstNumParts;
6680b57cec5SDimitry Andric     // Extra cost for moving part of mask before unpacking.
6690b57cec5SDimitry Andric     PackCost += DstNumParts - 1;
6700b57cec5SDimitry Andric   }
6710b57cec5SDimitry Andric 
6720b57cec5SDimitry Andric   return PackCost;
6730b57cec5SDimitry Andric }
6740b57cec5SDimitry Andric 
6750b57cec5SDimitry Andric // Return the type of the compared operands. This is needed to compute the
6760b57cec5SDimitry Andric // cost for a Select / ZExt or SExt instruction.
getCmpOpsType(const Instruction * I,unsigned VF=1)6770b57cec5SDimitry Andric static Type *getCmpOpsType(const Instruction *I, unsigned VF = 1) {
6780b57cec5SDimitry Andric   Type *OpTy = nullptr;
6790b57cec5SDimitry Andric   if (CmpInst *CI = dyn_cast<CmpInst>(I->getOperand(0)))
6800b57cec5SDimitry Andric     OpTy = CI->getOperand(0)->getType();
6810b57cec5SDimitry Andric   else if (Instruction *LogicI = dyn_cast<Instruction>(I->getOperand(0)))
6820b57cec5SDimitry Andric     if (LogicI->getNumOperands() == 2)
6830b57cec5SDimitry Andric       if (CmpInst *CI0 = dyn_cast<CmpInst>(LogicI->getOperand(0)))
6840b57cec5SDimitry Andric         if (isa<CmpInst>(LogicI->getOperand(1)))
6850b57cec5SDimitry Andric           OpTy = CI0->getOperand(0)->getType();
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric   if (OpTy != nullptr) {
6880b57cec5SDimitry Andric     if (VF == 1) {
6890b57cec5SDimitry Andric       assert (!OpTy->isVectorTy() && "Expected scalar type");
6900b57cec5SDimitry Andric       return OpTy;
6910b57cec5SDimitry Andric     }
6920b57cec5SDimitry Andric     // Return the potentially vectorized type based on 'I' and 'VF'.  'I' may
6930b57cec5SDimitry Andric     // be either scalar or already vectorized with a same or lesser VF.
6940b57cec5SDimitry Andric     Type *ElTy = OpTy->getScalarType();
6955ffd83dbSDimitry Andric     return FixedVectorType::get(ElTy, VF);
6960b57cec5SDimitry Andric   }
6970b57cec5SDimitry Andric 
6980b57cec5SDimitry Andric   return nullptr;
6990b57cec5SDimitry Andric }
7000b57cec5SDimitry Andric 
7010b57cec5SDimitry Andric // Get the cost of converting a boolean vector to a vector with same width
7020b57cec5SDimitry Andric // and element size as Dst, plus the cost of zero extending if needed.
7030b57cec5SDimitry Andric unsigned SystemZTTIImpl::
getBoolVecToIntConversionCost(unsigned Opcode,Type * Dst,const Instruction * I)7040b57cec5SDimitry Andric getBoolVecToIntConversionCost(unsigned Opcode, Type *Dst,
7050b57cec5SDimitry Andric                               const Instruction *I) {
7065ffd83dbSDimitry Andric   auto *DstVTy = cast<FixedVectorType>(Dst);
7075ffd83dbSDimitry Andric   unsigned VF = DstVTy->getNumElements();
7080b57cec5SDimitry Andric   unsigned Cost = 0;
7090b57cec5SDimitry Andric   // If we know what the widths of the compared operands, get any cost of
7100b57cec5SDimitry Andric   // converting it to match Dst. Otherwise assume same widths.
7110b57cec5SDimitry Andric   Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr);
7120b57cec5SDimitry Andric   if (CmpOpTy != nullptr)
7130b57cec5SDimitry Andric     Cost = getVectorBitmaskConversionCost(CmpOpTy, Dst);
7140b57cec5SDimitry Andric   if (Opcode == Instruction::ZExt || Opcode == Instruction::UIToFP)
7150b57cec5SDimitry Andric     // One 'vn' per dst vector with an immediate mask.
7160b57cec5SDimitry Andric     Cost += getNumVectorRegs(Dst);
7170b57cec5SDimitry Andric   return Cost;
7180b57cec5SDimitry Andric }
7190b57cec5SDimitry Andric 
getCastInstrCost(unsigned Opcode,Type * Dst,Type * Src,TTI::CastContextHint CCH,TTI::TargetCostKind CostKind,const Instruction * I)720*5f7ddb14SDimitry Andric InstructionCost SystemZTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
721*5f7ddb14SDimitry Andric                                                  Type *Src,
722af732203SDimitry Andric                                                  TTI::CastContextHint CCH,
7235ffd83dbSDimitry Andric                                                  TTI::TargetCostKind CostKind,
7240b57cec5SDimitry Andric                                                  const Instruction *I) {
7255ffd83dbSDimitry Andric   // FIXME: Can the logic below also be used for these cost kinds?
7265ffd83dbSDimitry Andric   if (CostKind == TTI::TCK_CodeSize || CostKind == TTI::TCK_SizeAndLatency) {
727*5f7ddb14SDimitry Andric     auto BaseCost = BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
7285ffd83dbSDimitry Andric     return BaseCost == 0 ? BaseCost : 1;
7295ffd83dbSDimitry Andric   }
7305ffd83dbSDimitry Andric 
7310b57cec5SDimitry Andric   unsigned DstScalarBits = Dst->getScalarSizeInBits();
7320b57cec5SDimitry Andric   unsigned SrcScalarBits = Src->getScalarSizeInBits();
7330b57cec5SDimitry Andric 
7345ffd83dbSDimitry Andric   if (!Src->isVectorTy()) {
7355ffd83dbSDimitry Andric     assert (!Dst->isVectorTy());
7365ffd83dbSDimitry Andric 
7375ffd83dbSDimitry Andric     if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP) {
7385ffd83dbSDimitry Andric       if (SrcScalarBits >= 32 ||
7395ffd83dbSDimitry Andric           (I != nullptr && isa<LoadInst>(I->getOperand(0))))
7405ffd83dbSDimitry Andric         return 1;
7415ffd83dbSDimitry Andric       return SrcScalarBits > 1 ? 2 /*i8/i16 extend*/ : 5 /*branch seq.*/;
7425ffd83dbSDimitry Andric     }
7435ffd83dbSDimitry Andric 
7445ffd83dbSDimitry Andric     if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
7455ffd83dbSDimitry Andric         Src->isIntegerTy(1)) {
7465ffd83dbSDimitry Andric       if (ST->hasLoadStoreOnCond2())
7475ffd83dbSDimitry Andric         return 2; // li 0; loc 1
7485ffd83dbSDimitry Andric 
7495ffd83dbSDimitry Andric       // This should be extension of a compare i1 result, which is done with
7505ffd83dbSDimitry Andric       // ipm and a varying sequence of instructions.
7515ffd83dbSDimitry Andric       unsigned Cost = 0;
7525ffd83dbSDimitry Andric       if (Opcode == Instruction::SExt)
7535ffd83dbSDimitry Andric         Cost = (DstScalarBits < 64 ? 3 : 4);
7545ffd83dbSDimitry Andric       if (Opcode == Instruction::ZExt)
7555ffd83dbSDimitry Andric         Cost = 3;
7565ffd83dbSDimitry Andric       Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I) : nullptr);
7575ffd83dbSDimitry Andric       if (CmpOpTy != nullptr && CmpOpTy->isFloatingPointTy())
7585ffd83dbSDimitry Andric         // If operands of an fp-type was compared, this costs +1.
7595ffd83dbSDimitry Andric         Cost++;
7605ffd83dbSDimitry Andric       return Cost;
7615ffd83dbSDimitry Andric     }
7625ffd83dbSDimitry Andric   }
7635ffd83dbSDimitry Andric   else if (ST->hasVector()) {
764*5f7ddb14SDimitry Andric     // Vector to scalar cast.
7655ffd83dbSDimitry Andric     auto *SrcVecTy = cast<FixedVectorType>(Src);
766*5f7ddb14SDimitry Andric     auto *DstVecTy = dyn_cast<FixedVectorType>(Dst);
767*5f7ddb14SDimitry Andric     if (!DstVecTy) {
768*5f7ddb14SDimitry Andric       // TODO: tune vector-to-scalar cast.
769*5f7ddb14SDimitry Andric       return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
770*5f7ddb14SDimitry Andric     }
7715ffd83dbSDimitry Andric     unsigned VF = SrcVecTy->getNumElements();
7720b57cec5SDimitry Andric     unsigned NumDstVectors = getNumVectorRegs(Dst);
7730b57cec5SDimitry Andric     unsigned NumSrcVectors = getNumVectorRegs(Src);
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric     if (Opcode == Instruction::Trunc) {
7760b57cec5SDimitry Andric       if (Src->getScalarSizeInBits() == Dst->getScalarSizeInBits())
7770b57cec5SDimitry Andric         return 0; // Check for NOOP conversions.
7780b57cec5SDimitry Andric       return getVectorTruncCost(Src, Dst);
7790b57cec5SDimitry Andric     }
7800b57cec5SDimitry Andric 
7810b57cec5SDimitry Andric     if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) {
7820b57cec5SDimitry Andric       if (SrcScalarBits >= 8) {
7830b57cec5SDimitry Andric         // ZExt/SExt will be handled with one unpack per doubling of width.
7840b57cec5SDimitry Andric         unsigned NumUnpacks = getElSizeLog2Diff(Src, Dst);
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric         // For types that spans multiple vector registers, some additional
7870b57cec5SDimitry Andric         // instructions are used to setup the unpacking.
7880b57cec5SDimitry Andric         unsigned NumSrcVectorOps =
7890b57cec5SDimitry Andric           (NumUnpacks > 1 ? (NumDstVectors - NumSrcVectors)
7900b57cec5SDimitry Andric                           : (NumDstVectors / 2));
7910b57cec5SDimitry Andric 
7920b57cec5SDimitry Andric         return (NumUnpacks * NumDstVectors) + NumSrcVectorOps;
7930b57cec5SDimitry Andric       }
7940b57cec5SDimitry Andric       else if (SrcScalarBits == 1)
7950b57cec5SDimitry Andric         return getBoolVecToIntConversionCost(Opcode, Dst, I);
7960b57cec5SDimitry Andric     }
7970b57cec5SDimitry Andric 
7980b57cec5SDimitry Andric     if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP ||
7990b57cec5SDimitry Andric         Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI) {
8000b57cec5SDimitry Andric       // TODO: Fix base implementation which could simplify things a bit here
8010b57cec5SDimitry Andric       // (seems to miss on differentiating on scalar/vector types).
8020b57cec5SDimitry Andric 
8038bcb0991SDimitry Andric       // Only 64 bit vector conversions are natively supported before z15.
8040b57cec5SDimitry Andric       if (DstScalarBits == 64 || ST->hasVectorEnhancements2()) {
8050b57cec5SDimitry Andric         if (SrcScalarBits == DstScalarBits)
8060b57cec5SDimitry Andric           return NumDstVectors;
8070b57cec5SDimitry Andric 
8080b57cec5SDimitry Andric         if (SrcScalarBits == 1)
8090b57cec5SDimitry Andric           return getBoolVecToIntConversionCost(Opcode, Dst, I) + NumDstVectors;
8100b57cec5SDimitry Andric       }
8110b57cec5SDimitry Andric 
8120b57cec5SDimitry Andric       // Return the cost of multiple scalar invocation plus the cost of
8130b57cec5SDimitry Andric       // inserting and extracting the values. Base implementation does not
8140b57cec5SDimitry Andric       // realize float->int gets scalarized.
815*5f7ddb14SDimitry Andric       InstructionCost ScalarCost = getCastInstrCost(
816af732203SDimitry Andric           Opcode, Dst->getScalarType(), Src->getScalarType(), CCH, CostKind);
817*5f7ddb14SDimitry Andric       InstructionCost TotCost = VF * ScalarCost;
8180b57cec5SDimitry Andric       bool NeedsInserts = true, NeedsExtracts = true;
8190b57cec5SDimitry Andric       // FP128 registers do not get inserted or extracted.
8200b57cec5SDimitry Andric       if (DstScalarBits == 128 &&
8210b57cec5SDimitry Andric           (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP))
8220b57cec5SDimitry Andric         NeedsInserts = false;
8230b57cec5SDimitry Andric       if (SrcScalarBits == 128 &&
8240b57cec5SDimitry Andric           (Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI))
8250b57cec5SDimitry Andric         NeedsExtracts = false;
8260b57cec5SDimitry Andric 
8275ffd83dbSDimitry Andric       TotCost += getScalarizationOverhead(SrcVecTy, false, NeedsExtracts);
8285ffd83dbSDimitry Andric       TotCost += getScalarizationOverhead(DstVecTy, NeedsInserts, false);
8290b57cec5SDimitry Andric 
8300b57cec5SDimitry Andric       // FIXME: VF 2 for float<->i32 is currently just as expensive as for VF 4.
8310b57cec5SDimitry Andric       if (VF == 2 && SrcScalarBits == 32 && DstScalarBits == 32)
8320b57cec5SDimitry Andric         TotCost *= 2;
8330b57cec5SDimitry Andric 
8340b57cec5SDimitry Andric       return TotCost;
8350b57cec5SDimitry Andric     }
8360b57cec5SDimitry Andric 
8370b57cec5SDimitry Andric     if (Opcode == Instruction::FPTrunc) {
8380b57cec5SDimitry Andric       if (SrcScalarBits == 128)  // fp128 -> double/float + inserts of elements.
8395ffd83dbSDimitry Andric         return VF /*ldxbr/lexbr*/ +
8405ffd83dbSDimitry Andric                getScalarizationOverhead(DstVecTy, true, false);
8410b57cec5SDimitry Andric       else // double -> float
8420b57cec5SDimitry Andric         return VF / 2 /*vledb*/ + std::max(1U, VF / 4 /*vperm*/);
8430b57cec5SDimitry Andric     }
8440b57cec5SDimitry Andric 
8450b57cec5SDimitry Andric     if (Opcode == Instruction::FPExt) {
8460b57cec5SDimitry Andric       if (SrcScalarBits == 32 && DstScalarBits == 64) {
8470b57cec5SDimitry Andric         // float -> double is very rare and currently unoptimized. Instead of
8480b57cec5SDimitry Andric         // using vldeb, which can do two at a time, all conversions are
8490b57cec5SDimitry Andric         // scalarized.
8500b57cec5SDimitry Andric         return VF * 2;
8510b57cec5SDimitry Andric       }
8520b57cec5SDimitry Andric       // -> fp128.  VF * lxdb/lxeb + extraction of elements.
8535ffd83dbSDimitry Andric       return VF + getScalarizationOverhead(SrcVecTy, false, true);
8540b57cec5SDimitry Andric     }
8550b57cec5SDimitry Andric   }
8560b57cec5SDimitry Andric 
857af732203SDimitry Andric   return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
8580b57cec5SDimitry Andric }
8590b57cec5SDimitry Andric 
8600b57cec5SDimitry Andric // Scalar i8 / i16 operations will typically be made after first extending
8610b57cec5SDimitry Andric // the operands to i32.
getOperandsExtensionCost(const Instruction * I)8620b57cec5SDimitry Andric static unsigned getOperandsExtensionCost(const Instruction *I) {
8630b57cec5SDimitry Andric   unsigned ExtCost = 0;
8640b57cec5SDimitry Andric   for (Value *Op : I->operands())
8650b57cec5SDimitry Andric     // A load of i8 or i16 sign/zero extends to i32.
8660b57cec5SDimitry Andric     if (!isa<LoadInst>(Op) && !isa<ConstantInt>(Op))
8670b57cec5SDimitry Andric       ExtCost++;
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   return ExtCost;
8700b57cec5SDimitry Andric }
8710b57cec5SDimitry Andric 
getCmpSelInstrCost(unsigned Opcode,Type * ValTy,Type * CondTy,CmpInst::Predicate VecPred,TTI::TargetCostKind CostKind,const Instruction * I)872*5f7ddb14SDimitry Andric InstructionCost SystemZTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
873*5f7ddb14SDimitry Andric                                                    Type *CondTy,
874*5f7ddb14SDimitry Andric                                                    CmpInst::Predicate VecPred,
8755ffd83dbSDimitry Andric                                                    TTI::TargetCostKind CostKind,
8765ffd83dbSDimitry Andric                                                    const Instruction *I) {
8775ffd83dbSDimitry Andric   if (CostKind != TTI::TCK_RecipThroughput)
878af732203SDimitry Andric     return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind);
8795ffd83dbSDimitry Andric 
8805ffd83dbSDimitry Andric   if (!ValTy->isVectorTy()) {
8815ffd83dbSDimitry Andric     switch (Opcode) {
8825ffd83dbSDimitry Andric     case Instruction::ICmp: {
8835ffd83dbSDimitry Andric       // A loaded value compared with 0 with multiple users becomes Load and
8845ffd83dbSDimitry Andric       // Test. The load is then not foldable, so return 0 cost for the ICmp.
8855ffd83dbSDimitry Andric       unsigned ScalarBits = ValTy->getScalarSizeInBits();
8865ffd83dbSDimitry Andric       if (I != nullptr && ScalarBits >= 32)
8875ffd83dbSDimitry Andric         if (LoadInst *Ld = dyn_cast<LoadInst>(I->getOperand(0)))
8885ffd83dbSDimitry Andric           if (const ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)))
8895ffd83dbSDimitry Andric             if (!Ld->hasOneUse() && Ld->getParent() == I->getParent() &&
890af732203SDimitry Andric                 C->isZero())
8915ffd83dbSDimitry Andric               return 0;
8925ffd83dbSDimitry Andric 
8935ffd83dbSDimitry Andric       unsigned Cost = 1;
8945ffd83dbSDimitry Andric       if (ValTy->isIntegerTy() && ValTy->getScalarSizeInBits() <= 16)
8955ffd83dbSDimitry Andric         Cost += (I != nullptr ? getOperandsExtensionCost(I) : 2);
8965ffd83dbSDimitry Andric       return Cost;
8975ffd83dbSDimitry Andric     }
8985ffd83dbSDimitry Andric     case Instruction::Select:
8995ffd83dbSDimitry Andric       if (ValTy->isFloatingPointTy())
9005ffd83dbSDimitry Andric         return 4; // No load on condition for FP - costs a conditional jump.
9015ffd83dbSDimitry Andric       return 1; // Load On Condition / Select Register.
9025ffd83dbSDimitry Andric     }
9035ffd83dbSDimitry Andric   }
9045ffd83dbSDimitry Andric   else if (ST->hasVector()) {
9055ffd83dbSDimitry Andric     unsigned VF = cast<FixedVectorType>(ValTy)->getNumElements();
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric     // Called with a compare instruction.
9080b57cec5SDimitry Andric     if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) {
9090b57cec5SDimitry Andric       unsigned PredicateExtraCost = 0;
9100b57cec5SDimitry Andric       if (I != nullptr) {
9110b57cec5SDimitry Andric         // Some predicates cost one or two extra instructions.
9120b57cec5SDimitry Andric         switch (cast<CmpInst>(I)->getPredicate()) {
9130b57cec5SDimitry Andric         case CmpInst::Predicate::ICMP_NE:
9140b57cec5SDimitry Andric         case CmpInst::Predicate::ICMP_UGE:
9150b57cec5SDimitry Andric         case CmpInst::Predicate::ICMP_ULE:
9160b57cec5SDimitry Andric         case CmpInst::Predicate::ICMP_SGE:
9170b57cec5SDimitry Andric         case CmpInst::Predicate::ICMP_SLE:
9180b57cec5SDimitry Andric           PredicateExtraCost = 1;
9190b57cec5SDimitry Andric           break;
9200b57cec5SDimitry Andric         case CmpInst::Predicate::FCMP_ONE:
9210b57cec5SDimitry Andric         case CmpInst::Predicate::FCMP_ORD:
9220b57cec5SDimitry Andric         case CmpInst::Predicate::FCMP_UEQ:
9230b57cec5SDimitry Andric         case CmpInst::Predicate::FCMP_UNO:
9240b57cec5SDimitry Andric           PredicateExtraCost = 2;
9250b57cec5SDimitry Andric           break;
9260b57cec5SDimitry Andric         default:
9270b57cec5SDimitry Andric           break;
9280b57cec5SDimitry Andric         }
9290b57cec5SDimitry Andric       }
9300b57cec5SDimitry Andric 
9310b57cec5SDimitry Andric       // Float is handled with 2*vmr[lh]f + 2*vldeb + vfchdb for each pair of
9320b57cec5SDimitry Andric       // floats.  FIXME: <2 x float> generates same code as <4 x float>.
9330b57cec5SDimitry Andric       unsigned CmpCostPerVector = (ValTy->getScalarType()->isFloatTy() ? 10 : 1);
9340b57cec5SDimitry Andric       unsigned NumVecs_cmp = getNumVectorRegs(ValTy);
9350b57cec5SDimitry Andric 
9360b57cec5SDimitry Andric       unsigned Cost = (NumVecs_cmp * (CmpCostPerVector + PredicateExtraCost));
9370b57cec5SDimitry Andric       return Cost;
9380b57cec5SDimitry Andric     }
9390b57cec5SDimitry Andric     else { // Called with a select instruction.
9400b57cec5SDimitry Andric       assert (Opcode == Instruction::Select);
9410b57cec5SDimitry Andric 
9420b57cec5SDimitry Andric       // We can figure out the extra cost of packing / unpacking if the
9430b57cec5SDimitry Andric       // instruction was passed and the compare instruction is found.
9440b57cec5SDimitry Andric       unsigned PackCost = 0;
9450b57cec5SDimitry Andric       Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr);
9460b57cec5SDimitry Andric       if (CmpOpTy != nullptr)
9470b57cec5SDimitry Andric         PackCost =
9480b57cec5SDimitry Andric           getVectorBitmaskConversionCost(CmpOpTy, ValTy);
9490b57cec5SDimitry Andric 
9500b57cec5SDimitry Andric       return getNumVectorRegs(ValTy) /*vsel*/ + PackCost;
9510b57cec5SDimitry Andric     }
9520b57cec5SDimitry Andric   }
9530b57cec5SDimitry Andric 
954af732203SDimitry Andric   return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind);
9550b57cec5SDimitry Andric }
9560b57cec5SDimitry Andric 
getVectorInstrCost(unsigned Opcode,Type * Val,unsigned Index)957*5f7ddb14SDimitry Andric InstructionCost SystemZTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
958*5f7ddb14SDimitry Andric                                                    unsigned Index) {
9590b57cec5SDimitry Andric   // vlvgp will insert two grs into a vector register, so only count half the
9600b57cec5SDimitry Andric   // number of instructions.
9610b57cec5SDimitry Andric   if (Opcode == Instruction::InsertElement && Val->isIntOrIntVectorTy(64))
9620b57cec5SDimitry Andric     return ((Index % 2 == 0) ? 1 : 0);
9630b57cec5SDimitry Andric 
9640b57cec5SDimitry Andric   if (Opcode == Instruction::ExtractElement) {
9650b57cec5SDimitry Andric     int Cost = ((getScalarSizeInBits(Val) == 1) ? 2 /*+test-under-mask*/ : 1);
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric     // Give a slight penalty for moving out of vector pipeline to FXU unit.
9680b57cec5SDimitry Andric     if (Index == 0 && Val->isIntOrIntVectorTy())
9690b57cec5SDimitry Andric       Cost += 1;
9700b57cec5SDimitry Andric 
9710b57cec5SDimitry Andric     return Cost;
9720b57cec5SDimitry Andric   }
9730b57cec5SDimitry Andric 
9740b57cec5SDimitry Andric   return BaseT::getVectorInstrCost(Opcode, Val, Index);
9750b57cec5SDimitry Andric }
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric // Check if a load may be folded as a memory operand in its user.
9780b57cec5SDimitry Andric bool SystemZTTIImpl::
isFoldableLoad(const LoadInst * Ld,const Instruction * & FoldedValue)9790b57cec5SDimitry Andric isFoldableLoad(const LoadInst *Ld, const Instruction *&FoldedValue) {
9800b57cec5SDimitry Andric   if (!Ld->hasOneUse())
9810b57cec5SDimitry Andric     return false;
9820b57cec5SDimitry Andric   FoldedValue = Ld;
9830b57cec5SDimitry Andric   const Instruction *UserI = cast<Instruction>(*Ld->user_begin());
9840b57cec5SDimitry Andric   unsigned LoadedBits = getScalarSizeInBits(Ld->getType());
9850b57cec5SDimitry Andric   unsigned TruncBits = 0;
9860b57cec5SDimitry Andric   unsigned SExtBits = 0;
9870b57cec5SDimitry Andric   unsigned ZExtBits = 0;
9880b57cec5SDimitry Andric   if (UserI->hasOneUse()) {
9890b57cec5SDimitry Andric     unsigned UserBits = UserI->getType()->getScalarSizeInBits();
9900b57cec5SDimitry Andric     if (isa<TruncInst>(UserI))
9910b57cec5SDimitry Andric       TruncBits = UserBits;
9920b57cec5SDimitry Andric     else if (isa<SExtInst>(UserI))
9930b57cec5SDimitry Andric       SExtBits = UserBits;
9940b57cec5SDimitry Andric     else if (isa<ZExtInst>(UserI))
9950b57cec5SDimitry Andric       ZExtBits = UserBits;
9960b57cec5SDimitry Andric   }
9970b57cec5SDimitry Andric   if (TruncBits || SExtBits || ZExtBits) {
9980b57cec5SDimitry Andric     FoldedValue = UserI;
9990b57cec5SDimitry Andric     UserI = cast<Instruction>(*UserI->user_begin());
10000b57cec5SDimitry Andric     // Load (single use) -> trunc/extend (single use) -> UserI
10010b57cec5SDimitry Andric   }
10020b57cec5SDimitry Andric   if ((UserI->getOpcode() == Instruction::Sub ||
10030b57cec5SDimitry Andric        UserI->getOpcode() == Instruction::SDiv ||
10040b57cec5SDimitry Andric        UserI->getOpcode() == Instruction::UDiv) &&
10050b57cec5SDimitry Andric       UserI->getOperand(1) != FoldedValue)
10060b57cec5SDimitry Andric     return false; // Not commutative, only RHS foldable.
10070b57cec5SDimitry Andric   // LoadOrTruncBits holds the number of effectively loaded bits, but 0 if an
10080b57cec5SDimitry Andric   // extension was made of the load.
10090b57cec5SDimitry Andric   unsigned LoadOrTruncBits =
10100b57cec5SDimitry Andric       ((SExtBits || ZExtBits) ? 0 : (TruncBits ? TruncBits : LoadedBits));
10110b57cec5SDimitry Andric   switch (UserI->getOpcode()) {
10120b57cec5SDimitry Andric   case Instruction::Add: // SE: 16->32, 16/32->64, z14:16->64. ZE: 32->64
10130b57cec5SDimitry Andric   case Instruction::Sub:
10140b57cec5SDimitry Andric   case Instruction::ICmp:
10150b57cec5SDimitry Andric     if (LoadedBits == 32 && ZExtBits == 64)
10160b57cec5SDimitry Andric       return true;
10170b57cec5SDimitry Andric     LLVM_FALLTHROUGH;
10180b57cec5SDimitry Andric   case Instruction::Mul: // SE: 16->32, 32->64, z14:16->64
10190b57cec5SDimitry Andric     if (UserI->getOpcode() != Instruction::ICmp) {
10200b57cec5SDimitry Andric       if (LoadedBits == 16 &&
10210b57cec5SDimitry Andric           (SExtBits == 32 ||
10220b57cec5SDimitry Andric            (SExtBits == 64 && ST->hasMiscellaneousExtensions2())))
10230b57cec5SDimitry Andric         return true;
10240b57cec5SDimitry Andric       if (LoadOrTruncBits == 16)
10250b57cec5SDimitry Andric         return true;
10260b57cec5SDimitry Andric     }
10270b57cec5SDimitry Andric     LLVM_FALLTHROUGH;
10280b57cec5SDimitry Andric   case Instruction::SDiv:// SE: 32->64
10290b57cec5SDimitry Andric     if (LoadedBits == 32 && SExtBits == 64)
10300b57cec5SDimitry Andric       return true;
10310b57cec5SDimitry Andric     LLVM_FALLTHROUGH;
10320b57cec5SDimitry Andric   case Instruction::UDiv:
10330b57cec5SDimitry Andric   case Instruction::And:
10340b57cec5SDimitry Andric   case Instruction::Or:
10350b57cec5SDimitry Andric   case Instruction::Xor:
10360b57cec5SDimitry Andric     // This also makes sense for float operations, but disabled for now due
10370b57cec5SDimitry Andric     // to regressions.
10380b57cec5SDimitry Andric     // case Instruction::FCmp:
10390b57cec5SDimitry Andric     // case Instruction::FAdd:
10400b57cec5SDimitry Andric     // case Instruction::FSub:
10410b57cec5SDimitry Andric     // case Instruction::FMul:
10420b57cec5SDimitry Andric     // case Instruction::FDiv:
10430b57cec5SDimitry Andric 
10440b57cec5SDimitry Andric     // All possible extensions of memory checked above.
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric     // Comparison between memory and immediate.
10470b57cec5SDimitry Andric     if (UserI->getOpcode() == Instruction::ICmp)
10480b57cec5SDimitry Andric       if (ConstantInt *CI = dyn_cast<ConstantInt>(UserI->getOperand(1)))
1049af732203SDimitry Andric         if (CI->getValue().isIntN(16))
10500b57cec5SDimitry Andric           return true;
10510b57cec5SDimitry Andric     return (LoadOrTruncBits == 32 || LoadOrTruncBits == 64);
10520b57cec5SDimitry Andric     break;
10530b57cec5SDimitry Andric   }
10540b57cec5SDimitry Andric   return false;
10550b57cec5SDimitry Andric }
10560b57cec5SDimitry Andric 
isBswapIntrinsicCall(const Value * V)10570b57cec5SDimitry Andric static bool isBswapIntrinsicCall(const Value *V) {
10580b57cec5SDimitry Andric   if (const Instruction *I = dyn_cast<Instruction>(V))
10590b57cec5SDimitry Andric     if (auto *CI = dyn_cast<CallInst>(I))
10600b57cec5SDimitry Andric       if (auto *F = CI->getCalledFunction())
10610b57cec5SDimitry Andric         if (F->getIntrinsicID() == Intrinsic::bswap)
10620b57cec5SDimitry Andric           return true;
10630b57cec5SDimitry Andric   return false;
10640b57cec5SDimitry Andric }
10650b57cec5SDimitry Andric 
getMemoryOpCost(unsigned Opcode,Type * Src,MaybeAlign Alignment,unsigned AddressSpace,TTI::TargetCostKind CostKind,const Instruction * I)1066*5f7ddb14SDimitry Andric InstructionCost SystemZTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
1067*5f7ddb14SDimitry Andric                                                 MaybeAlign Alignment,
1068*5f7ddb14SDimitry Andric                                                 unsigned AddressSpace,
10695ffd83dbSDimitry Andric                                                 TTI::TargetCostKind CostKind,
10700b57cec5SDimitry Andric                                                 const Instruction *I) {
10710b57cec5SDimitry Andric   assert(!Src->isVoidTy() && "Invalid type");
10720b57cec5SDimitry Andric 
10735ffd83dbSDimitry Andric   // TODO: Handle other cost kinds.
10745ffd83dbSDimitry Andric   if (CostKind != TTI::TCK_RecipThroughput)
10755ffd83dbSDimitry Andric     return 1;
10765ffd83dbSDimitry Andric 
10770b57cec5SDimitry Andric   if (!Src->isVectorTy() && Opcode == Instruction::Load && I != nullptr) {
10780b57cec5SDimitry Andric     // Store the load or its truncated or extended value in FoldedValue.
10790b57cec5SDimitry Andric     const Instruction *FoldedValue = nullptr;
10800b57cec5SDimitry Andric     if (isFoldableLoad(cast<LoadInst>(I), FoldedValue)) {
10810b57cec5SDimitry Andric       const Instruction *UserI = cast<Instruction>(*FoldedValue->user_begin());
10820b57cec5SDimitry Andric       assert (UserI->getNumOperands() == 2 && "Expected a binop.");
10830b57cec5SDimitry Andric 
10840b57cec5SDimitry Andric       // UserI can't fold two loads, so in that case return 0 cost only
10850b57cec5SDimitry Andric       // half of the time.
10860b57cec5SDimitry Andric       for (unsigned i = 0; i < 2; ++i) {
10870b57cec5SDimitry Andric         if (UserI->getOperand(i) == FoldedValue)
10880b57cec5SDimitry Andric           continue;
10890b57cec5SDimitry Andric 
10900b57cec5SDimitry Andric         if (Instruction *OtherOp = dyn_cast<Instruction>(UserI->getOperand(i))){
10910b57cec5SDimitry Andric           LoadInst *OtherLoad = dyn_cast<LoadInst>(OtherOp);
10920b57cec5SDimitry Andric           if (!OtherLoad &&
10930b57cec5SDimitry Andric               (isa<TruncInst>(OtherOp) || isa<SExtInst>(OtherOp) ||
10940b57cec5SDimitry Andric                isa<ZExtInst>(OtherOp)))
10950b57cec5SDimitry Andric             OtherLoad = dyn_cast<LoadInst>(OtherOp->getOperand(0));
10960b57cec5SDimitry Andric           if (OtherLoad && isFoldableLoad(OtherLoad, FoldedValue/*dummy*/))
10970b57cec5SDimitry Andric             return i == 0; // Both operands foldable.
10980b57cec5SDimitry Andric         }
10990b57cec5SDimitry Andric       }
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric       return 0; // Only I is foldable in user.
11020b57cec5SDimitry Andric     }
11030b57cec5SDimitry Andric   }
11040b57cec5SDimitry Andric 
11050b57cec5SDimitry Andric   unsigned NumOps =
11060b57cec5SDimitry Andric     (Src->isVectorTy() ? getNumVectorRegs(Src) : getNumberOfParts(Src));
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric   // Store/Load reversed saves one instruction.
11090b57cec5SDimitry Andric   if (((!Src->isVectorTy() && NumOps == 1) || ST->hasVectorEnhancements2()) &&
11100b57cec5SDimitry Andric       I != nullptr) {
11110b57cec5SDimitry Andric     if (Opcode == Instruction::Load && I->hasOneUse()) {
11120b57cec5SDimitry Andric       const Instruction *LdUser = cast<Instruction>(*I->user_begin());
11130b57cec5SDimitry Andric       // In case of load -> bswap -> store, return normal cost for the load.
11140b57cec5SDimitry Andric       if (isBswapIntrinsicCall(LdUser) &&
11150b57cec5SDimitry Andric           (!LdUser->hasOneUse() || !isa<StoreInst>(*LdUser->user_begin())))
11160b57cec5SDimitry Andric         return 0;
11170b57cec5SDimitry Andric     }
11180b57cec5SDimitry Andric     else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
11190b57cec5SDimitry Andric       const Value *StoredVal = SI->getValueOperand();
11200b57cec5SDimitry Andric       if (StoredVal->hasOneUse() && isBswapIntrinsicCall(StoredVal))
11210b57cec5SDimitry Andric         return 0;
11220b57cec5SDimitry Andric     }
11230b57cec5SDimitry Andric   }
11240b57cec5SDimitry Andric 
11250b57cec5SDimitry Andric   if (Src->getScalarSizeInBits() == 128)
11260b57cec5SDimitry Andric     // 128 bit scalars are held in a pair of two 64 bit registers.
11270b57cec5SDimitry Andric     NumOps *= 2;
11280b57cec5SDimitry Andric 
11290b57cec5SDimitry Andric   return  NumOps;
11300b57cec5SDimitry Andric }
11310b57cec5SDimitry Andric 
11320b57cec5SDimitry Andric // The generic implementation of getInterleavedMemoryOpCost() is based on
11330b57cec5SDimitry Andric // adding costs of the memory operations plus all the extracts and inserts
11340b57cec5SDimitry Andric // needed for using / defining the vector operands. The SystemZ version does
11350b57cec5SDimitry Andric // roughly the same but bases the computations on vector permutations
11360b57cec5SDimitry Andric // instead.
getInterleavedMemoryOpCost(unsigned Opcode,Type * VecTy,unsigned Factor,ArrayRef<unsigned> Indices,Align Alignment,unsigned AddressSpace,TTI::TargetCostKind CostKind,bool UseMaskForCond,bool UseMaskForGaps)1137*5f7ddb14SDimitry Andric InstructionCost SystemZTTIImpl::getInterleavedMemoryOpCost(
11385ffd83dbSDimitry Andric     unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
11395ffd83dbSDimitry Andric     Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind,
11405ffd83dbSDimitry Andric     bool UseMaskForCond, bool UseMaskForGaps) {
11410b57cec5SDimitry Andric   if (UseMaskForCond || UseMaskForGaps)
11420b57cec5SDimitry Andric     return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
11435ffd83dbSDimitry Andric                                              Alignment, AddressSpace, CostKind,
11440b57cec5SDimitry Andric                                              UseMaskForCond, UseMaskForGaps);
11450b57cec5SDimitry Andric   assert(isa<VectorType>(VecTy) &&
11460b57cec5SDimitry Andric          "Expect a vector type for interleaved memory op");
11470b57cec5SDimitry Andric 
11485ffd83dbSDimitry Andric   unsigned NumElts = cast<FixedVectorType>(VecTy)->getNumElements();
11490b57cec5SDimitry Andric   assert(Factor > 1 && NumElts % Factor == 0 && "Invalid interleave factor");
11500b57cec5SDimitry Andric   unsigned VF = NumElts / Factor;
11510b57cec5SDimitry Andric   unsigned NumEltsPerVecReg = (128U / getScalarSizeInBits(VecTy));
11520b57cec5SDimitry Andric   unsigned NumVectorMemOps = getNumVectorRegs(VecTy);
11530b57cec5SDimitry Andric   unsigned NumPermutes = 0;
11540b57cec5SDimitry Andric 
11550b57cec5SDimitry Andric   if (Opcode == Instruction::Load) {
11560b57cec5SDimitry Andric     // Loading interleave groups may have gaps, which may mean fewer
11570b57cec5SDimitry Andric     // loads. Find out how many vectors will be loaded in total, and in how
11580b57cec5SDimitry Andric     // many of them each value will be in.
11590b57cec5SDimitry Andric     BitVector UsedInsts(NumVectorMemOps, false);
11600b57cec5SDimitry Andric     std::vector<BitVector> ValueVecs(Factor, BitVector(NumVectorMemOps, false));
11610b57cec5SDimitry Andric     for (unsigned Index : Indices)
11620b57cec5SDimitry Andric       for (unsigned Elt = 0; Elt < VF; ++Elt) {
11630b57cec5SDimitry Andric         unsigned Vec = (Index + Elt * Factor) / NumEltsPerVecReg;
11640b57cec5SDimitry Andric         UsedInsts.set(Vec);
11650b57cec5SDimitry Andric         ValueVecs[Index].set(Vec);
11660b57cec5SDimitry Andric       }
11670b57cec5SDimitry Andric     NumVectorMemOps = UsedInsts.count();
11680b57cec5SDimitry Andric 
11690b57cec5SDimitry Andric     for (unsigned Index : Indices) {
11700b57cec5SDimitry Andric       // Estimate that each loaded source vector containing this Index
11710b57cec5SDimitry Andric       // requires one operation, except that vperm can handle two input
11720b57cec5SDimitry Andric       // registers first time for each dst vector.
11730b57cec5SDimitry Andric       unsigned NumSrcVecs = ValueVecs[Index].count();
1174*5f7ddb14SDimitry Andric       unsigned NumDstVecs = divideCeil(VF * getScalarSizeInBits(VecTy), 128U);
11750b57cec5SDimitry Andric       assert (NumSrcVecs >= NumDstVecs && "Expected at least as many sources");
11760b57cec5SDimitry Andric       NumPermutes += std::max(1U, NumSrcVecs - NumDstVecs);
11770b57cec5SDimitry Andric     }
11780b57cec5SDimitry Andric   } else {
11790b57cec5SDimitry Andric     // Estimate the permutes for each stored vector as the smaller of the
11800b57cec5SDimitry Andric     // number of elements and the number of source vectors. Subtract one per
11810b57cec5SDimitry Andric     // dst vector for vperm (S.A.).
11820b57cec5SDimitry Andric     unsigned NumSrcVecs = std::min(NumEltsPerVecReg, Factor);
11830b57cec5SDimitry Andric     unsigned NumDstVecs = NumVectorMemOps;
11840b57cec5SDimitry Andric     assert (NumSrcVecs > 1 && "Expected at least two source vectors.");
11850b57cec5SDimitry Andric     NumPermutes += (NumDstVecs * NumSrcVecs) - NumDstVecs;
11860b57cec5SDimitry Andric   }
11870b57cec5SDimitry Andric 
11880b57cec5SDimitry Andric   // Cost of load/store operations and the permutations needed.
11890b57cec5SDimitry Andric   return NumVectorMemOps + NumPermutes;
11900b57cec5SDimitry Andric }
11910b57cec5SDimitry Andric 
getVectorIntrinsicInstrCost(Intrinsic::ID ID,Type * RetTy)11920b57cec5SDimitry Andric static int getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy) {
11930b57cec5SDimitry Andric   if (RetTy->isVectorTy() && ID == Intrinsic::bswap)
11940b57cec5SDimitry Andric     return getNumVectorRegs(RetTy); // VPERM
11950b57cec5SDimitry Andric   return -1;
11960b57cec5SDimitry Andric }
11970b57cec5SDimitry Andric 
1198*5f7ddb14SDimitry Andric InstructionCost
getIntrinsicInstrCost(const IntrinsicCostAttributes & ICA,TTI::TargetCostKind CostKind)1199*5f7ddb14SDimitry Andric SystemZTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
12005ffd83dbSDimitry Andric                                       TTI::TargetCostKind CostKind) {
1201*5f7ddb14SDimitry Andric   InstructionCost Cost =
1202*5f7ddb14SDimitry Andric       getVectorIntrinsicInstrCost(ICA.getID(), ICA.getReturnType());
12030b57cec5SDimitry Andric   if (Cost != -1)
12040b57cec5SDimitry Andric     return Cost;
12055ffd83dbSDimitry Andric   return BaseT::getIntrinsicInstrCost(ICA, CostKind);
12060b57cec5SDimitry Andric }
1207