Skip to content

Commit f7da97d

Browse files
committed
[RISCV][CostModel] Remove inst cost of cmp inst in cmp-select sequence.
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 f7da97d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

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

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

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

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
4+
define void @icmp-select() {
5+
; CHECK-LABEL: 'icmp-select'
6+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %cmp1 = icmp slt i64 0, 1
77
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %select1 = select i1 %cmp1, i32 5, i32 4
88
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
99
;
1010
%cmp1 = icmp slt i64 0, 1
1111
%select1 = select i1 %cmp1, i32 5, i32 4
1212
ret void
1313
}
14+
15+
define void @fcmp-select() {
16+
; CHECK-LABEL: 'fcmp-select'
17+
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %fcmp1 = fcmp ogt float 0.000000e+00, 1.000000e+00
18+
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %select1 = select i1 %fcmp1, i32 5, i32 4
19+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
20+
;
21+
%fcmp1 = fcmp ogt float 0.0, 1.0
22+
%select1 = select i1 %fcmp1, i32 5, i32 4
23+
ret void
24+
}

llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ define void @double_stride_ptr_iv(ptr %p, ptr %p2, i64 %stride) {
715715
; STRIDED-NEXT: entry:
716716
; STRIDED-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
717717
; STRIDED-NEXT: [[TMP1:%.*]] = mul i64 [[TMP0]], 4
718-
; STRIDED-NEXT: [[TMP2:%.*]] = call i64 @llvm.umax.i64(i64 32, i64 [[TMP1]])
718+
; STRIDED-NEXT: [[TMP2:%.*]] = call i64 @llvm.umax.i64(i64 24, i64 [[TMP1]])
719719
; STRIDED-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 1024, [[TMP2]]
720720
; STRIDED-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]
721721
; STRIDED: vector.memcheck:

0 commit comments

Comments
 (0)