blob: 43fbbc07e89ed8b481a7aa46ead42cbf010765d9 [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
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000014#include "X86TargetAsmInfo.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000015#include "X86TargetMachine.h"
Chris Lattner5bcd95c2002-12-24 00:04:01 +000016#include "X86.h"
Chris Lattnerbb144a82003-08-24 19:49:48 +000017#include "llvm/Module.h"
Chris Lattner155e68f2003-04-23 16:24:55 +000018#include "llvm/PassManager.h"
Chris Lattner3dffa792002-10-30 00:47:49 +000019#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd91d86f2003-01-13 00:51:23 +000020#include "llvm/CodeGen/Passes.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"
Chris Lattnerd36c9702004-07-11 02:48:49 +000023#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner1e60a912003-12-20 01:22:19 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Jeff Cohen1c32f792005-01-03 16:34:19 +000026/// X86TargetMachineModule - Note that this is used on hosts that cannot link
27/// in a library unless there are references into the library. In particular,
28/// it seems that it is not possible to get things to work on Win32 without
29/// this. Though it is unused, do not remove it.
30extern "C" int X86TargetMachineModule;
31int X86TargetMachineModule = 0;
32
Dan Gohman844731a2008-05-13 00:00:25 +000033// Register the target.
Daniel Dunbar51b198a2009-07-15 20:24:03 +000034extern Target TheX86_32Target;
Dan Gohman844731a2008-05-13 00:00:25 +000035static RegisterTarget<X86_32TargetMachine>
Daniel Dunbar51b198a2009-07-15 20:24:03 +000036X(TheX86_32Target, "x86", "32-bit X86: Pentium-Pro and above");
37
38extern Target TheX86_64Target;
Dan Gohman844731a2008-05-13 00:00:25 +000039static RegisterTarget<X86_64TargetMachine>
Daniel Dunbar51b198a2009-07-15 20:24:03 +000040Y(TheX86_64Target, "x86-64", "64-bit X86: EM64T and AMD64");
Chris Lattner439a27a2002-12-16 16:15:51 +000041
Bob Wilsona96751f2009-06-23 23:59:40 +000042// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +000043extern "C" void LLVMInitializeX86Target() {
44
45}
Douglas Gregor1555a232009-06-16 20:12:29 +000046
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000047const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000048 if (Subtarget.isFlavorIntel())
49 return new X86WinTargetAsmInfo(*this);
50 else
51 switch (Subtarget.TargetType) {
52 case X86Subtarget::isDarwin:
53 return new X86DarwinTargetAsmInfo(*this);
54 case X86Subtarget::isELF:
55 return new X86ELFTargetAsmInfo(*this);
56 case X86Subtarget::isMingw:
57 case X86Subtarget::isCygwin:
58 return new X86COFFTargetAsmInfo(*this);
59 case X86Subtarget::isWindows:
60 return new X86WinTargetAsmInfo(*this);
61 default:
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000062 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000063 }
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000064}
65
Daniel Dunbar51b198a2009-07-15 20:24:03 +000066X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
67 const std::string &FS)
68 : X86TargetMachine(T, M, FS, false) {
Evan Cheng25ab6902006-09-08 06:48:29 +000069}
70
71
Daniel Dunbar51b198a2009-07-15 20:24:03 +000072X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
73 const std::string &FS)
74 : X86TargetMachine(T, M, FS, true) {
Evan Cheng25ab6902006-09-08 06:48:29 +000075}
76
Chris Lattner11348ee2009-07-09 03:32:31 +000077/// X86TargetMachine ctor - Create an X86 target.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000078///
Daniel Dunbar51b198a2009-07-15 20:24:03 +000079X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
80 const std::string &FS, bool is64Bit)
81 : LLVMTargetMachine(T),
82 Subtarget(M, FS, is64Bit),
Dale Johannesen27f92be2007-08-06 21:48:35 +000083 DataLayout(Subtarget.getDataLayout()),
Nate Begemanfb5792f2005-07-12 01:41:54 +000084 FrameInfo(TargetFrameInfo::StackGrowsDown,
Evan Cheng25ab6902006-09-08 06:48:29 +000085 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000086 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Chengaabe38b2007-12-22 09:40:20 +000087 DefRelocModel = getRelocationModel();
Chris Lattner11348ee2009-07-09 03:32:31 +000088
89 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000090 if (getRelocationModel() == Reloc::Default) {
Chris Lattner11348ee2009-07-09 03:32:31 +000091 if (!Subtarget.isTargetDarwin())
Evan Cheng2c312ad2006-12-04 18:07:10 +000092 setRelocationModel(Reloc::Static);
Chris Lattner11348ee2009-07-09 03:32:31 +000093 else if (Subtarget.is64Bit())
94 setRelocationModel(Reloc::PIC_);
95 else
96 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000097 }
Dan Gohman6520e202008-10-18 02:06:02 +000098
Chris Lattnere4df7562009-07-09 03:15:51 +000099 assert(getRelocationModel() != Reloc::Default &&
100 "Relocation mode not picked");
101
Chris Lattner11348ee2009-07-09 03:32:31 +0000102 // If no code model is picked, default to small.
103 if (getCodeModel() == CodeModel::Default)
104 setCodeModel(CodeModel::Small);
105
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000106 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner11348ee2009-07-09 03:32:31 +0000107 // is defined as a model for code which may be used in static or dynamic
Chris Lattner88e1fd52009-07-09 04:24:46 +0000108 // executables but not necessarily a shared library. On X86-32 we just
109 // compile in -static mode, in x86-64 we use PIC.
110 if (getRelocationModel() == Reloc::DynamicNoPIC) {
111 if (is64Bit)
112 setRelocationModel(Reloc::PIC_);
113 else if (!Subtarget.isTargetDarwin())
114 setRelocationModel(Reloc::Static);
115 }
Dan Gohman6520e202008-10-18 02:06:02 +0000116
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000117 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
118 // the Mach-O file format doesn't support it.
119 if (getRelocationModel() == Reloc::Static &&
120 Subtarget.isTargetDarwin() &&
121 is64Bit)
122 setRelocationModel(Reloc::PIC_);
123
Chris Lattner11348ee2009-07-09 03:32:31 +0000124 // Determine the PICStyle based on the target selected.
125 if (getRelocationModel() == Reloc::Static) {
126 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
127 Subtarget.setPICStyle(PICStyles::None);
128 } else if (Subtarget.isTargetCygMing()) {
Chris Lattnere4df7562009-07-09 03:15:51 +0000129 Subtarget.setPICStyle(PICStyles::None);
130 } else if (Subtarget.isTargetDarwin()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000131 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000132 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattner8097b652009-07-10 20:58:47 +0000133 else if (getRelocationModel() == Reloc::PIC_)
134 Subtarget.setPICStyle(PICStyles::StubPIC);
135 else {
136 assert(getRelocationModel() == Reloc::DynamicNoPIC);
137 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
138 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000139 } else if (Subtarget.isTargetELF()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000140 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000141 Subtarget.setPICStyle(PICStyles::RIPRel);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000142 else
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000143 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000144 }
Chris Lattnere4df7562009-07-09 03:15:51 +0000145
Chris Lattner11348ee2009-07-09 03:32:31 +0000146 // Finally, if we have "none" as our PIC style, force to static mode.
147 if (Subtarget.getPICStyle() == PICStyles::None)
148 setRelocationModel(Reloc::Static);
Chris Lattner4efab052006-02-03 18:59:39 +0000149}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000150
Chris Lattner1911fd42006-09-04 04:14:57 +0000151//===----------------------------------------------------------------------===//
152// Pass Pipeline Configuration
153//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000154
Bill Wendling98a366d2009-04-29 23:29:43 +0000155bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
156 CodeGenOpt::Level OptLevel) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000157 // Install an instruction selector.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000158 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman71b7f642008-10-25 17:46:52 +0000159
160 // If we're using Fast-ISel, clean up the mess.
161 if (EnableFastISel)
162 PM.add(createDeadMachineInstructionElimPass());
163
Dan Gohmanbc5cbb82008-11-12 22:55:05 +0000164 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
165 PM.add(createX87FPRegKillInserterPass());
166
Chris Lattner1911fd42006-09-04 04:14:57 +0000167 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000168}
169
Bill Wendling98a366d2009-04-29 23:29:43 +0000170bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
171 CodeGenOpt::Level OptLevel) {
Anton Korobeynikovd52bdaf2008-04-23 18:23:30 +0000172 // Calculate and set max stack object alignment early, so we can decide
173 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov856914f2008-04-23 18:23:05 +0000174 PM.add(createX86MaxStackAlignmentCalculatorPass());
175 return false; // -print-machineinstr shouldn't print after this.
176}
177
Bill Wendling98a366d2009-04-29 23:29:43 +0000178bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
179 CodeGenOpt::Level OptLevel) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000180 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000181 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000182}
183
Bill Wendling98a366d2009-04-29 23:29:43 +0000184bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
185 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000186 MachineCodeEmitter &MCE) {
Chris Lattner2130b992006-09-04 18:48:41 +0000187 // FIXME: Move this to TargetJITInfo!
Dale Johannesen5f777192008-08-12 21:02:08 +0000188 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
189 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000190 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Chengaabe38b2007-12-22 09:40:20 +0000191 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000192 Subtarget.setPICStyle(PICStyles::None);
193 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000194
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000195 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen5f777192008-08-12 21:02:08 +0000196 // On Darwin, use small code model but hack the call instruction for
197 // externals. Elsewhere, do not assume globals are in the lower 4G.
198 if (Subtarget.is64Bit()) {
199 if (Subtarget.isTargetDarwin())
200 setCodeModel(CodeModel::Small);
201 else
202 setCodeModel(CodeModel::Large);
203 }
Anton Korobeynikov15fccf12006-12-20 01:03:20 +0000204
Evan Cheng55fc2802006-07-25 20:40:54 +0000205 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng8bd60352007-07-20 21:56:13 +0000206
Chris Lattner81b6ed72005-07-11 05:17:48 +0000207 return false;
208}
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000209
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +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().
215 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 }
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000220
221 // 64-bit JIT places everything in the same buffer except external functions.
222 // On Darwin, use small code model but hack the call instruction for
223 // externals. Elsewhere, do not assume globals are in the lower 4G.
224 if (Subtarget.is64Bit()) {
225 if (Subtarget.isTargetDarwin())
226 setCodeModel(CodeModel::Small);
227 else
228 setCodeModel(CodeModel::Large);
229 }
230
231 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000232
233 return false;
234}
235
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000236bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
237 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000238 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000239 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000240 return false;
241}
242
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000243bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000244 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000245 MachineCodeEmitter &MCE) {
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000246 PM.add(createX86CodeEmitterPass(*this, MCE));
247 return false;
248}
Dan Gohman97135e12008-09-26 19:15:30 +0000249
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000250bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
251 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000252 JITCodeEmitter &JCE) {
253 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000254 return false;
255}
256
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000257bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
258 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000259 ObjectCodeEmitter &OCE) {
260 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000261 return false;
262}