X86Subtarget.cpp revision 276479
1251881Speter//===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===// 2251881Speter// 3251881Speter// The LLVM Compiler Infrastructure 4251881Speter// 5251881Speter// This file is distributed under the University of Illinois Open Source 6251881Speter// License. See LICENSE.TXT for details. 7251881Speter// 8251881Speter//===----------------------------------------------------------------------===// 9251881Speter// 10251881Speter// This file implements the X86 specific subclass of TargetSubtargetInfo. 11251881Speter// 12251881Speter//===----------------------------------------------------------------------===// 13251881Speter 14251881Speter#include "X86Subtarget.h" 15251881Speter#include "X86InstrInfo.h" 16251881Speter#include "llvm/IR/Attributes.h" 17251881Speter#include "llvm/IR/Function.h" 18251881Speter#include "llvm/IR/GlobalValue.h" 19251881Speter#include "llvm/Support/CommandLine.h" 20251881Speter#include "llvm/Support/Debug.h" 21251881Speter#include "llvm/Support/ErrorHandling.h" 22251881Speter#include "llvm/Support/Host.h" 23251881Speter#include "llvm/Support/raw_ostream.h" 24251881Speter#include "llvm/Target/TargetMachine.h" 25251881Speter#include "llvm/Target/TargetOptions.h" 26251881Speter 27251881Speter#if defined(_MSC_VER) 28251881Speter#include <intrin.h> 29251881Speter#endif 30251881Speter 31251881Speterusing namespace llvm; 32251881Speter 33251881Speter#define DEBUG_TYPE "subtarget" 34251881Speter 35251881Speter#define GET_SUBTARGETINFO_TARGET_DESC 36251881Speter#define GET_SUBTARGETINFO_CTOR 37251881Speter#include "X86GenSubtargetInfo.inc" 38251881Speter 39251881Speter// Temporary option to control early if-conversion for x86 while adding machine 40251881Speter// models. 41251881Speterstatic cl::opt<bool> 42251881SpeterX86EarlyIfConv("x86-early-ifcvt", cl::Hidden, 43251881Speter cl::desc("Enable early if-conversion on X86")); 44251881Speter 45251881Speter 46251881Speter/// ClassifyBlockAddressReference - Classify a blockaddress reference for the 47251881Speter/// current subtarget according to how we should reference it in a non-pcrel 48251881Speter/// context. 49251881Speterunsigned char X86Subtarget::ClassifyBlockAddressReference() const { 50251881Speter if (isPICStyleGOT()) // 32-bit ELF targets. 51251881Speter return X86II::MO_GOTOFF; 52251881Speter 53251881Speter if (isPICStyleStubPIC()) // Darwin/32 in PIC mode. 54251881Speter return X86II::MO_PIC_BASE_OFFSET; 55251881Speter 56251881Speter // Direct static reference to label. 57251881Speter return X86II::MO_NO_FLAG; 58251881Speter} 59251881Speter 60251881Speter/// ClassifyGlobalReference - Classify a global variable reference for the 61251881Speter/// current subtarget according to how we should reference it in a non-pcrel 62251881Speter/// context. 63251881Speterunsigned char X86Subtarget:: 64251881SpeterClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const { 65251881Speter // DLLImport only exists on windows, it is implemented as a load from a 66251881Speter // DLLIMPORT stub. 67251881Speter if (GV->hasDLLImportStorageClass()) 68251881Speter return X86II::MO_DLLIMPORT; 69251881Speter 70251881Speter // Determine whether this is a reference to a definition or a declaration. 71251881Speter // Materializable GVs (in JIT lazy compilation mode) do not require an extra 72251881Speter // load from stub. 73251881Speter bool isDecl = GV->hasAvailableExternallyLinkage(); 74251881Speter if (GV->isDeclaration() && !GV->isMaterializable()) 75251881Speter isDecl = true; 76251881Speter 77251881Speter // X86-64 in PIC mode. 78251881Speter 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. 159const 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 168bool 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. 176bool 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 185void 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 201void 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 InstrItins = getInstrItineraryForCPU(CPUName); 223 224 // It's important to keep the MCSubtargetInfo feature bits in sync with 225 // target data structure which is shared with MC code emitter, etc. 226 if (In64BitMode) 227 ToggleFeature(X86::Mode64Bit); 228 else if (In32BitMode) 229 ToggleFeature(X86::Mode32Bit); 230 else if (In16BitMode) 231 ToggleFeature(X86::Mode16Bit); 232 else 233 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!"); 234 235 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel 236 << ", 3DNowLevel " << X863DNowLevel 237 << ", 64bit " << HasX86_64 << "\n"); 238 assert((!In64BitMode || HasX86_64) && 239 "64-bit code requested on a subtarget that doesn't support it!"); 240 241 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both 242 // 32 and 64 bit) and for all 64-bit targets. 243 if (StackAlignOverride) 244 stackAlignment = StackAlignOverride; 245 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() || 246 In64BitMode) 247 stackAlignment = 16; 248} 249 250void X86Subtarget::initializeEnvironment() { 251 X86SSELevel = NoMMXSSE; 252 X863DNowLevel = NoThreeDNow; 253 HasCMov = false; 254 HasX86_64 = false; 255 HasPOPCNT = false; 256 HasSSE4A = false; 257 HasAES = false; 258 HasPCLMUL = false; 259 HasFMA = false; 260 HasFMA4 = false; 261 HasXOP = false; 262 HasTBM = false; 263 HasMOVBE = false; 264 HasRDRAND = false; 265 HasF16C = false; 266 HasFSGSBase = false; 267 HasLZCNT = false; 268 HasBMI = false; 269 HasBMI2 = false; 270 HasRTM = false; 271 HasHLE = false; 272 HasERI = false; 273 HasCDI = false; 274 HasPFI = false; 275 HasDQI = false; 276 HasBWI = false; 277 HasVLX = 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 PadShortFunctions = false; 290 CallRegIndirect = false; 291 LEAUsesAG = false; 292 SlowLEA = false; 293 SlowIncDec = false; 294 stackAlignment = 4; 295 // FIXME: this is a known good value for Yonah. How about others? 296 MaxInlineSizeThreshold = 128; 297} 298 299static std::string computeDataLayout(const X86Subtarget &ST) { 300 // X86 is little endian 301 std::string Ret = "e"; 302 303 Ret += DataLayout::getManglingComponent(ST.getTargetTriple()); 304 // X86 and x32 have 32 bit pointers. 305 if (ST.isTarget64BitILP32() || !ST.is64Bit()) 306 Ret += "-p:32:32"; 307 308 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32. 309 if (ST.is64Bit() || ST.isOSWindows() || ST.isTargetNaCl()) 310 Ret += "-i64:64"; 311 else 312 Ret += "-f64:32:64"; 313 314 // Some ABIs align long double to 128 bits, others to 32. 315 if (ST.isTargetNaCl()) 316 ; // No f80 317 else if (ST.is64Bit() || ST.isTargetDarwin()) 318 Ret += "-f80:128"; 319 else 320 Ret += "-f80:32"; 321 322 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits. 323 if (ST.is64Bit()) 324 Ret += "-n8:16:32:64"; 325 else 326 Ret += "-n8:16:32"; 327 328 // The stack is aligned to 32 bits on some ABIs and 128 bits on others. 329 if (!ST.is64Bit() && ST.isOSWindows()) 330 Ret += "-S32"; 331 else 332 Ret += "-S128"; 333 334 return Ret; 335} 336 337X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU, 338 StringRef FS) { 339 initializeEnvironment(); 340 resetSubtargetFeatures(CPU, FS); 341 return *this; 342} 343 344X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU, 345 const std::string &FS, X86TargetMachine &TM, 346 unsigned StackAlignOverride) 347 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others), 348 PICStyle(PICStyles::None), TargetTriple(TT), 349 StackAlignOverride(StackAlignOverride), 350 In64BitMode(TargetTriple.getArch() == Triple::x86_64), 351 In32BitMode(TargetTriple.getArch() == Triple::x86 && 352 TargetTriple.getEnvironment() != Triple::CODE16), 353 In16BitMode(TargetTriple.getArch() == Triple::x86 && 354 TargetTriple.getEnvironment() == Triple::CODE16), 355 DL(computeDataLayout(*this)), TSInfo(DL), 356 InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM), 357 FrameLowering(TargetFrameLowering::StackGrowsDown, getStackAlignment(), 358 is64Bit() ? -8 : -4), 359 JITInfo(hasSSE1()) {} 360 361bool X86Subtarget::enableEarlyIfConversion() const { 362 return hasCMov() && X86EarlyIfConv; 363} 364 365