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 "X86TargetAsmInfo.h"
15 #include "X86TargetMachine.h"
17 #include "llvm/Module.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Support/FormattedStream.h"
22 #include "llvm/Target/TargetOptions.h"
23 #include "llvm/Target/TargetRegistry.h"
26 extern "C" void LLVMInitializeX86Target() {
27 // Register the target.
28 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
29 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
32 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
33 if (Subtarget.isFlavorIntel())
34 return new X86WinTargetAsmInfo(*this);
36 switch (Subtarget.TargetType) {
37 case X86Subtarget::isDarwin:
38 return new X86DarwinTargetAsmInfo(*this);
39 case X86Subtarget::isELF:
40 return new X86ELFTargetAsmInfo(*this);
41 case X86Subtarget::isMingw:
42 case X86Subtarget::isCygwin:
43 return new X86COFFTargetAsmInfo(*this);
44 case X86Subtarget::isWindows:
45 return new X86WinTargetAsmInfo(*this);
47 return new X86GenericTargetAsmInfo(*this);
51 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
52 const std::string &FS)
53 : X86TargetMachine(T, M, FS, false) {
57 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
58 const std::string &FS)
59 : X86TargetMachine(T, M, FS, true) {
62 /// X86TargetMachine ctor - Create an X86 target.
64 X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
65 const std::string &FS, bool is64Bit)
66 : LLVMTargetMachine(T),
67 Subtarget(M, FS, is64Bit),
68 DataLayout(Subtarget.getDataLayout()),
69 FrameInfo(TargetFrameInfo::StackGrowsDown,
70 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
71 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
72 DefRelocModel = getRelocationModel();
74 // If no relocation model was picked, default as appropriate for the target.
75 if (getRelocationModel() == Reloc::Default) {
76 if (!Subtarget.isTargetDarwin())
77 setRelocationModel(Reloc::Static);
78 else if (Subtarget.is64Bit())
79 setRelocationModel(Reloc::PIC_);
81 setRelocationModel(Reloc::DynamicNoPIC);
84 assert(getRelocationModel() != Reloc::Default &&
85 "Relocation mode not picked");
87 // If no code model is picked, default to small.
88 if (getCodeModel() == CodeModel::Default)
89 setCodeModel(CodeModel::Small);
91 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
92 // is defined as a model for code which may be used in static or dynamic
93 // executables but not necessarily a shared library. On X86-32 we just
94 // compile in -static mode, in x86-64 we use PIC.
95 if (getRelocationModel() == Reloc::DynamicNoPIC) {
97 setRelocationModel(Reloc::PIC_);
98 else if (!Subtarget.isTargetDarwin())
99 setRelocationModel(Reloc::Static);
102 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
103 // the Mach-O file format doesn't support it.
104 if (getRelocationModel() == Reloc::Static &&
105 Subtarget.isTargetDarwin() &&
107 setRelocationModel(Reloc::PIC_);
109 // Determine the PICStyle based on the target selected.
110 if (getRelocationModel() == Reloc::Static) {
111 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
112 Subtarget.setPICStyle(PICStyles::None);
113 } else if (Subtarget.isTargetCygMing()) {
114 Subtarget.setPICStyle(PICStyles::None);
115 } else if (Subtarget.isTargetDarwin()) {
116 if (Subtarget.is64Bit())
117 Subtarget.setPICStyle(PICStyles::RIPRel);
118 else if (getRelocationModel() == Reloc::PIC_)
119 Subtarget.setPICStyle(PICStyles::StubPIC);
121 assert(getRelocationModel() == Reloc::DynamicNoPIC);
122 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
124 } else if (Subtarget.isTargetELF()) {
125 if (Subtarget.is64Bit())
126 Subtarget.setPICStyle(PICStyles::RIPRel);
128 Subtarget.setPICStyle(PICStyles::GOT);
131 // Finally, if we have "none" as our PIC style, force to static mode.
132 if (Subtarget.getPICStyle() == PICStyles::None)
133 setRelocationModel(Reloc::Static);
136 //===----------------------------------------------------------------------===//
137 // Pass Pipeline Configuration
138 //===----------------------------------------------------------------------===//
140 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
141 CodeGenOpt::Level OptLevel) {
142 // Install an instruction selector.
143 PM.add(createX86ISelDag(*this, OptLevel));
145 // If we're using Fast-ISel, clean up the mess.
147 PM.add(createDeadMachineInstructionElimPass());
149 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
150 PM.add(createX87FPRegKillInserterPass());
155 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
156 CodeGenOpt::Level OptLevel) {
157 // Calculate and set max stack object alignment early, so we can decide
158 // whether we will need stack realignment (and thus FP).
159 PM.add(createX86MaxStackAlignmentCalculatorPass());
160 return false; // -print-machineinstr shouldn't print after this.
163 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
164 CodeGenOpt::Level OptLevel) {
165 PM.add(createX86FloatingPointStackifierPass());
166 return true; // -print-machineinstr should print after this.
169 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
170 CodeGenOpt::Level OptLevel,
171 MachineCodeEmitter &MCE) {
172 // FIXME: Move this to TargetJITInfo!
173 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
174 if (DefRelocModel == Reloc::Default &&
175 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
176 setRelocationModel(Reloc::Static);
177 Subtarget.setPICStyle(PICStyles::None);
180 // 64-bit JIT places everything in the same buffer except external functions.
181 // On Darwin, use small code model but hack the call instruction for
182 // externals. Elsewhere, do not assume globals are in the lower 4G.
183 if (Subtarget.is64Bit()) {
184 if (Subtarget.isTargetDarwin())
185 setCodeModel(CodeModel::Small);
187 setCodeModel(CodeModel::Large);
190 PM.add(createX86CodeEmitterPass(*this, MCE));
195 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
196 CodeGenOpt::Level OptLevel,
197 JITCodeEmitter &JCE) {
198 // FIXME: Move this to TargetJITInfo!
199 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
200 if (DefRelocModel == Reloc::Default &&
201 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
202 setRelocationModel(Reloc::Static);
203 Subtarget.setPICStyle(PICStyles::None);
206 // 64-bit JIT places everything in the same buffer except external functions.
207 // On Darwin, use small code model but hack the call instruction for
208 // externals. Elsewhere, do not assume globals are in the lower 4G.
209 if (Subtarget.is64Bit()) {
210 if (Subtarget.isTargetDarwin())
211 setCodeModel(CodeModel::Small);
213 setCodeModel(CodeModel::Large);
216 PM.add(createX86JITCodeEmitterPass(*this, JCE));
221 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
222 CodeGenOpt::Level OptLevel,
223 ObjectCodeEmitter &OCE) {
224 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
228 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
229 CodeGenOpt::Level OptLevel,
230 MachineCodeEmitter &MCE) {
231 PM.add(createX86CodeEmitterPass(*this, MCE));
235 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
236 CodeGenOpt::Level OptLevel,
237 JITCodeEmitter &JCE) {
238 PM.add(createX86JITCodeEmitterPass(*this, JCE));
242 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
243 CodeGenOpt::Level OptLevel,
244 ObjectCodeEmitter &OCE) {
245 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));