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"
|
2010-05-21 20:54:43 +08:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCStreamer.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
|
|
|
|
2010-03-21 06:36:22 +08:00
|
|
|
static MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
|
2009-08-12 15:22:17 +08:00
|
|
|
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:
|
|
|
|
case Triple::Win32:
|
2011-02-01 09:14:13 +08:00
|
|
|
if (TheTriple.getEnvironment() == Triple::MachO)
|
|
|
|
return new X86MCAsmInfoDarwin(TheTriple);
|
|
|
|
else
|
|
|
|
return new X86MCAsmInfoCOFF(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-21 20:54:43 +08:00
|
|
|
static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
|
|
|
|
MCContext &Ctx, TargetAsmBackend &TAB,
|
|
|
|
raw_ostream &_OS,
|
|
|
|
MCCodeEmitter *_Emitter,
|
2011-01-24 01:55:27 +08:00
|
|
|
bool RelaxAll,
|
|
|
|
bool NoExecStack) {
|
2010-05-21 20:54:43 +08:00
|
|
|
Triple TheTriple(TT);
|
|
|
|
switch (TheTriple.getOS()) {
|
2010-08-04 21:16:30 +08:00
|
|
|
case Triple::Darwin:
|
|
|
|
return createMachOStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
|
|
|
|
case Triple::MinGW32:
|
|
|
|
case Triple::MinGW64:
|
|
|
|
case Triple::Cygwin:
|
2010-07-27 14:46:15 +08:00
|
|
|
case Triple::Win32:
|
2011-02-01 09:14:13 +08:00
|
|
|
if (TheTriple.getEnvironment() == Triple::MachO)
|
|
|
|
return createMachOStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
|
|
|
|
else
|
|
|
|
return createWinCOFFStreamer(Ctx, TAB, *_Emitter, _OS, RelaxAll);
|
2010-05-21 20:54:43 +08:00
|
|
|
default:
|
2011-01-24 01:55:27 +08:00
|
|
|
return createELFStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll, NoExecStack);
|
2010-05-21 20:54:43 +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.
|
2010-02-04 05:14:33 +08:00
|
|
|
TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
|
2010-02-13 08:49:29 +08:00
|
|
|
createX86_32MCCodeEmitter);
|
2010-02-04 05:14:33 +08:00
|
|
|
TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
|
2010-02-13 08:49:29 +08:00
|
|
|
createX86_64MCCodeEmitter);
|
2010-02-22 05:54:14 +08:00
|
|
|
|
|
|
|
// Register the asm backend.
|
|
|
|
TargetRegistry::RegisterAsmBackend(TheX86_32Target,
|
|
|
|
createX86_32AsmBackend);
|
|
|
|
TargetRegistry::RegisterAsmBackend(TheX86_64Target,
|
|
|
|
createX86_64AsmBackend);
|
2010-05-21 20:54:43 +08:00
|
|
|
|
|
|
|
// Register the object streamer.
|
|
|
|
TargetRegistry::RegisterObjectStreamer(TheX86_32Target,
|
|
|
|
createMCStreamer);
|
|
|
|
TargetRegistry::RegisterObjectStreamer(TheX86_64Target,
|
|
|
|
createMCStreamer);
|
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)
|
2010-10-04 02:59:45 +08:00
|
|
|
: X86TargetMachine(T, TT, FS, false),
|
|
|
|
DataLayout(getSubtargetImpl()->isTargetDarwin() ?
|
|
|
|
"e-p:32:32-f64:32:64-i64:32:64-f80:128:128-n8:16:32" :
|
|
|
|
(getSubtargetImpl()->isTargetCygMing() ||
|
|
|
|
getSubtargetImpl()->isTargetWindows()) ?
|
|
|
|
"e-p:32:32-f64:64:64-i64:64:64-f80:32:32-n8:16:32" :
|
|
|
|
"e-p:32:32-f64:32:64-i64:32:64-f80:32:32-n8:16:32"),
|
|
|
|
InstrInfo(*this),
|
|
|
|
TSInfo(*this),
|
|
|
|
TLInfo(*this),
|
|
|
|
JITInfo(*this) {
|
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)
|
2010-10-04 02:59:45 +08:00
|
|
|
: X86TargetMachine(T, TT, FS, true),
|
|
|
|
DataLayout("e-p:64:64-s:64-f64:64:64-i64:64:64-f80:128:128-n8:16:32:64"),
|
|
|
|
InstrInfo(*this),
|
|
|
|
TSInfo(*this),
|
|
|
|
TLInfo(*this),
|
|
|
|
JITInfo(*this) {
|
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)
|
2010-11-19 07:25:52 +08:00
|
|
|
: LLVMTargetMachine(T, TT),
|
2009-08-03 07:37:13 +08:00
|
|
|
Subtarget(TT, FS, is64Bit),
|
2011-01-10 20:39:04 +08:00
|
|
|
FrameLowering(*this, Subtarget),
|
2010-10-04 02:59:45 +08:00
|
|
|
ELFWriterInfo(is64Bit, true) {
|
2007-12-22 17:40:20 +08:00
|
|
|
DefRelocModel = getRelocationModel();
|
2010-08-22 01:21:11 +08:00
|
|
|
|
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) {
|
2010-08-22 01:21:11 +08:00
|
|
|
// Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
|
|
|
|
// Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
|
|
|
|
// use static relocation model by default.
|
|
|
|
if (Subtarget.isTargetDarwin()) {
|
|
|
|
if (Subtarget.is64Bit())
|
|
|
|
setRelocationModel(Reloc::PIC_);
|
|
|
|
else
|
|
|
|
setRelocationModel(Reloc::DynamicNoPIC);
|
|
|
|
} else if (Subtarget.isTargetWin64())
|
2009-07-09 11:32:31 +08:00
|
|
|
setRelocationModel(Reloc::PIC_);
|
|
|
|
else
|
2010-08-22 01:21:11 +08:00
|
|
|
setRelocationModel(Reloc::Static);
|
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: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_);
|
2010-08-22 01:21:11 +08:00
|
|
|
|
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);
|
2010-08-22 01:21:11 +08:00
|
|
|
} else if (Subtarget.is64Bit()) {
|
|
|
|
// PIC in 64 bit mode is always rip-rel.
|
|
|
|
Subtarget.setPICStyle(PICStyles::RIPRel);
|
2009-07-09 11:32:31 +08:00
|
|
|
} else if (Subtarget.isTargetCygMing()) {
|
2009-07-09 11:15:51 +08:00
|
|
|
Subtarget.setPICStyle(PICStyles::None);
|
|
|
|
} else if (Subtarget.isTargetDarwin()) {
|
2010-08-22 01:21:11 +08:00
|
|
|
if (getRelocationModel() == Reloc::PIC_)
|
2009-07-11 04:58:47 +08:00
|
|
|
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()) {
|
2010-08-22 01:21:11 +08:00
|
|
|
Subtarget.setPICStyle(PICStyles::GOT);
|
2008-02-20 19:22:39 +08:00
|
|
|
}
|
2010-08-22 01:21:11 +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
|
|
|
|
2010-07-10 17:00:22 +08:00
|
|
|
// For 32-bit, prepend instructions to set the "global base reg" for PIC.
|
|
|
|
if (!Subtarget.is64Bit())
|
|
|
|
PM.add(createGlobalBaseRegPass());
|
|
|
|
|
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) {
|
2010-04-07 04:26:37 +08:00
|
|
|
PM.add(createX86MaxStackAlignmentHeuristicPass());
|
2008-04-24 02:23:05 +08:00
|
|
|
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
|
|
|
|
2010-03-26 01:25:00 +08:00
|
|
|
bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
2010-05-05 15:35:59 +08:00
|
|
|
if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
|
2010-03-26 01:25:00 +08:00
|
|
|
PM.add(createSSEDomainFixPass());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
PM.add(createX86JITCodeEmitterPass(*this, JCE));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-21 16:15:29 +08:00
|
|
|
void X86TargetMachine::setCodeModelForStatic() {
|
|
|
|
|
|
|
|
if (getCodeModel() != CodeModel::Default) return;
|
|
|
|
|
|
|
|
// For static codegen, if we're not already set, use Small codegen.
|
|
|
|
setCodeModel(CodeModel::Small);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void X86TargetMachine::setCodeModelForJIT() {
|
|
|
|
|
|
|
|
if (getCodeModel() != CodeModel::Default) return;
|
|
|
|
|
|
|
|
// 64-bit JIT places everything in the same buffer except external functions.
|
|
|
|
if (Subtarget.is64Bit())
|
|
|
|
setCodeModel(CodeModel::Large);
|
|
|
|
else
|
|
|
|
setCodeModel(CodeModel::Small);
|
|
|
|
}
|