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/TargetMachineRegistry.h"
26 // Register the target.
27 static RegisterTarget<X86_32TargetMachine>
28 X(TheX86_32Target, "x86", "32-bit X86: Pentium-Pro and above");
30 static RegisterTarget<X86_64TargetMachine>
31 Y(TheX86_64Target, "x86-64", "64-bit X86: EM64T and AMD64");
33 // Force static initialization.
34 extern "C" void LLVMInitializeX86Target() {
38 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
39 if (Subtarget.isFlavorIntel())
40 return new X86WinTargetAsmInfo(*this);
42 switch (Subtarget.TargetType) {
43 case X86Subtarget::isDarwin:
44 return new X86DarwinTargetAsmInfo(*this);
45 case X86Subtarget::isELF:
46 return new X86ELFTargetAsmInfo(*this);
47 case X86Subtarget::isMingw:
48 case X86Subtarget::isCygwin:
49 return new X86COFFTargetAsmInfo(*this);
50 case X86Subtarget::isWindows:
51 return new X86WinTargetAsmInfo(*this);
53 return new X86GenericTargetAsmInfo(*this);
57 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
58 const std::string &FS)
59 : X86TargetMachine(T, M, FS, false) {
63 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
64 const std::string &FS)
65 : X86TargetMachine(T, M, FS, true) {
68 /// X86TargetMachine ctor - Create an X86 target.
70 X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
71 const std::string &FS, bool is64Bit)
72 : LLVMTargetMachine(T),
73 Subtarget(M, FS, is64Bit),
74 DataLayout(Subtarget.getDataLayout()),
75 FrameInfo(TargetFrameInfo::StackGrowsDown,
76 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
77 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
78 DefRelocModel = getRelocationModel();
80 // If no relocation model was picked, default as appropriate for the target.
81 if (getRelocationModel() == Reloc::Default) {
82 if (!Subtarget.isTargetDarwin())
83 setRelocationModel(Reloc::Static);
84 else if (Subtarget.is64Bit())
85 setRelocationModel(Reloc::PIC_);
87 setRelocationModel(Reloc::DynamicNoPIC);
90 assert(getRelocationModel() != Reloc::Default &&
91 "Relocation mode not picked");
93 // If no code model is picked, default to small.
94 if (getCodeModel() == CodeModel::Default)
95 setCodeModel(CodeModel::Small);
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 // Calculate and set max stack object alignment early, so we can decide
164 // whether we will need stack realignment (and thus FP).
165 PM.add(createX86MaxStackAlignmentCalculatorPass());
166 return false; // -print-machineinstr shouldn't print after this.
169 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
170 CodeGenOpt::Level OptLevel) {
171 PM.add(createX86FloatingPointStackifierPass());
172 return true; // -print-machineinstr should print after this.
175 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
176 CodeGenOpt::Level OptLevel,
177 MachineCodeEmitter &MCE) {
178 // FIXME: Move this to TargetJITInfo!
179 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
180 if (DefRelocModel == Reloc::Default &&
181 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
182 setRelocationModel(Reloc::Static);
183 Subtarget.setPICStyle(PICStyles::None);
186 // 64-bit JIT places everything in the same buffer except external functions.
187 // On Darwin, use small code model but hack the call instruction for
188 // externals. Elsewhere, do not assume globals are in the lower 4G.
189 if (Subtarget.is64Bit()) {
190 if (Subtarget.isTargetDarwin())
191 setCodeModel(CodeModel::Small);
193 setCodeModel(CodeModel::Large);
196 PM.add(createX86CodeEmitterPass(*this, MCE));
201 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
202 CodeGenOpt::Level OptLevel,
203 JITCodeEmitter &JCE) {
204 // FIXME: Move this to TargetJITInfo!
205 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
206 if (DefRelocModel == Reloc::Default &&
207 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
208 setRelocationModel(Reloc::Static);
209 Subtarget.setPICStyle(PICStyles::None);
212 // 64-bit JIT places everything in the same buffer except external functions.
213 // On Darwin, use small code model but hack the call instruction for
214 // externals. Elsewhere, do not assume globals are in the lower 4G.
215 if (Subtarget.is64Bit()) {
216 if (Subtarget.isTargetDarwin())
217 setCodeModel(CodeModel::Small);
219 setCodeModel(CodeModel::Large);
222 PM.add(createX86JITCodeEmitterPass(*this, JCE));
227 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
228 CodeGenOpt::Level OptLevel,
229 ObjectCodeEmitter &OCE) {
230 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
234 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
235 CodeGenOpt::Level OptLevel,
236 MachineCodeEmitter &MCE) {
237 PM.add(createX86CodeEmitterPass(*this, MCE));
241 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
242 CodeGenOpt::Level OptLevel,
243 JITCodeEmitter &JCE) {
244 PM.add(createX86JITCodeEmitterPass(*this, JCE));
248 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
249 CodeGenOpt::Level OptLevel,
250 ObjectCodeEmitter &OCE) {
251 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));