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

Asaf Badouh via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 17 02:08:22 PDT 2016


AsafBadouh added a subscriber: AsafBadouh.

================
Comment at: lib/Target/X86/X86Subtarget.cpp:91
@@ +90,3 @@
+      bool isPIEDefinition =
+          GV->hasExactDefinition() && TM.Options.PositionIndependentExecutable;
+
----------------
rnk wrote:
> You don't want `hasExactDefinition`, it will be false for linkonce_odr and weak_odr globals, which can benefit from this more efficient access. I think you want something equivalent to this condition:
>   TM.Options.PositionIndependentExecutable && !GV->isDeclaration() && !GV->mayBeOverridden()
I agree with RNK, 
I'm not sure if it will handle weak attribute and reference to function.
for example:

```
long  bar_add; 
int __attribute__((weak)) weak_x = 3; 
int ext_bar(void);  // extern function

int bar()
{
  return (long)&ext_bar; // reading address of extern function should use GOT
}

long foo()
{
  bar_add = (long)&bar;       // reading address of bar should be directly (not by GOT)
  return bar_add
          + weak_x;
}
```

bar() should be compute directly, (not loaded from GOT like extern ext_bar()
so it should generate code like:

```
bar():
[...]
        mov     rax, QWORD PTR ext_bar()@GOTPCREL[rip]
[...]
        ret
foo():
[...]
        lea     rax, bar()[rip]
        mov     QWORD PTR bar_add[rip], rax
        mov     eax, DWORD PTR weak_x[rip]
[...]
        ret
```

I think the condition should be something similar to this :

```
TM.Options.PositionIndependentExecutable && !(GV->isDeclarationForLinker() && isa<Function>(GV))

```




http://reviews.llvm.org/D19040





More information about the llvm-commits mailing list