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