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