blob: f835e295e6e08644271e6d819c7b0b950e07d5a5 [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
Chris Lattner621c44d2009-08-22 20:48:53 +000014#include "X86MCAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#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 Dunbarde46a5b2009-11-06 10:58:06 +000025static const MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000026 Triple TheTriple(TT);
27 switch (TheTriple.getOS()) {
28 case Triple::Darwin:
Chris Lattnercd4cab92009-08-22 21:03:30 +000029 return new X86MCAsmInfoDarwin(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000030 case Triple::MinGW32:
31 case Triple::MinGW64:
32 case Triple::Cygwin:
Chris Lattnercd4cab92009-08-22 21:03:30 +000033 return new X86MCAsmInfoCOFF(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000034 case Triple::Win32:
Chris Lattner621c44d2009-08-22 20:48:53 +000035 return new X86WinMCAsmInfo(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000036 default:
Chris Lattner621c44d2009-08-22 20:48:53 +000037 return new X86ELFMCAsmInfo(TheTriple);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000038 }
39}
40
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000041extern "C" void LLVMInitializeX86Target() {
Daniel Dunbarc680b012009-07-25 06:49:55 +000042 // Register the target.
43 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
44 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Chris Lattnerd88f9fd2009-08-12 07:22:17 +000045
46 // Register the target asm info.
Chris Lattner621c44d2009-08-22 20:48:53 +000047 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
48 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
Daniel Dunbar2f379632009-08-27 08:12:55 +000049
50 // Register the code emitter.
Chris Lattnerdd990132010-02-03 21:24:49 +000051 // FIXME: Remove the heinous one when the new one works.
Chris Lattner917e8982010-02-03 21:14:33 +000052 TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
53 createHeinousX86MCCodeEmitter);
54 TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
55 createHeinousX86MCCodeEmitter);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000056}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000057
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000059X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000060 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000061 : X86TargetMachine(T, TT, FS, false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062}
63
64
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000065X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000066 const std::string &FS)
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000067 : X86TargetMachine(T, TT, FS, true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068}
69
Chris Lattner5b5e32d2009-07-09 03:32:31 +000070/// X86TargetMachine ctor - Create an X86 target.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071///
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000072X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000073 const std::string &FS, bool is64Bit)
Chris Lattnerd9236ec2009-08-11 20:42:37 +000074 : LLVMTargetMachine(T, TT),
Daniel Dunbarf5c2b852009-08-02 23:37:13 +000075 Subtarget(TT, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +000076 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 FrameInfo(TargetFrameInfo::StackGrowsDown,
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +000078 Subtarget.getStackAlignment(),
79 (Subtarget.isTargetWin64() ? -40 :
80 (Subtarget.is64Bit() ? -8 : -4))),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000081 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +000082 DefRelocModel = getRelocationModel();
Chris Lattner5b5e32d2009-07-09 03:32:31 +000083
84 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000085 if (getRelocationModel() == Reloc::Default) {
Chris Lattner5b5e32d2009-07-09 03:32:31 +000086 if (!Subtarget.isTargetDarwin())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 setRelocationModel(Reloc::Static);
Chris Lattner5b5e32d2009-07-09 03:32:31 +000088 else if (Subtarget.is64Bit())
89 setRelocationModel(Reloc::PIC_);
90 else
91 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000092 }
Dan Gohman36322c72008-10-18 02:06:02 +000093
Chris Lattneraa7c6d22009-07-09 03:15:51 +000094 assert(getRelocationModel() != Reloc::Default &&
95 "Relocation mode not picked");
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
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000151 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
152 PM.add(createX87FPRegKillInserterPass());
153
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 return false;
155}
156
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000157bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
158 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000159 return false; // -print-machineinstr shouldn't print after this.
160}
161
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000162bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
163 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 PM.add(createX86FloatingPointStackifierPass());
165 return true; // -print-machineinstr should print after this.
166}
167
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000168bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
169 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000170 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000171 // FIXME: Move this to TargetJITInfo!
172 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
173 if (DefRelocModel == Reloc::Default &&
Chris Lattner4c8b6862009-07-09 03:37:30 +0000174 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000175 setRelocationModel(Reloc::Static);
Chris Lattner4c8b6862009-07-09 03:37:30 +0000176 Subtarget.setPICStyle(PICStyles::None);
177 }
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000178
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000179
180 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000181
182 return false;
183}
184
Eric Christopher7669c972009-12-21 08:15:29 +0000185void X86TargetMachine::setCodeModelForStatic() {
186
187 if (getCodeModel() != CodeModel::Default) return;
188
189 // For static codegen, if we're not already set, use Small codegen.
190 setCodeModel(CodeModel::Small);
191}
192
193
194void X86TargetMachine::setCodeModelForJIT() {
195
196 if (getCodeModel() != CodeModel::Default) return;
197
198 // 64-bit JIT places everything in the same buffer except external functions.
199 if (Subtarget.is64Bit())
200 setCodeModel(CodeModel::Large);
201 else
202 setCodeModel(CodeModel::Small);
203}
Bill Wendlinge46f64b2010-01-16 01:40:55 +0000204
Bill Wendling63104742010-01-18 22:36:35 +0000205/// getLSDAEncoding - Returns the LSDA pointer encoding. The choices are 4-byte,
206/// 8-byte, and target default. The CIE is hard-coded to indicate that the LSDA
207/// pointer in the FDE section is an "sdata4", and should be encoded as a 4-byte
208/// pointer by default. However, some systems may require a different size due
209/// to bugs or other conditions. We will default to a 4-byte encoding unless the
210/// system tells us otherwise.
211///
Bill Wendlinge5138b62010-01-19 02:44:01 +0000212/// The issue is when the CIE says their is an LSDA. That mandates that every
213/// FDE have an LSDA slot. But if the function does not need an LSDA. There
214/// needs to be some way to signify there is none. The LSDA is encoded as
215/// pc-rel. But you don't look for some magic value after adding the pc. You
216/// have to look for a zero before adding the pc. The problem is that the size
217/// of the zero to look for depends on the encoding. The unwinder bug in SL is
218/// that it always checks for a pointer-size zero. So on x86_64 it looks for 8
219/// bytes of zero. If you have an LSDA, it works fine since the 8-bytes are
220/// non-zero so it goes ahead and then reads the value based on the encoding.
221/// But if you use sdata4 and there is no LSDA, then the test for zero gives a
222/// false negative and the unwinder thinks there is an LSDA.
223///
Bill Wendling43b36f72010-01-18 19:47:53 +0000224/// FIXME: This call-back isn't good! We should be using the correct encoding
225/// regardless of the system. However, there are some systems which have bugs
226/// that prevent this from occuring.
Bill Wendlinge46f64b2010-01-16 01:40:55 +0000227DwarfLSDAEncoding::Encoding X86TargetMachine::getLSDAEncoding() const {
Bill Wendling6442f652010-01-18 19:36:27 +0000228 if (Subtarget.isTargetDarwin() && Subtarget.getDarwinVers() != 10)
Bill Wendling63104742010-01-18 22:36:35 +0000229 return DwarfLSDAEncoding::Default;
Bill Wendlinge46f64b2010-01-16 01:40:55 +0000230
Bill Wendling6442f652010-01-18 19:36:27 +0000231 return DwarfLSDAEncoding::EightByte;
Bill Wendlinge46f64b2010-01-16 01:40:55 +0000232}