From 7cdb10f1aabfe57b3e1dcb1a8e5a064b1c7343dc Mon Sep 17 00:00:00 2001 From: Sriraman Tallam Date: Fri, 3 Nov 2017 00:10:19 +0000 Subject: [PATCH] Avoid PLT for external calls when attribute nonlazybind is used. Differential Revision: https://reviews.llvm.org/D39065 llvm-svn: 317292 --- llvm/lib/Target/X86/X86Subtarget.cpp | 11 +++++++++-- llvm/test/CodeGen/X86/no-plt.ll | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 llvm/test/CodeGen/X86/no-plt.ll diff --git a/llvm/lib/Target/X86/X86Subtarget.cpp b/llvm/lib/Target/X86/X86Subtarget.cpp index b0ce133..9e060f9 100644 --- a/llvm/lib/Target/X86/X86Subtarget.cpp +++ b/llvm/lib/Target/X86/X86Subtarget.cpp @@ -144,6 +144,15 @@ X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV) const { unsigned char X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV, const Module &M) const { + const Function *F = dyn_cast_or_null(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::classifyGlobalFunctionReference(const GlobalValue *GV, return X86II::MO_DLLIMPORT; } - const Function *F = dyn_cast_or_null(GV); - if (isTargetELF()) { if (is64Bit() && F && (CallingConv::X86_RegCall == F->getCallingConv())) // According to psABI, PLT stub clobbers XMM8-XMM15. diff --git a/llvm/test/CodeGen/X86/no-plt.ll b/llvm/test/CodeGen/X86/no-plt.ll new file mode 100644 index 0000000..77ef686 --- /dev/null +++ b/llvm/test/CodeGen/X86/no-plt.ll @@ -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@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 } -- 2.7.4