LLVM API Documentation
00001 //===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the operating system Host concept. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Support/Host.h" 00015 #include "llvm/ADT/SmallVector.h" 00016 #include "llvm/ADT/StringRef.h" 00017 #include "llvm/ADT/StringSwitch.h" 00018 #include "llvm/ADT/Triple.h" 00019 #include "llvm/Config/config.h" 00020 #include "llvm/Support/DataStream.h" 00021 #include "llvm/Support/Debug.h" 00022 #include "llvm/Support/raw_ostream.h" 00023 #include <string.h> 00024 00025 // Include the platform-specific parts of this class. 00026 #ifdef LLVM_ON_UNIX 00027 #include "Unix/Host.inc" 00028 #endif 00029 #ifdef LLVM_ON_WIN32 00030 #include "Windows/Host.inc" 00031 #endif 00032 #ifdef _MSC_VER 00033 #include <intrin.h> 00034 #endif 00035 #if defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__)) 00036 #include <mach/mach.h> 00037 #include <mach/mach_host.h> 00038 #include <mach/host_info.h> 00039 #include <mach/machine.h> 00040 #endif 00041 00042 #define DEBUG_TYPE "host-detection" 00043 00044 //===----------------------------------------------------------------------===// 00045 // 00046 // Implementations of the CPU detection routines 00047 // 00048 //===----------------------------------------------------------------------===// 00049 00050 using namespace llvm; 00051 00052 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\ 00053 || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64) 00054 00055 /// GetX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in the 00056 /// specified arguments. If we can't run cpuid on the host, return true. 00057 static bool GetX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX, 00058 unsigned *rECX, unsigned *rEDX) { 00059 #if defined(__GNUC__) || defined(__clang__) 00060 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64) 00061 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually. 00062 asm ("movq\t%%rbx, %%rsi\n\t" 00063 "cpuid\n\t" 00064 "xchgq\t%%rbx, %%rsi\n\t" 00065 : "=a" (*rEAX), 00066 "=S" (*rEBX), 00067 "=c" (*rECX), 00068 "=d" (*rEDX) 00069 : "a" (value)); 00070 return false; 00071 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86) 00072 asm ("movl\t%%ebx, %%esi\n\t" 00073 "cpuid\n\t" 00074 "xchgl\t%%ebx, %%esi\n\t" 00075 : "=a" (*rEAX), 00076 "=S" (*rEBX), 00077 "=c" (*rECX), 00078 "=d" (*rEDX) 00079 : "a" (value)); 00080 return false; 00081 // pedantic #else returns to appease -Wunreachable-code (so we don't generate 00082 // postprocessed code that looks like "return true; return false;") 00083 #else 00084 return true; 00085 #endif 00086 #elif defined(_MSC_VER) 00087 // The MSVC intrinsic is portable across x86 and x64. 00088 int registers[4]; 00089 __cpuid(registers, value); 00090 *rEAX = registers[0]; 00091 *rEBX = registers[1]; 00092 *rECX = registers[2]; 00093 *rEDX = registers[3]; 00094 return false; 00095 #else 00096 return true; 00097 #endif 00098 } 00099 00100 /// GetX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return the 00101 /// 4 values in the specified arguments. If we can't run cpuid on the host, 00102 /// return true. 00103 static bool GetX86CpuIDAndInfoEx(unsigned value, unsigned subleaf, 00104 unsigned *rEAX, unsigned *rEBX, unsigned *rECX, 00105 unsigned *rEDX) { 00106 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64) 00107 #if defined(__GNUC__) 00108 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually. 00109 asm ("movq\t%%rbx, %%rsi\n\t" 00110 "cpuid\n\t" 00111 "xchgq\t%%rbx, %%rsi\n\t" 00112 : "=a" (*rEAX), 00113 "=S" (*rEBX), 00114 "=c" (*rECX), 00115 "=d" (*rEDX) 00116 : "a" (value), 00117 "c" (subleaf)); 00118 return false; 00119 #elif defined(_MSC_VER) 00120 // __cpuidex was added in MSVC++ 9.0 SP1 00121 #if (_MSC_VER > 1500) || (_MSC_VER == 1500 && _MSC_FULL_VER >= 150030729) 00122 int registers[4]; 00123 __cpuidex(registers, value, subleaf); 00124 *rEAX = registers[0]; 00125 *rEBX = registers[1]; 00126 *rECX = registers[2]; 00127 *rEDX = registers[3]; 00128 return false; 00129 #else 00130 return true; 00131 #endif 00132 #else 00133 return true; 00134 #endif 00135 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86) 00136 #if defined(__GNUC__) 00137 asm ("movl\t%%ebx, %%esi\n\t" 00138 "cpuid\n\t" 00139 "xchgl\t%%ebx, %%esi\n\t" 00140 : "=a" (*rEAX), 00141 "=S" (*rEBX), 00142 "=c" (*rECX), 00143 "=d" (*rEDX) 00144 : "a" (value), 00145 "c" (subleaf)); 00146 return false; 00147 #elif defined(_MSC_VER) 00148 __asm { 00149 mov eax,value 00150 mov ecx,subleaf 00151 cpuid 00152 mov esi,rEAX 00153 mov dword ptr [esi],eax 00154 mov esi,rEBX 00155 mov dword ptr [esi],ebx 00156 mov esi,rECX 00157 mov dword ptr [esi],ecx 00158 mov esi,rEDX 00159 mov dword ptr [esi],edx 00160 } 00161 return false; 00162 #else 00163 return true; 00164 #endif 00165 #else 00166 return true; 00167 #endif 00168 } 00169 00170 static bool OSHasAVXSupport() { 00171 #if defined(__GNUC__) 00172 // Check xgetbv; this uses a .byte sequence instead of the instruction 00173 // directly because older assemblers do not include support for xgetbv and 00174 // there is no easy way to conditionally compile based on the assembler used. 00175 int rEAX, rEDX; 00176 __asm__ (".byte 0x0f, 0x01, 0xd0" : "=a" (rEAX), "=d" (rEDX) : "c" (0)); 00177 #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK) 00178 unsigned long long rEAX = _xgetbv(_XCR_XFEATURE_ENABLED_MASK); 00179 #else 00180 int rEAX = 0; // Ensures we return false 00181 #endif 00182 return (rEAX & 6) == 6; 00183 } 00184 00185 static void DetectX86FamilyModel(unsigned EAX, unsigned &Family, 00186 unsigned &Model) { 00187 Family = (EAX >> 8) & 0xf; // Bits 8 - 11 00188 Model = (EAX >> 4) & 0xf; // Bits 4 - 7 00189 if (Family == 6 || Family == 0xf) { 00190 if (Family == 0xf) 00191 // Examine extended family ID if family ID is F. 00192 Family += (EAX >> 20) & 0xff; // Bits 20 - 27 00193 // Examine extended model ID if family ID is 6 or F. 00194 Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19 00195 } 00196 } 00197 00198 StringRef sys::getHostCPUName() { 00199 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0; 00200 if (GetX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX)) 00201 return "generic"; 00202 unsigned Family = 0; 00203 unsigned Model = 0; 00204 DetectX86FamilyModel(EAX, Family, Model); 00205 00206 union { 00207 unsigned u[3]; 00208 char c[12]; 00209 } text; 00210 00211 GetX86CpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1); 00212 00213 unsigned MaxLeaf = EAX; 00214 bool HasSSE3 = (ECX & 0x1); 00215 bool HasSSE41 = (ECX & 0x80000); 00216 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV 00217 // indicates that the AVX registers will be saved and restored on context 00218 // switch, then we have full AVX support. 00219 const unsigned AVXBits = (1 << 27) | (1 << 28); 00220 bool HasAVX = ((ECX & AVXBits) == AVXBits) && OSHasAVXSupport(); 00221 bool HasAVX2 = HasAVX && MaxLeaf >= 0x7 && 00222 !GetX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX) && 00223 (EBX & 0x20); 00224 GetX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX); 00225 bool Em64T = (EDX >> 29) & 0x1; 00226 bool HasTBM = (ECX >> 21) & 0x1; 00227 00228 if (memcmp(text.c, "GenuineIntel", 12) == 0) { 00229 switch (Family) { 00230 case 3: 00231 return "i386"; 00232 case 4: 00233 switch (Model) { 00234 case 0: // Intel486 DX processors 00235 case 1: // Intel486 DX processors 00236 case 2: // Intel486 SX processors 00237 case 3: // Intel487 processors, IntelDX2 OverDrive processors, 00238 // IntelDX2 processors 00239 case 4: // Intel486 SL processor 00240 case 5: // IntelSX2 processors 00241 case 7: // Write-Back Enhanced IntelDX2 processors 00242 case 8: // IntelDX4 OverDrive processors, IntelDX4 processors 00243 default: return "i486"; 00244 } 00245 case 5: 00246 switch (Model) { 00247 case 1: // Pentium OverDrive processor for Pentium processor (60, 66), 00248 // Pentium processors (60, 66) 00249 case 2: // Pentium OverDrive processor for Pentium processor (75, 90, 00250 // 100, 120, 133), Pentium processors (75, 90, 100, 120, 133, 00251 // 150, 166, 200) 00252 case 3: // Pentium OverDrive processors for Intel486 processor-based 00253 // systems 00254 return "pentium"; 00255 00256 case 4: // Pentium OverDrive processor with MMX technology for Pentium 00257 // processor (75, 90, 100, 120, 133), Pentium processor with 00258 // MMX technology (166, 200) 00259 return "pentium-mmx"; 00260 00261 default: return "pentium"; 00262 } 00263 case 6: 00264 switch (Model) { 00265 case 1: // Pentium Pro processor 00266 return "pentiumpro"; 00267 00268 case 3: // Intel Pentium II OverDrive processor, Pentium II processor, 00269 // model 03 00270 case 5: // Pentium II processor, model 05, Pentium II Xeon processor, 00271 // model 05, and Intel Celeron processor, model 05 00272 case 6: // Celeron processor, model 06 00273 return "pentium2"; 00274 00275 case 7: // Pentium III processor, model 07, and Pentium III Xeon 00276 // processor, model 07 00277 case 8: // Pentium III processor, model 08, Pentium III Xeon processor, 00278 // model 08, and Celeron processor, model 08 00279 case 10: // Pentium III Xeon processor, model 0Ah 00280 case 11: // Pentium III processor, model 0Bh 00281 return "pentium3"; 00282 00283 case 9: // Intel Pentium M processor, Intel Celeron M processor model 09. 00284 case 13: // Intel Pentium M processor, Intel Celeron M processor, model 00285 // 0Dh. All processors are manufactured using the 90 nm process. 00286 return "pentium-m"; 00287 00288 case 14: // Intel Core Duo processor, Intel Core Solo processor, model 00289 // 0Eh. All processors are manufactured using the 65 nm process. 00290 return "yonah"; 00291 00292 case 15: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile 00293 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad 00294 // mobile processor, Intel Core 2 Extreme processor, Intel 00295 // Pentium Dual-Core processor, Intel Xeon processor, model 00296 // 0Fh. All processors are manufactured using the 65 nm process. 00297 case 22: // Intel Celeron processor model 16h. All processors are 00298 // manufactured using the 65 nm process 00299 return "core2"; 00300 00301 case 21: // Intel EP80579 Integrated Processor and Intel EP80579 00302 // Integrated Processor with Intel QuickAssist Technology 00303 return "i686"; // FIXME: ??? 00304 00305 case 23: // Intel Core 2 Extreme processor, Intel Xeon processor, model 00306 // 17h. All processors are manufactured using the 45 nm process. 00307 // 00308 // 45nm: Penryn , Wolfdale, Yorkfield (XE) 00309 // Not all Penryn processors support SSE 4.1 (such as the Pentium brand) 00310 return HasSSE41 ? "penryn" : "core2"; 00311 00312 case 26: // Intel Core i7 processor and Intel Xeon processor. All 00313 // processors are manufactured using the 45 nm process. 00314 case 29: // Intel Xeon processor MP. All processors are manufactured using 00315 // the 45 nm process. 00316 case 30: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz. 00317 // As found in a Summer 2010 model iMac. 00318 case 37: // Intel Core i7, laptop version. 00319 case 44: // Intel Core i7 processor and Intel Xeon processor. All 00320 // processors are manufactured using the 32 nm process. 00321 case 46: // Nehalem EX 00322 case 47: // Westmere EX 00323 return "corei7"; 00324 00325 // SandyBridge: 00326 case 42: // Intel Core i7 processor. All processors are manufactured 00327 // using the 32 nm process. 00328 case 45: 00329 // Not all Sandy Bridge processors support AVX (such as the Pentium 00330 // versions instead of the i7 versions). 00331 return HasAVX ? "corei7-avx" : "corei7"; 00332 00333 // Ivy Bridge: 00334 case 58: 00335 case 62: // Ivy Bridge EP 00336 // Not all Ivy Bridge processors support AVX (such as the Pentium 00337 // versions instead of the i7 versions). 00338 return HasAVX ? "core-avx-i" : "corei7"; 00339 00340 // Haswell: 00341 case 60: 00342 case 63: 00343 case 69: 00344 case 70: 00345 // Not all Haswell processors support AVX too (such as the Pentium 00346 // versions instead of the i7 versions). 00347 return HasAVX2 ? "core-avx2" : "corei7"; 00348 00349 case 28: // Most 45 nm Intel Atom processors 00350 case 38: // 45 nm Atom Lincroft 00351 case 39: // 32 nm Atom Medfield 00352 case 53: // 32 nm Atom Midview 00353 case 54: // 32 nm Atom Midview 00354 return "atom"; 00355 00356 // Atom Silvermont codes from the Intel software optimization guide. 00357 case 55: 00358 case 74: 00359 case 77: 00360 return "slm"; 00361 00362 default: return (Em64T) ? "x86-64" : "i686"; 00363 } 00364 case 15: { 00365 switch (Model) { 00366 case 0: // Pentium 4 processor, Intel Xeon processor. All processors are 00367 // model 00h and manufactured using the 0.18 micron process. 00368 case 1: // Pentium 4 processor, Intel Xeon processor, Intel Xeon 00369 // processor MP, and Intel Celeron processor. All processors are 00370 // model 01h and manufactured using the 0.18 micron process. 00371 case 2: // Pentium 4 processor, Mobile Intel Pentium 4 processor - M, 00372 // Intel Xeon processor, Intel Xeon processor MP, Intel Celeron 00373 // processor, and Mobile Intel Celeron processor. All processors 00374 // are model 02h and manufactured using the 0.13 micron process. 00375 return (Em64T) ? "x86-64" : "pentium4"; 00376 00377 case 3: // Pentium 4 processor, Intel Xeon processor, Intel Celeron D 00378 // processor. All processors are model 03h and manufactured using 00379 // the 90 nm process. 00380 case 4: // Pentium 4 processor, Pentium 4 processor Extreme Edition, 00381 // Pentium D processor, Intel Xeon processor, Intel Xeon 00382 // processor MP, Intel Celeron D processor. All processors are 00383 // model 04h and manufactured using the 90 nm process. 00384 case 6: // Pentium 4 processor, Pentium D processor, Pentium processor 00385 // Extreme Edition, Intel Xeon processor, Intel Xeon processor 00386 // MP, Intel Celeron D processor. All processors are model 06h 00387 // and manufactured using the 65 nm process. 00388 return (Em64T) ? "nocona" : "prescott"; 00389 00390 default: 00391 return (Em64T) ? "x86-64" : "pentium4"; 00392 } 00393 } 00394 00395 default: 00396 return "generic"; 00397 } 00398 } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) { 00399 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There 00400 // appears to be no way to generate the wide variety of AMD-specific targets 00401 // from the information returned from CPUID. 00402 switch (Family) { 00403 case 4: 00404 return "i486"; 00405 case 5: 00406 switch (Model) { 00407 case 6: 00408 case 7: return "k6"; 00409 case 8: return "k6-2"; 00410 case 9: 00411 case 13: return "k6-3"; 00412 case 10: return "geode"; 00413 default: return "pentium"; 00414 } 00415 case 6: 00416 switch (Model) { 00417 case 4: return "athlon-tbird"; 00418 case 6: 00419 case 7: 00420 case 8: return "athlon-mp"; 00421 case 10: return "athlon-xp"; 00422 default: return "athlon"; 00423 } 00424 case 15: 00425 if (HasSSE3) 00426 return "k8-sse3"; 00427 switch (Model) { 00428 case 1: return "opteron"; 00429 case 5: return "athlon-fx"; // also opteron 00430 default: return "athlon64"; 00431 } 00432 case 16: 00433 return "amdfam10"; 00434 case 20: 00435 return "btver1"; 00436 case 21: 00437 if (!HasAVX) // If the OS doesn't support AVX provide a sane fallback. 00438 return "btver1"; 00439 if (Model >= 0x50) 00440 return "bdver4"; // 50h-6Fh: Excavator 00441 if (Model >= 0x30) 00442 return "bdver3"; // 30h-3Fh: Steamroller 00443 if (Model >= 0x10 || HasTBM) 00444 return "bdver2"; // 10h-1Fh: Piledriver 00445 return "bdver1"; // 00h-0Fh: Bulldozer 00446 case 22: 00447 if (!HasAVX) // If the OS doesn't support AVX provide a sane fallback. 00448 return "btver1"; 00449 return "btver2"; 00450 default: 00451 return "generic"; 00452 } 00453 } 00454 return "generic"; 00455 } 00456 #elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__)) 00457 StringRef sys::getHostCPUName() { 00458 host_basic_info_data_t hostInfo; 00459 mach_msg_type_number_t infoCount; 00460 00461 infoCount = HOST_BASIC_INFO_COUNT; 00462 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, 00463 &infoCount); 00464 00465 if (hostInfo.cpu_type != CPU_TYPE_POWERPC) return "generic"; 00466 00467 switch(hostInfo.cpu_subtype) { 00468 case CPU_SUBTYPE_POWERPC_601: return "601"; 00469 case CPU_SUBTYPE_POWERPC_602: return "602"; 00470 case CPU_SUBTYPE_POWERPC_603: return "603"; 00471 case CPU_SUBTYPE_POWERPC_603e: return "603e"; 00472 case CPU_SUBTYPE_POWERPC_603ev: return "603ev"; 00473 case CPU_SUBTYPE_POWERPC_604: return "604"; 00474 case CPU_SUBTYPE_POWERPC_604e: return "604e"; 00475 case CPU_SUBTYPE_POWERPC_620: return "620"; 00476 case CPU_SUBTYPE_POWERPC_750: return "750"; 00477 case CPU_SUBTYPE_POWERPC_7400: return "7400"; 00478 case CPU_SUBTYPE_POWERPC_7450: return "7450"; 00479 case CPU_SUBTYPE_POWERPC_970: return "970"; 00480 default: ; 00481 } 00482 00483 return "generic"; 00484 } 00485 #elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__)) 00486 StringRef sys::getHostCPUName() { 00487 // Access to the Processor Version Register (PVR) on PowerPC is privileged, 00488 // and so we must use an operating-system interface to determine the current 00489 // processor type. On Linux, this is exposed through the /proc/cpuinfo file. 00490 const char *generic = "generic"; 00491 00492 // Note: We cannot mmap /proc/cpuinfo here and then process the resulting 00493 // memory buffer because the 'file' has 0 size (it can be read from only 00494 // as a stream). 00495 00496 std::string Err; 00497 DataStreamer *DS = getDataFileStreamer("/proc/cpuinfo", &Err); 00498 if (!DS) { 00499 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << Err << "\n"); 00500 return generic; 00501 } 00502 00503 // The cpu line is second (after the 'processor: 0' line), so if this 00504 // buffer is too small then something has changed (or is wrong). 00505 char buffer[1024]; 00506 size_t CPUInfoSize = DS->GetBytes((unsigned char*) buffer, sizeof(buffer)); 00507 delete DS; 00508 00509 const char *CPUInfoStart = buffer; 00510 const char *CPUInfoEnd = buffer + CPUInfoSize; 00511 00512 const char *CIP = CPUInfoStart; 00513 00514 const char *CPUStart = 0; 00515 size_t CPULen = 0; 00516 00517 // We need to find the first line which starts with cpu, spaces, and a colon. 00518 // After the colon, there may be some additional spaces and then the cpu type. 00519 while (CIP < CPUInfoEnd && CPUStart == 0) { 00520 if (CIP < CPUInfoEnd && *CIP == '\n') 00521 ++CIP; 00522 00523 if (CIP < CPUInfoEnd && *CIP == 'c') { 00524 ++CIP; 00525 if (CIP < CPUInfoEnd && *CIP == 'p') { 00526 ++CIP; 00527 if (CIP < CPUInfoEnd && *CIP == 'u') { 00528 ++CIP; 00529 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t')) 00530 ++CIP; 00531 00532 if (CIP < CPUInfoEnd && *CIP == ':') { 00533 ++CIP; 00534 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t')) 00535 ++CIP; 00536 00537 if (CIP < CPUInfoEnd) { 00538 CPUStart = CIP; 00539 while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' && 00540 *CIP != ',' && *CIP != '\n')) 00541 ++CIP; 00542 CPULen = CIP - CPUStart; 00543 } 00544 } 00545 } 00546 } 00547 } 00548 00549 if (CPUStart == 0) 00550 while (CIP < CPUInfoEnd && *CIP != '\n') 00551 ++CIP; 00552 } 00553 00554 if (CPUStart == 0) 00555 return generic; 00556 00557 return StringSwitch<const char *>(StringRef(CPUStart, CPULen)) 00558 .Case("604e", "604e") 00559 .Case("604", "604") 00560 .Case("7400", "7400") 00561 .Case("7410", "7400") 00562 .Case("7447", "7400") 00563 .Case("7455", "7450") 00564 .Case("G4", "g4") 00565 .Case("POWER4", "970") 00566 .Case("PPC970FX", "970") 00567 .Case("PPC970MP", "970") 00568 .Case("G5", "g5") 00569 .Case("POWER5", "g5") 00570 .Case("A2", "a2") 00571 .Case("POWER6", "pwr6") 00572 .Case("POWER7", "pwr7") 00573 .Case("POWER8", "pwr8") 00574 .Case("POWER8E", "pwr8") 00575 .Default(generic); 00576 } 00577 #elif defined(__linux__) && defined(__arm__) 00578 StringRef sys::getHostCPUName() { 00579 // The cpuid register on arm is not accessible from user space. On Linux, 00580 // it is exposed through the /proc/cpuinfo file. 00581 // Note: We cannot mmap /proc/cpuinfo here and then process the resulting 00582 // memory buffer because the 'file' has 0 size (it can be read from only 00583 // as a stream). 00584 00585 std::string Err; 00586 DataStreamer *DS = getDataFileStreamer("/proc/cpuinfo", &Err); 00587 if (!DS) { 00588 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << Err << "\n"); 00589 return "generic"; 00590 } 00591 00592 // Read 1024 bytes from /proc/cpuinfo, which should contain the CPU part line 00593 // in all cases. 00594 char buffer[1024]; 00595 size_t CPUInfoSize = DS->GetBytes((unsigned char*) buffer, sizeof(buffer)); 00596 delete DS; 00597 00598 StringRef Str(buffer, CPUInfoSize); 00599 00600 SmallVector<StringRef, 32> Lines; 00601 Str.split(Lines, "\n"); 00602 00603 // Look for the CPU implementer line. 00604 StringRef Implementer; 00605 for (unsigned I = 0, E = Lines.size(); I != E; ++I) 00606 if (Lines[I].startswith("CPU implementer")) 00607 Implementer = Lines[I].substr(15).ltrim("\t :"); 00608 00609 if (Implementer == "0x41") // ARM Ltd. 00610 // Look for the CPU part line. 00611 for (unsigned I = 0, E = Lines.size(); I != E; ++I) 00612 if (Lines[I].startswith("CPU part")) 00613 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The 00614 // values correspond to the "Part number" in the CP15/c0 register. The 00615 // contents are specified in the various processor manuals. 00616 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :")) 00617 .Case("0x926", "arm926ej-s") 00618 .Case("0xb02", "mpcore") 00619 .Case("0xb36", "arm1136j-s") 00620 .Case("0xb56", "arm1156t2-s") 00621 .Case("0xb76", "arm1176jz-s") 00622 .Case("0xc08", "cortex-a8") 00623 .Case("0xc09", "cortex-a9") 00624 .Case("0xc0f", "cortex-a15") 00625 .Case("0xc20", "cortex-m0") 00626 .Case("0xc23", "cortex-m3") 00627 .Case("0xc24", "cortex-m4") 00628 .Default("generic"); 00629 00630 if (Implementer == "0x51") // Qualcomm Technologies, Inc. 00631 // Look for the CPU part line. 00632 for (unsigned I = 0, E = Lines.size(); I != E; ++I) 00633 if (Lines[I].startswith("CPU part")) 00634 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The 00635 // values correspond to the "Part number" in the CP15/c0 register. The 00636 // contents are specified in the various processor manuals. 00637 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :")) 00638 .Case("0x06f", "krait") // APQ8064 00639 .Default("generic"); 00640 00641 return "generic"; 00642 } 00643 #elif defined(__linux__) && defined(__s390x__) 00644 StringRef sys::getHostCPUName() { 00645 // STIDP is a privileged operation, so use /proc/cpuinfo instead. 00646 // Note: We cannot mmap /proc/cpuinfo here and then process the resulting 00647 // memory buffer because the 'file' has 0 size (it can be read from only 00648 // as a stream). 00649 00650 std::string Err; 00651 DataStreamer *DS = getDataFileStreamer("/proc/cpuinfo", &Err); 00652 if (!DS) { 00653 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << Err << "\n"); 00654 return "generic"; 00655 } 00656 00657 // The "processor 0:" line comes after a fair amount of other information, 00658 // including a cache breakdown, but this should be plenty. 00659 char buffer[2048]; 00660 size_t CPUInfoSize = DS->GetBytes((unsigned char*) buffer, sizeof(buffer)); 00661 delete DS; 00662 00663 StringRef Str(buffer, CPUInfoSize); 00664 SmallVector<StringRef, 32> Lines; 00665 Str.split(Lines, "\n"); 00666 for (unsigned I = 0, E = Lines.size(); I != E; ++I) { 00667 if (Lines[I].startswith("processor ")) { 00668 size_t Pos = Lines[I].find("machine = "); 00669 if (Pos != StringRef::npos) { 00670 Pos += sizeof("machine = ") - 1; 00671 unsigned int Id; 00672 if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) { 00673 if (Id >= 2827) 00674 return "zEC12"; 00675 if (Id >= 2817) 00676 return "z196"; 00677 } 00678 } 00679 break; 00680 } 00681 } 00682 00683 return "generic"; 00684 } 00685 #else 00686 StringRef sys::getHostCPUName() { 00687 return "generic"; 00688 } 00689 #endif 00690 00691 #if defined(__linux__) && (defined(__arm__) || defined(__aarch64__)) 00692 bool sys::getHostCPUFeatures(StringMap<bool> &Features) { 00693 std::string Err; 00694 DataStreamer *DS = getDataFileStreamer("/proc/cpuinfo", &Err); 00695 if (!DS) { 00696 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << Err << "\n"); 00697 return false; 00698 } 00699 00700 // Read 1024 bytes from /proc/cpuinfo, which should contain the Features line 00701 // in all cases. 00702 char buffer[1024]; 00703 size_t CPUInfoSize = DS->GetBytes((unsigned char*) buffer, sizeof(buffer)); 00704 delete DS; 00705 00706 StringRef Str(buffer, CPUInfoSize); 00707 00708 SmallVector<StringRef, 32> Lines; 00709 Str.split(Lines, "\n"); 00710 00711 SmallVector<StringRef, 32> CPUFeatures; 00712 00713 // Look for the CPU features. 00714 for (unsigned I = 0, E = Lines.size(); I != E; ++I) 00715 if (Lines[I].startswith("Features")) { 00716 Lines[I].split(CPUFeatures, " "); 00717 break; 00718 } 00719 00720 #if defined(__aarch64__) 00721 // Keep track of which crypto features we have seen 00722 enum { 00723 CAP_AES = 0x1, 00724 CAP_PMULL = 0x2, 00725 CAP_SHA1 = 0x4, 00726 CAP_SHA2 = 0x8 00727 }; 00728 uint32_t crypto = 0; 00729 #endif 00730 00731 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) { 00732 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I]) 00733 #if defined(__aarch64__) 00734 .Case("asimd", "neon") 00735 .Case("fp", "fp-armv8") 00736 .Case("crc32", "crc") 00737 #else 00738 .Case("half", "fp16") 00739 .Case("neon", "neon") 00740 .Case("vfpv3", "vfp3") 00741 .Case("vfpv3d16", "d16") 00742 .Case("vfpv4", "vfp4") 00743 .Case("idiva", "hwdiv-arm") 00744 .Case("idivt", "hwdiv") 00745 #endif 00746 .Default(""); 00747 00748 #if defined(__aarch64__) 00749 // We need to check crypto separately since we need all of the crypto 00750 // extensions to enable the subtarget feature 00751 if (CPUFeatures[I] == "aes") 00752 crypto |= CAP_AES; 00753 else if (CPUFeatures[I] == "pmull") 00754 crypto |= CAP_PMULL; 00755 else if (CPUFeatures[I] == "sha1") 00756 crypto |= CAP_SHA1; 00757 else if (CPUFeatures[I] == "sha2") 00758 crypto |= CAP_SHA2; 00759 #endif 00760 00761 if (LLVMFeatureStr != "") 00762 Features.GetOrCreateValue(LLVMFeatureStr).setValue(true); 00763 } 00764 00765 #if defined(__aarch64__) 00766 // If we have all crypto bits we can add the feature 00767 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2)) 00768 Features.GetOrCreateValue("crypto").setValue(true); 00769 #endif 00770 00771 return true; 00772 } 00773 #else 00774 bool sys::getHostCPUFeatures(StringMap<bool> &Features){ 00775 return false; 00776 } 00777 #endif 00778 00779 std::string sys::getProcessTriple() { 00780 Triple PT(Triple::normalize(LLVM_HOST_TRIPLE)); 00781 00782 if (sizeof(void *) == 8 && PT.isArch32Bit()) 00783 PT = PT.get64BitArchVariant(); 00784 if (sizeof(void *) == 4 && PT.isArch64Bit()) 00785 PT = PT.get32BitArchVariant(); 00786 00787 return PT.str(); 00788 }