Skip to content

Commit 1393a8f

Browse files
committed
Add tests
1 parent 5ea8d38 commit 1393a8f

File tree

5 files changed

+1073
-0
lines changed

5 files changed

+1073
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple=riscv64 -run-pass=riscv-live-variables -verify-machineinstrs -o - %s | FileCheck %s
3+
#
4+
# Test basic live variable analysis with simple control flow and basic blocks
5+
6+
--- |
7+
define i64 @test_simple_add(i64 %a, i64 %b) {
8+
entry:
9+
%sum = add i64 %a, %b
10+
ret i64 %sum
11+
}
12+
13+
define i64 @test_if_then_else(i64 %a, i64 %b) {
14+
entry:
15+
%cmp = icmp sgt i64 %a, %b
16+
br i1 %cmp, label %then, label %else
17+
18+
then:
19+
%mul = mul i64 %a, %b
20+
br label %end
21+
22+
else:
23+
%sub = sub i64 %a, %b
24+
br label %end
25+
26+
end:
27+
%result = phi i64 [ %mul, %then ], [ %sub, %else ]
28+
ret i64 %result
29+
}
30+
31+
define i64 @test_multiple_uses(i64 %a, i64 %b, i64 %c) {
32+
entry:
33+
%t1 = add i64 %a, %b
34+
%t2 = mul i64 %t1, %c
35+
%t3 = sub i64 %t2, %a
36+
%t4 = add i64 %t3, %b
37+
ret i64 %t4
38+
}
39+
...
40+
---
41+
name: test_simple_add
42+
alignment: 4
43+
tracksRegLiveness: true
44+
registers:
45+
- { id: 0, class: gpr }
46+
- { id: 1, class: gpr }
47+
- { id: 2, class: gpr }
48+
liveins:
49+
- { reg: '$x10', virtual-reg: '%0' }
50+
- { reg: '$x11', virtual-reg: '%1' }
51+
body: |
52+
bb.0.entry:
53+
liveins: $x10, $x11
54+
; CHECK-LABEL: name: test_simple_add
55+
; CHECK: bb.0.entry:
56+
; CHECK-NEXT: liveins: $x10, $x11
57+
; Test that %0 and %1 are live-in, used once, and %2 is defined
58+
59+
%0:gpr = COPY $x10
60+
%1:gpr = COPY $x11
61+
%2:gpr = ADD %0, %1
62+
$x10 = COPY %2
63+
PseudoRET implicit $x10
64+
...
65+
---
66+
name: test_if_then_else
67+
alignment: 4
68+
tracksRegLiveness: true
69+
registers:
70+
- { id: 0, class: gpr }
71+
- { id: 1, class: gpr }
72+
- { id: 2, class: gpr }
73+
- { id: 3, class: gpr }
74+
- { id: 4, class: gpr }
75+
liveins:
76+
- { reg: '$x10', virtual-reg: '%0' }
77+
- { reg: '$x11', virtual-reg: '%1' }
78+
body: |
79+
bb.0.entry:
80+
liveins: $x10, $x11
81+
; CHECK-LABEL: name: test_if_then_else
82+
; CHECK: bb.0.entry:
83+
; CHECK-NEXT: successors
84+
; CHECK-NEXT: liveins: $x10, $x11
85+
; Test that %0 and %1 are live across multiple blocks
86+
87+
%0:gpr = COPY $x10
88+
%1:gpr = COPY $x11
89+
%2:gpr = SLT %1, %0
90+
BEQ %2, $x0, %bb.2
91+
92+
bb.1.then:
93+
; CHECK: bb.1.then:
94+
; %0 and %1 should be live-in here
95+
%3:gpr = MUL %0, %1
96+
PseudoBR %bb.3
97+
98+
bb.2.else:
99+
; CHECK: bb.2.else:
100+
; %0 and %1 should be live-in here
101+
%4:gpr = SUB %0, %1
102+
PseudoBR %bb.3
103+
104+
bb.3.end:
105+
; CHECK: bb.3.end:
106+
; Either %3 or %4 should be live-in (phi sources)
107+
%5:gpr = PHI %3, %bb.1, %4, %bb.2
108+
$x10 = COPY %5
109+
PseudoRET implicit $x10
110+
...
111+
---
112+
name: test_multiple_uses
113+
alignment: 4
114+
tracksRegLiveness: true
115+
registers:
116+
- { id: 0, class: gpr }
117+
- { id: 1, class: gpr }
118+
- { id: 2, class: gpr }
119+
- { id: 3, class: gpr }
120+
- { id: 4, class: gpr }
121+
- { id: 5, class: gpr }
122+
- { id: 6, class: gpr }
123+
liveins:
124+
- { reg: '$x10', virtual-reg: '%0' }
125+
- { reg: '$x11', virtual-reg: '%1' }
126+
- { reg: '$x12', virtual-reg: '%2' }
127+
body: |
128+
bb.0.entry:
129+
liveins: $x10, $x11, $x12
130+
; CHECK-LABEL: name: test_multiple_uses
131+
; CHECK: bb.0.entry:
132+
; CHECK-NEXT: liveins: $x10, $x11, $x12
133+
; Test that variables with multiple uses have correct liveness ranges
134+
135+
%0:gpr = COPY $x10
136+
%1:gpr = COPY $x11
137+
%2:gpr = COPY $x12
138+
; %0 used here and later
139+
%3:gpr = ADD %0, %1
140+
; %3 used in next instruction
141+
%4:gpr = MUL %3, %2
142+
; %0 used again here (should still be live)
143+
%5:gpr = SUB %4, %0
144+
; %1 used again here
145+
%6:gpr = ADD %5, %1
146+
$x10 = COPY %6
147+
PseudoRET implicit $x10
148+
...
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple=riscv64 -run-pass=riscv-live-variables -verify-machineinstrs -o - %s | FileCheck %s
3+
#
4+
# Test live variable analysis with function calls and register clobbering
5+
# Function calls clobber caller-saved registers, which affects liveness
6+
7+
--- |
8+
declare i64 @external_func(i64, i64)
9+
declare void @void_func(i64)
10+
11+
define i64 @test_call_simple(i64 %a, i64 %b) {
12+
entry:
13+
%result = call i64 @external_func(i64 %a, i64 %b)
14+
ret i64 %result
15+
}
16+
17+
define i64 @test_call_with_live_values(i64 %a, i64 %b, i64 %c) {
18+
entry:
19+
%sum1 = add i64 %a, %b
20+
%result = call i64 @external_func(i64 %sum1, i64 %c)
21+
; %b is live across the call
22+
%final = add i64 %result, %b
23+
ret i64 %final
24+
}
25+
26+
define i64 @test_multiple_calls(i64 %a, i64 %b) {
27+
entry:
28+
%r1 = call i64 @external_func(i64 %a, i64 %b)
29+
call void @void_func(i64 %r1)
30+
%r2 = call i64 @external_func(i64 %r1, i64 %b)
31+
ret i64 %r2
32+
}
33+
34+
define i64 @test_call_with_spill(i64 %a, i64 %b, i64 %c, i64 %d, i64 %e, i64 %f, i64 %g, i64 %h) {
35+
entry:
36+
%sum = add i64 %a, %b
37+
%result = call i64 @external_func(i64 %sum, i64 %c)
38+
; Many values live across call - may require spilling
39+
%t1 = add i64 %result, %d
40+
%t2 = add i64 %t1, %e
41+
%t3 = add i64 %t2, %f
42+
%t4 = add i64 %t3, %g
43+
%t5 = add i64 %t4, %h
44+
ret i64 %t5
45+
}
46+
...
47+
---
48+
name: test_call_simple
49+
alignment: 4
50+
tracksRegLiveness: true
51+
registers:
52+
- { id: 0, class: gpr }
53+
- { id: 1, class: gpr }
54+
- { id: 2, class: gpr }
55+
liveins:
56+
- { reg: '$x10', virtual-reg: '%0' }
57+
- { reg: '$x11', virtual-reg: '%1' }
58+
body: |
59+
bb.0.entry:
60+
liveins: $x10, $x11
61+
; CHECK-LABEL: name: test_call_simple
62+
; CHECK: bb.0.entry:
63+
; CHECK-NEXT: liveins: $x10, $x11
64+
65+
%0:gpr = COPY $x10
66+
%1:gpr = COPY $x11
67+
$x10 = COPY %0
68+
$x11 = COPY %1
69+
; Call clobbers many registers per calling convention
70+
PseudoCALL target-flags(riscv-call) @external_func, csr_ilp32_lp64, implicit-def $x1, implicit $x10, implicit $x11, implicit-def $x10
71+
%2:gpr = COPY $x10
72+
$x10 = COPY %2
73+
PseudoRET implicit $x10
74+
...
75+
---
76+
name: test_call_with_live_values
77+
alignment: 4
78+
tracksRegLiveness: true
79+
registers:
80+
- { id: 0, class: gpr }
81+
- { id: 1, class: gpr }
82+
- { id: 2, class: gpr }
83+
- { id: 3, class: gpr }
84+
- { id: 4, class: gpr }
85+
- { id: 5, class: gpr }
86+
liveins:
87+
- { reg: '$x10', virtual-reg: '%0' }
88+
- { reg: '$x11', virtual-reg: '%1' }
89+
- { reg: '$x12', virtual-reg: '%2' }
90+
body: |
91+
bb.0.entry:
92+
liveins: $x10, $x11, $x12
93+
; CHECK-LABEL: name: test_call_with_live_values
94+
; CHECK: bb.0.entry:
95+
; CHECK-NEXT: liveins: $x10, $x11, $x12
96+
97+
%0:gpr = COPY $x10
98+
%1:gpr = COPY $x11
99+
%2:gpr = COPY $x12
100+
%3:gpr = ADD %0, %1
101+
; %1 must remain live across this call
102+
$x10 = COPY %3
103+
$x11 = COPY %2
104+
PseudoCALL target-flags(riscv-call) @external_func, csr_ilp32_lp64, implicit-def $x1, implicit $x10, implicit $x11, implicit-def $x10
105+
%4:gpr = COPY $x10
106+
; %1 is used again here after the call
107+
%5:gpr = ADD %4, %1
108+
$x10 = COPY %5
109+
PseudoRET implicit $x10
110+
...
111+
---
112+
name: test_multiple_calls
113+
alignment: 4
114+
tracksRegLiveness: true
115+
registers:
116+
- { id: 0, class: gpr }
117+
- { id: 1, class: gpr }
118+
- { id: 2, class: gpr }
119+
- { id: 3, class: gpr }
120+
liveins:
121+
- { reg: '$x10', virtual-reg: '%0' }
122+
- { reg: '$x11', virtual-reg: '%1' }
123+
body: |
124+
bb.0.entry:
125+
liveins: $x10, $x11
126+
; CHECK-LABEL: name: test_multiple_calls
127+
; CHECK: bb.0.entry:
128+
; CHECK-NEXT: liveins: $x10, $x11
129+
130+
%0:gpr = COPY $x10
131+
%1:gpr = COPY $x11
132+
133+
; First call
134+
$x10 = COPY %0
135+
$x11 = COPY %1
136+
PseudoCALL target-flags(riscv-call) @external_func, csr_ilp32_lp64, implicit-def $x1, implicit $x10, implicit $x11, implicit-def $x10
137+
%2:gpr = COPY $x10
138+
139+
; Second call (void)
140+
$x10 = COPY %2
141+
PseudoCALL target-flags(riscv-call) @void_func, csr_ilp32_lp64, implicit-def $x1, implicit $x10
142+
143+
; Third call - %2 and %1 are both live here
144+
$x10 = COPY %2
145+
$x11 = COPY %1
146+
PseudoCALL target-flags(riscv-call) @external_func, csr_ilp32_lp64, implicit-def $x1, implicit $x10, implicit $x11, implicit-def $x10
147+
%3:gpr = COPY $x10
148+
149+
$x10 = COPY %3
150+
PseudoRET implicit $x10
151+
...
152+
---
153+
name: test_call_with_spill
154+
alignment: 4
155+
tracksRegLiveness: true
156+
registers:
157+
- { id: 0, class: gpr }
158+
- { id: 1, class: gpr }
159+
- { id: 2, class: gpr }
160+
- { id: 3, class: gpr }
161+
- { id: 4, class: gpr }
162+
- { id: 5, class: gpr }
163+
- { id: 6, class: gpr }
164+
- { id: 7, class: gpr }
165+
- { id: 8, class: gpr }
166+
- { id: 9, class: gpr }
167+
- { id: 10, class: gpr }
168+
- { id: 11, class: gpr }
169+
- { id: 12, class: gpr }
170+
- { id: 13, class: gpr }
171+
liveins:
172+
- { reg: '$x10', virtual-reg: '%0' }
173+
- { reg: '$x11', virtual-reg: '%1' }
174+
- { reg: '$x12', virtual-reg: '%2' }
175+
- { reg: '$x13', virtual-reg: '%3' }
176+
- { reg: '$x14', virtual-reg: '%4' }
177+
- { reg: '$x15', virtual-reg: '%5' }
178+
- { reg: '$x16', virtual-reg: '%6' }
179+
- { reg: '$x17', virtual-reg: '%7' }
180+
body: |
181+
bb.0.entry:
182+
liveins: $x10, $x11, $x12, $x13, $x14, $x15, $x16, $x17
183+
; CHECK-LABEL: name: test_call_with_spill
184+
; CHECK: bb.0.entry:
185+
; CHECK-NEXT: liveins: $x10, $x11, $x12, $x13, $x14, $x15, $x16, $x17
186+
; Many registers live across call - tests register pressure
187+
188+
%0:gpr = COPY $x10
189+
%1:gpr = COPY $x11
190+
%2:gpr = COPY $x12
191+
%3:gpr = COPY $x13
192+
%4:gpr = COPY $x14
193+
%5:gpr = COPY $x15
194+
%6:gpr = COPY $x16
195+
%7:gpr = COPY $x17
196+
197+
%8:gpr = ADD %0, %1
198+
199+
; Call with many live values - %3, %4, %5, %6, %7 all live across
200+
$x10 = COPY %8
201+
$x11 = COPY %2
202+
PseudoCALL target-flags(riscv-call) @external_func, csr_ilp32_lp64, implicit-def $x1, implicit $x10, implicit $x11, implicit-def $x10
203+
%9:gpr = COPY $x10
204+
205+
; All these values should have been kept live
206+
%10:gpr = ADD %9, %3
207+
%11:gpr = ADD %10, %4
208+
%12:gpr = ADD %11, %5
209+
%13:gpr = ADD %12, %6
210+
%14:gpr = ADD %13, %7
211+
212+
$x10 = COPY %14
213+
PseudoRET implicit $x10
214+
...

0 commit comments

Comments
 (0)