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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-22 20:48:53 +00:00
|
|
|
#include "X86MCAsmInfo.h"
|
2002-10-29 22:37:54 +00:00
|
|
|
#include "X86TargetMachine.h"
|
2002-12-24 00:04:01 +00:00
|
|
|
#include "X86.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"
|
2010-05-21 12:54:43 +00:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCStreamer.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"
|
2009-07-25 06:49:55 +00:00
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2003-12-20 01:22:19 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2010-03-20 22:36:22 +00:00
|
|
|
static MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
|
2009-08-12 07:22:17 +00:00
|
|
|
Triple TheTriple(TT);
|
|
|
|
switch (TheTriple.getOS()) {
|
|
|
|
case Triple::Darwin:
|
2009-08-22 21:03:30 +00:00
|
|
|
return new X86MCAsmInfoDarwin(TheTriple);
|
2009-08-12 07:22:17 +00:00
|
|
|
case Triple::MinGW32:
|
|
|
|
case Triple::MinGW64:
|
|
|
|
case Triple::Cygwin:
|
|
|
|
case Triple::Win32:
|
2010-02-14 15:19:54 +00:00
|
|
|
return new X86MCAsmInfoCOFF(TheTriple);
|
2009-08-12 07:22:17 +00:00
|
|
|
default:
|
2009-08-22 20:48:53 +00:00
|
|
|
return new X86ELFMCAsmInfo(TheTriple);
|
2009-08-12 07:22:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-21 12:54:43 +00:00
|
|
|
static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
|
|
|
|
MCContext &Ctx, TargetAsmBackend &TAB,
|
|
|
|
raw_ostream &_OS,
|
|
|
|
MCCodeEmitter *_Emitter,
|
2011-01-23 17:55:27 +00:00
|
|
|
bool RelaxAll,
|
|
|
|
bool NoExecStack) {
|
2010-05-21 12:54:43 +00:00
|
|
|
Triple TheTriple(TT);
|
|
|
|
switch (TheTriple.getOS()) {
|
2010-08-04 13:16:30 +00:00
|
|
|
case Triple::Darwin:
|
|
|
|
return createMachOStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
|
|
|
|
case Triple::MinGW32:
|
|
|
|
case Triple::MinGW64:
|
|
|
|
case Triple::Cygwin:
|
2010-07-27 06:46:15 +00:00
|
|
|
case Triple::Win32:
|
2010-07-31 06:22:29 +00:00
|
|
|
return createWinCOFFStreamer(Ctx, TAB, *_Emitter, _OS, RelaxAll);
|
2010-05-21 12:54:43 +00:00
|
|
|
default:
|
2011-01-23 17:55:27 +00:00
|
|
|
return createELFStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll, NoExecStack);
|
2010-05-21 12:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 20:24:03 +00:00
|
|
|
extern "C" void LLVMInitializeX86Target() {
|
2009-07-25 06:49:55 +00:00
|
|
|
// Register the target.
|
|
|
|
RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
|
|
|
|
RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
|
2009-06-16 20:12:29 +00:00
|
|
|
|
2009-08-12 07:22:17 +00:00
|
|
|
// Register the target asm info.
|
2009-08-22 20:48:53 +00:00
|
|
|
RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
|
|
|
|
RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
|
2009-08-27 08:12:55 +00:00
|
|
|
|
|
|
|
// Register the code emitter.
|
2010-02-03 21:14:33 +00:00
|
|
|
TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
|
2010-02-13 00:49:29 +00:00
|
|
|
createX86_32MCCodeEmitter);
|
2010-02-03 21:14:33 +00:00
|
|
|
TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
|
2010-02-13 00:49:29 +00:00
|
|
|
createX86_64MCCodeEmitter);
|
2010-02-21 21:54:14 +00:00
|
|
|
|
|
|
|
// Register the asm backend.
|
|
|
|
TargetRegistry::RegisterAsmBackend(TheX86_32Target,
|
|
|
|
createX86_32AsmBackend);
|
|
|
|
TargetRegistry::RegisterAsmBackend(TheX86_64Target,
|
|
|
|
createX86_64AsmBackend);
|
2010-05-21 12:54:43 +00:00
|
|
|
|
|
|
|
// Register the object streamer.
|
|
|
|
TargetRegistry::RegisterObjectStreamer(TheX86_32Target,
|
|
|
|
createMCStreamer);
|
|
|
|
TargetRegistry::RegisterObjectStreamer(TheX86_64Target,
|
|
|
|
createMCStreamer);
|
2006-09-07 23:39:26 +00:00
|
|
|
}
|
2006-09-08 06:48:29 +00:00
|
|
|
|
2009-08-12 07:22:17 +00:00
|
|
|
|
2009-08-02 23:37:13 +00:00
|
|
|
X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
|
2009-07-15 20:24:03 +00:00
|
|
|
const std::string &FS)
|
2010-10-03 18:59:45 +00: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 06:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-02 23:37:13 +00:00
|
|
|
X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
|
2009-07-15 20:24:03 +00:00
|
|
|
const std::string &FS)
|
2010-10-03 18:59:45 +00: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 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-08-02 23:37:13 +00:00
|
|
|
X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
|
2009-07-15 20:24:03 +00:00
|
|
|
const std::string &FS, bool is64Bit)
|
2010-11-18 23:25:52 +00:00
|
|
|
: LLVMTargetMachine(T, TT),
|
2009-08-02 23:37:13 +00:00
|
|
|
Subtarget(TT, FS, is64Bit),
|
2011-01-10 12:39:04 +00:00
|
|
|
FrameLowering(*this, Subtarget),
|
2010-10-03 18:59:45 +00:00
|
|
|
ELFWriterInfo(is64Bit, true) {
|
2007-12-22 09:40:20 +00:00
|
|
|
DefRelocModel = getRelocationModel();
|
2010-08-21 17:21:11 +00:00
|
|
|
|
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) {
|
2010-08-21 17:21:11 +00: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 03:32:31 +00:00
|
|
|
setRelocationModel(Reloc::PIC_);
|
|
|
|
else
|
2010-08-21 17:21:11 +00:00
|
|
|
setRelocationModel(Reloc::Static);
|
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: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_);
|
2010-08-21 17:21:11 +00:00
|
|
|
|
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);
|
2010-08-21 17:21:11 +00:00
|
|
|
} else if (Subtarget.is64Bit()) {
|
|
|
|
// PIC in 64 bit mode is always rip-rel.
|
|
|
|
Subtarget.setPICStyle(PICStyles::RIPRel);
|
2009-07-09 03:32:31 +00:00
|
|
|
} else if (Subtarget.isTargetCygMing()) {
|
2009-07-09 03:15:51 +00:00
|
|
|
Subtarget.setPICStyle(PICStyles::None);
|
|
|
|
} else if (Subtarget.isTargetDarwin()) {
|
2010-08-21 17:21:11 +00:00
|
|
|
if (getRelocationModel() == Reloc::PIC_)
|
2009-07-10 20:58:47 +00:00
|
|
|
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()) {
|
2010-08-21 17:21:11 +00:00
|
|
|
Subtarget.setPICStyle(PICStyles::GOT);
|
2008-02-20 11:22:39 +00:00
|
|
|
}
|
2010-08-21 17:21:11 +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
|
|
|
|
2010-07-10 09:00:22 +00:00
|
|
|
// For 32-bit, prepend instructions to set the "global base reg" for PIC.
|
|
|
|
if (!Subtarget.is64Bit())
|
|
|
|
PM.add(createGlobalBaseRegPass());
|
|
|
|
|
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) {
|
2010-04-06 20:26:37 +00:00
|
|
|
PM.add(createX86MaxStackAlignmentHeuristicPass());
|
2008-04-23 18:23:05 +00:00
|
|
|
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
|
|
|
|
2010-03-25 17:25:00 +00:00
|
|
|
bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
2010-05-05 07:35:59 +00:00
|
|
|
if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
|
2010-03-25 17:25:00 +00:00
|
|
|
PM.add(createSSEDomainFixPass());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
PM.add(createX86JITCodeEmitterPass(*this, JCE));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-21 08:15:29 +00: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);
|
|
|
|
}
|