[llvm] r266863 - [X86] enable PIE for functions

Asaf Badouh via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 20 01:32:57 PDT 2016


Author: abadouh
Date: Wed Apr 20 03:32:57 2016
New Revision: 266863

URL: http://llvm.org/viewvc/llvm-project?rev=266863&view=rev
Log:
[X86] enable PIE for functions

Call locally defined function directly for PIE/fPIE

Differential Revision: http://reviews.llvm.org/D19226

Added:
    llvm/trunk/test/CodeGen/X86/pie.ll
Modified:
    llvm/trunk/lib/Target/X86/X86FastISel.cpp
    llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
    llvm/trunk/lib/Target/X86/X86Subtarget.cpp
    llvm/trunk/lib/Target/X86/X86Subtarget.h

Modified: llvm/trunk/lib/Target/X86/X86FastISel.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86FastISel.cpp?rev=266863&r1=266862&r2=266863&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86FastISel.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86FastISel.cpp Wed Apr 20 03:32:57 2016
@@ -3157,25 +3157,10 @@ bool X86FastISel::fastLowerCall(CallLowe
     unsigned CallOpc = Is64Bit ? X86::CALL64pcrel32 : X86::CALLpcrel32;
 
     // See if we need any target-specific flags on the GV operand.
-    unsigned char OpFlags = 0;
-
-    // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
-    // external symbols most go through the PLT in PIC mode.  If the symbol
-    // has hidden or protected visibility, or if it is static or local, then
-    // we don't need to use the PLT - we can directly call it.
-    if (Subtarget->isTargetELF() &&
-        TM.getRelocationModel() == Reloc::PIC_ &&
-        GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
-      OpFlags = X86II::MO_PLT;
-    } else if (Subtarget->isPICStyleStubAny() &&
-               !GV->isStrongDefinitionForLinker() &&
-               (!Subtarget->getTargetTriple().isMacOSX() ||
-                Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
-      // PC-relative references to external symbols should go through $stub,
-      // unless we're building with the leopard linker or later, which
-      // automatically synthesizes these stubs.
-      OpFlags = X86II::MO_DARWIN_STUB;
-    }
+    unsigned char OpFlags = Subtarget->classifyGlobalFunctionReference(GV, TM);
+    // Ignore NonLazyBind attribute in FastISel
+    if (OpFlags == X86II::MO_GOTPCREL)
+      OpFlags = 0;
 
     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
     if (Symbol)

Modified: llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86ISelLowering.cpp?rev=266863&r1=266862&r2=266863&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86ISelLowering.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86ISelLowering.cpp Wed Apr 20 03:32:57 2016
@@ -3273,31 +3273,8 @@ X86TargetLowering::LowerCall(TargetLower
     // non-JIT mode.
     const GlobalValue *GV = G->getGlobal();
     if (!GV->hasDLLImportStorageClass()) {
-      unsigned char OpFlags = 0;
-
-      // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
-      // external symbols most go through the PLT in PIC mode.  If the symbol
-      // has hidden or protected visibility, or if it is static or local, then
-      // we don't need to use the PLT - we can directly call it.
-      if (Subtarget.isTargetELF() &&
-          DAG.getTarget().getRelocationModel() == Reloc::PIC_ &&
-          GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
-        OpFlags = X86II::MO_PLT;
-      } else if (Subtarget.isPICStyleStubAny() &&
-                 !GV->isStrongDefinitionForLinker() &&
-                 (!Subtarget.getTargetTriple().isMacOSX() ||
-                  Subtarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
-        // PC-relative references to external symbols should go through $stub,
-        // unless we're building with the leopard linker or later, which
-        // automatically synthesizes these stubs.
-        OpFlags = X86II::MO_DARWIN_STUB;
-      } else if (Subtarget.isPICStyleRIPRel() && isa<Function>(GV) &&
-                 cast<Function>(GV)->hasFnAttribute(Attribute::NonLazyBind)) {
-        // If the function is marked as non-lazy, generate an indirect call
-        // which loads from the GOT directly. This avoids runtime overhead
-        // at the cost of eager binding (and one extra byte of encoding).
-        OpFlags = X86II::MO_GOTPCREL;
-      }
+      unsigned char OpFlags =
+          Subtarget.classifyGlobalFunctionReference(GV, DAG.getTarget());
 
       Callee = DAG.getTargetGlobalAddress(
           GV, dl, getPointerTy(DAG.getDataLayout()), G->getOffset(), OpFlags);

Modified: llvm/trunk/lib/Target/X86/X86Subtarget.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86Subtarget.cpp?rev=266863&r1=266862&r2=266863&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86Subtarget.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86Subtarget.cpp Wed Apr 20 03:32:57 2016
@@ -144,6 +144,35 @@ ClassifyGlobalReference(const GlobalValu
   return X86II::MO_NO_FLAG;
 }
 
+unsigned char X86Subtarget::classifyGlobalFunctionReference(
+    const GlobalValue *GV, const TargetMachine &TM) const {
+  // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
+  // external symbols most go through the PLT in PIC mode.  If the symbol
+  // has hidden or protected visibility, or if it is static or local, then
+  // we don't need to use the PLT - we can directly call it.
+  // In PIE mode, calls to global functions don't need to go through PLT
+  if (isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
+      (!TM.Options.PositionIndependentExecutable ||
+       GV->isDeclarationForLinker()) &&
+      GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
+    return X86II::MO_PLT;
+  } else if (isPICStyleStubAny() && !GV->isStrongDefinitionForLinker() &&
+             (!getTargetTriple().isMacOSX() ||
+              getTargetTriple().isMacOSXVersionLT(10, 5))) {
+    // PC-relative references to external symbols should go through $stub,
+    // unless we're building with the leopard linker or later, which
+    // automatically synthesizes these stubs.
+    return X86II::MO_DARWIN_STUB;
+  } else if (isPICStyleRIPRel() && isa<Function>(GV) &&
+             cast<Function>(GV)->hasFnAttribute(Attribute::NonLazyBind)) {
+    // If the function is marked as non-lazy, generate an indirect call
+    // which loads from the GOT directly. This avoids runtime overhead
+    // at the cost of eager binding (and one extra byte of encoding).
+    return X86II::MO_GOTPCREL;
+  }
+
+  return X86II::MO_NO_FLAG;
+}
 
 /// This function returns the name of a function which has an interface like
 /// the non-standard bzero function, if such a function exists on the

Modified: llvm/trunk/lib/Target/X86/X86Subtarget.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86Subtarget.h?rev=266863&r1=266862&r2=266863&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86Subtarget.h (original)
+++ llvm/trunk/lib/Target/X86/X86Subtarget.h Wed Apr 20 03:32:57 2016
@@ -554,6 +554,11 @@ public:
   unsigned char ClassifyGlobalReference(const GlobalValue *GV,
                                         const TargetMachine &TM)const;
 
+  /// classifyGlobalFunctionReference - Classify a global function reference
+  /// for the current subtarget.
+  unsigned char classifyGlobalFunctionReference(const GlobalValue *GV,
+                                                const TargetMachine &TM) const;
+
   /// Classify a blockaddress reference for the current subtarget according to
   /// how we should reference it in a non-pcrel context.
   unsigned char ClassifyBlockAddressReference() const;

Added: llvm/trunk/test/CodeGen/X86/pie.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/pie.ll?rev=266863&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/pie.ll (added)
+++ llvm/trunk/test/CodeGen/X86/pie.ll Wed Apr 20 03:32:57 2016
@@ -0,0 +1,41 @@
+; RUN: llc < %s -O0 -mcpu=generic -mtriple=i686-linux-gnu -relocation-model=pic -enable-pie | FileCheck %s 
+; RUN: llc < %s -O0 -mcpu=generic -mtriple=i686-linux-gnu -fast-isel -relocation-model=pic -enable-pie | FileCheck %s 
+; RUN: llc < %s -O0 -mcpu=generic -mtriple=x86_64-linux-gnu -relocation-model=pic -enable-pie | FileCheck %s 
+; RUN: llc < %s -O0 -mcpu=generic -mtriple=x86_64-linux-gnu -fast-isel -relocation-model=pic -enable-pie | FileCheck %s 
+
+; CHECK-LABEL:  bar:
+; CHECK:  call{{l|q}}  foo{{$}}
+; CHECK:  call{{l|q}}  weak_odr_foo{{$}}
+; CHECK:  call{{l|q}}  weak_foo{{$}}
+; CHECK:  call{{l|q}}  internal_foo{{$}}
+; CHECK:  call{{l|q}}  ext_baz at PLT
+
+define weak void @weak_foo() {
+  ret void
+}
+
+define weak_odr void @weak_odr_foo() {
+  ret void
+}
+
+define internal void @internal_foo() {
+  ret void
+}
+
+declare i32 @ext_baz()
+
+define void @foo() {
+  ret void
+}
+
+define void @bar() {
+entry:
+  call void @foo()
+  call void @weak_odr_foo()
+  call void @weak_foo()
+  call void @internal_foo()
+  call i32 @ext_baz()
+  ret void
+}
+
+; -fpie for local global data tests should be added here




More information about the llvm-commits mailing list