Skip to content

Commit cfdafc1

Browse files
authored
[RISCV][GISel] Support G_PTRTOINT and G_INTTOPTR (#69542)
Legalizer, register bank selection, and instruction selection.
1 parent f533e8c commit cfdafc1

File tree

11 files changed

+745
-8
lines changed

11 files changed

+745
-8
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
289289
switch (Opc) {
290290
case TargetOpcode::G_ANYEXT:
291291
case TargetOpcode::G_PTRTOINT:
292+
case TargetOpcode::G_INTTOPTR:
292293
case TargetOpcode::G_TRUNC:
293294
return selectCopy(MI, MRI);
294295
case TargetOpcode::G_CONSTANT:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST) {
122122
getActionDefinitionsBuilder(G_PTR_ADD)
123123
.legalFor({{p0, XLenLLT}});
124124

125+
getActionDefinitionsBuilder(G_PTRTOINT)
126+
.legalFor({{XLenLLT, p0}})
127+
.clampScalar(0, XLenLLT, XLenLLT);
128+
129+
getActionDefinitionsBuilder(G_INTTOPTR)
130+
.legalFor({{p0, XLenLLT}})
131+
.clampScalar(1, XLenLLT, XLenLLT);
132+
125133
getActionDefinitionsBuilder(G_BRCOND)
126134
.legalFor({XLenLLT})
127135
.minScalar(0, XLenLLT);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ RISCVRegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
126126
case TargetOpcode::G_UREM:
127127
case TargetOpcode::G_UMULH:
128128
case TargetOpcode::G_PTR_ADD:
129+
case TargetOpcode::G_PTRTOINT:
130+
case TargetOpcode::G_INTTOPTR:
129131
case TargetOpcode::G_TRUNC:
130132
case TargetOpcode::G_ANYEXT:
131133
case TargetOpcode::G_SEXT:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -march=riscv32 -run-pass=instruction-select -simplify-mir \
3+
# RUN: -verify-machineinstrs %s -o - | FileCheck %s
4+
5+
---
6+
name: ptrtoint_s32
7+
legalized: true
8+
regBankSelected: true
9+
tracksRegLiveness: true
10+
body: |
11+
bb.0.entry:
12+
liveins: $x10, $x11
13+
14+
; CHECK-LABEL: name: ptrtoint_s32
15+
; CHECK: liveins: $x10, $x11
16+
; CHECK-NEXT: {{ $}}
17+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
18+
; CHECK-NEXT: $x10 = COPY [[COPY]]
19+
; CHECK-NEXT: PseudoRET implicit $x10
20+
%0:gprb(p0) = COPY $x10
21+
%1:gprb(s32) = G_PTRTOINT %0(p0)
22+
$x10 = COPY %1(s32)
23+
PseudoRET implicit $x10
24+
25+
...
26+
---
27+
name: inttoptr_s32
28+
legalized: true
29+
regBankSelected: true
30+
tracksRegLiveness: true
31+
body: |
32+
bb.0.entry:
33+
liveins: $x10, $x11
34+
35+
; CHECK-LABEL: name: inttoptr_s32
36+
; CHECK: liveins: $x10, $x11
37+
; CHECK-NEXT: {{ $}}
38+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
39+
; CHECK-NEXT: $x10 = COPY [[COPY]]
40+
; CHECK-NEXT: PseudoRET implicit $x10
41+
%0:gprb(s32) = COPY $x10
42+
%1:gprb(p0) = G_INTTOPTR %0(s32)
43+
$x10 = COPY %1(p0)
44+
PseudoRET implicit $x10
45+
46+
...
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -march=riscv64 -run-pass=instruction-select -simplify-mir \
3+
# RUN: -verify-machineinstrs %s -o - | FileCheck %s
4+
5+
---
6+
name: ptrtoint_s64
7+
legalized: true
8+
regBankSelected: true
9+
tracksRegLiveness: true
10+
body: |
11+
bb.0.entry:
12+
liveins: $x10, $x11
13+
14+
; CHECK-LABEL: name: ptrtoint_s64
15+
; CHECK: liveins: $x10, $x11
16+
; CHECK-NEXT: {{ $}}
17+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
18+
; CHECK-NEXT: $x10 = COPY [[COPY]]
19+
; CHECK-NEXT: PseudoRET implicit $x10
20+
%0:gprb(p0) = COPY $x10
21+
%1:gprb(s64) = G_PTRTOINT %0(p0)
22+
$x10 = COPY %1(s64)
23+
PseudoRET implicit $x10
24+
25+
...
26+
---
27+
name: inttoptr_s64
28+
legalized: true
29+
regBankSelected: true
30+
tracksRegLiveness: true
31+
body: |
32+
bb.0.entry:
33+
liveins: $x10, $x11
34+
35+
; CHECK-LABEL: name: inttoptr_s64
36+
; CHECK: liveins: $x10, $x11
37+
; CHECK-NEXT: {{ $}}
38+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr = COPY $x10
39+
; CHECK-NEXT: $x10 = COPY [[COPY]]
40+
; CHECK-NEXT: PseudoRET implicit $x10
41+
%0:gprb(s64) = COPY $x10
42+
%1:gprb(p0) = G_INTTOPTR %0(s64)
43+
$x10 = COPY %1(p0)
44+
PseudoRET implicit $x10
45+
46+
...
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple=riscv32 -run-pass=legalizer -verify-machineinstrs %s -o - | FileCheck %s
3+
4+
---
5+
name: p0_s32
6+
body: |
7+
bb.0:
8+
liveins: $x10
9+
10+
; CHECK-LABEL: name: p0_s32
11+
; CHECK: liveins: $x10
12+
; CHECK-NEXT: {{ $}}
13+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $x10
14+
; CHECK-NEXT: [[INTTOPTR:%[0-9]+]]:_(p0) = G_INTTOPTR [[COPY]](s32)
15+
; CHECK-NEXT: $x10 = COPY [[INTTOPTR]](p0)
16+
; CHECK-NEXT: PseudoRET implicit $x10
17+
%0:_(s32) = COPY $x10
18+
%1:_(p0) = G_INTTOPTR %0(s32)
19+
$x10 = COPY %1(p0)
20+
PseudoRET implicit $x10
21+
22+
...
23+
---
24+
name: p0_s16
25+
body: |
26+
bb.0:
27+
liveins: $x10
28+
29+
; CHECK-LABEL: name: p0_s16
30+
; CHECK: liveins: $x10
31+
; CHECK-NEXT: {{ $}}
32+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $x10
33+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 65535
34+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY]], [[C]]
35+
; CHECK-NEXT: [[INTTOPTR:%[0-9]+]]:_(p0) = G_INTTOPTR [[AND]](s32)
36+
; CHECK-NEXT: $x10 = COPY [[INTTOPTR]](p0)
37+
; CHECK-NEXT: PseudoRET implicit $x10
38+
%1:_(s32) = COPY $x10
39+
%0:_(s16) = G_TRUNC %1(s32)
40+
%2:_(p0) = G_INTTOPTR %0(s16)
41+
$x10 = COPY %2(p0)
42+
PseudoRET implicit $x10
43+
44+
...
45+
---
46+
name: p0_s8
47+
body: |
48+
bb.0:
49+
liveins: $x10
50+
51+
; CHECK-LABEL: name: p0_s8
52+
; CHECK: liveins: $x10
53+
; CHECK-NEXT: {{ $}}
54+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $x10
55+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 255
56+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY]], [[C]]
57+
; CHECK-NEXT: [[INTTOPTR:%[0-9]+]]:_(p0) = G_INTTOPTR [[AND]](s32)
58+
; CHECK-NEXT: $x10 = COPY [[INTTOPTR]](p0)
59+
; CHECK-NEXT: PseudoRET implicit $x10
60+
%1:_(s32) = COPY $x10
61+
%0:_(s8) = G_TRUNC %1(s32)
62+
%2:_(p0) = G_INTTOPTR %0(s8)
63+
$x10 = COPY %2(p0)
64+
PseudoRET implicit $x10
65+
66+
...
67+
---
68+
name: p0_s1
69+
body: |
70+
bb.0:
71+
liveins: $x10
72+
73+
; CHECK-LABEL: name: p0_s1
74+
; CHECK: liveins: $x10
75+
; CHECK-NEXT: {{ $}}
76+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $x10
77+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
78+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY]], [[C]]
79+
; CHECK-NEXT: [[INTTOPTR:%[0-9]+]]:_(p0) = G_INTTOPTR [[AND]](s32)
80+
; CHECK-NEXT: $x10 = COPY [[INTTOPTR]](p0)
81+
; CHECK-NEXT: PseudoRET implicit $x10
82+
%1:_(s32) = COPY $x10
83+
%0:_(s1) = G_TRUNC %1(s32)
84+
%2:_(p0) = G_INTTOPTR %0(s1)
85+
$x10 = COPY %2(p0)
86+
PseudoRET implicit $x10
87+
88+
...
89+
---
90+
name: p0_s64
91+
body: |
92+
bb.0:
93+
liveins: $x10, $x11
94+
95+
; CHECK-LABEL: name: p0_s64
96+
; CHECK: liveins: $x10, $x11
97+
; CHECK-NEXT: {{ $}}
98+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $x10
99+
; CHECK-NEXT: [[INTTOPTR:%[0-9]+]]:_(p0) = G_INTTOPTR [[COPY]](s32)
100+
; CHECK-NEXT: $x10 = COPY [[INTTOPTR]](p0)
101+
; CHECK-NEXT: PseudoRET implicit $x10
102+
%1:_(s32) = COPY $x10
103+
%2:_(s32) = COPY $x11
104+
%0:_(s64) = G_MERGE_VALUES %1(s32), %2(s32)
105+
%3:_(p0) = G_INTTOPTR %0(s64)
106+
$x10 = COPY %3(p0)
107+
PseudoRET implicit $x10
108+
109+
...
110+
---
111+
name: p0_s44
112+
body: |
113+
bb.0:
114+
liveins: $x10, $x11
115+
116+
; CHECK-LABEL: name: p0_s44
117+
; CHECK: liveins: $x10, $x11
118+
; CHECK-NEXT: {{ $}}
119+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $x10
120+
; CHECK-NEXT: [[INTTOPTR:%[0-9]+]]:_(p0) = G_INTTOPTR [[COPY]](s32)
121+
; CHECK-NEXT: $x10 = COPY [[INTTOPTR]](p0)
122+
; CHECK-NEXT: PseudoRET implicit $x10
123+
%1:_(s32) = COPY $x10
124+
%2:_(s32) = COPY $x11
125+
%3:_(s64) = G_MERGE_VALUES %1(s32), %2(s32)
126+
%0:_(s44) = G_TRUNC %3(s64)
127+
%4:_(p0) = G_INTTOPTR %0(s44)
128+
$x10 = COPY %4(p0)
129+
PseudoRET implicit $x10
130+
131+
...
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple=riscv32 -run-pass=legalizer -verify-machineinstrs %s -o - | FileCheck %s
3+
4+
---
5+
name: p0_s32
6+
body: |
7+
bb.0:
8+
liveins: $x10
9+
10+
; CHECK-LABEL: name: p0_s32
11+
; CHECK: liveins: $x10
12+
; CHECK-NEXT: {{ $}}
13+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(p0) = COPY $x10
14+
; CHECK-NEXT: [[PTRTOINT:%[0-9]+]]:_(s32) = G_PTRTOINT [[COPY]](p0)
15+
; CHECK-NEXT: $x10 = COPY [[PTRTOINT]](s32)
16+
; CHECK-NEXT: PseudoRET implicit $x10
17+
%0:_(p0) = COPY $x10
18+
%1:_(s32) = G_PTRTOINT %0(p0)
19+
$x10 = COPY %1(s32)
20+
PseudoRET implicit $x10
21+
22+
...
23+
---
24+
name: p0_s16
25+
body: |
26+
bb.0:
27+
liveins: $x10
28+
29+
; CHECK-LABEL: name: p0_s16
30+
; CHECK: liveins: $x10
31+
; CHECK-NEXT: {{ $}}
32+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(p0) = COPY $x10
33+
; CHECK-NEXT: [[PTRTOINT:%[0-9]+]]:_(s32) = G_PTRTOINT [[COPY]](p0)
34+
; CHECK-NEXT: $x10 = COPY [[PTRTOINT]](s32)
35+
; CHECK-NEXT: PseudoRET implicit $x10
36+
%0:_(p0) = COPY $x10
37+
%1:_(s16) = G_PTRTOINT %0(p0)
38+
%2:_(s32) = G_ANYEXT %1(s16)
39+
$x10 = COPY %2(s32)
40+
PseudoRET implicit $x10
41+
42+
...
43+
---
44+
name: p0_s8
45+
body: |
46+
bb.0:
47+
liveins: $x10
48+
49+
; CHECK-LABEL: name: p0_s8
50+
; CHECK: liveins: $x10
51+
; CHECK-NEXT: {{ $}}
52+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(p0) = COPY $x10
53+
; CHECK-NEXT: [[PTRTOINT:%[0-9]+]]:_(s32) = G_PTRTOINT [[COPY]](p0)
54+
; CHECK-NEXT: $x10 = COPY [[PTRTOINT]](s32)
55+
; CHECK-NEXT: PseudoRET implicit $x10
56+
%0:_(p0) = COPY $x10
57+
%1:_(s8) = G_PTRTOINT %0(p0)
58+
%2:_(s32) = G_ANYEXT %1(s8)
59+
$x10 = COPY %2(s32)
60+
PseudoRET implicit $x10
61+
62+
...
63+
---
64+
name: p0_s1
65+
body: |
66+
bb.0:
67+
liveins: $x10
68+
69+
; CHECK-LABEL: name: p0_s1
70+
; CHECK: liveins: $x10
71+
; CHECK-NEXT: {{ $}}
72+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(p0) = COPY $x10
73+
; CHECK-NEXT: [[PTRTOINT:%[0-9]+]]:_(s32) = G_PTRTOINT [[COPY]](p0)
74+
; CHECK-NEXT: $x10 = COPY [[PTRTOINT]](s32)
75+
; CHECK-NEXT: PseudoRET implicit $x10
76+
%0:_(p0) = COPY $x10
77+
%1:_(s1) = G_PTRTOINT %0(p0)
78+
%2:_(s32) = G_ANYEXT %1(s1)
79+
$x10 = COPY %2(s32)
80+
PseudoRET implicit $x10
81+
82+
...
83+
---
84+
name: p0_s64
85+
body: |
86+
bb.0:
87+
liveins: $x10
88+
89+
; CHECK-LABEL: name: p0_s64
90+
; CHECK: liveins: $x10
91+
; CHECK-NEXT: {{ $}}
92+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(p0) = COPY $x10
93+
; CHECK-NEXT: [[PTRTOINT:%[0-9]+]]:_(s32) = G_PTRTOINT [[COPY]](p0)
94+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 0
95+
; CHECK-NEXT: $x10 = COPY [[PTRTOINT]](s32)
96+
; CHECK-NEXT: $x11 = COPY [[C]](s32)
97+
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11
98+
%0:_(p0) = COPY $x10
99+
%1:_(s64) = G_PTRTOINT %0(p0)
100+
%2:_(s32), %3:_(s32) = G_UNMERGE_VALUES %1(s64)
101+
$x10 = COPY %2(s32)
102+
$x11 = COPY %3(s32)
103+
PseudoRET implicit $x10, implicit $x11
104+
105+
...
106+
---
107+
name: p0_s44
108+
body: |
109+
bb.0:
110+
liveins: $x10
111+
112+
; CHECK-LABEL: name: p0_s44
113+
; CHECK: liveins: $x10
114+
; CHECK-NEXT: {{ $}}
115+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(p0) = COPY $x10
116+
; CHECK-NEXT: [[PTRTOINT:%[0-9]+]]:_(s32) = G_PTRTOINT [[COPY]](p0)
117+
; CHECK-NEXT: $x10 = COPY [[PTRTOINT]](s32)
118+
; CHECK-NEXT: PseudoRET implicit $x10
119+
%0:_(p0) = COPY $x10
120+
%1:_(s44) = G_PTRTOINT %0(p0)
121+
%4:_(s32) = G_TRUNC %1
122+
$x10 = COPY %4(s32)
123+
PseudoRET implicit $x10
124+
125+
...

0 commit comments

Comments
 (0)