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