Daniel Dunbar | bb14672 | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 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 header file implements the operating system Host concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/System/Host.h" |
| 15 | #include "llvm/Config/config.h" |
Daniel Dunbar | 067d024 | 2009-11-14 10:09:12 +0000 | [diff] [blame^] | 16 | #include <string.h> |
Daniel Dunbar | bb14672 | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 17 | |
| 18 | // Include the platform-specific parts of this class. |
| 19 | #ifdef LLVM_ON_UNIX |
| 20 | #include "Unix/Host.inc" |
| 21 | #endif |
| 22 | #ifdef LLVM_ON_WIN32 |
| 23 | #include "Win32/Host.inc" |
| 24 | #endif |
| 25 | |
Daniel Dunbar | 067d024 | 2009-11-14 10:09:12 +0000 | [diff] [blame^] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // |
| 28 | // Implementations of the CPU detection routines |
| 29 | // |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | using namespace llvm; |
| 33 | |
| 34 | #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\ |
| 35 | || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64) |
| 36 | |
| 37 | /// GetX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in the |
| 38 | /// specified arguments. If we can't run cpuid on the host, return true. |
| 39 | static bool GetX86CpuIDAndInfo(unsigned value, unsigned *rEAX, |
| 40 | unsigned *rEBX, unsigned *rECX, unsigned *rEDX) { |
| 41 | #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64) |
| 42 | #if defined(__GNUC__) |
| 43 | // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually. |
| 44 | asm ("movq\t%%rbx, %%rsi\n\t" |
| 45 | "cpuid\n\t" |
| 46 | "xchgq\t%%rbx, %%rsi\n\t" |
| 47 | : "=a" (*rEAX), |
| 48 | "=S" (*rEBX), |
| 49 | "=c" (*rECX), |
| 50 | "=d" (*rEDX) |
| 51 | : "a" (value)); |
| 52 | return false; |
| 53 | #elif defined(_MSC_VER) |
| 54 | int registers[4]; |
| 55 | __cpuid(registers, value); |
| 56 | *rEAX = registers[0]; |
| 57 | *rEBX = registers[1]; |
| 58 | *rECX = registers[2]; |
| 59 | *rEDX = registers[3]; |
| 60 | return false; |
| 61 | #endif |
| 62 | #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86) |
| 63 | #if defined(__GNUC__) |
| 64 | asm ("movl\t%%ebx, %%esi\n\t" |
| 65 | "cpuid\n\t" |
| 66 | "xchgl\t%%ebx, %%esi\n\t" |
| 67 | : "=a" (*rEAX), |
| 68 | "=S" (*rEBX), |
| 69 | "=c" (*rECX), |
| 70 | "=d" (*rEDX) |
| 71 | : "a" (value)); |
| 72 | return false; |
| 73 | #elif defined(_MSC_VER) |
| 74 | __asm { |
| 75 | mov eax,value |
| 76 | cpuid |
| 77 | mov esi,rEAX |
| 78 | mov dword ptr [esi],eax |
| 79 | mov esi,rEBX |
| 80 | mov dword ptr [esi],ebx |
| 81 | mov esi,rECX |
| 82 | mov dword ptr [esi],ecx |
| 83 | mov esi,rEDX |
| 84 | mov dword ptr [esi],edx |
| 85 | } |
| 86 | return false; |
| 87 | #endif |
| 88 | #endif |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | static void DetectX86FamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) { |
| 93 | Family = (EAX >> 8) & 0xf; // Bits 8 - 11 |
| 94 | Model = (EAX >> 4) & 0xf; // Bits 4 - 7 |
| 95 | if (Family == 6 || Family == 0xf) { |
| 96 | if (Family == 0xf) |
| 97 | // Examine extended family ID if family ID is F. |
| 98 | Family += (EAX >> 20) & 0xff; // Bits 20 - 27 |
| 99 | // Examine extended model ID if family ID is 6 or F. |
| 100 | Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19 |
| 101 | } |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | |
| 106 | std::string sys::getHostCPUName() { |
| 107 | #if defined(__x86_64__) || defined(__i386__) |
| 108 | unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0; |
| 109 | if (GetX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX)) |
| 110 | return "generic"; |
| 111 | unsigned Family = 0; |
| 112 | unsigned Model = 0; |
| 113 | DetectX86FamilyModel(EAX, Family, Model); |
| 114 | |
| 115 | GetX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX); |
| 116 | bool Em64T = (EDX >> 29) & 0x1; |
| 117 | bool HasSSE3 = (ECX & 0x1); |
| 118 | |
| 119 | union { |
| 120 | unsigned u[3]; |
| 121 | char c[12]; |
| 122 | } text; |
| 123 | |
| 124 | GetX86CpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1); |
| 125 | if (memcmp(text.c, "GenuineIntel", 12) == 0) { |
| 126 | switch (Family) { |
| 127 | case 3: |
| 128 | return "i386"; |
| 129 | case 4: |
| 130 | return "i486"; |
| 131 | case 5: |
| 132 | switch (Model) { |
| 133 | case 4: return "pentium-mmx"; |
| 134 | default: return "pentium"; |
| 135 | } |
| 136 | case 6: |
| 137 | switch (Model) { |
| 138 | case 1: return "pentiumpro"; |
| 139 | case 3: |
| 140 | case 5: |
| 141 | case 6: return "pentium2"; |
| 142 | case 7: |
| 143 | case 8: |
| 144 | case 10: |
| 145 | case 11: return "pentium3"; |
| 146 | case 9: |
| 147 | case 13: return "pentium-m"; |
| 148 | case 14: return "yonah"; |
| 149 | case 15: |
| 150 | case 22: // Celeron M 540 |
| 151 | return "core2"; |
| 152 | case 23: // 45nm: Penryn , Wolfdale, Yorkfield (XE) |
| 153 | return "penryn"; |
| 154 | default: return "i686"; |
| 155 | } |
| 156 | case 15: { |
| 157 | switch (Model) { |
| 158 | case 3: |
| 159 | case 4: |
| 160 | case 6: // same as 4, but 65nm |
| 161 | return (Em64T) ? "nocona" : "prescott"; |
| 162 | case 26: |
| 163 | return "corei7"; |
| 164 | case 28: |
| 165 | return "atom"; |
| 166 | default: |
| 167 | return (Em64T) ? "x86-64" : "pentium4"; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | default: |
| 172 | return "generic"; |
| 173 | } |
| 174 | } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) { |
| 175 | // FIXME: this poorly matches the generated SubtargetFeatureKV table. There |
| 176 | // appears to be no way to generate the wide variety of AMD-specific targets |
| 177 | // from the information returned from CPUID. |
| 178 | switch (Family) { |
| 179 | case 4: |
| 180 | return "i486"; |
| 181 | case 5: |
| 182 | switch (Model) { |
| 183 | case 6: |
| 184 | case 7: return "k6"; |
| 185 | case 8: return "k6-2"; |
| 186 | case 9: |
| 187 | case 13: return "k6-3"; |
| 188 | default: return "pentium"; |
| 189 | } |
| 190 | case 6: |
| 191 | switch (Model) { |
| 192 | case 4: return "athlon-tbird"; |
| 193 | case 6: |
| 194 | case 7: |
| 195 | case 8: return "athlon-mp"; |
| 196 | case 10: return "athlon-xp"; |
| 197 | default: return "athlon"; |
| 198 | } |
| 199 | case 15: |
| 200 | if (HasSSE3) { |
| 201 | return "k8-sse3"; |
| 202 | } else { |
| 203 | switch (Model) { |
| 204 | case 1: return "opteron"; |
| 205 | case 5: return "athlon-fx"; // also opteron |
| 206 | default: return "athlon64"; |
| 207 | } |
| 208 | } |
| 209 | case 16: |
| 210 | return "amdfam10"; |
| 211 | default: |
| 212 | return "generic"; |
| 213 | } |
| 214 | } else { |
| 215 | return "generic"; |
| 216 | } |
| 217 | #else |
| 218 | return "generic"; |
| 219 | #endif |
| 220 | } |