blob: caa846bc59d782a31da284350220beeb0b7f2734 [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"
Evan Chengabc346c2006-10-06 08:21:07 +000015//#include "X86GenSubtarget.inc"
Nate Begemanfb5792f2005-07-12 01:41:54 +000016#include "llvm/Module.h"
Jim Laskey05a059d2006-09-07 12:23:47 +000017#include "llvm/Support/CommandLine.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000018#include <iostream>
Nate Begemanfb5792f2005-07-12 01:41:54 +000019using namespace llvm;
20
Jim Laskey05a059d2006-09-07 12:23:47 +000021cl::opt<X86Subtarget::AsmWriterFlavorTy>
Chris Lattnercdb341d2006-09-07 22:29:41 +000022AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::unset),
Jim Laskey05a059d2006-09-07 12:23:47 +000023 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 Lattnercdb341d2006-09-07 22:29:41 +000027 clEnumValEnd));
Jim Laskey05a059d2006-09-07 12:23:47 +000028
Chris Lattner1e39a152006-01-28 06:05:41 +000029/// 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 Cheng88c15782006-10-06 07:50:56 +000031static inline bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
Jeff Cohen41adb0d2006-01-28 18:09:06 +000032 unsigned *rECX, unsigned *rEDX) {
Evan Cheng25ab6902006-09-08 06:48:29 +000033#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 Cheng559806f2006-01-27 08:10:46 +000045#if defined(__GNUC__)
46 asm ("pushl\t%%ebx\n\t"
47 "cpuid\n\t"
Evan Chengb3a7e212006-01-27 19:30:30 +000048 "movl\t%%ebx, %%esi\n\t"
Evan Cheng559806f2006-01-27 08:10:46 +000049 "popl\t%%ebx"
Jeff Cohen41adb0d2006-01-28 18:09:06 +000050 : "=a" (*rEAX),
51 "=S" (*rEBX),
52 "=c" (*rECX),
53 "=d" (*rEDX)
Evan Cheng559806f2006-01-27 08:10:46 +000054 : "a" (value));
Chris Lattner1e39a152006-01-28 06:05:41 +000055 return false;
Jeff Cohen41adb0d2006-01-28 18:09:06 +000056#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 Cheng559806f2006-01-27 08:10:46 +000070#endif
71#endif
Chris Lattner1e39a152006-01-28 06:05:41 +000072 return true;
Evan Cheng559806f2006-01-27 08:10:46 +000073}
74
Evan Chengabc346c2006-10-06 08:21:07 +000075void X86Subtarget::DetectSubtargetFeatures() {
Evan Chengb3a7e212006-01-27 19:30:30 +000076 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Jeff Cohena3496402006-01-28 18:47:32 +000077 union {
Jeff Cohen216d2812006-01-28 19:48:34 +000078 unsigned u[3];
79 char c[12];
Jeff Cohena3496402006-01-28 18:47:32 +000080 } text;
81
Evan Chengabc346c2006-10-06 08:21:07 +000082 if (GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
83 return;
Jeff Cohen41adb0d2006-01-28 18:09:06 +000084
Evan Chengabc346c2006-10-06 08:21:07 +000085 // 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 Cheng559806f2006-01-27 08:10:46 +000096 }
97}
Evan Cheng97c7fc32006-01-26 09:53:06 +000098
Evan Cheng25ab6902006-09-08 06:48:29 +000099X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
Evan Cheng8e0055d2006-10-04 18:33:00 +0000100 : AsmFlavor(AsmWriterFlavor)
Evan Cheng25ab6902006-09-08 06:48:29 +0000101 , X86SSELevel(NoMMXSSE)
Evan Cheng25ab6902006-09-08 06:48:29 +0000102 , 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 Lattner104988a2006-01-27 22:37:09 +0000108
Evan Cheng97c7fc32006-01-26 09:53:06 +0000109 // Determine default and user specified characteristics
Evan Chengabc346c2006-10-06 08:21:07 +0000110 DetectSubtargetFeatures();
Evan Cheng25ab6902006-09-08 06:48:29 +0000111 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 Begemanfb5792f2005-07-12 01:41:54 +0000117 // 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 Lattnere5600e52005-11-21 22:31:58 +0000121 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 Begemanfb5792f2005-07-12 01:41:54 +0000128 } else if (TT.empty()) {
129#if defined(__CYGWIN__) || defined(__MINGW32__)
Chris Lattnere5600e52005-11-21 22:31:58 +0000130 TargetType = isCygwin;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000131#elif defined(__APPLE__)
Chris Lattnere5600e52005-11-21 22:31:58 +0000132 TargetType = isDarwin;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000133#elif defined(_WIN32)
Chris Lattnere5600e52005-11-21 22:31:58 +0000134 TargetType = isWindows;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000135#endif
136 }
137
Chris Lattnercdb341d2006-09-07 22:29:41 +0000138 // 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 Cheng932ad512006-05-25 21:59:08 +0000148 if (TargetType == isDarwin || TargetType == isCygwin)
Nate Begemanfb5792f2005-07-12 01:41:54 +0000149 stackAlignment = 16;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000150}