Skip to content

Commit 9978f6a

Browse files
committed
[CostModel][X86] Reduce the extra costs for ICMP complex predicates when an operand is constant
In most cases, SETCC lowering will be able to simplify/commute the comparison by adjusting the constant. TODO: We still need to adjust ExtraCost based on CostKind Fixes llvm#80122
1 parent c16d0d1 commit 9978f6a

File tree

3 files changed

+892
-911
lines changed

3 files changed

+892
-911
lines changed

llvm/lib/Target/X86/X86TargetTransformInfo.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,6 +3090,7 @@ InstructionCost X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
30903090
InstructionCost ExtraCost = 0;
30913091
if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) {
30923092
// Some vector comparison predicates cost extra instructions.
3093+
// TODO: Adjust ExtraCost based on CostKind?
30933094
// TODO: Should we invert this and assume worst case cmp costs
30943095
// and reduce for particular predicates?
30953096
if (MTy.isVector() &&
@@ -3102,21 +3103,25 @@ InstructionCost X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
31023103
Pred == CmpInst::BAD_FCMP_PREDICATE))
31033104
Pred = cast<CmpInst>(I)->getPredicate();
31043105

3106+
bool CmpWithConstant = false;
3107+
if (auto *CmpInstr = dyn_cast_or_null<CmpInst>(I))
3108+
CmpWithConstant = isa<Constant>(CmpInstr->getOperand(1));
3109+
31053110
switch (Pred) {
31063111
case CmpInst::Predicate::ICMP_NE:
31073112
// xor(cmpeq(x,y),-1)
3108-
ExtraCost = 1;
3113+
ExtraCost = CmpWithConstant ? 0 : 1;
31093114
break;
31103115
case CmpInst::Predicate::ICMP_SGE:
31113116
case CmpInst::Predicate::ICMP_SLE:
31123117
// xor(cmpgt(x,y),-1)
3113-
ExtraCost = 1;
3118+
ExtraCost = CmpWithConstant ? 0 : 1;
31143119
break;
31153120
case CmpInst::Predicate::ICMP_ULT:
31163121
case CmpInst::Predicate::ICMP_UGT:
31173122
// cmpgt(xor(x,signbit),xor(y,signbit))
31183123
// xor(cmpeq(pmaxu(x,y),x),-1)
3119-
ExtraCost = 2;
3124+
ExtraCost = CmpWithConstant ? 1 : 2;
31203125
break;
31213126
case CmpInst::Predicate::ICMP_ULE:
31223127
case CmpInst::Predicate::ICMP_UGE:
@@ -3127,7 +3132,7 @@ InstructionCost X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
31273132
ExtraCost = 1;
31283133
} else {
31293134
// xor(cmpgt(xor(x,signbit),xor(y,signbit)),-1)
3130-
ExtraCost = 3;
3135+
ExtraCost = CmpWithConstant ? 2 : 3;
31313136
}
31323137
break;
31333138
case CmpInst::Predicate::FCMP_ONE:

0 commit comments

Comments
 (0)