Index: lib/Target/X86/X86FastISel.cpp =================================================================== --- lib/Target/X86/X86FastISel.cpp +++ lib/Target/X86/X86FastISel.cpp @@ -3109,25 +3109,8 @@ 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); MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc)); if (Symbol) Index: lib/Target/X86/X86ISelLowering.cpp =================================================================== --- lib/Target/X86/X86ISelLowering.cpp +++ lib/Target/X86/X86ISelLowering.cpp @@ -3314,28 +3314,13 @@ // non-JIT mode. const GlobalValue *GV = G->getGlobal(); if (!GV->hasDLLImportStorageClass()) { - unsigned char OpFlags = 0; + unsigned char OpFlags + = Subtarget->ClassifyGlobalFunctionReference(GV, DAG.getTarget()); bool ExtraLoad = false; unsigned WrapperKind = ISD::DELETED_NODE; - - // 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(GV) && - cast(GV)->hasFnAttribute(Attribute::NonLazyBind)) { + if (OpFlags == X86II::MO_NO_FLAG && + Subtarget->isPICStyleRIPRel() && isa(GV) && + cast(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). Index: lib/Target/X86/X86Subtarget.h =================================================================== --- lib/Target/X86/X86Subtarget.h +++ lib/Target/X86/X86Subtarget.h @@ -501,6 +501,11 @@ 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; Index: lib/Target/X86/X86Subtarget.cpp =================================================================== --- lib/Target/X86/X86Subtarget.cpp +++ lib/Target/X86/X86Subtarget.cpp @@ -144,6 +144,32 @@ return X86II::MO_NO_FLAG; } +/// Classify a global function reference for the current subtarget. +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. + if (isTargetELF() && + TM.getRelocationModel() == Reloc::PIC_ && + !(TM.Options.PositionIndependentExecutable && + GV->isStrongDefinitionForLinker()) && + 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; + } + + 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 Index: test/CodeGen/X86/pie.ll =================================================================== --- /dev/null +++ test/CodeGen/X86/pie.ll @@ -0,0 +1,27 @@ +; RUN: llc < %s -mcpu=generic -mtriple=i686-linux-gnu -relocation-model=pic -asm-verbose=false -enable-pie | FileCheck %s -check-prefix=LINUX +; RUN: llc < %s -mcpu=generic -mtriple=i686-linux-gnu -fast-isel -relocation-model=pic -asm-verbose=false -enable-pie | FileCheck %s -check-prefix=LINUX +; RUN: llc < %s -mcpu=generic -mtriple=x86_64-linux-gnu -relocation-model=pic -asm-verbose=false -enable-pie | FileCheck %s -check-prefix=LINUX +; RUN: llc < %s -mcpu=generic -mtriple=x86_64-linux-gnu -fast-isel -relocation-model=pic -asm-verbose=false -enable-pie | FileCheck %s -check-prefix=LINUX + +; LINUX: call{{l|q}} foo{{$}} + +@glob = common global i32 0, align 4 + +; Function Attrs: nounwind uwtable +define i32 @foo() { +entry: + %0 = load i32, i32* @glob, align 4 + ret i32 %0 +} + +; Function Attrs: nounwind uwtable +define void @bar() { +entry: + %call = call i32 @foo() + store i32 %call, i32* @glob, align 4 + ret void +} + +!llvm.module.flags = !{!0} + +!0 = !{i32 1, !"PIC Level", i32 2}