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