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/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 extern "C" void LLVMInitializeX86Target() {
26 // Register the target.
27 RegisterTargetMachine
<X86_32TargetMachine
> X(TheX86_32Target
);
28 RegisterTargetMachine
<X86_64TargetMachine
> Y(TheX86_64Target
);
31 const TargetAsmInfo
*X86TargetMachine::createTargetAsmInfo() const {
32 switch (Subtarget
.TargetType
) {
33 default: llvm_unreachable("unknown subtarget type");
34 case X86Subtarget::isDarwin
:
35 return new X86DarwinTargetAsmInfo(*this);
36 case X86Subtarget::isELF
:
37 return new X86ELFTargetAsmInfo(*this);
38 case X86Subtarget::isMingw
:
39 case X86Subtarget::isCygwin
:
40 return new X86COFFTargetAsmInfo(*this);
41 case X86Subtarget::isWindows
:
42 return new X86WinTargetAsmInfo(*this);
46 X86_32TargetMachine::X86_32TargetMachine(const Target
&T
, const std::string
&TT
,
47 const std::string
&FS
)
48 : X86TargetMachine(T
, TT
, FS
, false) {
52 X86_64TargetMachine::X86_64TargetMachine(const Target
&T
, const std::string
&TT
,
53 const std::string
&FS
)
54 : X86TargetMachine(T
, TT
, FS
, true) {
57 /// X86TargetMachine ctor - Create an X86 target.
59 X86TargetMachine::X86TargetMachine(const Target
&T
, const std::string
&TT
,
60 const std::string
&FS
, bool is64Bit
)
61 : LLVMTargetMachine(T
),
62 Subtarget(TT
, FS
, is64Bit
),
63 DataLayout(Subtarget
.getDataLayout()),
64 FrameInfo(TargetFrameInfo::StackGrowsDown
,
65 Subtarget
.getStackAlignment(),
66 (Subtarget
.isTargetWin64() ? -40 :
67 (Subtarget
.is64Bit() ? -8 : -4))),
68 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
69 DefRelocModel
= getRelocationModel();
71 // If no relocation model was picked, default as appropriate for the target.
72 if (getRelocationModel() == Reloc::Default
) {
73 if (!Subtarget
.isTargetDarwin())
74 setRelocationModel(Reloc::Static
);
75 else if (Subtarget
.is64Bit())
76 setRelocationModel(Reloc::PIC_
);
78 setRelocationModel(Reloc::DynamicNoPIC
);
81 assert(getRelocationModel() != Reloc::Default
&&
82 "Relocation mode not picked");
84 // If no code model is picked, default to small.
85 if (getCodeModel() == CodeModel::Default
)
86 setCodeModel(CodeModel::Small
);
88 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
89 // is defined as a model for code which may be used in static or dynamic
90 // executables but not necessarily a shared library. On X86-32 we just
91 // compile in -static mode, in x86-64 we use PIC.
92 if (getRelocationModel() == Reloc::DynamicNoPIC
) {
94 setRelocationModel(Reloc::PIC_
);
95 else if (!Subtarget
.isTargetDarwin())
96 setRelocationModel(Reloc::Static
);
99 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
100 // the Mach-O file format doesn't support it.
101 if (getRelocationModel() == Reloc::Static
&&
102 Subtarget
.isTargetDarwin() &&
104 setRelocationModel(Reloc::PIC_
);
106 // Determine the PICStyle based on the target selected.
107 if (getRelocationModel() == Reloc::Static
) {
108 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
109 Subtarget
.setPICStyle(PICStyles::None
);
110 } else if (Subtarget
.isTargetCygMing()) {
111 Subtarget
.setPICStyle(PICStyles::None
);
112 } else if (Subtarget
.isTargetDarwin()) {
113 if (Subtarget
.is64Bit())
114 Subtarget
.setPICStyle(PICStyles::RIPRel
);
115 else if (getRelocationModel() == Reloc::PIC_
)
116 Subtarget
.setPICStyle(PICStyles::StubPIC
);
118 assert(getRelocationModel() == Reloc::DynamicNoPIC
);
119 Subtarget
.setPICStyle(PICStyles::StubDynamicNoPIC
);
121 } else if (Subtarget
.isTargetELF()) {
122 if (Subtarget
.is64Bit())
123 Subtarget
.setPICStyle(PICStyles::RIPRel
);
125 Subtarget
.setPICStyle(PICStyles::GOT
);
128 // Finally, if we have "none" as our PIC style, force to static mode.
129 if (Subtarget
.getPICStyle() == PICStyles::None
)
130 setRelocationModel(Reloc::Static
);
133 //===----------------------------------------------------------------------===//
134 // Pass Pipeline Configuration
135 //===----------------------------------------------------------------------===//
137 bool X86TargetMachine::addInstSelector(PassManagerBase
&PM
,
138 CodeGenOpt::Level OptLevel
) {
139 // Install an instruction selector.
140 PM
.add(createX86ISelDag(*this, OptLevel
));
142 // If we're using Fast-ISel, clean up the mess.
144 PM
.add(createDeadMachineInstructionElimPass());
146 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
147 PM
.add(createX87FPRegKillInserterPass());
152 bool X86TargetMachine::addPreRegAlloc(PassManagerBase
&PM
,
153 CodeGenOpt::Level OptLevel
) {
154 // Calculate and set max stack object alignment early, so we can decide
155 // whether we will need stack realignment (and thus FP).
156 PM
.add(createX86MaxStackAlignmentCalculatorPass());
157 return false; // -print-machineinstr shouldn't print after this.
160 bool X86TargetMachine::addPostRegAlloc(PassManagerBase
&PM
,
161 CodeGenOpt::Level OptLevel
) {
162 PM
.add(createX86FloatingPointStackifierPass());
163 return true; // -print-machineinstr should print after this.
166 bool X86TargetMachine::addCodeEmitter(PassManagerBase
&PM
,
167 CodeGenOpt::Level OptLevel
,
168 MachineCodeEmitter
&MCE
) {
169 // FIXME: Move this to TargetJITInfo!
170 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
171 if (DefRelocModel
== Reloc::Default
&&
172 (!Subtarget
.isTargetDarwin() || !Subtarget
.is64Bit())) {
173 setRelocationModel(Reloc::Static
);
174 Subtarget
.setPICStyle(PICStyles::None
);
177 // 64-bit JIT places everything in the same buffer except external functions.
178 // On Darwin, use small code model but hack the call instruction for
179 // externals. Elsewhere, do not assume globals are in the lower 4G.
180 if (Subtarget
.is64Bit()) {
181 if (Subtarget
.isTargetDarwin())
182 setCodeModel(CodeModel::Small
);
184 setCodeModel(CodeModel::Large
);
187 PM
.add(createX86CodeEmitterPass(*this, MCE
));
192 bool X86TargetMachine::addCodeEmitter(PassManagerBase
&PM
,
193 CodeGenOpt::Level OptLevel
,
194 JITCodeEmitter
&JCE
) {
195 // FIXME: Move this to TargetJITInfo!
196 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
197 if (DefRelocModel
== Reloc::Default
&&
198 (!Subtarget
.isTargetDarwin() || !Subtarget
.is64Bit())) {
199 setRelocationModel(Reloc::Static
);
200 Subtarget
.setPICStyle(PICStyles::None
);
203 // 64-bit JIT places everything in the same buffer except external functions.
204 // On Darwin, use small code model but hack the call instruction for
205 // externals. Elsewhere, do not assume globals are in the lower 4G.
206 if (Subtarget
.is64Bit()) {
207 if (Subtarget
.isTargetDarwin())
208 setCodeModel(CodeModel::Small
);
210 setCodeModel(CodeModel::Large
);
213 PM
.add(createX86JITCodeEmitterPass(*this, JCE
));
218 bool X86TargetMachine::addCodeEmitter(PassManagerBase
&PM
,
219 CodeGenOpt::Level OptLevel
,
220 ObjectCodeEmitter
&OCE
) {
221 PM
.add(createX86ObjectCodeEmitterPass(*this, OCE
));
225 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase
&PM
,
226 CodeGenOpt::Level OptLevel
,
227 MachineCodeEmitter
&MCE
) {
228 PM
.add(createX86CodeEmitterPass(*this, MCE
));
232 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase
&PM
,
233 CodeGenOpt::Level OptLevel
,
234 JITCodeEmitter
&JCE
) {
235 PM
.add(createX86JITCodeEmitterPass(*this, JCE
));
239 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase
&PM
,
240 CodeGenOpt::Level OptLevel
,
241 ObjectCodeEmitter
&OCE
) {
242 PM
.add(createX86ObjectCodeEmitterPass(*this, OCE
));