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