Fix PR6696 and PR6663
[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/Support/CommandLine.h"
21 #include "llvm/Support/FormattedStream.h"
22 #include "llvm/Target/TargetOptions.h"
23 #include "llvm/Target/TargetRegistry.h"
24 using namespace llvm;
25
26 static MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
27   Triple TheTriple(TT);
28   switch (TheTriple.getOS()) {
29   case Triple::Darwin:
30     return new X86MCAsmInfoDarwin(TheTriple);
31   case Triple::MinGW32:
32   case Triple::MinGW64:
33   case Triple::Cygwin:
34   case Triple::Win32:
35     return new X86MCAsmInfoCOFF(TheTriple);
36   default:
37     return new X86ELFMCAsmInfo(TheTriple);
38   }
39 }
40
41 extern "C" void LLVMInitializeX86Target() { 
42   // Register the target.
43   RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
44   RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
45
46   // Register the target asm info.
47   RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
48   RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
49
50   // Register the code emitter.
51   TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
52                                       createX86_32MCCodeEmitter);
53   TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
54                                       createX86_64MCCodeEmitter);
55
56   // Register the asm backend.
57   TargetRegistry::RegisterAsmBackend(TheX86_32Target,
58                                      createX86_32AsmBackend);
59   TargetRegistry::RegisterAsmBackend(TheX86_64Target,
60                                      createX86_64AsmBackend);
61 }
62
63
64 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
65                                          const std::string &FS)
66   : X86TargetMachine(T, TT, FS, false) {
67 }
68
69
70 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
71                                          const std::string &FS)
72   : X86TargetMachine(T, TT, FS, true) {
73 }
74
75 /// X86TargetMachine ctor - Create an X86 target.
76 ///
77 X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT, 
78                                    const std::string &FS, bool is64Bit)
79   : LLVMTargetMachine(T, TT), 
80     Subtarget(TT, FS, is64Bit),
81     DataLayout(Subtarget.getDataLayout()),
82     FrameInfo(TargetFrameInfo::StackGrowsDown,
83               Subtarget.getStackAlignment(),
84               (Subtarget.isTargetWin64() ? -40 :
85                (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   // ELF and X86-64 don't have a distinct DynamicNoPIC model.  DynamicNoPIC
103   // is defined as a model for code which may be used in static or dynamic
104   // executables but not necessarily a shared library. On X86-32 we just
105   // compile in -static mode, in x86-64 we use PIC.
106   if (getRelocationModel() == Reloc::DynamicNoPIC) {
107     if (is64Bit)
108       setRelocationModel(Reloc::PIC_);
109     else if (!Subtarget.isTargetDarwin())
110       setRelocationModel(Reloc::Static);
111   }
112
113   // If we are on Darwin, disallow static relocation model in X86-64 mode, since
114   // the Mach-O file format doesn't support it.
115   if (getRelocationModel() == Reloc::Static &&
116       Subtarget.isTargetDarwin() &&
117       is64Bit)
118     setRelocationModel(Reloc::PIC_);
119       
120   // Determine the PICStyle based on the target selected.
121   if (getRelocationModel() == Reloc::Static) {
122     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
123     Subtarget.setPICStyle(PICStyles::None);
124   } else if (Subtarget.isTargetCygMing()) {
125     Subtarget.setPICStyle(PICStyles::None);
126   } else if (Subtarget.isTargetDarwin()) {
127     if (Subtarget.is64Bit())
128       Subtarget.setPICStyle(PICStyles::RIPRel);
129     else if (getRelocationModel() == Reloc::PIC_)
130       Subtarget.setPICStyle(PICStyles::StubPIC);
131     else {
132       assert(getRelocationModel() == Reloc::DynamicNoPIC);
133       Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
134     }
135   } else if (Subtarget.isTargetELF()) {
136     if (Subtarget.is64Bit())
137       Subtarget.setPICStyle(PICStyles::RIPRel);
138     else
139       Subtarget.setPICStyle(PICStyles::GOT);
140   }
141       
142   // Finally, if we have "none" as our PIC style, force to static mode.
143   if (Subtarget.getPICStyle() == PICStyles::None)
144     setRelocationModel(Reloc::Static);
145 }
146
147 //===----------------------------------------------------------------------===//
148 // Pass Pipeline Configuration
149 //===----------------------------------------------------------------------===//
150
151 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
152                                        CodeGenOpt::Level OptLevel) {
153   // Install an instruction selector.
154   PM.add(createX86ISelDag(*this, OptLevel));
155
156   // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
157   PM.add(createX87FPRegKillInserterPass());
158
159   return false;
160 }
161
162 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
163                                       CodeGenOpt::Level OptLevel) {
164   PM.add(createX86MaxStackAlignmentHeuristicPass());
165   return false;  // -print-machineinstr shouldn't print after this.
166 }
167
168 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
169                                        CodeGenOpt::Level OptLevel) {
170   PM.add(createX86FloatingPointStackifierPass());
171   return true;  // -print-machineinstr should print after this.
172 }
173
174 bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
175                                       CodeGenOpt::Level OptLevel) {
176   if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
177     PM.add(createSSEDomainFixPass());
178     return true;
179   }
180   return false;
181 }
182
183 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
184                                       CodeGenOpt::Level OptLevel,
185                                       JITCodeEmitter &JCE) {
186   // FIXME: Move this to TargetJITInfo!
187   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
188   if (DefRelocModel == Reloc::Default && 
189       (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
190     setRelocationModel(Reloc::Static);
191     Subtarget.setPICStyle(PICStyles::None);
192   }
193   
194
195   PM.add(createX86JITCodeEmitterPass(*this, JCE));
196
197   return false;
198 }
199
200 void X86TargetMachine::setCodeModelForStatic() {
201
202     if (getCodeModel() != CodeModel::Default) return;
203
204     // For static codegen, if we're not already set, use Small codegen.
205     setCodeModel(CodeModel::Small);
206 }
207
208
209 void X86TargetMachine::setCodeModelForJIT() {
210
211   if (getCodeModel() != CodeModel::Default) return;
212
213   // 64-bit JIT places everything in the same buffer except external functions.
214   if (Subtarget.is64Bit())
215     setCodeModel(CodeModel::Large);
216   else
217     setCodeModel(CodeModel::Small);
218 }