Skip to content

Commit 5106b22

Browse files
committed
[AArch64] Treat the icmp in icmp(and(..), 0) as free
As in https://godbolt.org/z/4dafd9Geq, the icmp from an And may use an Ands to set flags, meaning the icmp is free. This could also be done for add/sub, but those patterns often happen in the induction variable of a loop, making them quite performance sensitive. Differential Revision: https://reviews.llvm.org/D153611
1 parent 8ccf042 commit 5106b22

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,6 +2812,16 @@ InstructionCost AArch64TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
28122812
return LT.first * 4; // fcvtl + fcvtl + fcmp + xtn
28132813
}
28142814

2815+
// Treat the icmp in icmp(and, 0) as free, as we can make use of ands.
2816+
// FIXME: This can apply to more conditions and add/sub if it can be shown to
2817+
// be profitable.
2818+
if (ValTy->isIntegerTy() && ISD == ISD::SETCC && I &&
2819+
ICmpInst::isEquality(VecPred) &&
2820+
TLI->isTypeLegal(TLI->getValueType(DL, ValTy)) &&
2821+
match(I->getOperand(1), m_Zero()) &&
2822+
match(I->getOperand(0), m_And(m_Value(), m_Value())))
2823+
return 0;
2824+
28152825
// The base case handles scalable vectors fine for now, since it treats the
28162826
// cost as 1 * legalization cost.
28172827
return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind, I);

llvm/test/Analysis/CostModel/AArch64/cmp.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ define void @andcmp() {
6464
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %a16 = and i16 undef, undef
6565
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %c16 = icmp ne i16 %a16, 0
6666
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %a32 = and i32 undef, undef
67-
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %c32 = icmp eq i32 %a32, 0
67+
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %c32 = icmp eq i32 %a32, 0
6868
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %a64 = and i64 undef, undef
69-
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %c64 = icmp ne i64 %a64, 0
69+
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %c64 = icmp ne i64 %a64, 0
7070
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %a128 = and i128 undef, undef
7171
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %c128 = icmp eq i128 %a128, 0
7272
; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %av16i8 = and <16 x i8> undef, undef

0 commit comments

Comments
 (0)