1; RUN: llc < %s -asm-verbose=false -verify-machineinstrs | FileCheck %s
2
3; Test the register stackifier pass.
4
5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6target 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
47; 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:
53; CHECK: .param i32, i32, i32, i32{{$}}
54; CHECK-NEXT: .result i32{{$}}
55; CHECK-NEXT: block{{$}}
56; 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{{$}}
69; CHECK-NEXT: br_if       0, $pop8{{$}}
70; CHECK-NEXT: i32.const   $push9=, 0{{$}}
71; CHECK-NEXT: return      $pop9{{$}}
72; CHECK-NEXT: .LBB4_2:
73; CHECK-NEXT: end_block{{$}}
74; CHECK-NEXT: i32.const   $push14=, 1{{$}}
75; CHECK-NEXT: return      $pop14{{$}}
76define i32 @stack_uses(i32 %x, i32 %y, i32 %z, i32 %w) {
77entry:
78  %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
86true:
87  ret i32 0
88false:
89  ret i32 1
90}
91
92; Test an interesting case where the load has multiple uses and cannot
93; be trivially stackified. However, it can be stackified with a tee_local.
94
95; CHECK-LABEL: multiple_uses:
96; CHECK: .param       i32, i32, i32{{$}}
97; CHECK-NEXT: .local       i32{{$}}
98; CHECK-NEXT: block{{$}}
99; 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]]{{$}}
105; CHECK-NEXT: i32.store   $discard=, 0($2), $3{{$}}
106; CHECK-NEXT: .LBB5_3:
107; CHECK-NEXT: end_block{{$}}
108; 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
132; 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
149; 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:
154; CHECK: .param i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32{{$}}
155; 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:
195; CHECK:  .param      i32, i32{{$}}
196; CHECK-NEXT:  i32.mul     $push[[NUM0:[0-9]+]]=, $1, $0{{$}}
197; CHECK-NEXT:  tee_local   $push[[NUM1:[0-9]+]]=, $0=, $pop[[NUM0]]{{$}}
198; CHECK-NEXT:  call        use_a@FUNCTION, $pop[[NUM1]]{{$}}
199; CHECK-NEXT:  call        use_b@FUNCTION, $0{{$}}
200; 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:
213; CHECK:  .param      i32, i32{{$}}
214; CHECK-NEXT:  i32.mul     $push[[NUM0:[0-9]+]]=, $1, $0{{$}}
215; CHECK-NEXT:  tee_local   $push[[NUM1:[0-9]+]]=, $0=, $pop[[NUM0]]{{$}}
216; CHECK-NEXT:  call        use_2@FUNCTION, $pop[[NUM1]], $0{{$}}
217; 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:
228; CHECK-NOT: param
229; CHECK:  .result     i32{{$}}
230; 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
248; 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
269; 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]]=,
276; CHECK:        $[[NUM2]]=,
277define void @multiple_defs(i32 %arg, i32 %arg1, i1 %arg2, i1 %arg3, i1 %arg4) {
278bb:
279  br label %bb5
280
281bb5:                                              ; preds = %bb21, %bb
282  %tmp = phi double [ 0.000000e+00, %bb ], [ %tmp22, %bb21 ]
283  %tmp6 = phi double [ 0.000000e+00, %bb ], [ %tmp23, %bb21 ]
284  %tmp7 = fcmp olt double %tmp6, 2.323450e+01
285  br i1 %tmp7, label %bb8, label %bb21
286
287bb8:                                              ; preds = %bb17, %bb5
288  %tmp9 = phi double [ %tmp19, %bb17 ], [ %tmp, %bb5 ]
289  %tmp10 = fadd double %tmp6, -1.000000e+00
290  %tmp11 = select i1 %arg2, double -1.135357e+04, double %tmp10
291  %tmp12 = fadd double %tmp11, %tmp9
292  br i1 %arg3, label %bb17, label %bb13
293
294bb13:                                             ; preds = %bb8
295  %tmp14 = or i32 %arg1, 2
296  %tmp15 = icmp eq i32 %tmp14, 14
297  %tmp16 = select i1 %tmp15, double -1.135357e+04, double 0xBFCE147AE147B000
298  br label %bb17
299
300bb17:                                             ; preds = %bb13, %bb8
301  %tmp18 = phi double [ %tmp16, %bb13 ], [ %tmp10, %bb8 ]
302  %tmp19 = fadd double %tmp18, %tmp12
303  %tmp20 = fcmp olt double %tmp6, 2.323450e+01
304  br i1 %tmp20, label %bb8, label %bb21
305
306bb21:                                             ; preds = %bb17, %bb5
307  %tmp22 = phi double [ %tmp, %bb5 ], [ %tmp9, %bb17 ]
308  %tmp23 = fadd double %tmp6, 1.000000e+00
309  br label %bb5
310}
311
312; Don't move calls past loads
313; CHECK-LABEL: no_stackify_call_past_load:
314; CHECK: i32.call $0=, red
315; CHECK: i32.const $push0=, 0
316; CHECK: i32.load $1=, count($pop0)
317@count = hidden global i32 0, align 4
318define i32 @no_stackify_call_past_load() {
319  %a = call i32 @red()
320  %b = load i32, i32* @count, align 4
321  call i32 @callee(i32 %a)
322  ret i32 %b
323  ; use of a
324}
325
326; Don't move stores past loads if there may be aliasing
327; CHECK-LABEL: no_stackify_store_past_load
328; CHECK: i32.store {{.*}}, 0($1), $0
329; CHECK: i32.load {{.*}}, 0($2)
330; CHECK: i32.call {{.*}}, callee@FUNCTION, $0
331define i32 @no_stackify_store_past_load(i32 %a, i32* %p1, i32* %p2) {
332  store i32 %a, i32* %p1
333  %b = load i32, i32* %p2, align 4
334  call i32 @callee(i32 %a)
335  ret i32 %b
336}
337
338; Can still stackify past invariant loads.
339; CHECK-LABEL: store_past_invar_load
340; CHECK: i32.store $push{{.*}}, 0($1), $0
341; CHECK: i32.call {{.*}}, callee@FUNCTION, $pop
342; CHECK: i32.load $push{{.*}}, 0($2)
343; CHECK: return $pop
344define i32 @store_past_invar_load(i32 %a, i32* %p1, i32* dereferenceable(4) %p2) {
345  store i32 %a, i32* %p1
346  %b = load i32, i32* %p2, !invariant.load !0
347  call i32 @callee(i32 %a)
348  ret i32 %b
349}
350
351; CHECK-LABEL: ignore_dbg_value:
352; CHECK-NEXT: .Lfunc_begin
353; CHECK-NEXT: unreachable
354declare void @llvm.dbg.value(metadata, i64, metadata, metadata)
355define void @ignore_dbg_value() {
356  call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !7, metadata !9), !dbg !10
357  unreachable
358}
359
360!llvm.module.flags = !{!0}
361!llvm.dbg.cu = !{!1}
362
363!0 = !{i32 2, !"Debug Info Version", i32 3}
364!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)
365!2 = !DIFile(filename: "test.c", directory: "/")
366!3 = !{}
367!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)
368!6 = !DISubroutineType(types: !3)
369!7 = !DILocalVariable(name: "nzcnt", scope: !5, file: !2, line: 15, type: !8)
370!8 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
371!9 = !DIExpression()
372!10 = !DILocation(line: 15, column: 6, scope: !5)
373