43fbbc07e89ed8b481a7aa46ead42cbf010765d9
[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::addCodeEmitter(PassManagerBase &PM,
185                                       CodeGenOpt::Level OptLevel,
186                                       MachineCodeEmitter &MCE) {
187   // FIXME: Move this to TargetJITInfo!
188   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
189   if (DefRelocModel == Reloc::Default && 
190       (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
191     setRelocationModel(Reloc::Static);
192     Subtarget.setPICStyle(PICStyles::None);
193   }
194   
195   // 64-bit JIT places everything in the same buffer except external functions.
196   // On Darwin, use small code model but hack the call instruction for 
197   // externals.  Elsewhere, do not assume globals are in the lower 4G.
198   if (Subtarget.is64Bit()) {
199     if (Subtarget.isTargetDarwin())
200       setCodeModel(CodeModel::Small);
201     else
202       setCodeModel(CodeModel::Large);
203   }
204
205   PM.add(createX86CodeEmitterPass(*this, MCE));
206
207   return false;
208 }
209
210 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
211                                       CodeGenOpt::Level OptLevel,
212                                       JITCodeEmitter &JCE) {
213   // FIXME: Move this to TargetJITInfo!
214   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
215   if (DefRelocModel == Reloc::Default && 
216       (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
217     setRelocationModel(Reloc::Static);
218     Subtarget.setPICStyle(PICStyles::None);
219   }
220   
221   // 64-bit JIT places everything in the same buffer except external functions.
222   // On Darwin, use small code model but hack the call instruction for 
223   // externals.  Elsewhere, do not assume globals are in the lower 4G.
224   if (Subtarget.is64Bit()) {
225     if (Subtarget.isTargetDarwin())
226       setCodeModel(CodeModel::Small);
227     else
228       setCodeModel(CodeModel::Large);
229   }
230
231   PM.add(createX86JITCodeEmitterPass(*this, JCE));
232
233   return false;
234 }
235
236 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
237                                       CodeGenOpt::Level OptLevel,
238                                       ObjectCodeEmitter &OCE) {
239   PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
240   return false;
241 }
242
243 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
244                                             CodeGenOpt::Level OptLevel,
245                                             MachineCodeEmitter &MCE) {
246   PM.add(createX86CodeEmitterPass(*this, MCE));
247   return false;
248 }
249
250 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
251                                             CodeGenOpt::Level OptLevel,
252                                             JITCodeEmitter &JCE) {
253   PM.add(createX86JITCodeEmitterPass(*this, JCE));
254   return false;
255 }
256
257 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
258                                             CodeGenOpt::Level OptLevel,
259                                             ObjectCodeEmitter &OCE) {
260   PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
261   return false;
262 }