blob: 2c2acec299a5e571c3776dd1acc169e40bdb4ee4 [file] [log] [blame]
Nate Begeman8c00f8c2005-08-04 07:12:09 +00001//===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===//
Nate Begemanfb5792f2005-07-12 01:41:54 +00002//
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"
Evan Cheng97c7fc32006-01-26 09:53:06 +000016#include "X86GenSubtarget.inc"
Nate Begemanfb5792f2005-07-12 01:41:54 +000017using namespace llvm;
18
Chris Lattner1e39a152006-01-28 06:05:41 +000019/// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
20/// specified arguments. If we can't run cpuid on the host, return true.
Jeff Cohen41adb0d2006-01-28 18:09:06 +000021static bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
22 unsigned *rECX, unsigned *rEDX) {
Evan Cheng559806f2006-01-27 08:10:46 +000023#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
24#if defined(__GNUC__)
25 asm ("pushl\t%%ebx\n\t"
26 "cpuid\n\t"
Evan Chengb3a7e212006-01-27 19:30:30 +000027 "movl\t%%ebx, %%esi\n\t"
Evan Cheng559806f2006-01-27 08:10:46 +000028 "popl\t%%ebx"
Jeff Cohen41adb0d2006-01-28 18:09:06 +000029 : "=a" (*rEAX),
30 "=S" (*rEBX),
31 "=c" (*rECX),
32 "=d" (*rEDX)
Evan Cheng559806f2006-01-27 08:10:46 +000033 : "a" (value));
Chris Lattner1e39a152006-01-28 06:05:41 +000034 return false;
Jeff Cohen41adb0d2006-01-28 18:09:06 +000035#elif defined(_MSC_VER)
36 __asm {
37 mov eax,value
38 cpuid
39 mov esi,rEAX
40 mov dword ptr [esi],eax
41 mov esi,rEBX
42 mov dword ptr [esi],ebx
43 mov esi,rECX
44 mov dword ptr [esi],ecx
45 mov esi,rEDX
46 mov dword ptr [esi],edx
47 }
48 return false;
Evan Cheng559806f2006-01-27 08:10:46 +000049#endif
50#endif
Chris Lattner1e39a152006-01-28 06:05:41 +000051 return true;
Evan Cheng559806f2006-01-27 08:10:46 +000052}
53
54static const char *GetCurrentX86CPU() {
Evan Chengb3a7e212006-01-27 19:30:30 +000055 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Chris Lattner1e39a152006-01-28 06:05:41 +000056 if (GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
57 return "generic";
Evan Cheng559806f2006-01-27 08:10:46 +000058 unsigned Family = (EAX & (0xffffffff >> (32 - 4)) << 8) >> 8; // Bits 8 - 11
59 unsigned Model = (EAX & (0xffffffff >> (32 - 4)) << 4) >> 4; // Bits 4 - 7
Evan Chengb3a7e212006-01-27 19:30:30 +000060 GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Evan Cheng559806f2006-01-27 08:10:46 +000061 bool Em64T = EDX & (1 << 29);
62
Jeff Cohena3496402006-01-28 18:47:32 +000063 union {
Jeff Cohen216d2812006-01-28 19:48:34 +000064 unsigned u[3];
65 char c[12];
Jeff Cohena3496402006-01-28 18:47:32 +000066 } text;
67
Jeff Cohen216d2812006-01-28 19:48:34 +000068 GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
69 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
Jeff Cohen41adb0d2006-01-28 18:09:06 +000070 switch (Family) {
Evan Cheng559806f2006-01-27 08:10:46 +000071 case 3:
Jeff Cohen41adb0d2006-01-28 18:09:06 +000072 return "i386";
Evan Cheng559806f2006-01-27 08:10:46 +000073 case 4:
Jeff Cohen41adb0d2006-01-28 18:09:06 +000074 return "i486";
75 case 5:
76 switch (Model) {
77 case 4: return "pentium-mmx";
78 default: return "pentium";
79 }
80 case 6:
81 switch (Model) {
82 case 1: return "pentiumpro";
83 case 3:
84 case 5:
85 case 6: return "pentium2";
86 case 7:
87 case 8:
88 case 10:
89 case 11: return "pentium3";
90 case 9:
91 case 13: return "pentium-m";
92 case 14: return "yonah";
Evan Cheng54edc842006-06-16 21:58:49 +000093 default:
94 return (Model > 14) ? "yonah" : "i686";
Jeff Cohen41adb0d2006-01-28 18:09:06 +000095 }
96 case 15: {
97 switch (Model) {
98 case 3:
99 case 4:
100 return (Em64T) ? "nocona" : "prescott";
101 default:
102 return (Em64T) ? "x86-64" : "pentium4";
103 }
Evan Cheng559806f2006-01-27 08:10:46 +0000104 }
Jeff Cohen41adb0d2006-01-28 18:09:06 +0000105
106 default:
107 return "generic";
Evan Cheng559806f2006-01-27 08:10:46 +0000108 }
Jeff Cohen216d2812006-01-28 19:48:34 +0000109 } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
Jeff Cohenc4013d62006-01-28 20:30:18 +0000110 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
111 // appears to be no way to generate the wide variety of AMD-specific targets
112 // from the information returned from CPUID.
Jeff Cohen41adb0d2006-01-28 18:09:06 +0000113 switch (Family) {
Jeff Cohenc4013d62006-01-28 20:30:18 +0000114 case 4:
115 return "i486";
116 case 5:
117 switch (Model) {
118 case 6:
119 case 7: return "k6";
120 case 8: return "k6-2";
121 case 9:
122 case 13: return "k6-3";
123 default: return "pentium";
124 }
125 case 6:
126 switch (Model) {
127 case 4: return "athlon-tbird";
128 case 6:
129 case 7:
130 case 8: return "athlon-mp";
131 case 10: return "athlon-xp";
132 default: return "athlon";
133 }
Jeff Cohen41adb0d2006-01-28 18:09:06 +0000134 case 15:
Jeff Cohenc4013d62006-01-28 20:30:18 +0000135 switch (Model) {
136 case 5: return "athlon-fx"; // also opteron
137 default: return "athlon64";
138 }
Jeff Cohen41adb0d2006-01-28 18:09:06 +0000139
140 default:
141 return "generic";
142 }
143 } else {
Evan Cheng559806f2006-01-27 08:10:46 +0000144 return "generic";
145 }
146}
Evan Cheng97c7fc32006-01-26 09:53:06 +0000147
Chris Lattner104988a2006-01-27 22:37:09 +0000148X86Subtarget::X86Subtarget(const Module &M, const std::string &FS) {
149 stackAlignment = 8;
Evan Cheng18a84522006-02-16 00:21:07 +0000150 // FIXME: this is a known good value for Yonah. Not sure about others.
151 MinRepStrSizeThreshold = 128;
Chris Lattner104988a2006-01-27 22:37:09 +0000152 X86SSELevel = NoMMXSSE;
153 X863DNowLevel = NoThreeDNow;
154 Is64Bit = false;
155
Evan Cheng97c7fc32006-01-26 09:53:06 +0000156 // Determine default and user specified characteristics
Evan Cheng559806f2006-01-27 08:10:46 +0000157 std::string CPU = GetCurrentX86CPU();
Evan Cheng97c7fc32006-01-26 09:53:06 +0000158
159 // Parse features string.
160 ParseSubtargetFeatures(FS, CPU);
161
Chris Lattnere5600e52005-11-21 22:31:58 +0000162 // Default to ELF unless otherwise specified.
163 TargetType = isELF;
Chris Lattner9f96a322006-01-27 18:30:50 +0000164
Nate Begemanfb5792f2005-07-12 01:41:54 +0000165 // Set the boolean corresponding to the current target triple, or the default
166 // if one cannot be determined, to true.
167 const std::string& TT = M.getTargetTriple();
168 if (TT.length() > 5) {
Chris Lattnere5600e52005-11-21 22:31:58 +0000169 if (TT.find("cygwin") != std::string::npos ||
170 TT.find("mingw") != std::string::npos)
171 TargetType = isCygwin;
172 else if (TT.find("darwin") != std::string::npos)
173 TargetType = isDarwin;
174 else if (TT.find("win32") != std::string::npos)
175 TargetType = isWindows;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000176 } else if (TT.empty()) {
177#if defined(__CYGWIN__) || defined(__MINGW32__)
Chris Lattnere5600e52005-11-21 22:31:58 +0000178 TargetType = isCygwin;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000179#elif defined(__APPLE__)
Chris Lattnere5600e52005-11-21 22:31:58 +0000180 TargetType = isDarwin;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000181#elif defined(_WIN32)
Chris Lattnere5600e52005-11-21 22:31:58 +0000182 TargetType = isWindows;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000183#endif
184 }
185
Evan Cheng932ad512006-05-25 21:59:08 +0000186 if (TargetType == isDarwin || TargetType == isCygwin)
Nate Begemanfb5792f2005-07-12 01:41:54 +0000187 stackAlignment = 16;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000188}