blob: 4651f462a5df98854fac05c1402d1761f05d23fe [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
Chris Lattner621c44d2009-08-22 20:48:53 +000014#include "X86MCAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#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
Chris Lattner621c44d2009-08-22 20:48:53 +000025static const MCAsmInfo *createMCAsmInfo(const Target &T,
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000026 const StringRef &TT) {
27 Triple TheTriple(TT);
28 switch (TheTriple.getOS()) {
29 case Triple::Darwin:
Chris Lattnercd4cab92009-08-22 21:03:30 +000030 return new X86MCAsmInfoDarwin(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000031 case Triple::MinGW32:
32 case Triple::MinGW64:
33 case Triple::Cygwin:
Chris Lattnercd4cab92009-08-22 21:03:30 +000034 return new X86MCAsmInfoCOFF(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000035 case Triple::Win32:
Chris Lattner621c44d2009-08-22 20:48:53 +000036 return new X86WinMCAsmInfo(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000037 default:
Chris Lattner621c44d2009-08-22 20:48:53 +000038 return new X86ELFMCAsmInfo(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000039 }
40}
41
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000042extern "C" void LLVMInitializeX86Target() {
Daniel Dunbarc680b012009-07-25 06:49:55 +000043 // Register the target.
44 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
45 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000046
47 // Register the target asm info.
Chris Lattner621c44d2009-08-22 20:48:53 +000048 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
49 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000050}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000051
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000053X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000054 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000055 : X86TargetMachine(T, TT, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056}
57
58
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000059X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000060 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000061 : X86TargetMachine(T, TT, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062}
63
Chris Lattner5b5e32d2009-07-09 03:32:31 +000064/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065///
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000066X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000067 const std::string &FS, bool is64Bit)
Chris Lattnerd9236ec2009-08-11 20:42:37 +000068 : LLVMTargetMachine(T, TT),
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000069 Subtarget(TT, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +000070 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 FrameInfo(TargetFrameInfo::StackGrowsDown,
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +000072 Subtarget.getStackAlignment(),
73 (Subtarget.isTargetWin64() ? -40 :
74 (Subtarget.is64Bit() ? -8 : -4))),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000075 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +000076 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +000077
78 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000079 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +000080 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +000082 else if (Subtarget.is64Bit())
83 setRelocationModel(Reloc::PIC_);
84 else
85 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000086 }
Dan Gohman36322c72008-10-18 02:06:02 +000087
Chris Lattneraa7c6d22009-07-09 03:15:51 +000088 assert(getRelocationModel() != Reloc::Default &&
89 "Relocation mode not picked");
90
Chris Lattner5b5e32d2009-07-09 03:32:31 +000091 // If no code model is picked, default to small.
92 if (getCodeModel() == CodeModel::Default)
93 setCodeModel(CodeModel::Small);
94
Chris Lattner4c8b6862009-07-09 03:37:30 +000095 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +000096 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +000097 // executables but not necessarily a shared library. On X86-32 we just
98 // compile in -static mode, in x86-64 we use PIC.
99 if (getRelocationModel() == Reloc::DynamicNoPIC) {
100 if (is64Bit)
101 setRelocationModel(Reloc::PIC_);
102 else if (!Subtarget.isTargetDarwin())
103 setRelocationModel(Reloc::Static);
104 }
Dan Gohman36322c72008-10-18 02:06:02 +0000105
Chris Lattner4c8b6862009-07-09 03:37:30 +0000106 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
107 // the Mach-O file format doesn't support it.
108 if (getRelocationModel() == Reloc::Static &&
109 Subtarget.isTargetDarwin() &&
110 is64Bit)
111 setRelocationModel(Reloc::PIC_);
112
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000113 // Determine the PICStyle based on the target selected.
114 if (getRelocationModel() == Reloc::Static) {
115 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
116 Subtarget.setPICStyle(PICStyles::None);
117 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000118 Subtarget.setPICStyle(PICStyles::None);
119 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000121 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattnerfb502722009-07-10 20:58:47 +0000122 else if (getRelocationModel() == Reloc::PIC_)
123 Subtarget.setPICStyle(PICStyles::StubPIC);
124 else {
125 assert(getRelocationModel() == Reloc::DynamicNoPIC);
126 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
127 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000128 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000130 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000132 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000133 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000134
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000135 // Finally, if we have "none" as our PIC style, force to static mode.
136 if (Subtarget.getPICStyle() == PICStyles::None)
137 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138}
139
140//===----------------------------------------------------------------------===//
141// Pass Pipeline Configuration
142//===----------------------------------------------------------------------===//
143
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000144bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
145 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000147 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000148
149 // If we're using Fast-ISel, clean up the mess.
150 if (EnableFastISel)
151 PM.add(createDeadMachineInstructionElimPass());
152
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000153 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
154 PM.add(createX87FPRegKillInserterPass());
155
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 return false;
157}
158
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000159bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
160 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000161 // Calculate and set max stack object alignment early, so we can decide
162 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000163 PM.add(createX86MaxStackAlignmentCalculatorPass());
164 return false; // -print-machineinstr shouldn't print after this.
165}
166
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000167bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
168 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 PM.add(createX86FloatingPointStackifierPass());
170 return true; // -print-machineinstr should print after this.
171}
172
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000173bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
174 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000175 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000177 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
178 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000179 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000180 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000181 Subtarget.setPICStyle(PICStyles::None);
182 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183
Dale Johannesenc501c082008-08-11 23:46:25 +0000184 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000185 // On Darwin, use small code model but hack the call instruction for
186 // externals. Elsewhere, do not assume globals are in the lower 4G.
187 if (Subtarget.is64Bit()) {
188 if (Subtarget.isTargetDarwin())
189 setCodeModel(CodeModel::Small);
190 else
191 setCodeModel(CodeModel::Large);
192 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193
194 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 return false;
197}
198
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000199bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
200 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000201 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000202 // FIXME: Move this to TargetJITInfo!
203 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
204 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000205 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000206 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000207 Subtarget.setPICStyle(PICStyles::None);
208 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000209
210 // 64-bit JIT places everything in the same buffer except external functions.
211 // On Darwin, use small code model but hack the call instruction for
212 // externals. Elsewhere, do not assume globals are in the lower 4G.
213 if (Subtarget.is64Bit()) {
214 if (Subtarget.isTargetDarwin())
215 setCodeModel(CodeModel::Small);
216 else
217 setCodeModel(CodeModel::Large);
218 }
219
220 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000221
222 return false;
223}
224
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000225bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
226 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000227 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000228 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000229 return false;
230}
231
Bill Wendling58ed5d22009-04-29 00:15:41 +0000232bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000233 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000234 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 PM.add(createX86CodeEmitterPass(*this, MCE));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 return false;
237}
Dan Gohmanc6413362008-09-26 19:15:30 +0000238
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000239bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
240 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000241 JITCodeEmitter &JCE) {
242 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000243 return false;
244}
245
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000246bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
247 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000248 ObjectCodeEmitter &OCE) {
249 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000250 return false;
251}