2002-10-29 22:37:54 +00:00
|
|
|
//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
|
2005-04-21 23:38:14 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:38:14 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-21 23:38:14 +00:00
|
|
|
//
|
2002-10-29 22:37:54 +00:00
|
|
|
// This file defines the X86 specific subclass of TargetMachine.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-09-07 23:39:26 +00:00
|
|
|
#include "X86TargetAsmInfo.h"
|
2002-10-29 22:37:54 +00:00
|
|
|
#include "X86TargetMachine.h"
|
2002-12-24 00:04:01 +00:00
|
|
|
#include "X86.h"
|
2003-08-24 19:49:48 +00:00
|
|
|
#include "llvm/Module.h"
|
2003-04-23 16:24:55 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2002-10-30 00:47:49 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2003-01-13 00:51:23 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2009-07-14 20:18:05 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2004-07-11 04:17:10 +00:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2004-07-11 02:48:49 +00:00
|
|
|
#include "llvm/Target/TargetMachineRegistry.h"
|
2003-12-20 01:22:19 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
// Register the target.
|
2009-07-15 20:24:03 +00:00
|
|
|
extern Target TheX86_32Target;
|
2008-05-13 00:00:25 +00:00
|
|
|
static RegisterTarget<X86_32TargetMachine>
|
2009-07-15 20:24:03 +00:00
|
|
|
X(TheX86_32Target, "x86", "32-bit X86: Pentium-Pro and above");
|
|
|
|
|
|
|
|
extern Target TheX86_64Target;
|
2008-05-13 00:00:25 +00:00
|
|
|
static RegisterTarget<X86_64TargetMachine>
|
2009-07-15 20:24:03 +00:00
|
|
|
Y(TheX86_64Target, "x86-64", "64-bit X86: EM64T and AMD64");
|
2002-12-16 16:15:51 +00:00
|
|
|
|
2009-06-23 23:59:40 +00:00
|
|
|
// Force static initialization.
|
2009-07-15 20:24:03 +00:00
|
|
|
extern "C" void LLVMInitializeX86Target() {
|
|
|
|
|
|
|
|
}
|
2009-06-16 20:12:29 +00:00
|
|
|
|
2006-09-07 23:39:26 +00:00
|
|
|
const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
|
2008-07-09 13:20:48 +00:00
|
|
|
if (Subtarget.isFlavorIntel())
|
|
|
|
return new X86WinTargetAsmInfo(*this);
|
|
|
|
else
|
|
|
|
switch (Subtarget.TargetType) {
|
|
|
|
case X86Subtarget::isDarwin:
|
|
|
|
return new X86DarwinTargetAsmInfo(*this);
|
|
|
|
case X86Subtarget::isELF:
|
|
|
|
return new X86ELFTargetAsmInfo(*this);
|
|
|
|
case X86Subtarget::isMingw:
|
|
|
|
case X86Subtarget::isCygwin:
|
|
|
|
return new X86COFFTargetAsmInfo(*this);
|
|
|
|
case X86Subtarget::isWindows:
|
|
|
|
return new X86WinTargetAsmInfo(*this);
|
|
|
|
default:
|
2008-09-25 21:00:33 +00:00
|
|
|
return new X86GenericTargetAsmInfo(*this);
|
2008-07-09 13:20:48 +00:00
|
|
|
}
|
2006-09-07 23:39:26 +00:00
|
|
|
}
|
2006-09-08 06:48:29 +00:00
|
|
|
|
2009-07-15 20:24:03 +00:00
|
|
|
X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
|
|
|
|
const std::string &FS)
|
|
|
|
: X86TargetMachine(T, M, FS, false) {
|
2006-09-08 06:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-15 20:24:03 +00:00
|
|
|
X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
|
|
|
|
const std::string &FS)
|
|
|
|
: X86TargetMachine(T, M, FS, true) {
|
2006-09-08 06:48:29 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 03:32:31 +00:00
|
|
|
/// X86TargetMachine ctor - Create an X86 target.
|
2002-10-29 22:37:54 +00:00
|
|
|
///
|
2009-07-15 20:24:03 +00:00
|
|
|
X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
|
|
|
|
const std::string &FS, bool is64Bit)
|
|
|
|
: LLVMTargetMachine(T),
|
|
|
|
Subtarget(M, FS, is64Bit),
|
2007-08-06 21:48:35 +00:00
|
|
|
DataLayout(Subtarget.getDataLayout()),
|
2005-07-12 01:41:54 +00:00
|
|
|
FrameInfo(TargetFrameInfo::StackGrowsDown,
|
2006-09-08 06:48:29 +00:00
|
|
|
Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
|
2009-06-11 19:16:03 +00:00
|
|
|
InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
|
2007-12-22 09:40:20 +00:00
|
|
|
DefRelocModel = getRelocationModel();
|
2009-07-09 03:32:31 +00:00
|
|
|
|
|
|
|
// If no relocation model was picked, default as appropriate for the target.
|
2008-02-20 11:22:39 +00:00
|
|
|
if (getRelocationModel() == Reloc::Default) {
|
2009-07-09 03:32:31 +00:00
|
|
|
if (!Subtarget.isTargetDarwin())
|
2006-12-04 18:07:10 +00:00
|
|
|
setRelocationModel(Reloc::Static);
|
2009-07-09 03:32:31 +00:00
|
|
|
else if (Subtarget.is64Bit())
|
|
|
|
setRelocationModel(Reloc::PIC_);
|
|
|
|
else
|
|
|
|
setRelocationModel(Reloc::DynamicNoPIC);
|
2008-02-20 11:22:39 +00:00
|
|
|
}
|
Teach DAGCombine to fold constant offsets into GlobalAddress nodes,
and add a TargetLowering hook for it to use to determine when this
is legal (i.e. not in PIC mode, etc.)
This allows instruction selection to emit folded constant offsets
in more cases, such as the included testcase, eliminating the need
for explicit arithmetic instructions.
This eliminates the need for the C++ code in X86ISelDAGToDAG.cpp
that attempted to achieve the same effect, but wasn't as effective.
Also, fix handling of offsets in GlobalAddressSDNodes in several
places, including changing GlobalAddressSDNode's offset from
int to int64_t.
The Mips, Alpha, Sparc, and CellSPU targets appear to be
unaware of GlobalAddress offsets currently, so set the hook to
false on those targets.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@57748 91177308-0d34-0410-b5e6-96231b3b80d8
2008-10-18 02:06:02 +00:00
|
|
|
|
2009-07-09 03:15:51 +00:00
|
|
|
assert(getRelocationModel() != Reloc::Default &&
|
|
|
|
"Relocation mode not picked");
|
|
|
|
|
2009-07-09 03:32:31 +00:00
|
|
|
// If no code model is picked, default to small.
|
|
|
|
if (getCodeModel() == CodeModel::Default)
|
|
|
|
setCodeModel(CodeModel::Small);
|
|
|
|
|
2009-07-09 03:37:30 +00:00
|
|
|
// ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
|
2009-07-09 03:32:31 +00:00
|
|
|
// is defined as a model for code which may be used in static or dynamic
|
2009-07-09 04:24:46 +00:00
|
|
|
// executables but not necessarily a shared library. On X86-32 we just
|
|
|
|
// compile in -static mode, in x86-64 we use PIC.
|
|
|
|
if (getRelocationModel() == Reloc::DynamicNoPIC) {
|
|
|
|
if (is64Bit)
|
|
|
|
setRelocationModel(Reloc::PIC_);
|
|
|
|
else if (!Subtarget.isTargetDarwin())
|
|
|
|
setRelocationModel(Reloc::Static);
|
|
|
|
}
|
Teach DAGCombine to fold constant offsets into GlobalAddress nodes,
and add a TargetLowering hook for it to use to determine when this
is legal (i.e. not in PIC mode, etc.)
This allows instruction selection to emit folded constant offsets
in more cases, such as the included testcase, eliminating the need
for explicit arithmetic instructions.
This eliminates the need for the C++ code in X86ISelDAGToDAG.cpp
that attempted to achieve the same effect, but wasn't as effective.
Also, fix handling of offsets in GlobalAddressSDNodes in several
places, including changing GlobalAddressSDNode's offset from
int to int64_t.
The Mips, Alpha, Sparc, and CellSPU targets appear to be
unaware of GlobalAddress offsets currently, so set the hook to
false on those targets.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@57748 91177308-0d34-0410-b5e6-96231b3b80d8
2008-10-18 02:06:02 +00:00
|
|
|
|
2009-07-09 03:37:30 +00:00
|
|
|
// If we are on Darwin, disallow static relocation model in X86-64 mode, since
|
|
|
|
// the Mach-O file format doesn't support it.
|
|
|
|
if (getRelocationModel() == Reloc::Static &&
|
|
|
|
Subtarget.isTargetDarwin() &&
|
|
|
|
is64Bit)
|
|
|
|
setRelocationModel(Reloc::PIC_);
|
|
|
|
|
2009-07-09 03:32:31 +00:00
|
|
|
// Determine the PICStyle based on the target selected.
|
|
|
|
if (getRelocationModel() == Reloc::Static) {
|
|
|
|
// Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
|
|
|
|
Subtarget.setPICStyle(PICStyles::None);
|
|
|
|
} else if (Subtarget.isTargetCygMing()) {
|
2009-07-09 03:15:51 +00:00
|
|
|
Subtarget.setPICStyle(PICStyles::None);
|
|
|
|
} else if (Subtarget.isTargetDarwin()) {
|
2007-01-18 22:27:12 +00:00
|
|
|
if (Subtarget.is64Bit())
|
2008-11-28 09:29:37 +00:00
|
|
|
Subtarget.setPICStyle(PICStyles::RIPRel);
|
2009-07-10 20:58:47 +00:00
|
|
|
else if (getRelocationModel() == Reloc::PIC_)
|
|
|
|
Subtarget.setPICStyle(PICStyles::StubPIC);
|
|
|
|
else {
|
|
|
|
assert(getRelocationModel() == Reloc::DynamicNoPIC);
|
|
|
|
Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
|
|
|
|
}
|
2008-02-20 11:22:39 +00:00
|
|
|
} else if (Subtarget.isTargetELF()) {
|
2007-01-18 22:27:12 +00:00
|
|
|
if (Subtarget.is64Bit())
|
2008-11-28 09:29:37 +00:00
|
|
|
Subtarget.setPICStyle(PICStyles::RIPRel);
|
2007-01-12 19:20:47 +00:00
|
|
|
else
|
2008-11-28 09:29:37 +00:00
|
|
|
Subtarget.setPICStyle(PICStyles::GOT);
|
2008-02-20 11:22:39 +00:00
|
|
|
}
|
2009-07-09 03:15:51 +00:00
|
|
|
|
2009-07-09 03:32:31 +00:00
|
|
|
// Finally, if we have "none" as our PIC style, force to static mode.
|
|
|
|
if (Subtarget.getPICStyle() == PICStyles::None)
|
|
|
|
setRelocationModel(Reloc::Static);
|
2006-02-03 18:59:39 +00:00
|
|
|
}
|
2002-10-29 22:37:54 +00:00
|
|
|
|
2006-09-04 04:14:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass Pipeline Configuration
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-05 16:34:44 +00:00
|
|
|
|
2009-04-29 23:29:43 +00:00
|
|
|
bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
2005-08-18 23:53:15 +00:00
|
|
|
// Install an instruction selector.
|
2009-04-29 00:15:41 +00:00
|
|
|
PM.add(createX86ISelDag(*this, OptLevel));
|
2008-10-25 17:46:52 +00:00
|
|
|
|
|
|
|
// If we're using Fast-ISel, clean up the mess.
|
|
|
|
if (EnableFastISel)
|
|
|
|
PM.add(createDeadMachineInstructionElimPass());
|
|
|
|
|
2008-11-12 22:55:05 +00:00
|
|
|
// Install a pass to insert x87 FP_REG_KILL instructions, as needed.
|
|
|
|
PM.add(createX87FPRegKillInserterPass());
|
|
|
|
|
2006-09-04 04:14:57 +00:00
|
|
|
return false;
|
2003-06-18 21:43:21 +00:00
|
|
|
}
|
|
|
|
|
2009-04-29 23:29:43 +00:00
|
|
|
bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
2008-04-23 18:23:30 +00:00
|
|
|
// Calculate and set max stack object alignment early, so we can decide
|
|
|
|
// whether we will need stack realignment (and thus FP).
|
2008-04-23 18:23:05 +00:00
|
|
|
PM.add(createX86MaxStackAlignmentCalculatorPass());
|
|
|
|
return false; // -print-machineinstr shouldn't print after this.
|
|
|
|
}
|
|
|
|
|
2009-04-29 23:29:43 +00:00
|
|
|
bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
2003-01-13 00:51:23 +00:00
|
|
|
PM.add(createX86FloatingPointStackifierPass());
|
2006-09-04 04:14:57 +00:00
|
|
|
return true; // -print-machineinstr should print after this.
|
|
|
|
}
|
2003-01-13 00:51:23 +00:00
|
|
|
|
2009-04-29 23:29:43 +00:00
|
|
|
bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
2009-06-01 19:57:37 +00:00
|
|
|
MachineCodeEmitter &MCE) {
|
2006-09-04 18:48:41 +00:00
|
|
|
// FIXME: Move this to TargetJITInfo!
|
2008-08-12 21:02:08 +00:00
|
|
|
// On Darwin, do not override 64-bit setting made in X86TargetMachine().
|
|
|
|
if (DefRelocModel == Reloc::Default &&
|
2009-07-09 03:37:30 +00:00
|
|
|
(!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
|
2007-12-22 09:40:20 +00:00
|
|
|
setRelocationModel(Reloc::Static);
|
2009-07-09 03:37:30 +00:00
|
|
|
Subtarget.setPICStyle(PICStyles::None);
|
|
|
|
}
|
2007-01-12 19:20:47 +00:00
|
|
|
|
2008-08-11 23:46:25 +00:00
|
|
|
// 64-bit JIT places everything in the same buffer except external functions.
|
2008-08-12 21:02:08 +00:00
|
|
|
// On Darwin, use small code model but hack the call instruction for
|
|
|
|
// externals. Elsewhere, do not assume globals are in the lower 4G.
|
|
|
|
if (Subtarget.is64Bit()) {
|
|
|
|
if (Subtarget.isTargetDarwin())
|
|
|
|
setCodeModel(CodeModel::Small);
|
|
|
|
else
|
|
|
|
setCodeModel(CodeModel::Large);
|
|
|
|
}
|
2006-12-20 01:03:20 +00:00
|
|
|
|
2006-07-25 20:40:54 +00:00
|
|
|
PM.add(createX86CodeEmitterPass(*this, MCE));
|
2007-07-20 21:56:13 +00:00
|
|
|
|
2005-07-11 05:17:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-02-08 01:39:44 +00:00
|
|
|
|
2009-05-30 20:51:52 +00:00
|
|
|
bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
2009-06-01 19:57:37 +00:00
|
|
|
JITCodeEmitter &JCE) {
|
2009-05-30 20:51:52 +00:00
|
|
|
// FIXME: Move this to TargetJITInfo!
|
|
|
|
// On Darwin, do not override 64-bit setting made in X86TargetMachine().
|
|
|
|
if (DefRelocModel == Reloc::Default &&
|
2009-07-09 03:37:30 +00:00
|
|
|
(!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
|
2009-05-30 20:51:52 +00:00
|
|
|
setRelocationModel(Reloc::Static);
|
2009-07-09 03:37:30 +00:00
|
|
|
Subtarget.setPICStyle(PICStyles::None);
|
|
|
|
}
|
2009-05-30 20:51:52 +00:00
|
|
|
|
|
|
|
// 64-bit JIT places everything in the same buffer except external functions.
|
|
|
|
// On Darwin, use small code model but hack the call instruction for
|
|
|
|
// externals. Elsewhere, do not assume globals are in the lower 4G.
|
|
|
|
if (Subtarget.is64Bit()) {
|
|
|
|
if (Subtarget.isTargetDarwin())
|
|
|
|
setCodeModel(CodeModel::Small);
|
|
|
|
else
|
|
|
|
setCodeModel(CodeModel::Large);
|
|
|
|
}
|
|
|
|
|
|
|
|
PM.add(createX86JITCodeEmitterPass(*this, JCE));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-06 05:09:34 +00:00
|
|
|
bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
ObjectCodeEmitter &OCE) {
|
|
|
|
PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-29 00:15:41 +00:00
|
|
|
bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
|
2009-04-29 23:29:43 +00:00
|
|
|
CodeGenOpt::Level OptLevel,
|
2009-04-29 00:15:41 +00:00
|
|
|
MachineCodeEmitter &MCE) {
|
2007-02-08 01:39:44 +00:00
|
|
|
PM.add(createX86CodeEmitterPass(*this, MCE));
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-26 19:15:30 +00:00
|
|
|
|
2009-05-30 20:51:52 +00:00
|
|
|
bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
JITCodeEmitter &JCE) {
|
|
|
|
PM.add(createX86JITCodeEmitterPass(*this, JCE));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-06 05:09:34 +00:00
|
|
|
bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
ObjectCodeEmitter &OCE) {
|
|
|
|
PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
|
|
|
|
return false;
|
|
|
|
}
|