blob: c64084756c160c8dbbf14da547edce0f41c78427 [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
14#include "X86TargetAsmInfo.h"
15#include "X86TargetMachine.h"
16#include "X86.h"
17#include "llvm/Module.h"
18#include "llvm/PassManager.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/Passes.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"
23#include "llvm/Target/TargetMachineRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
26/// X86TargetMachineModule - Note that this is used on hosts that cannot link
27/// in a library unless there are references into the library. In particular,
28/// it seems that it is not possible to get things to work on Win32 without
29/// this. Though it is unused, do not remove it.
30extern "C" int X86TargetMachineModule;
31int X86TargetMachineModule = 0;
32
Dan Gohman089efff2008-05-13 00:00:25 +000033// Register the target.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000034extern Target TheX86_32Target;
Dan Gohman089efff2008-05-13 00:00:25 +000035static RegisterTarget<X86_32TargetMachine>
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000036X(TheX86_32Target, "x86", "32-bit X86: Pentium-Pro and above");
37
38extern Target TheX86_64Target;
Dan Gohman089efff2008-05-13 00:00:25 +000039static RegisterTarget<X86_64TargetMachine>
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000040Y(TheX86_64Target, "x86-64", "64-bit X86: EM64T and AMD64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
Bob Wilsonebbc1c42009-06-23 23:59:40 +000042// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000043extern "C" void LLVMInitializeX86Target() {
44
45}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000046
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +000047// No assembler printer by default
48X86TargetMachine::AsmPrinterCtorFn X86TargetMachine::AsmPrinterCtor = 0;
49
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikovf700e682008-07-09 13:20:48 +000051 if (Subtarget.isFlavorIntel())
52 return new X86WinTargetAsmInfo(*this);
53 else
54 switch (Subtarget.TargetType) {
55 case X86Subtarget::isDarwin:
56 return new X86DarwinTargetAsmInfo(*this);
57 case X86Subtarget::isELF:
58 return new X86ELFTargetAsmInfo(*this);
59 case X86Subtarget::isMingw:
60 case X86Subtarget::isCygwin:
61 return new X86COFFTargetAsmInfo(*this);
62 case X86Subtarget::isWindows:
63 return new X86WinTargetAsmInfo(*this);
64 default:
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000065 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikovf700e682008-07-09 13:20:48 +000066 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067}
68
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000069X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
70 const std::string &FS)
71 : X86TargetMachine(T, M, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072}
73
74
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000075X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
76 const std::string &FS)
77 : X86TargetMachine(T, M, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078}
79
Chris Lattner5b5e32d2009-07-09 03:32:31 +000080/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081///
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000082X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
83 const std::string &FS, bool is64Bit)
84 : LLVMTargetMachine(T),
85 Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +000086 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 FrameInfo(TargetFrameInfo::StackGrowsDown,
88 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000089 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +000090 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +000091
92 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000093 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +000094 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +000096 else if (Subtarget.is64Bit())
97 setRelocationModel(Reloc::PIC_);
98 else
99 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000100 }
Dan Gohman36322c72008-10-18 02:06:02 +0000101
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000102 assert(getRelocationModel() != Reloc::Default &&
103 "Relocation mode not picked");
104
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000105 // If no code model is picked, default to small.
106 if (getCodeModel() == CodeModel::Default)
107 setCodeModel(CodeModel::Small);
108
Chris Lattner4c8b6862009-07-09 03:37:30 +0000109 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000110 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +0000111 // executables but not necessarily a shared library. On X86-32 we just
112 // compile in -static mode, in x86-64 we use PIC.
113 if (getRelocationModel() == Reloc::DynamicNoPIC) {
114 if (is64Bit)
115 setRelocationModel(Reloc::PIC_);
116 else if (!Subtarget.isTargetDarwin())
117 setRelocationModel(Reloc::Static);
118 }
Dan Gohman36322c72008-10-18 02:06:02 +0000119
Chris Lattner4c8b6862009-07-09 03:37:30 +0000120 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
121 // the Mach-O file format doesn't support it.
122 if (getRelocationModel() == Reloc::Static &&
123 Subtarget.isTargetDarwin() &&
124 is64Bit)
125 setRelocationModel(Reloc::PIC_);
126
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000127 // Determine the PICStyle based on the target selected.
128 if (getRelocationModel() == Reloc::Static) {
129 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
130 Subtarget.setPICStyle(PICStyles::None);
131 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000132 Subtarget.setPICStyle(PICStyles::None);
133 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000135 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattnerfb502722009-07-10 20:58:47 +0000136 else if (getRelocationModel() == Reloc::PIC_)
137 Subtarget.setPICStyle(PICStyles::StubPIC);
138 else {
139 assert(getRelocationModel() == Reloc::DynamicNoPIC);
140 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
141 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000142 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000144 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000146 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000147 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000148
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000149 // Finally, if we have "none" as our PIC style, force to static mode.
150 if (Subtarget.getPICStyle() == PICStyles::None)
151 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152}
153
154//===----------------------------------------------------------------------===//
155// Pass Pipeline Configuration
156//===----------------------------------------------------------------------===//
157
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000158bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
159 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000161 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000162
163 // If we're using Fast-ISel, clean up the mess.
164 if (EnableFastISel)
165 PM.add(createDeadMachineInstructionElimPass());
166
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000167 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
168 PM.add(createX87FPRegKillInserterPass());
169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 return false;
171}
172
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000173bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
174 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000175 // Calculate and set max stack object alignment early, so we can decide
176 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000177 PM.add(createX86MaxStackAlignmentCalculatorPass());
178 return false; // -print-machineinstr shouldn't print after this.
179}
180
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000181bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
182 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 PM.add(createX86FloatingPointStackifierPass());
184 return true; // -print-machineinstr should print after this.
185}
186
Bill Wendling58ed5d22009-04-29 00:15:41 +0000187bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000188 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000189 bool Verbose,
David Greene302008d2009-07-14 20:18:05 +0000190 formatted_raw_ostream &Out) {
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000191 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
192 if (AsmPrinterCtor)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000193 PM.add(AsmPrinterCtor(Out, *this, Verbose));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 return false;
195}
196
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000197bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
198 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000199 bool DumpAsm,
200 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000202 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
203 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000204 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000205 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000206 Subtarget.setPICStyle(PICStyles::None);
207 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208
Dale Johannesenc501c082008-08-11 23:46:25 +0000209 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000210 // On Darwin, use small code model but hack the call instruction for
211 // externals. Elsewhere, do not assume globals are in the lower 4G.
212 if (Subtarget.is64Bit()) {
213 if (Subtarget.isTargetDarwin())
214 setCodeModel(CodeModel::Small);
215 else
216 setCodeModel(CodeModel::Large);
217 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218
219 PM.add(createX86CodeEmitterPass(*this, MCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000220 if (DumpAsm)
221 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Evan Cheng77547212007-07-20 21:56:13 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 return false;
224}
225
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000226bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
227 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000228 bool DumpAsm,
229 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000230 // FIXME: Move this to TargetJITInfo!
231 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
232 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000233 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000234 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000235 Subtarget.setPICStyle(PICStyles::None);
236 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000237
238 // 64-bit JIT places everything in the same buffer except external functions.
239 // On Darwin, use small code model but hack the call instruction for
240 // externals. Elsewhere, do not assume globals are in the lower 4G.
241 if (Subtarget.is64Bit()) {
242 if (Subtarget.isTargetDarwin())
243 setCodeModel(CodeModel::Small);
244 else
245 setCodeModel(CodeModel::Large);
246 }
247
248 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000249 if (DumpAsm)
250 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000251
252 return false;
253}
254
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000255bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
256 CodeGenOpt::Level OptLevel,
257 bool DumpAsm,
258 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000259 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000260 if (DumpAsm)
261 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000262
263 return false;
264}
265
Bill Wendling58ed5d22009-04-29 00:15:41 +0000266bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000267 CodeGenOpt::Level OptLevel,
268 bool DumpAsm,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000269 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 PM.add(createX86CodeEmitterPass(*this, MCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000271 if (DumpAsm)
272 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 return false;
275}
Dan Gohmanc6413362008-09-26 19:15:30 +0000276
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000277bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
278 CodeGenOpt::Level OptLevel,
279 bool DumpAsm,
280 JITCodeEmitter &JCE) {
281 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000282 if (DumpAsm)
283 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000284
285 return false;
286}
287
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000288bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
289 CodeGenOpt::Level OptLevel,
290 bool DumpAsm,
291 ObjectCodeEmitter &OCE) {
292 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Daniel Dunbar8ba03b72009-07-15 12:49:15 +0000293 if (DumpAsm)
294 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000295
296 return false;
297}