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 return new X86MCAsmInfoDarwin(TheTriple);
33 if (TheTriple.isOSWindows())
34 return new X86MCAsmInfoCOFF(TheTriple);
36 return new X86ELFMCAsmInfo(TheTriple);
39 static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
40 MCContext &Ctx, TargetAsmBackend &TAB,
42 MCCodeEmitter *_Emitter,
47 if (TheTriple.isOSDarwin() || TheTriple.getEnvironment() == Triple::MachO)
48 return createMachOStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
50 if (TheTriple.isOSWindows())
51 return createWinCOFFStreamer(Ctx, TAB, *_Emitter, _OS, RelaxAll);
53 return createELFStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll, NoExecStack);
56 extern "C" void LLVMInitializeX86Target() {
57 // Register the target.
58 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
59 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
61 // Register the target asm info.
62 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
63 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
65 // Register the code emitter.
66 TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
67 createX86_32MCCodeEmitter);
68 TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
69 createX86_64MCCodeEmitter);
71 // Register the asm backend.
72 TargetRegistry::RegisterAsmBackend(TheX86_32Target,
73 createX86_32AsmBackend);
74 TargetRegistry::RegisterAsmBackend(TheX86_64Target,
75 createX86_64AsmBackend);
77 // Register the object streamer.
78 TargetRegistry::RegisterObjectStreamer(TheX86_32Target,
80 TargetRegistry::RegisterObjectStreamer(TheX86_64Target,
85 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
86 const std::string &FS)
87 : X86TargetMachine(T, TT, FS, false),
88 DataLayout(getSubtargetImpl()->isTargetDarwin() ?
89 "e-p:32:32-f64:32:64-i64:32:64-f80:128:128-f128:128:128-n8:16:32" :
90 (getSubtargetImpl()->isTargetCygMing() ||
91 getSubtargetImpl()->isTargetWindows()) ?
92 "e-p:32:32-f64:64:64-i64:64:64-f80:32:32-f128:128:128-n8:16:32" :
93 "e-p:32:32-f64:32:64-i64:32:64-f80:32:32-f128:128:128-n8:16:32"),
101 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
102 const std::string &FS)
103 : X86TargetMachine(T, TT, FS, true),
104 DataLayout("e-p:64:64-s:64-f64:64:64-i64:64:64-f80:128:128-f128:128:128-n8:16:32:64"),
111 /// X86TargetMachine ctor - Create an X86 target.
113 X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
114 const std::string &FS, bool is64Bit)
115 : LLVMTargetMachine(T, TT),
116 Subtarget(TT, FS, is64Bit),
117 FrameLowering(*this, Subtarget),
118 ELFWriterInfo(is64Bit, true) {
119 DefRelocModel = getRelocationModel();
121 // If no relocation model was picked, default as appropriate for the target.
122 if (getRelocationModel() == Reloc::Default) {
123 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
124 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
125 // use static relocation model by default.
126 if (Subtarget.isTargetDarwin()) {
127 if (Subtarget.is64Bit())
128 setRelocationModel(Reloc::PIC_);
130 setRelocationModel(Reloc::DynamicNoPIC);
131 } else if (Subtarget.isTargetWin64())
132 setRelocationModel(Reloc::PIC_);
134 setRelocationModel(Reloc::Static);
137 assert(getRelocationModel() != Reloc::Default &&
138 "Relocation mode not picked");
140 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
141 // is defined as a model for code which may be used in static or dynamic
142 // executables but not necessarily a shared library. On X86-32 we just
143 // compile in -static mode, in x86-64 we use PIC.
144 if (getRelocationModel() == Reloc::DynamicNoPIC) {
146 setRelocationModel(Reloc::PIC_);
147 else if (!Subtarget.isTargetDarwin())
148 setRelocationModel(Reloc::Static);
151 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
152 // the Mach-O file format doesn't support it.
153 if (getRelocationModel() == Reloc::Static &&
154 Subtarget.isTargetDarwin() &&
156 setRelocationModel(Reloc::PIC_);
158 // Determine the PICStyle based on the target selected.
159 if (getRelocationModel() == Reloc::Static) {
160 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
161 Subtarget.setPICStyle(PICStyles::None);
162 } else if (Subtarget.is64Bit()) {
163 // PIC in 64 bit mode is always rip-rel.
164 Subtarget.setPICStyle(PICStyles::RIPRel);
165 } else if (Subtarget.isTargetCygMing()) {
166 Subtarget.setPICStyle(PICStyles::None);
167 } else if (Subtarget.isTargetDarwin()) {
168 if (getRelocationModel() == Reloc::PIC_)
169 Subtarget.setPICStyle(PICStyles::StubPIC);
171 assert(getRelocationModel() == Reloc::DynamicNoPIC);
172 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
174 } else if (Subtarget.isTargetELF()) {
175 Subtarget.setPICStyle(PICStyles::GOT);
178 // Finally, if we have "none" as our PIC style, force to static mode.
179 if (Subtarget.getPICStyle() == PICStyles::None)
180 setRelocationModel(Reloc::Static);
183 //===----------------------------------------------------------------------===//
184 // Pass Pipeline Configuration
185 //===----------------------------------------------------------------------===//
187 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
188 CodeGenOpt::Level OptLevel) {
189 // Install an instruction selector.
190 PM.add(createX86ISelDag(*this, OptLevel));
192 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
193 if (!Subtarget.is64Bit())
194 PM.add(createGlobalBaseRegPass());
199 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
200 CodeGenOpt::Level OptLevel) {
201 PM.add(createX86MaxStackAlignmentHeuristicPass());
202 return false; // -print-machineinstr shouldn't print after this.
205 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
206 CodeGenOpt::Level OptLevel) {
207 PM.add(createX86FloatingPointStackifierPass());
208 return true; // -print-machineinstr should print after this.
211 bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
212 CodeGenOpt::Level OptLevel) {
213 if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
214 PM.add(createSSEDomainFixPass());
220 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
221 CodeGenOpt::Level OptLevel,
222 JITCodeEmitter &JCE) {
223 // FIXME: Move this to TargetJITInfo!
224 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
225 if (DefRelocModel == Reloc::Default &&
226 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
227 setRelocationModel(Reloc::Static);
228 Subtarget.setPICStyle(PICStyles::None);
232 PM.add(createX86JITCodeEmitterPass(*this, JCE));
237 void X86TargetMachine::setCodeModelForStatic() {
239 if (getCodeModel() != CodeModel::Default) return;
241 // For static codegen, if we're not already set, use Small codegen.
242 setCodeModel(CodeModel::Small);
246 void X86TargetMachine::setCodeModelForJIT() {
248 if (getCodeModel() != CodeModel::Default) return;
250 // 64-bit JIT places everything in the same buffer except external functions.
251 if (Subtarget.is64Bit())
252 setCodeModel(CodeModel::Large);
254 setCodeModel(CodeModel::Small);