blob: 8264462506aaee65d1dd66ee734adfa8352296f5 [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"
Owen Andersoncb371882008-08-21 00:14:44 +000021#include "llvm/Support/raw_ostream.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.
34static RegisterTarget<X86_32TargetMachine>
Dan Gohmanb8cab922008-10-14 20:25:08 +000035X("x86", "32-bit X86: Pentium-Pro and above");
Dan Gohman844731a2008-05-13 00:00:25 +000036static RegisterTarget<X86_64TargetMachine>
Dan Gohmanb8cab922008-10-14 20:25:08 +000037Y("x86-64", "64-bit X86: EM64T and AMD64");
Chris Lattner439a27a2002-12-16 16:15:51 +000038
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +000039// No assembler printer by default
40X86TargetMachine::AsmPrinterCtorFn X86TargetMachine::AsmPrinterCtor = 0;
41
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000042const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000043 if (Subtarget.isFlavorIntel())
44 return new X86WinTargetAsmInfo(*this);
45 else
46 switch (Subtarget.TargetType) {
47 case X86Subtarget::isDarwin:
48 return new X86DarwinTargetAsmInfo(*this);
49 case X86Subtarget::isELF:
50 return new X86ELFTargetAsmInfo(*this);
51 case X86Subtarget::isMingw:
52 case X86Subtarget::isCygwin:
53 return new X86COFFTargetAsmInfo(*this);
54 case X86Subtarget::isWindows:
55 return new X86WinTargetAsmInfo(*this);
56 default:
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000057 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000058 }
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000059}
60
Evan Cheng25ab6902006-09-08 06:48:29 +000061unsigned X86_32TargetMachine::getJITMatchQuality() {
Chris Lattner7d0974b2004-10-18 15:54:17 +000062#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
Chris Lattnerd36c9702004-07-11 02:48:49 +000063 return 10;
Chris Lattnerd36c9702004-07-11 02:48:49 +000064#endif
Evan Cheng25ab6902006-09-08 06:48:29 +000065 return 0;
Chris Lattnerd36c9702004-07-11 02:48:49 +000066}
67
Evan Cheng25ab6902006-09-08 06:48:29 +000068unsigned X86_64TargetMachine::getJITMatchQuality() {
Anton Korobeynikov8c278292008-03-23 13:43:47 +000069#if defined(__x86_64__) || defined(_M_AMD64)
Evan Cheng25ab6902006-09-08 06:48:29 +000070 return 10;
71#endif
72 return 0;
73}
74
75unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
Chris Lattner3ea78c42004-12-12 17:40:28 +000076 // We strongly match "i[3-9]86-*".
77 std::string TT = M.getTargetTriple();
78 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
79 TT[4] == '-' && TT[1] - '3' < 6)
80 return 20;
Chris Lattner87bdba62007-07-09 17:25:29 +000081 // If the target triple is something non-X86, we don't match.
82 if (!TT.empty()) return 0;
Chris Lattner3ea78c42004-12-12 17:40:28 +000083
Chris Lattnerd36c9702004-07-11 02:48:49 +000084 if (M.getEndianness() == Module::LittleEndian &&
85 M.getPointerSize() == Module::Pointer32)
Chris Lattner3ea78c42004-12-12 17:40:28 +000086 return 10; // Weak match
Chris Lattnerd36c9702004-07-11 02:48:49 +000087 else if (M.getEndianness() != Module::AnyEndianness ||
88 M.getPointerSize() != Module::AnyPointerSize)
89 return 0; // Match for some other target
90
91 return getJITMatchQuality()/2;
92}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000093
Evan Cheng25ab6902006-09-08 06:48:29 +000094unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
95 // We strongly match "x86_64-*".
96 std::string TT = M.getTargetTriple();
97 if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
98 TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
99 return 20;
100
Chris Lattner21847f42006-12-19 19:40:09 +0000101 // We strongly match "amd64-*".
102 if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
103 TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
104 return 20;
105
Chris Lattner87bdba62007-07-09 17:25:29 +0000106 // If the target triple is something non-X86-64, we don't match.
107 if (!TT.empty()) return 0;
108
Evan Cheng25ab6902006-09-08 06:48:29 +0000109 if (M.getEndianness() == Module::LittleEndian &&
110 M.getPointerSize() == Module::Pointer64)
111 return 10; // Weak match
112 else if (M.getEndianness() != Module::AnyEndianness ||
113 M.getPointerSize() != Module::AnyPointerSize)
114 return 0; // Match for some other target
115
116 return getJITMatchQuality()/2;
117}
118
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000119X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
Evan Cheng25ab6902006-09-08 06:48:29 +0000120 : X86TargetMachine(M, FS, false) {
121}
122
123
124X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
125 : X86TargetMachine(M, FS, true) {
126}
127
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000128/// X86TargetMachine ctor - Create an ILP32 architecture model
129///
Evan Chengcdc69442007-02-23 03:03:16 +0000130X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
131 bool is64Bit)
Evan Cheng25ab6902006-09-08 06:48:29 +0000132 : Subtarget(M, FS, is64Bit),
Dale Johannesen27f92be2007-08-06 21:48:35 +0000133 DataLayout(Subtarget.getDataLayout()),
Nate Begemanfb5792f2005-07-12 01:41:54 +0000134 FrameInfo(TargetFrameInfo::StackGrowsDown,
Evan Cheng25ab6902006-09-08 06:48:29 +0000135 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Evan Chengaa3c1412006-05-30 21:45:53 +0000136 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
Evan Chengaabe38b2007-12-22 09:40:20 +0000137 DefRelocModel = getRelocationModel();
Anton Korobeynikovc15b81b2008-03-23 13:41:18 +0000138 // FIXME: Correctly select PIC model for Win64 stuff
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000139 if (getRelocationModel() == Reloc::Default) {
Anton Korobeynikovc15b81b2008-03-23 13:41:18 +0000140 if (Subtarget.isTargetDarwin() ||
141 (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
Evan Cheng4c1aa862006-02-22 20:19:42 +0000142 setRelocationModel(Reloc::DynamicNoPIC);
143 else
Evan Cheng2c312ad2006-12-04 18:07:10 +0000144 setRelocationModel(Reloc::Static);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000145 }
Dan Gohman6520e202008-10-18 02:06:02 +0000146
147 // ELF doesn't have a distinct dynamic-no-PIC model. Dynamic-no-PIC
148 // is defined as a model for code which may be used in static or
149 // dynamic executables but not necessarily a shared library. On ELF
150 // implement this by using the Static model.
151 if (Subtarget.isTargetELF() &&
152 getRelocationModel() == Reloc::DynamicNoPIC)
153 setRelocationModel(Reloc::Static);
154
Evan Cheng25ab6902006-09-08 06:48:29 +0000155 if (Subtarget.is64Bit()) {
156 // No DynamicNoPIC support under X86-64.
157 if (getRelocationModel() == Reloc::DynamicNoPIC)
158 setRelocationModel(Reloc::PIC_);
159 // Default X86-64 code model is small.
160 if (getCodeModel() == CodeModel::Default)
161 setCodeModel(CodeModel::Small);
162 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000163
Evan Chengae19abc2007-01-18 22:27:12 +0000164 if (Subtarget.isTargetCygMing())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000165 Subtarget.setPICStyle(PICStyles::WinPIC);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000166 else if (Subtarget.isTargetDarwin()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000167 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000168 Subtarget.setPICStyle(PICStyles::RIPRel);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000169 else
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000170 Subtarget.setPICStyle(PICStyles::Stub);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000171 } else if (Subtarget.isTargetELF()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000172 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000173 Subtarget.setPICStyle(PICStyles::RIPRel);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000174 else
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000175 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000176 }
Chris Lattner4efab052006-02-03 18:59:39 +0000177}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000178
Chris Lattner1911fd42006-09-04 04:14:57 +0000179//===----------------------------------------------------------------------===//
180// Pass Pipeline Configuration
181//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000182
Bill Wendling98a366d2009-04-29 23:29:43 +0000183bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
184 CodeGenOpt::Level OptLevel) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000185 // Install an instruction selector.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000186 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman71b7f642008-10-25 17:46:52 +0000187
188 // If we're using Fast-ISel, clean up the mess.
189 if (EnableFastISel)
190 PM.add(createDeadMachineInstructionElimPass());
191
Dan Gohmanbc5cbb82008-11-12 22:55:05 +0000192 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
193 PM.add(createX87FPRegKillInserterPass());
194
Chris Lattner1911fd42006-09-04 04:14:57 +0000195 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000196}
197
Bill Wendling98a366d2009-04-29 23:29:43 +0000198bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
199 CodeGenOpt::Level OptLevel) {
Anton Korobeynikovd52bdaf2008-04-23 18:23:30 +0000200 // Calculate and set max stack object alignment early, so we can decide
201 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov856914f2008-04-23 18:23:05 +0000202 PM.add(createX86MaxStackAlignmentCalculatorPass());
203 return false; // -print-machineinstr shouldn't print after this.
204}
205
Bill Wendling98a366d2009-04-29 23:29:43 +0000206bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
207 CodeGenOpt::Level OptLevel) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000208 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000209 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000210}
211
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000212bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000213 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000214 bool Verbose,
215 raw_ostream &Out) {
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +0000216 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
217 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000218 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
Chris Lattner1911fd42006-09-04 04:14:57 +0000219 return false;
220}
221
Bill Wendling98a366d2009-04-29 23:29:43 +0000222bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
223 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000224 bool DumpAsm,
225 MachineCodeEmitter &MCE) {
Chris Lattner2130b992006-09-04 18:48:41 +0000226 // FIXME: Move this to TargetJITInfo!
Dale Johannesen5f777192008-08-12 21:02:08 +0000227 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
228 if (DefRelocModel == Reloc::Default &&
229 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
Evan Chengaabe38b2007-12-22 09:40:20 +0000230 setRelocationModel(Reloc::Static);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000231
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000232 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen5f777192008-08-12 21:02:08 +0000233 // On Darwin, use small code model but hack the call instruction for
234 // externals. Elsewhere, do not assume globals are in the lower 4G.
235 if (Subtarget.is64Bit()) {
236 if (Subtarget.isTargetDarwin())
237 setCodeModel(CodeModel::Small);
238 else
239 setCodeModel(CodeModel::Large);
240 }
Anton Korobeynikov15fccf12006-12-20 01:03:20 +0000241
Evan Cheng55fc2802006-07-25 20:40:54 +0000242 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +0000243 if (DumpAsm) {
244 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
245 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000246 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +0000247 }
Evan Cheng8bd60352007-07-20 21:56:13 +0000248
Chris Lattner81b6ed72005-07-11 05:17:48 +0000249 return false;
250}
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000251
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000252bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
253 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000254 bool DumpAsm,
255 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000256 // FIXME: Move this to TargetJITInfo!
257 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
258 if (DefRelocModel == Reloc::Default &&
259 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
260 setRelocationModel(Reloc::Static);
261
262 // 64-bit JIT places everything in the same buffer except external functions.
263 // On Darwin, use small code model but hack the call instruction for
264 // externals. Elsewhere, do not assume globals are in the lower 4G.
265 if (Subtarget.is64Bit()) {
266 if (Subtarget.isTargetDarwin())
267 setCodeModel(CodeModel::Small);
268 else
269 setCodeModel(CodeModel::Large);
270 }
271
272 PM.add(createX86JITCodeEmitterPass(*this, JCE));
273 if (DumpAsm) {
274 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
275 if (AsmPrinterCtor)
276 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
277 }
278
279 return false;
280}
281
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000282bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000283 CodeGenOpt::Level OptLevel,
284 bool DumpAsm,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000285 MachineCodeEmitter &MCE) {
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000286 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +0000287 if (DumpAsm) {
288 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
289 if (AsmPrinterCtor)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000290 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov3c3bc482008-08-17 13:53:59 +0000291 }
292
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000293 return false;
294}
Dan Gohman97135e12008-09-26 19:15:30 +0000295
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000296bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
297 CodeGenOpt::Level OptLevel,
298 bool DumpAsm,
299 JITCodeEmitter &JCE) {
300 PM.add(createX86JITCodeEmitterPass(*this, JCE));
301 if (DumpAsm) {
302 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
303 if (AsmPrinterCtor)
304 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
305 }
306
307 return false;
308}
309
Dan Gohman288b8242009-02-07 00:42:54 +0000310/// symbolicAddressesAreRIPRel - Return true if symbolic addresses are
311/// RIP-relative on this machine, taking into consideration the relocation
312/// model and subtarget. RIP-relative addresses cannot have a separate
313/// base or index register.
Dan Gohman97135e12008-09-26 19:15:30 +0000314bool X86TargetMachine::symbolicAddressesAreRIPRel() const {
315 return getRelocationModel() != Reloc::Static &&
316 Subtarget.isPICStyleRIPRel();
317}