blob: d13bf266fb46b2b5cab5da48d735d9defaccb671 [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"
David Greene71847812009-07-14 20:18:05 +000020#include "llvm/Support/FormattedStream.h"
Chris Lattner0cf0c372004-07-11 04:17:10 +000021#include "llvm/Target/TargetOptions.h"
Daniel Dunbar0c795d62009-07-25 06:49:55 +000022#include "llvm/Target/TargetRegistry.h"
Chris Lattner1e60a912003-12-20 01:22:19 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Daniel Dunbar2928c832009-11-06 10:58:06 +000025static const MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
Chris Lattnera7ac47c2009-08-12 07:22:17 +000026 Triple TheTriple(TT);
27 switch (TheTriple.getOS()) {
28 case Triple::Darwin:
Chris Lattner2807afa2009-08-22 21:03:30 +000029 return new X86MCAsmInfoDarwin(TheTriple);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000030 case Triple::MinGW32:
31 case Triple::MinGW64:
32 case Triple::Cygwin:
Chris Lattner2807afa2009-08-22 21:03:30 +000033 return new X86MCAsmInfoCOFF(TheTriple);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000034 case Triple::Win32:
Chris Lattneraf76e592009-08-22 20:48:53 +000035 return new X86WinMCAsmInfo(TheTriple);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000036 default:
Chris Lattneraf76e592009-08-22 20:48:53 +000037 return new X86ELFMCAsmInfo(TheTriple);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000038 }
39}
40
Daniel Dunbar51b198a2009-07-15 20:24:03 +000041extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +000042 // Register the target.
43 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
44 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000045
46 // Register the target asm info.
Chris Lattneraf76e592009-08-22 20:48:53 +000047 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
48 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
Daniel Dunbar7168a7d2009-08-27 08:12:55 +000049
50 // Register the code emitter.
Chris Lattnerce79a252010-02-03 21:14:33 +000051 TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
Chris Lattnerf0683042010-02-13 00:49:29 +000052 createX86_32MCCodeEmitter);
Chris Lattnerce79a252010-02-03 21:14:33 +000053 TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
Chris Lattnerf0683042010-02-13 00:49:29 +000054 createX86_64MCCodeEmitter);
Daniel Dunbar51b198a2009-07-15 20:24:03 +000055}
Douglas Gregor1555a232009-06-16 20:12:29 +000056
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000057
Daniel Dunbare28039c2009-08-02 23:37:13 +000058X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000059 const std::string &FS)
Daniel Dunbare28039c2009-08-02 23:37:13 +000060 : X86TargetMachine(T, TT, FS, false) {
Evan Cheng25ab6902006-09-08 06:48:29 +000061}
62
63
Daniel Dunbare28039c2009-08-02 23:37:13 +000064X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000065 const std::string &FS)
Daniel Dunbare28039c2009-08-02 23:37:13 +000066 : X86TargetMachine(T, TT, FS, true) {
Evan Cheng25ab6902006-09-08 06:48:29 +000067}
68
Chris Lattner11348ee2009-07-09 03:32:31 +000069/// X86TargetMachine ctor - Create an X86 target.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000070///
Daniel Dunbare28039c2009-08-02 23:37:13 +000071X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000072 const std::string &FS, bool is64Bit)
Chris Lattner0a31d2f2009-08-11 20:42:37 +000073 : LLVMTargetMachine(T, TT),
Daniel Dunbare28039c2009-08-02 23:37:13 +000074 Subtarget(TT, FS, is64Bit),
Dale Johannesen27f92be2007-08-06 21:48:35 +000075 DataLayout(Subtarget.getDataLayout()),
Nate Begemanfb5792f2005-07-12 01:41:54 +000076 FrameInfo(TargetFrameInfo::StackGrowsDown,
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +000077 Subtarget.getStackAlignment(),
78 (Subtarget.isTargetWin64() ? -40 :
79 (Subtarget.is64Bit() ? -8 : -4))),
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000080 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Chengaabe38b2007-12-22 09:40:20 +000081 DefRelocModel = getRelocationModel();
Chris Lattner11348ee2009-07-09 03:32:31 +000082
83 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000084 if (getRelocationModel() == Reloc::Default) {
Chris Lattner11348ee2009-07-09 03:32:31 +000085 if (!Subtarget.isTargetDarwin())
Evan Cheng2c312ad2006-12-04 18:07:10 +000086 setRelocationModel(Reloc::Static);
Chris Lattner11348ee2009-07-09 03:32:31 +000087 else if (Subtarget.is64Bit())
88 setRelocationModel(Reloc::PIC_);
89 else
90 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000091 }
Dan Gohman6520e202008-10-18 02:06:02 +000092
Chris Lattnere4df7562009-07-09 03:15:51 +000093 assert(getRelocationModel() != Reloc::Default &&
94 "Relocation mode not picked");
95
Chris Lattnerb2fc55b2009-07-09 03:37:30 +000096 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner11348ee2009-07-09 03:32:31 +000097 // is defined as a model for code which may be used in static or dynamic
Chris Lattner88e1fd52009-07-09 04:24:46 +000098 // executables but not necessarily a shared library. On X86-32 we just
99 // compile in -static mode, in x86-64 we use PIC.
100 if (getRelocationModel() == Reloc::DynamicNoPIC) {
101 if (is64Bit)
102 setRelocationModel(Reloc::PIC_);
103 else if (!Subtarget.isTargetDarwin())
104 setRelocationModel(Reloc::Static);
105 }
Dan Gohman6520e202008-10-18 02:06:02 +0000106
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000107 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
108 // the Mach-O file format doesn't support it.
109 if (getRelocationModel() == Reloc::Static &&
110 Subtarget.isTargetDarwin() &&
111 is64Bit)
112 setRelocationModel(Reloc::PIC_);
113
Chris Lattner11348ee2009-07-09 03:32:31 +0000114 // Determine the PICStyle based on the target selected.
115 if (getRelocationModel() == Reloc::Static) {
116 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
117 Subtarget.setPICStyle(PICStyles::None);
118 } else if (Subtarget.isTargetCygMing()) {
Chris Lattnere4df7562009-07-09 03:15:51 +0000119 Subtarget.setPICStyle(PICStyles::None);
120 } else if (Subtarget.isTargetDarwin()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000121 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000122 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattner8097b652009-07-10 20:58:47 +0000123 else if (getRelocationModel() == Reloc::PIC_)
124 Subtarget.setPICStyle(PICStyles::StubPIC);
125 else {
126 assert(getRelocationModel() == Reloc::DynamicNoPIC);
127 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
128 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000129 } else if (Subtarget.isTargetELF()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000130 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000131 Subtarget.setPICStyle(PICStyles::RIPRel);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000132 else
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000133 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000134 }
Chris Lattnere4df7562009-07-09 03:15:51 +0000135
Chris Lattner11348ee2009-07-09 03:32:31 +0000136 // Finally, if we have "none" as our PIC style, force to static mode.
137 if (Subtarget.getPICStyle() == PICStyles::None)
138 setRelocationModel(Reloc::Static);
Chris Lattner4efab052006-02-03 18:59:39 +0000139}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000140
Chris Lattner1911fd42006-09-04 04:14:57 +0000141//===----------------------------------------------------------------------===//
142// Pass Pipeline Configuration
143//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000144
Bill Wendling98a366d2009-04-29 23:29:43 +0000145bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
146 CodeGenOpt::Level OptLevel) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000147 // Install an instruction selector.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000148 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman71b7f642008-10-25 17:46:52 +0000149
Dan Gohmanbc5cbb82008-11-12 22:55:05 +0000150 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
151 PM.add(createX87FPRegKillInserterPass());
152
Chris Lattner1911fd42006-09-04 04:14:57 +0000153 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000154}
155
Bill Wendling98a366d2009-04-29 23:29:43 +0000156bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
157 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov856914f2008-04-23 18:23:05 +0000158 return false; // -print-machineinstr shouldn't print after this.
159}
160
Bill Wendling98a366d2009-04-29 23:29:43 +0000161bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
162 CodeGenOpt::Level OptLevel) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000163 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000164 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000165}
166
Bill Wendling98a366d2009-04-29 23:29:43 +0000167bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
168 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000169 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000170 // FIXME: Move this to TargetJITInfo!
171 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
172 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000173 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000174 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000175 Subtarget.setPICStyle(PICStyles::None);
176 }
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000177
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000178
179 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000180
181 return false;
182}
183
Eric Christopherf4f43cb2009-12-21 08:15:29 +0000184void X86TargetMachine::setCodeModelForStatic() {
185
186 if (getCodeModel() != CodeModel::Default) return;
187
188 // For static codegen, if we're not already set, use Small codegen.
189 setCodeModel(CodeModel::Small);
190}
191
192
193void X86TargetMachine::setCodeModelForJIT() {
194
195 if (getCodeModel() != CodeModel::Default) return;
196
197 // 64-bit JIT places everything in the same buffer except external functions.
198 if (Subtarget.is64Bit())
199 setCodeModel(CodeModel::Large);
200 else
201 setCodeModel(CodeModel::Small);
202}
Bill Wendlingd58e9cb2010-01-16 01:40:55 +0000203
Bill Wendling48814682010-01-18 22:36:35 +0000204/// getLSDAEncoding - Returns the LSDA pointer encoding. The choices are 4-byte,
205/// 8-byte, and target default. The CIE is hard-coded to indicate that the LSDA
206/// pointer in the FDE section is an "sdata4", and should be encoded as a 4-byte
207/// pointer by default. However, some systems may require a different size due
208/// to bugs or other conditions. We will default to a 4-byte encoding unless the
209/// system tells us otherwise.
210///
Bill Wendlinge1e0f482010-01-19 02:44:01 +0000211/// The issue is when the CIE says their is an LSDA. That mandates that every
212/// FDE have an LSDA slot. But if the function does not need an LSDA. There
213/// needs to be some way to signify there is none. The LSDA is encoded as
214/// pc-rel. But you don't look for some magic value after adding the pc. You
215/// have to look for a zero before adding the pc. The problem is that the size
216/// of the zero to look for depends on the encoding. The unwinder bug in SL is
217/// that it always checks for a pointer-size zero. So on x86_64 it looks for 8
218/// bytes of zero. If you have an LSDA, it works fine since the 8-bytes are
219/// non-zero so it goes ahead and then reads the value based on the encoding.
220/// But if you use sdata4 and there is no LSDA, then the test for zero gives a
221/// false negative and the unwinder thinks there is an LSDA.
222///
Bill Wendling097ea832010-01-18 19:47:53 +0000223/// FIXME: This call-back isn't good! We should be using the correct encoding
224/// regardless of the system. However, there are some systems which have bugs
225/// that prevent this from occuring.
Bill Wendlingd58e9cb2010-01-16 01:40:55 +0000226DwarfLSDAEncoding::Encoding X86TargetMachine::getLSDAEncoding() const {
Bill Wendlinga8c18892010-01-18 19:36:27 +0000227 if (Subtarget.isTargetDarwin() && Subtarget.getDarwinVers() != 10)
Bill Wendling48814682010-01-18 22:36:35 +0000228 return DwarfLSDAEncoding::Default;
Bill Wendlingd58e9cb2010-01-16 01:40:55 +0000229
Bill Wendlinga8c18892010-01-18 19:36:27 +0000230 return DwarfLSDAEncoding::EightByte;
Bill Wendlingd58e9cb2010-01-16 01:40:55 +0000231}