isel (i32 anyext i16) as insert_subreg when 16-bit ops are being promoted.
[oota-llvm.git] / lib / Target / X86 / X86Subtarget.cpp
1 //===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the X86 specific subclass of TargetSubtarget.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "subtarget"
15 #include "X86Subtarget.h"
16 #include "X86InstrInfo.h"
17 #include "X86GenSubtarget.inc"
18 #include "llvm/GlobalValue.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/System/Host.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/ADT/SmallVector.h"
26 using namespace llvm;
27
28 static cl::opt<bool>
29 DoPromote16Bit("promote-16bit", cl::Hidden,
30                cl::desc("Promote 16-bit instructions"));
31
32 #if defined(_MSC_VER)
33 #include <intrin.h>
34 #endif
35
36 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
37 /// current subtarget according to how we should reference it in a non-pcrel
38 /// context.
39 unsigned char X86Subtarget::
40 ClassifyBlockAddressReference() const {
41   if (isPICStyleGOT())    // 32-bit ELF targets.
42     return X86II::MO_GOTOFF;
43   
44   if (isPICStyleStubPIC())   // Darwin/32 in PIC mode.
45     return X86II::MO_PIC_BASE_OFFSET;
46   
47   // Direct static reference to label.
48   return X86II::MO_NO_FLAG;
49 }
50
51 /// ClassifyGlobalReference - Classify a global variable reference for the
52 /// current subtarget according to how we should reference it in a non-pcrel
53 /// context.
54 unsigned char X86Subtarget::
55 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
56   // DLLImport only exists on windows, it is implemented as a load from a
57   // DLLIMPORT stub.
58   if (GV->hasDLLImportLinkage())
59     return X86II::MO_DLLIMPORT;
60
61   // Materializable GVs (in JIT lazy compilation mode) do not require an
62   // extra load from stub.
63   bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
64
65   // X86-64 in PIC mode.
66   if (isPICStyleRIPRel()) {
67     // Large model never uses stubs.
68     if (TM.getCodeModel() == CodeModel::Large)
69       return X86II::MO_NO_FLAG;
70       
71     if (isTargetDarwin()) {
72       // If symbol visibility is hidden, the extra load is not needed if
73       // target is x86-64 or the symbol is definitely defined in the current
74       // translation unit.
75       if (GV->hasDefaultVisibility() &&
76           (isDecl || GV->isWeakForLinker()))
77         return X86II::MO_GOTPCREL;
78     } else {
79       assert(isTargetELF() && "Unknown rip-relative target");
80
81       // Extra load is needed for all externally visible.
82       if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
83         return X86II::MO_GOTPCREL;
84     }
85
86     return X86II::MO_NO_FLAG;
87   }
88   
89   if (isPICStyleGOT()) {   // 32-bit ELF targets.
90     // Extra load is needed for all externally visible.
91     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
92       return X86II::MO_GOTOFF;
93     return X86II::MO_GOT;
94   }
95   
96   if (isPICStyleStubPIC()) {  // Darwin/32 in PIC mode.
97     // Determine whether we have a stub reference and/or whether the reference
98     // is relative to the PIC base or not.
99     
100     // If this is a strong reference to a definition, it is definitely not
101     // through a stub.
102     if (!isDecl && !GV->isWeakForLinker())
103       return X86II::MO_PIC_BASE_OFFSET;
104
105     // Unless we have a symbol with hidden visibility, we have to go through a
106     // normal $non_lazy_ptr stub because this symbol might be resolved late.
107     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
108       return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
109     
110     // If symbol visibility is hidden, we have a stub for common symbol
111     // references and external declarations.
112     if (isDecl || GV->hasCommonLinkage()) {
113       // Hidden $non_lazy_ptr reference.
114       return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
115     }
116     
117     // Otherwise, no stub.
118     return X86II::MO_PIC_BASE_OFFSET;
119   }
120   
121   if (isPICStyleStubNoDynamic()) {  // Darwin/32 in -mdynamic-no-pic mode.
122     // Determine whether we have a stub reference.
123     
124     // If this is a strong reference to a definition, it is definitely not
125     // through a stub.
126     if (!isDecl && !GV->isWeakForLinker())
127       return X86II::MO_NO_FLAG;
128     
129     // Unless we have a symbol with hidden visibility, we have to go through a
130     // normal $non_lazy_ptr stub because this symbol might be resolved late.
131     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
132       return X86II::MO_DARWIN_NONLAZY;
133
134     // Otherwise, no stub.
135     return X86II::MO_NO_FLAG;
136   }
137   
138   // Direct static reference to global.
139   return X86II::MO_NO_FLAG;
140 }
141
142
143 /// getBZeroEntry - This function returns the name of a function which has an
144 /// interface like the non-standard bzero function, if such a function exists on
145 /// the current subtarget and it is considered prefereable over memset with zero
146 /// passed as the second argument. Otherwise it returns null.
147 const char *X86Subtarget::getBZeroEntry() const {
148   // Darwin 10 has a __bzero entry point for this purpose.
149   if (getDarwinVers() >= 10)
150     return "__bzero";
151
152   return 0;
153 }
154
155 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
156 /// to immediate address.
157 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
158   if (Is64Bit)
159     return false;
160   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
161 }
162
163 /// getSpecialAddressLatency - For targets where it is beneficial to
164 /// backschedule instructions that compute addresses, return a value
165 /// indicating the number of scheduling cycles of backscheduling that
166 /// should be attempted.
167 unsigned X86Subtarget::getSpecialAddressLatency() const {
168   // For x86 out-of-order targets, back-schedule address computations so
169   // that loads and stores aren't blocked.
170   // This value was chosen arbitrarily.
171   return 200;
172 }
173
174 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
175 /// specified arguments.  If we can't run cpuid on the host, return true.
176 static bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX,
177                             unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
178 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
179   #if defined(__GNUC__)
180     // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
181     asm ("movq\t%%rbx, %%rsi\n\t"
182          "cpuid\n\t"
183          "xchgq\t%%rbx, %%rsi\n\t"
184          : "=a" (*rEAX),
185            "=S" (*rEBX),
186            "=c" (*rECX),
187            "=d" (*rEDX)
188          :  "a" (value));
189     return false;
190   #elif defined(_MSC_VER)
191     int registers[4];
192     __cpuid(registers, value);
193     *rEAX = registers[0];
194     *rEBX = registers[1];
195     *rECX = registers[2];
196     *rEDX = registers[3];
197     return false;
198   #endif
199 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
200   #if defined(__GNUC__)
201     asm ("movl\t%%ebx, %%esi\n\t"
202          "cpuid\n\t"
203          "xchgl\t%%ebx, %%esi\n\t"
204          : "=a" (*rEAX),
205            "=S" (*rEBX),
206            "=c" (*rECX),
207            "=d" (*rEDX)
208          :  "a" (value));
209     return false;
210   #elif defined(_MSC_VER)
211     __asm {
212       mov   eax,value
213       cpuid
214       mov   esi,rEAX
215       mov   dword ptr [esi],eax
216       mov   esi,rEBX
217       mov   dword ptr [esi],ebx
218       mov   esi,rECX
219       mov   dword ptr [esi],ecx
220       mov   esi,rEDX
221       mov   dword ptr [esi],edx
222     }
223     return false;
224   #endif
225 #endif
226   return true;
227 }
228
229 static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
230   Family = (EAX >> 8) & 0xf; // Bits 8 - 11
231   Model  = (EAX >> 4) & 0xf; // Bits 4 - 7
232   if (Family == 6 || Family == 0xf) {
233     if (Family == 0xf)
234       // Examine extended family ID if family ID is F.
235       Family += (EAX >> 20) & 0xff;    // Bits 20 - 27
236     // Examine extended model ID if family ID is 6 or F.
237     Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
238   }
239 }
240
241 void X86Subtarget::AutoDetectSubtargetFeatures() {
242   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
243   union {
244     unsigned u[3];
245     char     c[12];
246   } text;
247   
248   if (GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
249     return;
250
251   GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
252   
253   if ((EDX >> 15) & 1) HasCMov = true;
254   if ((EDX >> 23) & 1) X86SSELevel = MMX;
255   if ((EDX >> 25) & 1) X86SSELevel = SSE1;
256   if ((EDX >> 26) & 1) X86SSELevel = SSE2;
257   if (ECX & 0x1)       X86SSELevel = SSE3;
258   if ((ECX >> 9)  & 1) X86SSELevel = SSSE3;
259   if ((ECX >> 19) & 1) X86SSELevel = SSE41;
260   if ((ECX >> 20) & 1) X86SSELevel = SSE42;
261
262   bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
263   bool IsAMD   = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
264
265   HasFMA3 = IsIntel && ((ECX >> 12) & 0x1);
266   HasAVX = ((ECX >> 28) & 0x1);
267   HasAES = IsIntel && ((ECX >> 25) & 0x1);
268
269   if (IsIntel || IsAMD) {
270     // Determine if bit test memory instructions are slow.
271     unsigned Family = 0;
272     unsigned Model  = 0;
273     DetectFamilyModel(EAX, Family, Model);
274     IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
275     // If it's Nehalem, unaligned memory access is fast.
276     if (Family == 15 && Model == 26)
277       IsUAMemFast = true;
278
279     GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
280     HasX86_64 = (EDX >> 29) & 0x1;
281     HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
282     HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
283   }
284 }
285
286 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &FS, 
287                            bool is64Bit)
288   : PICStyle(PICStyles::None)
289   , X86SSELevel(NoMMXSSE)
290   , X863DNowLevel(NoThreeDNow)
291   , HasCMov(false)
292   , HasX86_64(false)
293   , HasSSE4A(false)
294   , HasAVX(false)
295   , HasAES(false)
296   , HasFMA3(false)
297   , HasFMA4(false)
298   , IsBTMemSlow(false)
299   , IsUAMemFast(false)
300   , HasVectorUAMem(false)
301   , Promote16Bit(DoPromote16Bit)
302   , DarwinVers(0)
303   , stackAlignment(8)
304   // FIXME: this is a known good value for Yonah. How about others?
305   , MaxInlineSizeThreshold(128)
306   , Is64Bit(is64Bit)
307   , TargetType(isELF) { // Default to ELF unless otherwise specified.
308
309   // default to hard float ABI
310   if (FloatABIType == FloatABI::Default)
311     FloatABIType = FloatABI::Hard;
312     
313   // Determine default and user specified characteristics
314   if (!FS.empty()) {
315     // If feature string is not empty, parse features string.
316     std::string CPU = sys::getHostCPUName();
317     ParseSubtargetFeatures(FS, CPU);
318     // All X86-64 CPUs also have SSE2, however user might request no SSE via 
319     // -mattr, so don't force SSELevel here.
320   } else {
321     // Otherwise, use CPUID to auto-detect feature set.
322     AutoDetectSubtargetFeatures();
323     // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
324     if (Is64Bit && X86SSELevel < SSE2)
325       X86SSELevel = SSE2;
326   }
327
328   // If requesting codegen for X86-64, make sure that 64-bit features
329   // are enabled.
330   if (Is64Bit) {
331     HasX86_64 = true;
332
333     // All 64-bit cpus have cmov support.
334     HasCMov = true;
335   }
336     
337
338   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
339                << ", 3DNowLevel " << X863DNowLevel
340                << ", 64bit " << HasX86_64 << "\n");
341   assert((!Is64Bit || HasX86_64) &&
342          "64-bit code requested on a subtarget that doesn't support it!");
343
344   // Set the boolean corresponding to the current target triple, or the default
345   // if one cannot be determined, to true.
346   if (TT.length() > 5) {
347     size_t Pos;
348     if ((Pos = TT.find("-darwin")) != std::string::npos) {
349       TargetType = isDarwin;
350       
351       // Compute the darwin version number.
352       if (isdigit(TT[Pos+7]))
353         DarwinVers = atoi(&TT[Pos+7]);
354       else
355         DarwinVers = 8;  // Minimum supported darwin is Tiger.
356     } else if (TT.find("linux") != std::string::npos) {
357       // Linux doesn't imply ELF, but we don't currently support anything else.
358       TargetType = isELF;
359     } else if (TT.find("cygwin") != std::string::npos) {
360       TargetType = isCygwin;
361     } else if (TT.find("mingw") != std::string::npos) {
362       TargetType = isMingw;
363     } else if (TT.find("win32") != std::string::npos) {
364       TargetType = isWindows;
365     } else if (TT.find("windows") != std::string::npos) {
366       TargetType = isWindows;
367     } else if (TT.find("-cl") != std::string::npos) {
368       TargetType = isDarwin;
369       DarwinVers = 9;
370     }
371   }
372
373   // Stack alignment is 16 bytes on Darwin (both 32 and 64 bit) and for all 64
374   // bit targets.
375   if (TargetType == isDarwin || Is64Bit)
376     stackAlignment = 16;
377
378   if (StackAlignment)
379     stackAlignment = StackAlignment;
380 }