blob: 9ea10db74495fa43c6676364baec7147f215c7f6 [file] [log] [blame]
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00001//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00009//
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000010// This file defines the X86 specific subclass of TargetMachine.
11//
12//===----------------------------------------------------------------------===//
13
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000014#include "X86TargetAsmInfo.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000015#include "X86TargetMachine.h"
Chris Lattner5bcd95c2002-12-24 00:04:01 +000016#include "X86.h"
Chris Lattnerbb144a82003-08-24 19:49:48 +000017#include "llvm/Module.h"
Chris Lattner155e68f2003-04-23 16:24:55 +000018#include "llvm/PassManager.h"
Chris Lattner3dffa792002-10-30 00:47:49 +000019#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd91d86f2003-01-13 00:51:23 +000020#include "llvm/CodeGen/Passes.h"
David Greene71847812009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Chris Lattner0cf0c372004-07-11 04:17:10 +000022#include "llvm/Target/TargetOptions.h"
Chris Lattnerd36c9702004-07-11 02:48:49 +000023#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner1e60a912003-12-20 01:22:19 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Jeff Cohen1c32f792005-01-03 16:34:19 +000026/// 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 Gohman844731a2008-05-13 00:00:25 +000033// Register the target.
Daniel Dunbar42467902009-07-15 09:22:31 +000034extern Target TheX86_32Target;
Dan Gohman844731a2008-05-13 00:00:25 +000035static RegisterTarget<X86_32TargetMachine>
Daniel Dunbar42467902009-07-15 09:22:31 +000036X(TheX86_32Target, "x86", "32-bit X86: Pentium-Pro and above");
37
38extern Target TheX86_64Target;
Dan Gohman844731a2008-05-13 00:00:25 +000039static RegisterTarget<X86_64TargetMachine>
Daniel Dunbar42467902009-07-15 09:22:31 +000040Y(TheX86_64Target, "x86-64", "64-bit X86: EM64T and AMD64");
Chris Lattner439a27a2002-12-16 16:15:51 +000041
Bob Wilsona96751f2009-06-23 23:59:40 +000042// Force static initialization.
Daniel Dunbar42467902009-07-15 09:22:31 +000043extern "C" void LLVMInitializeX86Target() {
44
45}
Douglas Gregor1555a232009-06-16 20:12:29 +000046
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +000047// No assembler printer by default
48X86TargetMachine::AsmPrinterCtorFn X86TargetMachine::AsmPrinterCtor = 0;
49
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000050const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov4468b7a2008-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 Korobeynikov32b952a2008-09-25 21:00:33 +000065 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000066 }
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000067}
68
Daniel Dunbar03f4bc52009-07-15 12:11:05 +000069X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
70 const std::string &FS)
71 : X86TargetMachine(T, M, FS, false) {
Evan Cheng25ab6902006-09-08 06:48:29 +000072}
73
74
Daniel Dunbar03f4bc52009-07-15 12:11:05 +000075X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
76 const std::string &FS)
77 : X86TargetMachine(T, M, FS, true) {
Evan Cheng25ab6902006-09-08 06:48:29 +000078}
79
Chris Lattner11348ee2009-07-09 03:32:31 +000080/// X86TargetMachine ctor - Create an X86 target.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000081///
Daniel Dunbar03f4bc52009-07-15 12:11:05 +000082X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
83 const std::string &FS, bool is64Bit)
84 : LLVMTargetMachine(T),
85 Subtarget(M, FS, is64Bit),
Dale Johannesen27f92be2007-08-06 21:48:35 +000086 DataLayout(Subtarget.getDataLayout()),
Nate Begemanfb5792f2005-07-12 01:41:54 +000087 FrameInfo(TargetFrameInfo::StackGrowsDown,
Evan Cheng25ab6902006-09-08 06:48:29 +000088 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000089 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Chengaabe38b2007-12-22 09:40:20 +000090 DefRelocModel = getRelocationModel();
Chris Lattner11348ee2009-07-09 03:32:31 +000091
92 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000093 if (getRelocationModel() == Reloc::Default) {
Chris Lattner11348ee2009-07-09 03:32:31 +000094 if (!Subtarget.isTargetDarwin())
Evan Cheng2c312ad2006-12-04 18:07:10 +000095 setRelocationModel(Reloc::Static);
Chris Lattner11348ee2009-07-09 03:32:31 +000096 else if (Subtarget.is64Bit())
97 setRelocationModel(Reloc::PIC_);
98 else
99 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000100 }
Dan Gohman6520e202008-10-18 02:06:02 +0000101
Chris Lattnere4df7562009-07-09 03:15:51 +0000102 assert(getRelocationModel() != Reloc::Default &&
103 "Relocation mode not picked");
104
Chris Lattner11348ee2009-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 Lattnerb2fc55b2009-07-09 03:37:30 +0000109 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner11348ee2009-07-09 03:32:31 +0000110 // is defined as a model for code which may be used in static or dynamic
Chris Lattner88e1fd52009-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 Gohman6520e202008-10-18 02:06:02 +0000119
Chris Lattnerb2fc55b2009-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 Lattner11348ee2009-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 Lattnere4df7562009-07-09 03:15:51 +0000132 Subtarget.setPICStyle(PICStyles::None);
133 } else if (Subtarget.isTargetDarwin()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000134 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000135 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattner8097b652009-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 Korobeynikov7c1c2612008-02-20 11:22:39 +0000142 } else if (Subtarget.isTargetELF()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000143 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000144 Subtarget.setPICStyle(PICStyles::RIPRel);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000145 else
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000146 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000147 }
Chris Lattnere4df7562009-07-09 03:15:51 +0000148
Chris Lattner11348ee2009-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);
Chris Lattner4efab052006-02-03 18:59:39 +0000152}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000153
Chris Lattner1911fd42006-09-04 04:14:57 +0000154//===----------------------------------------------------------------------===//
155// Pass Pipeline Configuration
156//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000157
Bill Wendling98a366d2009-04-29 23:29:43 +0000158bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
159 CodeGenOpt::Level OptLevel) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000160 // Install an instruction selector.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000161 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman71b7f642008-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 Gohmanbc5cbb82008-11-12 22:55:05 +0000167 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
168 PM.add(createX87FPRegKillInserterPass());
169
Chris Lattner1911fd42006-09-04 04:14:57 +0000170 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000171}
172
Bill Wendling98a366d2009-04-29 23:29:43 +0000173bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
174 CodeGenOpt::Level OptLevel) {
Anton Korobeynikovd52bdaf2008-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 Korobeynikov856914f2008-04-23 18:23:05 +0000177 PM.add(createX86MaxStackAlignmentCalculatorPass());
178 return false; // -print-machineinstr shouldn't print after this.
179}
180
Bill Wendling98a366d2009-04-29 23:29:43 +0000181bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
182 CodeGenOpt::Level OptLevel) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000183 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000184 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000185}
186
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000187bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000188 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000189 bool Verbose,
David Greene71847812009-07-14 20:18:05 +0000190 formatted_raw_ostream &Out) {
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +0000191 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
192 if (AsmPrinterCtor)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000193 PM.add(AsmPrinterCtor(Out, *this, Verbose));
Chris Lattner1911fd42006-09-04 04:14:57 +0000194 return false;
195}
196
Bill Wendling98a366d2009-04-29 23:29:43 +0000197bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
198 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000199 bool DumpAsm,
200 MachineCodeEmitter &MCE) {
Chris Lattner2130b992006-09-04 18:48:41 +0000201 // FIXME: Move this to TargetJITInfo!
Dale Johannesen5f777192008-08-12 21:02:08 +0000202 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
203 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000204 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Chengaabe38b2007-12-22 09:40:20 +0000205 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000206 Subtarget.setPICStyle(PICStyles::None);
207 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000208
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000209 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen5f777192008-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 }
Anton Korobeynikov15fccf12006-12-20 01:03:20 +0000218
Evan Cheng55fc2802006-07-25 20:40:54 +0000219 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +0000220 if (DumpAsm) {
221 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
222 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000223 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +0000224 }
Evan Cheng8bd60352007-07-20 21:56:13 +0000225
Chris Lattner81b6ed72005-07-11 05:17:48 +0000226 return false;
227}
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000228
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000229bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
230 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000231 bool DumpAsm,
232 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000233 // FIXME: Move this to TargetJITInfo!
234 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
235 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000236 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000237 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000238 Subtarget.setPICStyle(PICStyles::None);
239 }
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000240
241 // 64-bit JIT places everything in the same buffer except external functions.
242 // On Darwin, use small code model but hack the call instruction for
243 // externals. Elsewhere, do not assume globals are in the lower 4G.
244 if (Subtarget.is64Bit()) {
245 if (Subtarget.isTargetDarwin())
246 setCodeModel(CodeModel::Small);
247 else
248 setCodeModel(CodeModel::Large);
249 }
250
251 PM.add(createX86JITCodeEmitterPass(*this, JCE));
252 if (DumpAsm) {
253 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
254 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000255 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000256 }
257
258 return false;
259}
260
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000261bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
262 CodeGenOpt::Level OptLevel,
263 bool DumpAsm,
264 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000265 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
266 if (DumpAsm) {
267 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
268 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000269 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000270 }
271
272 return false;
273}
274
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000275bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000276 CodeGenOpt::Level OptLevel,
277 bool DumpAsm,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000278 MachineCodeEmitter &MCE) {
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000279 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +0000280 if (DumpAsm) {
281 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
282 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000283 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +0000284 }
285
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000286 return false;
287}
Dan Gohman97135e12008-09-26 19:15:30 +0000288
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000289bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
290 CodeGenOpt::Level OptLevel,
291 bool DumpAsm,
292 JITCodeEmitter &JCE) {
293 PM.add(createX86JITCodeEmitterPass(*this, JCE));
294 if (DumpAsm) {
295 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
296 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000297 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000298 }
299
300 return false;
301}
302
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000303bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
304 CodeGenOpt::Level OptLevel,
305 bool DumpAsm,
306 ObjectCodeEmitter &OCE) {
307 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
308 if (DumpAsm) {
309 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
310 if (AsmPrinterCtor)
David Greene71847812009-07-14 20:18:05 +0000311 PM.add(AsmPrinterCtor(ferrs(), *this, true));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000312 }
313
314 return false;
315}