Skip to content

Commit c0de13b

Browse files
authored
[GISel][RISCV] Legalize shifts with non-trivial shamt types (#93019)
This patch widens the illegal shamt type `i48` -> `i64` to fix legalization failure: https://godbolt.org/z/4zMTnoW7h
1 parent 821bcba commit c0de13b

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
141141
.widenScalarToNextPow2(0)
142142
.clampScalar(1, s32, sXLen)
143143
.clampScalar(0, s32, sXLen)
144-
.minScalarSameAs(1, 0);
144+
.minScalarSameAs(1, 0)
145+
.widenScalarToNextPow2(1);
145146

146147
auto &ExtActions =
147148
getActionDefinitionsBuilder({G_ZEXT, G_SEXT, G_ANYEXT})

llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-lshr-rv64.mir

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,29 @@ body: |
336336
PseudoRET implicit $x10
337337
338338
...
339+
---
340+
name: lshr_i32_i48
341+
body: |
342+
bb.1:
343+
liveins: $x10
344+
345+
; CHECK-LABEL: name: lshr_i32_i48
346+
; CHECK: liveins: $x10
347+
; CHECK-NEXT: {{ $}}
348+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x10
349+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 16
350+
; CHECK-NEXT: [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[COPY]](s64)
351+
; CHECK-NEXT: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[TRUNC]], [[C]](s64)
352+
; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[LSHR]](s32)
353+
; CHECK-NEXT: $x10 = COPY [[ANYEXT]](s64)
354+
; CHECK-NEXT: PseudoRET implicit $x10
355+
%1:_(s64) = COPY $x10
356+
%0:_(s48) = G_TRUNC %1(s64)
357+
%2:_(s48) = G_CONSTANT i48 16
358+
%6:_(s32) = G_TRUNC %0(s48)
359+
%7:_(s32) = G_LSHR %6, %2(s48)
360+
%5:_(s64) = G_ANYEXT %7(s32)
361+
$x10 = COPY %5(s64)
362+
PseudoRET implicit $x10
363+
364+
...
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -mtriple=riscv32 -global-isel -global-isel-abort=1 -verify-machineinstrs < %s 2>&1 | FileCheck %s --check-prefixes=RV32
3+
; RUN: llc -mtriple=riscv64 -global-isel -global-isel-abort=1 -verify-machineinstrs < %s 2>&1 | FileCheck %s --check-prefixes=RV64
4+
5+
define i16 @test_lshr_i48(i48 %x) {
6+
; RV32-LABEL: test_lshr_i48:
7+
; RV32: # %bb.0:
8+
; RV32-NEXT: srli a0, a0, 16
9+
; RV32-NEXT: ret
10+
;
11+
; RV64-LABEL: test_lshr_i48:
12+
; RV64: # %bb.0:
13+
; RV64-NEXT: srliw a0, a0, 16
14+
; RV64-NEXT: ret
15+
%lshr = lshr i48 %x, 16
16+
%trunc = trunc i48 %lshr to i16
17+
ret i16 %trunc
18+
}
19+
20+
define i16 @test_ashr_i48(i48 %x) {
21+
; RV32-LABEL: test_ashr_i48:
22+
; RV32: # %bb.0:
23+
; RV32-NEXT: srai a0, a0, 16
24+
; RV32-NEXT: ret
25+
;
26+
; RV64-LABEL: test_ashr_i48:
27+
; RV64: # %bb.0:
28+
; RV64-NEXT: sraiw a0, a0, 16
29+
; RV64-NEXT: ret
30+
%ashr = ashr i48 %x, 16
31+
%trunc = trunc i48 %ashr to i16
32+
ret i16 %trunc
33+
}
34+
35+
define i16 @test_shl_i48(i48 %x) {
36+
; RV32-LABEL: test_shl_i48:
37+
; RV32: # %bb.0:
38+
; RV32-NEXT: slli a0, a0, 8
39+
; RV32-NEXT: ret
40+
;
41+
; RV64-LABEL: test_shl_i48:
42+
; RV64: # %bb.0:
43+
; RV64-NEXT: slliw a0, a0, 8
44+
; RV64-NEXT: ret
45+
%shl = shl i48 %x, 8
46+
%trunc = trunc i48 %shl to i16
47+
ret i16 %trunc
48+
}

0 commit comments

Comments
 (0)