1; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s
2
3; Test that the "returned" attribute is optimized effectively.
4
5target triple = "wasm32-unknown-unknown"
6
7; CHECK-LABEL: _Z3foov:
8; CHECK-NEXT: .functype _Z3foov () -> (i32){{$}}
9; CHECK-NEXT: i32.const $push0=, 1{{$}}
10; CHECK-NEXT: {{^}} call      $push1=, _Znwm, $pop0{{$}}
11; CHECK-NEXT: {{^}} call      $push2=, _ZN5AppleC1Ev, $pop1{{$}}
12; CHECK-NEXT: return    $pop2{{$}}
13%class.Apple = type { i8 }
14declare noalias i8* @_Znwm(i32)
15declare %class.Apple* @_ZN5AppleC1Ev(%class.Apple* returned)
16define %class.Apple* @_Z3foov() {
17entry:
18  %call = tail call noalias i8* @_Znwm(i32 1)
19  %0 = bitcast i8* %call to %class.Apple*
20  %call1 = tail call %class.Apple* @_ZN5AppleC1Ev(%class.Apple* %0)
21  ret %class.Apple* %0
22}
23
24; CHECK-LABEL: _Z3barPvS_l:
25; CHECK-NEXT: .functype _Z3barPvS_l (i32, i32, i32) -> (i32){{$}}
26; CHECK-NEXT: {{^}} call     $push0=, memcpy, $0, $1, $2{{$}}
27; CHECK-NEXT: return   $pop0{{$}}
28declare i8* @memcpy(i8* returned, i8*, i32)
29define i8* @_Z3barPvS_l(i8* %p, i8* %s, i32 %n) {
30entry:
31  %call = tail call i8* @memcpy(i8* %p, i8* %s, i32 %n)
32  ret i8* %p
33}
34
35; Test that the optimization isn't performed on constant arguments.
36
37; CHECK-LABEL: test_constant_arg:
38; CHECK:      i32.const   $push0=, global{{$}}
39; CHECK-NEXT: {{^}} call        $drop=, returns_arg, $pop0{{$}}
40; CHECK-NEXT: return{{$}}
41@global = external global i32
42@addr = global i32* @global
43define void @test_constant_arg() {
44  %call = call i32* @returns_arg(i32* @global)
45  ret void
46}
47declare i32* @returns_arg(i32* returned)
48
49; Test that the optimization isn't performed on arguments without the
50; "returned" attribute.
51
52; CHECK-LABEL: test_other_skipped:
53; CHECK-NEXT: .functype test_other_skipped (i32, i32, f64) -> (){{$}}
54; CHECK-NEXT: {{^}} call     $drop=, do_something, $0, $1, $2{{$}}
55; CHECK-NEXT: {{^}} call     do_something_with_i32, $1{{$}}
56; CHECK-NEXT: {{^}} call     do_something_with_double, $2{{$}}
57declare i32 @do_something(i32 returned, i32, double)
58declare void @do_something_with_i32(i32)
59declare void @do_something_with_double(double)
60define void @test_other_skipped(i32 %a, i32 %b, double %c) {
61    %call = call i32 @do_something(i32 %a, i32 %b, double %c)
62    call void @do_something_with_i32(i32 %b)
63    call void @do_something_with_double(double %c)
64    ret void
65}
66
67; Test that the optimization is performed on arguments other than the first.
68
69; CHECK-LABEL: test_second_arg:
70; CHECK-NEXT: .functype test_second_arg (i32, i32) -> (i32){{$}}
71; CHECK-NEXT: {{^}} call     $push0=, do_something_else, $0, $1{{$}}
72; CHECK-NEXT: return   $pop0{{$}}
73declare i32 @do_something_else(i32, i32 returned)
74define i32 @test_second_arg(i32 %a, i32 %b) {
75    %call = call i32 @do_something_else(i32 %a, i32 %b)
76    ret i32 %b
77}
78