1 //===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===//
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 implements the X86 specific subclass of TargetSubtargetInfo.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86Subtarget.h"
15 #include "X86InstrInfo.h"
16 #include "llvm/IR/Attributes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
26
27 #if defined(_MSC_VER)
28 #include <intrin.h>
29 #endif
30
31 using namespace llvm;
32
33 #define DEBUG_TYPE "subtarget"
34
35 #define GET_SUBTARGETINFO_TARGET_DESC
36 #define GET_SUBTARGETINFO_CTOR
37 #include "X86GenSubtargetInfo.inc"
38
39 // Temporary option to control early if-conversion for x86 while adding machine
40 // models.
41 static cl::opt<bool>
42 X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
43 cl::desc("Enable early if-conversion on X86"));
44
45
46 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
47 /// current subtarget according to how we should reference it in a non-pcrel
48 /// context.
ClassifyBlockAddressReference() const49 unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
50 if (isPICStyleGOT()) // 32-bit ELF targets.
51 return X86II::MO_GOTOFF;
52
53 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
54 return X86II::MO_PIC_BASE_OFFSET;
55
56 // Direct static reference to label.
57 return X86II::MO_NO_FLAG;
58 }
59
60 /// ClassifyGlobalReference - Classify a global variable reference for the
61 /// current subtarget according to how we should reference it in a non-pcrel
62 /// context.
63 unsigned char X86Subtarget::
ClassifyGlobalReference(const GlobalValue * GV,const TargetMachine & TM) const64 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
65 // DLLImport only exists on windows, it is implemented as a load from a
66 // DLLIMPORT stub.
67 if (GV->hasDLLImportStorageClass())
68 return X86II::MO_DLLIMPORT;
69
70 // Determine whether this is a reference to a definition or a declaration.
71 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
72 // load from stub.
73 bool isDecl = GV->hasAvailableExternallyLinkage();
74 if (GV->isDeclaration() && !GV->isMaterializable())
75 isDecl = true;
76
77 // X86-64 in PIC mode.
78 if (isPICStyleRIPRel()) {
79 // Large model never uses stubs.
80 if (TM.getCodeModel() == CodeModel::Large)
81 return X86II::MO_NO_FLAG;
82
83 if (isTargetDarwin()) {
84 // If symbol visibility is hidden, the extra load is not needed if
85 // target is x86-64 or the symbol is definitely defined in the current
86 // translation unit.
87 if (GV->hasDefaultVisibility() &&
88 (isDecl || GV->isWeakForLinker()))
89 return X86II::MO_GOTPCREL;
90 } else if (!isTargetWin64()) {
91 assert(isTargetELF() && "Unknown rip-relative target");
92
93 // Extra load is needed for all externally visible.
94 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
95 return X86II::MO_GOTPCREL;
96 }
97
98 return X86II::MO_NO_FLAG;
99 }
100
101 if (isPICStyleGOT()) { // 32-bit ELF targets.
102 // Extra load is needed for all externally visible.
103 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
104 return X86II::MO_GOTOFF;
105 return X86II::MO_GOT;
106 }
107
108 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
109 // Determine whether we have a stub reference and/or whether the reference
110 // is relative to the PIC base or not.
111
112 // If this is a strong reference to a definition, it is definitely not
113 // through a stub.
114 if (!isDecl && !GV->isWeakForLinker())
115 return X86II::MO_PIC_BASE_OFFSET;
116
117 // Unless we have a symbol with hidden visibility, we have to go through a
118 // normal $non_lazy_ptr stub because this symbol might be resolved late.
119 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
120 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
121
122 // If symbol visibility is hidden, we have a stub for common symbol
123 // references and external declarations.
124 if (isDecl || GV->hasCommonLinkage()) {
125 // Hidden $non_lazy_ptr reference.
126 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
127 }
128
129 // Otherwise, no stub.
130 return X86II::MO_PIC_BASE_OFFSET;
131 }
132
133 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
134 // Determine whether we have a stub reference.
135
136 // If this is a strong reference to a definition, it is definitely not
137 // through a stub.
138 if (!isDecl && !GV->isWeakForLinker())
139 return X86II::MO_NO_FLAG;
140
141 // Unless we have a symbol with hidden visibility, we have to go through a
142 // normal $non_lazy_ptr stub because this symbol might be resolved late.
143 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
144 return X86II::MO_DARWIN_NONLAZY;
145
146 // Otherwise, no stub.
147 return X86II::MO_NO_FLAG;
148 }
149
150 // Direct static reference to global.
151 return X86II::MO_NO_FLAG;
152 }
153
154
155 /// getBZeroEntry - This function returns the name of a function which has an
156 /// interface like the non-standard bzero function, if such a function exists on
157 /// the current subtarget and it is considered prefereable over memset with zero
158 /// passed as the second argument. Otherwise it returns null.
getBZeroEntry() const159 const char *X86Subtarget::getBZeroEntry() const {
160 // Darwin 10 has a __bzero entry point for this purpose.
161 if (getTargetTriple().isMacOSX() &&
162 !getTargetTriple().isMacOSXVersionLT(10, 6))
163 return "__bzero";
164
165 return nullptr;
166 }
167
hasSinCos() const168 bool X86Subtarget::hasSinCos() const {
169 return getTargetTriple().isMacOSX() &&
170 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
171 is64Bit();
172 }
173
174 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
175 /// to immediate address.
IsLegalToCallImmediateAddr(const TargetMachine & TM) const176 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
177 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
178 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does,
179 // the following check for Win32 should be removed.
180 if (In64BitMode || isTargetWin32())
181 return false;
182 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
183 }
184
resetSubtargetFeatures(const MachineFunction * MF)185 void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
186 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
187 Attribute CPUAttr =
188 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
189 Attribute FSAttr =
190 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
191 std::string CPU =
192 !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
193 std::string FS =
194 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
195 if (!FS.empty()) {
196 initializeEnvironment();
197 resetSubtargetFeatures(CPU, FS);
198 }
199 }
200
resetSubtargetFeatures(StringRef CPU,StringRef FS)201 void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
202 std::string CPUName = CPU;
203 if (CPUName.empty())
204 CPUName = "generic";
205
206 // Make sure 64-bit features are available in 64-bit mode. (But make sure
207 // SSE2 can be turned off explicitly.)
208 std::string FullFS = FS;
209 if (In64BitMode) {
210 if (!FullFS.empty())
211 FullFS = "+64bit,+sse2," + FullFS;
212 else
213 FullFS = "+64bit,+sse2";
214 }
215
216 // If feature string is not empty, parse features string.
217 ParseSubtargetFeatures(CPUName, FullFS);
218
219 // Make sure the right MCSchedModel is used.
220 InitCPUSchedModel(CPUName);
221
222 if (X86ProcFamily == IntelAtom || X86ProcFamily == IntelSLM)
223 PostRAScheduler = true;
224
225 InstrItins = getInstrItineraryForCPU(CPUName);
226
227 // It's important to keep the MCSubtargetInfo feature bits in sync with
228 // target data structure which is shared with MC code emitter, etc.
229 if (In64BitMode)
230 ToggleFeature(X86::Mode64Bit);
231 else if (In32BitMode)
232 ToggleFeature(X86::Mode32Bit);
233 else if (In16BitMode)
234 ToggleFeature(X86::Mode16Bit);
235 else
236 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
237
238 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
239 << ", 3DNowLevel " << X863DNowLevel
240 << ", 64bit " << HasX86_64 << "\n");
241 assert((!In64BitMode || HasX86_64) &&
242 "64-bit code requested on a subtarget that doesn't support it!");
243
244 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
245 // 32 and 64 bit) and for all 64-bit targets.
246 if (StackAlignOverride)
247 stackAlignment = StackAlignOverride;
248 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
249 In64BitMode)
250 stackAlignment = 16;
251 }
252
initializeEnvironment()253 void X86Subtarget::initializeEnvironment() {
254 X86SSELevel = NoMMXSSE;
255 X863DNowLevel = NoThreeDNow;
256 HasCMov = false;
257 HasX86_64 = false;
258 HasPOPCNT = false;
259 HasSSE4A = false;
260 HasAES = false;
261 HasPCLMUL = false;
262 HasFMA = false;
263 HasFMA4 = false;
264 HasXOP = false;
265 HasTBM = false;
266 HasMOVBE = false;
267 HasRDRAND = false;
268 HasF16C = false;
269 HasFSGSBase = false;
270 HasLZCNT = false;
271 HasBMI = false;
272 HasBMI2 = false;
273 HasRTM = false;
274 HasHLE = false;
275 HasERI = false;
276 HasCDI = false;
277 HasPFI = false;
278 HasADX = false;
279 HasSHA = false;
280 HasPRFCHW = false;
281 HasRDSEED = false;
282 IsBTMemSlow = false;
283 IsSHLDSlow = false;
284 IsUAMemFast = false;
285 HasVectorUAMem = false;
286 HasCmpxchg16b = false;
287 UseLeaForSP = false;
288 HasSlowDivide = false;
289 PostRAScheduler = false;
290 PadShortFunctions = false;
291 CallRegIndirect = false;
292 LEAUsesAG = false;
293 SlowLEA = false;
294 SlowIncDec = false;
295 stackAlignment = 4;
296 // FIXME: this is a known good value for Yonah. How about others?
297 MaxInlineSizeThreshold = 128;
298 }
299
computeDataLayout(const X86Subtarget & ST)300 static std::string computeDataLayout(const X86Subtarget &ST) {
301 // X86 is little endian
302 std::string Ret = "e";
303
304 Ret += DataLayout::getManglingComponent(ST.getTargetTriple());
305 // X86 and x32 have 32 bit pointers.
306 if (ST.isTarget64BitILP32() || !ST.is64Bit())
307 Ret += "-p:32:32";
308
309 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
310 if (ST.is64Bit() || ST.isOSWindows() || ST.isTargetNaCl())
311 Ret += "-i64:64";
312 else
313 Ret += "-f64:32:64";
314
315 // Some ABIs align long double to 128 bits, others to 32.
316 if (ST.isTargetNaCl())
317 ; // No f80
318 else if (ST.is64Bit() || ST.isTargetDarwin())
319 Ret += "-f80:128";
320 else
321 Ret += "-f80:32";
322
323 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
324 if (ST.is64Bit())
325 Ret += "-n8:16:32:64";
326 else
327 Ret += "-n8:16:32";
328
329 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
330 if (!ST.is64Bit() && ST.isOSWindows())
331 Ret += "-S32";
332 else
333 Ret += "-S128";
334
335 return Ret;
336 }
337
initializeSubtargetDependencies(StringRef CPU,StringRef FS)338 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
339 StringRef FS) {
340 initializeEnvironment();
341 resetSubtargetFeatures(CPU, FS);
342 return *this;
343 }
344
X86Subtarget(const std::string & TT,const std::string & CPU,const std::string & FS,X86TargetMachine & TM,unsigned StackAlignOverride)345 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
346 const std::string &FS, X86TargetMachine &TM,
347 unsigned StackAlignOverride)
348 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
349 PICStyle(PICStyles::None), TargetTriple(TT),
350 StackAlignOverride(StackAlignOverride),
351 In64BitMode(TargetTriple.getArch() == Triple::x86_64),
352 In32BitMode(TargetTriple.getArch() == Triple::x86 &&
353 TargetTriple.getEnvironment() != Triple::CODE16),
354 In16BitMode(TargetTriple.getArch() == Triple::x86 &&
355 TargetTriple.getEnvironment() == Triple::CODE16),
356 DL(computeDataLayout(*this)), TSInfo(DL),
357 InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM),
358 FrameLowering(TargetFrameLowering::StackGrowsDown, getStackAlignment(),
359 is64Bit() ? -8 : -4),
360 JITInfo(hasSSE1()) {}
361
362 bool
enablePostRAScheduler(CodeGenOpt::Level OptLevel,TargetSubtargetInfo::AntiDepBreakMode & Mode,RegClassVector & CriticalPathRCs) const363 X86Subtarget::enablePostRAScheduler(CodeGenOpt::Level OptLevel,
364 TargetSubtargetInfo::AntiDepBreakMode &Mode,
365 RegClassVector &CriticalPathRCs) const {
366 Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
367 CriticalPathRCs.clear();
368 return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
369 }
370
371 bool
enableEarlyIfConversion() const372 X86Subtarget::enableEarlyIfConversion() const {
373 return hasCMov() && X86EarlyIfConv;
374 }
375