blob: ee0daed167c68ee2f0bf8ecd930cd6634324dd15 [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"
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 Dunbarfe5939f2009-07-15 20:24:03 +000026extern "C" void LLVMInitializeX86Target() {
Daniel Dunbarc680b012009-07-25 06:49:55 +000027 // Register the target.
28 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
29 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000030}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000031
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikovf700e682008-07-09 13:20:48 +000033 if (Subtarget.isFlavorIntel())
34 return new X86WinTargetAsmInfo(*this);
Chris Lattnerc4c40a92009-07-28 03:13:23 +000035 switch (Subtarget.TargetType) {
36 default: llvm_unreachable("unknown subtarget type");
37 case X86Subtarget::isDarwin:
38 return new X86DarwinTargetAsmInfo(*this);
39 case X86Subtarget::isELF:
40 return new X86ELFTargetAsmInfo(*this);
41 case X86Subtarget::isMingw:
42 case X86Subtarget::isCygwin:
43 return new X86COFFTargetAsmInfo(*this);
44 case X86Subtarget::isWindows:
45 return new X86WinTargetAsmInfo(*this);
46 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047}
48
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000049X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
50 const std::string &FS)
51 : X86TargetMachine(T, M, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052}
53
54
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000055X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
56 const std::string &FS)
57 : X86TargetMachine(T, M, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058}
59
Chris Lattner5b5e32d2009-07-09 03:32:31 +000060/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061///
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000062X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
63 const std::string &FS, bool is64Bit)
64 : LLVMTargetMachine(T),
65 Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +000066 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 FrameInfo(TargetFrameInfo::StackGrowsDown,
68 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000069 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +000070 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +000071
72 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000073 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +000074 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +000076 else if (Subtarget.is64Bit())
77 setRelocationModel(Reloc::PIC_);
78 else
79 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000080 }
Dan Gohman36322c72008-10-18 02:06:02 +000081
Chris Lattneraa7c6d22009-07-09 03:15:51 +000082 assert(getRelocationModel() != Reloc::Default &&
83 "Relocation mode not picked");
84
Chris Lattner5b5e32d2009-07-09 03:32:31 +000085 // If no code model is picked, default to small.
86 if (getCodeModel() == CodeModel::Default)
87 setCodeModel(CodeModel::Small);
88
Chris Lattner4c8b6862009-07-09 03:37:30 +000089 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +000090 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +000091 // executables but not necessarily a shared library. On X86-32 we just
92 // compile in -static mode, in x86-64 we use PIC.
93 if (getRelocationModel() == Reloc::DynamicNoPIC) {
94 if (is64Bit)
95 setRelocationModel(Reloc::PIC_);
96 else if (!Subtarget.isTargetDarwin())
97 setRelocationModel(Reloc::Static);
98 }
Dan Gohman36322c72008-10-18 02:06:02 +000099
Chris Lattner4c8b6862009-07-09 03:37:30 +0000100 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
101 // the Mach-O file format doesn't support it.
102 if (getRelocationModel() == Reloc::Static &&
103 Subtarget.isTargetDarwin() &&
104 is64Bit)
105 setRelocationModel(Reloc::PIC_);
106
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000107 // Determine the PICStyle based on the target selected.
108 if (getRelocationModel() == Reloc::Static) {
109 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
110 Subtarget.setPICStyle(PICStyles::None);
111 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000112 Subtarget.setPICStyle(PICStyles::None);
113 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000115 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattnerfb502722009-07-10 20:58:47 +0000116 else if (getRelocationModel() == Reloc::PIC_)
117 Subtarget.setPICStyle(PICStyles::StubPIC);
118 else {
119 assert(getRelocationModel() == Reloc::DynamicNoPIC);
120 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
121 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000122 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000124 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000126 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000127 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000128
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000129 // Finally, if we have "none" as our PIC style, force to static mode.
130 if (Subtarget.getPICStyle() == PICStyles::None)
131 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132}
133
134//===----------------------------------------------------------------------===//
135// Pass Pipeline Configuration
136//===----------------------------------------------------------------------===//
137
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000138bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
139 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000141 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000142
143 // If we're using Fast-ISel, clean up the mess.
144 if (EnableFastISel)
145 PM.add(createDeadMachineInstructionElimPass());
146
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000147 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
148 PM.add(createX87FPRegKillInserterPass());
149
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 return false;
151}
152
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000153bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
154 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000155 // Calculate and set max stack object alignment early, so we can decide
156 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000157 PM.add(createX86MaxStackAlignmentCalculatorPass());
158 return false; // -print-machineinstr shouldn't print after this.
159}
160
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000161bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
162 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 PM.add(createX86FloatingPointStackifierPass());
164 return true; // -print-machineinstr should print after this.
165}
166
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000167bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
168 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000169 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000171 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
172 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000173 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000174 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000175 Subtarget.setPICStyle(PICStyles::None);
176 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Dale Johannesenc501c082008-08-11 23:46:25 +0000178 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000179 // On Darwin, use small code model but hack the call instruction for
180 // externals. Elsewhere, do not assume globals are in the lower 4G.
181 if (Subtarget.is64Bit()) {
182 if (Subtarget.isTargetDarwin())
183 setCodeModel(CodeModel::Small);
184 else
185 setCodeModel(CodeModel::Large);
186 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187
188 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000189
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 return false;
191}
192
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000193bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
194 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000195 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000196 // FIXME: Move this to TargetJITInfo!
197 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
198 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000199 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000200 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000201 Subtarget.setPICStyle(PICStyles::None);
202 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000203
204 // 64-bit JIT places everything in the same buffer except external functions.
205 // On Darwin, use small code model but hack the call instruction for
206 // externals. Elsewhere, do not assume globals are in the lower 4G.
207 if (Subtarget.is64Bit()) {
208 if (Subtarget.isTargetDarwin())
209 setCodeModel(CodeModel::Small);
210 else
211 setCodeModel(CodeModel::Large);
212 }
213
214 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000215
216 return false;
217}
218
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000219bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
220 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000221 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000222 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000223 return false;
224}
225
Bill Wendling58ed5d22009-04-29 00:15:41 +0000226bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000227 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000228 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 PM.add(createX86CodeEmitterPass(*this, MCE));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 return false;
231}
Dan Gohmanc6413362008-09-26 19:15:30 +0000232
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000233bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
234 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000235 JITCodeEmitter &JCE) {
236 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000237 return false;
238}
239
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000240bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
241 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000242 ObjectCodeEmitter &OCE) {
243 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000244 return false;
245}