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 /// X86TargetMachine ctor - Create an X86 target.
34 X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT, StringRef CPU,
35 StringRef FS, const TargetOptions &Options,
36 Reloc::Model RM, CodeModel::Model CM,
38 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
39 Subtarget(TT, CPU, FS, *this, Options.StackAlignmentOverride) {
40 // Determine the PICStyle based on the target selected.
41 if (getRelocationModel() == Reloc::Static) {
42 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
43 Subtarget.setPICStyle(PICStyles::None);
44 } else if (Subtarget.is64Bit()) {
45 // PIC in 64 bit mode is always rip-rel.
46 Subtarget.setPICStyle(PICStyles::RIPRel);
47 } else if (Subtarget.isTargetCOFF()) {
48 Subtarget.setPICStyle(PICStyles::None);
49 } else if (Subtarget.isTargetDarwin()) {
50 if (getRelocationModel() == Reloc::PIC_)
51 Subtarget.setPICStyle(PICStyles::StubPIC);
53 assert(getRelocationModel() == Reloc::DynamicNoPIC);
54 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
56 } else if (Subtarget.isTargetELF()) {
57 Subtarget.setPICStyle(PICStyles::GOT);
60 // default to hard float ABI
61 if (Options.FloatABIType == FloatABI::Default)
62 this->Options.FloatABIType = FloatABI::Hard;
64 // Windows stack unwinder gets confused when execution flow "falls through"
65 // after a call to 'noreturn' function.
66 // To prevent that, we emit a trap for 'unreachable' IR instructions.
67 // (which on X86, happens to be the 'ud2' instruction)
68 if (Subtarget.isTargetWin64())
69 this->Options.TrapUnreachable = true;
74 //===----------------------------------------------------------------------===//
75 // Command line options for x86
76 //===----------------------------------------------------------------------===//
78 UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
79 cl::desc("Minimize AVX to SSE transition penalty"),
82 //===----------------------------------------------------------------------===//
83 // X86 Analysis Pass Setup
84 //===----------------------------------------------------------------------===//
86 void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
87 // Add first the target-independent BasicTTI pass, then our X86 pass. This
88 // allows the X86 pass to delegate to the target independent layer when
90 PM.add(createBasicTargetTransformInfoPass(this));
91 PM.add(createX86TargetTransformInfoPass(this));
95 //===----------------------------------------------------------------------===//
96 // Pass Pipeline Configuration
97 //===----------------------------------------------------------------------===//
100 /// X86 Code Generator Pass Configuration Options.
101 class X86PassConfig : public TargetPassConfig {
103 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
104 : TargetPassConfig(TM, PM) {}
106 X86TargetMachine &getX86TargetMachine() const {
107 return getTM<X86TargetMachine>();
110 const X86Subtarget &getX86Subtarget() const {
111 return *getX86TargetMachine().getSubtargetImpl();
114 bool addInstSelector() override;
115 bool addILPOpts() override;
116 bool addPreRegAlloc() override;
117 bool addPostRegAlloc() override;
118 bool addPreEmitPass() override;
122 TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
123 return new X86PassConfig(this, PM);
126 bool X86PassConfig::addInstSelector() {
127 // Install an instruction selector.
128 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
130 // For ELF, cleanup any local-dynamic TLS accesses.
131 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
132 addPass(createCleanupLocalDynamicTLSPass());
134 addPass(createX86GlobalBaseRegPass());
139 bool X86PassConfig::addILPOpts() {
140 addPass(&EarlyIfConverterID);
144 bool X86PassConfig::addPreRegAlloc() {
145 return false; // -print-machineinstr shouldn't print after this.
148 bool X86PassConfig::addPostRegAlloc() {
149 addPass(createX86FloatingPointStackifierPass());
150 return true; // -print-machineinstr should print after this.
153 bool X86PassConfig::addPreEmitPass() {
154 bool ShouldPrint = false;
155 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
156 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
161 addPass(createX86IssueVZeroUpperPass());
165 if (getOptLevel() != CodeGenOpt::None) {
166 addPass(createX86PadShortFunctions());
167 addPass(createX86FixupLEAs());
174 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
175 JITCodeEmitter &JCE) {
176 PM.add(createX86JITCodeEmitterPass(*this, JCE));