9b0d23aa8f52484a42c67f1f004fed46a93114ec
[oota-llvm.git] / lib / Target / X86 / X86TargetMachine.cpp
1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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/Support/FormattedStream.h"
22 #include "llvm/Target/TargetOptions.h"
23 #include "llvm/Target/TargetMachineRegistry.h"
24 using 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.
30 extern "C" int X86TargetMachineModule;
31 int X86TargetMachineModule = 0;
32
33 // Register the target.
34 extern Target TheX86_32Target;
35 static RegisterTarget<X86_32TargetMachine>
36 X(TheX86_32Target, "x86",    "32-bit X86: Pentium-Pro and above");
37
38 extern Target TheX86_64Target;
39 static RegisterTarget<X86_64TargetMachine>
40 Y(TheX86_64Target, "x86-64", "64-bit X86: EM64T and AMD64");
41
42 // Force static initialization.
43 extern "C" void LLVMInitializeX86Target() { 
44   
45 }
46
47 // No assembler printer by default
48 X86TargetMachine::AsmPrinterCtorFn X86TargetMachine::AsmPrinterCtor = 0;
49
50 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
51   if (Subtarget.isFlavorIntel())
52     return new X86WinTargetAsmInfo(*this);
53   else
54     switch (Subtarget.TargetType) {
55      case X86Subtarget::isDarwin:
56       return new X86DarwinTargetAsmInfo(*this);
57      case X86Subtarget::isELF:
58       return new X86ELFTargetAsmInfo(*this);
59      case X86Subtarget::isMingw:
60      case X86Subtarget::isCygwin:
61       return new X86COFFTargetAsmInfo(*this);
62      case X86Subtarget::isWindows:
63       return new X86WinTargetAsmInfo(*this);
64      default:
65       return new X86GenericTargetAsmInfo(*this);
66     }
67 }
68
69 unsigned X86_32TargetMachine::getJITMatchQuality() {
70 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
71   return 10;
72 #endif
73   return 0;
74 }
75
76 unsigned X86_64TargetMachine::getJITMatchQuality() {
77 #if defined(__x86_64__) || defined(_M_AMD64)
78   return 10;
79 #endif
80   return 0;
81 }
82
83 unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
84   // We strongly match "i[3-9]86-*".
85   std::string TT = M.getTargetTriple();
86   if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
87       TT[4] == '-' && TT[1] - '3' < 6)
88     return 20;
89   // If the target triple is something non-X86, we don't match.
90   if (!TT.empty()) return 0;
91
92   if (M.getEndianness()  == Module::LittleEndian &&
93       M.getPointerSize() == Module::Pointer32)
94     return 10;                                   // Weak match
95   else if (M.getEndianness() != Module::AnyEndianness ||
96            M.getPointerSize() != Module::AnyPointerSize)
97     return 0;                                    // Match for some other target
98
99   return getJITMatchQuality()/2;
100 }
101
102 unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
103   // We strongly match "x86_64-*".
104   std::string TT = M.getTargetTriple();
105   if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
106       TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
107     return 20;
108
109   // We strongly match "amd64-*".
110   if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
111       TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
112     return 20;
113   
114   // If the target triple is something non-X86-64, we don't match.
115   if (!TT.empty()) return 0;
116
117   if (M.getEndianness()  == Module::LittleEndian &&
118       M.getPointerSize() == Module::Pointer64)
119     return 10;                                   // Weak match
120   else if (M.getEndianness() != Module::AnyEndianness ||
121            M.getPointerSize() != Module::AnyPointerSize)
122     return 0;                                    // Match for some other target
123
124   return getJITMatchQuality()/2;
125 }
126
127 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M, 
128                                          const std::string &FS)
129   : X86TargetMachine(T, M, FS, false) {
130 }
131
132
133 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M, 
134                                          const std::string &FS)
135   : X86TargetMachine(T, M, FS, true) {
136 }
137
138 /// X86TargetMachine ctor - Create an X86 target.
139 ///
140 X86TargetMachine::X86TargetMachine(const Target &T, const Module &M, 
141                                    const std::string &FS, bool is64Bit)
142   : LLVMTargetMachine(T), 
143     Subtarget(M, FS, is64Bit),
144     DataLayout(Subtarget.getDataLayout()),
145     FrameInfo(TargetFrameInfo::StackGrowsDown,
146               Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
147     InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
148   DefRelocModel = getRelocationModel();
149       
150   // If no relocation model was picked, default as appropriate for the target.
151   if (getRelocationModel() == Reloc::Default) {
152     if (!Subtarget.isTargetDarwin())
153       setRelocationModel(Reloc::Static);
154     else if (Subtarget.is64Bit())
155       setRelocationModel(Reloc::PIC_);
156     else
157       setRelocationModel(Reloc::DynamicNoPIC);
158   }
159
160   assert(getRelocationModel() != Reloc::Default &&
161          "Relocation mode not picked");
162
163   // If no code model is picked, default to small.
164   if (getCodeModel() == CodeModel::Default)
165     setCodeModel(CodeModel::Small);
166       
167   // ELF and X86-64 don't have a distinct DynamicNoPIC model.  DynamicNoPIC
168   // is defined as a model for code which may be used in static or dynamic
169   // executables but not necessarily a shared library. On X86-32 we just
170   // compile in -static mode, in x86-64 we use PIC.
171   if (getRelocationModel() == Reloc::DynamicNoPIC) {
172     if (is64Bit)
173       setRelocationModel(Reloc::PIC_);
174     else if (!Subtarget.isTargetDarwin())
175       setRelocationModel(Reloc::Static);
176   }
177
178   // If we are on Darwin, disallow static relocation model in X86-64 mode, since
179   // the Mach-O file format doesn't support it.
180   if (getRelocationModel() == Reloc::Static &&
181       Subtarget.isTargetDarwin() &&
182       is64Bit)
183     setRelocationModel(Reloc::PIC_);
184       
185   // Determine the PICStyle based on the target selected.
186   if (getRelocationModel() == Reloc::Static) {
187     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
188     Subtarget.setPICStyle(PICStyles::None);
189   } else if (Subtarget.isTargetCygMing()) {
190     Subtarget.setPICStyle(PICStyles::None);
191   } else if (Subtarget.isTargetDarwin()) {
192     if (Subtarget.is64Bit())
193       Subtarget.setPICStyle(PICStyles::RIPRel);
194     else if (getRelocationModel() == Reloc::PIC_)
195       Subtarget.setPICStyle(PICStyles::StubPIC);
196     else {
197       assert(getRelocationModel() == Reloc::DynamicNoPIC);
198       Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
199     }
200   } else if (Subtarget.isTargetELF()) {
201     if (Subtarget.is64Bit())
202       Subtarget.setPICStyle(PICStyles::RIPRel);
203     else
204       Subtarget.setPICStyle(PICStyles::GOT);
205   }
206       
207   // Finally, if we have "none" as our PIC style, force to static mode.
208   if (Subtarget.getPICStyle() == PICStyles::None)
209     setRelocationModel(Reloc::Static);
210 }
211
212 //===----------------------------------------------------------------------===//
213 // Pass Pipeline Configuration
214 //===----------------------------------------------------------------------===//
215
216 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
217                                        CodeGenOpt::Level OptLevel) {
218   // Install an instruction selector.
219   PM.add(createX86ISelDag(*this, OptLevel));
220
221   // If we're using Fast-ISel, clean up the mess.
222   if (EnableFastISel)
223     PM.add(createDeadMachineInstructionElimPass());
224
225   // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
226   PM.add(createX87FPRegKillInserterPass());
227
228   return false;
229 }
230
231 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
232                                       CodeGenOpt::Level OptLevel) {
233   // Calculate and set max stack object alignment early, so we can decide
234   // whether we will need stack realignment (and thus FP).
235   PM.add(createX86MaxStackAlignmentCalculatorPass());
236   return false;  // -print-machineinstr shouldn't print after this.
237 }
238
239 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
240                                        CodeGenOpt::Level OptLevel) {
241   PM.add(createX86FloatingPointStackifierPass());
242   return true;  // -print-machineinstr should print after this.
243 }
244
245 bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM,
246                                           CodeGenOpt::Level OptLevel,
247                                           bool Verbose,
248                                           formatted_raw_ostream &Out) {
249   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
250   if (AsmPrinterCtor)
251     PM.add(AsmPrinterCtor(Out, *this, Verbose));
252   return false;
253 }
254
255 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
256                                       CodeGenOpt::Level OptLevel,
257                                       bool DumpAsm, 
258                                       MachineCodeEmitter &MCE) {
259   // FIXME: Move this to TargetJITInfo!
260   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
261   if (DefRelocModel == Reloc::Default && 
262       (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
263     setRelocationModel(Reloc::Static);
264     Subtarget.setPICStyle(PICStyles::None);
265   }
266   
267   // 64-bit JIT places everything in the same buffer except external functions.
268   // On Darwin, use small code model but hack the call instruction for 
269   // externals.  Elsewhere, do not assume globals are in the lower 4G.
270   if (Subtarget.is64Bit()) {
271     if (Subtarget.isTargetDarwin())
272       setCodeModel(CodeModel::Small);
273     else
274       setCodeModel(CodeModel::Large);
275   }
276
277   PM.add(createX86CodeEmitterPass(*this, MCE));
278   if (DumpAsm) {
279     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
280     if (AsmPrinterCtor)
281       PM.add(AsmPrinterCtor(ferrs(), *this, true));
282   }
283
284   return false;
285 }
286
287 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
288                                       CodeGenOpt::Level OptLevel,
289                                       bool DumpAsm,
290                                       JITCodeEmitter &JCE) {
291   // FIXME: Move this to TargetJITInfo!
292   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
293   if (DefRelocModel == Reloc::Default && 
294       (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
295     setRelocationModel(Reloc::Static);
296     Subtarget.setPICStyle(PICStyles::None);
297   }
298   
299   // 64-bit JIT places everything in the same buffer except external functions.
300   // On Darwin, use small code model but hack the call instruction for 
301   // externals.  Elsewhere, do not assume globals are in the lower 4G.
302   if (Subtarget.is64Bit()) {
303     if (Subtarget.isTargetDarwin())
304       setCodeModel(CodeModel::Small);
305     else
306       setCodeModel(CodeModel::Large);
307   }
308
309   PM.add(createX86JITCodeEmitterPass(*this, JCE));
310   if (DumpAsm) {
311     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
312     if (AsmPrinterCtor)
313       PM.add(AsmPrinterCtor(ferrs(), *this, true));
314   }
315
316   return false;
317 }
318
319 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
320                                       CodeGenOpt::Level OptLevel,
321                                       bool DumpAsm,
322                                       ObjectCodeEmitter &OCE) {
323   PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
324   if (DumpAsm) {
325     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
326     if (AsmPrinterCtor)
327       PM.add(AsmPrinterCtor(ferrs(), *this, true));
328   }
329
330   return false;
331 }
332
333 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
334                                             CodeGenOpt::Level OptLevel,
335                                             bool DumpAsm,
336                                             MachineCodeEmitter &MCE) {
337   PM.add(createX86CodeEmitterPass(*this, MCE));
338   if (DumpAsm) {
339     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
340     if (AsmPrinterCtor)
341       PM.add(AsmPrinterCtor(ferrs(), *this, true));
342   }
343
344   return false;
345 }
346
347 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
348                                             CodeGenOpt::Level OptLevel,
349                                             bool DumpAsm,
350                                             JITCodeEmitter &JCE) {
351   PM.add(createX86JITCodeEmitterPass(*this, JCE));
352   if (DumpAsm) {
353     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
354     if (AsmPrinterCtor)
355       PM.add(AsmPrinterCtor(ferrs(), *this, true));
356   }
357
358   return false;
359 }
360
361 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
362                                             CodeGenOpt::Level OptLevel,
363                                             bool DumpAsm,
364                                             ObjectCodeEmitter &OCE) {
365   PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
366   if (DumpAsm) {
367     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
368     if (AsmPrinterCtor)
369       PM.add(AsmPrinterCtor(ferrs(), *this, true));
370   }
371
372   return false;
373 }