blob: a720e6e894d4abf272f25fcb7cce32e7a83deacc [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"
Daniel Dunbarc680b012009-07-25 06:49:55 +000023#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000026extern "C" void LLVMInitializeX86Target() {
Daniel Dunbarc680b012009-07-25 06:49:55 +000027 // Register the target.
28 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
29 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000030}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000031
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikovf700e682008-07-09 13:20:48 +000033 if (Subtarget.isFlavorIntel())
34 return new X86WinTargetAsmInfo(*this);
35 else
36 switch (Subtarget.TargetType) {
37 case X86Subtarget::isDarwin:
38 return new X86DarwinTargetAsmInfo(*this);
39 case X86Subtarget::isELF:
40 return new X86ELFTargetAsmInfo(*this);
41 case X86Subtarget::isMingw:
42 case X86Subtarget::isCygwin:
43 return new X86COFFTargetAsmInfo(*this);
44 case X86Subtarget::isWindows:
45 return new X86WinTargetAsmInfo(*this);
46 default:
Anton Korobeynikov3829e8a2008-09-25 21:00:33 +000047 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikovf700e682008-07-09 13:20:48 +000048 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049}
50
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000051X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
52 const std::string &FS)
53 : X86TargetMachine(T, M, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054}
55
56
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000057X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
58 const std::string &FS)
59 : X86TargetMachine(T, M, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060}
61
Chris Lattner5b5e32d2009-07-09 03:32:31 +000062/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063///
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000064X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
65 const std::string &FS, bool is64Bit)
66 : LLVMTargetMachine(T),
67 Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +000068 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 FrameInfo(TargetFrameInfo::StackGrowsDown,
70 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000071 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +000072 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +000073
74 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000075 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +000076 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +000078 else if (Subtarget.is64Bit())
79 setRelocationModel(Reloc::PIC_);
80 else
81 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000082 }
Dan Gohman36322c72008-10-18 02:06:02 +000083
Chris Lattneraa7c6d22009-07-09 03:15:51 +000084 assert(getRelocationModel() != Reloc::Default &&
85 "Relocation mode not picked");
86
Chris Lattner5b5e32d2009-07-09 03:32:31 +000087 // If no code model is picked, default to small.
88 if (getCodeModel() == CodeModel::Default)
89 setCodeModel(CodeModel::Small);
90
Chris Lattner4c8b6862009-07-09 03:37:30 +000091 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner5b5e32d2009-07-09 03:32:31 +000092 // is defined as a model for code which may be used in static or dynamic
Chris Lattnerf165d342009-07-09 04:24:46 +000093 // executables but not necessarily a shared library. On X86-32 we just
94 // compile in -static mode, in x86-64 we use PIC.
95 if (getRelocationModel() == Reloc::DynamicNoPIC) {
96 if (is64Bit)
97 setRelocationModel(Reloc::PIC_);
98 else if (!Subtarget.isTargetDarwin())
99 setRelocationModel(Reloc::Static);
100 }
Dan Gohman36322c72008-10-18 02:06:02 +0000101
Chris Lattner4c8b6862009-07-09 03:37:30 +0000102 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
103 // the Mach-O file format doesn't support it.
104 if (getRelocationModel() == Reloc::Static &&
105 Subtarget.isTargetDarwin() &&
106 is64Bit)
107 setRelocationModel(Reloc::PIC_);
108
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000109 // Determine the PICStyle based on the target selected.
110 if (getRelocationModel() == Reloc::Static) {
111 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
112 Subtarget.setPICStyle(PICStyles::None);
113 } else if (Subtarget.isTargetCygMing()) {
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000114 Subtarget.setPICStyle(PICStyles::None);
115 } else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000117 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattnerfb502722009-07-10 20:58:47 +0000118 else if (getRelocationModel() == Reloc::PIC_)
119 Subtarget.setPICStyle(PICStyles::StubPIC);
120 else {
121 assert(getRelocationModel() == Reloc::DynamicNoPIC);
122 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
123 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000124 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000126 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000128 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000129 }
Chris Lattneraa7c6d22009-07-09 03:15:51 +0000130
Chris Lattner5b5e32d2009-07-09 03:32:31 +0000131 // Finally, if we have "none" as our PIC style, force to static mode.
132 if (Subtarget.getPICStyle() == PICStyles::None)
133 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134}
135
136//===----------------------------------------------------------------------===//
137// Pass Pipeline Configuration
138//===----------------------------------------------------------------------===//
139
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000140bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
141 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000143 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000144
145 // If we're using Fast-ISel, clean up the mess.
146 if (EnableFastISel)
147 PM.add(createDeadMachineInstructionElimPass());
148
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000149 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
150 PM.add(createX87FPRegKillInserterPass());
151
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 return false;
153}
154
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000155bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
156 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000157 // Calculate and set max stack object alignment early, so we can decide
158 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000159 PM.add(createX86MaxStackAlignmentCalculatorPass());
160 return false; // -print-machineinstr shouldn't print after this.
161}
162
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000163bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
164 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 PM.add(createX86FloatingPointStackifierPass());
166 return true; // -print-machineinstr should print after this.
167}
168
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000169bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
170 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000171 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000173 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
174 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000175 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000176 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000177 Subtarget.setPICStyle(PICStyles::None);
178 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179
Dale Johannesenc501c082008-08-11 23:46:25 +0000180 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000181 // On Darwin, use small code model but hack the call instruction for
182 // externals. Elsewhere, do not assume globals are in the lower 4G.
183 if (Subtarget.is64Bit()) {
184 if (Subtarget.isTargetDarwin())
185 setCodeModel(CodeModel::Small);
186 else
187 setCodeModel(CodeModel::Large);
188 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189
190 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000191
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 return false;
193}
194
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000195bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
196 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000197 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000198 // FIXME: Move this to TargetJITInfo!
199 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
200 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000201 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000202 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000203 Subtarget.setPICStyle(PICStyles::None);
204 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000205
206 // 64-bit JIT places everything in the same buffer except external functions.
207 // On Darwin, use small code model but hack the call instruction for
208 // externals. Elsewhere, do not assume globals are in the lower 4G.
209 if (Subtarget.is64Bit()) {
210 if (Subtarget.isTargetDarwin())
211 setCodeModel(CodeModel::Small);
212 else
213 setCodeModel(CodeModel::Large);
214 }
215
216 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000217
218 return false;
219}
220
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000221bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
222 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000223 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000224 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000225 return false;
226}
227
Bill Wendling58ed5d22009-04-29 00:15:41 +0000228bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000229 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000230 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 PM.add(createX86CodeEmitterPass(*this, MCE));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 return false;
233}
Dan Gohmanc6413362008-09-26 19:15:30 +0000234
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000235bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
236 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000237 JITCodeEmitter &JCE) {
238 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000239 return false;
240}
241
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000242bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
243 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000244 ObjectCodeEmitter &OCE) {
245 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000246 return false;
247}