Skip to content

Commit 5bb03d2

Browse files
committed
[RISCV][GISel] Support G_CTPOP with Zbb.
1 parent 3bf6cbd commit 5bb03d2

File tree

5 files changed

+427
-263
lines changed

5 files changed

+427
-263
lines changed

llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,21 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
105105
BSwap.maxScalar(0, sXLen).lower();
106106

107107
getActionDefinitionsBuilder(
108-
{G_CTPOP, G_CTLZ, G_CTLZ_ZERO_UNDEF, G_CTTZ, G_CTTZ_ZERO_UNDEF})
108+
{G_CTLZ, G_CTLZ_ZERO_UNDEF, G_CTTZ, G_CTTZ_ZERO_UNDEF})
109109
.maxScalar(0, sXLen)
110110
.scalarSameSizeAs(1, 0)
111111
.lower();
112112

113+
auto &CTPOPActions = getActionDefinitionsBuilder(G_CTPOP);
114+
if (ST.hasStdExtZbb()) {
115+
CTPOPActions.legalFor({{s32, s32}, {sXLen, sXLen}})
116+
.clampScalar(0, s32, sXLen)
117+
.widenScalarToNextPow2(0)
118+
.scalarSameSizeAs(1, 0);
119+
} else {
120+
CTPOPActions.maxScalar(0, sXLen).scalarSameSizeAs(1, 0).lower();
121+
}
122+
113123
getActionDefinitionsBuilder({G_CONSTANT, G_IMPLICIT_DEF})
114124
.legalFor({s32, sXLen, p0})
115125
.widenScalarToNextPow2(0)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple=riscv32 -mattr=+zbb -run-pass=instruction-select \
3+
# RUN: -simplify-mir -verify-machineinstrs %s -o - \
4+
# RUN: | FileCheck -check-prefix=RV32I %s
5+
6+
---
7+
name: ctpop_s32
8+
legalized: true
9+
regBankSelected: true
10+
body: |
11+
bb.0.entry:
12+
; RV32I-LABEL: name: ctpop_s32
13+
; RV32I: [[COPY:%[0-9]+]]:gpr = COPY $x10
14+
; RV32I-NEXT: [[CPOP:%[0-9]+]]:gpr = CPOP [[COPY]]
15+
; RV32I-NEXT: $x10 = COPY [[CPOP]]
16+
; RV32I-NEXT: PseudoRET implicit $x10
17+
%0:gprb(s32) = COPY $x10
18+
%1:gprb(s32) = G_CTPOP %0
19+
$x10 = COPY %1(s32)
20+
PseudoRET implicit $x10
21+
22+
...
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple=riscv64 -mattr=+zbb -run-pass=instruction-select \
3+
# RUN: -simplify-mir -verify-machineinstrs %s -o - \
4+
# RUN: | FileCheck -check-prefix=RV64I %s
5+
6+
---
7+
name: ctpop_s32
8+
legalized: true
9+
regBankSelected: true
10+
body: |
11+
bb.0.entry:
12+
; RV64I-LABEL: name: ctpop_s32
13+
; RV64I: [[COPY:%[0-9]+]]:gpr = COPY $x10
14+
; RV64I-NEXT: [[CPOPW:%[0-9]+]]:gpr = CPOPW [[COPY]]
15+
; RV64I-NEXT: $x10 = COPY [[CPOPW]]
16+
; RV64I-NEXT: PseudoRET implicit $x10
17+
%0:gprb(s64) = COPY $x10
18+
%1:gprb(s32) = G_TRUNC %0
19+
%2:gprb(s32) = G_CTPOP %1
20+
%3:gprb(s64) = G_ANYEXT %2
21+
$x10 = COPY %3(s64)
22+
PseudoRET implicit $x10
23+
24+
...
25+
---
26+
name: ctpop_s64
27+
legalized: true
28+
regBankSelected: true
29+
body: |
30+
bb.0.entry:
31+
; RV64I-LABEL: name: ctpop_s64
32+
; RV64I: [[COPY:%[0-9]+]]:gpr = COPY $x10
33+
; RV64I-NEXT: [[CPOP:%[0-9]+]]:gpr = CPOP [[COPY]]
34+
; RV64I-NEXT: $x10 = COPY [[CPOP]]
35+
; RV64I-NEXT: PseudoRET implicit $x10
36+
%0:gprb(s64) = COPY $x10
37+
%1:gprb(s64) = G_CTPOP %0
38+
$x10 = COPY %1(s64)
39+
PseudoRET implicit $x10
40+
41+
...

0 commit comments

Comments
 (0)