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