blob: ba3314c6e77dfe411d51ff7e74c8a9b974a2b1c2 [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"
21#include "llvm/Target/TargetOptions.h"
22#include "llvm/Target/TargetMachineRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023using namespace llvm;
24
25/// X86TargetMachineModule - Note that this is used on hosts that cannot link
26/// in a library unless there are references into the library. In particular,
27/// it seems that it is not possible to get things to work on Win32 without
28/// this. Though it is unused, do not remove it.
29extern "C" int X86TargetMachineModule;
30int X86TargetMachineModule = 0;
31
Dan Gohman089efff2008-05-13 00:00:25 +000032// Register the target.
33static RegisterTarget<X86_32TargetMachine>
34X("x86", " 32-bit X86: Pentium-Pro and above");
35static RegisterTarget<X86_64TargetMachine>
36Y("x86-64", " 64-bit X86: EM64T and AMD64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +000038// No assembler printer by default
39X86TargetMachine::AsmPrinterCtorFn X86TargetMachine::AsmPrinterCtor = 0;
40
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikovf700e682008-07-09 13:20:48 +000042 if (Subtarget.isFlavorIntel())
43 return new X86WinTargetAsmInfo(*this);
44 else
45 switch (Subtarget.TargetType) {
46 case X86Subtarget::isDarwin:
47 return new X86DarwinTargetAsmInfo(*this);
48 case X86Subtarget::isELF:
49 return new X86ELFTargetAsmInfo(*this);
50 case X86Subtarget::isMingw:
51 case X86Subtarget::isCygwin:
52 return new X86COFFTargetAsmInfo(*this);
53 case X86Subtarget::isWindows:
54 return new X86WinTargetAsmInfo(*this);
55 default:
56 return new X86TargetAsmInfo(*this);
57 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058}
59
60unsigned X86_32TargetMachine::getJITMatchQuality() {
61#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
62 return 10;
63#endif
64 return 0;
65}
66
67unsigned X86_64TargetMachine::getJITMatchQuality() {
Anton Korobeynikov57348662008-03-23 13:43:47 +000068#if defined(__x86_64__) || defined(_M_AMD64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 return 10;
70#endif
71 return 0;
72}
73
74unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
75 // We strongly match "i[3-9]86-*".
76 std::string TT = M.getTargetTriple();
77 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
78 TT[4] == '-' && TT[1] - '3' < 6)
79 return 20;
80 // If the target triple is something non-X86, we don't match.
81 if (!TT.empty()) return 0;
82
83 if (M.getEndianness() == Module::LittleEndian &&
84 M.getPointerSize() == Module::Pointer32)
85 return 10; // Weak match
86 else if (M.getEndianness() != Module::AnyEndianness ||
87 M.getPointerSize() != Module::AnyPointerSize)
88 return 0; // Match for some other target
89
90 return getJITMatchQuality()/2;
91}
92
93unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
94 // We strongly match "x86_64-*".
95 std::string TT = M.getTargetTriple();
96 if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
97 TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
98 return 20;
99
100 // We strongly match "amd64-*".
101 if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
102 TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
103 return 20;
104
105 // If the target triple is something non-X86-64, we don't match.
106 if (!TT.empty()) return 0;
107
108 if (M.getEndianness() == Module::LittleEndian &&
109 M.getPointerSize() == Module::Pointer64)
110 return 10; // Weak match
111 else if (M.getEndianness() != Module::AnyEndianness ||
112 M.getPointerSize() != Module::AnyPointerSize)
113 return 0; // Match for some other target
114
115 return getJITMatchQuality()/2;
116}
117
118X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
119 : X86TargetMachine(M, FS, false) {
120}
121
122
123X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
124 : X86TargetMachine(M, FS, true) {
125}
126
127/// X86TargetMachine ctor - Create an ILP32 architecture model
128///
129X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
130 bool is64Bit)
131 : Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +0000132 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 FrameInfo(TargetFrameInfo::StackGrowsDown,
134 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
135 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000136 DefRelocModel = getRelocationModel();
Anton Korobeynikov0ab9fa82008-03-23 13:41:18 +0000137 // FIXME: Correctly select PIC model for Win64 stuff
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000138 if (getRelocationModel() == Reloc::Default) {
Anton Korobeynikov0ab9fa82008-03-23 13:41:18 +0000139 if (Subtarget.isTargetDarwin() ||
140 (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 setRelocationModel(Reloc::DynamicNoPIC);
142 else
143 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000144 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 if (Subtarget.is64Bit()) {
146 // No DynamicNoPIC support under X86-64.
147 if (getRelocationModel() == Reloc::DynamicNoPIC)
148 setRelocationModel(Reloc::PIC_);
149 // Default X86-64 code model is small.
150 if (getCodeModel() == CodeModel::Default)
151 setCodeModel(CodeModel::Small);
152 }
153
154 if (Subtarget.isTargetCygMing())
155 Subtarget.setPICStyle(PICStyle::WinPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000156 else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 if (Subtarget.is64Bit())
158 Subtarget.setPICStyle(PICStyle::RIPRel);
159 else
160 Subtarget.setPICStyle(PICStyle::Stub);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000161 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 if (Subtarget.is64Bit())
163 Subtarget.setPICStyle(PICStyle::RIPRel);
164 else
165 Subtarget.setPICStyle(PICStyle::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000166 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167}
168
169//===----------------------------------------------------------------------===//
170// Pass Pipeline Configuration
171//===----------------------------------------------------------------------===//
172
Dan Gohmane34aa772008-03-11 22:29:46 +0000173bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 // Install an instruction selector.
175 PM.add(createX86ISelDag(*this, Fast));
176 return false;
177}
178
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000179bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000180 // Calculate and set max stack object alignment early, so we can decide
181 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000182 PM.add(createX86MaxStackAlignmentCalculatorPass());
183 return false; // -print-machineinstr shouldn't print after this.
184}
185
Dan Gohmane34aa772008-03-11 22:29:46 +0000186bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 PM.add(createX86FloatingPointStackifierPass());
188 return true; // -print-machineinstr should print after this.
189}
190
Dan Gohmane34aa772008-03-11 22:29:46 +0000191bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 std::ostream &Out) {
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000193 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
194 if (AsmPrinterCtor)
195 PM.add(AsmPrinterCtor(Out, *this));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 return false;
197}
198
Dan Gohmane34aa772008-03-11 22:29:46 +0000199bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000200 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000202 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
203 if (DefRelocModel == Reloc::Default &&
204 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000205 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206
Dale Johannesenc501c082008-08-11 23:46:25 +0000207 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000208 // On Darwin, use small code model but hack the call instruction for
209 // externals. Elsewhere, do not assume globals are in the lower 4G.
210 if (Subtarget.is64Bit()) {
211 if (Subtarget.isTargetDarwin())
212 setCodeModel(CodeModel::Small);
213 else
214 setCodeModel(CodeModel::Large);
215 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216
217 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000218 if (DumpAsm) {
219 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
220 if (AsmPrinterCtor)
221 PM.add(AsmPrinterCtor(*cerr.stream(), *this));
222 }
Evan Cheng77547212007-07-20 21:56:13 +0000223
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 return false;
225}
226
Dan Gohmane34aa772008-03-11 22:29:46 +0000227bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000228 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 PM.add(createX86CodeEmitterPass(*this, MCE));
Anton Korobeynikov9ff5bee2008-08-17 13:53:59 +0000230 if (DumpAsm) {
231 assert(AsmPrinterCtor && "AsmPrinter was not linked in");
232 if (AsmPrinterCtor)
233 PM.add(AsmPrinterCtor(*cerr.stream(), *this));
234 }
235
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 return false;
237}