1; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false -mattr=+reference-types | FileCheck %s
2
3%funcref = type ptr addrspace(20) ;; addrspace 20 is nonintegral
4
5@funcref_table = local_unnamed_addr addrspace(1) global [0 x %funcref] undef
6
7define %funcref @get_funcref_from_table(i32 %i) {
8; CHECK-LABEL: get_funcref_from_table:
9; CHECK-NEXT: .functype       get_funcref_from_table (i32) -> (funcref)
10; CHECK-NEXT: local.get 0
11; CHECK-NEXT: table.get funcref_table
12; CHECK-NEXT: end_function
13  %p = getelementptr [0 x %funcref], ptr addrspace (1) @funcref_table, i32 0, i32 %i
14  %ref = load %funcref, ptr addrspace(1) %p
15  ret %funcref %ref
16}
17
18define %funcref @get_funcref_from_table_const() {
19; CHECK-LABEL: get_funcref_from_table_const:
20; CHECK-NEXT:  .functype      get_funcref_from_table_const () -> (funcref)
21; CHECK-NEXT:  i32.const      0
22; CHECK-NEXT:  table.get      funcref_table
23; CHECK-NEXT:  end_function
24  %p = getelementptr [0 x %funcref], ptr addrspace (1) @funcref_table, i32 0, i32 0
25  %ref = load %funcref, ptr addrspace(1) %p
26  ret %funcref %ref
27}
28
29define %funcref @get_funcref_from_table_with_offset(i32 %i) {
30; CHECK-LABEL: get_funcref_from_table_with_offset:
31; CHECK-NEXT:  .functype       get_funcref_from_table_with_offset (i32) -> (funcref)
32; CHECK-NEXT:  local.get       0
33; CHECK-NEXT:  i32.const       2
34; CHECK-NEXT:  i32.add
35; CHECK-NEXT:  table.get       funcref_table
36; CHECK-NEXT:  end_function
37  %off = add nsw i32 %i, 2
38  %p = getelementptr [0 x %funcref], ptr addrspace (1) @funcref_table, i32 0, i32 %off
39  %ref = load %funcref, ptr addrspace(1) %p
40  ret %funcref %ref
41}
42
43
44define %funcref @get_funcref_from_table_with_var_offset(i32 %i, i32 %j) {
45; CHECK-LABEL: get_funcref_from_table_with_var_offset:
46; CHECK-NEXT:  .functype       get_funcref_from_table_with_var_offset (i32, i32) -> (funcref)
47; CHECK-NEXT:  local.get       0
48; CHECK-NEXT:  local.get       1
49; CHECK-NEXT:  i32.add
50; CHECK-NEXT:  table.get       funcref_table
51; CHECK-NEXT:  end_function
52  %off = add nsw i32 %i, %j
53  %p = getelementptr [0 x %funcref], ptr addrspace (1) @funcref_table, i32 0, i32 %off
54  %ref = load %funcref, ptr addrspace(1) %p
55  ret %funcref %ref
56}
57
58declare i32 @get_offset()
59
60define %funcref @get_funcref_from_table_with_var_offset2(i32 %i) {
61; CHECK-LABEL: get_funcref_from_table_with_var_offset2:
62; CHECK-NEXT:  .functype       get_funcref_from_table_with_var_offset2 (i32) -> (funcref)
63; CHECK-NEXT:  local.get       0
64; CHECK-NEXT:  call    get_offset
65; CHECK-NEXT:  i32.add
66; CHECK-NEXT:  table.get       funcref_table
67; CHECK-NEXT:  end_function
68  %j = call i32 @get_offset()
69  %off = add nsw i32 %i, %j
70  %p = getelementptr [0 x %funcref], ptr addrspace (1) @funcref_table, i32 0, i32 %off
71  %ref = load %funcref, ptr addrspace(1) %p
72  ret %funcref %ref
73}
74
75;       CHECK: .tabletype funcref_table, funcref
76; CHECK-LABEL: funcref_table:
77