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