blob: a61de1cd182aca87c564636914bbbb638241a492 [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
Chris Lattneraf76e592009-08-22 20:48:53 +000025static const MCAsmInfo *createMCAsmInfo(const Target &T,
Chris Lattnera7ac47c2009-08-12 07:22:17 +000026 const StringRef &TT) {
27 Triple TheTriple(TT);
28 switch (TheTriple.getOS()) {
29 case Triple::Darwin:
Chris Lattner2807afa2009-08-22 21:03:30 +000030 return new X86MCAsmInfoDarwin(TheTriple);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000031 case Triple::MinGW32:
32 case Triple::MinGW64:
33 case Triple::Cygwin:
Chris Lattner2807afa2009-08-22 21:03:30 +000034 return new X86MCAsmInfoCOFF(TheTriple);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000035 case Triple::Win32:
Chris Lattneraf76e592009-08-22 20:48:53 +000036 return new X86WinMCAsmInfo(TheTriple);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000037 default:
Chris Lattneraf76e592009-08-22 20:48:53 +000038 return new X86ELFMCAsmInfo(TheTriple);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000039 }
40}
41
Daniel Dunbar51b198a2009-07-15 20:24:03 +000042extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +000043 // Register the target.
44 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
45 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Chris Lattnera7ac47c2009-08-12 07:22:17 +000046
47 // Register the target asm info.
Chris Lattneraf76e592009-08-22 20:48:53 +000048 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
49 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
Daniel Dunbar7168a7d2009-08-27 08:12:55 +000050
51 // Register the code emitter.
52 TargetRegistry::RegisterCodeEmitter(TheX86_32Target, createX86MCCodeEmitter);
53 TargetRegistry::RegisterCodeEmitter(TheX86_64Target, createX86MCCodeEmitter);
Daniel Dunbar51b198a2009-07-15 20:24:03 +000054}
Douglas Gregor1555a232009-06-16 20:12:29 +000055
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000056
Daniel Dunbare28039c2009-08-02 23:37:13 +000057X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000058 const std::string &FS)
Daniel Dunbare28039c2009-08-02 23:37:13 +000059 : X86TargetMachine(T, TT, FS, false) {
Evan Cheng25ab6902006-09-08 06:48:29 +000060}
61
62
Daniel Dunbare28039c2009-08-02 23:37:13 +000063X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000064 const std::string &FS)
Daniel Dunbare28039c2009-08-02 23:37:13 +000065 : X86TargetMachine(T, TT, FS, true) {
Evan Cheng25ab6902006-09-08 06:48:29 +000066}
67
Chris Lattner11348ee2009-07-09 03:32:31 +000068/// X86TargetMachine ctor - Create an X86 target.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000069///
Daniel Dunbare28039c2009-08-02 23:37:13 +000070X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000071 const std::string &FS, bool is64Bit)
Chris Lattner0a31d2f2009-08-11 20:42:37 +000072 : LLVMTargetMachine(T, TT),
Daniel Dunbare28039c2009-08-02 23:37:13 +000073 Subtarget(TT, FS, is64Bit),
Dale Johannesen27f92be2007-08-06 21:48:35 +000074 DataLayout(Subtarget.getDataLayout()),
Nate Begemanfb5792f2005-07-12 01:41:54 +000075 FrameInfo(TargetFrameInfo::StackGrowsDown,
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +000076 Subtarget.getStackAlignment(),
77 (Subtarget.isTargetWin64() ? -40 :
78 (Subtarget.is64Bit() ? -8 : -4))),
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000079 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Chengaabe38b2007-12-22 09:40:20 +000080 DefRelocModel = getRelocationModel();
Chris Lattner11348ee2009-07-09 03:32:31 +000081
82 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000083 if (getRelocationModel() == Reloc::Default) {
Chris Lattner11348ee2009-07-09 03:32:31 +000084 if (!Subtarget.isTargetDarwin())
Evan Cheng2c312ad2006-12-04 18:07:10 +000085 setRelocationModel(Reloc::Static);
Chris Lattner11348ee2009-07-09 03:32:31 +000086 else if (Subtarget.is64Bit())
87 setRelocationModel(Reloc::PIC_);
88 else
89 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000090 }
Dan Gohman6520e202008-10-18 02:06:02 +000091
Chris Lattnere4df7562009-07-09 03:15:51 +000092 assert(getRelocationModel() != Reloc::Default &&
93 "Relocation mode not picked");
94
Chris Lattner11348ee2009-07-09 03:32:31 +000095 // If no code model is picked, default to small.
96 if (getCodeModel() == CodeModel::Default)
97 setCodeModel(CodeModel::Small);
98
Chris Lattnerb2fc55b2009-07-09 03:37:30 +000099 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner11348ee2009-07-09 03:32:31 +0000100 // is defined as a model for code which may be used in static or dynamic
Chris Lattner88e1fd52009-07-09 04:24:46 +0000101 // executables but not necessarily a shared library. On X86-32 we just
102 // compile in -static mode, in x86-64 we use PIC.
103 if (getRelocationModel() == Reloc::DynamicNoPIC) {
104 if (is64Bit)
105 setRelocationModel(Reloc::PIC_);
106 else if (!Subtarget.isTargetDarwin())
107 setRelocationModel(Reloc::Static);
108 }
Dan Gohman6520e202008-10-18 02:06:02 +0000109
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000110 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
111 // the Mach-O file format doesn't support it.
112 if (getRelocationModel() == Reloc::Static &&
113 Subtarget.isTargetDarwin() &&
114 is64Bit)
115 setRelocationModel(Reloc::PIC_);
116
Chris Lattner11348ee2009-07-09 03:32:31 +0000117 // Determine the PICStyle based on the target selected.
118 if (getRelocationModel() == Reloc::Static) {
119 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
120 Subtarget.setPICStyle(PICStyles::None);
121 } else if (Subtarget.isTargetCygMing()) {
Chris Lattnere4df7562009-07-09 03:15:51 +0000122 Subtarget.setPICStyle(PICStyles::None);
123 } else if (Subtarget.isTargetDarwin()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000124 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000125 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattner8097b652009-07-10 20:58:47 +0000126 else if (getRelocationModel() == Reloc::PIC_)
127 Subtarget.setPICStyle(PICStyles::StubPIC);
128 else {
129 assert(getRelocationModel() == Reloc::DynamicNoPIC);
130 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
131 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000132 } else if (Subtarget.isTargetELF()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000133 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000134 Subtarget.setPICStyle(PICStyles::RIPRel);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000135 else
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000136 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000137 }
Chris Lattnere4df7562009-07-09 03:15:51 +0000138
Chris Lattner11348ee2009-07-09 03:32:31 +0000139 // Finally, if we have "none" as our PIC style, force to static mode.
140 if (Subtarget.getPICStyle() == PICStyles::None)
141 setRelocationModel(Reloc::Static);
Chris Lattner4efab052006-02-03 18:59:39 +0000142}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000143
Chris Lattner1911fd42006-09-04 04:14:57 +0000144//===----------------------------------------------------------------------===//
145// Pass Pipeline Configuration
146//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000147
Bill Wendling98a366d2009-04-29 23:29:43 +0000148bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
149 CodeGenOpt::Level OptLevel) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000150 // Install an instruction selector.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000151 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman71b7f642008-10-25 17:46:52 +0000152
153 // If we're using Fast-ISel, clean up the mess.
154 if (EnableFastISel)
155 PM.add(createDeadMachineInstructionElimPass());
156
Dan Gohmanbc5cbb82008-11-12 22:55:05 +0000157 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
158 PM.add(createX87FPRegKillInserterPass());
159
Chris Lattner1911fd42006-09-04 04:14:57 +0000160 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000161}
162
Bill Wendling98a366d2009-04-29 23:29:43 +0000163bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
164 CodeGenOpt::Level OptLevel) {
Anton Korobeynikovd52bdaf2008-04-23 18:23:30 +0000165 // Calculate and set max stack object alignment early, so we can decide
166 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov856914f2008-04-23 18:23:05 +0000167 PM.add(createX86MaxStackAlignmentCalculatorPass());
168 return false; // -print-machineinstr shouldn't print after this.
169}
170
Bill Wendling98a366d2009-04-29 23:29:43 +0000171bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
172 CodeGenOpt::Level OptLevel) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000173 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000174 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000175}
176
Bill Wendling98a366d2009-04-29 23:29:43 +0000177bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
178 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000179 MachineCodeEmitter &MCE) {
Chris Lattner2130b992006-09-04 18:48:41 +0000180 // FIXME: Move this to TargetJITInfo!
Dale Johannesen5f777192008-08-12 21:02:08 +0000181 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
182 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000183 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Chengaabe38b2007-12-22 09:40:20 +0000184 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000185 Subtarget.setPICStyle(PICStyles::None);
186 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000187
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000188 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen5f777192008-08-12 21:02:08 +0000189 // On Darwin, use small code model but hack the call instruction for
190 // externals. Elsewhere, do not assume globals are in the lower 4G.
191 if (Subtarget.is64Bit()) {
192 if (Subtarget.isTargetDarwin())
193 setCodeModel(CodeModel::Small);
194 else
195 setCodeModel(CodeModel::Large);
196 }
Anton Korobeynikov15fccf12006-12-20 01:03:20 +0000197
Evan Cheng55fc2802006-07-25 20:40:54 +0000198 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng8bd60352007-07-20 21:56:13 +0000199
Chris Lattner81b6ed72005-07-11 05:17:48 +0000200 return false;
201}
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000202
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000203bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
204 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000205 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000206 // FIXME: Move this to TargetJITInfo!
207 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
208 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000209 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000210 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000211 Subtarget.setPICStyle(PICStyles::None);
212 }
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000213
214 // 64-bit JIT places everything in the same buffer except external functions.
215 // On Darwin, use small code model but hack the call instruction for
216 // externals. Elsewhere, do not assume globals are in the lower 4G.
217 if (Subtarget.is64Bit()) {
218 if (Subtarget.isTargetDarwin())
219 setCodeModel(CodeModel::Small);
220 else
221 setCodeModel(CodeModel::Large);
222 }
223
224 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000225
226 return false;
227}
228
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000229bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
230 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000231 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000232 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000233 return false;
234}
235
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000236bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000237 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000238 MachineCodeEmitter &MCE) {
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000239 PM.add(createX86CodeEmitterPass(*this, MCE));
240 return false;
241}
Dan Gohman97135e12008-09-26 19:15:30 +0000242
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000243bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
244 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000245 JITCodeEmitter &JCE) {
246 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000247 return false;
248}
249
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000250bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
251 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000252 ObjectCodeEmitter &OCE) {
253 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000254 return false;
255}