xref: /llvm-project/llvm/test/MC/MachO/cstexpr-gotpcrel-64.ll (revision d19616fc66008ad128ecd21536c6e52992bea4e6)
1; RUN: llc -mtriple=x86_64-apple-darwin %s -o %t
2; RUN:  FileCheck %s -check-prefix=X86 < %t
3; RUN:  FileCheck %s -check-prefix=X86-GOT-EQUIV < %t
4; RUN:  FileCheck %s -check-prefix=X86-NOGOT-EQUIV < %t
5
6; GOT equivalent globals references can be replaced by the GOT entry of the
7; final symbol instead.
8
9%struct.data = type { i32, %struct.anon }
10%struct.anon = type { i32, i32 }
11
12; Check that these got equivalent symbols are never emitted.
13
14; X86-GOT-EQUIV-NOT: L_localgotequiv
15; X86-GOT-EQUIV-NOT: l_extgotequiv
16@localfoo = global i32 42
17@localgotequiv = private unnamed_addr constant ptr @localfoo
18
19@extfoo = external global i32
20@extgotequiv = private unnamed_addr constant ptr @extfoo
21
22; Don't replace GOT equivalent usage within instructions and emit the GOT
23; equivalent since it can't be replaced by the GOT entry. @bargotequiv is
24; used by an instruction inside @t0.
25;
26; X86: l_bargotequiv:
27; X86-NEXT:  .quad   _extbar
28@extbar = external global i32
29@bargotequiv = private unnamed_addr constant ptr @extbar
30
31@table = global [4 x %struct.data] [
32  %struct.data { i32 1, %struct.anon { i32 2, i32 3 } },
33; Test GOT equivalent usage inside nested constant arrays.
34
35; X86: .long   5
36; X86-NOT: .long   _localgotequiv-(_table+20)
37; X86-NEXT: .long   _localfoo@GOTPCREL+4
38  %struct.data { i32 4, %struct.anon { i32 5,
39    i32 trunc (i64 sub (i64 ptrtoint (ptr @localgotequiv to i64),
40                        i64 ptrtoint (ptr getelementptr inbounds ([4 x %struct.data], ptr @table, i32 0, i64 1, i32 1, i32 1) to i64))
41                        to i32)}
42  },
43; X86: .long   5
44; X86-NOT: _extgotequiv-(_table+32)
45; X86-NEXT: .long   _extfoo@GOTPCREL+4
46  %struct.data { i32 4, %struct.anon { i32 5,
47    i32 trunc (i64 sub (i64 ptrtoint (ptr @extgotequiv to i64),
48                        i64 ptrtoint (ptr getelementptr inbounds ([4 x %struct.data], ptr @table, i32 0, i64 2, i32 1, i32 1) to i64))
49                        to i32)}
50  },
51; Test support for arbitrary constants into the GOTPCREL offset.
52
53; X86: .long   5
54; X86-NOT: _extgotequiv-(_table+44)
55; X86-NEXT: .long   _extfoo@GOTPCREL+28
56  %struct.data { i32 4, %struct.anon { i32 5,
57    i32 add (i32 trunc (i64 sub (i64 ptrtoint (ptr @extgotequiv to i64),
58                                 i64 ptrtoint (ptr getelementptr inbounds ([4 x %struct.data], ptr @table, i32 0, i64 3, i32 1, i32 1) to i64))
59                                 to i32), i32 24)}
60  }
61], align 16
62
63; Test multiple uses of GOT equivalents.
64
65; X86-LABEL: _delta
66; X86: .long   _extfoo@GOTPCREL+4
67@delta = global i32 trunc (i64 sub (i64 ptrtoint (ptr @extgotequiv to i64),
68                                    i64 ptrtoint (ptr @delta to i64))
69                           to i32)
70
71; X86-LABEL: _deltaplus:
72; X86: .long   _localfoo@GOTPCREL+59
73@deltaplus = global i32 add (i32 trunc (i64 sub (i64 ptrtoint (ptr @localgotequiv to i64),
74                                        i64 ptrtoint (ptr @deltaplus to i64))
75                                        to i32), i32 55)
76
77define i32 @t0(i32 %a) {
78  %x = add i32 trunc (i64 sub (i64 ptrtoint (ptr @bargotequiv to i64),
79                               i64 ptrtoint (ptr @t0 to i64))
80                           to i32), %a
81  ret i32 %x
82}
83
84; Also test direct instruction uses.
85define ptr @t1() {
86  ret ptr @bargotequiv
87}
88
89; Do not crash when a pattern cannot be matched as a GOT equivalent
90define void @foo() {
91; X86-NOGOT-EQUIV-LABEL: _foo:
92; X86-NOGOT-EQUIV: leaq  _b(%rip), %rax
93  store ptr @b, ptr null
94  ret void
95}
96@a = external global i8
97@b = internal unnamed_addr constant ptr @a
98
99; X86-NOGOT-EQUIV-LABEL: _c:
100; X86-NOGOT-EQUIV:   .quad _b
101@c = global ptr @b
102
103; X86-LABEL: table_with_negative_offset:
104; X86-NEXT:    .long   _a@GOTPCREL+4294967292
105@table_with_negative_offset = dso_local unnamed_addr constant [3 x i32] [
106  i32 trunc (
107    i64 sub (
108      i64 ptrtoint (ptr @b to i64),
109      i64 ptrtoint (ptr getelementptr inbounds ([3 x i32], ptr @table_with_negative_offset, i32 0, i32 2) to i64)
110    )
111    to i32),
112  i32 0,
113  i32 0
114], align 4
115