Skip to content

Commit

Permalink
[X86] Extend some Linux special cases to cover kFreeBSD.
Browse files Browse the repository at this point in the history
Both Linux and kFreeBSD use glibc, so follow similiar code paths.
Add isTargetGlibc to check for this, and use it instead of isTargetLinux
in a few places.

Fixes PR22248 for kFreeBSD.

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

llvm-svn: 268624
  • Loading branch information
mwkmwkmwk committed May 5, 2016
1 parent 0360c0f commit 0275fac
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 5 deletions.
10 changes: 10 additions & 0 deletions llvm/include/llvm/ADT/Triple.h
Expand Up @@ -523,6 +523,16 @@ class Triple {
return getOS() == Triple::Linux;
}

/// Tests whether the OS is kFreeBSD.
bool isOSKFreeBSD() const {
return getOS() == Triple::KFreeBSD;
}

/// Tests whether the OS uses glibc.
bool isOSGlibc() const {
return getOS() == Triple::Linux || getOS() == Triple::KFreeBSD;
}

/// Tests whether the OS uses the ELF binary format.
bool isOSBinFormatELF() const {
return getObjectFormat() == Triple::ELF;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
Expand Up @@ -719,7 +719,7 @@ bool X86DAGToDAGISel::matchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM){
// For more information see http://people.redhat.com/drepper/tls.pdf
if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Address))
if (C->getSExtValue() == 0 && AM.Segment.getNode() == nullptr &&
Subtarget->isTargetLinux())
Subtarget->isTargetGlibc())
switch (N->getPointerInfo().getAddrSpace()) {
case 256:
AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Expand Up @@ -1919,7 +1919,9 @@ unsigned X86TargetLowering::getAddressSpace() const {
}

Value *X86TargetLowering::getIRStackGuard(IRBuilder<> &IRB) const {
if (!Subtarget.isTargetLinux())
// glibc has a special slot for the stack guard in tcbhead_t, use it instead
// of the usual global variable (see sysdeps/{i386,x86_64}/nptl/tls.h)
if (!Subtarget.isTargetGlibc())
return TargetLowering::getIRStackGuard(IRB);

// %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
Expand All @@ -1932,7 +1934,7 @@ Value *X86TargetLowering::getIRStackGuard(IRBuilder<> &IRB) const {
}

void X86TargetLowering::insertSSPDeclarations(Module &M) const {
if (!Subtarget.isTargetLinux())
if (!Subtarget.isTargetGlibc())
TargetLowering::insertSSPDeclarations(M);
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/X86/X86Subtarget.cpp
Expand Up @@ -262,12 +262,12 @@ void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
assert((!In64BitMode || HasX86_64) &&
"64-bit code requested on a subtarget that doesn't support it!");

// Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
// Stack alignment is 16 bytes on Darwin, Linux, kFreeBSD and Solaris (both
// 32 and 64 bit) and for all 64-bit targets.
if (StackAlignOverride)
stackAlignment = StackAlignOverride;
else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
In64BitMode)
isTargetKFreeBSD() || In64BitMode)
stackAlignment = 16;
}

Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/X86/X86Subtarget.h
Expand Up @@ -467,6 +467,8 @@ class X86Subtarget final : public X86GenSubtargetInfo {
bool isTargetMachO() const { return TargetTriple.isOSBinFormatMachO(); }

bool isTargetLinux() const { return TargetTriple.isOSLinux(); }
bool isTargetKFreeBSD() const { return TargetTriple.isOSKFreeBSD(); }
bool isTargetGlibc() const { return TargetTriple.isOSGlibc(); }
bool isTargetAndroid() const { return TargetTriple.isAndroid(); }
bool isTargetNaCl() const { return TargetTriple.isOSNaCl(); }
bool isTargetNaCl32() const { return isTargetNaCl() && !is64Bit(); }
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/X86/stack-align2.ll
@@ -1,7 +1,9 @@
; RUN: llc < %s -mcpu=generic -mtriple=i386-linux | FileCheck %s -check-prefix=LINUX-I386
; RUN: llc < %s -mcpu=generic -mtriple=i386-kfreebsd | FileCheck %s -check-prefix=KFREEBSD-I386
; RUN: llc < %s -mcpu=generic -mtriple=i386-netbsd | FileCheck %s -check-prefix=NETBSD-I386
; RUN: llc < %s -mcpu=generic -mtriple=i686-apple-darwin8 | FileCheck %s -check-prefix=DARWIN-I386
; RUN: llc < %s -mcpu=generic -mtriple=x86_64-linux | FileCheck %s -check-prefix=LINUX-X86_64
; RUN: llc < %s -mcpu=generic -mtriple=x86_64-kfreebsd | FileCheck %s -check-prefix=KFREEBSD-X86_64
; RUN: llc < %s -mcpu=generic -mtriple=x86_64-netbsd | FileCheck %s -check-prefix=NETBSD-X86_64
; RUN: llc < %s -mcpu=generic -mtriple=x86_64-apple-darwin8 | FileCheck %s -check-prefix=DARWIN-X86_64

Expand All @@ -11,6 +13,7 @@ entry:
ret i32 0

; LINUX-I386: subl $12, %esp
; KFREEBSD-I386: subl $12, %esp
; DARWIN-I386: subl $12, %esp
; NETBSD-I386-NOT: subl {{.*}}, %esp

Expand All @@ -20,6 +23,8 @@ entry:
; DARWIN-X86_64-NOT: subq {{.*}}, %rsp
; NETBSD-X86_64: pushq %{{.*}}
; NETBSD-X86_64-NOT: subq {{.*}}, %rsp
; KFREEBSD-X86_64: pushq %{{.*}}
; KFREEBSD-X86_64-NOT: subq {{.*}}, %rsp
}

declare void @test2()
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/X86/stack-protector-target.ll
Expand Up @@ -3,6 +3,8 @@
; RUN: llc -mtriple=x86_64-linux < %s -o - | FileCheck --check-prefix=LINUX-X64 %s
; RUN: llc -mtriple=i386-linux-android < %s -o - | FileCheck --check-prefix=LINUX-I386 %s
; RUN: llc -mtriple=x86_64-linux-android < %s -o - | FileCheck --check-prefix=LINUX-X64 %s
; RUN: llc -mtriple=i386-kfreebsd < %s -o - | FileCheck --check-prefix=LINUX-I386 %s
; RUN: llc -mtriple=x86_64-kfreebsd < %s -o - | FileCheck --check-prefix=LINUX-X64 %s

define void @_Z1fv() sspreq {
entry:
Expand Down

0 comments on commit 0275fac

Please sign in to comment.