xref: /llvm-project/llvm/test/CodeGen/WebAssembly/load-store-static.ll (revision 73856247eef35f5336e485dc009842a5b991c421)
1; RUN: llc < %s -asm-verbose=false -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s -check-prefixes=NON-PIC,CHECK
2
3target triple = "wasm32-unknown-emscripten"
4
5@hidden_global         = external hidden global i32
6@hidden_global_array   = external hidden global [10 x i32]
7@external_global       = external dso_local global i32
8@external_global_array = external dso_local global [10 x i32]
9
10declare i32 @foo();
11
12; For hidden symbols PIC code needs to offset all loads and stores
13; by the value of the __memory_base global
14
15define i32 @load_hidden_global() {
16; CHECK-LABEL: load_hidden_global:
17; NON-PIC:         i32.const $push0=, 0{{$}}
18; NON-PIC-NEXT:    i32.load $push1=, hidden_global($pop0){{$}}
19; CHECK-NEXT:    end_function
20
21  %1 = load i32, ptr @hidden_global
22  ret i32 %1
23}
24
25define i32 @load_hidden_global_offset() {
26; CHECK-LABEL: load_hidden_global_offset:
27; NON-PIC:     i32.const $push0=, 0{{$}}
28; NON-PIC-NEXT:i32.load  $push1=, hidden_global_array+20($pop0){{$}}
29; CHECK-NEXT:  end_function
30
31  %1 = getelementptr [10 x i32], ptr @hidden_global_array, i32 0, i32 5
32  %2 = load i32, ptr %1
33  ret i32 %2
34}
35
36; Store to a hidden global
37
38define void @store_hidden_global(i32 %n) {
39; CHECK-LABEL: store_hidden_global:
40; NON-PIC:       i32.const $push0=, 0{{$}}
41; NON-PIC-NEXT:  i32.store hidden_global($pop0), $0{{$}}
42; CHECK-NEXT:    end_function
43
44  store i32 %n, ptr @hidden_global
45  ret void
46}
47
48define void @store_hidden_global_offset(i32 %n) {
49; CHECK-LABEL: store_hidden_global_offset:
50; NON-PIC:      i32.const $push0=, 0{{$}}
51; NON-PIC-NEXT: i32.store hidden_global_array+20($pop0), $0{{$}}
52; CHECK-NEXT:   end_function
53
54  %1 = getelementptr [10 x i32], ptr @hidden_global_array, i32 0, i32 5
55  store i32 %n, ptr %1
56  ret void
57}
58
59; For non-hidden globals PIC code has to load the address from a wasm global
60; using the @GOT relocation type.
61
62
63define i32 @load_external_global() {
64; CHECK-LABEL:  load_external_global:
65; NON-PIC:      i32.const $push0=, 0{{$}}
66; NON-PIC-NEXT: i32.load $push1=, external_global($pop0){{$}}
67; CHECK-NEXT:   end_function
68
69  %1 = load i32, ptr @external_global
70  ret i32 %1
71}
72
73define i32 @load_external_global_offset() {
74; CHECK-LABEL:  load_external_global_offset:
75; NON-PIC:      i32.const $push[[L0:[0-9]+]]=, 0{{$}}
76; NON-PIC-NEXT: i32.load $push{{[0-9]+}}=, external_global_array+20($pop[[L0]]){{$}}
77; CHECK-NEXT:   end_function
78
79  %1 = getelementptr [10 x i32], ptr @external_global_array, i32 0, i32 5
80  %2 = load i32, ptr %1
81  ret i32 %2
82}
83
84; Store to a non-hidden global via the wasm global.
85
86define void @store_external_global(i32 %n) {
87; CHECK-LABEL:  store_external_global:
88; NON-PIC:      i32.const $push0=, 0{{$}}
89; NON-PIC-NEXT: i32.store external_global($pop0), $0{{$}}
90; CHECK-NEXT:   end_function
91
92  store i32 %n, ptr @external_global
93  ret void
94}
95
96define void @store_external_global_offset(i32 %n) {
97; CHECK-LABEL:  store_external_global_offset:
98; NON-PIC:      i32.const $push0=, 0{{$}}
99; NON-PIC-NEXT: i32.store external_global_array+20($pop0), $0{{$}}
100; CHECK-NEXT:   end_function
101
102  %1 = getelementptr [10 x i32], ptr @external_global_array, i32 0, i32 5
103  store i32 %n, ptr %1
104  ret void
105}
106