Fix some X86 JIT failures. This should really come from TargetJITInfo.
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the X86 specific subclass of TargetMachine.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86TargetMachine.h"
15 #include "X86.h"
16 #include "llvm/Module.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/Target/TargetOptions.h"
21 #include "llvm/Target/TargetMachineRegistry.h"
22 #include "llvm/Transforms/Scalar.h"
23 #include <iostream>
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 namespace {
34   // Register the target.
35   RegisterTarget<X86TargetMachine> X("x86", "  IA-32 (Pentium and above)");
36 }
37
38 unsigned X86TargetMachine::getJITMatchQuality() {
39 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
40   return 10;
41 #else
42   return 0;
43 #endif
44 }
45
46 unsigned X86TargetMachine::getModuleMatchQuality(const Module &M) {
47   // We strongly match "i[3-9]86-*".
48   std::string TT = M.getTargetTriple();
49   if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
50       TT[4] == '-' && TT[1] - '3' < 6)
51     return 20;
52
53   if (M.getEndianness()  == Module::LittleEndian &&
54       M.getPointerSize() == Module::Pointer32)
55     return 10;                                   // Weak match
56   else if (M.getEndianness() != Module::AnyEndianness ||
57            M.getPointerSize() != Module::AnyPointerSize)
58     return 0;                                    // Match for some other target
59
60   return getJITMatchQuality()/2;
61 }
62
63 /// X86TargetMachine ctor - Create an ILP32 architecture model
64 ///
65 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS)
66   : Subtarget(M, FS), DataLayout("e-p:32:32-d:32-l:32"),
67     FrameInfo(TargetFrameInfo::StackGrowsDown,
68               Subtarget.getStackAlignment(), -4),
69     InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
70   if (getRelocationModel() == Reloc::Default)
71     if (Subtarget.isTargetDarwin())
72       setRelocationModel(Reloc::DynamicNoPIC);
73     else
74       setRelocationModel(Reloc::PIC_);
75 }
76
77 //===----------------------------------------------------------------------===//
78 // Pass Pipeline Configuration
79 //===----------------------------------------------------------------------===//
80
81 bool X86TargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
82   // Install an instruction selector.
83   PM.add(createX86ISelDag(*this, Fast));
84   return false;
85 }
86
87 bool X86TargetMachine::addPostRegAlloc(FunctionPassManager &PM, bool Fast) {
88   PM.add(createX86FloatingPointStackifierPass());
89   return true;  // -print-machineinstr should print after this.
90 }
91
92 bool X86TargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast, 
93                                           std::ostream &Out) {
94   PM.add(createX86CodePrinterPass(Out, *this));
95   return false;
96 }
97
98 bool X86TargetMachine::addObjectWriter(FunctionPassManager &PM, bool Fast,
99                                        std::ostream &Out) {
100   if (Subtarget.isTargetELF()) {
101     addX86ELFObjectWriterPass(PM, Out, *this);
102     return false;
103   }
104   return true;
105 }
106
107 bool X86TargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
108                                       MachineCodeEmitter &MCE) {
109   // FIXME: Move this to TargetJITInfo!
110   setRelocationModel(Reloc::Static);
111   
112   PM.add(createX86CodeEmitterPass(*this, MCE));
113   return false;
114 }