blob: 74833291dc7ad7bd38d4d5dd93ffc16f712b1363 [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
Chris Lattneraf76e592009-08-22 20:48:53 +000014#include "X86MCAsmInfo.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000015#include "X86TargetMachine.h"
Chris Lattner5bcd95c2002-12-24 00:04:01 +000016#include "X86.h"
Chris Lattner155e68f2003-04-23 16:24:55 +000017#include "llvm/PassManager.h"
Chris Lattner3dffa792002-10-30 00:47:49 +000018#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd91d86f2003-01-13 00:51:23 +000019#include "llvm/CodeGen/Passes.h"
Matt Flemingd8a33dd2010-05-21 12:54:43 +000020#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCStreamer.h"
David Greene71847812009-07-14 20:18:05 +000022#include "llvm/Support/FormattedStream.h"
Chris Lattner0cf0c372004-07-11 04:17:10 +000023#include "llvm/Target/TargetOptions.h"
Daniel Dunbar0c795d62009-07-25 06:49:55 +000024#include "llvm/Target/TargetRegistry.h"
Chris Lattner1e60a912003-12-20 01:22:19 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Daniel Dunbar5d067fe2010-03-20 22:36:22 +000027static MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
Chris Lattnera7ac47c2009-08-12 07:22:17 +000028 Triple TheTriple(TT);
Daniel Dunbar912225e2011-04-19 21:14:45 +000029
Rafael Espindolabfa27cc2011-04-28 16:09:09 +000030 if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO) {
31 if (TheTriple.getArch() == Triple::x86_64)
32 return new X86_64MCAsmInfoDarwin(TheTriple);
33 else
34 return new X86MCAsmInfoDarwin(TheTriple);
35 }
Daniel Dunbar912225e2011-04-19 21:14:45 +000036
37 if (TheTriple.isOSWindows())
38 return new X86MCAsmInfoCOFF(TheTriple);
39
40 return new X86ELFMCAsmInfo(TheTriple);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000041}
42
Matt Flemingd8a33dd2010-05-21 12:54:43 +000043static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
44 MCContext &Ctx, TargetAsmBackend &TAB,
45 raw_ostream &_OS,
46 MCCodeEmitter *_Emitter,
Rafael Espindola96aa78c2011-01-23 17:55:27 +000047 bool RelaxAll,
48 bool NoExecStack) {
Matt Flemingd8a33dd2010-05-21 12:54:43 +000049 Triple TheTriple(TT);
Daniel Dunbar912225e2011-04-19 21:14:45 +000050
51 if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO)
Benjamin Kramerc5752832010-08-04 13:16:30 +000052 return createMachOStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
Daniel Dunbar912225e2011-04-19 21:14:45 +000053
54 if (TheTriple.isOSWindows())
55 return createWinCOFFStreamer(Ctx, TAB, *_Emitter, _OS, RelaxAll);
56
57 return createELFStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll, NoExecStack);
Matt Flemingd8a33dd2010-05-21 12:54:43 +000058}
59
NAKAMURA Takumie310b3a2011-02-17 12:23:50 +000060extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +000061 // Register the target.
62 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
63 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000064
65 // Register the target asm info.
Chris Lattneraf76e592009-08-22 20:48:53 +000066 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
67 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
Daniel Dunbar7168a7d2009-08-27 08:12:55 +000068
69 // Register the code emitter.
Chris Lattnerce79a252010-02-03 21:14:33 +000070 TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
Chris Lattnerf0683042010-02-13 00:49:29 +000071 createX86_32MCCodeEmitter);
Chris Lattnerce79a252010-02-03 21:14:33 +000072 TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
Chris Lattnerf0683042010-02-13 00:49:29 +000073 createX86_64MCCodeEmitter);
Daniel Dunbar12783d12010-02-21 21:54:14 +000074
75 // Register the asm backend.
76 TargetRegistry::RegisterAsmBackend(TheX86_32Target,
77 createX86_32AsmBackend);
78 TargetRegistry::RegisterAsmBackend(TheX86_64Target,
79 createX86_64AsmBackend);
Matt Flemingd8a33dd2010-05-21 12:54:43 +000080
81 // Register the object streamer.
82 TargetRegistry::RegisterObjectStreamer(TheX86_32Target,
83 createMCStreamer);
84 TargetRegistry::RegisterObjectStreamer(TheX86_64Target,
85 createMCStreamer);
Daniel Dunbar51b198a2009-07-15 20:24:03 +000086}
Douglas Gregor1555a232009-06-16 20:12:29 +000087
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000088
Daniel Dunbare28039c2009-08-02 23:37:13 +000089X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000090 const std::string &FS)
Rafael Espindola0febc462010-10-03 18:59:45 +000091 : X86TargetMachine(T, TT, FS, false),
92 DataLayout(getSubtargetImpl()->isTargetDarwin() ?
Duncan Sandsc92cb642011-03-01 20:56:50 +000093 "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 +000094 (getSubtargetImpl()->isTargetCygMing() ||
95 getSubtargetImpl()->isTargetWindows()) ?
Duncan Sandsc92cb642011-03-01 20:56:50 +000096 "e-p:32:32-f64:64:64-i64:64:64-f80:32:32-f128:128:128-n8:16:32" :
97 "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 +000098 InstrInfo(*this),
99 TSInfo(*this),
100 TLInfo(*this),
101 JITInfo(*this) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000102}
103
104
Daniel Dunbare28039c2009-08-02 23:37:13 +0000105X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000106 const std::string &FS)
Rafael Espindola0febc462010-10-03 18:59:45 +0000107 : X86TargetMachine(T, TT, FS, true),
Duncan Sandsc92cb642011-03-01 20:56:50 +0000108 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 +0000109 InstrInfo(*this),
110 TSInfo(*this),
111 TLInfo(*this),
112 JITInfo(*this) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000113}
114
Chris Lattner11348ee2009-07-09 03:32:31 +0000115/// X86TargetMachine ctor - Create an X86 target.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000116///
NAKAMURA Takumie310b3a2011-02-17 12:23:50 +0000117X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000118 const std::string &FS, bool is64Bit)
Anton Korobeynikovd9e33852010-11-18 23:25:52 +0000119 : LLVMTargetMachine(T, TT),
Daniel Dunbare28039c2009-08-02 23:37:13 +0000120 Subtarget(TT, FS, is64Bit),
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000121 FrameLowering(*this, Subtarget),
Rafael Espindola0febc462010-10-03 18:59:45 +0000122 ELFWriterInfo(is64Bit, true) {
Evan Chengaabe38b2007-12-22 09:40:20 +0000123 DefRelocModel = getRelocationModel();
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000124
Chris Lattner11348ee2009-07-09 03:32:31 +0000125 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000126 if (getRelocationModel() == Reloc::Default) {
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000127 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
128 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
129 // use static relocation model by default.
130 if (Subtarget.isTargetDarwin()) {
131 if (Subtarget.is64Bit())
132 setRelocationModel(Reloc::PIC_);
133 else
134 setRelocationModel(Reloc::DynamicNoPIC);
135 } else if (Subtarget.isTargetWin64())
Chris Lattner11348ee2009-07-09 03:32:31 +0000136 setRelocationModel(Reloc::PIC_);
137 else
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000138 setRelocationModel(Reloc::Static);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000139 }
Dan Gohman6520e202008-10-18 02:06:02 +0000140
Chris Lattnere4df7562009-07-09 03:15:51 +0000141 assert(getRelocationModel() != Reloc::Default &&
142 "Relocation mode not picked");
143
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000144 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner11348ee2009-07-09 03:32:31 +0000145 // is defined as a model for code which may be used in static or dynamic
Chris Lattner88e1fd52009-07-09 04:24:46 +0000146 // executables but not necessarily a shared library. On X86-32 we just
147 // compile in -static mode, in x86-64 we use PIC.
148 if (getRelocationModel() == Reloc::DynamicNoPIC) {
149 if (is64Bit)
150 setRelocationModel(Reloc::PIC_);
151 else if (!Subtarget.isTargetDarwin())
152 setRelocationModel(Reloc::Static);
153 }
Dan Gohman6520e202008-10-18 02:06:02 +0000154
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000155 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
156 // the Mach-O file format doesn't support it.
157 if (getRelocationModel() == Reloc::Static &&
158 Subtarget.isTargetDarwin() &&
159 is64Bit)
160 setRelocationModel(Reloc::PIC_);
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000161
Chris Lattner11348ee2009-07-09 03:32:31 +0000162 // Determine the PICStyle based on the target selected.
163 if (getRelocationModel() == Reloc::Static) {
164 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
165 Subtarget.setPICStyle(PICStyles::None);
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000166 } else if (Subtarget.is64Bit()) {
167 // PIC in 64 bit mode is always rip-rel.
168 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattner11348ee2009-07-09 03:32:31 +0000169 } else if (Subtarget.isTargetCygMing()) {
Chris Lattnere4df7562009-07-09 03:15:51 +0000170 Subtarget.setPICStyle(PICStyles::None);
171 } else if (Subtarget.isTargetDarwin()) {
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000172 if (getRelocationModel() == Reloc::PIC_)
Chris Lattner8097b652009-07-10 20:58:47 +0000173 Subtarget.setPICStyle(PICStyles::StubPIC);
174 else {
175 assert(getRelocationModel() == Reloc::DynamicNoPIC);
176 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
177 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000178 } else if (Subtarget.isTargetELF()) {
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000179 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000180 }
Anton Korobeynikov699647c2010-08-21 17:21:11 +0000181
Chris Lattner11348ee2009-07-09 03:32:31 +0000182 // Finally, if we have "none" as our PIC style, force to static mode.
183 if (Subtarget.getPICStyle() == PICStyles::None)
184 setRelocationModel(Reloc::Static);
Chris Lattner4efab052006-02-03 18:59:39 +0000185}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000186
Chris Lattner1911fd42006-09-04 04:14:57 +0000187//===----------------------------------------------------------------------===//
188// Pass Pipeline Configuration
189//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000190
Bill Wendling98a366d2009-04-29 23:29:43 +0000191bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
192 CodeGenOpt::Level OptLevel) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000193 // Install an instruction selector.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000194 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman71b7f642008-10-25 17:46:52 +0000195
Dan Gohman84023e02010-07-10 09:00:22 +0000196 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
197 if (!Subtarget.is64Bit())
198 PM.add(createGlobalBaseRegPass());
199
Chris Lattner1911fd42006-09-04 04:14:57 +0000200 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000201}
202
Bill Wendling98a366d2009-04-29 23:29:43 +0000203bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
204 CodeGenOpt::Level OptLevel) {
Jim Grosbachfa85eb62010-04-06 20:26:37 +0000205 PM.add(createX86MaxStackAlignmentHeuristicPass());
Anton Korobeynikov856914f2008-04-23 18:23:05 +0000206 return false; // -print-machineinstr shouldn't print after this.
207}
208
Bill Wendling98a366d2009-04-29 23:29:43 +0000209bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
210 CodeGenOpt::Level OptLevel) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000211 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000212 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000213}
214
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000215bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
216 CodeGenOpt::Level OptLevel) {
Eric Christopherf4f06902010-05-05 07:35:59 +0000217 if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000218 PM.add(createSSEDomainFixPass());
219 return true;
220 }
221 return false;
222}
223
Bill Wendling98a366d2009-04-29 23:29:43 +0000224bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
225 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000226 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000227 // FIXME: Move this to TargetJITInfo!
228 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
NAKAMURA Takumie310b3a2011-02-17 12:23:50 +0000229 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000230 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000231 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000232 Subtarget.setPICStyle(PICStyles::None);
233 }
NAKAMURA Takumie310b3a2011-02-17 12:23:50 +0000234
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000235
236 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000237
238 return false;
239}
240
Eric Christopherf4f43cb2009-12-21 08:15:29 +0000241void X86TargetMachine::setCodeModelForStatic() {
242
243 if (getCodeModel() != CodeModel::Default) return;
244
245 // For static codegen, if we're not already set, use Small codegen.
246 setCodeModel(CodeModel::Small);
247}
248
249
250void X86TargetMachine::setCodeModelForJIT() {
251
252 if (getCodeModel() != CodeModel::Default) return;
253
254 // 64-bit JIT places everything in the same buffer except external functions.
255 if (Subtarget.is64Bit())
256 setCodeModel(CodeModel::Large);
257 else
258 setCodeModel(CodeModel::Small);
259}