[PATCH] D19040: Remove unnecessary load via GOT when accessing globals with PIE in x86_64

Sriraman Tallam via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 12 16:10:45 PDT 2016


tmsriram created this revision.
tmsriram added reviewers: rnk, davidxl.
tmsriram added a subscriber: llvm-commits.

LLVM generates the following code with PIE for this example:

hello.cpp

int a = 0;

int main() {

return a;
}

$ clang -O2 -fPIE hello.cc -S
$ cat hello.s

main: # @main
movq a at GOTPCREL(%rip), %rax
movl	 (%rax), %eax
retq

Creating a GOT entry for global 'a' and storing its address there is unnecessary as 'a' is defined in hello.cpp which will be linked into a position independent executable (fPIE). Hence, the definition of 'a' cannot be overridden and we can remove a load. The efficient access is this:

main: # @main
movl	a(%rip), %eax
retq

This patch fixes the problem.

http://reviews.llvm.org/D19040

Files:
  lib/Target/X86/X86Subtarget.cpp
  test/CodeGen/X86/emutls-pie.ll

Index: test/CodeGen/X86/emutls-pie.ll
===================================================================
--- test/CodeGen/X86/emutls-pie.ll
+++ test/CodeGen/X86/emutls-pie.ll
@@ -47,7 +47,7 @@
 ; X32-NEXT: popl %ebx
 ; X32-NEXT: retl
 ; X64-LABEL: f1:
-; X64:      movq __emutls_v.i at GOTPCREL(%rip), %rdi
+; X64:      leaq __emutls_v.i(%rip), %rdi
 ; X64-NEXT: callq __emutls_get_address at PLT
 ; X64-NEXT: movl (%rax), %eax
 ; X64-NEXT: popq %rcx
@@ -64,7 +64,7 @@
 ; X32-NEXT: movl %eax, (%esp)
 ; X32-NEXT: calll __emutls_get_address at PLT
 ; X64-LABEL: f2:
-; X64:      movq __emutls_v.i at GOTPCREL(%rip), %rdi
+; X64:      leaq __emutls_v.i(%rip), %rdi
 ; X64-NEXT: callq __emutls_get_address at PLT
 
 entry:
Index: lib/Target/X86/X86Subtarget.cpp
===================================================================
--- lib/Target/X86/X86Subtarget.cpp
+++ lib/Target/X86/X86Subtarget.cpp
@@ -83,8 +83,15 @@
     } else if (!isTargetWin64()) {
       assert(isTargetELF() && "Unknown rip-relative target");
 
-      // Extra load is needed for all externally visible globals.
-      if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
+      // Extra load is needed for all externally visible globals except with
+      // PIE as the definition of the global in an executable is not
+      // overridden.
+
+      bool isPIEDefinition =
+          GV->hasExactDefinition() && TM.Options.PositionIndependentExecutable;
+
+      if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility() &&
+          !isPIEDefinition)
         return X86II::MO_GOTPCREL;
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19040.53485.patch
Type: text/x-patch
Size: 1563 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160412/9ff1d296/attachment.bin>


More information about the llvm-commits mailing list