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