#include "SystemZTargetTransformInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/CodeGen/BasicTTIImpl.h"
#include "llvm/CodeGen/CostTable.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/Support/Debug.h"
using namespace llvm;
#define DEBUG_TYPE "systemztti"
static bool isUsedAsMemCpySource(const Value *V, bool &OtherUse) {
bool UsedAsMemCpySource = false;
for (const User *U : V->users())
if (const Instruction *User = dyn_cast<Instruction>(U)) {
if (isa<BitCastInst>(User) || isa<GetElementPtrInst>(User)) {
UsedAsMemCpySource |= isUsedAsMemCpySource(User, OtherUse);
continue;
}
if (const MemCpyInst *Memcpy = dyn_cast<MemCpyInst>(User)) {
if (Memcpy->getOperand(1) == V && !Memcpy->isVolatile()) {
UsedAsMemCpySource = true;
continue;
}
}
OtherUse = true;
}
return UsedAsMemCpySource;
}
unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const {
unsigned Bonus = 0;
if (Function *Callee = CB->getCalledFunction())
for (Argument &Arg : Callee->args()) {
bool OtherUse = false;
if (isUsedAsMemCpySource(&Arg, OtherUse) && !OtherUse)
Bonus += 150;
}
LLVM_DEBUG(if (Bonus)
dbgs() << "++ SZTTI Adding inlining bonus: " << Bonus << "\n";);
return Bonus;
}
InstructionCost SystemZTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty,
TTI::TargetCostKind CostKind) {
assert(Ty->isIntegerTy());
unsigned BitSize = Ty->getPrimitiveSizeInBits();
if (BitSize == 0)
return TTI::TCC_Free;
if (BitSize > 64)
return TTI::TCC_Free;
if (Imm == 0)
return TTI::TCC_Free;
if (Imm.getBitWidth() <= 64) {
if (isInt<32>(Imm.getSExtValue()))
return TTI::TCC_Basic;
if (isUInt<32>(Imm.getZExtValue()))
return TTI::TCC_Basic;
if ((Imm.getZExtValue() & 0xffffffff) == 0)
return TTI::TCC_Basic;
return 2 * TTI::TCC_Basic;
}
return 4 * TTI::TCC_Basic;
}
InstructionCost SystemZTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
const APInt &Imm, Type *Ty,
TTI::TargetCostKind CostKind,
Instruction *Inst) {
assert(Ty->isIntegerTy());
unsigned BitSize = Ty->getPrimitiveSizeInBits();
if (BitSize == 0)
return TTI::TCC_Free;
if (BitSize > 64)
return TTI::TCC_Free;
switch (Opcode) {
default:
return TTI::TCC_Free;
case Instruction::GetElementPtr:
if (Idx == 0)
return 2 * TTI::TCC_Basic;
return TTI::TCC_Free;
case Instruction::Store:
if (Idx == 0 && Imm.getBitWidth() <= 64) {
if (BitSize == 8)
return TTI::TCC_Free;
if (isInt<16>(Imm.getSExtValue()))
return TTI::TCC_Free;
}
break;
case Instruction::ICmp:
if (Idx == 1 && Imm.getBitWidth() <= 64) {
if (isInt<32>(Imm.getSExtValue()))
return TTI::TCC_Free;
if (isUInt<32>(Imm.getZExtValue()))
return TTI::TCC_Free;
}
break;
case Instruction::Add:
case Instruction::Sub:
if (Idx == 1 && Imm.getBitWidth() <= 64) {
if (isUInt<32>(Imm.getZExtValue()))
return TTI::TCC_Free;
if (isUInt<32>(-Imm.getSExtValue()))
return TTI::TCC_Free;
}
break;
case Instruction::Mul:
if (Idx == 1 && Imm.getBitWidth() <= 64) {
if (isInt<32>(Imm.getSExtValue()))
return TTI::TCC_Free;
}
break;
case Instruction::Or:
case Instruction::Xor:
if (Idx == 1 && Imm.getBitWidth() <= 64) {
if (isUInt<32>(Imm.getZExtValue()))
return TTI::TCC_Free;
if ((Imm.getZExtValue() & 0xffffffff) == 0)
return TTI::TCC_Free;
}
break;
case Instruction::And:
if (Idx == 1 && Imm.getBitWidth() <= 64) {
if (BitSize <= 32)
return TTI::TCC_Free;
if (isUInt<32>(~Imm.getZExtValue()))
return TTI::TCC_Free;
if ((Imm.getZExtValue() & 0xffffffff) == 0xffffffff)
return TTI::TCC_Free;
const SystemZInstrInfo *TII = ST->getInstrInfo();
unsigned Start, End;
if (TII->isRxSBGMask(Imm.getZExtValue(), BitSize, Start, End))
return TTI::TCC_Free;
}
break;
case Instruction::Shl:
case Instruction::LShr:
case Instruction::AShr:
if (Idx == 1)
return TTI::TCC_Free;
break;
case Instruction::UDiv:
case Instruction::SDiv:
case Instruction::URem:
case Instruction::SRem:
case Instruction::Trunc:
case Instruction::ZExt:
case Instruction::SExt:
case Instruction::IntToPtr:
case Instruction::PtrToInt:
case Instruction::BitCast:
case Instruction::PHI:
case Instruction::Call:
case Instruction::Select:
case Instruction::Ret:
case Instruction::Load:
break;
}
return SystemZTTIImpl::getIntImmCost(Imm, Ty, CostKind);
}
InstructionCost
SystemZTTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
const APInt &Imm, Type *Ty,
TTI::TargetCostKind CostKind) {
assert(Ty->isIntegerTy());
unsigned BitSize = Ty->getPrimitiveSizeInBits();
if (BitSize == 0)
return TTI::TCC_Free;
if (BitSize > 64)
return TTI::TCC_Free;
switch (IID) {
default:
return TTI::TCC_Free;
case Intrinsic::sadd_with_overflow:
case Intrinsic::uadd_with_overflow:
case Intrinsic::ssub_with_overflow:
case Intrinsic::usub_with_overflow:
if (Idx == 1 && Imm.getBitWidth() <= 64) {
if (isUInt<32>(Imm.getZExtValue()))
return TTI::TCC_Free;
if (isUInt<32>(-Imm.getSExtValue()))
return TTI::TCC_Free;
}
break;
case Intrinsic::smul_with_overflow:
case Intrinsic::umul_with_overflow:
if (Idx == 1 && Imm.getBitWidth() <= 64) {
if (isInt<32>(Imm.getSExtValue()))
return TTI::TCC_Free;
}
break;
case Intrinsic::experimental_stackmap:
if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
return TTI::TCC_Free;
break;
case Intrinsic::experimental_patchpoint_void:
case Intrinsic::experimental_patchpoint_i64:
if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
return TTI::TCC_Free;
break;
}
return SystemZTTIImpl::getIntImmCost(Imm, Ty, CostKind);
}
TargetTransformInfo::PopcntSupportKind
SystemZTTIImpl::getPopcntSupport(unsigned TyWidth) {
assert(isPowerOf2_32(TyWidth) && "Type width must be power of 2");
if (ST->hasPopulationCount() && TyWidth <= 64)
return TTI::PSK_FastHardware;
return TTI::PSK_Software;
}
void SystemZTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
TTI::UnrollingPreferences &UP,
OptimizationRemarkEmitter *ORE) {
bool HasCall = false;
InstructionCost NumStores = 0;
for (auto &BB : L->blocks())
for (auto &I : *BB) {
if (isa<CallInst>(&I) || isa<InvokeInst>(&I)) {
if (const Function *F = cast<CallBase>(I).getCalledFunction()) {
if (isLoweredToCall(F))
HasCall = true;
if (F->getIntrinsicID() == Intrinsic::memcpy ||
F->getIntrinsicID() == Intrinsic::memset)
NumStores++;
} else { HasCall = true;
}
}
if (isa<StoreInst>(&I)) {
Type *MemAccessTy = I.getOperand(0)->getType();
NumStores += getMemoryOpCost(Instruction::Store, MemAccessTy, None, 0,
TTI::TCK_RecipThroughput);
}
}
unsigned const NumStoresVal = *NumStores.getValue();
unsigned const Max = (NumStoresVal ? (12 / NumStoresVal) : UINT_MAX);
if (HasCall) {
UP.FullUnrollMaxCount = Max;
UP.MaxCount = 1;
return;
}
UP.MaxCount = Max;
if (UP.MaxCount <= 1)
return;
UP.Partial = UP.Runtime = true;
UP.PartialThreshold = 75;
UP.DefaultUnrollRuntimeCount = 4;
UP.AllowExpensiveTripCount = true;
UP.Force = true;
}
void SystemZTTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE,
TTI::PeelingPreferences &PP) {
BaseT::getPeelingPreferences(L, SE, PP);
}
bool SystemZTTIImpl::isLSRCostLess(const TargetTransformInfo::LSRCost &C1,
const TargetTransformInfo::LSRCost &C2) {
return std::tie(C1.Insns, C1.NumRegs, C1.AddRecCost,
C1.NumIVMuls, C1.NumBaseAdds,
C1.ScaleCost, C1.SetupCost) <
std::tie(C2.Insns, C2.NumRegs, C2.AddRecCost,
C2.NumIVMuls, C2.NumBaseAdds,
C2.ScaleCost, C2.SetupCost);
}
unsigned SystemZTTIImpl::getNumberOfRegisters(unsigned ClassID) const {
bool Vector = (ClassID == 1);
if (!Vector)
return 14;
if (ST->hasVector())
return 32;
return 0;
}
TypeSize
SystemZTTIImpl::getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const {
switch (K) {
case TargetTransformInfo::RGK_Scalar:
return TypeSize::getFixed(64);
case TargetTransformInfo::RGK_FixedWidthVector:
return TypeSize::getFixed(ST->hasVector() ? 128 : 0);
case TargetTransformInfo::RGK_ScalableVector:
return TypeSize::getScalable(0);
}
llvm_unreachable("Unsupported register kind");
}
unsigned SystemZTTIImpl::getMinPrefetchStride(unsigned NumMemAccesses,
unsigned NumStridedMemAccesses,
unsigned NumPrefetches,
bool HasCall) const {
if (NumPrefetches > 16)
return UINT_MAX;
if (NumStridedMemAccesses > 32 && !HasCall &&
(NumMemAccesses - NumStridedMemAccesses) * 32 <= NumStridedMemAccesses)
return 1;
return ST->hasMiscellaneousExtensions3() ? 8192 : 2048;
}
bool SystemZTTIImpl::hasDivRemOp(Type *DataType, bool IsSigned) {
EVT VT = TLI->getValueType(DL, DataType);
return (VT.isScalarInteger() && TLI->isTypeLegal(VT));
}
static unsigned getScalarSizeInBits(Type *Ty) {
unsigned Size =
(Ty->isPtrOrPtrVectorTy() ? 64U : Ty->getScalarSizeInBits());
assert(Size > 0 && "Element must have non-zero size.");
return Size;
}
static unsigned getNumVectorRegs(Type *Ty) {
auto *VTy = cast<FixedVectorType>(Ty);
unsigned WideBits = getScalarSizeInBits(Ty) * VTy->getNumElements();
assert(WideBits > 0 && "Could not compute size of vector");
return ((WideBits % 128U) ? ((WideBits / 128U) + 1) : (WideBits / 128U));
}
InstructionCost SystemZTTIImpl::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) {
if (CostKind != TTI::TCK_RecipThroughput)
return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info,
Op2Info, Opd1PropInfo,
Opd2PropInfo, Args, CxtI);
unsigned ScalarBits = Ty->getScalarSizeInBits();
const unsigned DivInstrCost = 20;
const unsigned DivMulSeqCost = 10;
const unsigned SDivPow2Cost = 4;
bool SignedDivRem =
Opcode == Instruction::SDiv || Opcode == Instruction::SRem;
bool UnsignedDivRem =
Opcode == Instruction::UDiv || Opcode == Instruction::URem;
bool DivRemConst = false;
bool DivRemConstPow2 = false;
if ((SignedDivRem || UnsignedDivRem) && Args.size() == 2) {
if (const Constant *C = dyn_cast<Constant>(Args[1])) {
const ConstantInt *CVal =
(C->getType()->isVectorTy()
? dyn_cast_or_null<const ConstantInt>(C->getSplatValue())
: dyn_cast<const ConstantInt>(C));
if (CVal && (CVal->getValue().isPowerOf2() ||
CVal->getValue().isNegatedPowerOf2()))
DivRemConstPow2 = true;
else
DivRemConst = true;
}
}
if (!Ty->isVectorTy()) {
if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub ||
Opcode == Instruction::FMul || Opcode == Instruction::FDiv)
return 1;
if (Opcode == Instruction::FRem)
return LIBCALL_COST;
if (Args.size() == 2 && ST->hasMiscellaneousExtensions3()) {
if (Opcode == Instruction::Xor) {
for (const Value *A : Args) {
if (const Instruction *I = dyn_cast<Instruction>(A))
if (I->hasOneUse() &&
(I->getOpcode() == Instruction::And ||
I->getOpcode() == Instruction::Or ||
I->getOpcode() == Instruction::Xor))
return 0;
}
}
else if (Opcode == Instruction::Or || Opcode == Instruction::And) {
for (const Value *A : Args) {
if (const Instruction *I = dyn_cast<Instruction>(A))
if (I->hasOneUse() && I->getOpcode() == Instruction::Xor)
return 0;
}
}
}
if (Opcode == Instruction::Or)
return 1;
if (Opcode == Instruction::Xor && ScalarBits == 1) {
if (ST->hasLoadStoreOnCond2())
return 5; return 7; }
if (DivRemConstPow2)
return (SignedDivRem ? SDivPow2Cost : 1);
if (DivRemConst)
return DivMulSeqCost;
if (SignedDivRem || UnsignedDivRem)
return DivInstrCost;
}
else if (ST->hasVector()) {
auto *VTy = cast<FixedVectorType>(Ty);
unsigned VF = VTy->getNumElements();
unsigned NumVectors = getNumVectorRegs(Ty);
if (Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
Opcode == Instruction::AShr) {
return NumVectors;
}
if (DivRemConstPow2)
return (NumVectors * (SignedDivRem ? SDivPow2Cost : 1));
if (DivRemConst) {
SmallVector<Type *> Tys(Args.size(), Ty);
return VF * DivMulSeqCost + getScalarizationOverhead(VTy, Args, Tys);
}
if ((SignedDivRem || UnsignedDivRem) && VF > 4)
return 1000;
if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub ||
Opcode == Instruction::FMul || Opcode == Instruction::FDiv) {
switch (ScalarBits) {
case 32: {
if (ST->hasVectorEnhancements1())
return NumVectors;
InstructionCost ScalarCost =
getArithmeticInstrCost(Opcode, Ty->getScalarType(), CostKind);
SmallVector<Type *> Tys(Args.size(), Ty);
InstructionCost Cost =
(VF * ScalarCost) + getScalarizationOverhead(VTy, Args, Tys);
if (VF == 2)
Cost *= 2;
return Cost;
}
case 64:
case 128:
return NumVectors;
default:
break;
}
}
if (Opcode == Instruction::FRem) {
SmallVector<Type *> Tys(Args.size(), Ty);
InstructionCost Cost =
(VF * LIBCALL_COST) + getScalarizationOverhead(VTy, Args, Tys);
if (VF == 2 && ScalarBits == 32)
Cost *= 2;
return Cost;
}
}
return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info, Op2Info,
Opd1PropInfo, Opd2PropInfo, Args, CxtI);
}
InstructionCost SystemZTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
VectorType *Tp,
ArrayRef<int> Mask, int Index,
VectorType *SubTp,
ArrayRef<const Value *> Args) {
Kind = improveShuffleKindFromMask(Kind, Mask);
if (ST->hasVector()) {
unsigned NumVectors = getNumVectorRegs(Tp);
if (Tp->getScalarType()->isFP128Ty())
return (Kind == TargetTransformInfo::SK_Broadcast ? NumVectors - 1 : 0);
switch (Kind) {
case TargetTransformInfo::SK_ExtractSubvector:
return (Index == 0 ? 0 : NumVectors);
case TargetTransformInfo::SK_Broadcast:
return NumVectors - 1;
default:
return NumVectors;
}
}
return BaseT::getShuffleCost(Kind, Tp, Mask, Index, SubTp);
}
static unsigned getElSizeLog2Diff(Type *Ty0, Type *Ty1) {
unsigned Bits0 = Ty0->getScalarSizeInBits();
unsigned Bits1 = Ty1->getScalarSizeInBits();
if (Bits1 > Bits0)
return (Log2_32(Bits1) - Log2_32(Bits0));
return (Log2_32(Bits0) - Log2_32(Bits1));
}
unsigned SystemZTTIImpl::
getVectorTruncCost(Type *SrcTy, Type *DstTy) {
assert (SrcTy->isVectorTy() && DstTy->isVectorTy());
assert(SrcTy->getPrimitiveSizeInBits().getFixedSize() >
DstTy->getPrimitiveSizeInBits().getFixedSize() &&
"Packing must reduce size of vector type.");
assert(cast<FixedVectorType>(SrcTy)->getNumElements() ==
cast<FixedVectorType>(DstTy)->getNumElements() &&
"Packing should not change number of elements.");
unsigned NumParts = getNumVectorRegs(SrcTy);
if (NumParts <= 2)
return 1;
unsigned Cost = 0;
unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy);
unsigned VF = cast<FixedVectorType>(SrcTy)->getNumElements();
for (unsigned P = 0; P < Log2Diff; ++P) {
if (NumParts > 1)
NumParts /= 2;
Cost += NumParts;
}
if (VF == 8 && SrcTy->getScalarSizeInBits() == 64 &&
DstTy->getScalarSizeInBits() == 8)
Cost--;
return Cost;
}
unsigned SystemZTTIImpl::
getVectorBitmaskConversionCost(Type *SrcTy, Type *DstTy) {
assert (SrcTy->isVectorTy() && DstTy->isVectorTy() &&
"Should only be called with vector types.");
unsigned PackCost = 0;
unsigned SrcScalarBits = SrcTy->getScalarSizeInBits();
unsigned DstScalarBits = DstTy->getScalarSizeInBits();
unsigned Log2Diff = getElSizeLog2Diff(SrcTy, DstTy);
if (SrcScalarBits > DstScalarBits)
PackCost = getVectorTruncCost(SrcTy, DstTy);
else if (SrcScalarBits < DstScalarBits) {
unsigned DstNumParts = getNumVectorRegs(DstTy);
PackCost = Log2Diff * DstNumParts;
PackCost += DstNumParts - 1;
}
return PackCost;
}
static Type *getCmpOpsType(const Instruction *I, unsigned VF = 1) {
Type *OpTy = nullptr;
if (CmpInst *CI = dyn_cast<CmpInst>(I->getOperand(0)))
OpTy = CI->getOperand(0)->getType();
else if (Instruction *LogicI = dyn_cast<Instruction>(I->getOperand(0)))
if (LogicI->getNumOperands() == 2)
if (CmpInst *CI0 = dyn_cast<CmpInst>(LogicI->getOperand(0)))
if (isa<CmpInst>(LogicI->getOperand(1)))
OpTy = CI0->getOperand(0)->getType();
if (OpTy != nullptr) {
if (VF == 1) {
assert (!OpTy->isVectorTy() && "Expected scalar type");
return OpTy;
}
Type *ElTy = OpTy->getScalarType();
return FixedVectorType::get(ElTy, VF);
}
return nullptr;
}
unsigned SystemZTTIImpl::
getBoolVecToIntConversionCost(unsigned Opcode, Type *Dst,
const Instruction *I) {
auto *DstVTy = cast<FixedVectorType>(Dst);
unsigned VF = DstVTy->getNumElements();
unsigned Cost = 0;
Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr);
if (CmpOpTy != nullptr)
Cost = getVectorBitmaskConversionCost(CmpOpTy, Dst);
if (Opcode == Instruction::ZExt || Opcode == Instruction::UIToFP)
Cost += getNumVectorRegs(Dst);
return Cost;
}
InstructionCost SystemZTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
Type *Src,
TTI::CastContextHint CCH,
TTI::TargetCostKind CostKind,
const Instruction *I) {
if (CostKind == TTI::TCK_CodeSize || CostKind == TTI::TCK_SizeAndLatency) {
auto BaseCost = BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
return BaseCost == 0 ? BaseCost : 1;
}
unsigned DstScalarBits = Dst->getScalarSizeInBits();
unsigned SrcScalarBits = Src->getScalarSizeInBits();
if (!Src->isVectorTy()) {
assert (!Dst->isVectorTy());
if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP) {
if (SrcScalarBits >= 32 ||
(I != nullptr && isa<LoadInst>(I->getOperand(0))))
return 1;
return SrcScalarBits > 1 ? 2 : 5 ;
}
if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
Src->isIntegerTy(1)) {
if (ST->hasLoadStoreOnCond2())
return 2;
unsigned Cost = 0;
if (Opcode == Instruction::SExt)
Cost = (DstScalarBits < 64 ? 3 : 4);
if (Opcode == Instruction::ZExt)
Cost = 3;
Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I) : nullptr);
if (CmpOpTy != nullptr && CmpOpTy->isFloatingPointTy())
Cost++;
return Cost;
}
}
else if (ST->hasVector()) {
auto *SrcVecTy = cast<FixedVectorType>(Src);
auto *DstVecTy = dyn_cast<FixedVectorType>(Dst);
if (!DstVecTy) {
return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
}
unsigned VF = SrcVecTy->getNumElements();
unsigned NumDstVectors = getNumVectorRegs(Dst);
unsigned NumSrcVectors = getNumVectorRegs(Src);
if (Opcode == Instruction::Trunc) {
if (Src->getScalarSizeInBits() == Dst->getScalarSizeInBits())
return 0; return getVectorTruncCost(Src, Dst);
}
if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) {
if (SrcScalarBits >= 8) {
if (Opcode == Instruction::ZExt)
return NumDstVectors;
unsigned NumUnpacks = getElSizeLog2Diff(Src, Dst);
unsigned NumSrcVectorOps =
(NumUnpacks > 1 ? (NumDstVectors - NumSrcVectors)
: (NumDstVectors / 2));
return (NumUnpacks * NumDstVectors) + NumSrcVectorOps;
}
else if (SrcScalarBits == 1)
return getBoolVecToIntConversionCost(Opcode, Dst, I);
}
if (Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP ||
Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI) {
if (DstScalarBits == 64 || ST->hasVectorEnhancements2()) {
if (SrcScalarBits == DstScalarBits)
return NumDstVectors;
if (SrcScalarBits == 1)
return getBoolVecToIntConversionCost(Opcode, Dst, I) + NumDstVectors;
}
InstructionCost ScalarCost = getCastInstrCost(
Opcode, Dst->getScalarType(), Src->getScalarType(), CCH, CostKind);
InstructionCost TotCost = VF * ScalarCost;
bool NeedsInserts = true, NeedsExtracts = true;
if (DstScalarBits == 128 &&
(Opcode == Instruction::SIToFP || Opcode == Instruction::UIToFP))
NeedsInserts = false;
if (SrcScalarBits == 128 &&
(Opcode == Instruction::FPToSI || Opcode == Instruction::FPToUI))
NeedsExtracts = false;
TotCost += getScalarizationOverhead(SrcVecTy, false, NeedsExtracts);
TotCost += getScalarizationOverhead(DstVecTy, NeedsInserts, false);
if (VF == 2 && SrcScalarBits == 32 && DstScalarBits == 32)
TotCost *= 2;
return TotCost;
}
if (Opcode == Instruction::FPTrunc) {
if (SrcScalarBits == 128) return VF +
getScalarizationOverhead(DstVecTy, true, false);
else return VF / 2 + std::max(1U, VF / 4 );
}
if (Opcode == Instruction::FPExt) {
if (SrcScalarBits == 32 && DstScalarBits == 64) {
return VF * 2;
}
return VF + getScalarizationOverhead(SrcVecTy, false, true);
}
}
return BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
}
static unsigned getOperandsExtensionCost(const Instruction *I) {
unsigned ExtCost = 0;
for (Value *Op : I->operands())
if (!isa<LoadInst>(Op) && !isa<ConstantInt>(Op))
ExtCost++;
return ExtCost;
}
InstructionCost SystemZTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Type *CondTy,
CmpInst::Predicate VecPred,
TTI::TargetCostKind CostKind,
const Instruction *I) {
if (CostKind != TTI::TCK_RecipThroughput)
return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind);
if (!ValTy->isVectorTy()) {
switch (Opcode) {
case Instruction::ICmp: {
unsigned ScalarBits = ValTy->getScalarSizeInBits();
if (I != nullptr && ScalarBits >= 32)
if (LoadInst *Ld = dyn_cast<LoadInst>(I->getOperand(0)))
if (const ConstantInt *C = dyn_cast<ConstantInt>(I->getOperand(1)))
if (!Ld->hasOneUse() && Ld->getParent() == I->getParent() &&
C->isZero())
return 0;
unsigned Cost = 1;
if (ValTy->isIntegerTy() && ValTy->getScalarSizeInBits() <= 16)
Cost += (I != nullptr ? getOperandsExtensionCost(I) : 2);
return Cost;
}
case Instruction::Select:
if (ValTy->isFloatingPointTy())
return 4; return 1; }
}
else if (ST->hasVector()) {
unsigned VF = cast<FixedVectorType>(ValTy)->getNumElements();
if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) {
unsigned PredicateExtraCost = 0;
if (I != nullptr) {
switch (cast<CmpInst>(I)->getPredicate()) {
case CmpInst::Predicate::ICMP_NE:
case CmpInst::Predicate::ICMP_UGE:
case CmpInst::Predicate::ICMP_ULE:
case CmpInst::Predicate::ICMP_SGE:
case CmpInst::Predicate::ICMP_SLE:
PredicateExtraCost = 1;
break;
case CmpInst::Predicate::FCMP_ONE:
case CmpInst::Predicate::FCMP_ORD:
case CmpInst::Predicate::FCMP_UEQ:
case CmpInst::Predicate::FCMP_UNO:
PredicateExtraCost = 2;
break;
default:
break;
}
}
unsigned CmpCostPerVector = (ValTy->getScalarType()->isFloatTy() ? 10 : 1);
unsigned NumVecs_cmp = getNumVectorRegs(ValTy);
unsigned Cost = (NumVecs_cmp * (CmpCostPerVector + PredicateExtraCost));
return Cost;
}
else { assert (Opcode == Instruction::Select);
unsigned PackCost = 0;
Type *CmpOpTy = ((I != nullptr) ? getCmpOpsType(I, VF) : nullptr);
if (CmpOpTy != nullptr)
PackCost =
getVectorBitmaskConversionCost(CmpOpTy, ValTy);
return getNumVectorRegs(ValTy) + PackCost;
}
}
return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind);
}
InstructionCost SystemZTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
unsigned Index) {
if (Opcode == Instruction::InsertElement && Val->isIntOrIntVectorTy(64))
return ((Index % 2 == 0) ? 1 : 0);
if (Opcode == Instruction::ExtractElement) {
int Cost = ((getScalarSizeInBits(Val) == 1) ? 2 : 1);
if (Index == 0 && Val->isIntOrIntVectorTy())
Cost += 1;
return Cost;
}
return BaseT::getVectorInstrCost(Opcode, Val, Index);
}
bool SystemZTTIImpl::
isFoldableLoad(const LoadInst *Ld, const Instruction *&FoldedValue) {
if (!Ld->hasOneUse())
return false;
FoldedValue = Ld;
const Instruction *UserI = cast<Instruction>(*Ld->user_begin());
unsigned LoadedBits = getScalarSizeInBits(Ld->getType());
unsigned TruncBits = 0;
unsigned SExtBits = 0;
unsigned ZExtBits = 0;
if (UserI->hasOneUse()) {
unsigned UserBits = UserI->getType()->getScalarSizeInBits();
if (isa<TruncInst>(UserI))
TruncBits = UserBits;
else if (isa<SExtInst>(UserI))
SExtBits = UserBits;
else if (isa<ZExtInst>(UserI))
ZExtBits = UserBits;
}
if (TruncBits || SExtBits || ZExtBits) {
FoldedValue = UserI;
UserI = cast<Instruction>(*UserI->user_begin());
}
if ((UserI->getOpcode() == Instruction::Sub ||
UserI->getOpcode() == Instruction::SDiv ||
UserI->getOpcode() == Instruction::UDiv) &&
UserI->getOperand(1) != FoldedValue)
return false; unsigned LoadOrTruncBits =
((SExtBits || ZExtBits) ? 0 : (TruncBits ? TruncBits : LoadedBits));
switch (UserI->getOpcode()) {
case Instruction::Add: case Instruction::Sub:
case Instruction::ICmp:
if (LoadedBits == 32 && ZExtBits == 64)
return true;
LLVM_FALLTHROUGH;
case Instruction::Mul: if (UserI->getOpcode() != Instruction::ICmp) {
if (LoadedBits == 16 &&
(SExtBits == 32 ||
(SExtBits == 64 && ST->hasMiscellaneousExtensions2())))
return true;
if (LoadOrTruncBits == 16)
return true;
}
LLVM_FALLTHROUGH;
case Instruction::SDiv: if (LoadedBits == 32 && SExtBits == 64)
return true;
LLVM_FALLTHROUGH;
case Instruction::UDiv:
case Instruction::And:
case Instruction::Or:
case Instruction::Xor:
if (UserI->getOpcode() == Instruction::ICmp)
if (ConstantInt *CI = dyn_cast<ConstantInt>(UserI->getOperand(1)))
if (CI->getValue().isIntN(16))
return true;
return (LoadOrTruncBits == 32 || LoadOrTruncBits == 64);
break;
}
return false;
}
static bool isBswapIntrinsicCall(const Value *V) {
if (const Instruction *I = dyn_cast<Instruction>(V))
if (auto *CI = dyn_cast<CallInst>(I))
if (auto *F = CI->getCalledFunction())
if (F->getIntrinsicID() == Intrinsic::bswap)
return true;
return false;
}
InstructionCost SystemZTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
MaybeAlign Alignment,
unsigned AddressSpace,
TTI::TargetCostKind CostKind,
const Instruction *I) {
assert(!Src->isVoidTy() && "Invalid type");
if (CostKind != TTI::TCK_RecipThroughput)
return 1;
if (!Src->isVectorTy() && Opcode == Instruction::Load && I != nullptr) {
const Instruction *FoldedValue = nullptr;
if (isFoldableLoad(cast<LoadInst>(I), FoldedValue)) {
const Instruction *UserI = cast<Instruction>(*FoldedValue->user_begin());
assert (UserI->getNumOperands() == 2 && "Expected a binop.");
for (unsigned i = 0; i < 2; ++i) {
if (UserI->getOperand(i) == FoldedValue)
continue;
if (Instruction *OtherOp = dyn_cast<Instruction>(UserI->getOperand(i))){
LoadInst *OtherLoad = dyn_cast<LoadInst>(OtherOp);
if (!OtherLoad &&
(isa<TruncInst>(OtherOp) || isa<SExtInst>(OtherOp) ||
isa<ZExtInst>(OtherOp)))
OtherLoad = dyn_cast<LoadInst>(OtherOp->getOperand(0));
if (OtherLoad && isFoldableLoad(OtherLoad, FoldedValue))
return i == 0; }
}
return 0; }
}
unsigned NumOps =
(Src->isVectorTy() ? getNumVectorRegs(Src) : getNumberOfParts(Src));
if (((!Src->isVectorTy() && NumOps == 1) || ST->hasVectorEnhancements2()) &&
I != nullptr) {
if (Opcode == Instruction::Load && I->hasOneUse()) {
const Instruction *LdUser = cast<Instruction>(*I->user_begin());
if (isBswapIntrinsicCall(LdUser) &&
(!LdUser->hasOneUse() || !isa<StoreInst>(*LdUser->user_begin())))
return 0;
}
else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
const Value *StoredVal = SI->getValueOperand();
if (StoredVal->hasOneUse() && isBswapIntrinsicCall(StoredVal))
return 0;
}
}
if (Src->getScalarSizeInBits() == 128)
NumOps *= 2;
return NumOps;
}
InstructionCost SystemZTTIImpl::getInterleavedMemoryOpCost(
unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind,
bool UseMaskForCond, bool UseMaskForGaps) {
if (UseMaskForCond || UseMaskForGaps)
return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
Alignment, AddressSpace, CostKind,
UseMaskForCond, UseMaskForGaps);
assert(isa<VectorType>(VecTy) &&
"Expect a vector type for interleaved memory op");
unsigned NumElts = cast<FixedVectorType>(VecTy)->getNumElements();
assert(Factor > 1 && NumElts % Factor == 0 && "Invalid interleave factor");
unsigned VF = NumElts / Factor;
unsigned NumEltsPerVecReg = (128U / getScalarSizeInBits(VecTy));
unsigned NumVectorMemOps = getNumVectorRegs(VecTy);
unsigned NumPermutes = 0;
if (Opcode == Instruction::Load) {
BitVector UsedInsts(NumVectorMemOps, false);
std::vector<BitVector> ValueVecs(Factor, BitVector(NumVectorMemOps, false));
for (unsigned Index : Indices)
for (unsigned Elt = 0; Elt < VF; ++Elt) {
unsigned Vec = (Index + Elt * Factor) / NumEltsPerVecReg;
UsedInsts.set(Vec);
ValueVecs[Index].set(Vec);
}
NumVectorMemOps = UsedInsts.count();
for (unsigned Index : Indices) {
unsigned NumSrcVecs = ValueVecs[Index].count();
unsigned NumDstVecs = divideCeil(VF * getScalarSizeInBits(VecTy), 128U);
assert (NumSrcVecs >= NumDstVecs && "Expected at least as many sources");
NumPermutes += std::max(1U, NumSrcVecs - NumDstVecs);
}
} else {
unsigned NumSrcVecs = std::min(NumEltsPerVecReg, Factor);
unsigned NumDstVecs = NumVectorMemOps;
assert (NumSrcVecs > 1 && "Expected at least two source vectors.");
NumPermutes += (NumDstVecs * NumSrcVecs) - NumDstVecs;
}
return NumVectorMemOps + NumPermutes;
}
static int getVectorIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy) {
if (RetTy->isVectorTy() && ID == Intrinsic::bswap)
return getNumVectorRegs(RetTy); return -1;
}
InstructionCost
SystemZTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
TTI::TargetCostKind CostKind) {
InstructionCost Cost =
getVectorIntrinsicInstrCost(ICA.getID(), ICA.getReturnType());
if (Cost != -1)
return Cost;
return BaseT::getIntrinsicInstrCost(ICA, CostKind);
}