Home | History | Annotate | Download | only in WebAssembly
      1 ; RUN: llc < %s -asm-verbose=false -disable-wasm-explicit-locals -enable-emscripten-cxx-exceptions -wasm-temporary-workarounds=false | FileCheck %s
      2 
      3 ; Test that function pointer casts are replaced with wrappers.
      4 
      5 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
      6 target triple = "wasm32-unknown-unknown"
      7 
      8 declare void @has_i32_arg(i32)
      9 declare i32 @has_i32_ret()
     10 declare void @vararg(...)
     11 declare void @plain(i32)
     12 
     13 declare void @foo0()
     14 declare void @foo1()
     15 declare void @foo2()
     16 declare void @foo3()
     17 
     18 ; CHECK-LABEL: test:
     19 ; CHECK-NEXT: call        .Lbitcast@FUNCTION{{$}}
     20 ; CHECK-NEXT: call        .Lbitcast@FUNCTION{{$}}
     21 ; CHECK-NEXT: call        .Lbitcast.1@FUNCTION{{$}}
     22 ; CHECK-NEXT: i32.const   $push[[L0:[0-9]+]]=, 0
     23 ; CHECK-NEXT: call        .Lbitcast.4@FUNCTION, $pop[[L0]]{{$}}
     24 ; CHECK-NEXT: i32.const   $push[[L1:[0-9]+]]=, 0
     25 ; CHECK-NEXT: call        .Lbitcast.4@FUNCTION, $pop[[L1]]{{$}}
     26 ; CHECK-NEXT: i32.const   $push[[L2:[0-9]+]]=, 0
     27 ; CHECK-NEXT: call        .Lbitcast.4@FUNCTION, $pop[[L2]]{{$}}
     28 ; CHECK-NEXT: call        foo0@FUNCTION
     29 ; CHECK-NEXT: i32.call    $drop=, .Lbitcast.5@FUNCTION{{$}}
     30 ; CHECK-NEXT: call        foo2@FUNCTION{{$}}
     31 ; CHECK-NEXT: call        foo1@FUNCTION{{$}}
     32 ; CHECK-NEXT: call        foo3@FUNCTION{{$}}
     33 ; CHECK-NEXT: end_function
     34 define void @test() {
     35 entry:
     36   call void bitcast (void (i32)* @has_i32_arg to void ()*)()
     37   call void bitcast (void (i32)* @has_i32_arg to void ()*)()
     38   call void bitcast (i32 ()* @has_i32_ret to void ()*)()
     39   call void bitcast (void ()* @foo0 to void (i32)*)(i32 0)
     40   %p = bitcast void ()* @foo0 to void (i32)*
     41   call void %p(i32 0)
     42   %q = bitcast void ()* @foo0 to void (i32)*
     43   call void %q(i32 0)
     44   %r = bitcast void (i32)* %q to void ()*
     45   call void %r()
     46   %t = call i32 bitcast (void ()* @foo1 to i32 ()*)()
     47   call void bitcast (void ()* @foo2 to void ()*)()
     48   call void @foo1()
     49   call void @foo3()
     50 
     51   ret void
     52 }
     53 
     54 ; CHECK-LABEL: test_varargs:
     55 ; CHECK:      set_global
     56 ; CHECK:      i32.const   $push[[L3:[0-9]+]]=, 0{{$}}
     57 ; CHECK-NEXT: call        .Lbitcast.2@FUNCTION, $pop[[L3]]{{$}}
     58 ; CHECK-NEXT: i32.const   $push[[L4:[0-9]+]]=, 0{{$}}
     59 ; CHECK-NEXT: i32.store   0($[[L5:[0-9]+]]), $pop[[L4]]{{$}}
     60 ; CHECK-NEXT: call        .Lbitcast.3@FUNCTION, $[[L5]]{{$}}
     61 define void @test_varargs() {
     62   call void bitcast (void (...)* @vararg to void (i32)*)(i32 0)
     63   call void (...) bitcast (void (i32)* @plain to void (...)*)(i32 0)
     64   ret void
     65 }
     66 
     67 ; Don't use wrappers when the value is stored in memory
     68 
     69 @global_func = hidden local_unnamed_addr global void ()* null
     70 
     71 ; CHECK-LABEL: test_store:
     72 ; CHECK-NEXT: i32.const   $push[[L0:[0-9]+]]=, 0{{$}}
     73 ; CHECK-NEXT: i32.const   $push[[L1:[0-9]+]]=, has_i32_ret@FUNCTION{{$}}
     74 ; CHECK-NEXT: i32.store   global_func($pop[[L0]]), $pop[[L1]]{{$}}
     75 define void @test_store() {
     76   %1 = bitcast i32 ()* @has_i32_ret to void ()*
     77   store void ()* %1, void ()** @global_func
     78   ret void
     79 }
     80 
     81 ; CHECK-LABEL: test_load:
     82 ; CHECK-NEXT: result      i32{{$}}
     83 ; CHECK-NEXT: i32.const   $push[[L0:[0-9]+]]=, 0{{$}}
     84 ; CHECK-NEXT: i32.load    $push[[L1:[0-9]+]]=, global_func($pop[[L0]]){{$}}
     85 ; CHECK-NEXT: i32.call_indirect $push{{[0-9]+}}=, $pop[[L1]]{{$}}
     86 define i32 @test_load() {
     87   %1 = load i32 ()*, i32 ()** bitcast (void ()** @global_func to i32 ()**)
     88   %2 = call i32 %1()
     89   ret i32 %2
     90 }
     91 
     92 ; Don't use wrappers when the value is passed to a function call
     93 
     94 declare void @call_func(i32 ()*)
     95 
     96 ; CHECK-LABEL: test_argument:
     97 ; CHECK-NEXT: i32.const   $push[[L0:[0-9]+]]=, has_i32_ret@FUNCTION{{$}}
     98 ; CHECK-NEXT: call        call_func@FUNCTION, $pop[[L0]]{{$}}
     99 ; CHECK-NEXT: i32.const   $push[[L1:[0-9]+]]=, has_i32_arg@FUNCTION{{$}}
    100 ; CHECK-NEXT: call        call_func@FUNCTION, $pop[[L1]]{{$}}
    101 define void @test_argument() {
    102   call void @call_func(i32 ()* @has_i32_ret)
    103   call void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*))
    104   ret void
    105 }
    106 
    107 ; Invokes should be treated like calls
    108 
    109 ; CHECK-LABEL: test_invoke:
    110 ; CHECK:      i32.const   $push[[L1:[0-9]+]]=, call_func@FUNCTION{{$}}
    111 ; CHECK-NEXT: i32.const   $push[[L0:[0-9]+]]=, has_i32_ret@FUNCTION{{$}}
    112 ; CHECK-NEXT: call        "__invoke_void_i32()*"@FUNCTION, $pop[[L1]], $pop[[L0]]{{$}}
    113 ; CHECK:      i32.const   $push[[L3:[0-9]+]]=, call_func@FUNCTION{{$}}
    114 ; CHECK-NEXT: i32.const   $push[[L2:[0-9]+]]=, has_i32_arg@FUNCTION{{$}}
    115 ; CHECK-NEXT: call        "__invoke_void_i32()*"@FUNCTION, $pop[[L3]], $pop[[L2]]{{$}}
    116 ; CHECK:      i32.const   $push[[L4:[0-9]+]]=, .Lbitcast@FUNCTION{{$}}
    117 ; CHECK-NEXT: call        __invoke_void@FUNCTION, $pop[[L4]]{{$}}
    118 declare i32 @personality(...)
    119 define void @test_invoke() personality i32 (...)* @personality {
    120 entry:
    121   invoke void @call_func(i32 ()* @has_i32_ret)
    122           to label %cont unwind label %lpad
    123 
    124 cont:
    125   invoke void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*))
    126           to label %cont2 unwind label %lpad
    127 
    128 cont2:
    129   invoke void bitcast (void (i32)* @has_i32_arg to void ()*)()
    130           to label %end unwind label %lpad
    131 
    132 lpad:
    133   %0 = landingpad { i8*, i32 }
    134           catch i8* null
    135   br label %end
    136 
    137 end:
    138   ret void
    139 }
    140 
    141 ; CHECK-LABEL: .Lbitcast:
    142 ; CHECK-NEXT: call        has_i32_arg@FUNCTION, $0{{$}}
    143 ; CHECK-NEXT: end_function
    144 
    145 ; CHECK-LABEL: .Lbitcast.1:
    146 ; CHECK-NEXT: call        $drop=, has_i32_ret@FUNCTION{{$}}
    147 ; CHECK-NEXT: end_function
    148 
    149 ; CHECK-LABEL: .Lbitcast.2:
    150 ; CHECK: call        vararg@FUNCTION, $1{{$}}
    151 ; CHECK: end_function
    152 
    153 ; CHECK-LABEL: .Lbitcast.3:
    154 ; CHECK: call        plain@FUNCTION, $1{{$}}
    155 ; CHECK: end_function
    156 
    157 ; CHECK-LABEL: .Lbitcast.4:
    158 ; CHECK-NEXT: .param      i32
    159 ; CHECK-NEXT: call        foo0@FUNCTION{{$}}
    160 ; CHECK-NEXT: end_function
    161 
    162 ; CHECK-LABEL: .Lbitcast.5:
    163 ; CHECK-NEXT: .result     i32
    164 ; CHECK-NEXT: call        foo1@FUNCTION{{$}}
    165 ; CHECK-NEXT: copy_local  $push0=, $0
    166 ; CHECK-NEXT: end_function
    167