[PATCH] D140593: [X86] Emit RIP-relative access to local function in PIC medium code model

Thomas Köppe via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 23 05:01:07 PST 2022


tkoeppe updated this revision to Diff 485094.
tkoeppe added a comment.

Added a comment in the code explaining the choice for "medium code model, function", and updated the tests.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140593/new/

https://reviews.llvm.org/D140593

Files:
  llvm/lib/Target/X86/X86ISelLowering.cpp
  llvm/test/CodeGen/X86/code-model-elf.ll


Index: llvm/test/CodeGen/X86/code-model-elf.ll
===================================================================
--- llvm/test/CodeGen/X86/code-model-elf.ll
+++ llvm/test/CodeGen/X86/code-model-elf.ll
@@ -270,7 +270,7 @@
 ;
 ; MEDIUM-STATIC-LABEL: lea_static_fn:
 ; MEDIUM-STATIC:       # %bb.0:
-; MEDIUM-STATIC-NEXT:    movabsq $static_fn, %rax
+; MEDIUM-STATIC-NEXT:    leaq static_fn(%rip), %rax
 ; MEDIUM-STATIC-NEXT:    retq
 ;
 ; LARGE-STATIC-LABEL: lea_static_fn:
@@ -285,7 +285,7 @@
 ;
 ; MEDIUM-PIC-LABEL: lea_static_fn:
 ; MEDIUM-PIC:       # %bb.0:
-; MEDIUM-PIC-NEXT:    movabsq $static_fn, %rax
+; MEDIUM-PIC-NEXT:    leaq static_fn(%rip), %rax
 ; MEDIUM-PIC-NEXT:    retq
 ;
 ; LARGE-PIC-LABEL: lea_static_fn:
@@ -308,7 +308,7 @@
 ;
 ; MEDIUM-STATIC-LABEL: lea_global_fn:
 ; MEDIUM-STATIC:       # %bb.0:
-; MEDIUM-STATIC-NEXT:    movabsq $global_fn, %rax
+; MEDIUM-STATIC-NEXT:    leaq global_fn(%rip), %rax
 ; MEDIUM-STATIC-NEXT:    retq
 ;
 ; LARGE-STATIC-LABEL: lea_global_fn:
@@ -323,7 +323,7 @@
 ;
 ; MEDIUM-PIC-LABEL: lea_global_fn:
 ; MEDIUM-PIC:       # %bb.0:
-; MEDIUM-PIC-NEXT:    movabsq $global_fn, %rax
+; MEDIUM-PIC-NEXT:    leaq global_fn(%rip), %rax
 ; MEDIUM-PIC-NEXT:    retq
 ;
 ; LARGE-PIC-LABEL: lea_global_fn:
Index: llvm/lib/Target/X86/X86ISelLowering.cpp
===================================================================
--- llvm/lib/Target/X86/X86ISelLowering.cpp
+++ llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -20489,6 +20489,14 @@
     return X86ISD::Wrapper;
 
   CodeModel::Model M = getTargetMachine().getCodeModel();
+
+  // In the medium model, functions can always be referenced
+  // RIP-relatively, since they must be within 2GiB. This is
+  // also possible in non-PIC mode, and shorter than the 64-bit
+  // absolute immediate that would otherwise be emitted.
+  if (M == CodeModel::Medium && isa_and_nonnull<Function>(GV))
+    return X86ISD::WrapperRIP;
+
   if (Subtarget.isPICStyleRIPRel() &&
       (M == CodeModel::Small || M == CodeModel::Kernel))
     return X86ISD::WrapperRIP;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140593.485094.patch
Type: text/x-patch
Size: 2045 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221223/e21abd5c/attachment.bin>


More information about the llvm-commits mailing list