OSDN Git Service

Pizza-lize :-)
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / driver-i386.c
index d3088f3..1c005f8 100644 (file)
@@ -1,5 +1,5 @@
 /* Subroutines for the gcc driver.
-   Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2007, 2008, 2010 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -21,11 +21,10 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
-#include <stdlib.h>
 
 const char *host_detect_local_cpu (int argc, const char **argv);
 
-#ifdef __GNUC__
+#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
 #include "cpuid.h"
 
 struct cache_desc
@@ -264,7 +263,8 @@ enum cache_type
 };
 
 static void
-detect_caches_cpuid4 (struct cache_desc *level1, struct cache_desc *level2)
+detect_caches_cpuid4 (struct cache_desc *level1, struct cache_desc *level2,
+                     struct cache_desc *level3)
 {
   struct cache_desc *cache;
 
@@ -289,6 +289,9 @@ detect_caches_cpuid4 (struct cache_desc *level1, struct cache_desc *level2)
              case 2:
                cache = level2;
                break;
+             case 3:
+               cache = level3;
+               break;
              default:
                cache = NULL;
              }
@@ -303,7 +306,7 @@ detect_caches_cpuid4 (struct cache_desc *level1, struct cache_desc *level2)
 
                cache->sizekb = (cache->assoc * part
                                 * cache->line * sets) / 1024;
-             }        
+             }
          }
        default:
          break;
@@ -314,12 +317,13 @@ detect_caches_cpuid4 (struct cache_desc *level1, struct cache_desc *level2)
 /* Returns the description of caches for an Intel processor.  */
 
 static const char *
-detect_caches_intel (bool xeon_mp, unsigned max_level, unsigned max_ext_level)
+detect_caches_intel (bool xeon_mp, unsigned max_level,
+                    unsigned max_ext_level, unsigned *l2sizekb)
 {
-  struct cache_desc level1 = {0, 0, 0}, level2 = {0, 0, 0};
+  struct cache_desc level1 = {0, 0, 0}, level2 = {0, 0, 0}, level3 = {0, 0, 0};
 
   if (max_level >= 4)
-    detect_caches_cpuid4 (&level1, &level2);
+    detect_caches_cpuid4 (&level1, &level2, &level3);
   else if (max_level >= 2)
     detect_caches_cpuid2 (xeon_mp, &level1, &level2);
   else
@@ -328,18 +332,28 @@ detect_caches_intel (bool xeon_mp, unsigned max_level, unsigned max_ext_level)
   if (level1.sizekb == 0)
     return "";
 
+  /* Let the L3 replace the L2. This assumes inclusive caches
+     and single threaded program for now. */
+  if (level3.sizekb)
+    level2 = level3;
+
   /* Intel CPUs are equipped with AMD style L2 cache info.  Try this
      method if other methods fail to provide L2 cache parameters.  */
   if (level2.sizekb == 0 && max_ext_level >= 0x80000006)
     detect_l2_cache (&level2);
 
+  *l2sizekb = level2.sizekb;
+
   return describe_cache (level1, level2);
 }
 
 enum vendor_signatures
 {
   SIG_INTEL =  0x756e6547 /* Genu */,
-  SIG_AMD =    0x68747541 /* Auth */
+  SIG_AMD =    0x68747541 /* Auth */,
+  SIG_CENTAUR =        0x746e6543 /* Cent */,
+  SIG_CYRIX =  0x69727943 /* Cyri */,
+  SIG_NSC =    0x646f6547 /* Geod */
 };
 
 enum processor_signatures
@@ -382,11 +396,17 @@ const char *host_detect_local_cpu (int argc, const char **argv)
   unsigned int has_lahf_lm = 0, has_sse4a = 0;
   unsigned int has_longmode = 0, has_3dnowp = 0, has_3dnow = 0;
   unsigned int has_movbe = 0, has_sse4_1 = 0, has_sse4_2 = 0;
-  unsigned int has_popcnt = 0, has_aes = 0, has_avx = 0;
-  unsigned int has_pclmul = 0, has_abm = 0;
+  unsigned int has_popcnt = 0, has_aes = 0, has_avx = 0, has_avx2 = 0;
+  unsigned int has_pclmul = 0, has_abm = 0, has_lwp = 0;
+  unsigned int has_fma = 0, has_fma4 = 0, has_xop = 0;
+  unsigned int has_bmi = 0, has_bmi2 = 0, has_tbm = 0, has_lzcnt = 0;
+  unsigned int has_rdrnd = 0, has_f16c = 0, has_fsgsbase = 0;
+  unsigned int has_osxsave = 0;
 
   bool arch;
 
+  unsigned int l2sizekb = 0;
+
   if (argc < 1)
     return NULL;
 
@@ -423,11 +443,15 @@ const char *host_detect_local_cpu (int argc, const char **argv)
   has_sse4_1 = ecx & bit_SSE4_1;
   has_sse4_2 = ecx & bit_SSE4_2;
   has_avx = ecx & bit_AVX;
+  has_osxsave = ecx & bit_OSXSAVE;
   has_cmpxchg16b = ecx & bit_CMPXCHG16B;
   has_movbe = ecx & bit_MOVBE;
   has_popcnt = ecx & bit_POPCNT;
   has_aes = ecx & bit_AES;
   has_pclmul = ecx & bit_PCLMUL;
+  has_fma = ecx & bit_FMA;
+  has_f16c = ecx & bit_F16C;
+  has_rdrnd = ecx & bit_RDRND;
 
   has_cmpxchg8b = edx & bit_CMPXCHG8B;
   has_cmov = edx & bit_CMOV;
@@ -435,6 +459,16 @@ const char *host_detect_local_cpu (int argc, const char **argv)
   has_sse = edx & bit_SSE;
   has_sse2 = edx & bit_SSE2;
 
+  if (max_level >= 7)
+    {
+      __cpuid_count (7, 0, eax, ebx, ecx, edx);
+
+      has_bmi = ebx & bit_BMI;
+      has_avx2 = ebx & bit_AVX2;
+      has_bmi2 = ebx & bit_BMI2;
+      has_fsgsbase = ebx & bit_FSGSBASE;
+    }
+
   /* Check cpuid level of extended features.  */
   __cpuid (0x80000000, ext_level, ebx, ecx, edx);
 
@@ -445,20 +479,51 @@ const char *host_detect_local_cpu (int argc, const char **argv)
       has_lahf_lm = ecx & bit_LAHF_LM;
       has_sse4a = ecx & bit_SSE4a;
       has_abm = ecx & bit_ABM;
+      has_lwp = ecx & bit_LWP;
+      has_fma4 = ecx & bit_FMA4;
+      has_xop = ecx & bit_XOP;
+      has_tbm = ecx & bit_TBM;
+      has_lzcnt = ecx & bit_LZCNT;
 
       has_longmode = edx & bit_LM;
       has_3dnowp = edx & bit_3DNOWP;
       has_3dnow = edx & bit_3DNOW;
     }
 
+  /* Get XCR_XFEATURE_ENABLED_MASK register with xgetbv.  */
+#define XCR_XFEATURE_ENABLED_MASK      0x0
+#define XSTATE_FP                      0x1
+#define XSTATE_SSE                     0x2
+#define XSTATE_YMM                     0x4
+  if (has_osxsave)
+    asm (".byte 0x0f; .byte 0x01; .byte 0xd0"
+        : "=a" (eax), "=d" (edx)
+        : "c" (XCR_XFEATURE_ENABLED_MASK));
+
+  /* Check if SSE and YMM states are supported.  */
+  if (!has_osxsave
+      || (eax & (XSTATE_SSE | XSTATE_YMM)) != (XSTATE_SSE | XSTATE_YMM))
+    {
+      has_avx = 0;
+      has_avx2 = 0;
+      has_fma = 0;
+      has_fma4 = 0;
+      has_f16c = 0;
+      has_xop = 0;
+    }
+
   if (!arch)
     {
-      if (vendor == SIG_AMD)
+      if (vendor == SIG_AMD
+         || vendor == SIG_CENTAUR
+         || vendor == SIG_CYRIX
+         || vendor == SIG_NSC)
        cache = detect_caches_amd (ext_level);
       else if (vendor == SIG_INTEL)
        {
          bool xeon_mp = (family == 15 && model == 6);
-         cache = detect_caches_intel (xeon_mp, max_level, ext_level);
+         cache = detect_caches_intel (xeon_mp, max_level,
+                                      ext_level, &l2sizekb);
        }
     }
 
@@ -474,17 +539,54 @@ const char *host_detect_local_cpu (int argc, const char **argv)
 
       if (name == SIG_GEODE)
        processor = PROCESSOR_GEODE;
+      else if (has_bmi)
+        processor = PROCESSOR_BDVER2;
+      else if (has_xop)
+       processor = PROCESSOR_BDVER1;
+      else if (has_sse4a && has_ssse3)
+        processor = PROCESSOR_BTVER1;
       else if (has_sse4a)
        processor = PROCESSOR_AMDFAM10;
       else if (has_sse2 || has_longmode)
        processor = PROCESSOR_K8;
-      else if (has_3dnowp)
+      else if (has_3dnowp && family == 6)
        processor = PROCESSOR_ATHLON;
       else if (has_mmx)
        processor = PROCESSOR_K6;
       else
        processor = PROCESSOR_PENTIUM;
     }
+  else if (vendor == SIG_CENTAUR)
+    {
+      if (arch)
+       {
+         switch (family)
+           {
+           case 6:
+             if (model > 9)
+               /* Use the default detection procedure.  */
+               processor = PROCESSOR_GENERIC32;
+             else if (model == 9)
+               cpu = "c3-2";
+             else if (model >= 6)
+               cpu = "c3";
+             else
+               processor = PROCESSOR_GENERIC32;
+             break;
+           case 5:
+             if (has_3dnow)
+               cpu = "winchip2";
+             else if (has_mmx)
+               cpu = "winchip2-c6";
+             else
+               processor = PROCESSOR_GENERIC32;
+             break;
+           default:
+             /* We have no idea.  */
+             processor = PROCESSOR_GENERIC32;
+           }
+       }
+    }
   else
     {
       switch (family)
@@ -522,30 +624,81 @@ const char *host_detect_local_cpu (int argc, const char **argv)
        cpu = "pentium";
       break;
     case PROCESSOR_PENTIUMPRO:
-      if (has_longmode)
-       /* It is Core 2 or Atom.  */
-       cpu = (model == 28) ? "atom" : "core2";
-      else if (arch)
+      switch (model)
        {
-         if (has_sse3)
-           /* It is Core Duo.  */
-           cpu = "prescott";
-         else if (has_sse2)
-           /* It is Pentium M.  */
-           cpu = "pentium-m";
-         else if (has_sse)
-           /* It is Pentium III.  */
-           cpu = "pentium3";
-         else if (has_mmx)
-           /* It is Pentium II.  */
-           cpu = "pentium2";
+       case 0x1c:
+       case 0x26:
+         /* Atom.  */
+         cpu = "atom";
+         break;
+       case 0x0f:
+         /* Merom.  */
+       case 0x17:
+       case 0x1d:
+         /* Penryn.  */
+         cpu = "core2";
+         break;
+       case 0x1a:
+       case 0x1e:
+       case 0x1f:
+       case 0x2e:
+         /* Nehalem.  */
+       case 0x25:
+       case 0x2c:
+       case 0x2f:
+         /* Westmere.  */
+         cpu = "corei7";
+         break;
+       case 0x2a:
+       case 0x2d:
+         /* Sandy Bridge.  */
+         cpu = "corei7-avx";
+         break;
+       case 0x3a:
+       case 0x3e:
+         /* Ivy Bridge.  */
+         cpu = "core-avx-i";
+         break;
+       default:
+         if (arch)
+           {
+             /* This is unknown family 0x6 CPU.  */
+             if (has_avx)
+               /* Assume Sandy Bridge.  */
+               cpu = "corei7-avx";
+             else if (has_sse4_2)
+               /* Assume Core i7.  */
+               cpu = "corei7";
+             else if (has_ssse3)
+               {
+                 if (has_movbe)
+                   /* Assume Atom.  */
+                   cpu = "atom";
+                 else
+                   /* Assume Core 2.  */
+                   cpu = "core2";
+               }
+             else if (has_sse3)
+               /* It is Core Duo.  */
+               cpu = "pentium-m";
+             else if (has_sse2)
+               /* It is Pentium M.  */
+               cpu = "pentium-m";
+             else if (has_sse)
+               /* It is Pentium III.  */
+               cpu = "pentium3";
+             else if (has_mmx)
+               /* It is Pentium II.  */
+               cpu = "pentium2";
+             else
+               /* Default to Pentium Pro.  */
+               cpu = "pentiumpro";
+           }
          else
-           /* Default to Pentium Pro.  */
-           cpu = "pentiumpro";
+           /* For -mtune, we default to -mtune=generic.  */
+           cpu = "generic";
+         break;
        }
-      else
-       /* For -mtune, we default to -mtune=generic.  */
-       cpu = "generic";
       break;
     case PROCESSOR_PENTIUM4:
       if (has_sse3)
@@ -582,6 +735,15 @@ const char *host_detect_local_cpu (int argc, const char **argv)
     case PROCESSOR_AMDFAM10:
       cpu = "amdfam10";
       break;
+    case PROCESSOR_BDVER1:
+      cpu = "bdver1";
+      break;
+    case PROCESSOR_BDVER2:
+      cpu = "bdver2";
+      break;
+    case PROCESSOR_BTVER1:
+      cpu = "btver1";
+      break;
 
     default:
       /* Use something reasonable.  */
@@ -611,27 +773,33 @@ const char *host_detect_local_cpu (int argc, const char **argv)
 
   if (arch)
     {
-      if (has_cmpxchg16b)
-       options = concat (options, " -mcx16", NULL);
-      if (has_lahf_lm)
-       options = concat (options, " -msahf", NULL);
-      if (has_movbe)
-       options = concat (options, " -mmovbe", NULL);
-      if (has_aes)
-       options = concat (options, " -maes", NULL);
-      if (has_pclmul)
-       options = concat (options, " -mpclmul", NULL);
-      if (has_popcnt)
-       options = concat (options, " -mpopcnt", NULL);
-      if (has_abm)
-       options = concat (options, " -mabm", NULL);
-
-      if (has_avx)
-       options = concat (options, " -mavx", NULL);
-      else if (has_sse4_2)
-       options = concat (options, " -msse4.2", NULL);
-      else if (has_sse4_1)
-       options = concat (options, " -msse4.1", NULL);
+      const char *cx16 = has_cmpxchg16b ? " -mcx16" : " -mno-cx16";
+      const char *sahf = has_lahf_lm ? " -msahf" : " -mno-sahf";
+      const char *movbe = has_movbe ? " -mmovbe" : " -mno-movbe";
+      const char *ase = has_aes ? " -maes" : " -mno-aes";
+      const char *pclmul = has_pclmul ? " -mpclmul" : " -mno-pclmul";
+      const char *popcnt = has_popcnt ? " -mpopcnt" : " -mno-popcnt";
+      const char *abm = has_abm ? " -mabm" : " -mno-abm";
+      const char *lwp = has_lwp ? " -mlwp" : " -mno-lwp";
+      const char *fma = has_fma ? " -mfma" : " -mno-fma";
+      const char *fma4 = has_fma4 ? " -mfma4" : " -mno-fma4";
+      const char *xop = has_xop ? " -mxop" : " -mno-xop";
+      const char *bmi = has_bmi ? " -mbmi" : " -mno-bmi";
+      const char *bmi2 = has_bmi2 ? " -mbmi2" : " -mno-bmi2";
+      const char *tbm = has_tbm ? " -mtbm" : " -mno-tbm";
+      const char *avx = has_avx ? " -mavx" : " -mno-avx";
+      const char *avx2 = has_avx2 ? " -mavx2" : " -mno-avx2";
+      const char *sse4_2 = has_sse4_2 ? " -msse4.2" : " -mno-sse4.2";
+      const char *sse4_1 = has_sse4_1 ? " -msse4.1" : " -mno-sse4.1";
+      const char *lzcnt = has_lzcnt ? " -mlzcnt" : " -mno-lzcnt";
+      const char *rdrnd = has_rdrnd ? " -mrdrnd" : " -mno-rdrnd";
+      const char *f16c = has_f16c ? " -mf16c" : " -mno-f16c";
+      const char *fsgsbase = has_fsgsbase ? " -mfsgsbase" : " -mno-fsgsbase";
+
+      options = concat (options, cx16, sahf, movbe, ase, pclmul,
+                       popcnt, abm, lwp, fma, fma4, xop, bmi, bmi2,
+                       tbm, avx, avx2, sse4_2, sse4_1, lzcnt, rdrnd,
+                       f16c, fsgsbase, NULL);
     }
 
 done: