290e2b0d062d943f9dbd584c2771375be3ebf78d
[oota-llvm.git] / test / MC / MachO / AArch64 / cstexpr-gotpcrel.ll
1 ; RUN: llc -mtriple=arm64-apple-darwin %s -o %t
2 ; RUN:  FileCheck %s -check-prefix=ARM < %t
3 ; RUN:  FileCheck %s -check-prefix=ARM-GOT-EQUIV < %t
4
5 ; GOT equivalent globals references can be replaced by the GOT entry of the
6 ; final symbol instead.
7
8 %struct.data = type { i32, %struct.anon }
9 %struct.anon = type { i32, i32 }
10
11 ; Check that these got equivalent symbols are emitted on ARM64. Since ARM64 does
12 ; not support encoding an extra offset with @GOT, we still need to emit the
13 ; equivalents for use by such IR constructs.
14
15 ; ARM-GOT-EQUIV: {{.*}}extgotequiv:
16 ; ARM-GOT-EQUIV: {{.*}}localgotequiv:
17 @localfoo = global i32 42
18 @localgotequiv = private unnamed_addr constant i32* @localfoo
19
20 @extfoo = external global i32
21 @extgotequiv = private unnamed_addr constant i32* @extfoo
22
23 ; Don't replace GOT equivalent usage within instructions and emit the GOT
24 ; equivalent since it can't be replaced by the GOT entry. @bargotequiv is
25 ; used by an instruction inside @t0.
26 ;
27 ; ARM: {{.*}}bargotequiv:
28 ; ARM-NEXT:  .quad   _extbar
29 @extbar = external global i32
30 @bargotequiv = private unnamed_addr constant i32* @extbar
31
32 @table = global [4 x %struct.data] [
33 ; ARM-LABEL: _table
34   %struct.data { i32 1, %struct.anon { i32 2, i32 3 } },
35 ; Test GOT equivalent usage inside nested constant arrays.
36
37 ; ARM: .long   5
38 ; ARM-NOT: .long   _localgotequiv-(_table+20)
39 ; ARM-NEXT: Ltmp1:
40 ; ARM-NEXT: .long _localfoo@GOT-Ltmp1
41   %struct.data { i32 4, %struct.anon { i32 5,
42     i32 trunc (i64 sub (i64 ptrtoint (i32** @localgotequiv to i64),
43                         i64 ptrtoint (i32* getelementptr inbounds ([4 x %struct.data]* @table, i32 0, i64 1, i32 1, i32 1) to i64))
44                         to i32)}
45   },
46
47 ; ARM: .long   5
48 ; ARM-NOT: _extgotequiv-(_table+32)
49 ; ARM-NEXT: Ltmp2:
50 ; ARM-NEXT: _extfoo@GOT-Ltmp2
51   %struct.data { i32 4, %struct.anon { i32 5,
52     i32 trunc (i64 sub (i64 ptrtoint (i32** @extgotequiv to i64),
53                         i64 ptrtoint (i32* getelementptr inbounds ([4 x %struct.data]* @table, i32 0, i64 2, i32 1, i32 1) to i64))
54                         to i32)}
55   },
56 ; Test support for arbitrary constants into the GOTPCREL offset, which is
57 ; supported on x86-64 but not on ARM64
58
59 ; ARM: .long   5
60 ; ARM-NEXT: .long ({{.*}}extgotequiv-(_table+44))+24
61   %struct.data { i32 4, %struct.anon { i32 5,
62     i32 add (i32 trunc (i64 sub (i64 ptrtoint (i32** @extgotequiv to i64),
63                                  i64 ptrtoint (i32* getelementptr inbounds ([4 x %struct.data]* @table, i32 0, i64 3, i32 1, i32 1) to i64))
64                                  to i32), i32 24)}
65   }
66 ], align 16
67
68 ; Test multiple uses of GOT equivalents.
69
70 ; ARM-LABEL: _delta
71 ; ARM: Ltmp3:
72 ; ARM-NEXT:  .long _extfoo@GOT-Ltmp3
73 @delta = global i32 trunc (i64 sub (i64 ptrtoint (i32** @extgotequiv to i64),
74                                     i64 ptrtoint (i32* @delta to i64))
75                            to i32)
76
77 ; ARM-LABEL: _deltaplus:
78 ; ARM: .long  ({{.*}}localgotequiv-_deltaplus)+55
79 @deltaplus = global i32 add (i32 trunc (i64 sub (i64 ptrtoint (i32** @localgotequiv to i64),
80                                         i64 ptrtoint (i32* @deltaplus to i64))
81                                         to i32), i32 55)
82
83 define i32 @t0(i32 %a) {
84   %x = add i32 trunc (i64 sub (i64 ptrtoint (i32** @bargotequiv to i64),
85                                i64 ptrtoint (i32 (i32)* @t0 to i64))
86                            to i32), %a
87   ret i32 %x
88 }