16d0c7bc1SPaulo Matos; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false -mattr=+reference-types | FileCheck %s
26d0c7bc1SPaulo Matos
3*80fb7823SAlex Bradbury%funcref = type ptr addrspace(20) ;; addrspace 20 is nonintegral
46d0c7bc1SPaulo Matos
56d0c7bc1SPaulo Matos@funcref_table = local_unnamed_addr addrspace(1) global [0 x %funcref] undef
66d0c7bc1SPaulo Matos
76d0c7bc1SPaulo Matosdefine %funcref @get_funcref_from_table(i32 %i) {
86d0c7bc1SPaulo Matos; CHECK-LABEL: get_funcref_from_table:
96d0c7bc1SPaulo Matos; CHECK-NEXT: .functype       get_funcref_from_table (i32) -> (funcref)
106d0c7bc1SPaulo Matos; CHECK-NEXT: local.get 0
116d0c7bc1SPaulo Matos; CHECK-NEXT: table.get funcref_table
126d0c7bc1SPaulo Matos; CHECK-NEXT: end_function
13*80fb7823SAlex Bradbury  %p = getelementptr [0 x %funcref], ptr addrspace (1) @funcref_table, i32 0, i32 %i
14*80fb7823SAlex Bradbury  %ref = load %funcref, ptr addrspace(1) %p
156d0c7bc1SPaulo Matos  ret %funcref %ref
166d0c7bc1SPaulo Matos}
176d0c7bc1SPaulo Matos
186d0c7bc1SPaulo Matosdefine %funcref @get_funcref_from_table_const() {
196d0c7bc1SPaulo Matos; CHECK-LABEL: get_funcref_from_table_const:
206d0c7bc1SPaulo Matos; CHECK-NEXT:  .functype      get_funcref_from_table_const () -> (funcref)
216d0c7bc1SPaulo Matos; CHECK-NEXT:  i32.const      0
226d0c7bc1SPaulo Matos; CHECK-NEXT:  table.get      funcref_table
236d0c7bc1SPaulo Matos; CHECK-NEXT:  end_function
24*80fb7823SAlex Bradbury  %p = getelementptr [0 x %funcref], ptr addrspace (1) @funcref_table, i32 0, i32 0
25*80fb7823SAlex Bradbury  %ref = load %funcref, ptr addrspace(1) %p
266d0c7bc1SPaulo Matos  ret %funcref %ref
276d0c7bc1SPaulo Matos}
286d0c7bc1SPaulo Matos
296d0c7bc1SPaulo Matosdefine %funcref @get_funcref_from_table_with_offset(i32 %i) {
306d0c7bc1SPaulo Matos; CHECK-LABEL: get_funcref_from_table_with_offset:
316d0c7bc1SPaulo Matos; CHECK-NEXT:  .functype       get_funcref_from_table_with_offset (i32) -> (funcref)
326d0c7bc1SPaulo Matos; CHECK-NEXT:  local.get       0
336d0c7bc1SPaulo Matos; CHECK-NEXT:  i32.const       2
346d0c7bc1SPaulo Matos; CHECK-NEXT:  i32.add
356d0c7bc1SPaulo Matos; CHECK-NEXT:  table.get       funcref_table
366d0c7bc1SPaulo Matos; CHECK-NEXT:  end_function
376d0c7bc1SPaulo Matos  %off = add nsw i32 %i, 2
38*80fb7823SAlex Bradbury  %p = getelementptr [0 x %funcref], ptr addrspace (1) @funcref_table, i32 0, i32 %off
39*80fb7823SAlex Bradbury  %ref = load %funcref, ptr addrspace(1) %p
406d0c7bc1SPaulo Matos  ret %funcref %ref
416d0c7bc1SPaulo Matos}
426d0c7bc1SPaulo Matos
436d0c7bc1SPaulo Matos
446d0c7bc1SPaulo Matosdefine %funcref @get_funcref_from_table_with_var_offset(i32 %i, i32 %j) {
456d0c7bc1SPaulo Matos; CHECK-LABEL: get_funcref_from_table_with_var_offset:
466d0c7bc1SPaulo Matos; CHECK-NEXT:  .functype       get_funcref_from_table_with_var_offset (i32, i32) -> (funcref)
476d0c7bc1SPaulo Matos; CHECK-NEXT:  local.get       0
486d0c7bc1SPaulo Matos; CHECK-NEXT:  local.get       1
496d0c7bc1SPaulo Matos; CHECK-NEXT:  i32.add
506d0c7bc1SPaulo Matos; CHECK-NEXT:  table.get       funcref_table
516d0c7bc1SPaulo Matos; CHECK-NEXT:  end_function
526d0c7bc1SPaulo Matos  %off = add nsw i32 %i, %j
53*80fb7823SAlex Bradbury  %p = getelementptr [0 x %funcref], ptr addrspace (1) @funcref_table, i32 0, i32 %off
54*80fb7823SAlex Bradbury  %ref = load %funcref, ptr addrspace(1) %p
556d0c7bc1SPaulo Matos  ret %funcref %ref
566d0c7bc1SPaulo Matos}
576d0c7bc1SPaulo Matos
586d0c7bc1SPaulo Matosdeclare i32 @get_offset()
596d0c7bc1SPaulo Matos
606d0c7bc1SPaulo Matosdefine %funcref @get_funcref_from_table_with_var_offset2(i32 %i) {
616d0c7bc1SPaulo Matos; CHECK-LABEL: get_funcref_from_table_with_var_offset2:
626d0c7bc1SPaulo Matos; CHECK-NEXT:  .functype       get_funcref_from_table_with_var_offset2 (i32) -> (funcref)
636d0c7bc1SPaulo Matos; CHECK-NEXT:  local.get       0
646d0c7bc1SPaulo Matos; CHECK-NEXT:  call    get_offset
656d0c7bc1SPaulo Matos; CHECK-NEXT:  i32.add
666d0c7bc1SPaulo Matos; CHECK-NEXT:  table.get       funcref_table
676d0c7bc1SPaulo Matos; CHECK-NEXT:  end_function
686d0c7bc1SPaulo Matos  %j = call i32 @get_offset()
696d0c7bc1SPaulo Matos  %off = add nsw i32 %i, %j
70*80fb7823SAlex Bradbury  %p = getelementptr [0 x %funcref], ptr addrspace (1) @funcref_table, i32 0, i32 %off
71*80fb7823SAlex Bradbury  %ref = load %funcref, ptr addrspace(1) %p
726d0c7bc1SPaulo Matos  ret %funcref %ref
736d0c7bc1SPaulo Matos}
746d0c7bc1SPaulo Matos
75b5b5f0acSPaulo Matos;       CHECK: .tabletype funcref_table, funcref
76c67c9cfeSPaulo Matos; CHECK-LABEL: funcref_table:
77