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 "X86TargetMachine.h" 17#include "llvm/IR/Attributes.h" 18#include "llvm/IR/Function.h" 19#include "llvm/IR/GlobalValue.h" 20#include "llvm/Support/CommandLine.h" 21#include "llvm/Support/Debug.h" 22#include "llvm/Support/ErrorHandling.h" 23#include "llvm/Support/Host.h" 24#include "llvm/Support/raw_ostream.h" 25#include "llvm/Target/TargetMachine.h" 26#include "llvm/Target/TargetOptions.h" 27 28#if defined(_MSC_VER) 29#include <intrin.h> 30#endif 31 32using namespace llvm; 33 34#define DEBUG_TYPE "subtarget" 35 36#define GET_SUBTARGETINFO_TARGET_DESC 37#define GET_SUBTARGETINFO_CTOR 38#include "X86GenSubtargetInfo.inc" 39 40// Temporary option to control early if-conversion for x86 while adding machine 41// models. 42static cl::opt<bool> 43X86EarlyIfConv("x86-early-ifcvt", cl::Hidden, 44 cl::desc("Enable early if-conversion on X86")); 45 46 47/// Classify a blockaddress reference for the current subtarget according to how 48/// we should reference it in a non-pcrel context. 49unsigned 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/// Classify a global variable reference for the current subtarget according to 61/// how we should reference it in a non-pcrel context. 62unsigned char X86Subtarget:: 63ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const { 64 // DLLImport only exists on windows, it is implemented as a load from a 65 // DLLIMPORT stub. 66 if (GV->hasDLLImportStorageClass()) 67 return X86II::MO_DLLIMPORT; 68 69 bool isDef = GV->isStrongDefinitionForLinker(); 70 71 // X86-64 in PIC mode. 72 if (isPICStyleRIPRel()) { 73 // Large model never uses stubs. 74 if (TM.getCodeModel() == CodeModel::Large) 75 return X86II::MO_NO_FLAG; 76 77 if (isTargetDarwin()) { 78 // If symbol visibility is hidden, the extra load is not needed if 79 // target is x86-64 or the symbol is definitely defined in the current 80 // translation unit. 81 if (GV->hasDefaultVisibility() && !isDef) 82 return X86II::MO_GOTPCREL; 83 } else if (!isTargetWin64()) { 84 assert(isTargetELF() && "Unknown rip-relative target"); 85 86 // Extra load is needed for all externally visible. 87 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility()) 88 return X86II::MO_GOTPCREL; 89 } 90 91 return X86II::MO_NO_FLAG; 92 } 93 94 if (isPICStyleGOT()) { // 32-bit ELF targets. 95 // Extra load is needed for all externally visible. 96 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility()) 97 return X86II::MO_GOTOFF; 98 return X86II::MO_GOT; 99 } 100 101 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode. 102 // Determine whether we have a stub reference and/or whether the reference 103 // is relative to the PIC base or not. 104 105 // If this is a strong reference to a definition, it is definitely not 106 // through a stub. 107 if (isDef) 108 return X86II::MO_PIC_BASE_OFFSET; 109 110 // Unless we have a symbol with hidden visibility, we have to go through a 111 // normal $non_lazy_ptr stub because this symbol might be resolved late. 112 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference. 113 return X86II::MO_DARWIN_NONLAZY_PIC_BASE; 114 115 // If symbol visibility is hidden, we have a stub for common symbol 116 // references and external declarations. 117 if (GV->isDeclarationForLinker() || GV->hasCommonLinkage()) { 118 // Hidden $non_lazy_ptr reference. 119 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE; 120 } 121 122 // Otherwise, no stub. 123 return X86II::MO_PIC_BASE_OFFSET; 124 } 125 126 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode. 127 // Determine whether we have a stub reference. 128 129 // If this is a strong reference to a definition, it is definitely not 130 // through a stub. 131 if (isDef) 132 return X86II::MO_NO_FLAG; 133 134 // Unless we have a symbol with hidden visibility, we have to go through a 135 // normal $non_lazy_ptr stub because this symbol might be resolved late. 136 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference. 137 return X86II::MO_DARWIN_NONLAZY; 138 139 // Otherwise, no stub. 140 return X86II::MO_NO_FLAG; 141 } 142 143 // Direct static reference to global. 144 return X86II::MO_NO_FLAG; 145} 146 147 148/// This function returns the name of a function which has an interface like 149/// the non-standard bzero function, if such a function exists on the 150/// current subtarget and it is considered preferable over memset with zero 151/// passed as the second argument. Otherwise it returns null. 152const char *X86Subtarget::getBZeroEntry() const { 153 // Darwin 10 has a __bzero entry point for this purpose. 154 if (getTargetTriple().isMacOSX() && 155 !getTargetTriple().isMacOSXVersionLT(10, 6)) 156 return "__bzero"; 157 158 return nullptr; 159} 160 161bool X86Subtarget::hasSinCos() const { 162 return getTargetTriple().isMacOSX() && 163 !getTargetTriple().isMacOSXVersionLT(10, 9) && 164 is64Bit(); 165} 166 167/// Return true if the subtarget allows calls to immediate address. 168bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const { 169 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32 170 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does, 171 // the following check for Win32 should be removed. 172 if (In64BitMode || isTargetWin32()) 173 return false; 174 return isTargetELF() || TM.getRelocationModel() == Reloc::Static; 175} 176 177void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) { 178 std::string CPUName = CPU; 179 if (CPUName.empty()) 180 CPUName = "generic"; 181 182 // Make sure 64-bit features are available in 64-bit mode. (But make sure 183 // SSE2 can be turned off explicitly.) 184 std::string FullFS = FS; 185 if (In64BitMode) { 186 if (!FullFS.empty()) 187 FullFS = "+64bit,+sse2," + FullFS; 188 else 189 FullFS = "+64bit,+sse2"; 190 } 191 192 // LAHF/SAHF are always supported in non-64-bit mode. 193 if (!In64BitMode) { 194 if (!FullFS.empty()) 195 FullFS = "+sahf," + FullFS; 196 else 197 FullFS = "+sahf"; 198 } 199 200 201 // Parse features string and set the CPU. 202 ParseSubtargetFeatures(CPUName, FullFS); 203 204 // All CPUs that implement SSE4.2 or SSE4A support unaligned accesses of 205 // 16-bytes and under that are reasonably fast. These features were 206 // introduced with Intel's Nehalem/Silvermont and AMD's Family10h 207 // micro-architectures respectively. 208 if (hasSSE42() || hasSSE4A()) 209 IsUAMem16Slow = false; 210 211 InstrItins = getInstrItineraryForCPU(CPUName); 212 213 // It's important to keep the MCSubtargetInfo feature bits in sync with 214 // target data structure which is shared with MC code emitter, etc. 215 if (In64BitMode) 216 ToggleFeature(X86::Mode64Bit); 217 else if (In32BitMode) 218 ToggleFeature(X86::Mode32Bit); 219 else if (In16BitMode) 220 ToggleFeature(X86::Mode16Bit); 221 else 222 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!"); 223 224 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel 225 << ", 3DNowLevel " << X863DNowLevel 226 << ", 64bit " << HasX86_64 << "\n"); 227 assert((!In64BitMode || HasX86_64) && 228 "64-bit code requested on a subtarget that doesn't support it!"); 229 230 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both 231 // 32 and 64 bit) and for all 64-bit targets. 232 if (StackAlignOverride) 233 stackAlignment = StackAlignOverride; 234 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() || 235 In64BitMode) 236 stackAlignment = 16; 237} 238 239void X86Subtarget::initializeEnvironment() { 240 X86SSELevel = NoSSE; 241 X863DNowLevel = NoThreeDNow; 242 HasCMov = false; 243 HasX86_64 = false; 244 HasPOPCNT = false; 245 HasSSE4A = false; 246 HasAES = false; 247 HasFXSR = false; 248 HasXSAVE = false; 249 HasXSAVEOPT = false; 250 HasXSAVEC = false; 251 HasXSAVES = false; 252 HasPCLMUL = false; 253 HasFMA = false; 254 HasFMA4 = false; 255 HasXOP = false; 256 HasTBM = false; 257 HasMOVBE = false; 258 HasRDRAND = false; 259 HasF16C = false; 260 HasFSGSBase = false; 261 HasLZCNT = false; 262 HasBMI = false; 263 HasBMI2 = false; 264 HasRTM = false; 265 HasHLE = false; 266 HasERI = false; 267 HasCDI = false; 268 HasPFI = false; 269 HasDQI = false; 270 HasBWI = false; 271 HasVLX = false; 272 HasADX = false; 273 HasPKU = false; 274 HasSHA = false; 275 HasPRFCHW = false; 276 HasRDSEED = false; 277 HasLAHFSAHF = false; 278 HasMPX = false; 279 IsBTMemSlow = false; 280 IsSHLDSlow = false; 281 IsUAMem16Slow = false; 282 IsUAMem32Slow = false; 283 HasSSEUnalignedMem = false; 284 HasCmpxchg16b = false; 285 UseLeaForSP = false; 286 HasSlowDivide32 = false; 287 HasSlowDivide64 = false; 288 PadShortFunctions = false; 289 CallRegIndirect = false; 290 LEAUsesAG = false; 291 SlowLEA = false; 292 SlowIncDec = false; 293 stackAlignment = 4; 294 // FIXME: this is a known good value for Yonah. How about others? 295 MaxInlineSizeThreshold = 128; 296 UseSoftFloat = false; 297} 298 299X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU, 300 StringRef FS) { 301 initializeEnvironment(); 302 initSubtargetFeatures(CPU, FS); 303 return *this; 304} 305 306X86Subtarget::X86Subtarget(const Triple &TT, const std::string &CPU, 307 const std::string &FS, const X86TargetMachine &TM, 308 unsigned StackAlignOverride) 309 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others), 310 PICStyle(PICStyles::None), TargetTriple(TT), 311 StackAlignOverride(StackAlignOverride), 312 In64BitMode(TargetTriple.getArch() == Triple::x86_64), 313 In32BitMode(TargetTriple.getArch() == Triple::x86 && 314 TargetTriple.getEnvironment() != Triple::CODE16), 315 In16BitMode(TargetTriple.getArch() == Triple::x86 && 316 TargetTriple.getEnvironment() == Triple::CODE16), 317 TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, FS)), 318 TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) { 319 // Determine the PICStyle based on the target selected. 320 if (TM.getRelocationModel() == Reloc::Static) { 321 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None. 322 setPICStyle(PICStyles::None); 323 } else if (is64Bit()) { 324 // PIC in 64 bit mode is always rip-rel. 325 setPICStyle(PICStyles::RIPRel); 326 } else if (isTargetCOFF()) { 327 setPICStyle(PICStyles::None); 328 } else if (isTargetDarwin()) { 329 if (TM.getRelocationModel() == Reloc::PIC_) 330 setPICStyle(PICStyles::StubPIC); 331 else { 332 assert(TM.getRelocationModel() == Reloc::DynamicNoPIC); 333 setPICStyle(PICStyles::StubDynamicNoPIC); 334 } 335 } else if (isTargetELF()) { 336 setPICStyle(PICStyles::GOT); 337 } 338} 339 340bool X86Subtarget::enableEarlyIfConversion() const { 341 return hasCMov() && X86EarlyIfConv; 342} 343 344