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