Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 598d00e

Browse files
committed
[CostModel][X86] Add ICMP Predicate specific costs
First step towards PR40376, this patch adds support for getCmpSelInstrCost to use the (optional) Instruction CmpInst predicate to indicate the type of integer comparison we're performing and alter the costs accordingly. Differential Revision: https://reviews.llvm.org/D57013 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351810 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 49551c1 commit 598d00e

File tree

2 files changed

+1085
-1044
lines changed

2 files changed

+1085
-1044
lines changed

lib/Target/X86/X86TargetTransformInfo.cpp

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,47 @@ int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
16501650
int ISD = TLI->InstructionOpcodeToISD(Opcode);
16511651
assert(ISD && "Invalid opcode");
16521652

1653+
unsigned ExtraCost = 0;
1654+
if (I && (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)) {
1655+
// Some vector comparison predicates cost extra instructions.
1656+
if (MTy.isVector() &&
1657+
!((ST->hasXOP() && (!ST->hasAVX2() || MTy.is128BitVector())) ||
1658+
(ST->hasAVX512() && 32 <= MTy.getScalarSizeInBits()) ||
1659+
ST->hasBWI())) {
1660+
switch (cast<CmpInst>(I)->getPredicate()) {
1661+
case CmpInst::Predicate::ICMP_NE:
1662+
// xor(cmpeq(x,y),-1)
1663+
ExtraCost = 1;
1664+
break;
1665+
case CmpInst::Predicate::ICMP_SGE:
1666+
case CmpInst::Predicate::ICMP_SLE:
1667+
// xor(cmpgt(x,y),-1)
1668+
ExtraCost = 1;
1669+
break;
1670+
case CmpInst::Predicate::ICMP_ULT:
1671+
case CmpInst::Predicate::ICMP_UGT:
1672+
// cmpgt(xor(x,signbit),xor(y,signbit))
1673+
// xor(cmpeq(pmaxu(x,y),x),-1)
1674+
ExtraCost = 2;
1675+
break;
1676+
case CmpInst::Predicate::ICMP_ULE:
1677+
case CmpInst::Predicate::ICMP_UGE:
1678+
if ((ST->hasSSE41() && MTy.getScalarSizeInBits() == 32) ||
1679+
(ST->hasSSE2() && MTy.getScalarSizeInBits() < 32)) {
1680+
// cmpeq(psubus(x,y),0)
1681+
// cmpeq(pminu(x,y),x)
1682+
ExtraCost = 1;
1683+
} else {
1684+
// xor(cmpgt(xor(x,signbit),xor(y,signbit)),-1)
1685+
ExtraCost = 3;
1686+
}
1687+
break;
1688+
default:
1689+
break;
1690+
}
1691+
}
1692+
}
1693+
16531694
static const CostTblEntry AVX512BWCostTbl[] = {
16541695
{ ISD::SETCC, MVT::v32i16, 1 },
16551696
{ ISD::SETCC, MVT::v64i8, 1 },
@@ -1738,35 +1779,35 @@ int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
17381779

17391780
if (ST->hasBWI())
17401781
if (const auto *Entry = CostTableLookup(AVX512BWCostTbl, ISD, MTy))
1741-
return LT.first * Entry->Cost;
1782+
return LT.first * (ExtraCost + Entry->Cost);
17421783

17431784
if (ST->hasAVX512())
17441785
if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy))
1745-
return LT.first * Entry->Cost;
1786+
return LT.first * (ExtraCost + Entry->Cost);
17461787

17471788
if (ST->hasAVX2())
17481789
if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy))
1749-
return LT.first * Entry->Cost;
1790+
return LT.first * (ExtraCost + Entry->Cost);
17501791

17511792
if (ST->hasAVX())
17521793
if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy))
1753-
return LT.first * Entry->Cost;
1794+
return LT.first * (ExtraCost + Entry->Cost);
17541795

17551796
if (ST->hasSSE42())
17561797
if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy))
1757-
return LT.first * Entry->Cost;
1798+
return LT.first * (ExtraCost + Entry->Cost);
17581799

17591800
if (ST->hasSSE41())
17601801
if (const auto *Entry = CostTableLookup(SSE41CostTbl, ISD, MTy))
1761-
return LT.first * Entry->Cost;
1802+
return LT.first * (ExtraCost + Entry->Cost);
17621803

17631804
if (ST->hasSSE2())
17641805
if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy))
1765-
return LT.first * Entry->Cost;
1806+
return LT.first * (ExtraCost + Entry->Cost);
17661807

17671808
if (ST->hasSSE1())
17681809
if (const auto *Entry = CostTableLookup(SSE1CostTbl, ISD, MTy))
1769-
return LT.first * Entry->Cost;
1810+
return LT.first * (ExtraCost + Entry->Cost);
17701811

17711812
return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
17721813
}

0 commit comments

Comments
 (0)