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/Passes.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/FormattedStream.h"
20 #include "llvm/Support/TargetRegistry.h"
21 #include "llvm/Target/TargetOptions.h"
24 extern "C" void LLVMInitializeX86Target() {
25 // Register the target.
26 RegisterTargetMachine<X86TargetMachine> X(TheX86_32Target);
27 RegisterTargetMachine<X86TargetMachine> Y(TheX86_64Target);
30 void X86TargetMachine::anchor() { }
32 static std::string computeDataLayout(const X86Subtarget &ST) {
33 // X86 is little endian
34 std::string Ret = "e";
36 Ret += DataLayout::getManglingComponent(ST.getTargetTriple());
37 // X86 and x32 have 32 bit pointers.
38 if (ST.isTarget64BitILP32() || !ST.is64Bit())
41 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
42 if (ST.is64Bit() || ST.isTargetCygMing() || ST.isTargetKnownWindowsMSVC() ||
48 // Some ABIs align long double to 128 bits, others to 32.
49 if (ST.isTargetNaCl())
51 else if (ST.is64Bit() || ST.isTargetDarwin())
56 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
58 Ret += "-n8:16:32:64";
62 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
63 if (!ST.is64Bit() && (ST.isTargetCygMing() || ST.isTargetKnownWindowsMSVC()))
71 /// X86TargetMachine ctor - Create an X86 target.
73 X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
74 StringRef CPU, StringRef FS,
75 const TargetOptions &Options,
76 Reloc::Model RM, CodeModel::Model CM,
78 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
79 Subtarget(TT, CPU, FS, Options.StackAlignmentOverride),
80 FrameLowering(Subtarget),
81 DL(computeDataLayout(*getSubtargetImpl())),
86 // Determine the PICStyle based on the target selected.
87 if (getRelocationModel() == Reloc::Static) {
88 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
89 Subtarget.setPICStyle(PICStyles::None);
90 } else if (Subtarget.is64Bit()) {
91 // PIC in 64 bit mode is always rip-rel.
92 Subtarget.setPICStyle(PICStyles::RIPRel);
93 } else if (Subtarget.isTargetCOFF()) {
94 Subtarget.setPICStyle(PICStyles::None);
95 } else if (Subtarget.isTargetDarwin()) {
96 if (getRelocationModel() == Reloc::PIC_)
97 Subtarget.setPICStyle(PICStyles::StubPIC);
99 assert(getRelocationModel() == Reloc::DynamicNoPIC);
100 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
102 } else if (Subtarget.isTargetELF()) {
103 Subtarget.setPICStyle(PICStyles::GOT);
106 // default to hard float ABI
107 if (Options.FloatABIType == FloatABI::Default)
108 this->Options.FloatABIType = FloatABI::Hard;
110 // Windows stack unwinder gets confused when execution flow "falls through"
111 // after a call to 'noreturn' function.
112 // To prevent that, we emit a trap for 'unreachable' IR instructions.
113 // (which on X86, happens to be the 'ud2' instruction)
114 if (Subtarget.isTargetWin64())
115 this->Options.TrapUnreachable = true;
120 //===----------------------------------------------------------------------===//
121 // Command line options for x86
122 //===----------------------------------------------------------------------===//
124 UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
125 cl::desc("Minimize AVX to SSE transition penalty"),
128 //===----------------------------------------------------------------------===//
129 // X86 Analysis Pass Setup
130 //===----------------------------------------------------------------------===//
132 void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
133 // Add first the target-independent BasicTTI pass, then our X86 pass. This
134 // allows the X86 pass to delegate to the target independent layer when
136 PM.add(createBasicTargetTransformInfoPass(this));
137 PM.add(createX86TargetTransformInfoPass(this));
141 //===----------------------------------------------------------------------===//
142 // Pass Pipeline Configuration
143 //===----------------------------------------------------------------------===//
146 /// X86 Code Generator Pass Configuration Options.
147 class X86PassConfig : public TargetPassConfig {
149 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
150 : TargetPassConfig(TM, PM) {}
152 X86TargetMachine &getX86TargetMachine() const {
153 return getTM<X86TargetMachine>();
156 const X86Subtarget &getX86Subtarget() const {
157 return *getX86TargetMachine().getSubtargetImpl();
160 bool addInstSelector() override;
161 bool addILPOpts() override;
162 bool addPreRegAlloc() override;
163 bool addPostRegAlloc() override;
164 bool addPreEmitPass() override;
168 TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
169 return new X86PassConfig(this, PM);
172 bool X86PassConfig::addInstSelector() {
173 // Install an instruction selector.
174 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
176 // For ELF, cleanup any local-dynamic TLS accesses.
177 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
178 addPass(createCleanupLocalDynamicTLSPass());
180 addPass(createX86GlobalBaseRegPass());
185 bool X86PassConfig::addILPOpts() {
186 addPass(&EarlyIfConverterID);
190 bool X86PassConfig::addPreRegAlloc() {
191 return false; // -print-machineinstr shouldn't print after this.
194 bool X86PassConfig::addPostRegAlloc() {
195 addPass(createX86FloatingPointStackifierPass());
196 return true; // -print-machineinstr should print after this.
199 bool X86PassConfig::addPreEmitPass() {
200 bool ShouldPrint = false;
201 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
202 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
207 addPass(createX86IssueVZeroUpperPass());
211 if (getOptLevel() != CodeGenOpt::None) {
212 addPass(createX86PadShortFunctions());
213 addPass(createX86FixupLEAs());
220 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
221 JITCodeEmitter &JCE) {
222 PM.add(createX86JITCodeEmitterPass(*this, JCE));