PR #24964
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@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.