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