Create the specified TargetObjInfo and use it.
[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 "X86TargetAsmInfo.h"
15 #include "X86TargetObjInfo.h"
16 #include "X86TargetMachine.h"
17 #include "X86.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/Target/TargetOptions.h"
23 #include "llvm/Target/TargetMachineRegistry.h"
24 #include "llvm/Transforms/Scalar.h"
25 using namespace llvm;
26
27 /// X86TargetMachineModule - Note that this is used on hosts that cannot link
28 /// in a library unless there are references into the library.  In particular,
29 /// it seems that it is not possible to get things to work on Win32 without
30 /// this.  Though it is unused, do not remove it.
31 extern "C" int X86TargetMachineModule;
32 int X86TargetMachineModule = 0;
33
34 namespace {
35   // Register the target.
36   RegisterTarget<X86_32TargetMachine>
37   X("x86",    "  32-bit X86: Pentium-Pro and above");
38   RegisterTarget<X86_64TargetMachine>
39   Y("x86-64", "  64-bit X86: EM64T and AMD64");
40 }
41
42 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
43   return new X86TargetAsmInfo(*this);
44 }
45
46 const TargetObjInfo *X86TargetMachine::createTargetObjInfo() const {
47   return new ELFTargetObjInfo(*this);
48 }
49
50 unsigned X86_32TargetMachine::getJITMatchQuality() {
51 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
52   return 10;
53 #endif
54   return 0;
55 }
56
57 unsigned X86_64TargetMachine::getJITMatchQuality() {
58 #if defined(__x86_64__)
59   return 10;
60 #endif
61   return 0;
62 }
63
64 unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
65   // We strongly match "i[3-9]86-*".
66   std::string TT = M.getTargetTriple();
67   if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
68       TT[4] == '-' && TT[1] - '3' < 6)
69     return 20;
70
71   if (M.getEndianness()  == Module::LittleEndian &&
72       M.getPointerSize() == Module::Pointer32)
73     return 10;                                   // Weak match
74   else if (M.getEndianness() != Module::AnyEndianness ||
75            M.getPointerSize() != Module::AnyPointerSize)
76     return 0;                                    // Match for some other target
77
78   return getJITMatchQuality()/2;
79 }
80
81 unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
82   // We strongly match "x86_64-*".
83   std::string TT = M.getTargetTriple();
84   if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
85       TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
86     return 20;
87
88   // We strongly match "amd64-*".
89   if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
90       TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
91     return 20;
92   
93   if (M.getEndianness()  == Module::LittleEndian &&
94       M.getPointerSize() == Module::Pointer64)
95     return 10;                                   // Weak match
96   else if (M.getEndianness() != Module::AnyEndianness ||
97            M.getPointerSize() != Module::AnyPointerSize)
98     return 0;                                    // Match for some other target
99
100   return getJITMatchQuality()/2;
101 }
102
103 X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS) 
104   : X86TargetMachine(M, FS, false) {
105 }
106
107
108 X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
109   : X86TargetMachine(M, FS, true) {
110 }
111
112 /// X86TargetMachine ctor - Create an ILP32 architecture model
113 ///
114 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS, bool is64Bit)
115   : Subtarget(M, FS, is64Bit),
116     DataLayout(Subtarget.is64Bit() ?
117                std::string("e-p:64:64-d:32-l:32") :
118                std::string("e-p:32:32-d:32-l:32")),
119     FrameInfo(TargetFrameInfo::StackGrowsDown,
120               Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
121     InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
122   if (getRelocationModel() == Reloc::Default)
123     if (Subtarget.isTargetDarwin() || Subtarget.isTargetCygMing())
124       setRelocationModel(Reloc::DynamicNoPIC);
125     else
126       setRelocationModel(Reloc::Static);
127   if (Subtarget.is64Bit()) {
128     // No DynamicNoPIC support under X86-64.
129     if (getRelocationModel() == Reloc::DynamicNoPIC)
130       setRelocationModel(Reloc::PIC_);
131     // Default X86-64 code model is small.
132     if (getCodeModel() == CodeModel::Default)
133       setCodeModel(CodeModel::Small);
134   }
135
136   if (getRelocationModel() == Reloc::PIC_) {
137     if (Subtarget.isTargetDarwin()) {
138       if (Subtarget.is64Bit())
139         Subtarget.setPICStyle(PICStyle::RIPRel);
140       else
141         Subtarget.setPICStyle(PICStyle::Stub);
142     } else if (Subtarget.isTargetELF())
143       Subtarget.setPICStyle(PICStyle::GOT);
144     else
145       assert(0 && "Don't know how to generate PIC code for this target!");
146   } else if (getRelocationModel() == Reloc::DynamicNoPIC) {
147     if (Subtarget.isTargetDarwin())
148       Subtarget.setPICStyle(PICStyle::Stub);
149     else if (Subtarget.isTargetCygMing())
150       Subtarget.setPICStyle(PICStyle::WinPIC);
151     else
152       assert(0 && "Don't know how to generate PIC code for this target!");
153   }
154 }
155
156 //===----------------------------------------------------------------------===//
157 // Pass Pipeline Configuration
158 //===----------------------------------------------------------------------===//
159
160 bool X86TargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
161   // Install an instruction selector.
162   PM.add(createX86ISelDag(*this, Fast));
163   return false;
164 }
165
166 bool X86TargetMachine::addPostRegAlloc(FunctionPassManager &PM, bool Fast) {
167   PM.add(createX86FloatingPointStackifierPass());
168   return true;  // -print-machineinstr should print after this.
169 }
170
171 bool X86TargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast, 
172                                           std::ostream &Out) {
173   PM.add(createX86CodePrinterPass(Out, *this));
174   return false;
175 }
176
177 bool X86TargetMachine::addObjectWriter(FunctionPassManager &PM, bool Fast,
178                                        std::ostream &Out) {
179   if (Subtarget.isTargetELF()) {
180     addX86ELFObjectWriterPass(PM, Out, *this);
181     return false;
182   }
183   return true;
184 }
185
186 bool X86TargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
187                                       MachineCodeEmitter &MCE) {
188   // FIXME: Move this to TargetJITInfo!
189   setRelocationModel(Reloc::Static);
190   Subtarget.setPICStyle(PICStyle::None);
191   
192   // JIT cannot ensure globals are placed in the lower 4G of address.
193   if (Subtarget.is64Bit())
194     setCodeModel(CodeModel::Large);
195
196   PM.add(createX86CodeEmitterPass(*this, MCE));
197   return false;
198 }