1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the X86 specific subclass of TargetMachine.
12 //===----------------------------------------------------------------------===//
14 #include "X86MCAsmInfo.h"
15 #include "X86TargetMachine.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"
25 static const MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
27 switch (TheTriple.getOS()) {
29 return new X86MCAsmInfoDarwin(TheTriple);
33 return new X86MCAsmInfoCOFF(TheTriple);
35 return new X86WinMCAsmInfo(TheTriple);
37 return new X86ELFMCAsmInfo(TheTriple);
41 extern "C" void LLVMInitializeX86Target() {
42 // Register the target.
43 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
44 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
46 // Register the target asm info.
47 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
48 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
50 // Register the code emitter.
51 // FIXME: Remove the heinous one when the new one works.
52 TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
53 createHeinousX86MCCodeEmitter);
54 TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
55 createHeinousX86MCCodeEmitter);
59 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
60 const std::string &FS)
61 : X86TargetMachine(T, TT, FS, false) {
65 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
66 const std::string &FS)
67 : X86TargetMachine(T, TT, FS, true) {
70 /// X86TargetMachine ctor - Create an X86 target.
72 X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
73 const std::string &FS, bool is64Bit)
74 : LLVMTargetMachine(T, TT),
75 Subtarget(TT, FS, is64Bit),
76 DataLayout(Subtarget.getDataLayout()),
77 FrameInfo(TargetFrameInfo::StackGrowsDown,
78 Subtarget.getStackAlignment(),
79 (Subtarget.isTargetWin64() ? -40 :
80 (Subtarget.is64Bit() ? -8 : -4))),
81 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
82 DefRelocModel = getRelocationModel();
84 // If no relocation model was picked, default as appropriate for the target.
85 if (getRelocationModel() == Reloc::Default) {
86 if (!Subtarget.isTargetDarwin())
87 setRelocationModel(Reloc::Static);
88 else if (Subtarget.is64Bit())
89 setRelocationModel(Reloc::PIC_);
91 setRelocationModel(Reloc::DynamicNoPIC);
94 assert(getRelocationModel() != Reloc::Default &&
95 "Relocation mode not picked");
97 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
98 // is defined as a model for code which may be used in static or dynamic
99 // executables but not necessarily a shared library. On X86-32 we just
100 // compile in -static mode, in x86-64 we use PIC.
101 if (getRelocationModel() == Reloc::DynamicNoPIC) {
103 setRelocationModel(Reloc::PIC_);
104 else if (!Subtarget.isTargetDarwin())
105 setRelocationModel(Reloc::Static);
108 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
109 // the Mach-O file format doesn't support it.
110 if (getRelocationModel() == Reloc::Static &&
111 Subtarget.isTargetDarwin() &&
113 setRelocationModel(Reloc::PIC_);
115 // Determine the PICStyle based on the target selected.
116 if (getRelocationModel() == Reloc::Static) {
117 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
118 Subtarget.setPICStyle(PICStyles::None);
119 } else if (Subtarget.isTargetCygMing()) {
120 Subtarget.setPICStyle(PICStyles::None);
121 } else if (Subtarget.isTargetDarwin()) {
122 if (Subtarget.is64Bit())
123 Subtarget.setPICStyle(PICStyles::RIPRel);
124 else if (getRelocationModel() == Reloc::PIC_)
125 Subtarget.setPICStyle(PICStyles::StubPIC);
127 assert(getRelocationModel() == Reloc::DynamicNoPIC);
128 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
130 } else if (Subtarget.isTargetELF()) {
131 if (Subtarget.is64Bit())
132 Subtarget.setPICStyle(PICStyles::RIPRel);
134 Subtarget.setPICStyle(PICStyles::GOT);
137 // Finally, if we have "none" as our PIC style, force to static mode.
138 if (Subtarget.getPICStyle() == PICStyles::None)
139 setRelocationModel(Reloc::Static);
142 //===----------------------------------------------------------------------===//
143 // Pass Pipeline Configuration
144 //===----------------------------------------------------------------------===//
146 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
147 CodeGenOpt::Level OptLevel) {
148 // Install an instruction selector.
149 PM.add(createX86ISelDag(*this, OptLevel));
151 // If we're using Fast-ISel, clean up the mess.
153 PM.add(createDeadMachineInstructionElimPass());
155 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
156 PM.add(createX87FPRegKillInserterPass());
161 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
162 CodeGenOpt::Level OptLevel) {
163 return false; // -print-machineinstr shouldn't print after this.
166 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
167 CodeGenOpt::Level OptLevel) {
168 PM.add(createX86FloatingPointStackifierPass());
169 return true; // -print-machineinstr should print after this.
172 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
173 CodeGenOpt::Level OptLevel,
174 JITCodeEmitter &JCE) {
175 // FIXME: Move this to TargetJITInfo!
176 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
177 if (DefRelocModel == Reloc::Default &&
178 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
179 setRelocationModel(Reloc::Static);
180 Subtarget.setPICStyle(PICStyles::None);
184 PM.add(createX86JITCodeEmitterPass(*this, JCE));
189 void X86TargetMachine::setCodeModelForStatic() {
191 if (getCodeModel() != CodeModel::Default) return;
193 // For static codegen, if we're not already set, use Small codegen.
194 setCodeModel(CodeModel::Small);
198 void X86TargetMachine::setCodeModelForJIT() {
200 if (getCodeModel() != CodeModel::Default) return;
202 // 64-bit JIT places everything in the same buffer except external functions.
203 if (Subtarget.is64Bit())
204 setCodeModel(CodeModel::Large);
206 setCodeModel(CodeModel::Small);
209 /// getLSDAEncoding - Returns the LSDA pointer encoding. The choices are 4-byte,
210 /// 8-byte, and target default. The CIE is hard-coded to indicate that the LSDA
211 /// pointer in the FDE section is an "sdata4", and should be encoded as a 4-byte
212 /// pointer by default. However, some systems may require a different size due
213 /// to bugs or other conditions. We will default to a 4-byte encoding unless the
214 /// system tells us otherwise.
216 /// The issue is when the CIE says their is an LSDA. That mandates that every
217 /// FDE have an LSDA slot. But if the function does not need an LSDA. There
218 /// needs to be some way to signify there is none. The LSDA is encoded as
219 /// pc-rel. But you don't look for some magic value after adding the pc. You
220 /// have to look for a zero before adding the pc. The problem is that the size
221 /// of the zero to look for depends on the encoding. The unwinder bug in SL is
222 /// that it always checks for a pointer-size zero. So on x86_64 it looks for 8
223 /// bytes of zero. If you have an LSDA, it works fine since the 8-bytes are
224 /// non-zero so it goes ahead and then reads the value based on the encoding.
225 /// But if you use sdata4 and there is no LSDA, then the test for zero gives a
226 /// false negative and the unwinder thinks there is an LSDA.
228 /// FIXME: This call-back isn't good! We should be using the correct encoding
229 /// regardless of the system. However, there are some systems which have bugs
230 /// that prevent this from occuring.
231 DwarfLSDAEncoding::Encoding X86TargetMachine::getLSDAEncoding() const {
232 if (Subtarget.isTargetDarwin() && Subtarget.getDarwinVers() != 10)
233 return DwarfLSDAEncoding::Default;
235 return DwarfLSDAEncoding::EightByte;