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