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 "X86TargetMachine.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FormattedStream.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/Target/TargetOptions.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 void X86_32TargetMachine::anchor() { }
33 X86_32TargetMachine::X86_32TargetMachine(const Target &T, StringRef TT,
34 StringRef CPU, StringRef FS,
35 const TargetOptions &Options,
36 Reloc::Model RM, CodeModel::Model CM,
38 : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false),
39 DL(getSubtargetImpl()->isTargetDarwin() ?
40 "e-p:32:32-f64:32:64-i64:32:64-f80:128:128-f128:128:128-"
42 (getSubtargetImpl()->isTargetCygMing() ||
43 getSubtargetImpl()->isTargetWindows()) ?
44 "e-p:32:32-f64:64:64-i64:64:64-f80:32:32-f128:128:128-"
46 "e-p:32:32-f64:32:64-i64:32:64-f80:32:32-f128:128:128-"
54 void X86_64TargetMachine::anchor() { }
56 X86_64TargetMachine::X86_64TargetMachine(const Target &T, StringRef TT,
57 StringRef CPU, StringRef FS,
58 const TargetOptions &Options,
59 Reloc::Model RM, CodeModel::Model CM,
61 : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true),
62 DL("e-p:64:64-s:64-f64:64:64-i64:64:64-f80:128:128-f128:128:128-"
70 /// X86TargetMachine ctor - Create an X86 target.
72 X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
73 StringRef CPU, StringRef FS,
74 const TargetOptions &Options,
75 Reloc::Model RM, CodeModel::Model CM,
78 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
79 Subtarget(TT, CPU, FS, Options.StackAlignmentOverride, is64Bit),
80 FrameLowering(*this, Subtarget),
81 InstrItins(Subtarget.getInstrItineraryData()){
82 // Determine the PICStyle based on the target selected.
83 if (getRelocationModel() == Reloc::Static) {
84 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
85 Subtarget.setPICStyle(PICStyles::None);
86 } else if (Subtarget.is64Bit()) {
87 // PIC in 64 bit mode is always rip-rel.
88 Subtarget.setPICStyle(PICStyles::RIPRel);
89 } else if (Subtarget.isTargetCygMing()) {
90 Subtarget.setPICStyle(PICStyles::None);
91 } else if (Subtarget.isTargetDarwin()) {
92 if (getRelocationModel() == Reloc::PIC_)
93 Subtarget.setPICStyle(PICStyles::StubPIC);
95 assert(getRelocationModel() == Reloc::DynamicNoPIC);
96 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
98 } else if (Subtarget.isTargetELF()) {
99 Subtarget.setPICStyle(PICStyles::GOT);
102 // default to hard float ABI
103 if (Options.FloatABIType == FloatABI::Default)
104 this->Options.FloatABIType = FloatABI::Hard;
107 //===----------------------------------------------------------------------===//
108 // Command line options for x86
109 //===----------------------------------------------------------------------===//
111 UseVZeroUpper("x86-use-vzeroupper",
112 cl::desc("Minimize AVX to SSE transition penalty"),
115 // Temporary option to control early if-conversion for x86 while adding machine
118 X86EarlyIfConv("x86-early-ifcvt",
119 cl::desc("Enable early if-conversion on X86"));
121 //===----------------------------------------------------------------------===//
122 // X86 Analysis Pass Setup
123 //===----------------------------------------------------------------------===//
125 void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
126 // Add first the target-independent BasicTTI pass, then our X86 pass. This
127 // allows the X86 pass to delegate to the target independent layer when
129 PM.add(createBasicTargetTransformInfoPass(getTargetLowering()));
130 PM.add(createX86TargetTransformInfoPass(this));
134 //===----------------------------------------------------------------------===//
135 // Pass Pipeline Configuration
136 //===----------------------------------------------------------------------===//
139 /// X86 Code Generator Pass Configuration Options.
140 class X86PassConfig : public TargetPassConfig {
142 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
143 : TargetPassConfig(TM, PM) {}
145 X86TargetMachine &getX86TargetMachine() const {
146 return getTM<X86TargetMachine>();
149 const X86Subtarget &getX86Subtarget() const {
150 return *getX86TargetMachine().getSubtargetImpl();
153 virtual bool addInstSelector();
154 virtual bool addILPOpts();
155 virtual bool addPreRegAlloc();
156 virtual bool addPostRegAlloc();
157 virtual bool addPreEmitPass();
161 TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
162 return new X86PassConfig(this, PM);
165 bool X86PassConfig::addInstSelector() {
166 // Install an instruction selector.
167 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
169 // For ELF, cleanup any local-dynamic TLS accesses.
170 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
171 addPass(createCleanupLocalDynamicTLSPass());
173 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
174 if (!getX86Subtarget().is64Bit())
175 addPass(createGlobalBaseRegPass());
180 bool X86PassConfig::addILPOpts() {
181 if (X86EarlyIfConv && getX86Subtarget().hasCMov()) {
182 addPass(&EarlyIfConverterID);
188 bool X86PassConfig::addPreRegAlloc() {
189 return false; // -print-machineinstr shouldn't print after this.
192 bool X86PassConfig::addPostRegAlloc() {
193 addPass(createX86FloatingPointStackifierPass());
194 return true; // -print-machineinstr should print after this.
197 bool X86PassConfig::addPreEmitPass() {
198 bool ShouldPrint = false;
199 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
200 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
204 if (getX86Subtarget().hasAVX() && UseVZeroUpper) {
205 addPass(createX86IssueVZeroUpperPass());
209 if (getOptLevel() != CodeGenOpt::None &&
210 getX86Subtarget().padShortFunctions()) {
211 addPass(createX86PadShortFunctions());
218 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
219 JITCodeEmitter &JCE) {
220 PM.add(createX86JITCodeEmitterPass(*this, JCE));