blob: 5719a35318c30e36c576178a2e46fff4a1d9a79c [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 {
Chris Lattnerc4c40a92009-07-28 03:13:23 +000032 switch (Subtarget.TargetType) {
33 default: llvm_unreachable("unknown subtarget type");
34 case X86Subtarget::isDarwin:
35 return new X86DarwinTargetAsmInfo(*this);
36 case X86Subtarget::isELF:
37 return new X86ELFTargetAsmInfo(*this);
38 case X86Subtarget::isMingw:
39 case X86Subtarget::isCygwin:
40 return new X86COFFTargetAsmInfo(*this);
41 case X86Subtarget::isWindows:
42 return new X86WinTargetAsmInfo(*this);
43 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044}
45
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000046X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000047 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000048 : X86TargetMachine(T, TT, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049}
50
51
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000052X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000053 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000054 : X86TargetMachine(T, TT, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055}
56
Chris Lattner5b5e32d2009-07-09 03:32:31 +000057/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058///
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000059X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000060 const std::string &FS, bool is64Bit)
61 : LLVMTargetMachine(T),
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000062 Subtarget(TT, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +000063 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 FrameInfo(TargetFrameInfo::StackGrowsDown,
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +000065 Subtarget.getStackAlignment(),
66 (Subtarget.isTargetWin64() ? -40 :
67 (Subtarget.is64Bit() ? -8 : -4))),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000068 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +000069 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +000070
71 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000072 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +000073 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +000075 else if (Subtarget.is64Bit())
76 setRelocationModel(Reloc::PIC_);
77 else
78 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000079 }
Dan Gohman36322c72008-10-18 02:06:02 +000080
Chris Lattneraa7c6d22009-07-09 03:15:51 +000081 assert(getRelocationModel() != Reloc::Default &&
82 "Relocation mode not picked");
83
Chris Lattner5b5e32d2009-07-09 03:32:31 +000084 // If no code model is picked, default to small.
85 if (getCodeModel() == CodeModel::Default)
86 setCodeModel(CodeModel::Small);
87
Chris Lattner4c8b6862009-07-09 03:37:30 +000088 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +000089 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +000090 // executables but not necessarily a shared library. On X86-32 we just
91 // compile in -static mode, in x86-64 we use PIC.
92 if (getRelocationModel() == Reloc::DynamicNoPIC) {
93 if (is64Bit)
94 setRelocationModel(Reloc::PIC_);
95 else if (!Subtarget.isTargetDarwin())
96 setRelocationModel(Reloc::Static);
97 }
Dan Gohman36322c72008-10-18 02:06:02 +000098
Chris Lattner4c8b6862009-07-09 03:37:30 +000099 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
100 // the Mach-O file format doesn't support it.
101 if (getRelocationModel() == Reloc::Static &&
102 Subtarget.isTargetDarwin() &&
103 is64Bit)
104 setRelocationModel(Reloc::PIC_);
105
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000106 // Determine the PICStyle based on the target selected.
107 if (getRelocationModel() == Reloc::Static) {
108 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
109 Subtarget.setPICStyle(PICStyles::None);
110 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000111 Subtarget.setPICStyle(PICStyles::None);
112 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000114 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattnerfb502722009-07-10 20:58:47 +0000115 else if (getRelocationModel() == Reloc::PIC_)
116 Subtarget.setPICStyle(PICStyles::StubPIC);
117 else {
118 assert(getRelocationModel() == Reloc::DynamicNoPIC);
119 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
120 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000121 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000123 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000125 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000126 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000127
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000128 // Finally, if we have "none" as our PIC style, force to static mode.
129 if (Subtarget.getPICStyle() == PICStyles::None)
130 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131}
132
133//===----------------------------------------------------------------------===//
134// Pass Pipeline Configuration
135//===----------------------------------------------------------------------===//
136
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000137bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
138 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000140 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000141
142 // If we're using Fast-ISel, clean up the mess.
143 if (EnableFastISel)
144 PM.add(createDeadMachineInstructionElimPass());
145
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000146 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
147 PM.add(createX87FPRegKillInserterPass());
148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 return false;
150}
151
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000152bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
153 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000154 // Calculate and set max stack object alignment early, so we can decide
155 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000156 PM.add(createX86MaxStackAlignmentCalculatorPass());
157 return false; // -print-machineinstr shouldn't print after this.
158}
159
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000160bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
161 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 PM.add(createX86FloatingPointStackifierPass());
163 return true; // -print-machineinstr should print after this.
164}
165
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000166bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
167 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000168 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000170 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
171 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000172 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000173 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000174 Subtarget.setPICStyle(PICStyles::None);
175 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176
Dale Johannesenc501c082008-08-11 23:46:25 +0000177 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000178 // On Darwin, use small code model but hack the call instruction for
179 // externals. Elsewhere, do not assume globals are in the lower 4G.
180 if (Subtarget.is64Bit()) {
181 if (Subtarget.isTargetDarwin())
182 setCodeModel(CodeModel::Small);
183 else
184 setCodeModel(CodeModel::Large);
185 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
187 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000188
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 return false;
190}
191
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000192bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
193 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000194 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000195 // FIXME: Move this to TargetJITInfo!
196 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
197 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000198 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000199 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000200 Subtarget.setPICStyle(PICStyles::None);
201 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000202
203 // 64-bit JIT places everything in the same buffer except external functions.
204 // On Darwin, use small code model but hack the call instruction for
205 // externals. Elsewhere, do not assume globals are in the lower 4G.
206 if (Subtarget.is64Bit()) {
207 if (Subtarget.isTargetDarwin())
208 setCodeModel(CodeModel::Small);
209 else
210 setCodeModel(CodeModel::Large);
211 }
212
213 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000214
215 return false;
216}
217
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000218bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
219 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000220 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000221 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000222 return false;
223}
224
Bill Wendling58ed5d22009-04-29 00:15:41 +0000225bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000226 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000227 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 PM.add(createX86CodeEmitterPass(*this, MCE));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 return false;
230}
Dan Gohmanc6413362008-09-26 19:15:30 +0000231
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000232bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
233 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000234 JITCodeEmitter &JCE) {
235 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000236 return false;
237}
238
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000239bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
240 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000241 ObjectCodeEmitter &OCE) {
242 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000243 return false;
244}