blob: 0152121e531fdbb4aa49e38fab4e948d000026a7 [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
Daniel Dunbarde46a5b2009-11-06 10:58:06 +000025static const MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000026 Triple TheTriple(TT);
27 switch (TheTriple.getOS()) {
28 case Triple::Darwin:
Chris Lattnercd4cab92009-08-22 21:03:30 +000029 return new X86MCAsmInfoDarwin(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000030 case Triple::MinGW32:
31 case Triple::MinGW64:
32 case Triple::Cygwin:
Chris Lattnercd4cab92009-08-22 21:03:30 +000033 return new X86MCAsmInfoCOFF(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000034 case Triple::Win32:
Chris Lattner621c44d2009-08-22 20:48:53 +000035 return new X86WinMCAsmInfo(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000036 default:
Chris Lattner621c44d2009-08-22 20:48:53 +000037 return new X86ELFMCAsmInfo(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000038 }
39}
40
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000041extern "C" void LLVMInitializeX86Target() {
Daniel Dunbarc680b012009-07-25 06:49:55 +000042 // Register the target.
43 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
44 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000045
46 // Register the target asm info.
Chris Lattner621c44d2009-08-22 20:48:53 +000047 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
48 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
Daniel Dunbar2f379632009-08-27 08:12:55 +000049
50 // Register the code emitter.
51 TargetRegistry::RegisterCodeEmitter(TheX86_32Target, createX86MCCodeEmitter);
52 TargetRegistry::RegisterCodeEmitter(TheX86_64Target, createX86MCCodeEmitter);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000053}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000054
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000056X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000057 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000058 : X86TargetMachine(T, TT, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059}
60
61
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000062X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000063 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000064 : X86TargetMachine(T, TT, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065}
66
Chris Lattner5b5e32d2009-07-09 03:32:31 +000067/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068///
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000069X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000070 const std::string &FS, bool is64Bit)
Chris Lattnerd9236ec2009-08-11 20:42:37 +000071 : LLVMTargetMachine(T, TT),
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000072 Subtarget(TT, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +000073 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 FrameInfo(TargetFrameInfo::StackGrowsDown,
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +000075 Subtarget.getStackAlignment(),
76 (Subtarget.isTargetWin64() ? -40 :
77 (Subtarget.is64Bit() ? -8 : -4))),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000078 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +000079 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +000080
81 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000082 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +000083 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +000085 else if (Subtarget.is64Bit())
86 setRelocationModel(Reloc::PIC_);
87 else
88 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000089 }
Dan Gohman36322c72008-10-18 02:06:02 +000090
Chris Lattneraa7c6d22009-07-09 03:15:51 +000091 assert(getRelocationModel() != Reloc::Default &&
92 "Relocation mode not picked");
93
Chris Lattner5b5e32d2009-07-09 03:32:31 +000094 // If no code model is picked, default to small.
95 if (getCodeModel() == CodeModel::Default)
96 setCodeModel(CodeModel::Small);
97
Chris Lattner4c8b6862009-07-09 03:37:30 +000098 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +000099 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +0000100 // executables but not necessarily a shared library. On X86-32 we just
101 // compile in -static mode, in x86-64 we use PIC.
102 if (getRelocationModel() == Reloc::DynamicNoPIC) {
103 if (is64Bit)
104 setRelocationModel(Reloc::PIC_);
105 else if (!Subtarget.isTargetDarwin())
106 setRelocationModel(Reloc::Static);
107 }
Dan Gohman36322c72008-10-18 02:06:02 +0000108
Chris Lattner4c8b6862009-07-09 03:37:30 +0000109 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
110 // the Mach-O file format doesn't support it.
111 if (getRelocationModel() == Reloc::Static &&
112 Subtarget.isTargetDarwin() &&
113 is64Bit)
114 setRelocationModel(Reloc::PIC_);
115
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000116 // Determine the PICStyle based on the target selected.
117 if (getRelocationModel() == Reloc::Static) {
118 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
119 Subtarget.setPICStyle(PICStyles::None);
120 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000121 Subtarget.setPICStyle(PICStyles::None);
122 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000124 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattnerfb502722009-07-10 20:58:47 +0000125 else if (getRelocationModel() == Reloc::PIC_)
126 Subtarget.setPICStyle(PICStyles::StubPIC);
127 else {
128 assert(getRelocationModel() == Reloc::DynamicNoPIC);
129 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
130 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000131 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000133 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000135 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000136 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000137
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000138 // Finally, if we have "none" as our PIC style, force to static mode.
139 if (Subtarget.getPICStyle() == PICStyles::None)
140 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141}
142
143//===----------------------------------------------------------------------===//
144// Pass Pipeline Configuration
145//===----------------------------------------------------------------------===//
146
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000147bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
148 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000150 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000151
152 // If we're using Fast-ISel, clean up the mess.
153 if (EnableFastISel)
154 PM.add(createDeadMachineInstructionElimPass());
155
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000156 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
157 PM.add(createX87FPRegKillInserterPass());
158
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 return false;
160}
161
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000162bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
163 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000164 // Calculate and set max stack object alignment early, so we can decide
165 // whether we will need stack realignment (and thus FP).
Jim Grosbach4d20ee62009-12-02 19:30:24 +0000166 PM.add(createMaxStackAlignmentCalculatorPass());
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000167 return false; // -print-machineinstr shouldn't print after this.
168}
169
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000170bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
171 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 PM.add(createX86FloatingPointStackifierPass());
173 return true; // -print-machineinstr should print after this.
174}
175
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000176bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
177 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000178 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000180 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
181 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000182 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000183 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000184 Subtarget.setPICStyle(PICStyles::None);
185 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
Dale Johannesenc501c082008-08-11 23:46:25 +0000187 // 64-bit JIT places everything in the same buffer except external functions.
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +0000188 if (Subtarget.is64Bit())
Dale Johannesen58c6d512008-08-12 21:02:08 +0000189 setCodeModel(CodeModel::Large);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190
191 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000192
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 return false;
194}
195
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000196bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
197 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000198 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000199 // FIXME: Move this to TargetJITInfo!
200 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
201 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000202 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000203 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000204 Subtarget.setPICStyle(PICStyles::None);
205 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000206
207 // 64-bit JIT places everything in the same buffer except external functions.
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +0000208 if (Subtarget.is64Bit())
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000209 setCodeModel(CodeModel::Large);
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000210
211 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000212
213 return false;
214}
215
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000216bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
217 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000218 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000219 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000220 return false;
221}
222
Bill Wendling58ed5d22009-04-29 00:15:41 +0000223bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000224 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000225 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 PM.add(createX86CodeEmitterPass(*this, MCE));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 return false;
228}
Dan Gohmanc6413362008-09-26 19:15:30 +0000229
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000230bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
231 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000232 JITCodeEmitter &JCE) {
233 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000234 return false;
235}
236
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000237bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
238 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000239 ObjectCodeEmitter &OCE) {
240 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000241 return false;
242}