blob: a20e1c448968c3cf2d0ae7460e57fca98e9c48b7 [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
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +000039// No assembler printer by default
40X86TargetMachine::AsmPrinterCtorFn X86TargetMachine::AsmPrinterCtor = 0;
41
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikovf700e682008-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 Korobeynikov3829e8a2008-09-25 21:00:33 +000057 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikovf700e682008-07-09 13:20:48 +000058 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059}
60
61unsigned X86_32TargetMachine::getJITMatchQuality() {
62#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
63 return 10;
64#endif
65 return 0;
66}
67
68unsigned X86_64TargetMachine::getJITMatchQuality() {
Anton Korobeynikov57348662008-03-23 13:43:47 +000069#if defined(__x86_64__) || defined(_M_AMD64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 return 10;
71#endif
72 return 0;
73}
74
75unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
76 // 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;
81 // If the target triple is something non-X86, we don't match.
82 if (!TT.empty()) return 0;
83
84 if (M.getEndianness() == Module::LittleEndian &&
85 M.getPointerSize() == Module::Pointer32)
86 return 10; // Weak match
87 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}
93
94unsigned 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
101 // 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
106 // If the target triple is something non-X86-64, we don't match.
107 if (!TT.empty()) return 0;
108
109 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
119X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
120 : 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
128/// X86TargetMachine ctor - Create an ILP32 architecture model
129///
130X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
131 bool is64Bit)
132 : Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +0000133 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 FrameInfo(TargetFrameInfo::StackGrowsDown,
135 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
136 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000137 DefRelocModel = getRelocationModel();
Anton Korobeynikov0ab9fa82008-03-23 13:41:18 +0000138 // FIXME: Correctly select PIC model for Win64 stuff
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000139 if (getRelocationModel() == Reloc::Default) {
Anton Korobeynikov0ab9fa82008-03-23 13:41:18 +0000140 if (Subtarget.isTargetDarwin() ||
141 (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 setRelocationModel(Reloc::DynamicNoPIC);
143 else
144 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000145 }
Dan Gohman36322c72008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
163
164 if (Subtarget.isTargetCygMing())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000165 Subtarget.setPICStyle(PICStyles::WinPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000166 else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 if (Subtarget.is64Bit())
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000168 Subtarget.setPICStyle(PICStyles::RIPRel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 else
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000170 Subtarget.setPICStyle(PICStyles::Stub);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000171 } else if (Subtarget.isTargetELF()) {
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::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000176 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177}
178
179//===----------------------------------------------------------------------===//
180// Pass Pipeline Configuration
181//===----------------------------------------------------------------------===//
182
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000183bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 // Install an instruction selector.
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000185 PM.add(createX86ISelDag(*this, Fast));
Dan Gohman0e66ce82008-10-25 17:46:52 +0000186
187 // If we're using Fast-ISel, clean up the mess.
188 if (EnableFastISel)
189 PM.add(createDeadMachineInstructionElimPass());
190
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000191 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
192 PM.add(createX87FPRegKillInserterPass());
193
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 return false;
195}
196
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000197bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000198 // Calculate and set max stack object alignment early, so we can decide
199 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000200 PM.add(createX86MaxStackAlignmentCalculatorPass());
201 return false; // -print-machineinstr shouldn't print after this.
202}
203
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000204bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 PM.add(createX86FloatingPointStackifierPass());
206 return true; // -print-machineinstr should print after this.
207}
208
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000209bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
210 bool Verbose, raw_ostream &Out) {
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000211 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
212 if (AsmPrinterCtor)
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000213 PM.add(AsmPrinterCtor(Out, *this, Fast, Verbose));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 return false;
215}
216
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000217bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000218 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000220 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
221 if (DefRelocModel == Reloc::Default &&
222 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000223 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224
Dale Johannesenc501c082008-08-11 23:46:25 +0000225 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000226 // On Darwin, use small code model but hack the call instruction for
227 // externals. Elsewhere, do not assume globals are in the lower 4G.
228 if (Subtarget.is64Bit()) {
229 if (Subtarget.isTargetDarwin())
230 setCodeModel(CodeModel::Small);
231 else
232 setCodeModel(CodeModel::Large);
233 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234
235 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000236 if (DumpAsm) {
237 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
238 if (AsmPrinterCtor)
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000239 PM.add(AsmPrinterCtor(errs(), *this, Fast, true));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000240 }
Evan Cheng77547212007-07-20 21:56:13 +0000241
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 return false;
243}
244
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000245bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
246 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000248 if (DumpAsm) {
249 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
250 if (AsmPrinterCtor)
Bill Wendlingf4d0c732009-04-28 01:04:53 +0000251 PM.add(AsmPrinterCtor(errs(), *this, Fast, true));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000252 }
253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 return false;
255}
Dan Gohmanc6413362008-09-26 19:15:30 +0000256
Dan Gohman56e326f2009-02-07 00:42:54 +0000257/// symbolicAddressesAreRIPRel - Return true if symbolic addresses are
258/// RIP-relative on this machine, taking into consideration the relocation
259/// model and subtarget. RIP-relative addresses cannot have a separate
260/// base or index register.
Dan Gohmanc6413362008-09-26 19:15:30 +0000261bool X86TargetMachine::symbolicAddressesAreRIPRel() const {
262 return getRelocationModel() != Reloc::Static &&
263 Subtarget.isPICStyleRIPRel();
264}