[llvm] 1a8c691 - [X86] Use RIP-relative addressing for data under large data threshold for medium code model
Arthur Eubanks via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 19 11:19:45 PDT 2023
Author: Arthur Eubanks
Date: 2023-09-19T11:14:45-07:00
New Revision: 1a8c69176e24c57d6d5860c3cd7046f7aef8db6f
URL: https://github.com/llvm/llvm-project/commit/1a8c69176e24c57d6d5860c3cd7046f7aef8db6f
DIFF: https://github.com/llvm/llvm-project/commit/1a8c69176e24c57d6d5860c3cd7046f7aef8db6f.diff
LOG: [X86] Use RIP-relative addressing for data under large data threshold for medium code model
Since those data are assumed to be within the relocation offset limit.
Reviewed By: rnk
Differential Revision: https://reviews.llvm.org/D150297
Added:
Modified:
llvm/lib/Target/X86/X86Subtarget.cpp
llvm/test/CodeGen/X86/code-model-elf.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/X86/X86Subtarget.cpp b/llvm/lib/Target/X86/X86Subtarget.cpp
index e16e39b2a7666e6..0daa2ab5dc942fa 100644
--- a/llvm/lib/Target/X86/X86Subtarget.cpp
+++ b/llvm/lib/Target/X86/X86Subtarget.cpp
@@ -95,12 +95,17 @@ X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
case CodeModel::Large:
return X86II::MO_GOTOFF;
- // Medium is a hybrid: RIP-rel for code, GOTOFF for DSO local data.
+ // Medium is a hybrid: RIP-rel for code and non-large data, GOTOFF for
+ // remaining DSO local data.
case CodeModel::Medium:
// Constant pool and jump table handling pass a nullptr to this
// function so we need to use isa_and_nonnull.
if (isa_and_nonnull<Function>(GV))
return X86II::MO_NO_FLAG; // All code is RIP-relative
+ if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV)) {
+ if (!TM.isLargeData(GVar))
+ return X86II::MO_NO_FLAG;
+ }
return X86II::MO_GOTOFF; // Local symbols use GOTOFF.
}
llvm_unreachable("invalid code model");
diff --git a/llvm/test/CodeGen/X86/code-model-elf.ll b/llvm/test/CodeGen/X86/code-model-elf.ll
index aef1e0e117ee7c2..4bb6d04592b1f64 100644
--- a/llvm/test/CodeGen/X86/code-model-elf.ll
+++ b/llvm/test/CodeGen/X86/code-model-elf.ll
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --no_x86_scrub_sp
; Run with --no_x86_scrub_rip because we care a lot about how globals are
; accessed in the code model.
@@ -6,9 +6,12 @@
; RUN: llc -verify-machineinstrs < %s -relocation-model=static -code-model=medium | FileCheck %s --check-prefix=CHECK --check-prefix=MEDIUM-STATIC
; RUN: llc -verify-machineinstrs < %s -relocation-model=static -code-model=large | FileCheck %s --check-prefix=CHECK --check-prefix=LARGE-STATIC
; RUN: llc -verify-machineinstrs < %s -relocation-model=pic -code-model=small | FileCheck %s --check-prefix=CHECK --check-prefix=SMALL-PIC
+; RUN: llc -verify-machineinstrs < %s -relocation-model=pic -code-model=medium -large-data-threshold=1000 | FileCheck %s --check-prefix=CHECK --check-prefix=MEDIUM-SMALL-DATA-PIC
; RUN: llc -verify-machineinstrs < %s -relocation-model=pic -code-model=medium | FileCheck %s --check-prefix=CHECK --check-prefix=MEDIUM-PIC
; RUN: llc -verify-machineinstrs < %s -relocation-model=pic -code-model=large | FileCheck %s --check-prefix=CHECK --check-prefix=LARGE-PIC
+; FIXME: small pic and medium pic w/ big enough large data threshold should be equivalent
+
; Generated from this C source:
;
; static int static_data[10];
@@ -62,6 +65,11 @@ define dso_local ptr @lea_static_data() #0 {
; SMALL-PIC-NEXT: leaq static_data(%rip), %rax
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: lea_static_data:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: leaq static_data(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: lea_static_data:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rcx
@@ -102,6 +110,11 @@ define dso_local ptr @lea_global_data() #0 {
; SMALL-PIC-NEXT: leaq global_data(%rip), %rax
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: lea_global_data:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: leaq global_data(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: lea_global_data:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rcx
@@ -142,6 +155,11 @@ define dso_local ptr @lea_extern_data() #0 {
; SMALL-PIC-NEXT: movq extern_data at GOTPCREL(%rip), %rax
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: lea_extern_data:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: movq extern_data at GOTPCREL(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: lea_extern_data:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: movq extern_data at GOTPCREL(%rip), %rax
@@ -180,6 +198,13 @@ define dso_local ptr @lea_unknown_size_data() #0 {
; SMALL-PIC-NEXT: leaq unknown_size_data(%rip), %rax
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: lea_unknown_size_data:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rcx
+; MEDIUM-SMALL-DATA-PIC-NEXT: movabsq $unknown_size_data at GOTOFF, %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: addq %rcx, %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: lea_unknown_size_data:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rcx
@@ -222,6 +247,12 @@ define dso_local i32 @load_global_data() #0 {
; SMALL-PIC-NEXT: movl global_data+8(%rip), %eax
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: load_global_data:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: leaq global_data(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: movl 8(%rax), %eax
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: load_global_data:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rax
@@ -267,6 +298,12 @@ define dso_local i32 @load_extern_data() #0 {
; SMALL-PIC-NEXT: movl 8(%rax), %eax
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: load_extern_data:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: movq extern_data at GOTPCREL(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: movl 8(%rax), %eax
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: load_extern_data:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: movq extern_data at GOTPCREL(%rip), %rax
@@ -310,6 +347,13 @@ define dso_local i32 @load_unknown_size_data() #0 {
; SMALL-PIC-NEXT: movl unknown_size_data+8(%rip), %eax
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: load_unknown_size_data:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: movabsq $unknown_size_data at GOTOFF, %rcx
+; MEDIUM-SMALL-DATA-PIC-NEXT: movl 8(%rax,%rcx), %eax
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: load_unknown_size_data:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rax
@@ -367,6 +411,11 @@ define dso_local ptr @lea_static_fn() #0 {
; SMALL-PIC-NEXT: leaq static_fn(%rip), %rax
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: lea_static_fn:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: leaq static_fn(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: lea_static_fn:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: leaq static_fn(%rip), %rax
@@ -405,6 +454,11 @@ define dso_local ptr @lea_global_fn() #0 {
; SMALL-PIC-NEXT: leaq global_fn(%rip), %rax
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: lea_global_fn:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: leaq global_fn(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: lea_global_fn:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: leaq global_fn(%rip), %rax
@@ -443,6 +497,11 @@ define dso_local ptr @lea_extern_fn() #0 {
; SMALL-PIC-NEXT: movq extern_fn at GOTPCREL(%rip), %rax
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: lea_extern_fn:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: movq extern_fn at GOTPCREL(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: lea_extern_fn:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: movq extern_fn at GOTPCREL(%rip), %rax
@@ -516,6 +575,13 @@ define dso_local float @load_constant_pool(float %x) #0 {
; SMALL-PIC-NEXT: addss {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; SMALL-PIC-NEXT: retq
;
+; MEDIUM-SMALL-DATA-PIC-LABEL: load_constant_pool:
+; MEDIUM-SMALL-DATA-PIC: # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT: movabsq ${{\.?LCPI[0-9]+_[0-9]+}}@GOTOFF, %rcx
+; MEDIUM-SMALL-DATA-PIC-NEXT: addss (%rax,%rcx), %xmm0
+; MEDIUM-SMALL-DATA-PIC-NEXT: retq
+;
; MEDIUM-PIC-LABEL: load_constant_pool:
; MEDIUM-PIC: # %bb.0:
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rax
More information about the llvm-commits
mailing list