1; RUN: llc < %s -emulated-tls -mcpu=generic -mtriple=x86_64-linux-gnu -relocation-model=pic \
2; RUN:   | FileCheck -check-prefix=X64 %s
3; RUN: llc < %s -emulated-tls -mcpu=generic -mtriple=i386-linux-gnu -relocation-model=pic \
4; RUN:   | FileCheck -check-prefix=X32 %s
5
6; RUN: llc < %s -mcpu=generic -mtriple=x86_64-linux-gnu -relocation-model=pic \
7; RUN:   | FileCheck -check-prefix=X64 %s
8; RUN: llc < %s -mcpu=generic -mtriple=i386-linux-gnu -relocation-model=pic \
9; RUN:   | FileCheck -check-prefix=X32 %s
10
11; External Linkage
12@a = global i32 0, align 4
13
14define i32 @my_access_global_a() #0 {
15; X32-LABEL: my_access_global_a:
16; X32:       addl $_GLOBAL_OFFSET_TABLE_{{.*}}, %eax
17; X32-NEXT:  movl a@GOTOFF(%eax), %eax
18; X64-LABEL: my_access_global_a:
19; X64:       movl a(%rip), %eax
20
21entry:
22  %0 = load i32, i32* @a, align 4
23  ret i32 %0
24}
25
26; WeakAny Linkage
27@b = weak global i32 0, align 4
28
29define i32 @my_access_global_b() #0 {
30; X32-LABEL: my_access_global_b:
31; X32:       addl $_GLOBAL_OFFSET_TABLE_{{.*}}, %eax
32; X32-NEXT:  movl b@GOTOFF(%eax), %eax
33; X64-LABEL: my_access_global_b:
34; X64:       movl b(%rip), %eax
35
36entry:
37  %0 = load i32, i32* @b, align 4
38  ret i32 %0
39}
40
41; Internal Linkage
42@c = internal global i32 0, align 4
43
44define i32 @my_access_global_c() #0 {
45; X32-LABEL: my_access_global_c:
46; X32:       addl $_GLOBAL_OFFSET_TABLE_{{.*}}, %eax
47; X32-NEXT:  movl c@GOTOFF(%eax), %eax
48; X64-LABEL: my_access_global_c:
49; X64:       movl c(%rip), %eax
50
51entry:
52  %0 = load i32, i32* @c, align 4
53  ret i32 %0
54}
55
56; External Linkage, only declaration.
57@d = external global i32, align 4
58
59define i32 @my_access_global_load_d() #0 {
60; X32-LABEL: my_access_global_load_d:
61; X32:       addl $_GLOBAL_OFFSET_TABLE_{{.*}}, %eax
62; X32-NEXT:  movl d@GOT(%eax), %eax
63; X32-NEXT:  movl (%eax), %eax
64; X64-LABEL: my_access_global_load_d:
65; X64:       movq d@GOTPCREL(%rip), %rax
66; X64-NEXT:  movl (%rax), %eax
67
68entry:
69  %0 = load i32, i32* @d, align 4
70  ret i32 %0
71}
72
73; External Linkage, only declaration, store a value.
74
75define i32 @my_access_global_store_d() #0 {
76; X32-LABEL: my_access_global_store_d:
77; X32:       addl $_GLOBAL_OFFSET_TABLE_{{.*}}, %eax
78; X32-NEXT:  movl d@GOT(%eax), %eax
79; X32-NEXT:  movl $2, (%eax)
80; X64-LABEL: my_access_global_store_d:
81; X64:       movq d@GOTPCREL(%rip), %rax
82; X64-NEXT:  movl $2, (%rax)
83
84entry:
85  store i32 2, i32* @d, align 4
86  ret i32 0
87}
88
89; External Linkage, function pointer access.
90declare i32 @access_fp(i32 ()*)
91declare i32 @foo()
92
93define i32 @my_access_fp_foo() #0 {
94; X32-LABEL: my_access_fp_foo:
95; X32:       addl $_GLOBAL_OFFSET_TABLE_{{.*}}, %ebx
96; X32-NEXT:  movl	foo@GOT(%ebx), %eax
97; X64-LABEL: my_access_fp_foo:
98; X64:       movq foo@GOTPCREL(%rip), %rdi
99
100entry:
101  %call = call i32 @access_fp(i32 ()* @foo)
102  ret i32 %call
103}
104
105; LinkOnceODR Linkage, function pointer access.
106
107$bar = comdat any
108
109define linkonce_odr i32 @bar() comdat {
110entry:
111  ret i32 0
112}
113
114define i32 @my_access_fp_bar() #0 {
115; X32-LABEL: my_access_fp_bar:
116; X32:       addl $_GLOBAL_OFFSET_TABLE_{{.*}}, %ebx
117; X32-NEXT:  leal	bar@GOTOFF(%ebx), %eax
118; X64-LABEL: my_access_fp_bar:
119; X64:       leaq bar(%rip), %rdi
120
121entry:
122  %call = call i32 @access_fp(i32 ()* @bar)
123  ret i32 %call
124}
125
126!llvm.module.flags = !{!0, !1}
127!0 = !{i32 1, !"PIC Level", i32 1}
128!1 = !{i32 1, !"PIE Level", i32 1}
129