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/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/Support/FormattedStream.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/Target/TargetRegistry.h"
27 static MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
30 if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO) {
31 if (TheTriple.getArch() == Triple::x86_64)
32 return new X86_64MCAsmInfoDarwin(TheTriple);
34 return new X86MCAsmInfoDarwin(TheTriple);
37 if (TheTriple.isOSWindows())
38 return new X86MCAsmInfoCOFF(TheTriple);
40 return new X86ELFMCAsmInfo(TheTriple);
43 static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
44 MCContext &Ctx, TargetAsmBackend &TAB,
46 MCCodeEmitter *_Emitter,
51 if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO)
52 return createMachOStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
54 if (TheTriple.isOSWindows())
55 return createWinCOFFStreamer(Ctx, TAB, *_Emitter, _OS, RelaxAll);
57 return createELFStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll, NoExecStack);
60 extern "C" void LLVMInitializeX86Target() {
61 // Register the target.
62 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
63 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
65 // Register the target asm info.
66 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
67 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
69 // Register the code emitter.
70 TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
71 createX86_32MCCodeEmitter);
72 TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
73 createX86_64MCCodeEmitter);
75 // Register the asm backend.
76 TargetRegistry::RegisterAsmBackend(TheX86_32Target,
77 createX86_32AsmBackend);
78 TargetRegistry::RegisterAsmBackend(TheX86_64Target,
79 createX86_64AsmBackend);
81 // Register the object streamer.
82 TargetRegistry::RegisterObjectStreamer(TheX86_32Target,
84 TargetRegistry::RegisterObjectStreamer(TheX86_64Target,
89 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
90 const std::string &FS)
91 : X86TargetMachine(T, TT, FS, false),
92 DataLayout(getSubtargetImpl()->isTargetDarwin() ?
93 "e-p:32:32-f64:32:64-i64:32:64-f80:128:128-f128:128:128-n8:16:32" :
94 (getSubtargetImpl()->isTargetCygMing() ||
95 getSubtargetImpl()->isTargetWindows()) ?
96 "e-p:32:32-f64:64:64-i64:64:64-f80:32:32-f128:128:128-n8:16:32" :
97 "e-p:32:32-f64:32:64-i64:32:64-f80:32:32-f128:128:128-n8:16:32"),
105 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
106 const std::string &FS)
107 : X86TargetMachine(T, TT, FS, true),
108 DataLayout("e-p:64:64-s:64-f64:64:64-i64:64:64-f80:128:128-f128:128:128-n8:16:32:64"),
115 /// X86TargetMachine ctor - Create an X86 target.
117 X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
118 const std::string &FS, bool is64Bit)
119 : LLVMTargetMachine(T, TT),
120 Subtarget(TT, FS, is64Bit),
121 FrameLowering(*this, Subtarget),
122 ELFWriterInfo(is64Bit, true) {
123 DefRelocModel = getRelocationModel();
125 // If no relocation model was picked, default as appropriate for the target.
126 if (getRelocationModel() == Reloc::Default) {
127 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
128 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
129 // use static relocation model by default.
130 if (Subtarget.isTargetDarwin()) {
131 if (Subtarget.is64Bit())
132 setRelocationModel(Reloc::PIC_);
134 setRelocationModel(Reloc::DynamicNoPIC);
135 } else if (Subtarget.isTargetWin64())
136 setRelocationModel(Reloc::PIC_);
138 setRelocationModel(Reloc::Static);
141 assert(getRelocationModel() != Reloc::Default &&
142 "Relocation mode not picked");
144 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
145 // is defined as a model for code which may be used in static or dynamic
146 // executables but not necessarily a shared library. On X86-32 we just
147 // compile in -static mode, in x86-64 we use PIC.
148 if (getRelocationModel() == Reloc::DynamicNoPIC) {
150 setRelocationModel(Reloc::PIC_);
151 else if (!Subtarget.isTargetDarwin())
152 setRelocationModel(Reloc::Static);
155 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
156 // the Mach-O file format doesn't support it.
157 if (getRelocationModel() == Reloc::Static &&
158 Subtarget.isTargetDarwin() &&
160 setRelocationModel(Reloc::PIC_);
162 // Determine the PICStyle based on the target selected.
163 if (getRelocationModel() == Reloc::Static) {
164 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
165 Subtarget.setPICStyle(PICStyles::None);
166 } else if (Subtarget.is64Bit()) {
167 // PIC in 64 bit mode is always rip-rel.
168 Subtarget.setPICStyle(PICStyles::RIPRel);
169 } else if (Subtarget.isTargetCygMing()) {
170 Subtarget.setPICStyle(PICStyles::None);
171 } else if (Subtarget.isTargetDarwin()) {
172 if (getRelocationModel() == Reloc::PIC_)
173 Subtarget.setPICStyle(PICStyles::StubPIC);
175 assert(getRelocationModel() == Reloc::DynamicNoPIC);
176 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
178 } else if (Subtarget.isTargetELF()) {
179 Subtarget.setPICStyle(PICStyles::GOT);
182 // Finally, if we have "none" as our PIC style, force to static mode.
183 if (Subtarget.getPICStyle() == PICStyles::None)
184 setRelocationModel(Reloc::Static);
187 //===----------------------------------------------------------------------===//
188 // Pass Pipeline Configuration
189 //===----------------------------------------------------------------------===//
191 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
192 CodeGenOpt::Level OptLevel) {
193 // Install an instruction selector.
194 PM.add(createX86ISelDag(*this, OptLevel));
196 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
197 if (!Subtarget.is64Bit())
198 PM.add(createGlobalBaseRegPass());
203 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
204 CodeGenOpt::Level OptLevel) {
205 PM.add(createX86MaxStackAlignmentHeuristicPass());
206 return false; // -print-machineinstr shouldn't print after this.
209 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
210 CodeGenOpt::Level OptLevel) {
211 PM.add(createX86FloatingPointStackifierPass());
212 return true; // -print-machineinstr should print after this.
215 bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
216 CodeGenOpt::Level OptLevel) {
217 if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
218 PM.add(createSSEDomainFixPass());
224 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
225 CodeGenOpt::Level OptLevel,
226 JITCodeEmitter &JCE) {
227 // FIXME: Move this to TargetJITInfo!
228 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
229 if (DefRelocModel == Reloc::Default &&
230 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
231 setRelocationModel(Reloc::Static);
232 Subtarget.setPICStyle(PICStyles::None);
236 PM.add(createX86JITCodeEmitterPass(*this, JCE));
241 void X86TargetMachine::setCodeModelForStatic() {
243 if (getCodeModel() != CodeModel::Default) return;
245 // For static codegen, if we're not already set, use Small codegen.
246 setCodeModel(CodeModel::Small);
250 void X86TargetMachine::setCodeModelForJIT() {
252 if (getCodeModel() != CodeModel::Default) return;
254 // 64-bit JIT places everything in the same buffer except external functions.
255 if (Subtarget.is64Bit())
256 setCodeModel(CodeModel::Large);
258 setCodeModel(CodeModel::Small);