• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -verify-machineinstrs | FileCheck %s
2
3; Test varargs constructs.
4
5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6target triple = "wasm32-unknown-unknown"
7
8; Test va_start.
9
10; TODO: Test va_start.
11; CHECK-LABEL: start:
12; CHECK-NEXT: .functype start (i32, i32) -> ()
13; CHECK-NOT: __stack_pointer
14define void @start(i8** %ap, ...) {
15entry:
16  %0 = bitcast i8** %ap to i8*
17; Store the second argument (the hidden vararg buffer pointer) into ap
18; CHECK: i32.store 0($0), $1
19  call void @llvm.va_start(i8* %0)
20  ret void
21}
22
23; Test va_end.
24
25; CHECK-LABEL: end:
26; CHECK-NEXT: .functype end (i32) -> (){{$}}
27; CHECK-NEXT: return{{$}}
28define void @end(i8** %ap) {
29entry:
30  %0 = bitcast i8** %ap to i8*
31  call void @llvm.va_end(i8* %0)
32  ret void
33}
34
35; Test va_copy.
36
37; CHECK-LABEL: copy:
38; CHECK-NEXT: .functype copy (i32, i32) -> (){{$}}
39; CHECK-NEXT: i32.load  $push0=, 0($1){{$}}
40; CHECK-NEXT: i32.store 0($0), $pop0{{$}}
41; CHECK-NEXT: return{{$}}
42define void @copy(i8** %ap, i8** %bp) {
43entry:
44  %0 = bitcast i8** %ap to i8*
45  %1 = bitcast i8** %bp to i8*
46  call void @llvm.va_copy(i8* %0, i8* %1)
47  ret void
48}
49
50; Test va_arg with an i8 argument.
51
52; CHECK-LABEL: arg_i8:
53; CHECK-NEXT: .functype arg_i8 (i32) -> (i32){{$}}
54; CHECK-NEXT: i32.load   $push[[NUM0:[0-9]+]]=, 0($0){{$}}
55; CHECK-NEXT: local.tee  $push[[NUM1:[0-9]+]]=, $1=, $pop[[NUM0]]{{$}}
56; CHECK-NEXT: i32.const  $push[[NUM2:[0-9]+]]=, 4{{$}}
57; CHECK-NEXT: i32.add    $push[[NUM3:[0-9]+]]=, $pop[[NUM1]], $pop[[NUM2]]{{$}}
58; CHECK-NEXT: i32.store  0($0), $pop[[NUM3]]{{$}}
59; CHECK-NEXT: i32.load   $push[[NUM4:[0-9]+]]=, 0($1){{$}}
60; CHECK-NEXT: return     $pop[[NUM4]]{{$}}
61define i8 @arg_i8(i8** %ap) {
62entry:
63  %t = va_arg i8** %ap, i8
64  ret i8 %t
65}
66
67; Test va_arg with an i32 argument.
68
69; CHECK-LABEL: arg_i32:
70; CHECK-NEXT: .functype arg_i32 (i32) -> (i32){{$}}
71; CHECK-NEXT: i32.load   $push[[NUM0:[0-9]+]]=, 0($0){{$}}
72; CHECK-NEXT: i32.const  $push[[NUM1:[0-9]+]]=, 3{{$}}
73; CHECK-NEXT: i32.add    $push[[NUM2:[0-9]+]]=, $pop[[NUM0]], $pop[[NUM1]]{{$}}
74; CHECK-NEXT: i32.const  $push[[NUM3:[0-9]+]]=, -4{{$}}
75; CHECK-NEXT: i32.and    $push[[NUM4:[0-9]+]]=, $pop[[NUM2]], $pop[[NUM3]]{{$}}
76; CHECK-NEXT: local.tee  $push[[NUM5:[0-9]+]]=, $1=, $pop[[NUM4]]{{$}}
77; CHECK-NEXT: i32.const  $push[[NUM6:[0-9]+]]=, 4{{$}}
78; CHECK-NEXT: i32.add    $push[[NUM7:[0-9]+]]=, $pop[[NUM5]], $pop[[NUM6]]{{$}}
79; CHECK-NEXT: i32.store  0($0), $pop[[NUM7]]{{$}}
80; CHECK-NEXT: i32.load   $push[[NUM8:[0-9]+]]=, 0($1){{$}}
81; CHECK-NEXT: return     $pop[[NUM8]]{{$}}
82define i32 @arg_i32(i8** %ap) {
83entry:
84  %t = va_arg i8** %ap, i32
85  ret i32 %t
86}
87
88; Test va_arg with an i128 argument.
89
90; CHECK-LABEL: arg_i128:
91; CHECK-NEXT: .functype arg_i128 (i32, i32) -> (){{$}}
92; CHECK: i32.and
93; CHECK: i64.load
94; CHECK: i64.load
95; CHECK: return{{$}}
96define i128 @arg_i128(i8** %ap) {
97entry:
98  %t = va_arg i8** %ap, i128
99  ret i128 %t
100}
101
102; Test a varargs call with no actual arguments.
103
104declare void @callee(...)
105
106; CHECK-LABEL: caller_none:
107; CHECK:      i32.const $push0=, 0
108; CHECK-NEXT: call callee, $pop0
109; CHECK-NEXT: return{{$}}
110define void @caller_none() {
111  call void (...) @callee()
112  ret void
113}
114
115; Test a varargs call with some actual arguments.
116; Note that the store of 2.0 is converted to an i64 store; this optimization
117; is not needed on WebAssembly, but there isn't currently a convenient hook for
118; disabling it.
119
120; CHECK-LABEL: caller_some
121; CHECK-DAG: i32.store
122; CHECK-DAG: i64.store
123define void @caller_some() {
124  call void (...) @callee(i32 0, double 2.0)
125  ret void
126}
127
128; Test a va_start call in a non-entry block
129; CHECK-LABEL: startbb:
130; CHECK: .functype startbb (i32, i32, i32) -> ()
131define void @startbb(i1 %cond, i8** %ap, ...) {
132entry:
133  br i1 %cond, label %bb0, label %bb1
134bb0:
135  ret void
136bb1:
137  %0 = bitcast i8** %ap to i8*
138; Store the second argument (the hidden vararg buffer pointer) into ap
139; CHECK: i32.store 0($1), $2
140  call void @llvm.va_start(i8* %0)
141  ret void
142}
143
144; Test a call to a varargs function with a non-legal fixed argument.
145
146declare void @callee_with_nonlegal_fixed(fp128, ...) nounwind
147
148; CHECK-LABEL: call_nonlegal_fixed:
149; CHECK: i64.const       $push[[L0:[0-9]+]]=, 0
150; CHECK: i64.const       $push[[L1:[0-9]+]]=, 0
151; CHECK: i32.const       $push[[L2:[0-9]+]]=, 0
152; CHECK: call            callee_with_nonlegal_fixed, $pop[[L0]], $pop[[L1]], $pop[[L2]]{{$}}
153define void @call_nonlegal_fixed() nounwind {
154  call void (fp128, ...) @callee_with_nonlegal_fixed(fp128 0xL00000000000000000000000000000000)
155  ret void
156}
157
158; Test a definition a varargs function with a non-legal fixed argument.
159
160; CHECK-LABEL: nonlegal_fixed:
161; CHECK-NEXT: .functype nonlegal_fixed (i64, i64, i32) -> (){{$}}
162define void @nonlegal_fixed(fp128 %x, ...) nounwind {
163  ret void
164}
165
166; Test that an fp128 argument is properly aligned and allocated
167; within a vararg buffer.
168
169; CHECK-LABEL: call_fp128_alignment:
170; CHECK:      global.get      $push7=, __stack_pointer
171; CHECK-NEXT: i32.const       $push8=, 32
172; CHECK-NEXT: i32.sub         $push12=, $pop7, $pop8
173; CHECK-NEXT: local.tee       $push11=, $1=, $pop12
174; CHECK-NEXT: global.set      __stack_pointer, $pop11
175; CHECK-NEXT: i32.const       $push0=, 24
176; CHECK-NEXT: i32.add         $push1=, $1, $pop0
177; CHECK-NEXT: i64.const       $push2=, -9223372036854775808
178; CHECK-NEXT: i64.store       0($pop1), $pop2
179; CHECK-NEXT: i32.const       $push3=, 16
180; CHECK-NEXT: i32.add         $push4=, $1, $pop3
181; CHECK-NEXT: i64.const       $push5=, 1
182; CHECK-NEXT: i64.store       0($pop4), $pop5
183; CHECK-NEXT: i32.const       $push6=, 7
184; CHECK-NEXT: i32.store       0($1), $pop6
185; CHECK-NEXT: call            callee, $1
186define void @call_fp128_alignment(i8* %p) {
187entry:
188  call void (...) @callee(i8 7, fp128 0xL00000000000000018000000000000000)
189  ret void
190}
191
192declare void @llvm.va_start(i8*)
193declare void @llvm.va_end(i8*)
194declare void @llvm.va_copy(i8*, i8*)
195