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