2002-10-30 06:37:54 +08:00
|
|
|
//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
|
2005-04-22 07:38:14 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:38:14 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-22 07:38:14 +08:00
|
|
|
//
|
2002-10-30 06:37:54 +08:00
|
|
|
// This file defines the X86 specific subclass of TargetMachine.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-23 04:48:53 +08:00
|
|
|
#include "X86MCAsmInfo.h"
|
2002-10-30 06:37:54 +08:00
|
|
|
#include "X86TargetMachine.h"
|
2002-12-24 08:04:01 +08:00
|
|
|
#include "X86.h"
|
2003-04-24 00:24:55 +08:00
|
|
|
#include "llvm/PassManager.h"
|
2002-10-30 08:47:49 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2003-01-13 08:51:23 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2009-07-15 04:18:05 +08:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2004-07-11 12:17:10 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2009-07-25 14:49:55 +08:00
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2003-12-20 09:22:19 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2009-08-23 04:48:53 +08:00
|
|
|
static const MCAsmInfo *createMCAsmInfo(const Target &T,
|
2009-08-12 15:22:17 +08:00
|
|
|
const StringRef &TT) {
|
|
|
|
Triple TheTriple(TT);
|
|
|
|
switch (TheTriple.getOS()) {
|
|
|
|
case Triple::Darwin:
|
2009-08-23 05:03:30 +08:00
|
|
|
return new X86MCAsmInfoDarwin(TheTriple);
|
2009-08-12 15:22:17 +08:00
|
|
|
case Triple::MinGW32:
|
|
|
|
case Triple::MinGW64:
|
|
|
|
case Triple::Cygwin:
|
2009-08-23 05:03:30 +08:00
|
|
|
return new X86MCAsmInfoCOFF(TheTriple);
|
2009-08-12 15:22:17 +08:00
|
|
|
case Triple::Win32:
|
2009-08-23 04:48:53 +08:00
|
|
|
return new X86WinMCAsmInfo(TheTriple);
|
2009-08-12 15:22:17 +08:00
|
|
|
default:
|
2009-08-23 04:48:53 +08:00
|
|
|
return new X86ELFMCAsmInfo(TheTriple);
|
2009-08-12 15:22:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-16 04:24:03 +08:00
|
|
|
extern "C" void LLVMInitializeX86Target() {
|
2009-07-25 14:49:55 +08:00
|
|
|
// Register the target.
|
|
|
|
RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
|
|
|
|
RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
|
2009-06-17 04:12:29 +08:00
|
|
|
|
2009-08-12 15:22:17 +08:00
|
|
|
// Register the target asm info.
|
2009-08-23 04:48:53 +08:00
|
|
|
RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
|
|
|
|
RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
|
2009-08-27 16:12:55 +08:00
|
|
|
|
|
|
|
// Register the code emitter.
|
|
|
|
TargetRegistry::RegisterCodeEmitter(TheX86_32Target, createX86MCCodeEmitter);
|
|
|
|
TargetRegistry::RegisterCodeEmitter(TheX86_64Target, createX86MCCodeEmitter);
|
2006-09-08 07:39:26 +08:00
|
|
|
}
|
2006-09-08 14:48:29 +08:00
|
|
|
|
2009-08-12 15:22:17 +08:00
|
|
|
|
2009-08-03 07:37:13 +08:00
|
|
|
X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
|
2009-07-16 04:24:03 +08:00
|
|
|
const std::string &FS)
|
2009-08-03 07:37:13 +08:00
|
|
|
: X86TargetMachine(T, TT, FS, false) {
|
2006-09-08 14:48:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-03 07:37:13 +08:00
|
|
|
X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
|
2009-07-16 04:24:03 +08:00
|
|
|
const std::string &FS)
|
2009-08-03 07:37:13 +08:00
|
|
|
: X86TargetMachine(T, TT, FS, true) {
|
2006-09-08 14:48:29 +08:00
|
|
|
}
|
|
|
|
|
2009-07-09 11:32:31 +08:00
|
|
|
/// X86TargetMachine ctor - Create an X86 target.
|
2002-10-30 06:37:54 +08:00
|
|
|
///
|
2009-08-03 07:37:13 +08:00
|
|
|
X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
|
2009-07-16 04:24:03 +08:00
|
|
|
const std::string &FS, bool is64Bit)
|
2009-08-12 04:42:37 +08:00
|
|
|
: LLVMTargetMachine(T, TT),
|
2009-08-03 07:37:13 +08:00
|
|
|
Subtarget(TT, FS, is64Bit),
|
2007-08-07 05:48:35 +08:00
|
|
|
DataLayout(Subtarget.getDataLayout()),
|
2005-07-12 09:41:54 +08:00
|
|
|
FrameInfo(TargetFrameInfo::StackGrowsDown,
|
2009-08-03 16:12:53 +08:00
|
|
|
Subtarget.getStackAlignment(),
|
|
|
|
(Subtarget.isTargetWin64() ? -40 :
|
|
|
|
(Subtarget.is64Bit() ? -8 : -4))),
|
2009-06-12 03:16:03 +08:00
|
|
|
InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
|
2007-12-22 17:40:20 +08:00
|
|
|
DefRelocModel = getRelocationModel();
|
2009-07-09 11:32:31 +08:00
|
|
|
|
|
|
|
// If no relocation model was picked, default as appropriate for the target.
|
2008-02-20 19:22:39 +08:00
|
|
|
if (getRelocationModel() == Reloc::Default) {
|
2009-07-09 11:32:31 +08:00
|
|
|
if (!Subtarget.isTargetDarwin())
|
2006-12-05 02:07:10 +08:00
|
|
|
setRelocationModel(Reloc::Static);
|
2009-07-09 11:32:31 +08:00
|
|
|
else if (Subtarget.is64Bit())
|
|
|
|
setRelocationModel(Reloc::PIC_);
|
|
|
|
else
|
|
|
|
setRelocationModel(Reloc::DynamicNoPIC);
|
2008-02-20 19:22:39 +08: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.
llvm-svn: 57748
2008-10-18 10:06:02 +08:00
|
|
|
|
2009-07-09 11:15:51 +08:00
|
|
|
assert(getRelocationModel() != Reloc::Default &&
|
|
|
|
"Relocation mode not picked");
|
|
|
|
|
2009-07-09 11:32:31 +08:00
|
|
|
// If no code model is picked, default to small.
|
|
|
|
if (getCodeModel() == CodeModel::Default)
|
|
|
|
setCodeModel(CodeModel::Small);
|
|
|
|
|
2009-07-09 11:37:30 +08:00
|
|
|
// ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
|
2009-07-09 11:32:31 +08:00
|
|
|
// is defined as a model for code which may be used in static or dynamic
|
2009-07-09 12:24:46 +08: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.
llvm-svn: 57748
2008-10-18 10:06:02 +08:00
|
|
|
|
2009-07-09 11:37:30 +08: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 11:32:31 +08: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 11:15:51 +08:00
|
|
|
Subtarget.setPICStyle(PICStyles::None);
|
|
|
|
} else if (Subtarget.isTargetDarwin()) {
|
2007-01-19 06:27:12 +08:00
|
|
|
if (Subtarget.is64Bit())
|
2008-11-28 17:29:37 +08:00
|
|
|
Subtarget.setPICStyle(PICStyles::RIPRel);
|
2009-07-11 04:58:47 +08:00
|
|
|
else if (getRelocationModel() == Reloc::PIC_)
|
|
|
|
Subtarget.setPICStyle(PICStyles::StubPIC);
|
|
|
|
else {
|
|
|
|
assert(getRelocationModel() == Reloc::DynamicNoPIC);
|
|
|
|
Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
|
|
|
|
}
|
2008-02-20 19:22:39 +08:00
|
|
|
} else if (Subtarget.isTargetELF()) {
|
2007-01-19 06:27:12 +08:00
|
|
|
if (Subtarget.is64Bit())
|
2008-11-28 17:29:37 +08:00
|
|
|
Subtarget.setPICStyle(PICStyles::RIPRel);
|
2007-01-13 03:20:47 +08:00
|
|
|
else
|
2008-11-28 17:29:37 +08:00
|
|
|
Subtarget.setPICStyle(PICStyles::GOT);
|
2008-02-20 19:22:39 +08:00
|
|
|
}
|
2009-07-09 11:15:51 +08:00
|
|
|
|
2009-07-09 11:32:31 +08:00
|
|
|
// Finally, if we have "none" as our PIC style, force to static mode.
|
|
|
|
if (Subtarget.getPICStyle() == PICStyles::None)
|
|
|
|
setRelocationModel(Reloc::Static);
|
2006-02-04 02:59:39 +08:00
|
|
|
}
|
2002-10-30 06:37:54 +08:00
|
|
|
|
2006-09-04 12:14:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass Pipeline Configuration
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-06 00:34:44 +08:00
|
|
|
|
2009-04-30 07:29:43 +08:00
|
|
|
bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
2005-08-19 07:53:15 +08:00
|
|
|
// Install an instruction selector.
|
2009-04-29 08:15:41 +08:00
|
|
|
PM.add(createX86ISelDag(*this, OptLevel));
|
2008-10-26 01:46:52 +08:00
|
|
|
|
|
|
|
// If we're using Fast-ISel, clean up the mess.
|
|
|
|
if (EnableFastISel)
|
|
|
|
PM.add(createDeadMachineInstructionElimPass());
|
|
|
|
|
2008-11-13 06:55:05 +08:00
|
|
|
// Install a pass to insert x87 FP_REG_KILL instructions, as needed.
|
|
|
|
PM.add(createX87FPRegKillInserterPass());
|
|
|
|
|
2006-09-04 12:14:57 +08:00
|
|
|
return false;
|
2003-06-19 05:43:21 +08:00
|
|
|
}
|
|
|
|
|
2009-04-30 07:29:43 +08:00
|
|
|
bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
2008-04-24 02:23:30 +08:00
|
|
|
// Calculate and set max stack object alignment early, so we can decide
|
|
|
|
// whether we will need stack realignment (and thus FP).
|
2008-04-24 02:23:05 +08:00
|
|
|
PM.add(createX86MaxStackAlignmentCalculatorPass());
|
|
|
|
return false; // -print-machineinstr shouldn't print after this.
|
|
|
|
}
|
|
|
|
|
2009-04-30 07:29:43 +08:00
|
|
|
bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
2003-01-13 08:51:23 +08:00
|
|
|
PM.add(createX86FloatingPointStackifierPass());
|
2006-09-04 12:14:57 +08:00
|
|
|
return true; // -print-machineinstr should print after this.
|
|
|
|
}
|
2003-01-13 08:51:23 +08:00
|
|
|
|
2009-04-30 07:29:43 +08:00
|
|
|
bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
2009-06-02 03:57:37 +08:00
|
|
|
MachineCodeEmitter &MCE) {
|
2006-09-05 02:48:41 +08:00
|
|
|
// FIXME: Move this to TargetJITInfo!
|
2008-08-13 05:02:08 +08:00
|
|
|
// On Darwin, do not override 64-bit setting made in X86TargetMachine().
|
|
|
|
if (DefRelocModel == Reloc::Default &&
|
2009-07-09 11:37:30 +08:00
|
|
|
(!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
|
2007-12-22 17:40:20 +08:00
|
|
|
setRelocationModel(Reloc::Static);
|
2009-07-09 11:37:30 +08:00
|
|
|
Subtarget.setPICStyle(PICStyles::None);
|
|
|
|
}
|
2007-01-13 03:20:47 +08:00
|
|
|
|
2008-08-12 07:46:25 +08:00
|
|
|
// 64-bit JIT places everything in the same buffer except external functions.
|
2008-08-13 05:02:08 +08: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 09:03:20 +08:00
|
|
|
|
2006-07-26 04:40:54 +08:00
|
|
|
PM.add(createX86CodeEmitterPass(*this, MCE));
|
2007-07-21 05:56:13 +08:00
|
|
|
|
2005-07-11 13:17:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
2007-02-08 09:39:44 +08:00
|
|
|
|
2009-05-31 04:51:52 +08:00
|
|
|
bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
2009-06-02 03:57:37 +08:00
|
|
|
JITCodeEmitter &JCE) {
|
2009-05-31 04:51:52 +08:00
|
|
|
// FIXME: Move this to TargetJITInfo!
|
|
|
|
// On Darwin, do not override 64-bit setting made in X86TargetMachine().
|
|
|
|
if (DefRelocModel == Reloc::Default &&
|
2009-07-09 11:37:30 +08:00
|
|
|
(!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
|
2009-05-31 04:51:52 +08:00
|
|
|
setRelocationModel(Reloc::Static);
|
2009-07-09 11:37:30 +08:00
|
|
|
Subtarget.setPICStyle(PICStyles::None);
|
|
|
|
}
|
2009-05-31 04:51:52 +08: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 13:09:34 +08:00
|
|
|
bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
ObjectCodeEmitter &OCE) {
|
|
|
|
PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-29 08:15:41 +08:00
|
|
|
bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
|
2009-04-30 07:29:43 +08:00
|
|
|
CodeGenOpt::Level OptLevel,
|
2009-04-29 08:15:41 +08:00
|
|
|
MachineCodeEmitter &MCE) {
|
2007-02-08 09:39:44 +08:00
|
|
|
PM.add(createX86CodeEmitterPass(*this, MCE));
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-27 03:15:30 +08:00
|
|
|
|
2009-05-31 04:51:52 +08:00
|
|
|
bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
JITCodeEmitter &JCE) {
|
|
|
|
PM.add(createX86JITCodeEmitterPass(*this, JCE));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-06 13:09:34 +08:00
|
|
|
bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
ObjectCodeEmitter &OCE) {
|
|
|
|
PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
|
|
|
|
return false;
|
|
|
|
}
|