Nate Begeman | 8c00f8c | 2005-08-04 07:12:09 +0000 | [diff] [blame] | 1 | //===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===// |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Nate Begeman and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the X86 specific subclass of TargetSubtarget. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "X86Subtarget.h" |
| 15 | #include "llvm/Module.h" |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | 97c7fc3 | 2006-01-26 09:53:06 +0000 | [diff] [blame] | 17 | #include "X86GenSubtarget.inc" |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 20 | cl::opt<X86Subtarget::AsmWriterFlavorTy> |
Chris Lattner | cdb341d | 2006-09-07 22:29:41 +0000 | [diff] [blame^] | 21 | AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::unset), |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 22 | cl::desc("Choose style of code to emit from X86 backend:"), |
| 23 | cl::values( |
| 24 | clEnumValN(X86Subtarget::att, "att", " Emit AT&T-style assembly"), |
| 25 | clEnumValN(X86Subtarget::intel, "intel", " Emit Intel-style assembly"), |
Chris Lattner | cdb341d | 2006-09-07 22:29:41 +0000 | [diff] [blame^] | 26 | clEnumValEnd)); |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 1e39a15 | 2006-01-28 06:05:41 +0000 | [diff] [blame] | 28 | /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the |
| 29 | /// specified arguments. If we can't run cpuid on the host, return true. |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 30 | static bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX, |
| 31 | unsigned *rECX, unsigned *rEDX) { |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 32 | #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86) |
| 33 | #if defined(__GNUC__) |
| 34 | asm ("pushl\t%%ebx\n\t" |
| 35 | "cpuid\n\t" |
Evan Cheng | b3a7e21 | 2006-01-27 19:30:30 +0000 | [diff] [blame] | 36 | "movl\t%%ebx, %%esi\n\t" |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 37 | "popl\t%%ebx" |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 38 | : "=a" (*rEAX), |
| 39 | "=S" (*rEBX), |
| 40 | "=c" (*rECX), |
| 41 | "=d" (*rEDX) |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 42 | : "a" (value)); |
Chris Lattner | 1e39a15 | 2006-01-28 06:05:41 +0000 | [diff] [blame] | 43 | return false; |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 44 | #elif defined(_MSC_VER) |
| 45 | __asm { |
| 46 | mov eax,value |
| 47 | cpuid |
| 48 | mov esi,rEAX |
| 49 | mov dword ptr [esi],eax |
| 50 | mov esi,rEBX |
| 51 | mov dword ptr [esi],ebx |
| 52 | mov esi,rECX |
| 53 | mov dword ptr [esi],ecx |
| 54 | mov esi,rEDX |
| 55 | mov dword ptr [esi],edx |
| 56 | } |
| 57 | return false; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 58 | #endif |
| 59 | #endif |
Chris Lattner | 1e39a15 | 2006-01-28 06:05:41 +0000 | [diff] [blame] | 60 | return true; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static const char *GetCurrentX86CPU() { |
Evan Cheng | b3a7e21 | 2006-01-27 19:30:30 +0000 | [diff] [blame] | 64 | unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0; |
Chris Lattner | 1e39a15 | 2006-01-28 06:05:41 +0000 | [diff] [blame] | 65 | if (GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX)) |
| 66 | return "generic"; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 67 | unsigned Family = (EAX & (0xffffffff >> (32 - 4)) << 8) >> 8; // Bits 8 - 11 |
| 68 | unsigned Model = (EAX & (0xffffffff >> (32 - 4)) << 4) >> 4; // Bits 4 - 7 |
Evan Cheng | b3a7e21 | 2006-01-27 19:30:30 +0000 | [diff] [blame] | 69 | GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX); |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 70 | bool Em64T = EDX & (1 << 29); |
| 71 | |
Jeff Cohen | a349640 | 2006-01-28 18:47:32 +0000 | [diff] [blame] | 72 | union { |
Jeff Cohen | 216d281 | 2006-01-28 19:48:34 +0000 | [diff] [blame] | 73 | unsigned u[3]; |
| 74 | char c[12]; |
Jeff Cohen | a349640 | 2006-01-28 18:47:32 +0000 | [diff] [blame] | 75 | } text; |
| 76 | |
Jeff Cohen | 216d281 | 2006-01-28 19:48:34 +0000 | [diff] [blame] | 77 | GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1); |
| 78 | if (memcmp(text.c, "GenuineIntel", 12) == 0) { |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 79 | switch (Family) { |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 80 | case 3: |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 81 | return "i386"; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 82 | case 4: |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 83 | return "i486"; |
| 84 | case 5: |
| 85 | switch (Model) { |
| 86 | case 4: return "pentium-mmx"; |
| 87 | default: return "pentium"; |
| 88 | } |
| 89 | case 6: |
| 90 | switch (Model) { |
| 91 | case 1: return "pentiumpro"; |
| 92 | case 3: |
| 93 | case 5: |
| 94 | case 6: return "pentium2"; |
| 95 | case 7: |
| 96 | case 8: |
| 97 | case 10: |
| 98 | case 11: return "pentium3"; |
| 99 | case 9: |
| 100 | case 13: return "pentium-m"; |
| 101 | case 14: return "yonah"; |
Evan Cheng | 54edc84 | 2006-06-16 21:58:49 +0000 | [diff] [blame] | 102 | default: |
| 103 | return (Model > 14) ? "yonah" : "i686"; |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 104 | } |
| 105 | case 15: { |
| 106 | switch (Model) { |
| 107 | case 3: |
| 108 | case 4: |
| 109 | return (Em64T) ? "nocona" : "prescott"; |
| 110 | default: |
| 111 | return (Em64T) ? "x86-64" : "pentium4"; |
| 112 | } |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 113 | } |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 114 | |
| 115 | default: |
| 116 | return "generic"; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 117 | } |
Jeff Cohen | 216d281 | 2006-01-28 19:48:34 +0000 | [diff] [blame] | 118 | } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) { |
Jeff Cohen | c4013d6 | 2006-01-28 20:30:18 +0000 | [diff] [blame] | 119 | // FIXME: this poorly matches the generated SubtargetFeatureKV table. There |
| 120 | // appears to be no way to generate the wide variety of AMD-specific targets |
| 121 | // from the information returned from CPUID. |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 122 | switch (Family) { |
Jeff Cohen | c4013d6 | 2006-01-28 20:30:18 +0000 | [diff] [blame] | 123 | case 4: |
| 124 | return "i486"; |
| 125 | case 5: |
| 126 | switch (Model) { |
| 127 | case 6: |
| 128 | case 7: return "k6"; |
| 129 | case 8: return "k6-2"; |
| 130 | case 9: |
| 131 | case 13: return "k6-3"; |
| 132 | default: return "pentium"; |
| 133 | } |
| 134 | case 6: |
| 135 | switch (Model) { |
| 136 | case 4: return "athlon-tbird"; |
| 137 | case 6: |
| 138 | case 7: |
| 139 | case 8: return "athlon-mp"; |
| 140 | case 10: return "athlon-xp"; |
| 141 | default: return "athlon"; |
| 142 | } |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 143 | case 15: |
Jeff Cohen | c4013d6 | 2006-01-28 20:30:18 +0000 | [diff] [blame] | 144 | switch (Model) { |
| 145 | case 5: return "athlon-fx"; // also opteron |
| 146 | default: return "athlon64"; |
| 147 | } |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 148 | |
| 149 | default: |
| 150 | return "generic"; |
| 151 | } |
| 152 | } else { |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 153 | return "generic"; |
| 154 | } |
| 155 | } |
Evan Cheng | 97c7fc3 | 2006-01-26 09:53:06 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 104988a | 2006-01-27 22:37:09 +0000 | [diff] [blame] | 157 | X86Subtarget::X86Subtarget(const Module &M, const std::string &FS) { |
| 158 | stackAlignment = 8; |
Evan Cheng | 18a8452 | 2006-02-16 00:21:07 +0000 | [diff] [blame] | 159 | // FIXME: this is a known good value for Yonah. Not sure about others. |
| 160 | MinRepStrSizeThreshold = 128; |
Chris Lattner | 104988a | 2006-01-27 22:37:09 +0000 | [diff] [blame] | 161 | X86SSELevel = NoMMXSSE; |
| 162 | X863DNowLevel = NoThreeDNow; |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 163 | AsmFlavor = AsmWriterFlavor; |
Chris Lattner | 104988a | 2006-01-27 22:37:09 +0000 | [diff] [blame] | 164 | Is64Bit = false; |
| 165 | |
Evan Cheng | 97c7fc3 | 2006-01-26 09:53:06 +0000 | [diff] [blame] | 166 | // Determine default and user specified characteristics |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 167 | std::string CPU = GetCurrentX86CPU(); |
Evan Cheng | 97c7fc3 | 2006-01-26 09:53:06 +0000 | [diff] [blame] | 168 | |
| 169 | // Parse features string. |
| 170 | ParseSubtargetFeatures(FS, CPU); |
| 171 | |
Chris Lattner | e5600e5 | 2005-11-21 22:31:58 +0000 | [diff] [blame] | 172 | // Default to ELF unless otherwise specified. |
| 173 | TargetType = isELF; |
Chris Lattner | 9f96a32 | 2006-01-27 18:30:50 +0000 | [diff] [blame] | 174 | |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 175 | // Set the boolean corresponding to the current target triple, or the default |
| 176 | // if one cannot be determined, to true. |
| 177 | const std::string& TT = M.getTargetTriple(); |
| 178 | if (TT.length() > 5) { |
Chris Lattner | e5600e5 | 2005-11-21 22:31:58 +0000 | [diff] [blame] | 179 | if (TT.find("cygwin") != std::string::npos || |
| 180 | TT.find("mingw") != std::string::npos) |
| 181 | TargetType = isCygwin; |
| 182 | else if (TT.find("darwin") != std::string::npos) |
| 183 | TargetType = isDarwin; |
| 184 | else if (TT.find("win32") != std::string::npos) |
| 185 | TargetType = isWindows; |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 186 | } else if (TT.empty()) { |
| 187 | #if defined(__CYGWIN__) || defined(__MINGW32__) |
Chris Lattner | e5600e5 | 2005-11-21 22:31:58 +0000 | [diff] [blame] | 188 | TargetType = isCygwin; |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 189 | #elif defined(__APPLE__) |
Chris Lattner | e5600e5 | 2005-11-21 22:31:58 +0000 | [diff] [blame] | 190 | TargetType = isDarwin; |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 191 | #elif defined(_WIN32) |
Chris Lattner | e5600e5 | 2005-11-21 22:31:58 +0000 | [diff] [blame] | 192 | TargetType = isWindows; |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 193 | #endif |
| 194 | } |
| 195 | |
Chris Lattner | cdb341d | 2006-09-07 22:29:41 +0000 | [diff] [blame^] | 196 | // If the asm syntax hasn't been overridden on the command line, use whatever |
| 197 | // the target wants. |
| 198 | if (AsmFlavor == X86Subtarget::unset) { |
| 199 | if (TargetType == isWindows) { |
| 200 | AsmFlavor = X86Subtarget::intel; |
| 201 | } else { |
| 202 | AsmFlavor = X86Subtarget::att; |
| 203 | } |
| 204 | } |
| 205 | |
Evan Cheng | 932ad51 | 2006-05-25 21:59:08 +0000 | [diff] [blame] | 206 | if (TargetType == isDarwin || TargetType == isCygwin) |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 207 | stackAlignment = 16; |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 208 | } |