blob: 704c7d3a88c01714e1e59e7ac64dedbfb562a869 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/PassManager.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/Passes.h"
David Greene302008d2009-07-14 20:18:05 +000020#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetOptions.h"
Daniel Dunbarc680b012009-07-25 06:49:55 +000022#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023using namespace llvm;
24
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000025extern "C" void LLVMInitializeX86Target() {
Daniel Dunbarc680b012009-07-25 06:49:55 +000026 // Register the target.
27 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
28 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000029}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000030
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikovf700e682008-07-09 13:20:48 +000032 if (Subtarget.isFlavorIntel())
33 return new X86WinTargetAsmInfo(*this);
Chris Lattnerc4c40a92009-07-28 03:13:23 +000034 switch (Subtarget.TargetType) {
35 default: llvm_unreachable("unknown subtarget type");
36 case X86Subtarget::isDarwin:
37 return new X86DarwinTargetAsmInfo(*this);
38 case X86Subtarget::isELF:
39 return new X86ELFTargetAsmInfo(*this);
40 case X86Subtarget::isMingw:
41 case X86Subtarget::isCygwin:
42 return new X86COFFTargetAsmInfo(*this);
43 case X86Subtarget::isWindows:
44 return new X86WinTargetAsmInfo(*this);
45 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046}
47
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000048X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000049 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000050 : X86TargetMachine(T, TT, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051}
52
53
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000054X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000055 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000056 : X86TargetMachine(T, TT, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057}
58
Chris Lattner5b5e32d2009-07-09 03:32:31 +000059/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060///
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000061X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000062 const std::string &FS, bool is64Bit)
63 : LLVMTargetMachine(T),
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000064 Subtarget(TT, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +000065 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 FrameInfo(TargetFrameInfo::StackGrowsDown,
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +000067 Subtarget.getStackAlignment(),
68 (Subtarget.isTargetWin64() ? -40 :
69 (Subtarget.is64Bit() ? -8 : -4))),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000070 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +000071 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +000072
73 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000074 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +000075 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +000077 else if (Subtarget.is64Bit())
78 setRelocationModel(Reloc::PIC_);
79 else
80 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000081 }
Dan Gohman36322c72008-10-18 02:06:02 +000082
Chris Lattneraa7c6d22009-07-09 03:15:51 +000083 assert(getRelocationModel() != Reloc::Default &&
84 "Relocation mode not picked");
85
Chris Lattner5b5e32d2009-07-09 03:32:31 +000086 // If no code model is picked, default to small.
87 if (getCodeModel() == CodeModel::Default)
88 setCodeModel(CodeModel::Small);
89
Chris Lattner4c8b6862009-07-09 03:37:30 +000090 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +000091 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +000092 // executables but not necessarily a shared library. On X86-32 we just
93 // compile in -static mode, in x86-64 we use PIC.
94 if (getRelocationModel() == Reloc::DynamicNoPIC) {
95 if (is64Bit)
96 setRelocationModel(Reloc::PIC_);
97 else if (!Subtarget.isTargetDarwin())
98 setRelocationModel(Reloc::Static);
99 }
Dan Gohman36322c72008-10-18 02:06:02 +0000100
Chris Lattner4c8b6862009-07-09 03:37:30 +0000101 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
102 // the Mach-O file format doesn't support it.
103 if (getRelocationModel() == Reloc::Static &&
104 Subtarget.isTargetDarwin() &&
105 is64Bit)
106 setRelocationModel(Reloc::PIC_);
107
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000108 // Determine the PICStyle based on the target selected.
109 if (getRelocationModel() == Reloc::Static) {
110 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
111 Subtarget.setPICStyle(PICStyles::None);
112 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000113 Subtarget.setPICStyle(PICStyles::None);
114 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000116 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattnerfb502722009-07-10 20:58:47 +0000117 else if (getRelocationModel() == Reloc::PIC_)
118 Subtarget.setPICStyle(PICStyles::StubPIC);
119 else {
120 assert(getRelocationModel() == Reloc::DynamicNoPIC);
121 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
122 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000123 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000125 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000127 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000128 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000129
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000130 // Finally, if we have "none" as our PIC style, force to static mode.
131 if (Subtarget.getPICStyle() == PICStyles::None)
132 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133}
134
135//===----------------------------------------------------------------------===//
136// Pass Pipeline Configuration
137//===----------------------------------------------------------------------===//
138
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000139bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
140 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000142 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000143
144 // If we're using Fast-ISel, clean up the mess.
145 if (EnableFastISel)
146 PM.add(createDeadMachineInstructionElimPass());
147
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000148 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
149 PM.add(createX87FPRegKillInserterPass());
150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 return false;
152}
153
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000154bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
155 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000156 // Calculate and set max stack object alignment early, so we can decide
157 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000158 PM.add(createX86MaxStackAlignmentCalculatorPass());
159 return false; // -print-machineinstr shouldn't print after this.
160}
161
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000162bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
163 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 PM.add(createX86FloatingPointStackifierPass());
165 return true; // -print-machineinstr should print after this.
166}
167
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000168bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
169 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000170 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000172 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
173 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000174 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000175 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000176 Subtarget.setPICStyle(PICStyles::None);
177 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178
Dale Johannesenc501c082008-08-11 23:46:25 +0000179 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000180 // On Darwin, use small code model but hack the call instruction for
181 // externals. Elsewhere, do not assume globals are in the lower 4G.
182 if (Subtarget.is64Bit()) {
183 if (Subtarget.isTargetDarwin())
184 setCodeModel(CodeModel::Small);
185 else
186 setCodeModel(CodeModel::Large);
187 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188
189 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000190
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 return false;
192}
193
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000194bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
195 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000196 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000197 // FIXME: Move this to TargetJITInfo!
198 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
199 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000200 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000201 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000202 Subtarget.setPICStyle(PICStyles::None);
203 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000204
205 // 64-bit JIT places everything in the same buffer except external functions.
206 // On Darwin, use small code model but hack the call instruction for
207 // externals. Elsewhere, do not assume globals are in the lower 4G.
208 if (Subtarget.is64Bit()) {
209 if (Subtarget.isTargetDarwin())
210 setCodeModel(CodeModel::Small);
211 else
212 setCodeModel(CodeModel::Large);
213 }
214
215 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000216
217 return false;
218}
219
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000220bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
221 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000222 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000223 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000224 return false;
225}
226
Bill Wendling58ed5d22009-04-29 00:15:41 +0000227bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000228 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000229 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 PM.add(createX86CodeEmitterPass(*this, MCE));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 return false;
232}
Dan Gohmanc6413362008-09-26 19:15:30 +0000233
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000234bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
235 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000236 JITCodeEmitter &JCE) {
237 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000238 return false;
239}
240
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000241bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
242 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000243 ObjectCodeEmitter &OCE) {
244 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000245 return false;
246}