1 ; RUN: llc < %s -asm-verbose=false -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s -check-prefixes=NON-PIC,CHECK
2 ; RUN: llc < %s -asm-verbose=false -relocation-model=pic -fast-isel -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s -check-prefixes=PIC,CHECK
3 ; RUN: llc < %s -asm-verbose=false -relocation-model=pic -fast-isel=false -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s -check-prefixes=PIC,CHECK
5 ; Test that globals assemble as expected with -fPIC.
6 ; We test here both with and without fast-isel.
8 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
9 target triple = "wasm32-unknown-emscripten"
11 @hidden_global = external hidden global i32
12 @hidden_global_array = external hidden global [10 x i32]
13 @external_global = external global i32
14 @external_global_array = external global [10 x i32]
18 ; For hidden symbols PIC code needs to offset all loads and stores
19 ; by the value of the __memory_base global
21 define i32 @load_hidden_global() {
22 ; CHECK-LABEL: load_hidden_global:
23 ; PIC: global.get $push[[L0:[0-9]+]]=, __memory_base{{$}}
24 ; PIC-NEXT: i32.const $push[[L1:[0-9]+]]=, hidden_global@MBREL{{$}}
25 ; PIC-NEXT: i32.add $push[[L2:[0-9]+]]=, $pop[[L0]], $pop[[L1]]{{$}}
26 ; PIC-NEXT: i32.load $push[[L3:[0-9]+]]=, 0($pop[[L2]]){{$}}
28 ; NON-PIC: i32.const $push0=, 0{{$}}
29 ; NON-PIC-NEXT: i32.load $push1=, hidden_global($pop0){{$}}
30 ; CHECK-NEXT: end_function
32 %1 = load i32, i32* @hidden_global
36 define i32 @load_hidden_global_offset() {
37 ; CHECK-LABEL: load_hidden_global_offset:
38 ; PIC: global.get $push[[L0:[0-9]+]]=, __memory_base{{$}}
39 ; PIC-NEXT: i32.const $push[[L1:[0-9]+]]=, hidden_global_array@MBREL{{$}}
40 ; PIC-NEXT: i32.add $push[[L2:[0-9]+]]=, $pop[[L0]], $pop[[L1:[0-9]+]]{{$}}
41 ; PIC-NEXT: i32.const $push[[L3:[0-9]+]]=, 20{{$}}
42 ; PIC-NEXT: i32.add $push[[L4:[0-9]+]]=, $pop[[L2]], $pop[[L3]]{{$}}
43 ; PIC-NEXT: i32.load $push{{[0-9]+}}=, 0($pop[[L4]]){{$}}
45 ; NON-PIC: i32.const $push0=, 0{{$}}
46 ; NON-PIC-NEXT:i32.load $push1=, hidden_global_array+20($pop0){{$}}
47 ; CHECK-NEXT: end_function
49 %1 = getelementptr [10 x i32], [10 x i32]* @hidden_global_array, i32 0, i32 5
50 %2 = load i32, i32* %1
54 ; Store to a hidden global
56 define void @store_hidden_global(i32 %n) {
57 ; CHECK-LABEL: store_hidden_global:
58 ; PIC: global.get $push[[L0:[0-9]+]]=, __memory_base{{$}}
59 ; PIC-NEXT: i32.const $push[[L1:[0-9]+]]=, hidden_global@MBREL{{$}}
60 ; PIC-NEXT: i32.add $push[[L2:[0-9]+]]=, $pop[[L0]], $pop[[L1]]{{$}}
61 ; PIC-NEXT: i32.store 0($pop[[L2]]), $0{{$}}
63 ; NON-PIC: i32.const $push0=, 0{{$}}
64 ; NON-PIC-NEXT: i32.store hidden_global($pop0), $0{{$}}
65 ; CHECK-NEXT: end_function
67 store i32 %n, i32* @hidden_global
71 define void @store_hidden_global_offset(i32 %n) {
72 ; CHECK-LABEL: store_hidden_global_offset:
73 ; PIC: global.get $push[[L0:[0-9]+]]=, __memory_base{{$}}
74 ; PIC-NEXT: i32.const $push[[L1:[0-9]+]]=, hidden_global_array@MBREL{{$}}
75 ; PIC-NEXT: i32.add $push[[L2:[0-9]+]]=, $pop[[L0]], $pop[[L1]]{{$}}
76 ; PIC-NEXT: i32.const $push[[L3:[0-9]+]]=, 20{{$}}
77 ; PIC-NEXT: i32.add $push[[L4:[0-9]+]]=, $pop[[L2]], $pop[[L3]]{{$}}
78 ; PIC-NEXT: i32.store 0($pop[[L4]]), $0{{$}}
80 ; NON-PIC: i32.const $push0=, 0{{$}}
81 ; NON-PIC-NEXT: i32.store hidden_global_array+20($pop0), $0{{$}}
82 ; CHECK-NEXT: end_function
84 %1 = getelementptr [10 x i32], [10 x i32]* @hidden_global_array, i32 0, i32 5
89 ; For non-hidden globals PIC code has to load the address from a wasm global
90 ; using the @GOT relocation type.
93 define i32 @load_external_global() {
94 ; CHECK-LABEL: load_external_global:
95 ; PIC: global.get $push[[L0:[0-9]+]]=, external_global@GOT{{$}}
96 ; PIC-NEXT: i32.load $push{{[0-9]+}}=, 0($pop[[L0]]){{$}}
98 ; NON-PIC: i32.const $push0=, 0{{$}}
99 ; NON-PIC-NEXT: i32.load $push1=, external_global($pop0){{$}}
100 ; CHECK-NEXT: end_function
102 %1 = load i32, i32* @external_global
106 define i32 @load_external_global_offset() {
107 ; CHECK-LABEL: load_external_global_offset:
108 ; PIC: global.get $push[[L0:[0-9]+]]=, external_global_array@GOT{{$}}
109 ; PIC-NEXT: i32.const $push[[L1:[0-9]+]]=, 20{{$}}
110 ; PIC-NEXT: i32.add $push[[L2:[0-9]+]]=, $pop[[L0]], $pop[[L1]]{{$}}
111 ; PIC-NEXT: i32.load $push{{[0-9]+}}=, 0($pop[[L2]]){{$}}
113 ; NON-PIC: i32.const $push0=, 0{{$}}
114 ; NON-PIC-NEXT: i32.load $push1=, external_global_array+20($pop0){{$}}
115 ; CHECK-NEXT: end_function
117 %1 = getelementptr [10 x i32], [10 x i32]* @external_global_array, i32 0, i32 5
118 %2 = load i32, i32* %1
122 ; Store to a non-hidden global via the wasm global.
124 define void @store_external_global(i32 %n) {
125 ; CHECK-LABEL: store_external_global:
126 ; PIC: global.get $push[[L0:[0-9]+]]=, external_global@GOT{{$}}
127 ; PIC-NEXT: i32.store 0($pop[[L0]]), $0{{$}}
129 ; NON-PIC: i32.const $push0=, 0{{$}}
130 ; NON-PIC-NEXT: i32.store external_global($pop0), $0{{$}}
131 ; CHECK-NEXT: end_function
133 store i32 %n, i32* @external_global
137 define void @store_external_global_offset(i32 %n) {
138 ; CHECK-LABEL: store_external_global_offset:
139 ; PIC: global.get $push[[L0:[0-9]+]]=, external_global_array@GOT{{$}}
140 ; PIC-NEXT: i32.const $push[[L1:[0-9]+]]=, 20{{$}}
141 ; PIC-NEXT: i32.add $push[[L2:[0-9]+]]=, $pop[[L0]], $pop[[L1]]{{$}}
142 ; PIC-NEXT: i32.store 0($pop[[L2]]), $0{{$}}
144 ; NON-PIC: i32.const $push0=, 0{{$}}
145 ; NON-PIC-NEXT: i32.store external_global_array+20($pop0), $0{{$}}
146 ; CHECK-NEXT: end_function
148 %1 = getelementptr [10 x i32], [10 x i32]* @external_global_array, i32 0, i32 5
149 store i32 %n, i32* %1
153 ; PIC: .globaltype __memory_base, i32