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" |
Evan Cheng | abc346c | 2006-10-06 08:21:07 +0000 | [diff] [blame^] | 15 | //#include "X86GenSubtarget.inc" |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 18 | #include <iostream> |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 21 | cl::opt<X86Subtarget::AsmWriterFlavorTy> |
Chris Lattner | cdb341d | 2006-09-07 22:29:41 +0000 | [diff] [blame] | 22 | AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::unset), |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 23 | cl::desc("Choose style of code to emit from X86 backend:"), |
| 24 | cl::values( |
| 25 | clEnumValN(X86Subtarget::att, "att", " Emit AT&T-style assembly"), |
| 26 | clEnumValN(X86Subtarget::intel, "intel", " Emit Intel-style assembly"), |
Chris Lattner | cdb341d | 2006-09-07 22:29:41 +0000 | [diff] [blame] | 27 | clEnumValEnd)); |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 1e39a15 | 2006-01-28 06:05:41 +0000 | [diff] [blame] | 29 | /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the |
| 30 | /// specified arguments. If we can't run cpuid on the host, return true. |
Evan Cheng | 88c1578 | 2006-10-06 07:50:56 +0000 | [diff] [blame] | 31 | static inline bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX, |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 32 | unsigned *rECX, unsigned *rEDX) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 33 | #if defined(__x86_64__) |
| 34 | asm ("pushq\t%%rbx\n\t" |
| 35 | "cpuid\n\t" |
| 36 | "movl\t%%ebx, %%esi\n\t" |
| 37 | "popq\t%%rbx" |
| 38 | : "=a" (*rEAX), |
| 39 | "=S" (*rEBX), |
| 40 | "=c" (*rECX), |
| 41 | "=d" (*rEDX) |
| 42 | : "a" (value)); |
| 43 | return false; |
| 44 | #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86) |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 45 | #if defined(__GNUC__) |
| 46 | asm ("pushl\t%%ebx\n\t" |
| 47 | "cpuid\n\t" |
Evan Cheng | b3a7e21 | 2006-01-27 19:30:30 +0000 | [diff] [blame] | 48 | "movl\t%%ebx, %%esi\n\t" |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 49 | "popl\t%%ebx" |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 50 | : "=a" (*rEAX), |
| 51 | "=S" (*rEBX), |
| 52 | "=c" (*rECX), |
| 53 | "=d" (*rEDX) |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 54 | : "a" (value)); |
Chris Lattner | 1e39a15 | 2006-01-28 06:05:41 +0000 | [diff] [blame] | 55 | return false; |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 56 | #elif defined(_MSC_VER) |
| 57 | __asm { |
| 58 | mov eax,value |
| 59 | cpuid |
| 60 | mov esi,rEAX |
| 61 | mov dword ptr [esi],eax |
| 62 | mov esi,rEBX |
| 63 | mov dword ptr [esi],ebx |
| 64 | mov esi,rECX |
| 65 | mov dword ptr [esi],ecx |
| 66 | mov esi,rEDX |
| 67 | mov dword ptr [esi],edx |
| 68 | } |
| 69 | return false; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 70 | #endif |
| 71 | #endif |
Chris Lattner | 1e39a15 | 2006-01-28 06:05:41 +0000 | [diff] [blame] | 72 | return true; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Evan Cheng | abc346c | 2006-10-06 08:21:07 +0000 | [diff] [blame^] | 75 | void X86Subtarget::DetectSubtargetFeatures() { |
Evan Cheng | b3a7e21 | 2006-01-27 19:30:30 +0000 | [diff] [blame] | 76 | unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0; |
Jeff Cohen | a349640 | 2006-01-28 18:47:32 +0000 | [diff] [blame] | 77 | union { |
Jeff Cohen | 216d281 | 2006-01-28 19:48:34 +0000 | [diff] [blame] | 78 | unsigned u[3]; |
| 79 | char c[12]; |
Jeff Cohen | a349640 | 2006-01-28 18:47:32 +0000 | [diff] [blame] | 80 | } text; |
| 81 | |
Evan Cheng | abc346c | 2006-10-06 08:21:07 +0000 | [diff] [blame^] | 82 | if (GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1)) |
| 83 | return; |
Jeff Cohen | 41adb0d | 2006-01-28 18:09:06 +0000 | [diff] [blame] | 84 | |
Evan Cheng | abc346c | 2006-10-06 08:21:07 +0000 | [diff] [blame^] | 85 | // FIXME: support for AMD family of processors. |
| 86 | if (memcmp(text.c, "GenuineIntel", 12) == 0) { |
| 87 | GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX); |
| 88 | |
| 89 | if ((EDX >> 23) & 0x1) X86SSELevel = MMX; |
| 90 | if ((EDX >> 25) & 0x1) X86SSELevel = SSE1; |
| 91 | if ((EDX >> 26) & 0x1) X86SSELevel = SSE2; |
| 92 | if (ECX & 0x1) X86SSELevel = SSE3; |
| 93 | |
| 94 | GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX); |
| 95 | HasX86_64 = (EDX >> 29) & 0x1; |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
Evan Cheng | 97c7fc3 | 2006-01-26 09:53:06 +0000 | [diff] [blame] | 98 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 99 | X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit) |
Evan Cheng | 8e0055d | 2006-10-04 18:33:00 +0000 | [diff] [blame] | 100 | : AsmFlavor(AsmWriterFlavor) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 101 | , X86SSELevel(NoMMXSSE) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 102 | , HasX86_64(false) |
| 103 | , stackAlignment(8) |
| 104 | // FIXME: this is a known good value for Yonah. How about others? |
| 105 | , MinRepStrSizeThreshold(128) |
| 106 | , Is64Bit(is64Bit) |
| 107 | , TargetType(isELF) { // Default to ELF unless otherwise specified. |
Chris Lattner | 104988a | 2006-01-27 22:37:09 +0000 | [diff] [blame] | 108 | |
Evan Cheng | 97c7fc3 | 2006-01-26 09:53:06 +0000 | [diff] [blame] | 109 | // Determine default and user specified characteristics |
Evan Cheng | abc346c | 2006-10-06 08:21:07 +0000 | [diff] [blame^] | 110 | DetectSubtargetFeatures(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 111 | if (Is64Bit && !HasX86_64) { |
| 112 | std::cerr << "Warning: Generation of 64-bit code for a 32-bit processor " |
| 113 | "requested.\n"; |
| 114 | HasX86_64 = true; |
| 115 | } |
| 116 | |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 117 | // Set the boolean corresponding to the current target triple, or the default |
| 118 | // if one cannot be determined, to true. |
| 119 | const std::string& TT = M.getTargetTriple(); |
| 120 | if (TT.length() > 5) { |
Chris Lattner | e5600e5 | 2005-11-21 22:31:58 +0000 | [diff] [blame] | 121 | if (TT.find("cygwin") != std::string::npos || |
| 122 | TT.find("mingw") != std::string::npos) |
| 123 | TargetType = isCygwin; |
| 124 | else if (TT.find("darwin") != std::string::npos) |
| 125 | TargetType = isDarwin; |
| 126 | else if (TT.find("win32") != std::string::npos) |
| 127 | TargetType = isWindows; |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 128 | } else if (TT.empty()) { |
| 129 | #if defined(__CYGWIN__) || defined(__MINGW32__) |
Chris Lattner | e5600e5 | 2005-11-21 22:31:58 +0000 | [diff] [blame] | 130 | TargetType = isCygwin; |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 131 | #elif defined(__APPLE__) |
Chris Lattner | e5600e5 | 2005-11-21 22:31:58 +0000 | [diff] [blame] | 132 | TargetType = isDarwin; |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 133 | #elif defined(_WIN32) |
Chris Lattner | e5600e5 | 2005-11-21 22:31:58 +0000 | [diff] [blame] | 134 | TargetType = isWindows; |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 135 | #endif |
| 136 | } |
| 137 | |
Chris Lattner | cdb341d | 2006-09-07 22:29:41 +0000 | [diff] [blame] | 138 | // If the asm syntax hasn't been overridden on the command line, use whatever |
| 139 | // the target wants. |
| 140 | if (AsmFlavor == X86Subtarget::unset) { |
| 141 | if (TargetType == isWindows) { |
| 142 | AsmFlavor = X86Subtarget::intel; |
| 143 | } else { |
| 144 | AsmFlavor = X86Subtarget::att; |
| 145 | } |
| 146 | } |
| 147 | |
Evan Cheng | 932ad51 | 2006-05-25 21:59:08 +0000 | [diff] [blame] | 148 | if (TargetType == isDarwin || TargetType == isCygwin) |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 149 | stackAlignment = 16; |
Nate Begeman | fb5792f | 2005-07-12 01:41:54 +0000 | [diff] [blame] | 150 | } |