Skip to content

Commit 23aff11

Browse files
authored
[RISCV] Add FeaturePredictableSelectIsExpensive
This information is used in CGP/SelectOpt to decide when to convert selects into branches. Reviewers: dtcxzyw, mgudim, asb, preames, topperc, lukel97 Reviewed By: dtcxzyw, topperc, lukel97 Pull Request: llvm#97708
1 parent 9e21174 commit 23aff11

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

llvm/lib/Target/RISCV/RISCVFeatures.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,10 @@ def FeatureUnalignedVectorMem
12621262
def FeaturePostRAScheduler : SubtargetFeature<"use-postra-scheduler",
12631263
"UsePostRAScheduler", "true", "Schedule again after register allocation">;
12641264

1265+
def FeaturePredictableSelectIsExpensive
1266+
: SubtargetFeature<"predictable-select-expensive", "PredictableSelectIsExpensive", "true",
1267+
"Prefer likely predicted branches over selects">;
1268+
12651269
def TuneNoOptimizedZeroStrideLoad
12661270
: SubtargetFeature<"no-optimized-zero-stride-load", "HasOptimizedZeroStrideLoad",
12671271
"false", "Hasn't optimized (perform fewer memory operations)"

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,11 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
15181518

15191519
// Disable strict node mutation.
15201520
IsStrictFPEnabled = true;
1521+
1522+
// Let the subtarget decide if a predictable select is more expensive than the
1523+
// corresponding branch. This information is used in CGP/SelectOpt to decide
1524+
// when to convert selects into branches.
1525+
PredictableSelectIsExpensive = Subtarget.predictableSelectIsExpensive();
15211526
}
15221527

15231528
EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -mtriple=riscv64 -mattr=+zicond < %s | FileCheck %s --check-prefixes=CHECK,CHEAP
3+
; RUN: llc -mtriple=riscv64 -mattr=+zicond,+predictable-select-expensive < %s | FileCheck %s --check-prefixes=CHECK,EXPENSIVE
4+
5+
; Test has not predictable select, which should not be transformed to a branch
6+
define i32 @test1(i32 %a) {
7+
; CHECK-LABEL: test1:
8+
; CHECK: # %bb.0: # %entry
9+
; CHECK-NEXT: sext.w a1, a0
10+
; CHECK-NEXT: slti a1, a1, 1
11+
; CHECK-NEXT: addiw a0, a0, -1
12+
; CHECK-NEXT: czero.nez a0, a0, a1
13+
; CHECK-NEXT: ret
14+
entry:
15+
%cmp = icmp slt i32 %a, 1
16+
%dec = sub i32 %a, 1
17+
%res = select i1 %cmp, i32 0, i32 %dec, !prof !0
18+
ret i32 %res
19+
}
20+
21+
; Test has highly predictable select according to profile data,
22+
; which should be transformed to a branch on cores with enabled FeaturePredictableSelectIsExpensive
23+
define i32 @test2(i32 %a) {
24+
; CHEAP-LABEL: test2:
25+
; CHEAP: # %bb.0: # %entry
26+
; CHEAP-NEXT: sext.w a1, a0
27+
; CHEAP-NEXT: slti a1, a1, 1
28+
; CHEAP-NEXT: addiw a0, a0, -1
29+
; CHEAP-NEXT: czero.nez a0, a0, a1
30+
; CHEAP-NEXT: ret
31+
;
32+
; EXPENSIVE-LABEL: test2:
33+
; EXPENSIVE: # %bb.0: # %entry
34+
; EXPENSIVE-NEXT: sext.w a1, a0
35+
; EXPENSIVE-NEXT: blez a1, .LBB1_2
36+
; EXPENSIVE-NEXT: # %bb.1: # %select.false
37+
; EXPENSIVE-NEXT: addiw a0, a0, -1
38+
; EXPENSIVE-NEXT: ret
39+
; EXPENSIVE-NEXT: .LBB1_2:
40+
; EXPENSIVE-NEXT: li a0, 0
41+
; EXPENSIVE-NEXT: ret
42+
entry:
43+
%cmp = icmp slt i32 %a, 1
44+
%dec = sub i32 %a, 1
45+
%res = select i1 %cmp, i32 0, i32 %dec, !prof !1
46+
ret i32 %res
47+
}
48+
49+
!0 = !{!"branch_weights", i32 1, i32 1}
50+
!1 = !{!"branch_weights", i32 1, i32 1000}

0 commit comments

Comments
 (0)