blob: 989e0d61b6fcdfd66443731ea8d70a5b074a9523 [file] [log] [blame]
Bill Wendling2bce78e2010-12-04 23:57:24 +00001//===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===//
Nate Begemanf26625e2005-07-12 01:41:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begemanf26625e2005-07-12 01:41:54 +00007//
8//===----------------------------------------------------------------------===//
9//
Evan Cheng0d639a22011-07-01 21:01:15 +000010// This file implements the X86 specific subclass of TargetSubtargetInfo.
Nate Begemanf26625e2005-07-12 01:41:54 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "X86Subtarget.h"
Chris Lattnerdc842c02009-07-10 07:20:05 +000015#include "X86InstrInfo.h"
Bill Wendlingaef9c372013-02-15 22:31:27 +000016#include "llvm/IR/Attributes.h"
17#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/GlobalValue.h"
Eric Christopher3470bbb2014-05-21 23:51:57 +000019#include "llvm/Support/CommandLine.h"
Evan Cheng9a3ec1b2009-01-03 04:04:46 +000020#include "llvm/Support/Debug.h"
Rafael Espindola65596562011-09-07 16:10:57 +000021#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000022#include "llvm/Support/Host.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov430e68a12006-12-22 22:29:05 +000024#include "llvm/Target/TargetMachine.h"
Rafael Espindola65596562011-09-07 16:10:57 +000025#include "llvm/Target/TargetOptions.h"
Evan Cheng54b68e32011-07-01 20:45:01 +000026
Chris Lattner3ad60b12009-04-25 18:27:23 +000027#if defined(_MSC_VER)
Bill Wendling6eecd562009-08-03 00:11:34 +000028#include <intrin.h>
Chris Lattner3ad60b12009-04-25 18:27:23 +000029#endif
30
Chandler Carruth84e68b22014-04-22 02:41:26 +000031using namespace llvm;
32
33#define DEBUG_TYPE "subtarget"
34
Chandler Carruthd174b722014-04-22 02:03:14 +000035#define GET_SUBTARGETINFO_TARGET_DESC
36#define GET_SUBTARGETINFO_CTOR
37#include "X86GenSubtargetInfo.inc"
38
Eric Christopher6b0fcfe2014-05-21 23:40:26 +000039// Temporary option to control early if-conversion for x86 while adding machine
40// models.
41static cl::opt<bool>
42X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
43 cl::desc("Enable early if-conversion on X86"));
44
45
Dan Gohman7a6611792009-11-20 23:18:13 +000046/// ClassifyBlockAddressReference - Classify a blockaddress reference for the
47/// current subtarget according to how we should reference it in a non-pcrel
48/// context.
Eric Christophere2fbc672013-04-02 23:06:40 +000049unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
Dan Gohman7a6611792009-11-20 23:18:13 +000050 if (isPICStyleGOT()) // 32-bit ELF targets.
51 return X86II::MO_GOTOFF;
Chad Rosier24c19d22012-08-01 18:39:17 +000052
Dan Gohman7a6611792009-11-20 23:18:13 +000053 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
54 return X86II::MO_PIC_BASE_OFFSET;
Chad Rosier24c19d22012-08-01 18:39:17 +000055
Dan Gohman7a6611792009-11-20 23:18:13 +000056 // Direct static reference to label.
57 return X86II::MO_NO_FLAG;
58}
59
Chris Lattnerdc842c02009-07-10 07:20:05 +000060/// ClassifyGlobalReference - Classify a global variable reference for the
61/// current subtarget according to how we should reference it in a non-pcrel
62/// context.
63unsigned char X86Subtarget::
64ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
65 // DLLImport only exists on windows, it is implemented as a load from a
66 // DLLIMPORT stub.
Nico Rieck7157bb72014-01-14 15:22:47 +000067 if (GV->hasDLLImportStorageClass())
Chris Lattnerdc842c02009-07-10 07:20:05 +000068 return X86II::MO_DLLIMPORT;
69
Chris Lattnerfaa7bdc2010-06-14 20:11:56 +000070 // 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;
Evan Cheng02a76522009-07-16 22:53:10 +000076
Chris Lattnerdc842c02009-07-10 07:20:05 +000077 // 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;
Chad Rosier24c19d22012-08-01 18:39:17 +000082
Chris Lattner7dce9912009-07-10 21:01:59 +000083 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() &&
Evan Cheng02a76522009-07-16 22:53:10 +000088 (isDecl || GV->isWeakForLinker()))
Chris Lattner7dce9912009-07-10 21:01:59 +000089 return X86II::MO_GOTPCREL;
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000090 } else if (!isTargetWin64()) {
Chris Lattner7dce9912009-07-10 21:01:59 +000091 assert(isTargetELF() && "Unknown rip-relative target");
Chris Lattnerdc842c02009-07-10 07:20:05 +000092
Chris Lattner7dce9912009-07-10 21:01:59 +000093 // Extra load is needed for all externally visible.
94 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
95 return X86II::MO_GOTPCREL;
96 }
Chris Lattnerdc842c02009-07-10 07:20:05 +000097
98 return X86II::MO_NO_FLAG;
99 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000100
Chris Lattnerdc842c02009-07-10 07:20:05 +0000101 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 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000107
Chris Lattner21c29402009-07-10 21:00:45 +0000108 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000109 // Determine whether we have a stub reference and/or whether the reference
110 // is relative to the PIC base or not.
Chad Rosier24c19d22012-08-01 18:39:17 +0000111
Chris Lattnerdc842c02009-07-10 07:20:05 +0000112 // If this is a strong reference to a definition, it is definitely not
113 // through a stub.
Evan Cheng02a76522009-07-16 22:53:10 +0000114 if (!isDecl && !GV->isWeakForLinker())
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000115 return X86II::MO_PIC_BASE_OFFSET;
Chris Lattnerdc842c02009-07-10 07:20:05 +0000116
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.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000119 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
120 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
Chad Rosier24c19d22012-08-01 18:39:17 +0000121
Chris Lattnerdc842c02009-07-10 07:20:05 +0000122 // If symbol visibility is hidden, we have a stub for common symbol
123 // references and external declarations.
Evan Cheng02a76522009-07-16 22:53:10 +0000124 if (isDecl || GV->hasCommonLinkage()) {
Chris Lattnerdc842c02009-07-10 07:20:05 +0000125 // Hidden $non_lazy_ptr reference.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000126 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
Chris Lattnerdc842c02009-07-10 07:20:05 +0000127 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000128
Chris Lattnerdc842c02009-07-10 07:20:05 +0000129 // Otherwise, no stub.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000130 return X86II::MO_PIC_BASE_OFFSET;
131 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000132
Chris Lattner21c29402009-07-10 21:00:45 +0000133 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000134 // Determine whether we have a stub reference.
Chad Rosier24c19d22012-08-01 18:39:17 +0000135
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000136 // If this is a strong reference to a definition, it is definitely not
137 // through a stub.
Evan Cheng02a76522009-07-16 22:53:10 +0000138 if (!isDecl && !GV->isWeakForLinker())
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000139 return X86II::MO_NO_FLAG;
Chad Rosier24c19d22012-08-01 18:39:17 +0000140
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000141 // 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;
Evan Cheng1b389522009-09-03 07:04:02 +0000145
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000146 // Otherwise, no stub.
147 return X86II::MO_NO_FLAG;
Chris Lattnerdc842c02009-07-10 07:20:05 +0000148 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000149
Chris Lattnerdc842c02009-07-10 07:20:05 +0000150 // Direct static reference to global.
151 return X86II::MO_NO_FLAG;
152}
153
Anton Korobeynikov6dbdfe22006-11-30 22:42:55 +0000154
Bill Wendlingbd092622008-09-30 21:22:07 +0000155/// 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.
Bill Wendling17825842008-09-30 22:05:33 +0000159const char *X86Subtarget::getBZeroEntry() const {
Dan Gohman980d7202008-04-01 20:38:36 +0000160 // Darwin 10 has a __bzero entry point for this purpose.
Daniel Dunbarcd01ed52011-04-20 00:14:25 +0000161 if (getTargetTriple().isMacOSX() &&
162 !getTargetTriple().isMacOSXVersionLT(10, 6))
Bill Wendling17825842008-09-30 22:05:33 +0000163 return "__bzero";
Dan Gohman980d7202008-04-01 20:38:36 +0000164
Craig Topper062a2ba2014-04-25 05:30:21 +0000165 return nullptr;
Dan Gohman980d7202008-04-01 20:38:36 +0000166}
167
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000168bool X86Subtarget::hasSinCos() const {
169 return getTargetTriple().isMacOSX() &&
Evan Chengd2ca4e22013-01-30 22:56:35 +0000170 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
171 is64Bit();
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000172}
173
Evan Cheng96098332009-05-20 04:53:57 +0000174/// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
175/// to immediate address.
176bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
David Majnemer02f21882014-03-28 21:40:47 +0000177 // 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())
Evan Cheng96098332009-05-20 04:53:57 +0000181 return false;
182 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
183}
184
Bill Wendlingaef9c372013-02-15 22:31:27 +0000185void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
186 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
Eric Christopherb8f97682014-05-07 21:05:47 +0000187 Attribute CPUAttr =
188 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
189 Attribute FSAttr =
190 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
Nadav Rotem08ab8772013-02-27 05:56:20 +0000191 std::string CPU =
Eric Christopherb8f97682014-05-07 21:05:47 +0000192 !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
Nadav Rotem08ab8772013-02-27 05:56:20 +0000193 std::string FS =
Eric Christopherb8f97682014-05-07 21:05:47 +0000194 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
Bill Wendling61375d82013-02-16 01:36:26 +0000195 if (!FS.empty()) {
196 initializeEnvironment();
Bill Wendlingaef9c372013-02-15 22:31:27 +0000197 resetSubtargetFeatures(CPU, FS);
Bill Wendling61375d82013-02-16 01:36:26 +0000198 }
Bill Wendlingaef9c372013-02-15 22:31:27 +0000199}
200
201void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
Nadav Rotem08ab8772013-02-27 05:56:20 +0000202 std::string CPUName = CPU;
Jim Grosbach48551fb2014-04-12 01:34:29 +0000203 if (CPUName.empty())
204 CPUName = "generic";
Evan Cheng964cb5f2011-07-08 21:14:14 +0000205
Jim Grosbach48551fb2014-04-12 01:34:29 +0000206 // 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";
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000214 }
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000215
Jim Grosbach48551fb2014-04-12 01:34:29 +0000216 // If feature string is not empty, parse features string.
217 ParseSubtargetFeatures(CPUName, FullFS);
218
219 // Make sure the right MCSchedModel is used.
Craig Toppera8442342013-09-18 05:54:09 +0000220 InitCPUSchedModel(CPUName);
Preston Gurd35fcb542012-10-03 15:55:13 +0000221
Preston Gurd3fe264d2013-09-13 19:23:28 +0000222 if (X86ProcFamily == IntelAtom || X86ProcFamily == IntelSLM)
Andrew Trick8523b162012-02-01 23:20:51 +0000223 PostRAScheduler = true;
Andrew Tricke0c83b12012-08-07 00:25:30 +0000224
225 InstrItins = getInstrItineraryForCPU(CPUName);
Andrew Trick8523b162012-02-01 23:20:51 +0000226
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000227 // 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);
Craig Topper3c80d622014-01-06 04:55:54 +0000231 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!");
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000237
David Greene00411812010-01-05 01:29:13 +0000238 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
Bill Wendling6eecd562009-08-03 00:11:34 +0000239 << ", 3DNowLevel " << X863DNowLevel
240 << ", 64bit " << HasX86_64 << "\n");
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000241 assert((!In64BitMode || HasX86_64) &&
Dan Gohman74037512009-02-03 00:04:43 +0000242 "64-bit code requested on a subtarget that doesn't support it!");
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000243
Roman Divacky22135672012-11-09 20:10:44 +0000244 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
Roman Divackye8a93fe82011-02-22 17:30:05 +0000245 // 32 and 64 bit) and for all 64-bit targets.
Evan Cheng3a0c5e52011-06-23 17:54:54 +0000246 if (StackAlignOverride)
247 stackAlignment = StackAlignOverride;
Roman Divacky22135672012-11-09 20:10:44 +0000248 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
249 In64BitMode)
Nate Begemanf26625e2005-07-12 01:41:54 +0000250 stackAlignment = 16;
Dan Gohmandc53f1c2010-05-27 18:43:40 +0000251}
Andrew Trick8523b162012-02-01 23:20:51 +0000252
Bill Wendling61375d82013-02-16 01:36:26 +0000253void 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;
Yunzhong Gaodd36e932013-09-24 18:21:52 +0000265 HasTBM = false;
Bill Wendling61375d82013-02-16 01:36:26 +0000266 HasMOVBE = false;
267 HasRDRAND = false;
268 HasF16C = false;
269 HasFSGSBase = false;
270 HasLZCNT = false;
271 HasBMI = false;
272 HasBMI2 = false;
273 HasRTM = false;
Michael Liaoe344ec92013-03-26 22:46:02 +0000274 HasHLE = false;
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000275 HasERI = false;
276 HasCDI = false;
Craig Topper7a8cf012013-08-20 05:23:59 +0000277 HasPFI = false;
Bill Wendling61375d82013-02-16 01:36:26 +0000278 HasADX = false;
Ben Langmuir16501752013-09-12 15:51:31 +0000279 HasSHA = false;
Michael Liao5173ee02013-03-26 17:47:11 +0000280 HasPRFCHW = false;
Michael Liaoa486a112013-03-28 23:41:26 +0000281 HasRDSEED = false;
Bill Wendling61375d82013-02-16 01:36:26 +0000282 IsBTMemSlow = false;
Ekaterina Romanovad5fa5542013-11-21 23:21:26 +0000283 IsSHLDSlow = false;
Bill Wendling61375d82013-02-16 01:36:26 +0000284 IsUAMemFast = false;
285 HasVectorUAMem = false;
286 HasCmpxchg16b = false;
287 UseLeaForSP = false;
288 HasSlowDivide = false;
289 PostRAScheduler = false;
290 PadShortFunctions = false;
Preston Gurd663e6f92013-03-27 19:14:02 +0000291 CallRegIndirect = false;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000292 LEAUsesAG = false;
Alexey Volkov6226de62014-05-20 08:55:50 +0000293 SlowLEA = false;
Bill Wendling61375d82013-02-16 01:36:26 +0000294 stackAlignment = 4;
295 // FIXME: this is a known good value for Yonah. How about others?
296 MaxInlineSizeThreshold = 128;
297}
298
Bill Wendlingaef9c372013-02-15 22:31:27 +0000299X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
Eric Christopherb8f97682014-05-07 21:05:47 +0000300 const std::string &FS, unsigned StackAlignOverride)
301 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
302 PICStyle(PICStyles::None), TargetTriple(TT),
303 StackAlignOverride(StackAlignOverride),
304 In64BitMode(TargetTriple.getArch() == Triple::x86_64),
305 In32BitMode(TargetTriple.getArch() == Triple::x86 &&
306 TargetTriple.getEnvironment() != Triple::CODE16),
307 In16BitMode(TargetTriple.getArch() == Triple::x86 &&
308 TargetTriple.getEnvironment() == Triple::CODE16) {
Bill Wendling61375d82013-02-16 01:36:26 +0000309 initializeEnvironment();
Bill Wendlingaef9c372013-02-15 22:31:27 +0000310 resetSubtargetFeatures(CPU, FS);
311}
312
Eric Christopherb8f97682014-05-07 21:05:47 +0000313bool
314X86Subtarget::enablePostRAScheduler(CodeGenOpt::Level OptLevel,
315 TargetSubtargetInfo::AntiDepBreakMode &Mode,
316 RegClassVector &CriticalPathRCs) const {
Preston Gurd9a091472012-04-23 21:39:35 +0000317 Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
Andrew Trick8523b162012-02-01 23:20:51 +0000318 CriticalPathRCs.clear();
319 return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
320}
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000321
322bool
Eric Christopher3470bbb2014-05-21 23:51:57 +0000323X86Subtarget::enableEarlyIfConversion() const {
324 return hasCMov() && X86EarlyIfConv;
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000325}