It is pointless to turn a UINT_TO_FP into an
[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   if (Subtarget.isFlavorIntel())
40     return new X86WinTargetAsmInfo(*this);
41   else
42     switch (Subtarget.TargetType) {
43      case X86Subtarget::isDarwin:
44       return new X86DarwinTargetAsmInfo(*this);
45      case X86Subtarget::isELF:
46       return new X86ELFTargetAsmInfo(*this);
47      case X86Subtarget::isMingw:
48      case X86Subtarget::isCygwin:
49       return new X86COFFTargetAsmInfo(*this);
50      case X86Subtarget::isWindows:
51       return new X86WinTargetAsmInfo(*this);
52      default:
53       return new X86TargetAsmInfo(*this);
54     }
55 }
56
57 unsigned X86_32TargetMachine::getJITMatchQuality() {
58 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
59   return 10;
60 #endif
61   return 0;
62 }
63
64 unsigned X86_64TargetMachine::getJITMatchQuality() {
65 #if defined(__x86_64__) || defined(_M_AMD64)
66   return 10;
67 #endif
68   return 0;
69 }
70
71 unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
72   // We strongly match "i[3-9]86-*".
73   std::string TT = M.getTargetTriple();
74   if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
75       TT[4] == '-' && TT[1] - '3' < 6)
76     return 20;
77   // If the target triple is something non-X86, we don't match.
78   if (!TT.empty()) return 0;
79
80   if (M.getEndianness()  == Module::LittleEndian &&
81       M.getPointerSize() == Module::Pointer32)
82     return 10;                                   // Weak match
83   else if (M.getEndianness() != Module::AnyEndianness ||
84            M.getPointerSize() != Module::AnyPointerSize)
85     return 0;                                    // Match for some other target
86
87   return getJITMatchQuality()/2;
88 }
89
90 unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
91   // We strongly match "x86_64-*".
92   std::string TT = M.getTargetTriple();
93   if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
94       TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
95     return 20;
96
97   // We strongly match "amd64-*".
98   if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
99       TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
100     return 20;
101   
102   // If the target triple is something non-X86-64, we don't match.
103   if (!TT.empty()) return 0;
104
105   if (M.getEndianness()  == Module::LittleEndian &&
106       M.getPointerSize() == Module::Pointer64)
107     return 10;                                   // Weak match
108   else if (M.getEndianness() != Module::AnyEndianness ||
109            M.getPointerSize() != Module::AnyPointerSize)
110     return 0;                                    // Match for some other target
111
112   return getJITMatchQuality()/2;
113 }
114
115 X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS) 
116   : X86TargetMachine(M, FS, false) {
117 }
118
119
120 X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
121   : X86TargetMachine(M, FS, true) {
122 }
123
124 /// X86TargetMachine ctor - Create an ILP32 architecture model
125 ///
126 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
127                                    bool is64Bit)
128   : Subtarget(M, FS, is64Bit),
129     DataLayout(Subtarget.getDataLayout()),
130     FrameInfo(TargetFrameInfo::StackGrowsDown,
131               Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
132     InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
133   DefRelocModel = getRelocationModel();
134   // FIXME: Correctly select PIC model for Win64 stuff
135   if (getRelocationModel() == Reloc::Default) {
136     if (Subtarget.isTargetDarwin() ||
137         (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
138       setRelocationModel(Reloc::DynamicNoPIC);
139     else
140       setRelocationModel(Reloc::Static);
141   }
142   if (Subtarget.is64Bit()) {
143     // No DynamicNoPIC support under X86-64.
144     if (getRelocationModel() == Reloc::DynamicNoPIC)
145       setRelocationModel(Reloc::PIC_);
146     // Default X86-64 code model is small.
147     if (getCodeModel() == CodeModel::Default)
148       setCodeModel(CodeModel::Small);
149   }
150
151   if (Subtarget.isTargetCygMing())
152     Subtarget.setPICStyle(PICStyle::WinPIC);
153   else if (Subtarget.isTargetDarwin()) {
154     if (Subtarget.is64Bit())
155       Subtarget.setPICStyle(PICStyle::RIPRel);
156     else
157       Subtarget.setPICStyle(PICStyle::Stub);
158   } else if (Subtarget.isTargetELF()) {
159     if (Subtarget.is64Bit())
160       Subtarget.setPICStyle(PICStyle::RIPRel);
161     else
162       Subtarget.setPICStyle(PICStyle::GOT);
163   }
164 }
165
166 //===----------------------------------------------------------------------===//
167 // Pass Pipeline Configuration
168 //===----------------------------------------------------------------------===//
169
170 bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
171   // Install an instruction selector.
172   PM.add(createX86ISelDag(*this, Fast));
173   return false;
174 }
175
176 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) {
177   // Calculate and set max stack object alignment early, so we can decide
178   // whether we will need stack realignment (and thus FP).
179   PM.add(createX86MaxStackAlignmentCalculatorPass());
180   return false;  // -print-machineinstr shouldn't print after this.
181 }
182
183 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) {
184   PM.add(createX86FloatingPointStackifierPass());
185   return true;  // -print-machineinstr should print after this.
186 }
187
188 bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast, 
189                                           std::ostream &Out) {
190   PM.add(createX86CodePrinterPass(Out, *this));
191   return false;
192 }
193
194 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
195                                       bool DumpAsm, MachineCodeEmitter &MCE) {
196   // FIXME: Move this to TargetJITInfo!
197   if (DefRelocModel == Reloc::Default)
198     setRelocationModel(Reloc::Static);
199   
200   // JIT cannot ensure globals are placed in the lower 4G of address.
201   if (Subtarget.is64Bit())
202     setCodeModel(CodeModel::Large);
203
204   PM.add(createX86CodeEmitterPass(*this, MCE));
205   if (DumpAsm)
206     PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
207
208   return false;
209 }
210
211 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
212                                         bool DumpAsm, MachineCodeEmitter &MCE) {
213   PM.add(createX86CodeEmitterPass(*this, MCE));
214   if (DumpAsm)
215     PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
216   return false;
217 }