1 ; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -verify-machineinstrs | FileCheck %s
3 ; Test varargs constructs.
5 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6 target triple = "wasm32-unknown-unknown"
10 ; TODO: Test va_start.
12 ; CHECK-NEXT: .functype start (i32, i32) -> ()
13 ; CHECK-NOT: __stack_pointer
14 define void @start(i8** %ap, ...) {
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)
26 ; CHECK-NEXT: .functype end (i32) -> (){{$}}
27 ; CHECK-NEXT: return{{$}}
28 define void @end(i8** %ap) {
30 %0 = bitcast i8** %ap to i8*
31 call void @llvm.va_end(i8* %0)
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{{$}}
42 define void @copy(i8** %ap, i8** %bp) {
44 %0 = bitcast i8** %ap to i8*
45 %1 = bitcast i8** %bp to i8*
46 call void @llvm.va_copy(i8* %0, i8* %1)
50 ; Test va_arg with an i8 argument.
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]]{{$}}
61 define i8 @arg_i8(i8** %ap) {
63 %t = va_arg i8** %ap, i8
67 ; Test va_arg with an i32 argument.
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]]{{$}}
82 define i32 @arg_i32(i8** %ap) {
84 %t = va_arg i8** %ap, i32
88 ; Test va_arg with an i128 argument.
90 ; CHECK-LABEL: arg_i128:
91 ; CHECK-NEXT: .functype arg_i128 (i32, i32) -> (){{$}}
96 define i128 @arg_i128(i8** %ap) {
98 %t = va_arg i8** %ap, i128
102 ; Test a varargs call with no actual arguments.
104 declare void @callee(...)
106 ; CHECK-LABEL: caller_none:
107 ; CHECK: i32.const $push0=, 0
108 ; CHECK-NEXT: call callee, $pop0
109 ; CHECK-NEXT: return{{$}}
110 define void @caller_none() {
111 call void (...) @callee()
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
120 ; CHECK-LABEL: caller_some
121 ; CHECK-DAG: i32.store
122 ; CHECK-DAG: i64.store
123 define void @caller_some() {
124 call void (...) @callee(i32 0, double 2.0)
128 ; Test a va_start call in a non-entry block
129 ; CHECK-LABEL: startbb:
130 ; CHECK: .functype startbb (i32, i32, i32) -> ()
131 define void @startbb(i1 %cond, i8** %ap, ...) {
133 br i1 %cond, label %bb0, label %bb1
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)
144 ; Test a call to a varargs function with a non-legal fixed argument.
146 declare void @callee_with_nonlegal_fixed(fp128, ...) nounwind
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]]{{$}}
153 define void @call_nonlegal_fixed() nounwind {
154 call void (fp128, ...) @callee_with_nonlegal_fixed(fp128 0xL00000000000000000000000000000000)
158 ; Test a definition a varargs function with a non-legal fixed argument.
160 ; CHECK-LABEL: nonlegal_fixed:
161 ; CHECK-NEXT: .functype nonlegal_fixed (i64, i64, i32) -> (){{$}}
162 define void @nonlegal_fixed(fp128 %x, ...) nounwind {
166 ; Test that an fp128 argument is properly aligned and allocated
167 ; within a vararg buffer.
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
186 define void @call_fp128_alignment(i8* %p) {
188 call void (...) @callee(i8 7, fp128 0xL00000000000000018000000000000000)
192 declare void @llvm.va_start(i8*)
193 declare void @llvm.va_end(i8*)
194 declare void @llvm.va_copy(i8*, i8*)