blob: 9cab0e0890986f699b97b3c49bf758a51401d894 [file] [log] [blame]
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00001//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00009//
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000010// This file defines the X86 specific subclass of TargetMachine.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86TargetMachine.h"
Chris Lattner5bcd95c2002-12-24 00:04:01 +000015#include "X86.h"
Chris Lattner155e68f2003-04-23 16:24:55 +000016#include "llvm/PassManager.h"
Chris Lattner3dffa792002-10-30 00:47:49 +000017#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd91d86f2003-01-13 00:51:23 +000018#include "llvm/CodeGen/Passes.h"
Matt Flemingd8a33dd2010-05-21 12:54:43 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCStreamer.h"
David Greene71847812009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Chris Lattner0cf0c372004-07-11 04:17:10 +000022#include "llvm/Target/TargetOptions.h"
Daniel Dunbar0c795d62009-07-25 06:49:55 +000023#include "llvm/Target/TargetRegistry.h"
Chris Lattner1e60a912003-12-20 01:22:19 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Matt Flemingd8a33dd2010-05-21 12:54:43 +000026static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
27 MCContext &Ctx, TargetAsmBackend &TAB,
28 raw_ostream &_OS,
29 MCCodeEmitter *_Emitter,
Rafael Espindola96aa78c2011-01-23 17:55:27 +000030 bool RelaxAll,
31 bool NoExecStack) {
Matt Flemingd8a33dd2010-05-21 12:54:43 +000032 Triple TheTriple(TT);
Daniel Dunbar912225e2011-04-19 21:14:45 +000033
34 if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO)
Benjamin Kramerc5752832010-08-04 13:16:30 +000035 return createMachOStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
Daniel Dunbar912225e2011-04-19 21:14:45 +000036
37 if (TheTriple.isOSWindows())
38 return createWinCOFFStreamer(Ctx, TAB, *_Emitter, _OS, RelaxAll);
39
40 return createELFStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll, NoExecStack);
Matt Flemingd8a33dd2010-05-21 12:54:43 +000041}
42
NAKAMURA Takumie310b3a2011-02-17 12:23:50 +000043extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +000044 // Register the target.
45 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
46 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000047
Daniel Dunbar7168a7d2009-08-27 08:12:55 +000048 // Register the code emitter.
Chris Lattnerce79a252010-02-03 21:14:33 +000049 TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
Evan Cheng59ee62d2011-07-11 03:57:24 +000050 createX86MCCodeEmitter);
Chris Lattnerce79a252010-02-03 21:14:33 +000051 TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
Evan Cheng59ee62d2011-07-11 03:57:24 +000052 createX86MCCodeEmitter);
Daniel Dunbar12783d12010-02-21 21:54:14 +000053
54 // Register the asm backend.
55 TargetRegistry::RegisterAsmBackend(TheX86_32Target,
56 createX86_32AsmBackend);
57 TargetRegistry::RegisterAsmBackend(TheX86_64Target,
58 createX86_64AsmBackend);
Matt Flemingd8a33dd2010-05-21 12:54:43 +000059
60 // Register the object streamer.
61 TargetRegistry::RegisterObjectStreamer(TheX86_32Target,
62 createMCStreamer);
63 TargetRegistry::RegisterObjectStreamer(TheX86_64Target,
64 createMCStreamer);
Daniel Dunbar51b198a2009-07-15 20:24:03 +000065}
Douglas Gregor1555a232009-06-16 20:12:29 +000066
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000067
Daniel Dunbare28039c2009-08-02 23:37:13 +000068X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Evan Cheng276365d2011-06-30 01:53:36 +000069 const std::string &CPU,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000070 const std::string &FS)
Evan Cheng276365d2011-06-30 01:53:36 +000071 : X86TargetMachine(T, TT, CPU, FS, false),
Rafael Espindola0febc462010-10-03 18:59:45 +000072 DataLayout(getSubtargetImpl()->isTargetDarwin() ?
Duncan Sandsc92cb642011-03-01 20:56:50 +000073 "e-p:32:32-f64:32:64-i64:32:64-f80:128:128-f128:128:128-n8:16:32" :
Rafael Espindola0febc462010-10-03 18:59:45 +000074 (getSubtargetImpl()->isTargetCygMing() ||
75 getSubtargetImpl()->isTargetWindows()) ?
Duncan Sandsc92cb642011-03-01 20:56:50 +000076 "e-p:32:32-f64:64:64-i64:64:64-f80:32:32-f128:128:128-n8:16:32" :
77 "e-p:32:32-f64:32:64-i64:32:64-f80:32:32-f128:128:128-n8:16:32"),
Rafael Espindola0febc462010-10-03 18:59:45 +000078 InstrInfo(*this),
79 TSInfo(*this),
80 TLInfo(*this),
81 JITInfo(*this) {
Evan Cheng25ab6902006-09-08 06:48:29 +000082}
83
84
Daniel Dunbare28039c2009-08-02 23:37:13 +000085X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Evan Cheng276365d2011-06-30 01:53:36 +000086 const std::string &CPU,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000087 const std::string &FS)
Evan Cheng276365d2011-06-30 01:53:36 +000088 : X86TargetMachine(T, TT, CPU, FS, true),
Duncan Sandsc92cb642011-03-01 20:56:50 +000089 DataLayout("e-p:64:64-s:64-f64:64:64-i64:64:64-f80:128:128-f128:128:128-n8:16:32:64"),
Rafael Espindola0febc462010-10-03 18:59:45 +000090 InstrInfo(*this),
91 TSInfo(*this),
92 TLInfo(*this),
93 JITInfo(*this) {
Evan Cheng25ab6902006-09-08 06:48:29 +000094}
95
Chris Lattner11348ee2009-07-09 03:32:31 +000096/// X86TargetMachine ctor - Create an X86 target.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000097///
NAKAMURA Takumie310b3a2011-02-17 12:23:50 +000098X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Evan Cheng276365d2011-06-30 01:53:36 +000099 const std::string &CPU,
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000100 const std::string &FS, bool is64Bit)
Evan Chengebdeeab2011-07-08 01:53:10 +0000101 : LLVMTargetMachine(T, TT, CPU, FS),
Evan Cheng4d1a8dd2011-07-08 22:30:25 +0000102 Subtarget(TT, CPU, FS, StackAlignmentOverride, is64Bit),
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000103 FrameLowering(*this, Subtarget),
Rafael Espindola0febc462010-10-03 18:59:45 +0000104 ELFWriterInfo(is64Bit, true) {
Evan Chengaabe38b2007-12-22 09:40:20 +0000105 DefRelocModel = getRelocationModel();
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000106
Chris Lattner11348ee2009-07-09 03:32:31 +0000107 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000108 if (getRelocationModel() == Reloc::Default) {
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000109 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
110 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
111 // use static relocation model by default.
112 if (Subtarget.isTargetDarwin()) {
113 if (Subtarget.is64Bit())
114 setRelocationModel(Reloc::PIC_);
115 else
116 setRelocationModel(Reloc::DynamicNoPIC);
117 } else if (Subtarget.isTargetWin64())
Chris Lattner11348ee2009-07-09 03:32:31 +0000118 setRelocationModel(Reloc::PIC_);
119 else
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000120 setRelocationModel(Reloc::Static);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000121 }
Dan Gohman6520e202008-10-18 02:06:02 +0000122
Chris Lattnere4df7562009-07-09 03:15:51 +0000123 assert(getRelocationModel() != Reloc::Default &&
124 "Relocation mode not picked");
125
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000126 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner11348ee2009-07-09 03:32:31 +0000127 // is defined as a model for code which may be used in static or dynamic
Chris Lattner88e1fd52009-07-09 04:24:46 +0000128 // executables but not necessarily a shared library. On X86-32 we just
129 // compile in -static mode, in x86-64 we use PIC.
130 if (getRelocationModel() == Reloc::DynamicNoPIC) {
131 if (is64Bit)
132 setRelocationModel(Reloc::PIC_);
133 else if (!Subtarget.isTargetDarwin())
134 setRelocationModel(Reloc::Static);
135 }
Dan Gohman6520e202008-10-18 02:06:02 +0000136
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000137 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
138 // the Mach-O file format doesn't support it.
139 if (getRelocationModel() == Reloc::Static &&
140 Subtarget.isTargetDarwin() &&
141 is64Bit)
142 setRelocationModel(Reloc::PIC_);
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000143
Chris Lattner11348ee2009-07-09 03:32:31 +0000144 // Determine the PICStyle based on the target selected.
145 if (getRelocationModel() == Reloc::Static) {
146 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
147 Subtarget.setPICStyle(PICStyles::None);
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000148 } else if (Subtarget.is64Bit()) {
149 // PIC in 64 bit mode is always rip-rel.
150 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattner11348ee2009-07-09 03:32:31 +0000151 } else if (Subtarget.isTargetCygMing()) {
Chris Lattnere4df7562009-07-09 03:15:51 +0000152 Subtarget.setPICStyle(PICStyles::None);
153 } else if (Subtarget.isTargetDarwin()) {
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000154 if (getRelocationModel() == Reloc::PIC_)
Chris Lattner8097b652009-07-10 20:58:47 +0000155 Subtarget.setPICStyle(PICStyles::StubPIC);
156 else {
157 assert(getRelocationModel() == Reloc::DynamicNoPIC);
158 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
159 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000160 } else if (Subtarget.isTargetELF()) {
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000161 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000162 }
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000163
Chris Lattner11348ee2009-07-09 03:32:31 +0000164 // Finally, if we have "none" as our PIC style, force to static mode.
165 if (Subtarget.getPICStyle() == PICStyles::None)
166 setRelocationModel(Reloc::Static);
Evan Chengef41ff62011-06-23 17:54:54 +0000167
168 // default to hard float ABI
169 if (FloatABIType == FloatABI::Default)
170 FloatABIType = FloatABI::Hard;
Chris Lattner4efab052006-02-03 18:59:39 +0000171}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000172
Chris Lattner1911fd42006-09-04 04:14:57 +0000173//===----------------------------------------------------------------------===//
174// Pass Pipeline Configuration
175//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000176
Bill Wendling98a366d2009-04-29 23:29:43 +0000177bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
178 CodeGenOpt::Level OptLevel) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000179 // Install an instruction selector.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000180 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman71b7f642008-10-25 17:46:52 +0000181
Dan Gohman84023e02010-07-10 09:00:22 +0000182 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
183 if (!Subtarget.is64Bit())
184 PM.add(createGlobalBaseRegPass());
185
Chris Lattner1911fd42006-09-04 04:14:57 +0000186 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000187}
188
Bill Wendling98a366d2009-04-29 23:29:43 +0000189bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
190 CodeGenOpt::Level OptLevel) {
Jim Grosbachfa85eb62010-04-06 20:26:37 +0000191 PM.add(createX86MaxStackAlignmentHeuristicPass());
Anton Korobeynikov856914f2008-04-23 18:23:05 +0000192 return false; // -print-machineinstr shouldn't print after this.
193}
194
Bill Wendling98a366d2009-04-29 23:29:43 +0000195bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
196 CodeGenOpt::Level OptLevel) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000197 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000198 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000199}
200
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000201bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
202 CodeGenOpt::Level OptLevel) {
Eric Christopherf4f06902010-05-05 07:35:59 +0000203 if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000204 PM.add(createSSEDomainFixPass());
205 return true;
206 }
207 return false;
208}
209
Bill Wendling98a366d2009-04-29 23:29:43 +0000210bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
211 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000212 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000213 // FIXME: Move this to TargetJITInfo!
214 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
NAKAMURA Takumie310b3a2011-02-17 12:23:50 +0000215 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000216 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000217 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000218 Subtarget.setPICStyle(PICStyles::None);
219 }
NAKAMURA Takumie310b3a2011-02-17 12:23:50 +0000220
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000221
222 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000223
224 return false;
225}
226
Eric Christopherf4f43cb2009-12-21 08:15:29 +0000227void X86TargetMachine::setCodeModelForStatic() {
228
229 if (getCodeModel() != CodeModel::Default) return;
230
231 // For static codegen, if we're not already set, use Small codegen.
232 setCodeModel(CodeModel::Small);
233}
234
235
236void X86TargetMachine::setCodeModelForJIT() {
237
238 if (getCodeModel() != CodeModel::Default) return;
239
240 // 64-bit JIT places everything in the same buffer except external functions.
241 if (Subtarget.is64Bit())
242 setCodeModel(CodeModel::Large);
243 else
244 setCodeModel(CodeModel::Small);
245}