Skip to content

Commit 1a8c691

Browse files
committed
[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
1 parent 575a648 commit 1a8c691

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

llvm/lib/Target/X86/X86Subtarget.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,17 @@ X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
9595
case CodeModel::Large:
9696
return X86II::MO_GOTOFF;
9797

98-
// Medium is a hybrid: RIP-rel for code, GOTOFF for DSO local data.
98+
// Medium is a hybrid: RIP-rel for code and non-large data, GOTOFF for
99+
// remaining DSO local data.
99100
case CodeModel::Medium:
100101
// Constant pool and jump table handling pass a nullptr to this
101102
// function so we need to use isa_and_nonnull.
102103
if (isa_and_nonnull<Function>(GV))
103104
return X86II::MO_NO_FLAG; // All code is RIP-relative
105+
if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV)) {
106+
if (!TM.isLargeData(GVar))
107+
return X86II::MO_NO_FLAG;
108+
}
104109
return X86II::MO_GOTOFF; // Local symbols use GOTOFF.
105110
}
106111
llvm_unreachable("invalid code model");

llvm/test/CodeGen/X86/code-model-elf.ll

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --no_x86_scrub_sp
22
; Run with --no_x86_scrub_rip because we care a lot about how globals are
33
; accessed in the code model.
44

55
; RUN: llc -verify-machineinstrs < %s -relocation-model=static -code-model=small | FileCheck %s --check-prefix=CHECK --check-prefix=SMALL-STATIC
66
; RUN: llc -verify-machineinstrs < %s -relocation-model=static -code-model=medium | FileCheck %s --check-prefix=CHECK --check-prefix=MEDIUM-STATIC
77
; RUN: llc -verify-machineinstrs < %s -relocation-model=static -code-model=large | FileCheck %s --check-prefix=CHECK --check-prefix=LARGE-STATIC
88
; RUN: llc -verify-machineinstrs < %s -relocation-model=pic -code-model=small | FileCheck %s --check-prefix=CHECK --check-prefix=SMALL-PIC
9+
; 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
910
; RUN: llc -verify-machineinstrs < %s -relocation-model=pic -code-model=medium | FileCheck %s --check-prefix=CHECK --check-prefix=MEDIUM-PIC
1011
; RUN: llc -verify-machineinstrs < %s -relocation-model=pic -code-model=large | FileCheck %s --check-prefix=CHECK --check-prefix=LARGE-PIC
1112

13+
; FIXME: small pic and medium pic w/ big enough large data threshold should be equivalent
14+
1215
; Generated from this C source:
1316
;
1417
; static int static_data[10];
@@ -62,6 +65,11 @@ define dso_local ptr @lea_static_data() #0 {
6265
; SMALL-PIC-NEXT: leaq static_data(%rip), %rax
6366
; SMALL-PIC-NEXT: retq
6467
;
68+
; MEDIUM-SMALL-DATA-PIC-LABEL: lea_static_data:
69+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
70+
; MEDIUM-SMALL-DATA-PIC-NEXT: leaq static_data(%rip), %rax
71+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
72+
;
6573
; MEDIUM-PIC-LABEL: lea_static_data:
6674
; MEDIUM-PIC: # %bb.0:
6775
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rcx
@@ -102,6 +110,11 @@ define dso_local ptr @lea_global_data() #0 {
102110
; SMALL-PIC-NEXT: leaq global_data(%rip), %rax
103111
; SMALL-PIC-NEXT: retq
104112
;
113+
; MEDIUM-SMALL-DATA-PIC-LABEL: lea_global_data:
114+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
115+
; MEDIUM-SMALL-DATA-PIC-NEXT: leaq global_data(%rip), %rax
116+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
117+
;
105118
; MEDIUM-PIC-LABEL: lea_global_data:
106119
; MEDIUM-PIC: # %bb.0:
107120
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rcx
@@ -142,6 +155,11 @@ define dso_local ptr @lea_extern_data() #0 {
142155
; SMALL-PIC-NEXT: movq extern_data@GOTPCREL(%rip), %rax
143156
; SMALL-PIC-NEXT: retq
144157
;
158+
; MEDIUM-SMALL-DATA-PIC-LABEL: lea_extern_data:
159+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
160+
; MEDIUM-SMALL-DATA-PIC-NEXT: movq extern_data@GOTPCREL(%rip), %rax
161+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
162+
;
145163
; MEDIUM-PIC-LABEL: lea_extern_data:
146164
; MEDIUM-PIC: # %bb.0:
147165
; MEDIUM-PIC-NEXT: movq extern_data@GOTPCREL(%rip), %rax
@@ -180,6 +198,13 @@ define dso_local ptr @lea_unknown_size_data() #0 {
180198
; SMALL-PIC-NEXT: leaq unknown_size_data(%rip), %rax
181199
; SMALL-PIC-NEXT: retq
182200
;
201+
; MEDIUM-SMALL-DATA-PIC-LABEL: lea_unknown_size_data:
202+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
203+
; MEDIUM-SMALL-DATA-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rcx
204+
; MEDIUM-SMALL-DATA-PIC-NEXT: movabsq $unknown_size_data@GOTOFF, %rax
205+
; MEDIUM-SMALL-DATA-PIC-NEXT: addq %rcx, %rax
206+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
207+
;
183208
; MEDIUM-PIC-LABEL: lea_unknown_size_data:
184209
; MEDIUM-PIC: # %bb.0:
185210
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rcx
@@ -222,6 +247,12 @@ define dso_local i32 @load_global_data() #0 {
222247
; SMALL-PIC-NEXT: movl global_data+8(%rip), %eax
223248
; SMALL-PIC-NEXT: retq
224249
;
250+
; MEDIUM-SMALL-DATA-PIC-LABEL: load_global_data:
251+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
252+
; MEDIUM-SMALL-DATA-PIC-NEXT: leaq global_data(%rip), %rax
253+
; MEDIUM-SMALL-DATA-PIC-NEXT: movl 8(%rax), %eax
254+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
255+
;
225256
; MEDIUM-PIC-LABEL: load_global_data:
226257
; MEDIUM-PIC: # %bb.0:
227258
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rax
@@ -267,6 +298,12 @@ define dso_local i32 @load_extern_data() #0 {
267298
; SMALL-PIC-NEXT: movl 8(%rax), %eax
268299
; SMALL-PIC-NEXT: retq
269300
;
301+
; MEDIUM-SMALL-DATA-PIC-LABEL: load_extern_data:
302+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
303+
; MEDIUM-SMALL-DATA-PIC-NEXT: movq extern_data@GOTPCREL(%rip), %rax
304+
; MEDIUM-SMALL-DATA-PIC-NEXT: movl 8(%rax), %eax
305+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
306+
;
270307
; MEDIUM-PIC-LABEL: load_extern_data:
271308
; MEDIUM-PIC: # %bb.0:
272309
; MEDIUM-PIC-NEXT: movq extern_data@GOTPCREL(%rip), %rax
@@ -310,6 +347,13 @@ define dso_local i32 @load_unknown_size_data() #0 {
310347
; SMALL-PIC-NEXT: movl unknown_size_data+8(%rip), %eax
311348
; SMALL-PIC-NEXT: retq
312349
;
350+
; MEDIUM-SMALL-DATA-PIC-LABEL: load_unknown_size_data:
351+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
352+
; MEDIUM-SMALL-DATA-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rax
353+
; MEDIUM-SMALL-DATA-PIC-NEXT: movabsq $unknown_size_data@GOTOFF, %rcx
354+
; MEDIUM-SMALL-DATA-PIC-NEXT: movl 8(%rax,%rcx), %eax
355+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
356+
;
313357
; MEDIUM-PIC-LABEL: load_unknown_size_data:
314358
; MEDIUM-PIC: # %bb.0:
315359
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rax
@@ -367,6 +411,11 @@ define dso_local ptr @lea_static_fn() #0 {
367411
; SMALL-PIC-NEXT: leaq static_fn(%rip), %rax
368412
; SMALL-PIC-NEXT: retq
369413
;
414+
; MEDIUM-SMALL-DATA-PIC-LABEL: lea_static_fn:
415+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
416+
; MEDIUM-SMALL-DATA-PIC-NEXT: leaq static_fn(%rip), %rax
417+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
418+
;
370419
; MEDIUM-PIC-LABEL: lea_static_fn:
371420
; MEDIUM-PIC: # %bb.0:
372421
; MEDIUM-PIC-NEXT: leaq static_fn(%rip), %rax
@@ -405,6 +454,11 @@ define dso_local ptr @lea_global_fn() #0 {
405454
; SMALL-PIC-NEXT: leaq global_fn(%rip), %rax
406455
; SMALL-PIC-NEXT: retq
407456
;
457+
; MEDIUM-SMALL-DATA-PIC-LABEL: lea_global_fn:
458+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
459+
; MEDIUM-SMALL-DATA-PIC-NEXT: leaq global_fn(%rip), %rax
460+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
461+
;
408462
; MEDIUM-PIC-LABEL: lea_global_fn:
409463
; MEDIUM-PIC: # %bb.0:
410464
; MEDIUM-PIC-NEXT: leaq global_fn(%rip), %rax
@@ -443,6 +497,11 @@ define dso_local ptr @lea_extern_fn() #0 {
443497
; SMALL-PIC-NEXT: movq extern_fn@GOTPCREL(%rip), %rax
444498
; SMALL-PIC-NEXT: retq
445499
;
500+
; MEDIUM-SMALL-DATA-PIC-LABEL: lea_extern_fn:
501+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
502+
; MEDIUM-SMALL-DATA-PIC-NEXT: movq extern_fn@GOTPCREL(%rip), %rax
503+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
504+
;
446505
; MEDIUM-PIC-LABEL: lea_extern_fn:
447506
; MEDIUM-PIC: # %bb.0:
448507
; MEDIUM-PIC-NEXT: movq extern_fn@GOTPCREL(%rip), %rax
@@ -516,6 +575,13 @@ define dso_local float @load_constant_pool(float %x) #0 {
516575
; SMALL-PIC-NEXT: addss {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
517576
; SMALL-PIC-NEXT: retq
518577
;
578+
; MEDIUM-SMALL-DATA-PIC-LABEL: load_constant_pool:
579+
; MEDIUM-SMALL-DATA-PIC: # %bb.0:
580+
; MEDIUM-SMALL-DATA-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rax
581+
; MEDIUM-SMALL-DATA-PIC-NEXT: movabsq ${{\.?LCPI[0-9]+_[0-9]+}}@GOTOFF, %rcx
582+
; MEDIUM-SMALL-DATA-PIC-NEXT: addss (%rax,%rcx), %xmm0
583+
; MEDIUM-SMALL-DATA-PIC-NEXT: retq
584+
;
519585
; MEDIUM-PIC-LABEL: load_constant_pool:
520586
; MEDIUM-PIC: # %bb.0:
521587
; MEDIUM-PIC-NEXT: leaq _GLOBAL_OFFSET_TABLE_(%rip), %rax

0 commit comments

Comments
 (0)