blob: fa3ec6904dea114239e7910af5d21c2afb7b45d3 [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
Dan Gohman844731a2008-05-13 00:00:25 +000026// Register the target.
27static RegisterTarget<X86_32TargetMachine>
Daniel Dunbar51b198a2009-07-15 20:24:03 +000028X(TheX86_32Target, "x86", "32-bit X86: Pentium-Pro and above");
29
Dan Gohman844731a2008-05-13 00:00:25 +000030static RegisterTarget<X86_64TargetMachine>
Daniel Dunbar51b198a2009-07-15 20:24:03 +000031Y(TheX86_64Target, "x86-64", "64-bit X86: EM64T and AMD64");
Chris Lattner439a27a2002-12-16 16:15:51 +000032
Bob Wilsona96751f2009-06-23 23:59:40 +000033// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +000034extern "C" void LLVMInitializeX86Target() {
35
36}
Douglas Gregor1555a232009-06-16 20:12:29 +000037
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000038const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000039 if (Subtarget.isFlavorIntel())
40 return new X86WinTargetAsmInfo(*this);
41 else
42 switch (Subtarget.TargetType) {
43 case X86Subtarget::isDarwin:
44 return new X86DarwinTargetAsmInfo(*this);
45 case X86Subtarget::isELF:
46 return new X86ELFTargetAsmInfo(*this);
47 case X86Subtarget::isMingw:
48 case X86Subtarget::isCygwin:
49 return new X86COFFTargetAsmInfo(*this);
50 case X86Subtarget::isWindows:
51 return new X86WinTargetAsmInfo(*this);
52 default:
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000053 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000054 }
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000055}
56
Daniel Dunbar51b198a2009-07-15 20:24:03 +000057X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
58 const std::string &FS)
59 : X86TargetMachine(T, M, FS, false) {
Evan Cheng25ab6902006-09-08 06:48:29 +000060}
61
62
Daniel Dunbar51b198a2009-07-15 20:24:03 +000063X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
64 const std::string &FS)
65 : X86TargetMachine(T, M, 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 Dunbar51b198a2009-07-15 20:24:03 +000070X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
71 const std::string &FS, bool is64Bit)
72 : LLVMTargetMachine(T),
73 Subtarget(M, FS, is64Bit),
Dale Johannesen27f92be2007-08-06 21:48:35 +000074 DataLayout(Subtarget.getDataLayout()),
Nate Begemanfb5792f2005-07-12 01:41:54 +000075 FrameInfo(TargetFrameInfo::StackGrowsDown,
Evan Cheng25ab6902006-09-08 06:48:29 +000076 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000077 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Chengaabe38b2007-12-22 09:40:20 +000078 DefRelocModel = getRelocationModel();
Chris Lattner11348ee2009-07-09 03:32:31 +000079
80 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000081 if (getRelocationModel() == Reloc::Default) {
Chris Lattner11348ee2009-07-09 03:32:31 +000082 if (!Subtarget.isTargetDarwin())
Evan Cheng2c312ad2006-12-04 18:07:10 +000083 setRelocationModel(Reloc::Static);
Chris Lattner11348ee2009-07-09 03:32:31 +000084 else if (Subtarget.is64Bit())
85 setRelocationModel(Reloc::PIC_);
86 else
87 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000088 }
Dan Gohman6520e202008-10-18 02:06:02 +000089
Chris Lattnere4df7562009-07-09 03:15:51 +000090 assert(getRelocationModel() != Reloc::Default &&
91 "Relocation mode not picked");
92
Chris Lattner11348ee2009-07-09 03:32:31 +000093 // If no code model is picked, default to small.
94 if (getCodeModel() == CodeModel::Default)
95 setCodeModel(CodeModel::Small);
96
Chris Lattnerb2fc55b2009-07-09 03:37:30 +000097 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner11348ee2009-07-09 03:32:31 +000098 // is defined as a model for code which may be used in static or dynamic
Chris Lattner88e1fd52009-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 Gohman6520e202008-10-18 02:06:02 +0000107
Chris Lattnerb2fc55b2009-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 Lattner11348ee2009-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 Lattnere4df7562009-07-09 03:15:51 +0000120 Subtarget.setPICStyle(PICStyles::None);
121 } else if (Subtarget.isTargetDarwin()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000122 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000123 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattner8097b652009-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 Korobeynikov7c1c2612008-02-20 11:22:39 +0000130 } else if (Subtarget.isTargetELF()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000131 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000132 Subtarget.setPICStyle(PICStyles::RIPRel);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000133 else
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000134 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000135 }
Chris Lattnere4df7562009-07-09 03:15:51 +0000136
Chris Lattner11348ee2009-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);
Chris Lattner4efab052006-02-03 18:59:39 +0000140}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000141
Chris Lattner1911fd42006-09-04 04:14:57 +0000142//===----------------------------------------------------------------------===//
143// Pass Pipeline Configuration
144//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000145
Bill Wendling98a366d2009-04-29 23:29:43 +0000146bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
147 CodeGenOpt::Level OptLevel) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000148 // Install an instruction selector.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000149 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman71b7f642008-10-25 17:46:52 +0000150
151 // If we're using Fast-ISel, clean up the mess.
152 if (EnableFastISel)
153 PM.add(createDeadMachineInstructionElimPass());
154
Dan Gohmanbc5cbb82008-11-12 22:55:05 +0000155 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
156 PM.add(createX87FPRegKillInserterPass());
157
Chris Lattner1911fd42006-09-04 04:14:57 +0000158 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000159}
160
Bill Wendling98a366d2009-04-29 23:29:43 +0000161bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
162 CodeGenOpt::Level OptLevel) {
Anton Korobeynikovd52bdaf2008-04-23 18:23:30 +0000163 // Calculate and set max stack object alignment early, so we can decide
164 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov856914f2008-04-23 18:23:05 +0000165 PM.add(createX86MaxStackAlignmentCalculatorPass());
166 return false; // -print-machineinstr shouldn't print after this.
167}
168
Bill Wendling98a366d2009-04-29 23:29:43 +0000169bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
170 CodeGenOpt::Level OptLevel) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000171 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000172 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000173}
174
Bill Wendling98a366d2009-04-29 23:29:43 +0000175bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
176 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000177 MachineCodeEmitter &MCE) {
Chris Lattner2130b992006-09-04 18:48:41 +0000178 // FIXME: Move this to TargetJITInfo!
Dale Johannesen5f777192008-08-12 21:02:08 +0000179 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
180 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000181 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Chengaabe38b2007-12-22 09:40:20 +0000182 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000183 Subtarget.setPICStyle(PICStyles::None);
184 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000185
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000186 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen5f777192008-08-12 21:02:08 +0000187 // On Darwin, use small code model but hack the call instruction for
188 // externals. Elsewhere, do not assume globals are in the lower 4G.
189 if (Subtarget.is64Bit()) {
190 if (Subtarget.isTargetDarwin())
191 setCodeModel(CodeModel::Small);
192 else
193 setCodeModel(CodeModel::Large);
194 }
Anton Korobeynikov15fccf12006-12-20 01:03:20 +0000195
Evan Cheng55fc2802006-07-25 20:40:54 +0000196 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng8bd60352007-07-20 21:56:13 +0000197
Chris Lattner81b6ed72005-07-11 05:17:48 +0000198 return false;
199}
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000200
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000201bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
202 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000203 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000204 // FIXME: Move this to TargetJITInfo!
205 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
206 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000207 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000208 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000209 Subtarget.setPICStyle(PICStyles::None);
210 }
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000211
212 // 64-bit JIT places everything in the same buffer except external functions.
213 // On Darwin, use small code model but hack the call instruction for
214 // externals. Elsewhere, do not assume globals are in the lower 4G.
215 if (Subtarget.is64Bit()) {
216 if (Subtarget.isTargetDarwin())
217 setCodeModel(CodeModel::Small);
218 else
219 setCodeModel(CodeModel::Large);
220 }
221
222 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000223
224 return false;
225}
226
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000227bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
228 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000229 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000230 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000231 return false;
232}
233
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000234bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000235 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000236 MachineCodeEmitter &MCE) {
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000237 PM.add(createX86CodeEmitterPass(*this, MCE));
238 return false;
239}
Dan Gohman97135e12008-09-26 19:15:30 +0000240
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000241bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
242 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000243 JITCodeEmitter &JCE) {
244 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000245 return false;
246}
247
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000248bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
249 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000250 ObjectCodeEmitter &OCE) {
251 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000252 return false;
253}