Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 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 |
Bruno Cardoso Lopes | e864051 | 2015-07-21 21:45:42 +0000 | [diff] [blame] | 4 | ; RUN: FileCheck %s -check-prefix=X86-NOGOT-EQUIV < %t |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 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 | |
Bruno Cardoso Lopes | 2d54aa4 | 2015-03-06 14:41:56 +0000 | [diff] [blame] | 12 | ; Check that these got equivalent symbols are never emitted. |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 13 | |
| 14 | ; X86-GOT-EQUIV-NOT: L_localgotequiv |
| 15 | ; X86-GOT-EQUIV-NOT: l_extgotequiv |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 16 | @localfoo = global i32 42 |
| 17 | @localgotequiv = private unnamed_addr constant i32* @localfoo |
| 18 | |
| 19 | @extfoo = external global i32 |
| 20 | @extgotequiv = private unnamed_addr constant i32* @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 |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 28 | @extbar = external global i32 |
| 29 | @bargotequiv = private unnamed_addr constant i32* @extbar |
| 30 | |
| 31 | @table = global [4 x %struct.data] [ |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 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 |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 38 | %struct.data { i32 4, %struct.anon { i32 5, |
| 39 | i32 trunc (i64 sub (i64 ptrtoint (i32** @localgotequiv to i64), |
David Blaikie | f72d05b | 2015-03-13 18:20:45 +0000 | [diff] [blame] | 40 | i64 ptrtoint (i32* getelementptr inbounds ([4 x %struct.data], [4 x %struct.data]* @table, i32 0, i64 1, i32 1, i32 1) to i64)) |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 41 | to i32)} |
| 42 | }, |
| 43 | ; X86: .long 5 |
| 44 | ; X86-NOT: _extgotequiv-(_table+32) |
| 45 | ; X86-NEXT: .long _extfoo@GOTPCREL+4 |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 46 | %struct.data { i32 4, %struct.anon { i32 5, |
| 47 | i32 trunc (i64 sub (i64 ptrtoint (i32** @extgotequiv to i64), |
David Blaikie | f72d05b | 2015-03-13 18:20:45 +0000 | [diff] [blame] | 48 | i64 ptrtoint (i32* getelementptr inbounds ([4 x %struct.data], [4 x %struct.data]* @table, i32 0, i64 2, i32 1, i32 1) to i64)) |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 49 | to i32)} |
| 50 | }, |
Bruno Cardoso Lopes | 2d54aa4 | 2015-03-06 14:41:56 +0000 | [diff] [blame] | 51 | ; Test support for arbitrary constants into the GOTPCREL offset. |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 52 | |
| 53 | ; X86: .long 5 |
| 54 | ; X86-NOT: _extgotequiv-(_table+44) |
| 55 | ; X86-NEXT: .long _extfoo@GOTPCREL+28 |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 56 | %struct.data { i32 4, %struct.anon { i32 5, |
| 57 | i32 add (i32 trunc (i64 sub (i64 ptrtoint (i32** @extgotequiv to i64), |
David Blaikie | f72d05b | 2015-03-13 18:20:45 +0000 | [diff] [blame] | 58 | i64 ptrtoint (i32* getelementptr inbounds ([4 x %struct.data], [4 x %struct.data]* @table, i32 0, i64 3, i32 1, i32 1) to i64)) |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 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 |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 67 | @delta = global i32 trunc (i64 sub (i64 ptrtoint (i32** @extgotequiv to i64), |
| 68 | i64 ptrtoint (i32* @delta to i64)) |
| 69 | to i32) |
| 70 | |
| 71 | ; X86-LABEL: _deltaplus: |
| 72 | ; X86: .long _localfoo@GOTPCREL+59 |
Bruno Cardoso Lopes | 52b1391 | 2015-03-06 13:48:45 +0000 | [diff] [blame] | 73 | @deltaplus = global i32 add (i32 trunc (i64 sub (i64 ptrtoint (i32** @localgotequiv to i64), |
| 74 | i64 ptrtoint (i32* @deltaplus to i64)) |
| 75 | to i32), i32 55) |
| 76 | |
| 77 | define i32 @t0(i32 %a) { |
| 78 | %x = add i32 trunc (i64 sub (i64 ptrtoint (i32** @bargotequiv to i64), |
| 79 | i64 ptrtoint (i32 (i32)* @t0 to i64)) |
| 80 | to i32), %a |
| 81 | ret i32 %x |
| 82 | } |
Ahmed Bougacha | 821880a | 2015-03-27 01:40:54 +0000 | [diff] [blame] | 83 | |
| 84 | ; Also test direct instruction uses. |
| 85 | define i32** @t1() { |
| 86 | ret i32** @bargotequiv |
| 87 | } |
Bruno Cardoso Lopes | edb876d | 2015-06-25 15:17:23 +0000 | [diff] [blame] | 88 | |
| 89 | ; Do not crash when a pattern cannot be matched as a GOT equivalent |
Bruno Cardoso Lopes | e864051 | 2015-07-21 21:45:42 +0000 | [diff] [blame] | 90 | define void @foo() { |
| 91 | ; X86-NOGOT-EQUIV-LABEL: _foo: |
| 92 | ; X86-NOGOT-EQUIV: leaq _b(%rip), %rax |
| 93 | store i8** @b, i8*** null |
| 94 | ret void |
| 95 | } |
Bruno Cardoso Lopes | edb876d | 2015-06-25 15:17:23 +0000 | [diff] [blame] | 96 | @a = external global i8 |
| 97 | @b = internal unnamed_addr constant i8* @a |
| 98 | |
Bruno Cardoso Lopes | e864051 | 2015-07-21 21:45:42 +0000 | [diff] [blame] | 99 | ; X86-NOGOT-EQUIV-LABEL: _c: |
| 100 | ; X86-NOGOT-EQUIV: .quad _b |
Bruno Cardoso Lopes | edb876d | 2015-06-25 15:17:23 +0000 | [diff] [blame] | 101 | @c = global i8** @b |