blob: 53c46c3595ed5ed96fafbb027e36dfd450691636 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86 specific subclass of TargetMachine.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86TargetAsmInfo.h"
15#include "X86TargetMachine.h"
16#include "X86.h"
17#include "llvm/Module.h"
18#include "llvm/PassManager.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/Passes.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000021#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetOptions.h"
23#include "llvm/Target/TargetMachineRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
26/// 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 Gohman089efff2008-05-13 00:00:25 +000033// Register the target.
34static RegisterTarget<X86_32TargetMachine>
Dan Gohman669b9bf2008-10-14 20:25:08 +000035X("x86", "32-bit X86: Pentium-Pro and above");
Dan Gohman089efff2008-05-13 00:00:25 +000036static RegisterTarget<X86_64TargetMachine>
Dan Gohman669b9bf2008-10-14 20:25:08 +000037Y("x86-64", "64-bit X86: EM64T and AMD64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038
Douglas Gregor1dc5ff42009-06-16 20:12:29 +000039// Force static initialization when called from llvm/InitializeAllTargets.h
40namespace llvm {
41 void InitializeX86Target() { }
42}
43
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +000044// No assembler printer by default
45X86TargetMachine::AsmPrinterCtorFn X86TargetMachine::AsmPrinterCtor = 0;
46
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikovf700e682008-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 Korobeynikov3829e8a2008-09-25 21:00:33 +000062 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikovf700e682008-07-09 13:20:48 +000063 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064}
65
66unsigned X86_32TargetMachine::getJITMatchQuality() {
67#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
68 return 10;
69#endif
70 return 0;
71}
72
73unsigned X86_64TargetMachine::getJITMatchQuality() {
Anton Korobeynikov57348662008-03-23 13:43:47 +000074#if defined(__x86_64__) || defined(_M_AMD64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 return 10;
76#endif
77 return 0;
78}
79
80unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
81 // We strongly match "i[3-9]86-*".
82 std::string TT = M.getTargetTriple();
83 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
84 TT[4] == '-' && TT[1] - '3' < 6)
85 return 20;
86 // If the target triple is something non-X86, we don't match.
87 if (!TT.empty()) return 0;
88
89 if (M.getEndianness() == Module::LittleEndian &&
90 M.getPointerSize() == Module::Pointer32)
91 return 10; // Weak match
92 else if (M.getEndianness() != Module::AnyEndianness ||
93 M.getPointerSize() != Module::AnyPointerSize)
94 return 0; // Match for some other target
95
96 return getJITMatchQuality()/2;
97}
98
99unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
100 // We strongly match "x86_64-*".
101 std::string TT = M.getTargetTriple();
102 if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
103 TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
104 return 20;
105
106 // We strongly match "amd64-*".
107 if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
108 TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
109 return 20;
110
111 // If the target triple is something non-X86-64, we don't match.
112 if (!TT.empty()) return 0;
113
114 if (M.getEndianness() == Module::LittleEndian &&
115 M.getPointerSize() == Module::Pointer64)
116 return 10; // Weak match
117 else if (M.getEndianness() != Module::AnyEndianness ||
118 M.getPointerSize() != Module::AnyPointerSize)
119 return 0; // Match for some other target
120
121 return getJITMatchQuality()/2;
122}
123
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000124X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 : X86TargetMachine(M, FS, false) {
126}
127
128
129X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
130 : X86TargetMachine(M, FS, true) {
131}
132
133/// X86TargetMachine ctor - Create an ILP32 architecture model
134///
135X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
136 bool is64Bit)
137 : Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +0000138 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 FrameInfo(TargetFrameInfo::StackGrowsDown,
140 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000141 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000142 DefRelocModel = getRelocationModel();
Anton Korobeynikov0ab9fa82008-03-23 13:41:18 +0000143 // FIXME: Correctly select PIC model for Win64 stuff
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000144 if (getRelocationModel() == Reloc::Default) {
Anton Korobeynikov0ab9fa82008-03-23 13:41:18 +0000145 if (Subtarget.isTargetDarwin() ||
146 (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 setRelocationModel(Reloc::DynamicNoPIC);
148 else
149 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000150 }
Dan Gohman36322c72008-10-18 02:06:02 +0000151
152 // ELF doesn't have a distinct dynamic-no-PIC model. Dynamic-no-PIC
153 // is defined as a model for code which may be used in static or
154 // dynamic executables but not necessarily a shared library. On ELF
155 // implement this by using the Static model.
156 if (Subtarget.isTargetELF() &&
157 getRelocationModel() == Reloc::DynamicNoPIC)
158 setRelocationModel(Reloc::Static);
159
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 if (Subtarget.is64Bit()) {
161 // No DynamicNoPIC support under X86-64.
162 if (getRelocationModel() == Reloc::DynamicNoPIC)
163 setRelocationModel(Reloc::PIC_);
164 // Default X86-64 code model is small.
165 if (getCodeModel() == CodeModel::Default)
166 setCodeModel(CodeModel::Small);
167 }
168
169 if (Subtarget.isTargetCygMing())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000170 Subtarget.setPICStyle(PICStyles::WinPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000171 else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000173 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000175 Subtarget.setPICStyle(PICStyles::Stub);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000176 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000178 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000180 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000181 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182}
183
184//===----------------------------------------------------------------------===//
185// Pass Pipeline Configuration
186//===----------------------------------------------------------------------===//
187
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000188bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
189 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 // Install an instruction selector.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000191 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000192
193 // If we're using Fast-ISel, clean up the mess.
194 if (EnableFastISel)
195 PM.add(createDeadMachineInstructionElimPass());
196
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000197 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
198 PM.add(createX87FPRegKillInserterPass());
199
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 return false;
201}
202
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000203bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
204 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000205 // Calculate and set max stack object alignment early, so we can decide
206 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000207 PM.add(createX86MaxStackAlignmentCalculatorPass());
208 return false; // -print-machineinstr shouldn't print after this.
209}
210
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000211bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
212 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 PM.add(createX86FloatingPointStackifierPass());
214 return true; // -print-machineinstr should print after this.
215}
216
Bill Wendling58ed5d22009-04-29 00:15:41 +0000217bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000218 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000219 bool Verbose,
220 raw_ostream &Out) {
Evan Chengabb785b2009-06-03 21:13:54 +0000221 // FIXME: Move this somewhere else!
222 // On Darwin, override 64-bit static relocation to pic_ since the
223 // assembler doesn't support it.
224 if (DefRelocModel == Reloc::Static &&
225 Subtarget.isTargetDarwin() && Subtarget.is64Bit())
226 setRelocationModel(Reloc::PIC_);
227
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000228 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
229 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000230 PM.add(AsmPrinterCtor(Out, *this, OptLevel, Verbose));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 return false;
232}
233
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000234bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
235 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000236 bool DumpAsm,
237 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000239 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
240 if (DefRelocModel == Reloc::Default &&
241 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000242 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243
Dale Johannesenc501c082008-08-11 23:46:25 +0000244 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000245 // On Darwin, use small code model but hack the call instruction for
246 // externals. Elsewhere, do not assume globals are in the lower 4G.
247 if (Subtarget.is64Bit()) {
248 if (Subtarget.isTargetDarwin())
249 setCodeModel(CodeModel::Small);
250 else
251 setCodeModel(CodeModel::Large);
252 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253
254 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000255 if (DumpAsm) {
256 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
257 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000258 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000259 }
Evan Cheng77547212007-07-20 21:56:13 +0000260
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 return false;
262}
263
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000264bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
265 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000266 bool DumpAsm,
267 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000268 // FIXME: Move this to TargetJITInfo!
269 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
270 if (DefRelocModel == Reloc::Default &&
271 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
272 setRelocationModel(Reloc::Static);
273
274 // 64-bit JIT places everything in the same buffer except external functions.
275 // On Darwin, use small code model but hack the call instruction for
276 // externals. Elsewhere, do not assume globals are in the lower 4G.
277 if (Subtarget.is64Bit()) {
278 if (Subtarget.isTargetDarwin())
279 setCodeModel(CodeModel::Small);
280 else
281 setCodeModel(CodeModel::Large);
282 }
283
284 PM.add(createX86JITCodeEmitterPass(*this, JCE));
285 if (DumpAsm) {
286 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
287 if (AsmPrinterCtor)
288 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
289 }
290
291 return false;
292}
293
Bill Wendling58ed5d22009-04-29 00:15:41 +0000294bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000295 CodeGenOpt::Level OptLevel,
296 bool DumpAsm,
Bill Wendling58ed5d22009-04-29 00:15:41 +0000297 MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000299 if (DumpAsm) {
300 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
301 if (AsmPrinterCtor)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000302 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000303 }
304
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 return false;
306}
Dan Gohmanc6413362008-09-26 19:15:30 +0000307
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000308bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
309 CodeGenOpt::Level OptLevel,
310 bool DumpAsm,
311 JITCodeEmitter &JCE) {
312 PM.add(createX86JITCodeEmitterPass(*this, JCE));
313 if (DumpAsm) {
314 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
315 if (AsmPrinterCtor)
316 PM.add(AsmPrinterCtor(errs(), *this, OptLevel, true));
317 }
318
319 return false;
320}
321
Dan Gohman56e326f2009-02-07 00:42:54 +0000322/// symbolicAddressesAreRIPRel - Return true if symbolic addresses are
323/// RIP-relative on this machine, taking into consideration the relocation
324/// model and subtarget. RIP-relative addresses cannot have a separate
325/// base or index register.
Dan Gohmanc6413362008-09-26 19:15:30 +0000326bool X86TargetMachine::symbolicAddressesAreRIPRel() const {
327 return getRelocationModel() != Reloc::Static &&
328 Subtarget.isPICStyleRIPRel();
329}