blob: 59fd7a9c328750512cc76503452da7fdacc8f50a [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86 specific subclass of TargetMachine.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner621c44d2009-08-22 20:48:53 +000014#include "X86MCAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "X86TargetMachine.h"
16#include "X86.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/PassManager.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen0ba75302010-03-25 17:25:00 +000020#include "llvm/Support/CommandLine.h"
David Greene302008d2009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetOptions.h"
Daniel Dunbarc680b012009-07-25 06:49:55 +000023#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
Daniel Dunbar6c566962010-03-20 22:36:22 +000026static MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000027 Triple TheTriple(TT);
28 switch (TheTriple.getOS()) {
29 case Triple::Darwin:
Chris Lattnercd4cab92009-08-22 21:03:30 +000030 return new X86MCAsmInfoDarwin(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000031 case Triple::MinGW32:
32 case Triple::MinGW64:
33 case Triple::Cygwin:
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000034 case Triple::Win32:
Anton Korobeynikov6afbe282010-02-14 15:19:54 +000035 return new X86MCAsmInfoCOFF(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000036 default:
Chris Lattner621c44d2009-08-22 20:48:53 +000037 return new X86ELFMCAsmInfo(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000038 }
39}
40
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000041extern "C" void LLVMInitializeX86Target() {
Daniel Dunbarc680b012009-07-25 06:49:55 +000042 // Register the target.
43 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
44 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000045
46 // Register the target asm info.
Chris Lattner621c44d2009-08-22 20:48:53 +000047 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
48 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
Daniel Dunbar2f379632009-08-27 08:12:55 +000049
50 // Register the code emitter.
Chris Lattner917e8982010-02-03 21:14:33 +000051 TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
Chris Lattner20e92202010-02-13 00:49:29 +000052 createX86_32MCCodeEmitter);
Chris Lattner917e8982010-02-03 21:14:33 +000053 TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
Chris Lattner20e92202010-02-13 00:49:29 +000054 createX86_64MCCodeEmitter);
Daniel Dunbarc8ce0aa2010-02-21 21:54:14 +000055
56 // Register the asm backend.
57 TargetRegistry::RegisterAsmBackend(TheX86_32Target,
58 createX86_32AsmBackend);
59 TargetRegistry::RegisterAsmBackend(TheX86_64Target,
60 createX86_64AsmBackend);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000061}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000062
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000064X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000065 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000066 : X86TargetMachine(T, TT, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067}
68
69
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000070X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000071 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000072 : X86TargetMachine(T, TT, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073}
74
Chris Lattner5b5e32d2009-07-09 03:32:31 +000075/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076///
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000077X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000078 const std::string &FS, bool is64Bit)
Chris Lattnerd9236ec2009-08-11 20:42:37 +000079 : LLVMTargetMachine(T, TT),
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000080 Subtarget(TT, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +000081 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 FrameInfo(TargetFrameInfo::StackGrowsDown,
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +000083 Subtarget.getStackAlignment(),
84 (Subtarget.isTargetWin64() ? -40 :
85 (Subtarget.is64Bit() ? -8 : -4))),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000086 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +000087 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +000088
89 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000090 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +000091 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +000093 else if (Subtarget.is64Bit())
94 setRelocationModel(Reloc::PIC_);
95 else
96 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000097 }
Dan Gohman36322c72008-10-18 02:06:02 +000098
Chris Lattneraa7c6d22009-07-09 03:15:51 +000099 assert(getRelocationModel() != Reloc::Default &&
100 "Relocation mode not picked");
101
Chris Lattner4c8b6862009-07-09 03:37:30 +0000102 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000103 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +0000104 // executables but not necessarily a shared library. On X86-32 we just
105 // compile in -static mode, in x86-64 we use PIC.
106 if (getRelocationModel() == Reloc::DynamicNoPIC) {
107 if (is64Bit)
108 setRelocationModel(Reloc::PIC_);
109 else if (!Subtarget.isTargetDarwin())
110 setRelocationModel(Reloc::Static);
111 }
Dan Gohman36322c72008-10-18 02:06:02 +0000112
Chris Lattner4c8b6862009-07-09 03:37:30 +0000113 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
114 // the Mach-O file format doesn't support it.
115 if (getRelocationModel() == Reloc::Static &&
116 Subtarget.isTargetDarwin() &&
117 is64Bit)
118 setRelocationModel(Reloc::PIC_);
119
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000120 // Determine the PICStyle based on the target selected.
121 if (getRelocationModel() == Reloc::Static) {
122 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
123 Subtarget.setPICStyle(PICStyles::None);
124 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000125 Subtarget.setPICStyle(PICStyles::None);
126 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000128 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattnerfb502722009-07-10 20:58:47 +0000129 else if (getRelocationModel() == Reloc::PIC_)
130 Subtarget.setPICStyle(PICStyles::StubPIC);
131 else {
132 assert(getRelocationModel() == Reloc::DynamicNoPIC);
133 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
134 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000135 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000137 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000139 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000140 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000141
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000142 // Finally, if we have "none" as our PIC style, force to static mode.
143 if (Subtarget.getPICStyle() == PICStyles::None)
144 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145}
146
147//===----------------------------------------------------------------------===//
148// Pass Pipeline Configuration
149//===----------------------------------------------------------------------===//
150
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000151bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
152 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000154 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000155
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000156 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
157 PM.add(createX87FPRegKillInserterPass());
158
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 return false;
160}
161
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000162bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
163 CodeGenOpt::Level OptLevel) {
Jim Grosbachc5cf9942010-04-06 20:26:37 +0000164 PM.add(createX86MaxStackAlignmentHeuristicPass());
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000165 return false; // -print-machineinstr shouldn't print after this.
166}
167
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000168bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
169 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 PM.add(createX86FloatingPointStackifierPass());
171 return true; // -print-machineinstr should print after this.
172}
173
Jakob Stoklund Olesen0ba75302010-03-25 17:25:00 +0000174bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
175 CodeGenOpt::Level OptLevel) {
Jakob Stoklund Olesen950b8862010-03-30 22:47:00 +0000176 if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
Jakob Stoklund Olesen0ba75302010-03-25 17:25:00 +0000177 PM.add(createSSEDomainFixPass());
178 return true;
179 }
180 return false;
181}
182
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000183bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
184 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000185 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000186 // FIXME: Move this to TargetJITInfo!
187 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
188 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000189 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000190 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000191 Subtarget.setPICStyle(PICStyles::None);
192 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000193
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000194
195 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000196
197 return false;
198}
199
Eric Christopher7669c972009-12-21 08:15:29 +0000200void X86TargetMachine::setCodeModelForStatic() {
201
202 if (getCodeModel() != CodeModel::Default) return;
203
204 // For static codegen, if we're not already set, use Small codegen.
205 setCodeModel(CodeModel::Small);
206}
207
208
209void X86TargetMachine::setCodeModelForJIT() {
210
211 if (getCodeModel() != CodeModel::Default) return;
212
213 // 64-bit JIT places everything in the same buffer except external functions.
214 if (Subtarget.is64Bit())
215 setCodeModel(CodeModel::Large);
216 else
217 setCodeModel(CodeModel::Small);
218}