blob: f2c50583c95f7f1d00b7b204a2d39556009e266e [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"
Matt Flemingdcd35722010-05-21 12:54:43 +000020#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCStreamer.h"
David Greene302008d2009-07-14 20:18:05 +000022#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Target/TargetOptions.h"
Daniel Dunbarc680b012009-07-25 06:49:55 +000024#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025using namespace llvm;
26
Daniel Dunbar6c566962010-03-20 22:36:22 +000027static MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000028 Triple TheTriple(TT);
29 switch (TheTriple.getOS()) {
30 case Triple::Darwin:
Chris Lattnercd4cab92009-08-22 21:03:30 +000031 return new X86MCAsmInfoDarwin(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000032 case Triple::MinGW32:
33 case Triple::MinGW64:
34 case Triple::Cygwin:
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000035 case Triple::Win32:
Anton Korobeynikov6afbe282010-02-14 15:19:54 +000036 return new X86MCAsmInfoCOFF(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000037 default:
Chris Lattner621c44d2009-08-22 20:48:53 +000038 return new X86ELFMCAsmInfo(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000039 }
40}
41
Matt Flemingdcd35722010-05-21 12:54:43 +000042static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
43 MCContext &Ctx, TargetAsmBackend &TAB,
44 raw_ostream &_OS,
45 MCCodeEmitter *_Emitter,
46 bool RelaxAll) {
47 Triple TheTriple(TT);
48 switch (TheTriple.getOS()) {
49 default:
50 return createMachOStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
51 }
52}
53
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000054extern "C" void LLVMInitializeX86Target() {
Daniel Dunbarc680b012009-07-25 06:49:55 +000055 // Register the target.
56 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
57 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000058
59 // Register the target asm info.
Chris Lattner621c44d2009-08-22 20:48:53 +000060 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
61 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
Daniel Dunbar2f379632009-08-27 08:12:55 +000062
63 // Register the code emitter.
Chris Lattner917e8982010-02-03 21:14:33 +000064 TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
Chris Lattner20e92202010-02-13 00:49:29 +000065 createX86_32MCCodeEmitter);
Chris Lattner917e8982010-02-03 21:14:33 +000066 TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
Chris Lattner20e92202010-02-13 00:49:29 +000067 createX86_64MCCodeEmitter);
Daniel Dunbarc8ce0aa2010-02-21 21:54:14 +000068
69 // Register the asm backend.
70 TargetRegistry::RegisterAsmBackend(TheX86_32Target,
71 createX86_32AsmBackend);
72 TargetRegistry::RegisterAsmBackend(TheX86_64Target,
73 createX86_64AsmBackend);
Matt Flemingdcd35722010-05-21 12:54:43 +000074
75 // Register the object streamer.
76 TargetRegistry::RegisterObjectStreamer(TheX86_32Target,
77 createMCStreamer);
78 TargetRegistry::RegisterObjectStreamer(TheX86_64Target,
79 createMCStreamer);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000080}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000081
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000083X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000084 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000085 : X86TargetMachine(T, TT, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086}
87
88
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000089X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000090 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000091 : X86TargetMachine(T, TT, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092}
93
Chris Lattner5b5e32d2009-07-09 03:32:31 +000094/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095///
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000096X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000097 const std::string &FS, bool is64Bit)
Chris Lattnerd9236ec2009-08-11 20:42:37 +000098 : LLVMTargetMachine(T, TT),
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000099 Subtarget(TT, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +0000100 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 FrameInfo(TargetFrameInfo::StackGrowsDown,
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +0000102 Subtarget.getStackAlignment(),
103 (Subtarget.isTargetWin64() ? -40 :
104 (Subtarget.is64Bit() ? -8 : -4))),
Dan Gohmancfbb3232010-05-11 17:31:57 +0000105 InstrInfo(*this), JITInfo(*this), TLInfo(*this), TSInfo(*this),
106 ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000107 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000108
109 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000110 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000111 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000113 else if (Subtarget.is64Bit())
114 setRelocationModel(Reloc::PIC_);
115 else
116 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000117 }
Dan Gohman36322c72008-10-18 02:06:02 +0000118
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000119 assert(getRelocationModel() != Reloc::Default &&
120 "Relocation mode not picked");
121
Chris Lattner4c8b6862009-07-09 03:37:30 +0000122 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000123 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +0000124 // executables but not necessarily a shared library. On X86-32 we just
125 // compile in -static mode, in x86-64 we use PIC.
126 if (getRelocationModel() == Reloc::DynamicNoPIC) {
127 if (is64Bit)
128 setRelocationModel(Reloc::PIC_);
129 else if (!Subtarget.isTargetDarwin())
130 setRelocationModel(Reloc::Static);
131 }
Dan Gohman36322c72008-10-18 02:06:02 +0000132
Chris Lattner4c8b6862009-07-09 03:37:30 +0000133 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
134 // the Mach-O file format doesn't support it.
135 if (getRelocationModel() == Reloc::Static &&
136 Subtarget.isTargetDarwin() &&
137 is64Bit)
138 setRelocationModel(Reloc::PIC_);
139
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000140 // Determine the PICStyle based on the target selected.
141 if (getRelocationModel() == Reloc::Static) {
142 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
143 Subtarget.setPICStyle(PICStyles::None);
144 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000145 Subtarget.setPICStyle(PICStyles::None);
146 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000148 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattnerfb502722009-07-10 20:58:47 +0000149 else if (getRelocationModel() == Reloc::PIC_)
150 Subtarget.setPICStyle(PICStyles::StubPIC);
151 else {
152 assert(getRelocationModel() == Reloc::DynamicNoPIC);
153 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
154 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000155 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000157 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000159 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000160 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000161
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000162 // Finally, if we have "none" as our PIC style, force to static mode.
163 if (Subtarget.getPICStyle() == PICStyles::None)
164 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165}
166
167//===----------------------------------------------------------------------===//
168// Pass Pipeline Configuration
169//===----------------------------------------------------------------------===//
170
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000171bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
172 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000174 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000175
Dan Gohman17ce2612010-07-06 15:49:48 +0000176 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
177 PM.add(createX87FPRegKillInserterPass());
178
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 return false;
180}
181
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000182bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
183 CodeGenOpt::Level OptLevel) {
Jim Grosbachc5cf9942010-04-06 20:26:37 +0000184 PM.add(createX86MaxStackAlignmentHeuristicPass());
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000185 return false; // -print-machineinstr shouldn't print after this.
186}
187
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000188bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
189 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 PM.add(createX86FloatingPointStackifierPass());
191 return true; // -print-machineinstr should print after this.
192}
193
Jakob Stoklund Olesen0ba75302010-03-25 17:25:00 +0000194bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
195 CodeGenOpt::Level OptLevel) {
Eric Christopher51318e92010-05-05 07:35:59 +0000196 if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
Jakob Stoklund Olesen0ba75302010-03-25 17:25:00 +0000197 PM.add(createSSEDomainFixPass());
198 return true;
199 }
200 return false;
201}
202
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000203bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
204 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000205 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000206 // FIXME: Move this to TargetJITInfo!
207 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
208 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000209 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000210 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000211 Subtarget.setPICStyle(PICStyles::None);
212 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000213
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000214
215 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000216
217 return false;
218}
219
Eric Christopher7669c972009-12-21 08:15:29 +0000220void X86TargetMachine::setCodeModelForStatic() {
221
222 if (getCodeModel() != CodeModel::Default) return;
223
224 // For static codegen, if we're not already set, use Small codegen.
225 setCodeModel(CodeModel::Small);
226}
227
228
229void X86TargetMachine::setCodeModelForJIT() {
230
231 if (getCodeModel() != CodeModel::Default) return;
232
233 // 64-bit JIT places everything in the same buffer except external functions.
234 if (Subtarget.is64Bit())
235 setCodeModel(CodeModel::Large);
236 else
237 setCodeModel(CodeModel::Small);
238}