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 TargetRegistry::RegisterCodeEmitter(TheX86_32Target, createX86MCCodeEmitter);
52 TargetRegistry::RegisterCodeEmitter(TheX86_64Target, createX86MCCodeEmitter);
56 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
57 const std::string &FS)
58 : X86TargetMachine(T, TT, FS, false) {
62 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
63 const std::string &FS)
64 : X86TargetMachine(T, TT, FS, true) {
67 /// X86TargetMachine ctor - Create an X86 target.
69 X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
70 const std::string &FS, bool is64Bit)
71 : LLVMTargetMachine(T, TT),
72 Subtarget(TT, FS, is64Bit),
73 DataLayout(Subtarget.getDataLayout()),
74 FrameInfo(TargetFrameInfo::StackGrowsDown,
75 Subtarget.getStackAlignment(),
76 (Subtarget.isTargetWin64() ? -40 :
77 (Subtarget.is64Bit() ? -8 : -4))),
78 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
79 DefRelocModel = getRelocationModel();
81 // If no relocation model was picked, default as appropriate for the target.
82 if (getRelocationModel() == Reloc::Default) {
83 if (!Subtarget.isTargetDarwin())
84 setRelocationModel(Reloc::Static);
85 else if (Subtarget.is64Bit())
86 setRelocationModel(Reloc::PIC_);
88 setRelocationModel(Reloc::DynamicNoPIC);
91 assert(getRelocationModel() != Reloc::Default &&
92 "Relocation mode not picked");
94 // If no code model is picked, default to small.
95 if (getCodeModel() == CodeModel::Default)
96 setCodeModel(CodeModel::Small);
98 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
99 // is defined as a model for code which may be used in static or dynamic
100 // executables but not necessarily a shared library. On X86-32 we just
101 // compile in -static mode, in x86-64 we use PIC.
102 if (getRelocationModel() == Reloc::DynamicNoPIC) {
104 setRelocationModel(Reloc::PIC_);
105 else if (!Subtarget.isTargetDarwin())
106 setRelocationModel(Reloc::Static);
109 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
110 // the Mach-O file format doesn't support it.
111 if (getRelocationModel() == Reloc::Static &&
112 Subtarget.isTargetDarwin() &&
114 setRelocationModel(Reloc::PIC_);
116 // Determine the PICStyle based on the target selected.
117 if (getRelocationModel() == Reloc::Static) {
118 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
119 Subtarget.setPICStyle(PICStyles::None);
120 } else if (Subtarget.isTargetCygMing()) {
121 Subtarget.setPICStyle(PICStyles::None);
122 } else if (Subtarget.isTargetDarwin()) {
123 if (Subtarget.is64Bit())
124 Subtarget.setPICStyle(PICStyles::RIPRel);
125 else if (getRelocationModel() == Reloc::PIC_)
126 Subtarget.setPICStyle(PICStyles::StubPIC);
128 assert(getRelocationModel() == Reloc::DynamicNoPIC);
129 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
131 } else if (Subtarget.isTargetELF()) {
132 if (Subtarget.is64Bit())
133 Subtarget.setPICStyle(PICStyles::RIPRel);
135 Subtarget.setPICStyle(PICStyles::GOT);
138 // Finally, if we have "none" as our PIC style, force to static mode.
139 if (Subtarget.getPICStyle() == PICStyles::None)
140 setRelocationModel(Reloc::Static);
143 //===----------------------------------------------------------------------===//
144 // Pass Pipeline Configuration
145 //===----------------------------------------------------------------------===//
147 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
148 CodeGenOpt::Level OptLevel) {
149 // Install an instruction selector.
150 PM.add(createX86ISelDag(*this, OptLevel));
152 // If we're using Fast-ISel, clean up the mess.
154 PM.add(createDeadMachineInstructionElimPass());
156 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
157 PM.add(createX87FPRegKillInserterPass());
162 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
163 CodeGenOpt::Level OptLevel) {
164 // Calculate and set max stack object alignment early, so we can decide
165 // whether we will need stack realignment (and thus FP).
166 PM.add(createX86MaxStackAlignmentCalculatorPass());
167 return false; // -print-machineinstr shouldn't print after this.
170 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
171 CodeGenOpt::Level OptLevel) {
172 PM.add(createX86FloatingPointStackifierPass());
173 return true; // -print-machineinstr should print after this.
176 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
177 CodeGenOpt::Level OptLevel,
178 MachineCodeEmitter &MCE) {
179 // FIXME: Move this to TargetJITInfo!
180 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
181 if (DefRelocModel == Reloc::Default &&
182 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
183 setRelocationModel(Reloc::Static);
184 Subtarget.setPICStyle(PICStyles::None);
187 // 64-bit JIT places everything in the same buffer except external functions.
188 // On Darwin, use small code model but hack the call instruction for
189 // externals. Elsewhere, do not assume globals are in the lower 4G.
190 if (Subtarget.is64Bit()) {
191 if (Subtarget.isTargetDarwin())
192 setCodeModel(CodeModel::Small);
194 setCodeModel(CodeModel::Large);
197 PM.add(createX86CodeEmitterPass(*this, MCE));
202 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
203 CodeGenOpt::Level OptLevel,
204 JITCodeEmitter &JCE) {
205 // FIXME: Move this to TargetJITInfo!
206 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
207 if (DefRelocModel == Reloc::Default &&
208 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
209 setRelocationModel(Reloc::Static);
210 Subtarget.setPICStyle(PICStyles::None);
213 // 64-bit JIT places everything in the same buffer except external functions.
214 // On Darwin, use small code model but hack the call instruction for
215 // externals. Elsewhere, do not assume globals are in the lower 4G.
216 if (Subtarget.is64Bit()) {
217 if (Subtarget.isTargetDarwin())
218 setCodeModel(CodeModel::Small);
220 setCodeModel(CodeModel::Large);
223 PM.add(createX86JITCodeEmitterPass(*this, JCE));
228 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
229 CodeGenOpt::Level OptLevel,
230 ObjectCodeEmitter &OCE) {
231 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
235 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
236 CodeGenOpt::Level OptLevel,
237 MachineCodeEmitter &MCE) {
238 PM.add(createX86CodeEmitterPass(*this, MCE));
242 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
243 CodeGenOpt::Level OptLevel,
244 JITCodeEmitter &JCE) {
245 PM.add(createX86JITCodeEmitterPass(*this, JCE));
249 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
250 CodeGenOpt::Level OptLevel,
251 ObjectCodeEmitter &OCE) {
252 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));