f4c8b13490030a10e19af1041d16fbf276bc900d
[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/Support/FormattedStream.h"
22 #include "llvm/Target/TargetOptions.h"
23 #include "llvm/Target/TargetMachineRegistry.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 // Register the target.
34 extern Target TheX86_32Target;
35 static RegisterTarget<X86_32TargetMachine>
36 X(TheX86_32Target, "x86",    "32-bit X86: Pentium-Pro and above");
37
38 extern Target TheX86_64Target;
39 static RegisterTarget<X86_64TargetMachine>
40 Y(TheX86_64Target, "x86-64", "64-bit X86: EM64T and AMD64");
41
42 // Force static initialization.
43 extern "C" void LLVMInitializeX86Target() { 
44   
45 }
46
47 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
48   if (Subtarget.isFlavorIntel())
49     return new X86WinTargetAsmInfo(*this);
50   else
51     switch (Subtarget.TargetType) {
52      case X86Subtarget::isDarwin:
53       return new X86DarwinTargetAsmInfo(*this);
54      case X86Subtarget::isELF:
55       return new X86ELFTargetAsmInfo(*this);
56      case X86Subtarget::isMingw:
57      case X86Subtarget::isCygwin:
58       return new X86COFFTargetAsmInfo(*this);
59      case X86Subtarget::isWindows:
60       return new X86WinTargetAsmInfo(*this);
61      default:
62       return new X86GenericTargetAsmInfo(*this);
63     }
64 }
65
66 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M, 
67                                          const std::string &FS)
68   : X86TargetMachine(T, M, FS, false) {
69 }
70
71
72 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M, 
73                                          const std::string &FS)
74   : X86TargetMachine(T, M, FS, true) {
75 }
76
77 /// X86TargetMachine ctor - Create an X86 target.
78 ///
79 X86TargetMachine::X86TargetMachine(const Target &T, const Module &M, 
80                                    const std::string &FS, bool is64Bit)
81   : LLVMTargetMachine(T), 
82     Subtarget(M, FS, is64Bit),
83     DataLayout(Subtarget.getDataLayout()),
84     FrameInfo(TargetFrameInfo::StackGrowsDown,
85               Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
86     InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
87   DefRelocModel = getRelocationModel();
88       
89   // If no relocation model was picked, default as appropriate for the target.
90   if (getRelocationModel() == Reloc::Default) {
91     if (!Subtarget.isTargetDarwin())
92       setRelocationModel(Reloc::Static);
93     else if (Subtarget.is64Bit())
94       setRelocationModel(Reloc::PIC_);
95     else
96       setRelocationModel(Reloc::DynamicNoPIC);
97   }
98
99   assert(getRelocationModel() != Reloc::Default &&
100          "Relocation mode not picked");
101
102   // If no code model is picked, default to small.
103   if (getCodeModel() == CodeModel::Default)
104     setCodeModel(CodeModel::Small);
105       
106   // ELF and X86-64 don't have a distinct DynamicNoPIC model.  DynamicNoPIC
107   // is defined as a model for code which may be used in static or dynamic
108   // executables but not necessarily a shared library. On X86-32 we just
109   // compile in -static mode, in x86-64 we use PIC.
110   if (getRelocationModel() == Reloc::DynamicNoPIC) {
111     if (is64Bit)
112       setRelocationModel(Reloc::PIC_);
113     else if (!Subtarget.isTargetDarwin())
114       setRelocationModel(Reloc::Static);
115   }
116
117   // If we are on Darwin, disallow static relocation model in X86-64 mode, since
118   // the Mach-O file format doesn't support it.
119   if (getRelocationModel() == Reloc::Static &&
120       Subtarget.isTargetDarwin() &&
121       is64Bit)
122     setRelocationModel(Reloc::PIC_);
123       
124   // Determine the PICStyle based on the target selected.
125   if (getRelocationModel() == Reloc::Static) {
126     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
127     Subtarget.setPICStyle(PICStyles::None);
128   } else if (Subtarget.isTargetCygMing()) {
129     Subtarget.setPICStyle(PICStyles::None);
130   } else if (Subtarget.isTargetDarwin()) {
131     if (Subtarget.is64Bit())
132       Subtarget.setPICStyle(PICStyles::RIPRel);
133     else if (getRelocationModel() == Reloc::PIC_)
134       Subtarget.setPICStyle(PICStyles::StubPIC);
135     else {
136       assert(getRelocationModel() == Reloc::DynamicNoPIC);
137       Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
138     }
139   } else if (Subtarget.isTargetELF()) {
140     if (Subtarget.is64Bit())
141       Subtarget.setPICStyle(PICStyles::RIPRel);
142     else
143       Subtarget.setPICStyle(PICStyles::GOT);
144   }
145       
146   // Finally, if we have "none" as our PIC style, force to static mode.
147   if (Subtarget.getPICStyle() == PICStyles::None)
148     setRelocationModel(Reloc::Static);
149 }
150
151 //===----------------------------------------------------------------------===//
152 // Pass Pipeline Configuration
153 //===----------------------------------------------------------------------===//
154
155 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
156                                        CodeGenOpt::Level OptLevel) {
157   // Install an instruction selector.
158   PM.add(createX86ISelDag(*this, OptLevel));
159
160   // If we're using Fast-ISel, clean up the mess.
161   if (EnableFastISel)
162     PM.add(createDeadMachineInstructionElimPass());
163
164   // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
165   PM.add(createX87FPRegKillInserterPass());
166
167   return false;
168 }
169
170 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
171                                       CodeGenOpt::Level OptLevel) {
172   // Calculate and set max stack object alignment early, so we can decide
173   // whether we will need stack realignment (and thus FP).
174   PM.add(createX86MaxStackAlignmentCalculatorPass());
175   return false;  // -print-machineinstr shouldn't print after this.
176 }
177
178 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
179                                        CodeGenOpt::Level OptLevel) {
180   PM.add(createX86FloatingPointStackifierPass());
181   return true;  // -print-machineinstr should print after this.
182 }
183
184 bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM,
185                                           CodeGenOpt::Level OptLevel,
186                                           bool Verbose,
187                                           formatted_raw_ostream &Out) {
188   FunctionPass *Printer = getTarget().createAsmPrinter(Out, *this, Verbose);
189   if (!Printer)
190     llvm_report_error("unable to create assembly printer");
191   PM.add(Printer);
192   return false;
193 }
194
195 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
196                                       CodeGenOpt::Level OptLevel,
197                                       bool DumpAsm, 
198                                       MachineCodeEmitter &MCE) {
199   // FIXME: Move this to TargetJITInfo!
200   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
201   if (DefRelocModel == Reloc::Default && 
202       (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
203     setRelocationModel(Reloc::Static);
204     Subtarget.setPICStyle(PICStyles::None);
205   }
206   
207   // 64-bit JIT places everything in the same buffer except external functions.
208   // On Darwin, use small code model but hack the call instruction for 
209   // externals.  Elsewhere, do not assume globals are in the lower 4G.
210   if (Subtarget.is64Bit()) {
211     if (Subtarget.isTargetDarwin())
212       setCodeModel(CodeModel::Small);
213     else
214       setCodeModel(CodeModel::Large);
215   }
216
217   PM.add(createX86CodeEmitterPass(*this, MCE));
218   if (DumpAsm)
219     addAssemblyEmitter(PM, OptLevel, true, ferrs());
220
221   return false;
222 }
223
224 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
225                                       CodeGenOpt::Level OptLevel,
226                                       bool DumpAsm,
227                                       JITCodeEmitter &JCE) {
228   // FIXME: Move this to TargetJITInfo!
229   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
230   if (DefRelocModel == Reloc::Default && 
231       (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
232     setRelocationModel(Reloc::Static);
233     Subtarget.setPICStyle(PICStyles::None);
234   }
235   
236   // 64-bit JIT places everything in the same buffer except external functions.
237   // On Darwin, use small code model but hack the call instruction for 
238   // externals.  Elsewhere, do not assume globals are in the lower 4G.
239   if (Subtarget.is64Bit()) {
240     if (Subtarget.isTargetDarwin())
241       setCodeModel(CodeModel::Small);
242     else
243       setCodeModel(CodeModel::Large);
244   }
245
246   PM.add(createX86JITCodeEmitterPass(*this, JCE));
247   if (DumpAsm)
248     addAssemblyEmitter(PM, OptLevel, true, ferrs());
249
250   return false;
251 }
252
253 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
254                                       CodeGenOpt::Level OptLevel,
255                                       bool DumpAsm,
256                                       ObjectCodeEmitter &OCE) {
257   PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
258   if (DumpAsm)
259     addAssemblyEmitter(PM, OptLevel, true, ferrs());
260
261   return false;
262 }
263
264 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
265                                             CodeGenOpt::Level OptLevel,
266                                             bool DumpAsm,
267                                             MachineCodeEmitter &MCE) {
268   PM.add(createX86CodeEmitterPass(*this, MCE));
269   if (DumpAsm)
270     addAssemblyEmitter(PM, OptLevel, true, ferrs());
271
272   return false;
273 }
274
275 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
276                                             CodeGenOpt::Level OptLevel,
277                                             bool DumpAsm,
278                                             JITCodeEmitter &JCE) {
279   PM.add(createX86JITCodeEmitterPass(*this, JCE));
280   if (DumpAsm)
281     addAssemblyEmitter(PM, OptLevel, true, ferrs());
282
283   return false;
284 }
285
286 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
287                                             CodeGenOpt::Level OptLevel,
288                                             bool DumpAsm,
289                                             ObjectCodeEmitter &OCE) {
290   PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
291   if (DumpAsm)
292     addAssemblyEmitter(PM, OptLevel, true, ferrs());
293
294   return false;
295 }