Remove all local variables from X86SelectionDAGInfo, the DAG has
[oota-llvm.git] / lib / Target / X86 / X86TargetMachine.cpp
1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the X86 specific subclass of TargetMachine.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86TargetMachine.h"
15 #include "X86.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"
22 using namespace llvm;
23
24 extern "C" void LLVMInitializeX86Target() {
25   // Register the target.
26   RegisterTargetMachine<X86TargetMachine> X(TheX86_32Target);
27   RegisterTargetMachine<X86TargetMachine> Y(TheX86_64Target);
28 }
29
30 void X86TargetMachine::anchor() { }
31
32 static std::string computeDataLayout(const X86Subtarget &ST) {
33   // X86 is little endian
34   std::string Ret = "e";
35
36   Ret += DataLayout::getManglingComponent(ST.getTargetTriple());
37   // X86 and x32 have 32 bit pointers.
38   if (ST.isTarget64BitILP32() || !ST.is64Bit())
39     Ret += "-p:32:32";
40
41   // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
42   if (ST.is64Bit() || ST.isTargetCygMing() || ST.isTargetKnownWindowsMSVC() ||
43       ST.isTargetNaCl())
44     Ret += "-i64:64";
45   else
46     Ret += "-f64:32:64";
47
48   // Some ABIs align long double to 128 bits, others to 32.
49   if (ST.isTargetNaCl())
50     ; // No f80
51   else if (ST.is64Bit() || ST.isTargetDarwin())
52     Ret += "-f80:128";
53   else
54     Ret += "-f80:32";
55
56   // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
57   if (ST.is64Bit())
58     Ret += "-n8:16:32:64";
59   else
60     Ret += "-n8:16:32";
61
62   // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
63   if (!ST.is64Bit() && (ST.isTargetCygMing() || ST.isTargetKnownWindowsMSVC()))
64     Ret += "-S32";
65   else
66     Ret += "-S128";
67
68   return Ret;
69 }
70
71 /// X86TargetMachine ctor - Create an X86 target.
72 ///
73 X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT, StringRef CPU,
74                                    StringRef FS, const TargetOptions &Options,
75                                    Reloc::Model RM, CodeModel::Model CM,
76                                    CodeGenOpt::Level OL)
77     : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
78       Subtarget(TT, CPU, FS, Options.StackAlignmentOverride),
79       FrameLowering(TargetFrameLowering::StackGrowsDown,
80                     Subtarget.getStackAlignment(),
81                     Subtarget.is64Bit() ? -8 : -4),
82       DL(computeDataLayout(*getSubtargetImpl())), InstrInfo(*this),
83       TLInfo(*this), TSInfo(DL), JITInfo(*this) {
84   // Determine the PICStyle based on the target selected.
85   if (getRelocationModel() == Reloc::Static) {
86     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
87     Subtarget.setPICStyle(PICStyles::None);
88   } else if (Subtarget.is64Bit()) {
89     // PIC in 64 bit mode is always rip-rel.
90     Subtarget.setPICStyle(PICStyles::RIPRel);
91   } else if (Subtarget.isTargetCOFF()) {
92     Subtarget.setPICStyle(PICStyles::None);
93   } else if (Subtarget.isTargetDarwin()) {
94     if (getRelocationModel() == Reloc::PIC_)
95       Subtarget.setPICStyle(PICStyles::StubPIC);
96     else {
97       assert(getRelocationModel() == Reloc::DynamicNoPIC);
98       Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
99     }
100   } else if (Subtarget.isTargetELF()) {
101     Subtarget.setPICStyle(PICStyles::GOT);
102   }
103
104   // default to hard float ABI
105   if (Options.FloatABIType == FloatABI::Default)
106     this->Options.FloatABIType = FloatABI::Hard;
107
108   // Windows stack unwinder gets confused when execution flow "falls through"
109   // after a call to 'noreturn' function.
110   // To prevent that, we emit a trap for 'unreachable' IR instructions.
111   // (which on X86, happens to be the 'ud2' instruction)
112   if (Subtarget.isTargetWin64())
113     this->Options.TrapUnreachable = true;
114
115   initAsmInfo();
116 }
117
118 //===----------------------------------------------------------------------===//
119 // Command line options for x86
120 //===----------------------------------------------------------------------===//
121 static cl::opt<bool>
122 UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
123   cl::desc("Minimize AVX to SSE transition penalty"),
124   cl::init(true));
125
126 //===----------------------------------------------------------------------===//
127 // X86 Analysis Pass Setup
128 //===----------------------------------------------------------------------===//
129
130 void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
131   // Add first the target-independent BasicTTI pass, then our X86 pass. This
132   // allows the X86 pass to delegate to the target independent layer when
133   // appropriate.
134   PM.add(createBasicTargetTransformInfoPass(this));
135   PM.add(createX86TargetTransformInfoPass(this));
136 }
137
138
139 //===----------------------------------------------------------------------===//
140 // Pass Pipeline Configuration
141 //===----------------------------------------------------------------------===//
142
143 namespace {
144 /// X86 Code Generator Pass Configuration Options.
145 class X86PassConfig : public TargetPassConfig {
146 public:
147   X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
148     : TargetPassConfig(TM, PM) {}
149
150   X86TargetMachine &getX86TargetMachine() const {
151     return getTM<X86TargetMachine>();
152   }
153
154   const X86Subtarget &getX86Subtarget() const {
155     return *getX86TargetMachine().getSubtargetImpl();
156   }
157
158   bool addInstSelector() override;
159   bool addILPOpts() override;
160   bool addPreRegAlloc() override;
161   bool addPostRegAlloc() override;
162   bool addPreEmitPass() override;
163 };
164 } // namespace
165
166 TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
167   return new X86PassConfig(this, PM);
168 }
169
170 bool X86PassConfig::addInstSelector() {
171   // Install an instruction selector.
172   addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
173
174   // For ELF, cleanup any local-dynamic TLS accesses.
175   if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
176     addPass(createCleanupLocalDynamicTLSPass());
177
178   addPass(createX86GlobalBaseRegPass());
179
180   return false;
181 }
182
183 bool X86PassConfig::addILPOpts() {
184   addPass(&EarlyIfConverterID);
185   return true;
186 }
187
188 bool X86PassConfig::addPreRegAlloc() {
189   return false;  // -print-machineinstr shouldn't print after this.
190 }
191
192 bool X86PassConfig::addPostRegAlloc() {
193   addPass(createX86FloatingPointStackifierPass());
194   return true;  // -print-machineinstr should print after this.
195 }
196
197 bool X86PassConfig::addPreEmitPass() {
198   bool ShouldPrint = false;
199   if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
200     addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
201     ShouldPrint = true;
202   }
203
204   if (UseVZeroUpper) {
205     addPass(createX86IssueVZeroUpperPass());
206     ShouldPrint = true;
207   }
208
209   if (getOptLevel() != CodeGenOpt::None) {
210     addPass(createX86PadShortFunctions());
211     addPass(createX86FixupLEAs());
212     ShouldPrint = true;
213   }
214
215   return ShouldPrint;
216 }
217
218 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
219                                       JITCodeEmitter &JCE) {
220   PM.add(createX86JITCodeEmitterPass(*this, JCE));
221
222   return false;
223 }