Skip to content

Commit 68c976b

Browse files
committed
[X86] Fix referencing local tagged globals
We should treat the medium code model like the small code model. Classifying non-local references already properly handled this.
1 parent aad5c2f commit 68c976b

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

llvm/lib/Target/X86/X86Subtarget.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ X86Subtarget::classifyGlobalReference(const GlobalValue *GV) const {
6969

7070
unsigned char
7171
X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
72+
CodeModel::Model CM = TM.getCodeModel();
7273
// Tagged globals have non-zero upper bits, which makes direct references
73-
// require a 64-bit immediate. On the small code model this causes relocation
74-
// errors, so we go through the GOT instead.
75-
if (AllowTaggedGlobals && TM.getCodeModel() == CodeModel::Small && GV &&
76-
!isa<Function>(GV))
74+
// require a 64-bit immediate. With the small/medium code models this causes
75+
// relocation errors, so we go through the GOT instead.
76+
if (AllowTaggedGlobals && CM != CodeModel::Large && GV && !isa<Function>(GV))
7777
return X86II::MO_GOTPCREL_NORELAX;
7878

7979
// If we're not PIC, it's not very interesting.
@@ -83,7 +83,6 @@ X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
8383
if (is64Bit()) {
8484
// 64-bit ELF PIC local references may use GOTOFF relocations.
8585
if (isTargetELF()) {
86-
CodeModel::Model CM = TM.getCodeModel();
8786
assert(CM != CodeModel::Tiny &&
8887
"Tiny codesize model not supported on X86");
8988
// In the large code model, all text is far from any global data, so we

llvm/test/CodeGen/X86/tagged-globals-pic.ll

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2-
; RUN: llc --relocation-model=pic < %s | FileCheck %s
2+
; RUN: llc --relocation-model=pic -code-model=small < %s | FileCheck %s
3+
; RUN: llc --relocation-model=pic -code-model=medium < %s | FileCheck %s
4+
; RUN: llc --relocation-model=pic -code-model=large < %s | FileCheck %s --check-prefix=LARGE
35

46
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
57
target triple = "x86_64-unknown-linux-gnu"
@@ -12,6 +14,16 @@ define ptr @global_addr() #0 {
1214
; CHECK: # %bb.0:
1315
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
1416
; CHECK-NEXT: retq
17+
;
18+
; LARGE-LABEL: global_addr:
19+
; LARGE: # %bb.0:
20+
; LARGE-NEXT: .L0$pb:
21+
; LARGE-NEXT: leaq .L0$pb(%rip), %rax
22+
; LARGE-NEXT: movabsq $_GLOBAL_OFFSET_TABLE_-.L0$pb, %rcx
23+
; LARGE-NEXT: addq %rax, %rcx
24+
; LARGE-NEXT: movabsq $global@GOT, %rax
25+
; LARGE-NEXT: movq (%rcx,%rax), %rax
26+
; LARGE-NEXT: retq
1527
ret ptr @global
1628
}
1729

@@ -21,6 +33,17 @@ define i32 @global_load() #0 {
2133
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
2234
; CHECK-NEXT: movl (%rax), %eax
2335
; CHECK-NEXT: retq
36+
;
37+
; LARGE-LABEL: global_load:
38+
; LARGE: # %bb.0:
39+
; LARGE-NEXT: .L1$pb:
40+
; LARGE-NEXT: leaq .L1$pb(%rip), %rax
41+
; LARGE-NEXT: movabsq $_GLOBAL_OFFSET_TABLE_-.L1$pb, %rcx
42+
; LARGE-NEXT: addq %rax, %rcx
43+
; LARGE-NEXT: movabsq $global@GOT, %rax
44+
; LARGE-NEXT: movq (%rcx,%rax), %rax
45+
; LARGE-NEXT: movl (%rax), %eax
46+
; LARGE-NEXT: retq
2447
%load = load i32, ptr @global
2548
ret i32 %load
2649
}
@@ -31,6 +54,17 @@ define void @global_store() #0 {
3154
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
3255
; CHECK-NEXT: movl $0, (%rax)
3356
; CHECK-NEXT: retq
57+
;
58+
; LARGE-LABEL: global_store:
59+
; LARGE: # %bb.0:
60+
; LARGE-NEXT: .L2$pb:
61+
; LARGE-NEXT: leaq .L2$pb(%rip), %rax
62+
; LARGE-NEXT: movabsq $_GLOBAL_OFFSET_TABLE_-.L2$pb, %rcx
63+
; LARGE-NEXT: addq %rax, %rcx
64+
; LARGE-NEXT: movabsq $global@GOT, %rax
65+
; LARGE-NEXT: movq (%rcx,%rax), %rax
66+
; LARGE-NEXT: movl $0, (%rax)
67+
; LARGE-NEXT: retq
3468
store i32 0, ptr @global
3569
ret void
3670
}
@@ -40,6 +74,16 @@ define ptr @func_addr() #0 {
4074
; CHECK: # %bb.0:
4175
; CHECK-NEXT: movq func@GOTPCREL(%rip), %rax
4276
; CHECK-NEXT: retq
77+
;
78+
; LARGE-LABEL: func_addr:
79+
; LARGE: # %bb.0:
80+
; LARGE-NEXT: .L3$pb:
81+
; LARGE-NEXT: leaq .L3$pb(%rip), %rax
82+
; LARGE-NEXT: movabsq $_GLOBAL_OFFSET_TABLE_-.L3$pb, %rcx
83+
; LARGE-NEXT: addq %rax, %rcx
84+
; LARGE-NEXT: movabsq $func@GOT, %rax
85+
; LARGE-NEXT: movq (%rcx,%rax), %rax
86+
; LARGE-NEXT: retq
4387
ret ptr @func
4488
}
4589

llvm/test/CodeGen/X86/tagged-globals-static.ll

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2-
; RUN: llc --relocation-model=static < %s | FileCheck %s
2+
; RUN: llc --relocation-model=static -code-model=small < %s | FileCheck %s
3+
; RUN: llc --relocation-model=static -code-model=medium < %s | FileCheck %s
4+
; RUN: llc --relocation-model=static -code-model=large < %s | FileCheck %s --check-prefix=LARGE
35

46
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
57
target triple = "x86_64-unknown-linux-gnu"
@@ -12,6 +14,11 @@ define ptr @global_addr() #0 {
1214
; CHECK: # %bb.0:
1315
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
1416
; CHECK-NEXT: retq
17+
;
18+
; LARGE-LABEL: global_addr:
19+
; LARGE: # %bb.0:
20+
; LARGE-NEXT: movabsq $global, %rax
21+
; LARGE-NEXT: retq
1522
ret ptr @global
1623
}
1724

@@ -21,6 +28,12 @@ define i32 @global_load() #0 {
2128
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
2229
; CHECK-NEXT: movl (%rax), %eax
2330
; CHECK-NEXT: retq
31+
;
32+
; LARGE-LABEL: global_load:
33+
; LARGE: # %bb.0:
34+
; LARGE-NEXT: movabsq $global, %rax
35+
; LARGE-NEXT: movl (%rax), %eax
36+
; LARGE-NEXT: retq
2437
%load = load i32, ptr @global
2538
ret i32 %load
2639
}
@@ -31,6 +44,12 @@ define void @global_store() #0 {
3144
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
3245
; CHECK-NEXT: movl $0, (%rax)
3346
; CHECK-NEXT: retq
47+
;
48+
; LARGE-LABEL: global_store:
49+
; LARGE: # %bb.0:
50+
; LARGE-NEXT: movabsq $global, %rax
51+
; LARGE-NEXT: movl $0, (%rax)
52+
; LARGE-NEXT: retq
3453
store i32 0, ptr @global
3554
ret void
3655
}
@@ -40,6 +59,11 @@ define ptr @func_addr() #0 {
4059
; CHECK: # %bb.0:
4160
; CHECK-NEXT: movl $func, %eax
4261
; CHECK-NEXT: retq
62+
;
63+
; LARGE-LABEL: func_addr:
64+
; LARGE: # %bb.0:
65+
; LARGE-NEXT: movabsq $func, %rax
66+
; LARGE-NEXT: retq
4367
ret ptr @func
4468
}
4569

0 commit comments

Comments
 (0)