[llvm] r317292 - Avoid PLT for external calls when attribute nonlazybind is used.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 2 17:43:18 PDT 2017


Why force a call via the got when a direct one could be used?

With -fno-plt -fPIC gcc compiles

__attribute__((visibility("hidden"))) void foo(void);
void bar(void);
void zed(void) {
  foo();
  bar();
}

to

        call    foo
        call    *bar at GOTPCREL(%rip)

But this patch will use the got for both calls, no?

I think the idea of -fno-plt is to use a got when a plt would have been
used. If a plt was not going to be used, the option has no effect.

Cheers,
Rafael

Sriraman Tallam via llvm-commits <llvm-commits at lists.llvm.org> writes:

> Author: tmsriram
> Date: Thu Nov  2 17:10:19 2017
> New Revision: 317292
>
> URL: http://llvm.org/viewvc/llvm-project?rev=317292&view=rev
> Log:
> Avoid PLT for external calls when attribute nonlazybind is used.
>
> Differential Revision: https://reviews.llvm.org/D39065
>
> Added:
>     llvm/trunk/test/CodeGen/X86/no-plt.ll
> Modified:
>     llvm/trunk/lib/Target/X86/X86Subtarget.cpp
>
> Modified: llvm/trunk/lib/Target/X86/X86Subtarget.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86Subtarget.cpp?rev=317292&r1=317291&r2=317292&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/X86/X86Subtarget.cpp (original)
> +++ llvm/trunk/lib/Target/X86/X86Subtarget.cpp Thu Nov  2 17:10:19 2017
> @@ -144,6 +144,15 @@ X86Subtarget::classifyGlobalFunctionRefe
>  unsigned char
>  X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV,
>                                                const Module &M) const {
> +  const Function *F = dyn_cast_or_null<Function>(GV);
> +
> +  // Do not use the PLT when explicitly told to do so for ELF 64-bit
> +  // target.
> +  if (isTargetELF() && is64Bit() && F &&
> +      F->hasFnAttribute(Attribute::NonLazyBind) &&
> +      GV->isDeclarationForLinker())
> +    return X86II::MO_GOTPCREL;
> +
>    if (TM.shouldAssumeDSOLocal(M, GV))
>      return X86II::MO_NO_FLAG;
>  
> @@ -153,8 +162,6 @@ X86Subtarget::classifyGlobalFunctionRefe
>      return X86II::MO_DLLIMPORT;
>    }
>  
> -  const Function *F = dyn_cast_or_null<Function>(GV);
> -
>    if (isTargetELF()) {
>      if (is64Bit() && F && (CallingConv::X86_RegCall == F->getCallingConv()))
>        // According to psABI, PLT stub clobbers XMM8-XMM15.
>
> Added: llvm/trunk/test/CodeGen/X86/no-plt.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/no-plt.ll?rev=317292&view=auto
> ==============================================================================
> --- llvm/trunk/test/CodeGen/X86/no-plt.ll (added)
> +++ llvm/trunk/test/CodeGen/X86/no-plt.ll Thu Nov  2 17:10:19 2017
> @@ -0,0 +1,23 @@
> +; RUN: llc < %s -mcpu=generic -mtriple=x86_64-linux-gnu -relocation-model=pic \
> +; RUN:   | FileCheck -check-prefix=X64 %s
> +; RUN: llc < %s -mcpu=generic -mtriple=x86_64-linux-gnu \
> +; RUN:   | FileCheck -check-prefix=X64 %s
> +
> +define i32 @main() #0 {
> +; X64: callq *_Z3foov at GOTPCREL(%rip)
> +; X64: callq _Z3barv
> +
> +entry:
> +  %retval = alloca i32, align 4
> +  store i32 0, i32* %retval, align 4
> +  %call1 = call i32 @_Z3foov()
> +  %call2 = call i32 @_Z3barv()
> +  ret i32 0
> +}
> +
> +; Function Attrs: nonlazybind
> +declare i32 @_Z3foov() #1
> +
> +declare i32 @_Z3barv() #2
> +
> +attributes #1 = { nonlazybind }
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits


More information about the llvm-commits mailing list