[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.
This commit is contained in:
Arthur Eubanks 2023-12-15 13:01:55 -08:00
parent aad5c2f887
commit 68c976bf64
3 changed files with 74 additions and 7 deletions

View File

@ -69,11 +69,11 @@ X86Subtarget::classifyGlobalReference(const GlobalValue *GV) const {
unsigned char
X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
CodeModel::Model CM = TM.getCodeModel();
// Tagged globals have non-zero upper bits, which makes direct references
// require a 64-bit immediate. On the small code model this causes relocation
// errors, so we go through the GOT instead.
if (AllowTaggedGlobals && TM.getCodeModel() == CodeModel::Small && GV &&
!isa<Function>(GV))
// require a 64-bit immediate. With the small/medium code models this causes
// relocation errors, so we go through the GOT instead.
if (AllowTaggedGlobals && CM != CodeModel::Large && GV && !isa<Function>(GV))
return X86II::MO_GOTPCREL_NORELAX;
// If we're not PIC, it's not very interesting.
@ -83,7 +83,6 @@ X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
if (is64Bit()) {
// 64-bit ELF PIC local references may use GOTOFF relocations.
if (isTargetELF()) {
CodeModel::Model CM = TM.getCodeModel();
assert(CM != CodeModel::Tiny &&
"Tiny codesize model not supported on X86");
// In the large code model, all text is far from any global data, so we

View File

@ -1,5 +1,7 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
; RUN: llc --relocation-model=pic < %s | FileCheck %s
; RUN: llc --relocation-model=pic -code-model=small < %s | FileCheck %s
; RUN: llc --relocation-model=pic -code-model=medium < %s | FileCheck %s
; RUN: llc --relocation-model=pic -code-model=large < %s | FileCheck %s --check-prefix=LARGE
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@ -12,6 +14,16 @@ define ptr @global_addr() #0 {
; CHECK: # %bb.0:
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
; CHECK-NEXT: retq
;
; LARGE-LABEL: global_addr:
; LARGE: # %bb.0:
; LARGE-NEXT: .L0$pb:
; LARGE-NEXT: leaq .L0$pb(%rip), %rax
; LARGE-NEXT: movabsq $_GLOBAL_OFFSET_TABLE_-.L0$pb, %rcx
; LARGE-NEXT: addq %rax, %rcx
; LARGE-NEXT: movabsq $global@GOT, %rax
; LARGE-NEXT: movq (%rcx,%rax), %rax
; LARGE-NEXT: retq
ret ptr @global
}
@ -21,6 +33,17 @@ define i32 @global_load() #0 {
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
; CHECK-NEXT: movl (%rax), %eax
; CHECK-NEXT: retq
;
; LARGE-LABEL: global_load:
; LARGE: # %bb.0:
; LARGE-NEXT: .L1$pb:
; LARGE-NEXT: leaq .L1$pb(%rip), %rax
; LARGE-NEXT: movabsq $_GLOBAL_OFFSET_TABLE_-.L1$pb, %rcx
; LARGE-NEXT: addq %rax, %rcx
; LARGE-NEXT: movabsq $global@GOT, %rax
; LARGE-NEXT: movq (%rcx,%rax), %rax
; LARGE-NEXT: movl (%rax), %eax
; LARGE-NEXT: retq
%load = load i32, ptr @global
ret i32 %load
}
@ -31,6 +54,17 @@ define void @global_store() #0 {
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
; CHECK-NEXT: movl $0, (%rax)
; CHECK-NEXT: retq
;
; LARGE-LABEL: global_store:
; LARGE: # %bb.0:
; LARGE-NEXT: .L2$pb:
; LARGE-NEXT: leaq .L2$pb(%rip), %rax
; LARGE-NEXT: movabsq $_GLOBAL_OFFSET_TABLE_-.L2$pb, %rcx
; LARGE-NEXT: addq %rax, %rcx
; LARGE-NEXT: movabsq $global@GOT, %rax
; LARGE-NEXT: movq (%rcx,%rax), %rax
; LARGE-NEXT: movl $0, (%rax)
; LARGE-NEXT: retq
store i32 0, ptr @global
ret void
}
@ -40,6 +74,16 @@ define ptr @func_addr() #0 {
; CHECK: # %bb.0:
; CHECK-NEXT: movq func@GOTPCREL(%rip), %rax
; CHECK-NEXT: retq
;
; LARGE-LABEL: func_addr:
; LARGE: # %bb.0:
; LARGE-NEXT: .L3$pb:
; LARGE-NEXT: leaq .L3$pb(%rip), %rax
; LARGE-NEXT: movabsq $_GLOBAL_OFFSET_TABLE_-.L3$pb, %rcx
; LARGE-NEXT: addq %rax, %rcx
; LARGE-NEXT: movabsq $func@GOT, %rax
; LARGE-NEXT: movq (%rcx,%rax), %rax
; LARGE-NEXT: retq
ret ptr @func
}

View File

@ -1,5 +1,7 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
; RUN: llc --relocation-model=static < %s | FileCheck %s
; RUN: llc --relocation-model=static -code-model=small < %s | FileCheck %s
; RUN: llc --relocation-model=static -code-model=medium < %s | FileCheck %s
; RUN: llc --relocation-model=static -code-model=large < %s | FileCheck %s --check-prefix=LARGE
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@ -12,6 +14,11 @@ define ptr @global_addr() #0 {
; CHECK: # %bb.0:
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
; CHECK-NEXT: retq
;
; LARGE-LABEL: global_addr:
; LARGE: # %bb.0:
; LARGE-NEXT: movabsq $global, %rax
; LARGE-NEXT: retq
ret ptr @global
}
@ -21,6 +28,12 @@ define i32 @global_load() #0 {
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
; CHECK-NEXT: movl (%rax), %eax
; CHECK-NEXT: retq
;
; LARGE-LABEL: global_load:
; LARGE: # %bb.0:
; LARGE-NEXT: movabsq $global, %rax
; LARGE-NEXT: movl (%rax), %eax
; LARGE-NEXT: retq
%load = load i32, ptr @global
ret i32 %load
}
@ -31,6 +44,12 @@ define void @global_store() #0 {
; CHECK-NEXT: movq global@GOTPCREL_NORELAX(%rip), %rax
; CHECK-NEXT: movl $0, (%rax)
; CHECK-NEXT: retq
;
; LARGE-LABEL: global_store:
; LARGE: # %bb.0:
; LARGE-NEXT: movabsq $global, %rax
; LARGE-NEXT: movl $0, (%rax)
; LARGE-NEXT: retq
store i32 0, ptr @global
ret void
}
@ -40,6 +59,11 @@ define ptr @func_addr() #0 {
; CHECK: # %bb.0:
; CHECK-NEXT: movl $func, %eax
; CHECK-NEXT: retq
;
; LARGE-LABEL: func_addr:
; LARGE: # %bb.0:
; LARGE-NEXT: movabsq $func, %rax
; LARGE-NEXT: retq
ret ptr @func
}