blob: fa3ec6904dea114239e7910af5d21c2afb7b45d3 [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"
23#include "llvm/Target/TargetMachineRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
Dan Gohman089efff2008-05-13 00:00:25 +000026// Register the target.
27static RegisterTarget<X86_32TargetMachine>
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000028X(TheX86_32Target, "x86", "32-bit X86: Pentium-Pro and above");
29
Dan Gohman089efff2008-05-13 00:00:25 +000030static RegisterTarget<X86_64TargetMachine>
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000031Y(TheX86_64Target, "x86-64", "64-bit X86: EM64T and AMD64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032
Bob Wilsonebbc1c42009-06-23 23:59:40 +000033// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000034extern "C" void LLVMInitializeX86Target() {
35
36}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000037
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikovf700e682008-07-09 13:20:48 +000039 if (Subtarget.isFlavorIntel())
40 return new X86WinTargetAsmInfo(*this);
41 else
42 switch (Subtarget.TargetType) {
43 case X86Subtarget::isDarwin:
44 return new X86DarwinTargetAsmInfo(*this);
45 case X86Subtarget::isELF:
46 return new X86ELFTargetAsmInfo(*this);
47 case X86Subtarget::isMingw:
48 case X86Subtarget::isCygwin:
49 return new X86COFFTargetAsmInfo(*this);
50 case X86Subtarget::isWindows:
51 return new X86WinTargetAsmInfo(*this);
52 default:
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000053 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikovf700e682008-07-09 13:20:48 +000054 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055}
56
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000057X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
58 const std::string &FS)
59 : X86TargetMachine(T, M, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060}
61
62
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000063X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
64 const std::string &FS)
65 : X86TargetMachine(T, M, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066}
67
Chris Lattner5b5e32d2009-07-09 03:32:31 +000068/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069///
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000070X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
71 const std::string &FS, bool is64Bit)
72 : LLVMTargetMachine(T),
73 Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +000074 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 FrameInfo(TargetFrameInfo::StackGrowsDown,
76 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000077 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +000078 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +000079
80 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000081 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +000082 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +000084 else if (Subtarget.is64Bit())
85 setRelocationModel(Reloc::PIC_);
86 else
87 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000088 }
Dan Gohman36322c72008-10-18 02:06:02 +000089
Chris Lattneraa7c6d22009-07-09 03:15:51 +000090 assert(getRelocationModel() != Reloc::Default &&
91 "Relocation mode not picked");
92
Chris Lattner5b5e32d2009-07-09 03:32:31 +000093 // If no code model is picked, default to small.
94 if (getCodeModel() == CodeModel::Default)
95 setCodeModel(CodeModel::Small);
96
Chris Lattner4c8b6862009-07-09 03:37:30 +000097 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +000098 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +000099 // executables but not necessarily a shared library. On X86-32 we just
100 // compile in -static mode, in x86-64 we use PIC.
101 if (getRelocationModel() == Reloc::DynamicNoPIC) {
102 if (is64Bit)
103 setRelocationModel(Reloc::PIC_);
104 else if (!Subtarget.isTargetDarwin())
105 setRelocationModel(Reloc::Static);
106 }
Dan Gohman36322c72008-10-18 02:06:02 +0000107
Chris Lattner4c8b6862009-07-09 03:37:30 +0000108 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
109 // the Mach-O file format doesn't support it.
110 if (getRelocationModel() == Reloc::Static &&
111 Subtarget.isTargetDarwin() &&
112 is64Bit)
113 setRelocationModel(Reloc::PIC_);
114
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000115 // Determine the PICStyle based on the target selected.
116 if (getRelocationModel() == Reloc::Static) {
117 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
118 Subtarget.setPICStyle(PICStyles::None);
119 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000120 Subtarget.setPICStyle(PICStyles::None);
121 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000123 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattnerfb502722009-07-10 20:58:47 +0000124 else if (getRelocationModel() == Reloc::PIC_)
125 Subtarget.setPICStyle(PICStyles::StubPIC);
126 else {
127 assert(getRelocationModel() == Reloc::DynamicNoPIC);
128 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
129 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000130 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000132 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000134 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000135 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000136
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000137 // Finally, if we have "none" as our PIC style, force to static mode.
138 if (Subtarget.getPICStyle() == PICStyles::None)
139 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140}
141
142//===----------------------------------------------------------------------===//
143// Pass Pipeline Configuration
144//===----------------------------------------------------------------------===//
145
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000146bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
147 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000149 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000150
151 // If we're using Fast-ISel, clean up the mess.
152 if (EnableFastISel)
153 PM.add(createDeadMachineInstructionElimPass());
154
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000155 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
156 PM.add(createX87FPRegKillInserterPass());
157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 return false;
159}
160
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000161bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
162 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000163 // Calculate and set max stack object alignment early, so we can decide
164 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000165 PM.add(createX86MaxStackAlignmentCalculatorPass());
166 return false; // -print-machineinstr shouldn't print after this.
167}
168
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000169bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
170 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 PM.add(createX86FloatingPointStackifierPass());
172 return true; // -print-machineinstr should print after this.
173}
174
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000175bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
176 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000177 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000179 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
180 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000181 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000182 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000183 Subtarget.setPICStyle(PICStyles::None);
184 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185
Dale Johannesenc501c082008-08-11 23:46:25 +0000186 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000187 // On Darwin, use small code model but hack the call instruction for
188 // externals. Elsewhere, do not assume globals are in the lower 4G.
189 if (Subtarget.is64Bit()) {
190 if (Subtarget.isTargetDarwin())
191 setCodeModel(CodeModel::Small);
192 else
193 setCodeModel(CodeModel::Large);
194 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195
196 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000197
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 return false;
199}
200
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000201bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
202 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000203 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000204 // FIXME: Move this to TargetJITInfo!
205 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
206 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000207 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000208 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000209 Subtarget.setPICStyle(PICStyles::None);
210 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000211
212 // 64-bit JIT places everything in the same buffer except external functions.
213 // On Darwin, use small code model but hack the call instruction for
214 // externals. Elsewhere, do not assume globals are in the lower 4G.
215 if (Subtarget.is64Bit()) {
216 if (Subtarget.isTargetDarwin())
217 setCodeModel(CodeModel::Small);
218 else
219 setCodeModel(CodeModel::Large);
220 }
221
222 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000223
224 return false;
225}
226
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000227bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
228 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000229 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000230 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000231 return false;
232}
233
Bill Wendling58ed5d22009-04-29 00:15:41 +0000234bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000235 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000236 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 PM.add(createX86CodeEmitterPass(*this, MCE));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 return false;
239}
Dan Gohmanc6413362008-09-26 19:15:30 +0000240
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000241bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
242 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000243 JITCodeEmitter &JCE) {
244 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000245 return false;
246}
247
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000248bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
249 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000250 ObjectCodeEmitter &OCE) {
251 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000252 return false;
253}