[CostModel] Unify Shuffle and InsertElement Costs

Extract the existing code from getInstructionThroughput into
TTImpl::getUserCost. The duplicated code in the AMDGPU backend has
also been removed.

Differential Revision: https://reviews.llvm.org/D81448
This commit is contained in:
Sam Parker 2020-06-09 09:04:53 +01:00
parent fa8bff0cd1
commit 09d30cb977
4 changed files with 39 additions and 83 deletions

View File

@ -909,6 +909,41 @@ public:
return TargetTTI->getCmpSelInstrCost(Opcode, ValTy, U->getType(),
CostKind, I);
}
case Instruction::InsertElement: {
auto *IE = cast<InsertElementInst>(U);
auto *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
unsigned Idx = CI ? CI->getZExtValue() : -1;
return TargetTTI->getVectorInstrCost(Opcode, Ty, Idx);
}
case Instruction::ShuffleVector: {
auto *Shuffle = cast<ShuffleVectorInst>(U);
auto *VecTy = cast<VectorType>(U->getType());
auto *VecSrcTy = cast<VectorType>(U->getOperand(0)->getType());
// TODO: Identify and add costs for insert subvector, etc.
int SubIndex;
if (Shuffle->isExtractSubvectorMask(SubIndex))
return TargetTTI->getShuffleCost(TTI::SK_ExtractSubvector, VecSrcTy,
SubIndex, VecTy);
else if (Shuffle->changesLength())
return CostKind == TTI::TCK_RecipThroughput ? -1 : 1;
else if (Shuffle->isIdentity())
return 0;
else if (Shuffle->isReverse())
return TargetTTI->getShuffleCost(TTI::SK_Reverse, VecTy, 0, nullptr);
else if (Shuffle->isSelect())
return TargetTTI->getShuffleCost(TTI::SK_Select, VecTy, 0, nullptr);
else if (Shuffle->isTranspose())
return TargetTTI->getShuffleCost(TTI::SK_Transpose, VecTy, 0, nullptr);
else if (Shuffle->isZeroEltSplat())
return TargetTTI->getShuffleCost(TTI::SK_Broadcast, VecTy, 0, nullptr);
else if (Shuffle->isSingleSource())
return TargetTTI->getShuffleCost(TTI::SK_PermuteSingleSrc, VecTy, 0,
nullptr);
return TargetTTI->getShuffleCost(TTI::SK_PermuteTwoSrc, VecTy, 0,
nullptr);
}
}
// By default, just classify everything as 'basic'.
return TTI::TCC_Basic;

View File

@ -1357,49 +1357,10 @@ int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
return getVectorInstrCost(I->getOpcode(), EEI->getOperand(0)->getType(),
Idx);
}
case Instruction::InsertElement: {
const InsertElementInst *IE = cast<InsertElementInst>(I);
ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
unsigned Idx = -1;
if (CI)
Idx = CI->getZExtValue();
return getVectorInstrCost(I->getOpcode(), IE->getType(), Idx);
}
case Instruction::InsertElement:
case Instruction::ExtractValue:
return 0; // Model all ExtractValue nodes as free.
case Instruction::ShuffleVector: {
const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
auto *Ty = cast<VectorType>(Shuffle->getType());
auto *SrcTy = cast<VectorType>(Shuffle->getOperand(0)->getType());
// TODO: Identify and add costs for insert subvector, etc.
int SubIndex;
if (Shuffle->isExtractSubvectorMask(SubIndex))
return TTIImpl->getShuffleCost(SK_ExtractSubvector, SrcTy, SubIndex, Ty);
if (Shuffle->changesLength())
return -1;
if (Shuffle->isIdentity())
return 0;
if (Shuffle->isReverse())
return TTIImpl->getShuffleCost(SK_Reverse, Ty, 0, nullptr);
if (Shuffle->isSelect())
return TTIImpl->getShuffleCost(SK_Select, Ty, 0, nullptr);
if (Shuffle->isTranspose())
return TTIImpl->getShuffleCost(SK_Transpose, Ty, 0, nullptr);
if (Shuffle->isZeroEltSplat())
return TTIImpl->getShuffleCost(SK_Broadcast, Ty, 0, nullptr);
if (Shuffle->isSingleSource())
return TTIImpl->getShuffleCost(SK_PermuteSingleSrc, Ty, 0, nullptr);
return TTIImpl->getShuffleCost(SK_PermuteTwoSrc, Ty, 0, nullptr);
}
case Instruction::ShuffleVector:
return getUserCost(I, CostKind);
case Instruction::Call:
return getUserCost(I, CostKind);
default:

View File

@ -999,46 +999,6 @@ GCNTTIImpl::getUserCost(const User *U, ArrayRef<const Value *> Operands,
Idx = CI->getZExtValue();
return getVectorInstrCost(I->getOpcode(), I->getOperand(0)->getType(), Idx);
}
case Instruction::InsertElement: {
ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(2));
unsigned Idx = -1;
if (CI)
Idx = CI->getZExtValue();
return getVectorInstrCost(I->getOpcode(), I->getType(), Idx);
}
case Instruction::ShuffleVector: {
const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
auto *Ty = cast<VectorType>(Shuffle->getType());
auto *SrcTy = cast<VectorType>(Shuffle->getOperand(0)->getType());
// TODO: Identify and add costs for insert subvector, etc.
int SubIndex;
if (Shuffle->isExtractSubvectorMask(SubIndex))
return getShuffleCost(TTI::SK_ExtractSubvector, SrcTy, SubIndex, Ty);
if (Shuffle->changesLength())
return BaseT::getUserCost(U, Operands, CostKind);
if (Shuffle->isIdentity())
return 0;
if (Shuffle->isReverse())
return getShuffleCost(TTI::SK_Reverse, Ty, 0, nullptr);
if (Shuffle->isSelect())
return getShuffleCost(TTI::SK_Select, Ty, 0, nullptr);
if (Shuffle->isTranspose())
return getShuffleCost(TTI::SK_Transpose, Ty, 0, nullptr);
if (Shuffle->isZeroEltSplat())
return getShuffleCost(TTI::SK_Broadcast, Ty, 0, nullptr);
if (Shuffle->isSingleSource())
return getShuffleCost(TTI::SK_PermuteSingleSrc, Ty, 0, nullptr);
return getShuffleCost(TTI::SK_PermuteTwoSrc, Ty, 0, nullptr);
}
case Instruction::FNeg:
return getArithmeticInstrCost(I->getOpcode(), I->getType(), CostKind,
TTI::OK_AnyValue, TTI::OK_AnyValue,

View File

@ -210,7 +210,7 @@ CF240: ; preds = %CF240, %CF260, %CF
br i1 %E112, label %CF240, label %CF254
CF254: ; preds = %CF254, %CF267, %CF264, %CF240
%Shuff113 = shufflevector <2 x i32> %I68, <2 x i32> zeroinitializer, <2 x i32> undef
%Shuff113 = shufflevector <2 x i32> %I68, <2 x i32> zeroinitializer, <2 x i32><i32 undef, i32 0>
%I114 = insertelement <4 x i16> zeroinitializer, i16 27357, i32 3
%B115 = and i16 %Sl102, %Sl11
%FC116 = uitofp i16 %B115 to double