Host.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. //===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===//
  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 operating system Host concept.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/Host.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/ADT/StringSwitch.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/Config/config.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/FileSystem.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include <string.h>
  23. // Include the platform-specific parts of this class.
  24. #ifdef LLVM_ON_UNIX
  25. #include "Unix/Host.inc"
  26. #endif
  27. #ifdef LLVM_ON_WIN32
  28. #include "Windows/Host.inc"
  29. #endif
  30. #ifdef _MSC_VER
  31. #include <intrin.h>
  32. #endif
  33. #if defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
  34. #include <mach/mach.h>
  35. #include <mach/mach_host.h>
  36. #include <mach/host_info.h>
  37. #include <mach/machine.h>
  38. #endif
  39. #define DEBUG_TYPE "host-detection"
  40. //===----------------------------------------------------------------------===//
  41. //
  42. // Implementations of the CPU detection routines
  43. //
  44. //===----------------------------------------------------------------------===//
  45. using namespace llvm;
  46. #if defined(__linux__)
  47. static ssize_t LLVM_ATTRIBUTE_UNUSED readCpuInfo(void *Buf, size_t Size) {
  48. // Note: We cannot mmap /proc/cpuinfo here and then process the resulting
  49. // memory buffer because the 'file' has 0 size (it can be read from only
  50. // as a stream).
  51. int FD;
  52. std::error_code EC = sys::fs::openFileForRead("/proc/cpuinfo", FD);
  53. if (EC) {
  54. DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << EC.message() << "\n");
  55. return -1;
  56. }
  57. int Ret = read(FD, Buf, Size);
  58. int CloseStatus = close(FD);
  59. if (CloseStatus)
  60. return -1;
  61. return Ret;
  62. }
  63. #endif
  64. #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
  65. || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
  66. /// GetX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
  67. /// specified arguments. If we can't run cpuid on the host, return true.
  68. static bool GetX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
  69. unsigned *rECX, unsigned *rEDX) {
  70. #if defined(__GNUC__) || defined(__clang__)
  71. #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
  72. // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
  73. asm ("movq\t%%rbx, %%rsi\n\t"
  74. "cpuid\n\t"
  75. "xchgq\t%%rbx, %%rsi\n\t"
  76. : "=a" (*rEAX),
  77. "=S" (*rEBX),
  78. "=c" (*rECX),
  79. "=d" (*rEDX)
  80. : "a" (value));
  81. return false;
  82. #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
  83. asm ("movl\t%%ebx, %%esi\n\t"
  84. "cpuid\n\t"
  85. "xchgl\t%%ebx, %%esi\n\t"
  86. : "=a" (*rEAX),
  87. "=S" (*rEBX),
  88. "=c" (*rECX),
  89. "=d" (*rEDX)
  90. : "a" (value));
  91. return false;
  92. // pedantic #else returns to appease -Wunreachable-code (so we don't generate
  93. // postprocessed code that looks like "return true; return false;")
  94. #else
  95. return true;
  96. #endif
  97. #elif defined(_MSC_VER)
  98. // The MSVC intrinsic is portable across x86 and x64.
  99. int registers[4];
  100. __cpuid(registers, value);
  101. *rEAX = registers[0];
  102. *rEBX = registers[1];
  103. *rECX = registers[2];
  104. *rEDX = registers[3];
  105. return false;
  106. #else
  107. return true;
  108. #endif
  109. }
  110. /// GetX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return the
  111. /// 4 values in the specified arguments. If we can't run cpuid on the host,
  112. /// return true.
  113. static bool GetX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
  114. unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
  115. unsigned *rEDX) {
  116. #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
  117. #if defined(__GNUC__)
  118. // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
  119. asm ("movq\t%%rbx, %%rsi\n\t"
  120. "cpuid\n\t"
  121. "xchgq\t%%rbx, %%rsi\n\t"
  122. : "=a" (*rEAX),
  123. "=S" (*rEBX),
  124. "=c" (*rECX),
  125. "=d" (*rEDX)
  126. : "a" (value),
  127. "c" (subleaf));
  128. return false;
  129. #elif defined(_MSC_VER)
  130. int registers[4];
  131. __cpuidex(registers, value, subleaf);
  132. *rEAX = registers[0];
  133. *rEBX = registers[1];
  134. *rECX = registers[2];
  135. *rEDX = registers[3];
  136. return false;
  137. #else
  138. return true;
  139. #endif
  140. #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
  141. #if defined(__GNUC__)
  142. asm ("movl\t%%ebx, %%esi\n\t"
  143. "cpuid\n\t"
  144. "xchgl\t%%ebx, %%esi\n\t"
  145. : "=a" (*rEAX),
  146. "=S" (*rEBX),
  147. "=c" (*rECX),
  148. "=d" (*rEDX)
  149. : "a" (value),
  150. "c" (subleaf));
  151. return false;
  152. #elif defined(_MSC_VER)
  153. __asm {
  154. mov eax,value
  155. mov ecx,subleaf
  156. cpuid
  157. mov esi,rEAX
  158. mov dword ptr [esi],eax
  159. mov esi,rEBX
  160. mov dword ptr [esi],ebx
  161. mov esi,rECX
  162. mov dword ptr [esi],ecx
  163. mov esi,rEDX
  164. mov dword ptr [esi],edx
  165. }
  166. return false;
  167. #else
  168. return true;
  169. #endif
  170. #else
  171. return true;
  172. #endif
  173. }
  174. static bool GetX86XCR0(unsigned *rEAX, unsigned *rEDX) {
  175. #if defined(__GNUC__)
  176. // Check xgetbv; this uses a .byte sequence instead of the instruction
  177. // directly because older assemblers do not include support for xgetbv and
  178. // there is no easy way to conditionally compile based on the assembler used.
  179. __asm__ (".byte 0x0f, 0x01, 0xd0" : "=a" (*rEAX), "=d" (*rEDX) : "c" (0));
  180. return false;
  181. #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK) && !defined(_M_ARM64EC)
  182. unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
  183. *rEAX = Result;
  184. *rEDX = Result >> 32;
  185. return false;
  186. #else
  187. return true;
  188. #endif
  189. }
  190. static void DetectX86FamilyModel(unsigned EAX, unsigned &Family,
  191. unsigned &Model) {
  192. Family = (EAX >> 8) & 0xf; // Bits 8 - 11
  193. Model = (EAX >> 4) & 0xf; // Bits 4 - 7
  194. if (Family == 6 || Family == 0xf) {
  195. if (Family == 0xf)
  196. // Examine extended family ID if family ID is F.
  197. Family += (EAX >> 20) & 0xff; // Bits 20 - 27
  198. // Examine extended model ID if family ID is 6 or F.
  199. Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
  200. }
  201. }
  202. StringRef sys::getHostCPUName() {
  203. unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
  204. if (GetX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
  205. return "generic";
  206. unsigned Family = 0;
  207. unsigned Model = 0;
  208. DetectX86FamilyModel(EAX, Family, Model);
  209. union {
  210. unsigned u[3];
  211. char c[12];
  212. } text;
  213. unsigned MaxLeaf;
  214. GetX86CpuIDAndInfo(0, &MaxLeaf, text.u+0, text.u+2, text.u+1);
  215. bool HasMMX = (EDX >> 23) & 1;
  216. bool HasSSE = (EDX >> 25) & 1;
  217. bool HasSSE2 = (EDX >> 26) & 1;
  218. bool HasSSE3 = (ECX >> 0) & 1;
  219. bool HasSSSE3 = (ECX >> 9) & 1;
  220. bool HasSSE41 = (ECX >> 19) & 1;
  221. bool HasSSE42 = (ECX >> 20) & 1;
  222. bool HasMOVBE = (ECX >> 22) & 1;
  223. // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
  224. // indicates that the AVX registers will be saved and restored on context
  225. // switch, then we have full AVX support.
  226. const unsigned AVXBits = (1 << 27) | (1 << 28);
  227. bool HasAVX = ((ECX & AVXBits) == AVXBits) && !GetX86XCR0(&EAX, &EDX) &&
  228. ((EAX & 0x6) == 0x6);
  229. bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
  230. bool HasLeaf7 = MaxLeaf >= 0x7 &&
  231. !GetX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
  232. bool HasADX = HasLeaf7 && ((EBX >> 19) & 1);
  233. bool HasAVX2 = HasAVX && HasLeaf7 && (EBX & 0x20);
  234. bool HasAVX512 = HasLeaf7 && HasAVX512Save && ((EBX >> 16) & 1);
  235. GetX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
  236. bool Em64T = (EDX >> 29) & 0x1;
  237. bool HasTBM = (ECX >> 21) & 0x1;
  238. if (memcmp(text.c, "GenuineIntel", 12) == 0) {
  239. switch (Family) {
  240. case 3:
  241. return "i386";
  242. case 4:
  243. switch (Model) {
  244. case 0: // Intel486 DX processors
  245. case 1: // Intel486 DX processors
  246. case 2: // Intel486 SX processors
  247. case 3: // Intel487 processors, IntelDX2 OverDrive processors,
  248. // IntelDX2 processors
  249. case 4: // Intel486 SL processor
  250. case 5: // IntelSX2 processors
  251. case 7: // Write-Back Enhanced IntelDX2 processors
  252. case 8: // IntelDX4 OverDrive processors, IntelDX4 processors
  253. default: return "i486";
  254. }
  255. case 5:
  256. switch (Model) {
  257. case 1: // Pentium OverDrive processor for Pentium processor (60, 66),
  258. // Pentium processors (60, 66)
  259. case 2: // Pentium OverDrive processor for Pentium processor (75, 90,
  260. // 100, 120, 133), Pentium processors (75, 90, 100, 120, 133,
  261. // 150, 166, 200)
  262. case 3: // Pentium OverDrive processors for Intel486 processor-based
  263. // systems
  264. return "pentium";
  265. case 4: // Pentium OverDrive processor with MMX technology for Pentium
  266. // processor (75, 90, 100, 120, 133), Pentium processor with
  267. // MMX technology (166, 200)
  268. return "pentium-mmx";
  269. default: return "pentium";
  270. }
  271. case 6:
  272. switch (Model) {
  273. case 1: // Pentium Pro processor
  274. return "pentiumpro";
  275. case 3: // Intel Pentium II OverDrive processor, Pentium II processor,
  276. // model 03
  277. case 5: // Pentium II processor, model 05, Pentium II Xeon processor,
  278. // model 05, and Intel Celeron processor, model 05
  279. case 6: // Celeron processor, model 06
  280. return "pentium2";
  281. case 7: // Pentium III processor, model 07, and Pentium III Xeon
  282. // processor, model 07
  283. case 8: // Pentium III processor, model 08, Pentium III Xeon processor,
  284. // model 08, and Celeron processor, model 08
  285. case 10: // Pentium III Xeon processor, model 0Ah
  286. case 11: // Pentium III processor, model 0Bh
  287. return "pentium3";
  288. case 9: // Intel Pentium M processor, Intel Celeron M processor model 09.
  289. case 13: // Intel Pentium M processor, Intel Celeron M processor, model
  290. // 0Dh. All processors are manufactured using the 90 nm process.
  291. case 21: // Intel EP80579 Integrated Processor and Intel EP80579
  292. // Integrated Processor with Intel QuickAssist Technology
  293. return "pentium-m";
  294. case 14: // Intel Core Duo processor, Intel Core Solo processor, model
  295. // 0Eh. All processors are manufactured using the 65 nm process.
  296. return "yonah";
  297. case 15: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
  298. // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
  299. // mobile processor, Intel Core 2 Extreme processor, Intel
  300. // Pentium Dual-Core processor, Intel Xeon processor, model
  301. // 0Fh. All processors are manufactured using the 65 nm process.
  302. case 22: // Intel Celeron processor model 16h. All processors are
  303. // manufactured using the 65 nm process
  304. return "core2";
  305. case 23: // Intel Core 2 Extreme processor, Intel Xeon processor, model
  306. // 17h. All processors are manufactured using the 45 nm process.
  307. //
  308. // 45nm: Penryn , Wolfdale, Yorkfield (XE)
  309. case 29: // Intel Xeon processor MP. All processors are manufactured using
  310. // the 45 nm process.
  311. return "penryn";
  312. case 26: // Intel Core i7 processor and Intel Xeon processor. All
  313. // processors are manufactured using the 45 nm process.
  314. case 30: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
  315. // As found in a Summer 2010 model iMac.
  316. case 46: // Nehalem EX
  317. return "nehalem";
  318. case 37: // Intel Core i7, laptop version.
  319. case 44: // Intel Core i7 processor and Intel Xeon processor. All
  320. // processors are manufactured using the 32 nm process.
  321. case 47: // Westmere EX
  322. return "westmere";
  323. // SandyBridge:
  324. case 42: // Intel Core i7 processor. All processors are manufactured
  325. // using the 32 nm process.
  326. case 45:
  327. return "sandybridge";
  328. // Ivy Bridge:
  329. case 58:
  330. case 62: // Ivy Bridge EP
  331. return "ivybridge";
  332. // Haswell:
  333. case 60:
  334. case 63:
  335. case 69:
  336. case 70:
  337. return "haswell";
  338. // Broadwell:
  339. case 61:
  340. return "broadwell";
  341. case 28: // Most 45 nm Intel Atom processors
  342. case 38: // 45 nm Atom Lincroft
  343. case 39: // 32 nm Atom Medfield
  344. case 53: // 32 nm Atom Midview
  345. case 54: // 32 nm Atom Midview
  346. return "bonnell";
  347. // Atom Silvermont codes from the Intel software optimization guide.
  348. case 55:
  349. case 74:
  350. case 77:
  351. return "silvermont";
  352. default: // Unknown family 6 CPU, try to guess.
  353. if (HasAVX512)
  354. return "knl";
  355. if (HasADX)
  356. return "broadwell";
  357. if (HasAVX2)
  358. return "haswell";
  359. if (HasAVX)
  360. return "sandybridge";
  361. if (HasSSE42)
  362. return HasMOVBE ? "silvermont" : "nehalem";
  363. if (HasSSE41)
  364. return "penryn";
  365. if (HasSSSE3)
  366. return HasMOVBE ? "bonnell" : "core2";
  367. if (Em64T)
  368. return "x86-64";
  369. if (HasSSE2)
  370. return "pentium-m";
  371. if (HasSSE)
  372. return "pentium3";
  373. if (HasMMX)
  374. return "pentium2";
  375. return "pentiumpro";
  376. }
  377. case 15: {
  378. switch (Model) {
  379. case 0: // Pentium 4 processor, Intel Xeon processor. All processors are
  380. // model 00h and manufactured using the 0.18 micron process.
  381. case 1: // Pentium 4 processor, Intel Xeon processor, Intel Xeon
  382. // processor MP, and Intel Celeron processor. All processors are
  383. // model 01h and manufactured using the 0.18 micron process.
  384. case 2: // Pentium 4 processor, Mobile Intel Pentium 4 processor - M,
  385. // Intel Xeon processor, Intel Xeon processor MP, Intel Celeron
  386. // processor, and Mobile Intel Celeron processor. All processors
  387. // are model 02h and manufactured using the 0.13 micron process.
  388. return (Em64T) ? "x86-64" : "pentium4";
  389. case 3: // Pentium 4 processor, Intel Xeon processor, Intel Celeron D
  390. // processor. All processors are model 03h and manufactured using
  391. // the 90 nm process.
  392. case 4: // Pentium 4 processor, Pentium 4 processor Extreme Edition,
  393. // Pentium D processor, Intel Xeon processor, Intel Xeon
  394. // processor MP, Intel Celeron D processor. All processors are
  395. // model 04h and manufactured using the 90 nm process.
  396. case 6: // Pentium 4 processor, Pentium D processor, Pentium processor
  397. // Extreme Edition, Intel Xeon processor, Intel Xeon processor
  398. // MP, Intel Celeron D processor. All processors are model 06h
  399. // and manufactured using the 65 nm process.
  400. return (Em64T) ? "nocona" : "prescott";
  401. default:
  402. return (Em64T) ? "x86-64" : "pentium4";
  403. }
  404. }
  405. default:
  406. return "generic";
  407. }
  408. } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
  409. // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
  410. // appears to be no way to generate the wide variety of AMD-specific targets
  411. // from the information returned from CPUID.
  412. switch (Family) {
  413. case 4:
  414. return "i486";
  415. case 5:
  416. switch (Model) {
  417. case 6:
  418. case 7: return "k6";
  419. case 8: return "k6-2";
  420. case 9:
  421. case 13: return "k6-3";
  422. case 10: return "geode";
  423. default: return "pentium";
  424. }
  425. case 6:
  426. switch (Model) {
  427. case 4: return "athlon-tbird";
  428. case 6:
  429. case 7:
  430. case 8: return "athlon-mp";
  431. case 10: return "athlon-xp";
  432. default: return "athlon";
  433. }
  434. case 15:
  435. if (HasSSE3)
  436. return "k8-sse3";
  437. switch (Model) {
  438. case 1: return "opteron";
  439. case 5: return "athlon-fx"; // also opteron
  440. default: return "athlon64";
  441. }
  442. case 16:
  443. return "amdfam10";
  444. case 20:
  445. return "btver1";
  446. case 21:
  447. if (!HasAVX) // If the OS doesn't support AVX provide a sane fallback.
  448. return "btver1";
  449. if (Model >= 0x50)
  450. return "bdver4"; // 50h-6Fh: Excavator
  451. if (Model >= 0x30)
  452. return "bdver3"; // 30h-3Fh: Steamroller
  453. if (Model >= 0x10 || HasTBM)
  454. return "bdver2"; // 10h-1Fh: Piledriver
  455. return "bdver1"; // 00h-0Fh: Bulldozer
  456. case 22:
  457. if (!HasAVX) // If the OS doesn't support AVX provide a sane fallback.
  458. return "btver1";
  459. return "btver2";
  460. default:
  461. return "generic";
  462. }
  463. }
  464. return "generic";
  465. }
  466. #elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
  467. StringRef sys::getHostCPUName() {
  468. host_basic_info_data_t hostInfo;
  469. mach_msg_type_number_t infoCount;
  470. infoCount = HOST_BASIC_INFO_COUNT;
  471. host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
  472. &infoCount);
  473. if (hostInfo.cpu_type != CPU_TYPE_POWERPC) return "generic";
  474. switch(hostInfo.cpu_subtype) {
  475. case CPU_SUBTYPE_POWERPC_601: return "601";
  476. case CPU_SUBTYPE_POWERPC_602: return "602";
  477. case CPU_SUBTYPE_POWERPC_603: return "603";
  478. case CPU_SUBTYPE_POWERPC_603e: return "603e";
  479. case CPU_SUBTYPE_POWERPC_603ev: return "603ev";
  480. case CPU_SUBTYPE_POWERPC_604: return "604";
  481. case CPU_SUBTYPE_POWERPC_604e: return "604e";
  482. case CPU_SUBTYPE_POWERPC_620: return "620";
  483. case CPU_SUBTYPE_POWERPC_750: return "750";
  484. case CPU_SUBTYPE_POWERPC_7400: return "7400";
  485. case CPU_SUBTYPE_POWERPC_7450: return "7450";
  486. case CPU_SUBTYPE_POWERPC_970: return "970";
  487. default: ;
  488. }
  489. return "generic";
  490. }
  491. #elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
  492. StringRef sys::getHostCPUName() {
  493. // Access to the Processor Version Register (PVR) on PowerPC is privileged,
  494. // and so we must use an operating-system interface to determine the current
  495. // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
  496. const char *generic = "generic";
  497. // The cpu line is second (after the 'processor: 0' line), so if this
  498. // buffer is too small then something has changed (or is wrong).
  499. char buffer[1024];
  500. ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
  501. if (CPUInfoSize == -1)
  502. return generic;
  503. const char *CPUInfoStart = buffer;
  504. const char *CPUInfoEnd = buffer + CPUInfoSize;
  505. const char *CIP = CPUInfoStart;
  506. const char *CPUStart = 0;
  507. size_t CPULen = 0;
  508. // We need to find the first line which starts with cpu, spaces, and a colon.
  509. // After the colon, there may be some additional spaces and then the cpu type.
  510. while (CIP < CPUInfoEnd && CPUStart == 0) {
  511. if (CIP < CPUInfoEnd && *CIP == '\n')
  512. ++CIP;
  513. if (CIP < CPUInfoEnd && *CIP == 'c') {
  514. ++CIP;
  515. if (CIP < CPUInfoEnd && *CIP == 'p') {
  516. ++CIP;
  517. if (CIP < CPUInfoEnd && *CIP == 'u') {
  518. ++CIP;
  519. while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
  520. ++CIP;
  521. if (CIP < CPUInfoEnd && *CIP == ':') {
  522. ++CIP;
  523. while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
  524. ++CIP;
  525. if (CIP < CPUInfoEnd) {
  526. CPUStart = CIP;
  527. while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
  528. *CIP != ',' && *CIP != '\n'))
  529. ++CIP;
  530. CPULen = CIP - CPUStart;
  531. }
  532. }
  533. }
  534. }
  535. }
  536. if (CPUStart == 0)
  537. while (CIP < CPUInfoEnd && *CIP != '\n')
  538. ++CIP;
  539. }
  540. if (CPUStart == 0)
  541. return generic;
  542. return StringSwitch<const char *>(StringRef(CPUStart, CPULen))
  543. .Case("604e", "604e")
  544. .Case("604", "604")
  545. .Case("7400", "7400")
  546. .Case("7410", "7400")
  547. .Case("7447", "7400")
  548. .Case("7455", "7450")
  549. .Case("G4", "g4")
  550. .Case("POWER4", "970")
  551. .Case("PPC970FX", "970")
  552. .Case("PPC970MP", "970")
  553. .Case("G5", "g5")
  554. .Case("POWER5", "g5")
  555. .Case("A2", "a2")
  556. .Case("POWER6", "pwr6")
  557. .Case("POWER7", "pwr7")
  558. .Case("POWER8", "pwr8")
  559. .Case("POWER8E", "pwr8")
  560. .Default(generic);
  561. }
  562. #elif defined(__linux__) && defined(__arm__)
  563. StringRef sys::getHostCPUName() {
  564. // The cpuid register on arm is not accessible from user space. On Linux,
  565. // it is exposed through the /proc/cpuinfo file.
  566. // Read 1024 bytes from /proc/cpuinfo, which should contain the CPU part line
  567. // in all cases.
  568. char buffer[1024];
  569. ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
  570. if (CPUInfoSize == -1)
  571. return "generic";
  572. StringRef Str(buffer, CPUInfoSize);
  573. SmallVector<StringRef, 32> Lines;
  574. Str.split(Lines, "\n");
  575. // Look for the CPU implementer line.
  576. StringRef Implementer;
  577. for (unsigned I = 0, E = Lines.size(); I != E; ++I)
  578. if (Lines[I].startswith("CPU implementer"))
  579. Implementer = Lines[I].substr(15).ltrim("\t :");
  580. if (Implementer == "0x41") // ARM Ltd.
  581. // Look for the CPU part line.
  582. for (unsigned I = 0, E = Lines.size(); I != E; ++I)
  583. if (Lines[I].startswith("CPU part"))
  584. // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
  585. // values correspond to the "Part number" in the CP15/c0 register. The
  586. // contents are specified in the various processor manuals.
  587. return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
  588. .Case("0x926", "arm926ej-s")
  589. .Case("0xb02", "mpcore")
  590. .Case("0xb36", "arm1136j-s")
  591. .Case("0xb56", "arm1156t2-s")
  592. .Case("0xb76", "arm1176jz-s")
  593. .Case("0xc08", "cortex-a8")
  594. .Case("0xc09", "cortex-a9")
  595. .Case("0xc0f", "cortex-a15")
  596. .Case("0xc20", "cortex-m0")
  597. .Case("0xc23", "cortex-m3")
  598. .Case("0xc24", "cortex-m4")
  599. .Default("generic");
  600. if (Implementer == "0x51") // Qualcomm Technologies, Inc.
  601. // Look for the CPU part line.
  602. for (unsigned I = 0, E = Lines.size(); I != E; ++I)
  603. if (Lines[I].startswith("CPU part"))
  604. // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
  605. // values correspond to the "Part number" in the CP15/c0 register. The
  606. // contents are specified in the various processor manuals.
  607. return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
  608. .Case("0x06f", "krait") // APQ8064
  609. .Default("generic");
  610. return "generic";
  611. }
  612. #elif defined(__linux__) && defined(__s390x__)
  613. StringRef sys::getHostCPUName() {
  614. // STIDP is a privileged operation, so use /proc/cpuinfo instead.
  615. // The "processor 0:" line comes after a fair amount of other information,
  616. // including a cache breakdown, but this should be plenty.
  617. char buffer[2048];
  618. ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
  619. if (CPUInfoSize == -1)
  620. return "generic";
  621. StringRef Str(buffer, CPUInfoSize);
  622. SmallVector<StringRef, 32> Lines;
  623. Str.split(Lines, "\n");
  624. // Look for the CPU features.
  625. SmallVector<StringRef, 32> CPUFeatures;
  626. for (unsigned I = 0, E = Lines.size(); I != E; ++I)
  627. if (Lines[I].startswith("features")) {
  628. size_t Pos = Lines[I].find(":");
  629. if (Pos != StringRef::npos) {
  630. Lines[I].drop_front(Pos + 1).split(CPUFeatures, " ");
  631. break;
  632. }
  633. }
  634. // We need to check for the presence of vector support independently of
  635. // the machine type, since we may only use the vector register set when
  636. // supported by the kernel (and hypervisor).
  637. bool HaveVectorSupport = false;
  638. for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
  639. if (CPUFeatures[I] == "vx")
  640. HaveVectorSupport = true;
  641. }
  642. // Now check the processor machine type.
  643. for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
  644. if (Lines[I].startswith("processor ")) {
  645. size_t Pos = Lines[I].find("machine = ");
  646. if (Pos != StringRef::npos) {
  647. Pos += sizeof("machine = ") - 1;
  648. unsigned int Id;
  649. if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) {
  650. if (Id >= 2964 && HaveVectorSupport)
  651. return "z13";
  652. if (Id >= 2827)
  653. return "zEC12";
  654. if (Id >= 2817)
  655. return "z196";
  656. }
  657. }
  658. break;
  659. }
  660. }
  661. return "generic";
  662. }
  663. #else
  664. StringRef sys::getHostCPUName() {
  665. return "generic";
  666. }
  667. #endif
  668. #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
  669. || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
  670. bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
  671. unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
  672. unsigned MaxLevel;
  673. union {
  674. unsigned u[3];
  675. char c[12];
  676. } text;
  677. if (GetX86CpuIDAndInfo(0, &MaxLevel, text.u+0, text.u+2, text.u+1) ||
  678. MaxLevel < 1)
  679. return false;
  680. GetX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
  681. Features["cmov"] = (EDX >> 15) & 1;
  682. Features["mmx"] = (EDX >> 23) & 1;
  683. Features["sse"] = (EDX >> 25) & 1;
  684. Features["sse2"] = (EDX >> 26) & 1;
  685. Features["sse3"] = (ECX >> 0) & 1;
  686. Features["ssse3"] = (ECX >> 9) & 1;
  687. Features["sse4.1"] = (ECX >> 19) & 1;
  688. Features["sse4.2"] = (ECX >> 20) & 1;
  689. Features["pclmul"] = (ECX >> 1) & 1;
  690. Features["cx16"] = (ECX >> 13) & 1;
  691. Features["movbe"] = (ECX >> 22) & 1;
  692. Features["popcnt"] = (ECX >> 23) & 1;
  693. Features["aes"] = (ECX >> 25) & 1;
  694. Features["rdrnd"] = (ECX >> 30) & 1;
  695. // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
  696. // indicates that the AVX registers will be saved and restored on context
  697. // switch, then we have full AVX support.
  698. bool HasAVX = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) &&
  699. !GetX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6);
  700. Features["avx"] = HasAVX;
  701. Features["fma"] = HasAVX && (ECX >> 12) & 1;
  702. Features["f16c"] = HasAVX && (ECX >> 29) & 1;
  703. // AVX512 requires additional context to be saved by the OS.
  704. bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
  705. unsigned MaxExtLevel;
  706. GetX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
  707. bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
  708. !GetX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
  709. Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
  710. Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
  711. Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
  712. Features["xop"] = HasAVX && HasExtLeaf1 && ((ECX >> 11) & 1);
  713. Features["fma4"] = HasAVX && HasExtLeaf1 && ((ECX >> 16) & 1);
  714. Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
  715. bool HasLeaf7 = MaxLevel >= 7 &&
  716. !GetX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
  717. // AVX2 is only supported if we have the OS save support from AVX.
  718. Features["avx2"] = HasAVX && HasLeaf7 && (EBX >> 5) & 1;
  719. Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
  720. Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
  721. Features["hle"] = HasLeaf7 && ((EBX >> 4) & 1);
  722. Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
  723. Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
  724. Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
  725. Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
  726. Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
  727. // AVX512 is only supported if the OS supports the context save for it.
  728. Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
  729. Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
  730. Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
  731. Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
  732. Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
  733. Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
  734. Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
  735. return true;
  736. }
  737. #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
  738. bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
  739. // Read 1024 bytes from /proc/cpuinfo, which should contain the Features line
  740. // in all cases.
  741. char buffer[1024];
  742. ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
  743. if (CPUInfoSize == -1)
  744. return false;
  745. StringRef Str(buffer, CPUInfoSize);
  746. SmallVector<StringRef, 32> Lines;
  747. Str.split(Lines, "\n");
  748. SmallVector<StringRef, 32> CPUFeatures;
  749. // Look for the CPU features.
  750. for (unsigned I = 0, E = Lines.size(); I != E; ++I)
  751. if (Lines[I].startswith("Features")) {
  752. Lines[I].split(CPUFeatures, " ");
  753. break;
  754. }
  755. #if defined(__aarch64__)
  756. // Keep track of which crypto features we have seen
  757. enum {
  758. CAP_AES = 0x1,
  759. CAP_PMULL = 0x2,
  760. CAP_SHA1 = 0x4,
  761. CAP_SHA2 = 0x8
  762. };
  763. uint32_t crypto = 0;
  764. #endif
  765. for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
  766. StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
  767. #if defined(__aarch64__)
  768. .Case("asimd", "neon")
  769. .Case("fp", "fp-armv8")
  770. .Case("crc32", "crc")
  771. #else
  772. .Case("half", "fp16")
  773. .Case("neon", "neon")
  774. .Case("vfpv3", "vfp3")
  775. .Case("vfpv3d16", "d16")
  776. .Case("vfpv4", "vfp4")
  777. .Case("idiva", "hwdiv-arm")
  778. .Case("idivt", "hwdiv")
  779. #endif
  780. .Default("");
  781. #if defined(__aarch64__)
  782. // We need to check crypto separately since we need all of the crypto
  783. // extensions to enable the subtarget feature
  784. if (CPUFeatures[I] == "aes")
  785. crypto |= CAP_AES;
  786. else if (CPUFeatures[I] == "pmull")
  787. crypto |= CAP_PMULL;
  788. else if (CPUFeatures[I] == "sha1")
  789. crypto |= CAP_SHA1;
  790. else if (CPUFeatures[I] == "sha2")
  791. crypto |= CAP_SHA2;
  792. #endif
  793. if (LLVMFeatureStr != "")
  794. Features[LLVMFeatureStr] = true;
  795. }
  796. #if defined(__aarch64__)
  797. // If we have all crypto bits we can add the feature
  798. if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
  799. Features["crypto"] = true;
  800. #endif
  801. return true;
  802. }
  803. #else
  804. bool sys::getHostCPUFeatures(StringMap<bool> &Features){
  805. return false;
  806. }
  807. #endif
  808. std::string sys::getProcessTriple() {
  809. Triple PT(Triple::normalize(LLVM_HOST_TRIPLE));
  810. if (sizeof(void *) == 8 && PT.isArch32Bit())
  811. PT = PT.get64BitArchVariant();
  812. if (sizeof(void *) == 4 && PT.isArch64Bit())
  813. PT = PT.get32BitArchVariant();
  814. return PT.str();
  815. }