pass the TargetTriple down from each target ctor to the
[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 "X86TargetAsmInfo.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/FormattedStream.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include "llvm/Target/TargetRegistry.h"
23 using namespace llvm;
24
25 extern "C" void LLVMInitializeX86Target() { 
26   // Register the target.
27   RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
28   RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
29 }
30
31 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
32   switch (Subtarget.TargetType) {
33   default: llvm_unreachable("unknown subtarget type");
34   case X86Subtarget::isDarwin:
35     return new X86DarwinTargetAsmInfo(*this);
36   case X86Subtarget::isELF:
37     return new X86ELFTargetAsmInfo(*this);
38   case X86Subtarget::isMingw:
39   case X86Subtarget::isCygwin:
40     return new X86COFFTargetAsmInfo(*this);
41   case X86Subtarget::isWindows:
42     return new X86WinTargetAsmInfo(*this);
43   }
44 }
45
46 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
47                                          const std::string &FS)
48   : X86TargetMachine(T, TT, FS, false) {
49 }
50
51
52 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
53                                          const std::string &FS)
54   : X86TargetMachine(T, TT, FS, true) {
55 }
56
57 /// X86TargetMachine ctor - Create an X86 target.
58 ///
59 X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT, 
60                                    const std::string &FS, bool is64Bit)
61   : LLVMTargetMachine(T, TT), 
62     Subtarget(TT, FS, is64Bit),
63     DataLayout(Subtarget.getDataLayout()),
64     FrameInfo(TargetFrameInfo::StackGrowsDown,
65               Subtarget.getStackAlignment(),
66               (Subtarget.isTargetWin64() ? -40 :
67                (Subtarget.is64Bit() ? -8 : -4))),
68     InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
69   DefRelocModel = getRelocationModel();
70       
71   // If no relocation model was picked, default as appropriate for the target.
72   if (getRelocationModel() == Reloc::Default) {
73     if (!Subtarget.isTargetDarwin())
74       setRelocationModel(Reloc::Static);
75     else if (Subtarget.is64Bit())
76       setRelocationModel(Reloc::PIC_);
77     else
78       setRelocationModel(Reloc::DynamicNoPIC);
79   }
80
81   assert(getRelocationModel() != Reloc::Default &&
82          "Relocation mode not picked");
83
84   // If no code model is picked, default to small.
85   if (getCodeModel() == CodeModel::Default)
86     setCodeModel(CodeModel::Small);
87       
88   // ELF and X86-64 don't have a distinct DynamicNoPIC model.  DynamicNoPIC
89   // is defined as a model for code which may be used in static or dynamic
90   // executables but not necessarily a shared library. On X86-32 we just
91   // compile in -static mode, in x86-64 we use PIC.
92   if (getRelocationModel() == Reloc::DynamicNoPIC) {
93     if (is64Bit)
94       setRelocationModel(Reloc::PIC_);
95     else if (!Subtarget.isTargetDarwin())
96       setRelocationModel(Reloc::Static);
97   }
98
99   // If we are on Darwin, disallow static relocation model in X86-64 mode, since
100   // the Mach-O file format doesn't support it.
101   if (getRelocationModel() == Reloc::Static &&
102       Subtarget.isTargetDarwin() &&
103       is64Bit)
104     setRelocationModel(Reloc::PIC_);
105       
106   // Determine the PICStyle based on the target selected.
107   if (getRelocationModel() == Reloc::Static) {
108     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
109     Subtarget.setPICStyle(PICStyles::None);
110   } else if (Subtarget.isTargetCygMing()) {
111     Subtarget.setPICStyle(PICStyles::None);
112   } else if (Subtarget.isTargetDarwin()) {
113     if (Subtarget.is64Bit())
114       Subtarget.setPICStyle(PICStyles::RIPRel);
115     else if (getRelocationModel() == Reloc::PIC_)
116       Subtarget.setPICStyle(PICStyles::StubPIC);
117     else {
118       assert(getRelocationModel() == Reloc::DynamicNoPIC);
119       Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
120     }
121   } else if (Subtarget.isTargetELF()) {
122     if (Subtarget.is64Bit())
123       Subtarget.setPICStyle(PICStyles::RIPRel);
124     else
125       Subtarget.setPICStyle(PICStyles::GOT);
126   }
127       
128   // Finally, if we have "none" as our PIC style, force to static mode.
129   if (Subtarget.getPICStyle() == PICStyles::None)
130     setRelocationModel(Reloc::Static);
131 }
132
133 //===----------------------------------------------------------------------===//
134 // Pass Pipeline Configuration
135 //===----------------------------------------------------------------------===//
136
137 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
138                                        CodeGenOpt::Level OptLevel) {
139   // Install an instruction selector.
140   PM.add(createX86ISelDag(*this, OptLevel));
141
142   // If we're using Fast-ISel, clean up the mess.
143   if (EnableFastISel)
144     PM.add(createDeadMachineInstructionElimPass());
145
146   // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
147   PM.add(createX87FPRegKillInserterPass());
148
149   return false;
150 }
151
152 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
153                                       CodeGenOpt::Level OptLevel) {
154   // Calculate and set max stack object alignment early, so we can decide
155   // whether we will need stack realignment (and thus FP).
156   PM.add(createX86MaxStackAlignmentCalculatorPass());
157   return false;  // -print-machineinstr shouldn't print after this.
158 }
159
160 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
161                                        CodeGenOpt::Level OptLevel) {
162   PM.add(createX86FloatingPointStackifierPass());
163   return true;  // -print-machineinstr should print after this.
164 }
165
166 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
167                                       CodeGenOpt::Level OptLevel,
168                                       MachineCodeEmitter &MCE) {
169   // FIXME: Move this to TargetJITInfo!
170   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
171   if (DefRelocModel == Reloc::Default && 
172       (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
173     setRelocationModel(Reloc::Static);
174     Subtarget.setPICStyle(PICStyles::None);
175   }
176   
177   // 64-bit JIT places everything in the same buffer except external functions.
178   // On Darwin, use small code model but hack the call instruction for 
179   // externals.  Elsewhere, do not assume globals are in the lower 4G.
180   if (Subtarget.is64Bit()) {
181     if (Subtarget.isTargetDarwin())
182       setCodeModel(CodeModel::Small);
183     else
184       setCodeModel(CodeModel::Large);
185   }
186
187   PM.add(createX86CodeEmitterPass(*this, MCE));
188
189   return false;
190 }
191
192 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
193                                       CodeGenOpt::Level OptLevel,
194                                       JITCodeEmitter &JCE) {
195   // FIXME: Move this to TargetJITInfo!
196   // On Darwin, do not override 64-bit setting made in X86TargetMachine().
197   if (DefRelocModel == Reloc::Default && 
198       (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
199     setRelocationModel(Reloc::Static);
200     Subtarget.setPICStyle(PICStyles::None);
201   }
202   
203   // 64-bit JIT places everything in the same buffer except external functions.
204   // On Darwin, use small code model but hack the call instruction for 
205   // externals.  Elsewhere, do not assume globals are in the lower 4G.
206   if (Subtarget.is64Bit()) {
207     if (Subtarget.isTargetDarwin())
208       setCodeModel(CodeModel::Small);
209     else
210       setCodeModel(CodeModel::Large);
211   }
212
213   PM.add(createX86JITCodeEmitterPass(*this, JCE));
214
215   return false;
216 }
217
218 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
219                                       CodeGenOpt::Level OptLevel,
220                                       ObjectCodeEmitter &OCE) {
221   PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
222   return false;
223 }
224
225 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
226                                             CodeGenOpt::Level OptLevel,
227                                             MachineCodeEmitter &MCE) {
228   PM.add(createX86CodeEmitterPass(*this, MCE));
229   return false;
230 }
231
232 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
233                                             CodeGenOpt::Level OptLevel,
234                                             JITCodeEmitter &JCE) {
235   PM.add(createX86JITCodeEmitterPass(*this, JCE));
236   return false;
237 }
238
239 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
240                                             CodeGenOpt::Level OptLevel,
241                                             ObjectCodeEmitter &OCE) {
242   PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
243   return false;
244 }