blob: 0737de58e037113d11ea120137da023005db7e57 [file] [log] [blame]
Dan Gohman1cf96c02015-12-09 16:23:59 +00001; RUN: llc < %s -asm-verbose=false -verify-machineinstrs | FileCheck %s
Dan Gohman81719f82015-11-25 16:55:01 +00002
3; Test the register stackifier pass.
4
Dan Gohman0c6f5ac2016-01-07 03:19:23 +00005target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
Dan Gohman81719f82015-11-25 16:55:01 +00006target triple = "wasm32-unknown-unknown"
7
8; No because of pointer aliasing.
9
10; CHECK-LABEL: no0:
11; CHECK: return $1{{$}}
12define i32 @no0(i32* %p, i32* %q) {
13 %t = load i32, i32* %q
14 store i32 0, i32* %p
15 ret i32 %t
16}
17
18; No because of side effects.
19
20; CHECK-LABEL: no1:
21; CHECK: return $1{{$}}
22define i32 @no1(i32* %p, i32* dereferenceable(4) %q) {
23 %t = load volatile i32, i32* %q, !invariant.load !0
24 store volatile i32 0, i32* %p
25 ret i32 %t
26}
27
28; Yes because of invariant load and no side effects.
29
30; CHECK-LABEL: yes0:
31; CHECK: return $pop0{{$}}
32define i32 @yes0(i32* %p, i32* dereferenceable(4) %q) {
33 %t = load i32, i32* %q, !invariant.load !0
34 store i32 0, i32* %p
35 ret i32 %t
36}
37
38; Yes because of no intervening side effects.
39
40; CHECK-LABEL: yes1:
41; CHECK: return $pop0{{$}}
42define i32 @yes1(i32* %q) {
43 %t = load volatile i32, i32* %q
44 ret i32 %t
45}
46
Dan Gohman4da4abd2015-12-05 00:51:40 +000047; Don't schedule stack uses into the stack. To reduce register pressure, the
48; scheduler might be tempted to move the definition of $2 down. However, this
49; would risk getting incorrect liveness if the instructions are later
50; rearranged to make the stack contiguous.
51
52; CHECK-LABEL: stack_uses:
Derek Schuffb861ec82016-04-12 20:12:05 +000053; CHECK: .param i32, i32, i32, i32{{$}}
Dan Gohman4da4abd2015-12-05 00:51:40 +000054; CHECK-NEXT: .result i32{{$}}
Dan Gohman1d68e80f2016-01-12 19:14:46 +000055; CHECK-NEXT: block{{$}}
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000056; CHECK-NEXT: i32.const $push13=, 1{{$}}
57; CHECK-NEXT: i32.lt_s $push0=, $0, $pop13{{$}}
58; CHECK-NEXT: i32.const $push1=, 2{{$}}
59; CHECK-NEXT: i32.lt_s $push2=, $1, $pop1{{$}}
60; CHECK-NEXT: i32.xor $push5=, $pop0, $pop2{{$}}
61; CHECK-NEXT: i32.const $push12=, 1{{$}}
62; CHECK-NEXT: i32.lt_s $push3=, $2, $pop12{{$}}
63; CHECK-NEXT: i32.const $push11=, 2{{$}}
64; CHECK-NEXT: i32.lt_s $push4=, $3, $pop11{{$}}
65; CHECK-NEXT: i32.xor $push6=, $pop3, $pop4{{$}}
66; CHECK-NEXT: i32.xor $push7=, $pop5, $pop6{{$}}
67; CHECK-NEXT: i32.const $push10=, 1{{$}}
68; CHECK-NEXT: i32.ne $push8=, $pop7, $pop10{{$}}
Dan Gohman06b49582016-02-08 21:50:13 +000069; CHECK-NEXT: br_if 0, $pop8{{$}}
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000070; CHECK-NEXT: i32.const $push9=, 0{{$}}
71; CHECK-NEXT: return $pop9{{$}}
Dan Gohmana4730cf2016-01-07 18:49:53 +000072; CHECK-NEXT: .LBB4_2:
Dan Gohman1d68e80f2016-01-12 19:14:46 +000073; CHECK-NEXT: end_block{{$}}
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000074; CHECK-NEXT: i32.const $push14=, 1{{$}}
75; CHECK-NEXT: return $pop14{{$}}
Dan Gohmanf0b165a2015-12-05 03:03:35 +000076define i32 @stack_uses(i32 %x, i32 %y, i32 %z, i32 %w) {
Dan Gohman4da4abd2015-12-05 00:51:40 +000077entry:
Dan Gohmanf0b165a2015-12-05 03:03:35 +000078 %c = icmp sle i32 %x, 0
79 %d = icmp sle i32 %y, 1
80 %e = icmp sle i32 %z, 0
81 %f = icmp sle i32 %w, 1
82 %g = xor i1 %c, %d
83 %h = xor i1 %e, %f
84 %i = xor i1 %g, %h
85 br i1 %i, label %true, label %false
Dan Gohman4da4abd2015-12-05 00:51:40 +000086true:
87 ret i32 0
88false:
89 ret i32 1
90}
91
Dan Gohman8887d1f2015-12-25 00:31:02 +000092; Test an interesting case where the load has multiple uses and cannot
Dan Gohmanadf28172016-01-28 01:22:44 +000093; be trivially stackified. However, it can be stackified with a tee_local.
Dan Gohman8887d1f2015-12-25 00:31:02 +000094
95; CHECK-LABEL: multiple_uses:
Derek Schuffb861ec82016-04-12 20:12:05 +000096; CHECK: .param i32, i32, i32{{$}}
Dan Gohman1d68e80f2016-01-12 19:14:46 +000097; CHECK-NEXT: .local i32{{$}}
Dan Gohman1d68e80f2016-01-12 19:14:46 +000098; CHECK-NEXT: block{{$}}
Dan Gohman8aa237c2016-02-16 15:17:21 +000099; CHECK-NEXT: i32.load $push[[NUM0:[0-9]+]]=, 0($2){{$}}
100; CHECK-NEXT: tee_local $push[[NUM1:[0-9]+]]=, $3=, $pop[[NUM0]]{{$}}
101; CHECK-NEXT: i32.ge_u $push[[NUM2:[0-9]+]]=, $pop[[NUM1]], $1{{$}}
102; CHECK-NEXT: br_if 0, $pop[[NUM2]]{{$}}
103; CHECK-NEXT: i32.lt_u $push[[NUM3:[0-9]+]]=, $3, $0{{$}}
104; CHECK-NEXT: br_if 0, $pop[[NUM3]]{{$}}
Dan Gohman8887d1f2015-12-25 00:31:02 +0000105; CHECK-NEXT: i32.store $discard=, 0($2), $3{{$}}
Dan Gohmana4730cf2016-01-07 18:49:53 +0000106; CHECK-NEXT: .LBB5_3:
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000107; CHECK-NEXT: end_block{{$}}
Dan Gohman8887d1f2015-12-25 00:31:02 +0000108; CHECK-NEXT: return{{$}}
109define void @multiple_uses(i32* %arg0, i32* %arg1, i32* %arg2) nounwind {
110bb:
111 br label %loop
112
113loop:
114 %tmp7 = load i32, i32* %arg2
115 %tmp8 = inttoptr i32 %tmp7 to i32*
116 %tmp9 = icmp uge i32* %tmp8, %arg1
117 %tmp10 = icmp ult i32* %tmp8, %arg0
118 %tmp11 = or i1 %tmp9, %tmp10
119 br i1 %tmp11, label %back, label %then
120
121then:
122 store i32 %tmp7, i32* %arg2
123 br label %back
124
125back:
126 br i1 undef, label %return, label %loop
127
128return:
129 ret void
130}
131
Dan Gohman7e649172016-01-20 04:21:16 +0000132; Don't stackify stores effects across other instructions with side effects.
133
134; CHECK: side_effects:
135; CHECK: store
136; CHECK-NEXT: call
137; CHECK-NEXT: store
138; CHECK-NEXT: call
139declare void @evoke_side_effects()
140define hidden void @stackify_store_across_side_effects(double* nocapture %d) {
141entry:
142 store double 2.0, double* %d
143 call void @evoke_side_effects()
144 store double 2.0, double* %d
145 call void @evoke_side_effects()
146 ret void
147}
148
Dan Gohmanadf28172016-01-28 01:22:44 +0000149; Div instructions have side effects and can't be reordered, but this entire
150; function should still be able to be stackified because it's already in
151; tree order.
152
153; CHECK-LABEL: div_tree:
Derek Schuffb861ec82016-04-12 20:12:05 +0000154; CHECK: .param i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32{{$}}
Dan Gohmanadf28172016-01-28 01:22:44 +0000155; CHECK-NEXT: .result i32{{$}}
156; CHECK-NEXT: i32.div_s $push0=, $0, $1
157; CHECK-NEXT: i32.div_s $push1=, $2, $3
158; CHECK-NEXT: i32.div_s $push2=, $pop0, $pop1
159; CHECK-NEXT: i32.div_s $push3=, $4, $5
160; CHECK-NEXT: i32.div_s $push4=, $6, $7
161; CHECK-NEXT: i32.div_s $push5=, $pop3, $pop4
162; CHECK-NEXT: i32.div_s $push6=, $pop2, $pop5
163; CHECK-NEXT: i32.div_s $push7=, $8, $9
164; CHECK-NEXT: i32.div_s $push8=, $10, $11
165; CHECK-NEXT: i32.div_s $push9=, $pop7, $pop8
166; CHECK-NEXT: i32.div_s $push10=, $12, $13
167; CHECK-NEXT: i32.div_s $push11=, $14, $15
168; CHECK-NEXT: i32.div_s $push12=, $pop10, $pop11
169; CHECK-NEXT: i32.div_s $push13=, $pop9, $pop12
170; CHECK-NEXT: i32.div_s $push14=, $pop6, $pop13
171; CHECK-NEXT: return $pop14
172define i32 @div_tree(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %g, i32 %h, i32 %i, i32 %j, i32 %k, i32 %l, i32 %m, i32 %n, i32 %o, i32 %p) {
173entry:
174 %div = sdiv i32 %a, %b
175 %div1 = sdiv i32 %c, %d
176 %div2 = sdiv i32 %div, %div1
177 %div3 = sdiv i32 %e, %f
178 %div4 = sdiv i32 %g, %h
179 %div5 = sdiv i32 %div3, %div4
180 %div6 = sdiv i32 %div2, %div5
181 %div7 = sdiv i32 %i, %j
182 %div8 = sdiv i32 %k, %l
183 %div9 = sdiv i32 %div7, %div8
184 %div10 = sdiv i32 %m, %n
185 %div11 = sdiv i32 %o, %p
186 %div12 = sdiv i32 %div10, %div11
187 %div13 = sdiv i32 %div9, %div12
188 %div14 = sdiv i32 %div6, %div13
189 ret i32 %div14
190}
191
192; A simple multiple-use case.
193
194; CHECK-LABEL: simple_multiple_use:
Derek Schuffb861ec82016-04-12 20:12:05 +0000195; CHECK: .param i32, i32{{$}}
Dan Gohman8aa237c2016-02-16 15:17:21 +0000196; CHECK-NEXT: i32.mul $push[[NUM0:[0-9]+]]=, $1, $0{{$}}
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000197; CHECK-NEXT: tee_local $push[[NUM1:[0-9]+]]=, $[[NUM2:[0-9]+]]=, $pop[[NUM0]]{{$}}
Dan Gohman8aa237c2016-02-16 15:17:21 +0000198; CHECK-NEXT: call use_a@FUNCTION, $pop[[NUM1]]{{$}}
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000199; CHECK-NEXT: call use_b@FUNCTION, $[[NUM2]]{{$}}
Dan Gohmanadf28172016-01-28 01:22:44 +0000200; CHECK-NEXT: return{{$}}
201declare void @use_a(i32)
202declare void @use_b(i32)
203define void @simple_multiple_use(i32 %x, i32 %y) {
204 %mul = mul i32 %y, %x
205 call void @use_a(i32 %mul)
206 call void @use_b(i32 %mul)
207 ret void
208}
209
210; Multiple uses of the same value in one instruction.
211
212; CHECK-LABEL: multiple_uses_in_same_insn:
Derek Schuffb861ec82016-04-12 20:12:05 +0000213; CHECK: .param i32, i32{{$}}
Dan Gohman8aa237c2016-02-16 15:17:21 +0000214; CHECK-NEXT: i32.mul $push[[NUM0:[0-9]+]]=, $1, $0{{$}}
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000215; CHECK-NEXT: tee_local $push[[NUM1:[0-9]+]]=, $[[NUM2:[0-9]+]]=, $pop[[NUM0]]{{$}}
216; CHECK-NEXT: call use_2@FUNCTION, $pop[[NUM1]], $[[NUM2]]{{$}}
Dan Gohmanadf28172016-01-28 01:22:44 +0000217; CHECK-NEXT: return{{$}}
218declare void @use_2(i32, i32)
219define void @multiple_uses_in_same_insn(i32 %x, i32 %y) {
220 %mul = mul i32 %y, %x
221 call void @use_2(i32 %mul, i32 %mul)
222 ret void
223}
224
225; Commute operands to achieve better stackifying.
226
227; CHECK-LABEL: commute:
Derek Schuffb861ec82016-04-12 20:12:05 +0000228; CHECK-NOT: param
229; CHECK: .result i32{{$}}
Dan Gohmanadf28172016-01-28 01:22:44 +0000230; CHECK-NEXT: i32.call $push0=, red@FUNCTION{{$}}
231; CHECK-NEXT: i32.call $push1=, green@FUNCTION{{$}}
232; CHECK-NEXT: i32.add $push2=, $pop0, $pop1{{$}}
233; CHECK-NEXT: i32.call $push3=, blue@FUNCTION{{$}}
234; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}}
235; CHECK-NEXT: return $pop4{{$}}
236declare i32 @red()
237declare i32 @green()
238declare i32 @blue()
239define i32 @commute() {
240 %call = call i32 @red()
241 %call1 = call i32 @green()
242 %add = add i32 %call1, %call
243 %call2 = call i32 @blue()
244 %add3 = add i32 %add, %call2
245 ret i32 %add3
246}
247
Dan Gohmanfbfe5ec2016-01-28 03:59:09 +0000248; Don't stackify a register when it would move a the def of the register past
249; an implicit get_local for the register.
250
251; CHECK-LABEL: no_stackify_past_use:
252; CHECK: i32.call $1=, callee@FUNCTION, $0
253; CHECK: i32.const $push0=, 1
254; CHECK: i32.add $push1=, $0, $pop0
255; CHECK: i32.call $push2=, callee@FUNCTION, $pop1
256; CHECK: i32.add $push3=, $1, $pop2
257; CHECK: i32.mul $push4=, $1, $pop3
258; CHECK: return $pop4
259declare i32 @callee(i32)
260define i32 @no_stackify_past_use(i32 %arg) {
261 %tmp1 = call i32 @callee(i32 %arg)
262 %tmp2 = add i32 %arg, 1
263 %tmp3 = call i32 @callee(i32 %tmp2)
264 %tmp5 = add i32 %tmp3, %tmp1
265 %tmp6 = mul i32 %tmp5, %tmp1
266 ret i32 %tmp6
267}
268
Dan Gohman8aa237c2016-02-16 15:17:21 +0000269; Stackify individual defs of virtual registers with multiple defs.
270
271; CHECK-LABEL: multiple_defs:
272; CHECK: f64.add $push[[NUM0:[0-9]+]]=, ${{[0-9]+}}, $pop{{[0-9]+}}{{$}}
273; CHECK-NEXT: tee_local $push[[NUM1:[0-9]+]]=, $[[NUM2:[0-9]+]]=, $pop[[NUM0]]{{$}}
274; CHECK-NEXT: f64.select $push{{[0-9]+}}=, $pop{{[0-9]+}}, $pop[[NUM1]], ${{[0-9]+}}{{$}}
275; CHECK: $[[NUM2]]=,
Dan Gohman8aa237c2016-02-16 15:17:21 +0000276define void @multiple_defs(i32 %arg, i32 %arg1, i1 %arg2, i1 %arg3, i1 %arg4) {
277bb:
278 br label %bb5
279
280bb5: ; preds = %bb21, %bb
281 %tmp = phi double [ 0.000000e+00, %bb ], [ %tmp22, %bb21 ]
282 %tmp6 = phi double [ 0.000000e+00, %bb ], [ %tmp23, %bb21 ]
283 %tmp7 = fcmp olt double %tmp6, 2.323450e+01
284 br i1 %tmp7, label %bb8, label %bb21
285
286bb8: ; preds = %bb17, %bb5
287 %tmp9 = phi double [ %tmp19, %bb17 ], [ %tmp, %bb5 ]
288 %tmp10 = fadd double %tmp6, -1.000000e+00
289 %tmp11 = select i1 %arg2, double -1.135357e+04, double %tmp10
290 %tmp12 = fadd double %tmp11, %tmp9
291 br i1 %arg3, label %bb17, label %bb13
292
293bb13: ; preds = %bb8
294 %tmp14 = or i32 %arg1, 2
295 %tmp15 = icmp eq i32 %tmp14, 14
296 %tmp16 = select i1 %tmp15, double -1.135357e+04, double 0xBFCE147AE147B000
297 br label %bb17
298
299bb17: ; preds = %bb13, %bb8
300 %tmp18 = phi double [ %tmp16, %bb13 ], [ %tmp10, %bb8 ]
301 %tmp19 = fadd double %tmp18, %tmp12
302 %tmp20 = fcmp olt double %tmp6, 2.323450e+01
303 br i1 %tmp20, label %bb8, label %bb21
304
305bb21: ; preds = %bb17, %bb5
306 %tmp22 = phi double [ %tmp, %bb5 ], [ %tmp9, %bb17 ]
307 %tmp23 = fadd double %tmp6, 1.000000e+00
308 br label %bb5
309}
310
Derek Schufff8f8f092016-02-16 21:44:19 +0000311; Don't move calls past loads
312; CHECK-LABEL: no_stackify_call_past_load:
313; CHECK: i32.call $0=, red
314; CHECK: i32.const $push0=, 0
315; CHECK: i32.load $1=, count($pop0)
316@count = hidden global i32 0, align 4
317define i32 @no_stackify_call_past_load() {
318 %a = call i32 @red()
319 %b = load i32, i32* @count, align 4
320 call i32 @callee(i32 %a)
321 ret i32 %b
322 ; use of a
323}
324
325; Don't move stores past loads if there may be aliasing
326; CHECK-LABEL: no_stackify_store_past_load
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000327; CHECK: i32.store $[[L0:[0-9]+]]=, 0($1), $0
Derek Schufff8f8f092016-02-16 21:44:19 +0000328; CHECK: i32.load {{.*}}, 0($2)
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000329; CHECK: i32.call {{.*}}, callee@FUNCTION, $[[L0]]{{$}}
Derek Schufff8f8f092016-02-16 21:44:19 +0000330define i32 @no_stackify_store_past_load(i32 %a, i32* %p1, i32* %p2) {
331 store i32 %a, i32* %p1
332 %b = load i32, i32* %p2, align 4
333 call i32 @callee(i32 %a)
334 ret i32 %b
335}
336
337; Can still stackify past invariant loads.
338; CHECK-LABEL: store_past_invar_load
339; CHECK: i32.store $push{{.*}}, 0($1), $0
340; CHECK: i32.call {{.*}}, callee@FUNCTION, $pop
341; CHECK: i32.load $push{{.*}}, 0($2)
342; CHECK: return $pop
343define i32 @store_past_invar_load(i32 %a, i32* %p1, i32* dereferenceable(4) %p2) {
344 store i32 %a, i32* %p1
345 %b = load i32, i32* %p2, !invariant.load !0
346 call i32 @callee(i32 %a)
347 ret i32 %b
348}
349
Dan Gohman595e8ab2016-02-22 17:45:20 +0000350; CHECK-LABEL: ignore_dbg_value:
Derek Schuffb861ec82016-04-12 20:12:05 +0000351; CHECK-NEXT: .Lfunc_begin
Dan Gohman595e8ab2016-02-22 17:45:20 +0000352; CHECK-NEXT: unreachable
353declare void @llvm.dbg.value(metadata, i64, metadata, metadata)
354define void @ignore_dbg_value() {
Derek Schuffb861ec82016-04-12 20:12:05 +0000355 call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !7, metadata !9), !dbg !10
Dan Gohman595e8ab2016-02-22 17:45:20 +0000356 unreachable
357}
358
Dan Gohman450a8072016-05-05 20:41:15 +0000359; Don't stackify an expression that might use the stack into a return, since we
360; might insert a prologue before the return.
361
362; CHECK-LABEL: no_stackify_past_epilogue:
363; CHECK: return ${{[0-9]+}}{{$}}
364declare i32 @use_memory(i32*)
365define i32 @no_stackify_past_epilogue() {
366 %x = alloca i32
367 %call = call i32 @use_memory(i32* %x)
368 ret i32 %call
369}
370
Derek Schuffb861ec82016-04-12 20:12:05 +0000371!llvm.module.flags = !{!0}
372!llvm.dbg.cu = !{!1}
Dan Gohman595e8ab2016-02-22 17:45:20 +0000373
Derek Schuffb861ec82016-04-12 20:12:05 +0000374!0 = !{i32 2, !"Debug Info Version", i32 3}
Adrian Prantlab239892016-04-15 19:32:22 +0000375!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, producer: "clang version 3.9.0 (trunk 266005) (llvm/trunk 266105)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !3)
Derek Schuffb861ec82016-04-12 20:12:05 +0000376!2 = !DIFile(filename: "test.c", directory: "/")
377!3 = !{}
Adrian Prantl7a717c42016-04-15 19:38:14 +0000378!5 = distinct !DISubprogram(name: "test", scope: !2, file: !2, line: 10, type: !6, isLocal: false, isDefinition: true, scopeLine: 11, flags: DIFlagPrototyped, isOptimized: true, unit: !1, variables: !3)
Derek Schuffb861ec82016-04-12 20:12:05 +0000379!6 = !DISubroutineType(types: !3)
380!7 = !DILocalVariable(name: "nzcnt", scope: !5, file: !2, line: 15, type: !8)
381!8 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
382!9 = !DIExpression()
383!10 = !DILocation(line: 15, column: 6, scope: !5)