Remove TargetOptions.h dependency from X86Subtarget.
[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 "X86MCAsmInfo.h"
15 #include "X86TargetMachine.h"
16 #include "X86.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/Support/FormattedStream.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/Target/TargetRegistry.h"
25 using namespace llvm;
26
27 static MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
28   Triple TheTriple(TT);
29
30   if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO) {
31     if (TheTriple.getArch() == Triple::x86_64)
32       return new X86_64MCAsmInfoDarwin(TheTriple);
33     else
34       return new X86MCAsmInfoDarwin(TheTriple);
35   }
36
37   if (TheTriple.isOSWindows())
38     return new X86MCAsmInfoCOFF(TheTriple);
39
40   return new X86ELFMCAsmInfo(TheTriple);
41 }
42
43 static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
44                                     MCContext &Ctx, TargetAsmBackend &TAB,
45                                     raw_ostream &_OS,
46                                     MCCodeEmitter *_Emitter,
47                                     bool RelaxAll,
48                                     bool NoExecStack) {
49   Triple TheTriple(TT);
50
51   if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO)
52     return createMachOStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
53
54   if (TheTriple.isOSWindows())
55     return createWinCOFFStreamer(Ctx, TAB, *_Emitter, _OS, RelaxAll);
56
57   return createELFStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll, NoExecStack);
58 }
59
60 extern "C" void LLVMInitializeX86Target() {
61   // Register the target.
62   RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
63   RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
64
65   // Register the target asm info.
66   RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
67   RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
68
69   // Register the code emitter.
70   TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
71                                       createX86_32MCCodeEmitter);
72   TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
73                                       createX86_64MCCodeEmitter);
74
75   // Register the asm backend.
76   TargetRegistry::RegisterAsmBackend(TheX86_32Target,
77                                      createX86_32AsmBackend);
78   TargetRegistry::RegisterAsmBackend(TheX86_64Target,
79                                      createX86_64AsmBackend);
80
81   // Register the object streamer.
82   TargetRegistry::RegisterObjectStreamer(TheX86_32Target,
83                                          createMCStreamer);
84   TargetRegistry::RegisterObjectStreamer(TheX86_64Target,
85                                          createMCStreamer);
86 }
87
88
89 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
90                                          const std::string &FS)
91   : X86TargetMachine(T, TT, FS, false),
92     DataLayout(getSubtargetImpl()->isTargetDarwin() ?
93                "e-p:32:32-f64:32:64-i64:32:64-f80:128:128-f128:128:128-n8:16:32" :
94                (getSubtargetImpl()->isTargetCygMing() ||
95                 getSubtargetImpl()->isTargetWindows()) ?
96                "e-p:32:32-f64:64:64-i64:64:64-f80:32:32-f128:128:128-n8:16:32" :
97                "e-p:32:32-f64:32:64-i64:32:64-f80:32:32-f128:128:128-n8:16:32"),
98     InstrInfo(*this),
99     TSInfo(*this),
100     TLInfo(*this),
101     JITInfo(*this) {
102 }
103
104
105 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
106                                          const std::string &FS)
107   : X86TargetMachine(T, TT, FS, true),
108     DataLayout("e-p:64:64-s:64-f64:64:64-i64:64:64-f80:128:128-f128:128:128-n8:16:32:64"),
109     InstrInfo(*this),
110     TSInfo(*this),
111     TLInfo(*this),
112     JITInfo(*this) {
113 }
114
115 /// X86TargetMachine ctor - Create an X86 target.
116 ///
117 X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
118                                    const std::string &FS, bool is64Bit)
119   : LLVMTargetMachine(T, TT),
120     Subtarget(TT, FS, is64Bit, StackAlignment),
121     FrameLowering(*this, Subtarget),
122     ELFWriterInfo(is64Bit, true) {
123   DefRelocModel = getRelocationModel();
124
125   // If no relocation model was picked, default as appropriate for the target.
126   if (getRelocationModel() == Reloc::Default) {
127     // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
128     // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
129     // use static relocation model by default.
130     if (Subtarget.isTargetDarwin()) {
131       if (Subtarget.is64Bit())
132         setRelocationModel(Reloc::PIC_);
133       else
134         setRelocationModel(Reloc::DynamicNoPIC);
135     } else if (Subtarget.isTargetWin64())
136       setRelocationModel(Reloc::PIC_);
137     else
138       setRelocationModel(Reloc::Static);
139   }
140
141   assert(getRelocationModel() != Reloc::Default &&
142          "Relocation mode not picked");
143
144   // ELF and X86-64 don't have a distinct DynamicNoPIC model.  DynamicNoPIC
145   // is defined as a model for code which may be used in static or dynamic
146   // executables but not necessarily a shared library. On X86-32 we just
147   // compile in -static mode, in x86-64 we use PIC.
148   if (getRelocationModel() == Reloc::DynamicNoPIC) {
149     if (is64Bit)
150       setRelocationModel(Reloc::PIC_);
151     else if (!Subtarget.isTargetDarwin())
152       setRelocationModel(Reloc::Static);
153   }
154
155   // If we are on Darwin, disallow static relocation model in X86-64 mode, since
156   // the Mach-O file format doesn't support it.
157   if (getRelocationModel() == Reloc::Static &&
158       Subtarget.isTargetDarwin() &&
159       is64Bit)
160     setRelocationModel(Reloc::PIC_);
161
162   // Determine the PICStyle based on the target selected.
163   if (getRelocationModel() == Reloc::Static) {
164     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
165     Subtarget.setPICStyle(PICStyles::None);
166   } else if (Subtarget.is64Bit()) {
167     // PIC in 64 bit mode is always rip-rel.
168     Subtarget.setPICStyle(PICStyles::RIPRel);
169   } else if (Subtarget.isTargetCygMing()) {
170     Subtarget.setPICStyle(PICStyles::None);
171   } else if (Subtarget.isTargetDarwin()) {
172     if (getRelocationModel() == Reloc::PIC_)
173       Subtarget.setPICStyle(PICStyles::StubPIC);
174     else {
175       assert(getRelocationModel() == Reloc::DynamicNoPIC);
176       Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
177     }
178   } else if (Subtarget.isTargetELF()) {
179     Subtarget.setPICStyle(PICStyles::GOT);
180   }
181
182   // Finally, if we have "none" as our PIC style, force to static mode.
183   if (Subtarget.getPICStyle() == PICStyles::None)
184     setRelocationModel(Reloc::Static);
185
186   // default to hard float ABI
187   if (FloatABIType == FloatABI::Default)
188     FloatABIType = FloatABI::Hard;    
189 }
190
191 //===----------------------------------------------------------------------===//
192 // Pass Pipeline Configuration
193 //===----------------------------------------------------------------------===//
194
195 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
196                                        CodeGenOpt::Level OptLevel) {
197   // Install an instruction selector.
198   PM.add(createX86ISelDag(*this, OptLevel));
199
200   // For 32-bit, prepend instructions to set the "global base reg" for PIC.
201   if (!Subtarget.is64Bit())
202     PM.add(createGlobalBaseRegPass());
203
204   return false;
205 }
206
207 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
208                                       CodeGenOpt::Level OptLevel) {
209   PM.add(createX86MaxStackAlignmentHeuristicPass());
210   return false;  // -print-machineinstr shouldn't print after this.
211 }
212
213 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
214                                        CodeGenOpt::Level OptLevel) {
215   PM.add(createX86FloatingPointStackifierPass());
216   return true;  // -print-machineinstr should print after this.
217 }
218
219 bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
220                                       CodeGenOpt::Level OptLevel) {
221   if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
222     PM.add(createSSEDomainFixPass());
223     return true;
224   }
225   return false;
226 }
227
228 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
229                                       CodeGenOpt::Level OptLevel,
230                                       JITCodeEmitter &JCE) {
231   // FIXME: Move this to TargetJITInfo!
232   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
233   if (DefRelocModel == Reloc::Default &&
234       (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
235     setRelocationModel(Reloc::Static);
236     Subtarget.setPICStyle(PICStyles::None);
237   }
238
239
240   PM.add(createX86JITCodeEmitterPass(*this, JCE));
241
242   return false;
243 }
244
245 void X86TargetMachine::setCodeModelForStatic() {
246
247     if (getCodeModel() != CodeModel::Default) return;
248
249     // For static codegen, if we're not already set, use Small codegen.
250     setCodeModel(CodeModel::Small);
251 }
252
253
254 void X86TargetMachine::setCodeModelForJIT() {
255
256   if (getCodeModel() != CodeModel::Default) return;
257
258   // 64-bit JIT places everything in the same buffer except external functions.
259   if (Subtarget.is64Bit())
260     setCodeModel(CodeModel::Large);
261   else
262     setCodeModel(CodeModel::Small);
263 }