blob: eb0cf93562e094e81ebb9c8ffbc426f856f953a9 [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"
Owen Anderson847b99b2008-08-21 00:14:44 +000021#include "llvm/Support/raw_ostream.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.
34static RegisterTarget<X86_32TargetMachine>
Dan Gohman669b9bf2008-10-14 20:25:08 +000035X("x86", "32-bit X86: Pentium-Pro and above");
Dan Gohman089efff2008-05-13 00:00:25 +000036static RegisterTarget<X86_64TargetMachine>
Dan Gohman669b9bf2008-10-14 20:25:08 +000037Y("x86-64", "64-bit X86: EM64T and AMD64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038
Bob Wilsonebbc1c42009-06-23 23:59:40 +000039// Force static initialization.
40extern "C" void LLVMInitializeX86Target() { }
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000041
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +000042// No assembler printer by default
43X86TargetMachine::AsmPrinterCtorFn X86TargetMachine::AsmPrinterCtor = 0;
44
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikovf700e682008-07-09 13:20:48 +000046 if (Subtarget.isFlavorIntel())
47 return new X86WinTargetAsmInfo(*this);
48 else
49 switch (Subtarget.TargetType) {
50 case X86Subtarget::isDarwin:
51 return new X86DarwinTargetAsmInfo(*this);
52 case X86Subtarget::isELF:
53 return new X86ELFTargetAsmInfo(*this);
54 case X86Subtarget::isMingw:
55 case X86Subtarget::isCygwin:
56 return new X86COFFTargetAsmInfo(*this);
57 case X86Subtarget::isWindows:
58 return new X86WinTargetAsmInfo(*this);
59 default:
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000060 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikovf700e682008-07-09 13:20:48 +000061 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062}
63
64unsigned X86_32TargetMachine::getJITMatchQuality() {
65#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
66 return 10;
67#endif
68 return 0;
69}
70
71unsigned X86_64TargetMachine::getJITMatchQuality() {
Anton Korobeynikov57348662008-03-23 13:43:47 +000072#if defined(__x86_64__) || defined(_M_AMD64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 return 10;
74#endif
75 return 0;
76}
77
78unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
79 // We strongly match "i[3-9]86-*".
80 std::string TT = M.getTargetTriple();
81 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
82 TT[4] == '-' && TT[1] - '3' < 6)
83 return 20;
84 // If the target triple is something non-X86, we don't match.
85 if (!TT.empty()) return 0;
86
87 if (M.getEndianness() == Module::LittleEndian &&
88 M.getPointerSize() == Module::Pointer32)
89 return 10; // Weak match
90 else if (M.getEndianness() != Module::AnyEndianness ||
91 M.getPointerSize() != Module::AnyPointerSize)
92 return 0; // Match for some other target
93
94 return getJITMatchQuality()/2;
95}
96
97unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
98 // We strongly match "x86_64-*".
99 std::string TT = M.getTargetTriple();
100 if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
101 TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
102 return 20;
103
104 // We strongly match "amd64-*".
105 if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
106 TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
107 return 20;
108
109 // If the target triple is something non-X86-64, we don't match.
110 if (!TT.empty()) return 0;
111
112 if (M.getEndianness() == Module::LittleEndian &&
113 M.getPointerSize() == Module::Pointer64)
114 return 10; // Weak match
115 else if (M.getEndianness() != Module::AnyEndianness ||
116 M.getPointerSize() != Module::AnyPointerSize)
117 return 0; // Match for some other target
118
119 return getJITMatchQuality()/2;
120}
121
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000122X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 : X86TargetMachine(M, FS, false) {
124}
125
126
127X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
128 : X86TargetMachine(M, FS, true) {
129}
130
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000131/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132///
133X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
134 bool is64Bit)
135 : Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +0000136 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 FrameInfo(TargetFrameInfo::StackGrowsDown,
138 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000139 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000140 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000141
142 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000143 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000144 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000146 else if (Subtarget.is64Bit())
147 setRelocationModel(Reloc::PIC_);
148 else
149 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000150 }
Dan Gohman36322c72008-10-18 02:06:02 +0000151
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000152 assert(getRelocationModel() != Reloc::Default &&
153 "Relocation mode not picked");
154
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000155 // If no code model is picked, default to small.
156 if (getCodeModel() == CodeModel::Default)
157 setCodeModel(CodeModel::Small);
158
Chris Lattner4c8b6862009-07-09 03:37:30 +0000159 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000160 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +0000161 // executables but not necessarily a shared library. On X86-32 we just
162 // compile in -static mode, in x86-64 we use PIC.
163 if (getRelocationModel() == Reloc::DynamicNoPIC) {
164 if (is64Bit)
165 setRelocationModel(Reloc::PIC_);
166 else if (!Subtarget.isTargetDarwin())
167 setRelocationModel(Reloc::Static);
168 }
Dan Gohman36322c72008-10-18 02:06:02 +0000169
Chris Lattner4c8b6862009-07-09 03:37:30 +0000170 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
171 // the Mach-O file format doesn't support it.
172 if (getRelocationModel() == Reloc::Static &&
173 Subtarget.isTargetDarwin() &&
174 is64Bit)
175 setRelocationModel(Reloc::PIC_);
176
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000177 // Determine the PICStyle based on the target selected.
178 if (getRelocationModel() == Reloc::Static) {
179 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
180 Subtarget.setPICStyle(PICStyles::None);
181 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000182 Subtarget.setPICStyle(PICStyles::None);
183 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000185 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000187 Subtarget.setPICStyle(PICStyles::Stub);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000188 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000190 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000192 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000193 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000194
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000195 // Finally, if we have "none" as our PIC style, force to static mode.
196 if (Subtarget.getPICStyle() == PICStyles::None)
197 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198}
199
200//===----------------------------------------------------------------------===//
201// Pass Pipeline Configuration
202//===----------------------------------------------------------------------===//
203
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000204bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
205 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000207 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000208
209 // If we're using Fast-ISel, clean up the mess.
210 if (EnableFastISel)
211 PM.add(createDeadMachineInstructionElimPass());
212
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000213 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
214 PM.add(createX87FPRegKillInserterPass());
215
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 return false;
217}
218
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000219bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
220 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000221 // Calculate and set max stack object alignment early, so we can decide
222 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000223 PM.add(createX86MaxStackAlignmentCalculatorPass());
224 return false; // -print-machineinstr shouldn't print after this.
225}
226
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000227bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
228 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 PM.add(createX86FloatingPointStackifierPass());
230 return true; // -print-machineinstr should print after this.
231}
232
Bill Wendling58ed5d22009-04-29 00:15:41 +0000233bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000234 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000235 bool Verbose,
236 raw_ostream &Out) {
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000237 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
238 if (AsmPrinterCtor)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000239 PM.add(AsmPrinterCtor(Out, *this, Verbose));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 return false;
241}
242
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000243bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
244 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000245 bool DumpAsm,
246 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000248 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
249 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000250 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000251 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000252 Subtarget.setPICStyle(PICStyles::None);
253 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254
Dale Johannesenc501c082008-08-11 23:46:25 +0000255 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000256 // On Darwin, use small code model but hack the call instruction for
257 // externals. Elsewhere, do not assume globals are in the lower 4G.
258 if (Subtarget.is64Bit()) {
259 if (Subtarget.isTargetDarwin())
260 setCodeModel(CodeModel::Small);
261 else
262 setCodeModel(CodeModel::Large);
263 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264
265 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000266 if (DumpAsm) {
267 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
268 if (AsmPrinterCtor)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000269 PM.add(AsmPrinterCtor(errs(), *this, true));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000270 }
Evan Cheng77547212007-07-20 21:56:13 +0000271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 return false;
273}
274
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000275bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
276 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000277 bool DumpAsm,
278 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000279 // FIXME: Move this to TargetJITInfo!
280 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
281 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000282 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000283 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000284 Subtarget.setPICStyle(PICStyles::None);
285 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000286
287 // 64-bit JIT places everything in the same buffer except external functions.
288 // On Darwin, use small code model but hack the call instruction for
289 // externals. Elsewhere, do not assume globals are in the lower 4G.
290 if (Subtarget.is64Bit()) {
291 if (Subtarget.isTargetDarwin())
292 setCodeModel(CodeModel::Small);
293 else
294 setCodeModel(CodeModel::Large);
295 }
296
297 PM.add(createX86JITCodeEmitterPass(*this, JCE));
298 if (DumpAsm) {
299 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
300 if (AsmPrinterCtor)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000301 PM.add(AsmPrinterCtor(errs(), *this, true));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000302 }
303
304 return false;
305}
306
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000307bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
308 CodeGenOpt::Level OptLevel,
309 bool DumpAsm,
310 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000311 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
312 if (DumpAsm) {
313 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
314 if (AsmPrinterCtor)
315 PM.add(AsmPrinterCtor(errs(), *this, true));
316 }
317
318 return false;
319}
320
Bill Wendling58ed5d22009-04-29 00:15:41 +0000321bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000322 CodeGenOpt::Level OptLevel,
323 bool DumpAsm,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000324 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000326 if (DumpAsm) {
327 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
328 if (AsmPrinterCtor)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000329 PM.add(AsmPrinterCtor(errs(), *this, true));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000330 }
331
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 return false;
333}
Dan Gohmanc6413362008-09-26 19:15:30 +0000334
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000335bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
336 CodeGenOpt::Level OptLevel,
337 bool DumpAsm,
338 JITCodeEmitter &JCE) {
339 PM.add(createX86JITCodeEmitterPass(*this, JCE));
340 if (DumpAsm) {
341 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
342 if (AsmPrinterCtor)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000343 PM.add(AsmPrinterCtor(errs(), *this, true));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000344 }
345
346 return false;
347}
348
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000349bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
350 CodeGenOpt::Level OptLevel,
351 bool DumpAsm,
352 ObjectCodeEmitter &OCE) {
353 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
354 if (DumpAsm) {
355 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
356 if (AsmPrinterCtor)
357 PM.add(AsmPrinterCtor(errs(), *this, true));
358 }
359
360 return false;
361}