Skip to content

Commit cd61e8d

Browse files
committed
[RISCV][GlobalISel] Legalize add/sub for wide and non-pow2 types
Legalize G_ADD, G_SUB, G_(S/U)ADD(O/E). We test for (s7, s48, s64, s96) on rv32 and (s15, s72, s128, s192) on rv64. Differential Revision: https://reviews.llvm.org/D157019
1 parent 9abc1e0 commit cd61e8d

File tree

5 files changed

+483
-0
lines changed

5 files changed

+483
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST) {
3030
.widenScalarToNextPow2(0)
3131
.clampScalar(0, XLenLLT, XLenLLT);
3232

33+
getActionDefinitionsBuilder(
34+
{G_UADDE, G_UADDO, G_USUBE, G_USUBO, G_SADDE, G_SADDO, G_SSUBE, G_SSUBO})
35+
.legalFor({{XLenLLT, XLenLLT}})
36+
.clampScalar(0, XLenLLT, XLenLLT)
37+
.clampScalar(1, XLenLLT, XLenLLT)
38+
.widenScalarToNextPow2(0);
39+
3340
getActionDefinitionsBuilder({G_ASHR, G_LSHR, G_SHL})
3441
.legalFor({{XLenLLT, XLenLLT}})
3542
.widenScalarToNextPow2(0)

llvm/test/CodeGen/RISCV/GlobalISel/legalizer/rv32/legalize-add.mir

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22
# RUN: llc -mtriple=riscv32 -run-pass=legalizer %s -o - \
33
# RUN: | FileCheck %s
44
---
5+
name: add_i7
6+
body: |
7+
bb.0.entry:
8+
; CHECK-LABEL: name: add_i7
9+
; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $x10
10+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(s32) = COPY $x11
11+
; CHECK-NEXT: [[ADD:%[0-9]+]]:_(s32) = G_ADD [[COPY]], [[COPY1]]
12+
; CHECK-NEXT: $x10 = COPY [[ADD]](s32)
13+
; CHECK-NEXT: PseudoRET implicit $x10
14+
%0:_(s32) = COPY $x10
15+
%1:_(s32) = COPY $x11
16+
%2:_(s7) = G_TRUNC %0(s32)
17+
%3:_(s7) = G_TRUNC %1(s32)
18+
%4:_(s7) = G_ADD %2, %3
19+
%5:_(s32) = G_ANYEXT %4(s7)
20+
$x10 = COPY %5(s32)
21+
PseudoRET implicit $x10
22+
23+
...
24+
---
525
name: add_i8
626
body: |
727
bb.0.entry:
@@ -58,3 +78,102 @@ body: |
5878
PseudoRET implicit $x10
5979
6080
...
81+
---
82+
name: add_i48
83+
body: |
84+
bb.0.entry:
85+
; CHECK-LABEL: name: add_i48
86+
; CHECK: %xhi:_(s32) = COPY $x10
87+
; CHECK-NEXT: %xlo:_(s32) = COPY $x11
88+
; CHECK-NEXT: %yhi:_(s32) = COPY $x12
89+
; CHECK-NEXT: %ylo:_(s32) = COPY $x13
90+
; CHECK-NEXT: [[UADDO:%[0-9]+]]:_(s32), [[UADDO1:%[0-9]+]]:_(s32) = G_UADDO %xhi, %yhi
91+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
92+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[UADDO1]], [[C]]
93+
; CHECK-NEXT: [[UADDE:%[0-9]+]]:_(s32), [[UADDE1:%[0-9]+]]:_(s32) = G_UADDE %xlo, %ylo, [[AND]]
94+
; CHECK-NEXT: $x10 = COPY [[UADDO]](s32)
95+
; CHECK-NEXT: $x11 = COPY [[UADDE]](s32)
96+
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11
97+
%xhi:_(s32) = COPY $x10
98+
%xlo:_(s32) = COPY $x11
99+
%yhi:_(s32) = COPY $x12
100+
%ylo:_(s32) = COPY $x13
101+
%x0:_(s64) = G_MERGE_VALUES %xhi(s32), %xlo(s32)
102+
%y0:_(s64) = G_MERGE_VALUES %yhi(s32), %ylo(s32)
103+
%x:_(s48) = G_TRUNC %x0(s64)
104+
%y:_(s48) = G_TRUNC %y0(s64)
105+
%z:_(s48) = G_ADD %x, %y
106+
%z0:_(s64) = G_ANYEXT %z(s48)
107+
%zhi:_(s32), %zlo:_(s32) = G_UNMERGE_VALUES %z0(s64)
108+
$x10 = COPY %zhi(s32)
109+
$x11 = COPY %zlo(s32)
110+
PseudoRET implicit $x10, implicit $x11
111+
112+
...
113+
---
114+
name: add_i64
115+
body: |
116+
bb.0.entry:
117+
; CHECK-LABEL: name: add_i64
118+
; CHECK: %hi1:_(s32) = COPY $x10
119+
; CHECK-NEXT: %lo1:_(s32) = COPY $x11
120+
; CHECK-NEXT: %hi2:_(s32) = COPY $x12
121+
; CHECK-NEXT: %lo2:_(s32) = COPY $x13
122+
; CHECK-NEXT: [[UADDO:%[0-9]+]]:_(s32), [[UADDO1:%[0-9]+]]:_(s32) = G_UADDO %hi1, %hi2
123+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
124+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[UADDO1]], [[C]]
125+
; CHECK-NEXT: [[UADDE:%[0-9]+]]:_(s32), [[UADDE1:%[0-9]+]]:_(s32) = G_UADDE %lo1, %lo2, [[AND]]
126+
; CHECK-NEXT: $x10 = COPY [[UADDO]](s32)
127+
; CHECK-NEXT: $x11 = COPY [[UADDE]](s32)
128+
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11
129+
%hi1:_(s32) = COPY $x10
130+
%lo1:_(s32) = COPY $x11
131+
%hi2:_(s32) = COPY $x12
132+
%lo2:_(s32) = COPY $x13
133+
%x1:_(s64) = G_MERGE_VALUES %hi1(s32), %lo1(s32)
134+
%x2:_(s64) = G_MERGE_VALUES %hi2(s32), %lo2(s32)
135+
%y:_(s64) = G_ADD %x1, %x2
136+
%hiy:_(s32), %loy:_(s32) = G_UNMERGE_VALUES %y(s64)
137+
$x10 = COPY %hiy(s32)
138+
$x11 = COPY %loy(s32)
139+
PseudoRET implicit $x10, implicit $x11
140+
141+
...
142+
---
143+
name: add_i96
144+
body: |
145+
bb.0.entry:
146+
; CHECK-LABEL: name: add_i96
147+
; CHECK: %hi1:_(s32) = COPY $x10
148+
; CHECK-NEXT: %mid1:_(s32) = COPY $x11
149+
; CHECK-NEXT: %lo1:_(s32) = COPY $x12
150+
; CHECK-NEXT: %hi2:_(s32) = COPY $x13
151+
; CHECK-NEXT: %mid2:_(s32) = COPY $x14
152+
; CHECK-NEXT: %lo2:_(s32) = COPY $x15
153+
; CHECK-NEXT: [[UADDO:%[0-9]+]]:_(s32), [[UADDO1:%[0-9]+]]:_(s32) = G_UADDO %hi1, %hi2
154+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
155+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[UADDO1]], [[C]]
156+
; CHECK-NEXT: [[UADDE:%[0-9]+]]:_(s32), [[UADDE1:%[0-9]+]]:_(s32) = G_UADDE %mid1, %mid2, [[AND]]
157+
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
158+
; CHECK-NEXT: [[AND1:%[0-9]+]]:_(s32) = G_AND [[UADDE1]], [[C1]]
159+
; CHECK-NEXT: [[UADDE2:%[0-9]+]]:_(s32), [[UADDE3:%[0-9]+]]:_(s32) = G_UADDE %lo1, %lo2, [[AND1]]
160+
; CHECK-NEXT: $x10 = COPY [[UADDO]](s32)
161+
; CHECK-NEXT: $x11 = COPY [[UADDE]](s32)
162+
; CHECK-NEXT: $x12 = COPY [[UADDE2]](s32)
163+
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11, implicit $x12
164+
%hi1:_(s32) = COPY $x10
165+
%mid1:_(s32) = COPY $x11
166+
%lo1:_(s32) = COPY $x12
167+
%hi2:_(s32) = COPY $x13
168+
%mid2:_(s32) = COPY $x14
169+
%lo2:_(s32) = COPY $x15
170+
%x1:_(s96) = G_MERGE_VALUES %hi1(s32), %mid1(s32), %lo1(s32)
171+
%x2:_(s96) = G_MERGE_VALUES %hi2(s32), %mid2(s32), %lo2(s32)
172+
%y:_(s96) = G_ADD %x1, %x2
173+
%hi:_(s32), %mid:_(s32), %lo:_(s32) = G_UNMERGE_VALUES %y(s96)
174+
$x10 = COPY %hi(s32)
175+
$x11 = COPY %mid(s32)
176+
$x12 = COPY %lo(s32)
177+
PseudoRET implicit $x10, implicit $x11, implicit $x12
178+
179+
...

llvm/test/CodeGen/RISCV/GlobalISel/legalizer/rv32/legalize-sub.mir

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22
# RUN: llc -mtriple=riscv32 -run-pass=legalizer %s -o - \
33
# RUN: | FileCheck %s
44
---
5+
name: sub_i7
6+
body: |
7+
bb.0.entry:
8+
; CHECK-LABEL: name: sub_i7
9+
; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $x10
10+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(s32) = COPY $x11
11+
; CHECK-NEXT: [[SUB:%[0-9]+]]:_(s32) = G_SUB [[COPY]], [[COPY1]]
12+
; CHECK-NEXT: $x10 = COPY [[SUB]](s32)
13+
; CHECK-NEXT: PseudoRET implicit $x10
14+
%0:_(s32) = COPY $x10
15+
%1:_(s32) = COPY $x11
16+
%2:_(s7) = G_TRUNC %0(s32)
17+
%3:_(s7) = G_TRUNC %1(s32)
18+
%4:_(s7) = G_SUB %2, %3
19+
%5:_(s32) = G_ANYEXT %4(s7)
20+
$x10 = COPY %5(s32)
21+
PseudoRET implicit $x10
22+
23+
...
24+
---
525
name: sub_i8
626
body: |
727
bb.0.entry:
@@ -58,3 +78,102 @@ body: |
5878
PseudoRET implicit $x10
5979
6080
...
81+
---
82+
name: sub_i48
83+
body: |
84+
bb.0.entry:
85+
; CHECK-LABEL: name: sub_i48
86+
; CHECK: %xhi:_(s32) = COPY $x10
87+
; CHECK-NEXT: %xlo:_(s32) = COPY $x11
88+
; CHECK-NEXT: %yhi:_(s32) = COPY $x12
89+
; CHECK-NEXT: %ylo:_(s32) = COPY $x13
90+
; CHECK-NEXT: [[USUBO:%[0-9]+]]:_(s32), [[USUBO1:%[0-9]+]]:_(s32) = G_USUBO %xhi, %yhi
91+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
92+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[USUBO1]], [[C]]
93+
; CHECK-NEXT: [[USUBE:%[0-9]+]]:_(s32), [[USUBE1:%[0-9]+]]:_(s32) = G_USUBE %xlo, %ylo, [[AND]]
94+
; CHECK-NEXT: $x10 = COPY [[USUBO]](s32)
95+
; CHECK-NEXT: $x11 = COPY [[USUBE]](s32)
96+
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11
97+
%xhi:_(s32) = COPY $x10
98+
%xlo:_(s32) = COPY $x11
99+
%yhi:_(s32) = COPY $x12
100+
%ylo:_(s32) = COPY $x13
101+
%x0:_(s64) = G_MERGE_VALUES %xhi(s32), %xlo(s32)
102+
%y0:_(s64) = G_MERGE_VALUES %yhi(s32), %ylo(s32)
103+
%x:_(s48) = G_TRUNC %x0(s64)
104+
%y:_(s48) = G_TRUNC %y0(s64)
105+
%z:_(s48) = G_SUB %x, %y
106+
%z0:_(s64) = G_ANYEXT %z(s48)
107+
%zhi:_(s32), %zlo:_(s32) = G_UNMERGE_VALUES %z0(s64)
108+
$x10 = COPY %zhi(s32)
109+
$x11 = COPY %zlo(s32)
110+
PseudoRET implicit $x10, implicit $x11
111+
112+
...
113+
---
114+
name: sub_i64
115+
body: |
116+
bb.0.entry:
117+
; CHECK-LABEL: name: sub_i64
118+
; CHECK: %hi1:_(s32) = COPY $x10
119+
; CHECK-NEXT: %lo1:_(s32) = COPY $x11
120+
; CHECK-NEXT: %hi2:_(s32) = COPY $x12
121+
; CHECK-NEXT: %lo2:_(s32) = COPY $x13
122+
; CHECK-NEXT: [[USUBO:%[0-9]+]]:_(s32), [[USUBO1:%[0-9]+]]:_(s32) = G_USUBO %hi1, %hi2
123+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
124+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[USUBO1]], [[C]]
125+
; CHECK-NEXT: [[USUBE:%[0-9]+]]:_(s32), [[USUBE1:%[0-9]+]]:_(s32) = G_USUBE %lo1, %lo2, [[AND]]
126+
; CHECK-NEXT: $x10 = COPY [[USUBO]](s32)
127+
; CHECK-NEXT: $x11 = COPY [[USUBE]](s32)
128+
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11
129+
%hi1:_(s32) = COPY $x10
130+
%lo1:_(s32) = COPY $x11
131+
%hi2:_(s32) = COPY $x12
132+
%lo2:_(s32) = COPY $x13
133+
%x1:_(s64) = G_MERGE_VALUES %hi1(s32), %lo1(s32)
134+
%x2:_(s64) = G_MERGE_VALUES %hi2(s32), %lo2(s32)
135+
%y:_(s64) = G_SUB %x1, %x2
136+
%hiy:_(s32), %loy:_(s32) = G_UNMERGE_VALUES %y(s64)
137+
$x10 = COPY %hiy(s32)
138+
$x11 = COPY %loy(s32)
139+
PseudoRET implicit $x10, implicit $x11
140+
141+
...
142+
---
143+
name: sub_i96
144+
body: |
145+
bb.0.entry:
146+
; CHECK-LABEL: name: sub_i96
147+
; CHECK: %hi1:_(s32) = COPY $x10
148+
; CHECK-NEXT: %mid1:_(s32) = COPY $x11
149+
; CHECK-NEXT: %lo1:_(s32) = COPY $x12
150+
; CHECK-NEXT: %hi2:_(s32) = COPY $x13
151+
; CHECK-NEXT: %mid2:_(s32) = COPY $x14
152+
; CHECK-NEXT: %lo2:_(s32) = COPY $x15
153+
; CHECK-NEXT: [[USUBO:%[0-9]+]]:_(s32), [[USUBO1:%[0-9]+]]:_(s32) = G_USUBO %hi1, %hi2
154+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
155+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[USUBO1]], [[C]]
156+
; CHECK-NEXT: [[USUBE:%[0-9]+]]:_(s32), [[USUBE1:%[0-9]+]]:_(s32) = G_USUBE %mid1, %mid2, [[AND]]
157+
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
158+
; CHECK-NEXT: [[AND1:%[0-9]+]]:_(s32) = G_AND [[USUBE1]], [[C1]]
159+
; CHECK-NEXT: [[USUBE2:%[0-9]+]]:_(s32), [[USUBE3:%[0-9]+]]:_(s32) = G_USUBE %lo1, %lo2, [[AND1]]
160+
; CHECK-NEXT: $x10 = COPY [[USUBO]](s32)
161+
; CHECK-NEXT: $x11 = COPY [[USUBE]](s32)
162+
; CHECK-NEXT: $x12 = COPY [[USUBE2]](s32)
163+
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11, implicit $x12
164+
%hi1:_(s32) = COPY $x10
165+
%mid1:_(s32) = COPY $x11
166+
%lo1:_(s32) = COPY $x12
167+
%hi2:_(s32) = COPY $x13
168+
%mid2:_(s32) = COPY $x14
169+
%lo2:_(s32) = COPY $x15
170+
%x1:_(s96) = G_MERGE_VALUES %hi1(s32), %mid1(s32), %lo1(s32)
171+
%x2:_(s96) = G_MERGE_VALUES %hi2(s32), %mid2(s32), %lo2(s32)
172+
%y:_(s96) = G_SUB %x1, %x2
173+
%hi:_(s32), %mid:_(s32), %lo:_(s32) = G_UNMERGE_VALUES %y(s96)
174+
$x10 = COPY %hi(s32)
175+
$x11 = COPY %mid(s32)
176+
$x12 = COPY %lo(s32)
177+
PseudoRET implicit $x10, implicit $x11, implicit $x12
178+
179+
...

0 commit comments

Comments
 (0)