blob: 3553c60cada7c8af35a06527f67453a059043128 [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
Evan Chengdbd38d72006-01-27 21:49:34 +000019// FIXME: temporary.
20#include "llvm/Support/CommandLine.h"
21namespace {
22 cl::opt<bool> EnableSSE("enable-x86-sse", cl::Hidden,
23 cl::desc("Enable sse on X86"));
24}
25
Chris Lattner1e39a152006-01-28 06:05:41 +000026/// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
27/// specified arguments. If we can't run cpuid on the host, return true.
28static bool GetCpuIDAndInfo(unsigned value, unsigned *EAX, unsigned *EBX,
Evan Cheng559806f2006-01-27 08:10:46 +000029 unsigned *ECX, unsigned *EDX) {
30#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
31#if defined(__GNUC__)
32 asm ("pushl\t%%ebx\n\t"
33 "cpuid\n\t"
Evan Chengb3a7e212006-01-27 19:30:30 +000034 "movl\t%%ebx, %%esi\n\t"
Evan Cheng559806f2006-01-27 08:10:46 +000035 "popl\t%%ebx"
36 : "=a" (*EAX),
Evan Chengb3a7e212006-01-27 19:30:30 +000037 "=S" (*EBX),
Evan Cheng559806f2006-01-27 08:10:46 +000038 "=c" (*ECX),
39 "=d" (*EDX)
40 : "a" (value));
Chris Lattner1e39a152006-01-28 06:05:41 +000041 return false;
Evan Cheng559806f2006-01-27 08:10:46 +000042#endif
43#endif
Chris Lattner1e39a152006-01-28 06:05:41 +000044 return true;
Evan Cheng559806f2006-01-27 08:10:46 +000045}
46
47static const char *GetCurrentX86CPU() {
Evan Chengb3a7e212006-01-27 19:30:30 +000048 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Chris Lattner1e39a152006-01-28 06:05:41 +000049 if (GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
50 return "generic";
Evan Cheng559806f2006-01-27 08:10:46 +000051 unsigned Family = (EAX & (0xffffffff >> (32 - 4)) << 8) >> 8; // Bits 8 - 11
52 unsigned Model = (EAX & (0xffffffff >> (32 - 4)) << 4) >> 4; // Bits 4 - 7
Evan Chengb3a7e212006-01-27 19:30:30 +000053 GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Evan Cheng559806f2006-01-27 08:10:46 +000054 bool Em64T = EDX & (1 << 29);
55
56 switch (Family) {
57 case 3:
58 return "i386";
59 case 4:
60 return "i486";
61 case 5:
62 switch (Model) {
63 case 4: return "pentium-mmx";
64 default: return "pentium";
65 }
Evan Cheng559806f2006-01-27 08:10:46 +000066 case 6:
67 switch (Model) {
68 case 1: return "pentiumpro";
69 case 3:
70 case 5:
71 case 6: return "pentium2";
72 case 7:
73 case 8:
74 case 10:
75 case 11: return "pentium3";
76 case 9:
77 case 13: return "pentium-m";
78 case 14: return "yonah";
79 default: return "i686";
80 }
81 case 15: {
82 switch (Model) {
83 case 3:
84 case 4:
85 return (Em64T) ? "nocona" : "prescott";
86 default:
87 return (Em64T) ? "x86-64" : "pentium4";
88 }
89 }
90
91 default:
92 return "generic";
93 }
94}
Evan Cheng97c7fc32006-01-26 09:53:06 +000095
Chris Lattner104988a2006-01-27 22:37:09 +000096X86Subtarget::X86Subtarget(const Module &M, const std::string &FS) {
97 stackAlignment = 8;
98 indirectExternAndWeakGlobals = false;
99 X86SSELevel = NoMMXSSE;
100 X863DNowLevel = NoThreeDNow;
101 Is64Bit = false;
102
Evan Cheng97c7fc32006-01-26 09:53:06 +0000103 // Determine default and user specified characteristics
Evan Cheng559806f2006-01-27 08:10:46 +0000104 std::string CPU = GetCurrentX86CPU();
Evan Cheng97c7fc32006-01-26 09:53:06 +0000105
106 // Parse features string.
107 ParseSubtargetFeatures(FS, CPU);
108
Chris Lattnere5600e52005-11-21 22:31:58 +0000109 // Default to ELF unless otherwise specified.
110 TargetType = isELF;
Chris Lattner9f96a322006-01-27 18:30:50 +0000111
112 // FIXME: Force these off until they work. An llc-beta option should turn
113 // them back on.
Evan Chengdbd38d72006-01-27 21:49:34 +0000114 if (!EnableSSE) {
115 X86SSELevel = NoMMXSSE;
116 X863DNowLevel = NoThreeDNow;
117 }
Chris Lattnere5600e52005-11-21 22:31:58 +0000118
Nate Begemanfb5792f2005-07-12 01:41:54 +0000119 // Set the boolean corresponding to the current target triple, or the default
120 // if one cannot be determined, to true.
121 const std::string& TT = M.getTargetTriple();
122 if (TT.length() > 5) {
Chris Lattnere5600e52005-11-21 22:31:58 +0000123 if (TT.find("cygwin") != std::string::npos ||
124 TT.find("mingw") != std::string::npos)
125 TargetType = isCygwin;
126 else if (TT.find("darwin") != std::string::npos)
127 TargetType = isDarwin;
128 else if (TT.find("win32") != std::string::npos)
129 TargetType = isWindows;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000130 } else if (TT.empty()) {
131#if defined(__CYGWIN__) || defined(__MINGW32__)
Chris Lattnere5600e52005-11-21 22:31:58 +0000132 TargetType = isCygwin;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000133#elif defined(__APPLE__)
Chris Lattnere5600e52005-11-21 22:31:58 +0000134 TargetType = isDarwin;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000135#elif defined(_WIN32)
Chris Lattnere5600e52005-11-21 22:31:58 +0000136 TargetType = isWindows;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000137#endif
138 }
139
Chris Lattnerd460f572005-11-21 22:43:58 +0000140 if (TargetType == isDarwin) {
Nate Begemanfb5792f2005-07-12 01:41:54 +0000141 stackAlignment = 16;
142 indirectExternAndWeakGlobals = true;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000143 }
144}