[PM] Change the core design of the TTI analysis to use a polymorphic
[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 "X86TargetObjectFile.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/FormattedStream.h"
22 #include "llvm/Support/TargetRegistry.h"
23 #include "llvm/Target/TargetOptions.h"
24 using namespace llvm;
25
26 extern "C" void LLVMInitializeX86Target() {
27   // Register the target.
28   RegisterTargetMachine<X86TargetMachine> X(TheX86_32Target);
29   RegisterTargetMachine<X86TargetMachine> Y(TheX86_64Target);
30 }
31
32 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
33   if (TT.isOSBinFormatMachO()) {
34     if (TT.getArch() == Triple::x86_64)
35       return make_unique<X86_64MachoTargetObjectFile>();
36     return make_unique<TargetLoweringObjectFileMachO>();
37   }
38
39   if (TT.isOSLinux())
40     return make_unique<X86LinuxTargetObjectFile>();
41   if (TT.isOSBinFormatELF())
42     return make_unique<TargetLoweringObjectFileELF>();
43   if (TT.isKnownWindowsMSVCEnvironment())
44     return make_unique<X86WindowsTargetObjectFile>();
45   if (TT.isOSBinFormatCOFF())
46     return make_unique<TargetLoweringObjectFileCOFF>();
47   llvm_unreachable("unknown subtarget type");
48 }
49
50 static std::string computeDataLayout(const Triple &TT) {
51   // X86 is little endian
52   std::string Ret = "e";
53
54   Ret += DataLayout::getManglingComponent(TT);
55   // X86 and x32 have 32 bit pointers.
56   if ((TT.isArch64Bit() &&
57        (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
58       !TT.isArch64Bit())
59     Ret += "-p:32:32";
60
61   // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
62   if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
63     Ret += "-i64:64";
64   else
65     Ret += "-f64:32:64";
66
67   // Some ABIs align long double to 128 bits, others to 32.
68   if (TT.isOSNaCl())
69     ; // No f80
70   else if (TT.isArch64Bit() || TT.isOSDarwin())
71     Ret += "-f80:128";
72   else
73     Ret += "-f80:32";
74
75   // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
76   if (TT.isArch64Bit())
77     Ret += "-n8:16:32:64";
78   else
79     Ret += "-n8:16:32";
80
81   // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
82   if (!TT.isArch64Bit() && TT.isOSWindows())
83     Ret += "-S32";
84   else
85     Ret += "-S128";
86
87   return Ret;
88 }
89
90 /// X86TargetMachine ctor - Create an X86 target.
91 ///
92 X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT, StringRef CPU,
93                                    StringRef FS, const TargetOptions &Options,
94                                    Reloc::Model RM, CodeModel::Model CM,
95                                    CodeGenOpt::Level OL)
96     : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
97       TLOF(createTLOF(Triple(getTargetTriple()))),
98       DL(computeDataLayout(Triple(TT))),
99       Subtarget(TT, CPU, FS, *this, Options.StackAlignmentOverride) {
100   // default to hard float ABI
101   if (Options.FloatABIType == FloatABI::Default)
102     this->Options.FloatABIType = FloatABI::Hard;
103
104   // Windows stack unwinder gets confused when execution flow "falls through"
105   // after a call to 'noreturn' function.
106   // To prevent that, we emit a trap for 'unreachable' IR instructions.
107   // (which on X86, happens to be the 'ud2' instruction)
108   if (Subtarget.isTargetWin64())
109     this->Options.TrapUnreachable = true;
110
111   initAsmInfo();
112 }
113
114 X86TargetMachine::~X86TargetMachine() {}
115
116 const X86Subtarget *
117 X86TargetMachine::getSubtargetImpl(const Function &F) const {
118   AttributeSet FnAttrs = F.getAttributes();
119   Attribute CPUAttr =
120       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
121   Attribute FSAttr =
122       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
123
124   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
125                         ? CPUAttr.getValueAsString().str()
126                         : TargetCPU;
127   std::string FS = !FSAttr.hasAttribute(Attribute::None)
128                        ? FSAttr.getValueAsString().str()
129                        : TargetFS;
130
131   // FIXME: This is related to the code below to reset the target options,
132   // we need to know whether or not the soft float flag is set on the
133   // function before we can generate a subtarget. We also need to use
134   // it as a key for the subtarget since that can be the only difference
135   // between two functions.
136   Attribute SFAttr =
137       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "use-soft-float");
138   bool SoftFloat = !SFAttr.hasAttribute(Attribute::None)
139                        ? SFAttr.getValueAsString() == "true"
140                        : Options.UseSoftFloat;
141
142   auto &I = SubtargetMap[CPU + FS + (SoftFloat ? "use-soft-float=true"
143                                                : "use-soft-float=false")];
144   if (!I) {
145     // This needs to be done before we create a new subtarget since any
146     // creation will depend on the TM and the code generation flags on the
147     // function that reside in TargetOptions.
148     resetTargetOptions(F);
149     I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
150                                         Options.StackAlignmentOverride);
151   }
152   return I.get();
153 }
154
155 //===----------------------------------------------------------------------===//
156 // Command line options for x86
157 //===----------------------------------------------------------------------===//
158 static cl::opt<bool>
159 UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
160   cl::desc("Minimize AVX to SSE transition penalty"),
161   cl::init(true));
162
163 //===----------------------------------------------------------------------===//
164 // X86 Analysis Pass Setup
165 //===----------------------------------------------------------------------===//
166
167 void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
168   PM.add(createX86TargetTransformInfoPass(this));
169 }
170
171
172 //===----------------------------------------------------------------------===//
173 // Pass Pipeline Configuration
174 //===----------------------------------------------------------------------===//
175
176 namespace {
177 /// X86 Code Generator Pass Configuration Options.
178 class X86PassConfig : public TargetPassConfig {
179 public:
180   X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
181     : TargetPassConfig(TM, PM) {}
182
183   X86TargetMachine &getX86TargetMachine() const {
184     return getTM<X86TargetMachine>();
185   }
186
187   const X86Subtarget &getX86Subtarget() const {
188     return *getX86TargetMachine().getSubtargetImpl();
189   }
190
191   void addIRPasses() override;
192   bool addInstSelector() override;
193   bool addILPOpts() override;
194   void addPostRegAlloc() override;
195   void addPreEmitPass() override;
196 };
197 } // namespace
198
199 TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
200   return new X86PassConfig(this, PM);
201 }
202
203 void X86PassConfig::addIRPasses() {
204   addPass(createAtomicExpandPass(&getX86TargetMachine()));
205
206   TargetPassConfig::addIRPasses();
207 }
208
209 bool X86PassConfig::addInstSelector() {
210   // Install an instruction selector.
211   addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
212
213   // For ELF, cleanup any local-dynamic TLS accesses.
214   if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
215     addPass(createCleanupLocalDynamicTLSPass());
216
217   addPass(createX86GlobalBaseRegPass());
218
219   return false;
220 }
221
222 bool X86PassConfig::addILPOpts() {
223   addPass(&EarlyIfConverterID);
224   return true;
225 }
226
227 void X86PassConfig::addPostRegAlloc() {
228   addPass(createX86FloatingPointStackifierPass());
229 }
230
231 void X86PassConfig::addPreEmitPass() {
232   if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2())
233     addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
234
235   if (UseVZeroUpper)
236     addPass(createX86IssueVZeroUpperPass());
237
238   if (getOptLevel() != CodeGenOpt::None) {
239     addPass(createX86PadShortFunctions());
240     addPass(createX86FixupLEAs());
241   }
242 }