Skip to content

Commit 0550d47

Browse files
committed
[RISCV][CostModel] Remove cost of cmp inst in cmp+select with SFB.
With ShortFowrardBranchOpt(SFB) or ConditionalMoveFusion, scalar ICmp and scalar Select instructinos will lower to SELECT_CC and lower to PseudoCCMOVGPR which will generate a conditional branch instr and a move instr. The cost of scalar (ICmp + Select) = (0 + Select instr cost).
1 parent c577ebd commit 0550d47

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,17 @@ InstructionCost RISCVTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
14431443
}
14441444
}
14451445

1446+
// With ShortForwardBranchOpt or ConditionalMoveFusion, scalar icmp + select
1447+
// instructions will lower to SELECT_CC and lower to PseudoCCMOVGPR which will
1448+
// generate a conditional branch + mv. The cost of scalar (icmp + select) will
1449+
// be (0 + select instr cost).
1450+
if (ST->hasConditionalMoveFusion() && I && isa<ICmpInst>(I) &&
1451+
!ValTy->isVectorTy() && I->hasOneUser() &&
1452+
isa<SelectInst>(I->user_back()) &&
1453+
!I->user_back()->getType()->isVectorTy() &&
1454+
I->user_back()->getOperand(0) == I)
1455+
return 0;
1456+
14461457
// TODO: Add cost for scalar type.
14471458

14481459
return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind, I);
Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
2-
; RUN: opt < %s -mtriple=riscv64 -passes="print<cost-model>" -cost-kind=throughput 2>&1 -disable-output | FileCheck %s --check-prefixes=CHECK
2+
; RUN: opt < %s -mtriple=riscv64 -mcpu=sifive-u74 -passes="print<cost-model>" -cost-kind=throughput 2>&1 -disable-output | FileCheck %s --check-prefixes=SFB64
3+
; RUN: opt < %s -mtriple=riscv64 -passes="print<cost-model>" -cost-kind=throughput 2>&1 -disable-output | FileCheck %s --check-prefixes=RV64
34

4-
define void @cmp-select() {
5-
; CHECK-LABEL: 'cmp-select'
6-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %cmp1 = icmp slt i64 0, 1
7-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %select1 = select i1 %cmp1, i32 5, i32 4
8-
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
9-
;
10-
%cmp1 = icmp slt i64 0, 1
11-
%select1 = select i1 %cmp1, i32 5, i32 4
5+
define i64 @icmp-select(i64 %a, i64 %b) {
6+
; SFB64-LABEL: 'icmp-select'
7+
; SFB64-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %cmp1 = icmp slt i64 0, 1
8+
; SFB64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %select1 = select i1 %cmp1, i32 5, i32 4
9+
; SFB64-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
10+
;
11+
; RV64-LABEL: 'icmp-select'
12+
; RV64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %cmp1 = icmp slt i64 0, 1
13+
; RV64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %select1 = select i1 %cmp1, i32 5, i32 4
14+
; RV64-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
15+
;
16+
%cmp1 = icmp slt i64 %a, %b
17+
%select1 = select i1 %cmp1, i64 %a, i64 %b
18+
ret i64 %select1
19+
}
20+
21+
define void @fcmp-select() {
22+
; SFB64-LABEL: 'fcmp-select'
23+
; SFB64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %fcmp1 = fcmp ogt float 0.000000e+00, 1.000000e+00
24+
; SFB64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %select1 = select i1 %fcmp1, i32 5, i32 4
25+
; SFB64-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
26+
;
27+
; RV64-LABEL: 'fcmp-select'
28+
; RV64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %fcmp1 = fcmp ogt float 0.000000e+00, 1.000000e+00
29+
; RV64-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %select1 = select i1 %fcmp1, i32 5, i32 4
30+
; RV64-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
31+
;
32+
%fcmp1 = fcmp ogt float 0.0, 1.0
33+
%select1 = select i1 %fcmp1, i32 5, i32 4
1234
ret void
1335
}

0 commit comments

Comments
 (0)