Patchwork [google,gcc-4_7] Backport CPU runtime detection from trunk to google/gcc-4_7

login
register
mail settings
Submitter Sriraman Tallam
Date Aug. 23, 2012, 8:13 p.m.
Message ID <CAAs8HmybRZcC_K1w40yuZ8LLZ3SpU2=cfQDQsbZ8QAapz7gB=A@mail.gmail.com>
Download mbox | patch
Permalink /patch/179716/
State New
Headers show

Comments

Sriraman Tallam - Aug. 23, 2012, 8:13 p.m.
Hi David,

   I have backported CPU runtime detection feature in trunk to
google/gcc-4_7 by backporting trunk revisions:
r186789,r186795,r186855,r187365,r188417

   Patch attached.

Thanks,
-Sri.
Backport runtime CPU detection from trunk in revisions: r186789,r186795,r186855,r187365,r188417

	gcc/ChangeLog:
	Backport r186789,r186795,r186855,r187365,r188417

	r186789:
	2012-04-24  Sriraman Tallam  <tmsriram@google.com>

		* config/i386/i386.c (build_processor_model_struct): New function.
		(make_var_decl): New function.
		(fold_builtin_cpu): New function.
		(ix86_fold_builtin): New function.
		(make_cpu_type_builtin): New function.
		(ix86_init_platform_type_builtins): New function.
		(ix86_expand_builtin): Expand new builtins by folding them.
		(ix86_init_builtins): Make new builtins to detect CPU type.
		(TARGET_FOLD_BUILTIN): New macro.
		(IX86_BUILTIN_CPU_INIT): New enum value.
		(IX86_BUILTIN_CPU_IS): New enum value.
		(IX86_BUILTIN_CPU_SUPPORTS): New enum value.
		* config/i386/i386-builtin-types.def: New function type.
		* testsuite/gcc.target/builtin_target.c: New testcase.
		* doc/extend.texi: Document builtins.

	r186795:
	No changes.

	r186855:
	2012-04-25  Sriraman Tallam  <tmsriram@google.com>

		* doc/extend.texi: Document avx2 support.
		* config/i386/i386.c (fold_builtin_cpu): Add avx2.
		* testsuite/gcc.target/i386/builtin_target.c: Check avx2.

	r187365:
	2012-05-10  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>

		* doc/extend.texi (X86 Built-in Functions, __builtin_cpu_init):
		Document requirement to call in constructors.

		* config/i386/i386.c: Update comments for i386-cpuinfo.c name
		change.

	r188417:
	2012-06-11  Sriraman Tallam  <tmsriram@google.com>

		* testsuite/gcc.target/i386/builtin_target.c (vendor_signatures): New enum.
		(check_intel_cpu_model): New function.
		(check_amd_cpu_model): New function.
		(check_features): New function.
		(__get_cpuid_output): New function.
		(check_detailed): New function.
		(fn1): Rename to quick_check.
		(main): Update to call quick_check and call check_detailed.

	libgcc/ChangeLog:

		Backport r186789,r186795,r186855,r187365,r188417

	r186789:
	2012-04-24  Sriraman Tallam  <tmsriram@google.com>

		* libgcc/config/i386/i386-cpuinfo.c: New file.
		* libgcc/config/i386/t-cpuinfo: New file.
		* libgcc/config.host: Include t-cpuinfo.
		* libgcc/config/i386/libgcc-glibc.ver: Version symbol __cpu_model.

	r186795:
	2012-04-24  Sriraman Tallam  <tmsriram@google.com>
		* libgcc/config/i386/i386-cpuinfo.c: Set __cpu_vendor always.

	r186855:
	2012-04-25  Sriraman Tallam  <tmsriram@google.com>

		* config/i386/i386-cpuinfo.c (FEATURE_AVX2): New enum value.
		(get_available_features): New argument. Check for AVX2.
		(__cpu_indicator_init): Modify call to get_available_features.

	r187365:
	2012-05-10  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>

		* config.host (i[34567]86-*-linux*, x86_64-*-linux*)
		(i[34567]86-*-kfreebsd*-gnu, x86_64-*-kfreebsd*-gnu)
		(i[34567]86-*-knetbsd*-gnu, i[34567]86-*-gnu*): Move
		i386/t-cpuinfo ...
		(i[34567]86-*-*, x86_64-*-*): ... here.

		* config/i386/libgcc-bsd.ver (GCC_4.8.0): New version.
		* config/i386/libgcc-sol2.ver (GCC_4.8.0): New version.

		* config/i386/i386-cpuinfo.c: Rename to ...
		* config/i386/cpuinfo.c: ... this.
		* config/i386/t-cpuinfo (LIB2ADD): Reflect this.

		* configure.ac (AC_CONFIG_HEADER): Call for auto-target.h.
		(libgcc_cv_init_priority): New test.
		* configure: Regenerate.
		* config.in: New file.
		* Makefile.in (clean): Rename config.h to auto-target.h.
		(config.h): Likewise.
		(stamp-h): Likewise.

		* config/i386/cpuinfo.c (auto-target.h): Include.
		(CONSTRUCTOR_PRIORITY): Define.
		(__cpu_indicator_init): Use it.

	r188417:
	2012-06-11  Sriraman Tallam  <tmsriram@google.com>

		* config/i386/libgcc-bsd.ver: Version symbol __cpu_indicator_init.
		* config/i386/libgcc-sol2.ver: Ditto.
		* config/i386/libgcc-glibc.ver: Ditto.

Patch

Index: libgcc/config.host
===================================================================
--- libgcc/config.host	(revision 190625)
+++ libgcc/config.host	(working copy)
@@ -1142,6 +1142,12 @@  mep*-*-*)
 esac
 
 case ${host} in
+i[34567]86-*-* | x86_64-*-*)
+	tmake_file="${tmake_file} i386/t-cpuinfo"
+	;;
+esac
+
+case ${host} in
 i[34567]86-*-linux* | x86_64-*-linux* | \
   i[34567]86-*-kfreebsd*-gnu | x86_64-*-kfreebsd*-gnu | \
   i[34567]86-*-knetbsd*-gnu | \
Index: libgcc/Makefile.in
===================================================================
--- libgcc/Makefile.in	(revision 190625)
+++ libgcc/Makefile.in	(working copy)
@@ -121,7 +121,7 @@  installcheck:
 .PHONY: all clean
 
 clean:
-	-rm -f config.h libgcc_tm.h stamp-h stmp-ldirs libgcc.map
+	-rm -f auto-target.h libgcc_tm.h stamp-h stmp-ldirs libgcc.map
 	-rm -f *$(objext)
 	-rm -f *.dep
 	-rm -f *.a
@@ -139,9 +139,9 @@  Makefile: $(srcdir)/Makefile.in config.status
 
 # Depending on Makefile makes sure that config.status has been re-run
 # if needed.  This prevents problems with parallel builds.
-config.h: stamp-h ; @true
+auto-target.h: stamp-h ; @true
 stamp-h: $(srcdir)/config.in config.status Makefile
-	CONFIG_FILES= CONFIG_HEADERS=config.h:$(srcdir)/config.in $(SHELL) ./config.status
+	CONFIG_FILES= CONFIG_HEADERS=auto-target.h:$(srcdir)/config.in $(SHELL) ./config.status
 
 config.status: $(srcdir)/configure $(srcdir)/config.host
 	$(SHELL) ./config.status --recheck
Index: libgcc/configure.ac
===================================================================
--- libgcc/configure.ac	(revision 190625)
+++ libgcc/configure.ac	(working copy)
@@ -44,6 +44,7 @@  else
 fi
 AC_SUBST(libgcc_topdir)
 AC_CONFIG_AUX_DIR($libgcc_topdir)
+AC_CONFIG_HEADER(auto-target.h:config.in)
 
 AC_ARG_ENABLE(shared,
 [  --disable-shared        don't provide a shared libgcc],
@@ -332,6 +333,15 @@  if test "$enable_tls $gcc_cv_use_emutls" = "yes ye
 fi
 AC_SUBST(set_use_emutls)
 
+AC_CACHE_CHECK(for init priority support, libgcc_cv_init_priority, [
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,
+  [[void ip (void) __attribute__ ((constructor (1)));]])],
+  [libgcc_cv_init_priority=yes],[libgcc_cv_init_priority=no])])
+if test $libgcc_cv_init_priority = yes; then
+  AC_DEFINE(HAVE_INIT_PRIORITY, 1,
+  [Define if the compiler supports init priority.])
+fi
+
 # Conditionalize the sfp-machine.h header for this target machine.
 if test -z "${sfp_machine_header}"; then
 	sfp_machine_header=$cpu_type/sfp-machine.h
Index: libgcc/config/i386/libgcc-bsd.ver
===================================================================
--- libgcc/config/i386/libgcc-bsd.ver	(revision 190625)
+++ libgcc/config/i386/libgcc-bsd.ver	(working copy)
@@ -1,4 +1,4 @@ 
-# Copyright (C) 2010 Free Software Foundation, Inc.
+# Copyright (C) 2010, 2012 Free Software Foundation, Inc.
 #
 # This file is part of GCC.
 #
@@ -106,3 +106,8 @@  GCC_4.6.0 {
   __trunctfxf2
   __unordtf2
 }
+
+GCC_4.8.0 {
+  __cpu_model
+  __cpu_indicator_init
+}
Index: libgcc/config/i386/t-cpuinfo
===================================================================
--- libgcc/config/i386/t-cpuinfo	(revision 0)
+++ libgcc/config/i386/t-cpuinfo	(revision 0)
@@ -0,0 +1 @@ 
+LIB2ADD += $(srcdir)/config/i386/cpuinfo.c
Index: libgcc/config/i386/cpuinfo.c
===================================================================
--- libgcc/config/i386/cpuinfo.c	(revision 0)
+++ libgcc/config/i386/cpuinfo.c	(revision 0)
@@ -0,0 +1,346 @@ 
+/* Get CPU type and Features for x86 processors.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+   Contributed by Sriraman Tallam (tmsriram@google.com)
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>. */
+
+#include "cpuid.h"
+#include "tsystem.h"
+#include "auto-target.h"
+
+#ifdef HAVE_INIT_PRIORITY
+#define CONSTRUCTOR_PRIORITY (101)
+#else
+#define CONSTRUCTOR_PRIORITY
+#endif
+
+int __cpu_indicator_init (void)
+  __attribute__ ((constructor CONSTRUCTOR_PRIORITY));
+
+enum vendor_signatures
+{
+  SIG_INTEL =	0x756e6547 /* Genu */,
+  SIG_AMD =	0x68747541 /* Auth */
+};
+
+/* Processor Vendor and Models. */
+
+enum processor_vendor
+{
+  VENDOR_INTEL = 1,
+  VENDOR_AMD,
+  VENDOR_OTHER,
+  VENDOR_MAX
+};
+
+enum processor_types
+{
+  INTEL_ATOM = 1,
+  INTEL_CORE2,
+  INTEL_COREI7,
+  AMDFAM10H,
+  AMDFAM15H,
+  CPU_TYPE_MAX
+};
+
+enum processor_subtypes
+{
+  INTEL_COREI7_NEHALEM = 1,
+  INTEL_COREI7_WESTMERE,
+  INTEL_COREI7_SANDYBRIDGE,
+  AMDFAM10H_BARCELONA,
+  AMDFAM10H_SHANGHAI,
+  AMDFAM10H_ISTANBUL,
+  AMDFAM15H_BDVER1,
+  AMDFAM15H_BDVER2,
+  CPU_SUBTYPE_MAX
+};
+
+/* ISA Features supported. */
+
+enum processor_features
+{
+  FEATURE_CMOV = 0,
+  FEATURE_MMX,
+  FEATURE_POPCNT,
+  FEATURE_SSE,
+  FEATURE_SSE2,
+  FEATURE_SSE3,
+  FEATURE_SSSE3,
+  FEATURE_SSE4_1,
+  FEATURE_SSE4_2,
+  FEATURE_AVX,
+  FEATURE_AVX2
+};
+
+struct __processor_model
+{
+  unsigned int __cpu_vendor;
+  unsigned int __cpu_type;
+  unsigned int __cpu_subtype;
+  unsigned int __cpu_features[1];
+} __cpu_model;
+
+
+/* Get the specific type of AMD CPU.  */
+
+static void
+get_amd_cpu (unsigned int family, unsigned int model)
+{
+  switch (family)
+    {
+    /* AMD Family 10h.  */
+    case 0x10:
+      switch (model)
+	{
+	case 0x2:
+	  /* Barcelona.  */
+	  __cpu_model.__cpu_type = AMDFAM10H;
+	  __cpu_model.__cpu_subtype = AMDFAM10H_BARCELONA;
+	  break;
+	case 0x4:
+	  /* Shanghai.  */
+	  __cpu_model.__cpu_type = AMDFAM10H;
+	  __cpu_model.__cpu_subtype = AMDFAM10H_SHANGHAI;
+	  break;
+	case 0x8:
+	  /* Istanbul.  */
+	  __cpu_model.__cpu_type = AMDFAM10H;
+	  __cpu_model.__cpu_subtype = AMDFAM10H_ISTANBUL;
+	  break;
+	default:
+	  break;
+	}
+      break;
+    /* AMD Family 15h.  */
+    case 0x15:
+      __cpu_model.__cpu_type = AMDFAM15H;
+      /* Bulldozer version 1.  */
+      if ( model <= 0xf)
+	__cpu_model.__cpu_subtype = AMDFAM15H_BDVER1;
+      /* Bulldozer version 2.  */
+      if (model >= 0x10 && model <= 0x1f)
+	__cpu_model.__cpu_subtype = AMDFAM15H_BDVER2;
+      break;
+    default:
+      break;
+    }
+}
+
+/* Get the specific type of Intel CPU.  */
+
+static void
+get_intel_cpu (unsigned int family, unsigned int model, unsigned int brand_id)
+{
+  /* Parse family and model only if brand ID is 0. */
+  if (brand_id == 0)
+    {
+      switch (family)
+	{
+	case 0x5:
+	  /* Pentium.  */
+	  break;
+	case 0x6:
+	  switch (model)
+	    {
+	    case 0x1c:
+	    case 0x26:
+	      /* Atom.  */
+	      __cpu_model.__cpu_type = INTEL_ATOM;
+	      break;
+	    case 0x1a:
+	    case 0x1e:
+	    case 0x1f:
+	    case 0x2e:
+	      /* Nehalem.  */
+	      __cpu_model.__cpu_type = INTEL_COREI7;
+	      __cpu_model.__cpu_subtype = INTEL_COREI7_NEHALEM;
+	      break;
+	    case 0x25:
+	    case 0x2c:
+	    case 0x2f:
+	      /* Westmere.  */
+	      __cpu_model.__cpu_type = INTEL_COREI7;
+	      __cpu_model.__cpu_subtype = INTEL_COREI7_WESTMERE;
+	      break;
+	    case 0x2a:
+	      /* Sandy Bridge.  */
+	      __cpu_model.__cpu_type = INTEL_COREI7;
+	      __cpu_model.__cpu_subtype = INTEL_COREI7_SANDYBRIDGE;
+	      break;
+	    case 0x17:
+	    case 0x1d:
+	      /* Penryn.  */
+	    case 0x0f:
+	      /* Merom.  */
+	      __cpu_model.__cpu_type = INTEL_CORE2;
+	      break;
+	    default:
+	      break;
+	    }
+	  break;
+	default:
+	  /* We have no idea.  */
+	  break;
+	}
+    }
+}	             	
+
+/* ECX and EDX are output of CPUID at level one.  MAX_CPUID_LEVEL is
+   the max possible level of CPUID insn.  */
+static void
+get_available_features (unsigned int ecx, unsigned int edx,
+			int max_cpuid_level)
+{
+  unsigned int features = 0;
+
+  if (edx & bit_CMOV)
+    features |= (1 << FEATURE_CMOV);
+  if (edx & bit_MMX)
+    features |= (1 << FEATURE_MMX);
+  if (edx & bit_SSE)
+    features |= (1 << FEATURE_SSE);
+  if (edx & bit_SSE2)
+    features |= (1 << FEATURE_SSE2);
+  if (ecx & bit_POPCNT)
+    features |= (1 << FEATURE_POPCNT);
+  if (ecx & bit_SSE3)
+    features |= (1 << FEATURE_SSE3);
+  if (ecx & bit_SSSE3)
+    features |= (1 << FEATURE_SSSE3);
+  if (ecx & bit_SSE4_1)
+    features |= (1 << FEATURE_SSE4_1);
+  if (ecx & bit_SSE4_2)
+    features |= (1 << FEATURE_SSE4_2);
+  if (ecx & bit_AVX)
+    features |= (1 << FEATURE_AVX);
+
+  /* Get Advanced Features at level 7 (eax = 7, ecx = 0). */
+  if (max_cpuid_level >= 7)
+    {
+      unsigned int eax, ebx, ecx, edx;
+      __cpuid_count (7, 0, eax, ebx, ecx, edx);
+      if (ebx & bit_AVX2)
+	features |= (1 << FEATURE_AVX2);
+    }
+
+  __cpu_model.__cpu_features[0] = features;
+}
+
+/* A noinline function calling __get_cpuid. Having many calls to
+   cpuid in one function in 32-bit mode causes GCC to complain:
+   "can't find a register in class CLOBBERED_REGS".  This is
+   related to PR rtl-optimization 44174. */
+
+static int __attribute__ ((noinline))
+__get_cpuid_output (unsigned int __level,
+		    unsigned int *__eax, unsigned int *__ebx,
+		    unsigned int *__ecx, unsigned int *__edx)
+{
+  return __get_cpuid (__level, __eax, __ebx, __ecx, __edx);
+}
+
+
+/* A constructor function that is sets __cpu_model and __cpu_features with
+   the right values.  This needs to run only once.  This constructor is
+   given the highest priority and it should run before constructors without
+   the priority set.  However, it still runs after ifunc initializers and
+   needs to be called explicitly there.  */
+
+int __attribute__ ((constructor CONSTRUCTOR_PRIORITY))
+__cpu_indicator_init (void)
+{
+  unsigned int eax, ebx, ecx, edx;
+
+  int max_level = 5;
+  unsigned int vendor;
+  unsigned int model, family, brand_id;
+  unsigned int extended_model, extended_family;
+
+  /* This function needs to run just once.  */
+  if (__cpu_model.__cpu_vendor)
+    return 0;
+
+  /* Assume cpuid insn present. Run in level 0 to get vendor id. */
+  if (!__get_cpuid_output (0, &eax, &ebx, &ecx, &edx))
+    {
+      __cpu_model.__cpu_vendor = VENDOR_OTHER;
+      return -1;
+    }
+
+  vendor = ebx;
+  max_level = eax;
+
+  if (max_level < 1)
+    {
+      __cpu_model.__cpu_vendor = VENDOR_OTHER;
+      return -1;
+    }
+
+  if (!__get_cpuid_output (1, &eax, &ebx, &ecx, &edx))
+    {
+      __cpu_model.__cpu_vendor = VENDOR_OTHER;
+      return -1;
+    }
+
+  model = (eax >> 4) & 0x0f;
+  family = (eax >> 8) & 0x0f;
+  brand_id = ebx & 0xff;
+  extended_model = (eax >> 12) & 0xf0;
+  extended_family = (eax >> 20) & 0xff;
+
+  if (vendor == SIG_INTEL)
+    {
+      /* Adjust model and family for Intel CPUS. */
+      if (family == 0x0f)
+	{
+	  family += extended_family;
+	  model += extended_model;
+	}
+      else if (family == 0x06)
+	model += extended_model;
+
+      /* Get CPU type.  */
+      get_intel_cpu (family, model, brand_id);
+      /* Find available features. */
+      get_available_features (ecx, edx, max_level);
+      __cpu_model.__cpu_vendor = VENDOR_INTEL;
+    }
+  else if (vendor == SIG_AMD)
+    {
+      /* Adjust model and family for AMD CPUS. */
+      if (family == 0x0f)
+	{
+	  family += extended_family;
+	  model += (extended_model << 4);
+	}
+
+      /* Get CPU type.  */
+      get_amd_cpu (family, model);
+      /* Find available features. */
+      get_available_features (ecx, edx, max_level);
+      __cpu_model.__cpu_vendor = VENDOR_AMD;
+    }
+  else
+    __cpu_model.__cpu_vendor = VENDOR_OTHER;
+
+  gcc_assert (__cpu_model.__cpu_vendor < VENDOR_MAX);
+  gcc_assert (__cpu_model.__cpu_type < CPU_TYPE_MAX);
+  gcc_assert (__cpu_model.__cpu_subtype < CPU_SUBTYPE_MAX);
+
+  return 0;
+}
Index: libgcc/config/i386/libgcc-sol2.ver
===================================================================
--- libgcc/config/i386/libgcc-sol2.ver	(revision 190625)
+++ libgcc/config/i386/libgcc-sol2.ver	(working copy)
@@ -1,4 +1,4 @@ 
-# Copyright (C) 2010 Free Software Foundation, Inc.
+# Copyright (C) 2010, 2012 Free Software Foundation, Inc.
 #
 # This file is part of GCC.
 #
@@ -106,3 +106,8 @@  GCC_4.5.0 {
   __trunctfxf2
   __unordtf2
 }
+
+GCC_4.8.0 {
+  __cpu_model
+  __cpu_indicator_init
+}
Index: libgcc/config/i386/libgcc-glibc.ver
===================================================================
--- libgcc/config/i386/libgcc-glibc.ver	(revision 190625)
+++ libgcc/config/i386/libgcc-glibc.ver	(working copy)
@@ -1,4 +1,4 @@ 
-# Copyright (C) 2008, 2010 Free Software Foundation, Inc.
+# Copyright (C) 2008, 2010, 2012 Free Software Foundation, Inc.
 #
 # This file is part of GCC.
 #
@@ -147,6 +147,11 @@  GCC_4.3.0 {
   __trunctfxf2
   __unordtf2
 }
+
+GCC_4.8.0 {
+  __cpu_model
+  __cpu_indicator_init
+}
 %else
 GCC_4.4.0 {
   __addtf3
@@ -183,4 +188,9 @@  GCC_4.4.0 {
 GCC_4.5.0 {
   __extendxftf2
 }
+
+GCC_4.8.0 {
+  __cpu_model
+  __cpu_indicator_init
+}
 %endif
Index: libgcc/configure
===================================================================
--- libgcc/configure	(revision 190625)
+++ libgcc/configure	(working copy)
@@ -2113,7 +2113,9 @@  ac_config_sub="$SHELL $ac_aux_dir/config.sub"  # P
 ac_configure="$SHELL $ac_aux_dir/configure"  # Please don't use this var.
 
 
+ac_config_headers="$ac_config_headers auto-target.h:config.in"
 
+
 # Check whether --enable-shared was given.
 if test "${enable_shared+set}" = set; then :
   enableval=$enable_shared;
@@ -4435,6 +4437,38 @@  if test "$enable_tls $gcc_cv_use_emutls" = "yes ye
 fi
 
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for init priority support" >&5
+$as_echo_n "checking for init priority support... " >&6; }
+if test "${libgcc_cv_init_priority+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+int
+main ()
+{
+void ip (void) __attribute__ ((constructor (1)));
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  libgcc_cv_init_priority=yes
+else
+  libgcc_cv_init_priority=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libgcc_cv_init_priority" >&5
+$as_echo "$libgcc_cv_init_priority" >&6; }
+if test $libgcc_cv_init_priority = yes; then
+
+$as_echo "#define HAVE_INIT_PRIORITY 1" >>confdefs.h
+
+fi
+
 # Conditionalize the sfp-machine.h header for this target machine.
 if test -z "${sfp_machine_header}"; then
 	sfp_machine_header=$cpu_type/sfp-machine.h
@@ -4581,44 +4615,8 @@  test "x$prefix" = xNONE && prefix=$ac_default_pref
 # Let make expand exec_prefix.
 test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'
 
-# Transform confdefs.h into DEFS.
-# Protect against shell expansion while executing Makefile rules.
-# Protect against Makefile macro expansion.
-#
-# If the first sed substitution is executed (which looks for macros that
-# take arguments), then branch to the quote section.  Otherwise,
-# look for a macro that doesn't take arguments.
-ac_script='
-:mline
-/\\$/{
- N
- s,\\\n,,
- b mline
-}
-t clear
-:clear
-s/^[	 ]*#[	 ]*define[	 ][	 ]*\([^	 (][^	 (]*([^)]*)\)[	 ]*\(.*\)/-D\1=\2/g
-t quote
-s/^[	 ]*#[	 ]*define[	 ][	 ]*\([^	 ][^	 ]*\)[	 ]*\(.*\)/-D\1=\2/g
-t quote
-b any
-:quote
-s/[	 `~#$^&*(){}\\|;'\''"<>?]/\\&/g
-s/\[/\\&/g
-s/\]/\\&/g
-s/\$/$$/g
-H
-:any
-${
-	g
-	s/^\n//
-	s/\n/ /g
-	p
-}
-'
-DEFS=`sed -n "$ac_script" confdefs.h`
+DEFS=-DHAVE_CONFIG_H
 
-
 ac_libobjs=
 ac_ltlibobjs=
 for ac_i in : $LIBOBJS; do test "x$ac_i" = x: && continue
@@ -5061,11 +5059,15 @@  case $ac_config_files in *"
 "*) set x $ac_config_files; shift; ac_config_files=$*;;
 esac
 
+case $ac_config_headers in *"
+"*) set x $ac_config_headers; shift; ac_config_headers=$*;;
+esac
 
 
 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 # Files that config.status was made for.
 config_files="$ac_config_files"
+config_headers="$ac_config_headers"
 config_links="$ac_config_links"
 config_commands="$ac_config_commands"
 
@@ -5087,10 +5089,15 @@  Usage: $0 [OPTION]... [TAG]...
       --recheck    update $as_me by reconfiguring in the same conditions
       --file=FILE[:TEMPLATE]
                    instantiate the configuration file FILE
+      --header=FILE[:TEMPLATE]
+                   instantiate the configuration header FILE
 
 Configuration files:
 $config_files
 
+Configuration headers:
+$config_headers
+
 Configuration links:
 $config_links
 
@@ -5152,7 +5159,18 @@  do
     esac
     as_fn_append CONFIG_FILES " '$ac_optarg'"
     ac_need_defaults=false;;
-  --he | --h |  --help | --hel | -h )
+  --header | --heade | --head | --hea )
+    $ac_shift
+    case $ac_optarg in
+    *\'*) ac_optarg=`$as_echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"` ;;
+    esac
+    as_fn_append CONFIG_HEADERS " '$ac_optarg'"
+    ac_need_defaults=false;;
+  --he | --h)
+    # Conflict between --help and --header
+    as_fn_error "ambiguous option: \`$1'
+Try \`$0 --help' for more information.";;
+  --help | --hel | -h )
     $as_echo "$ac_cs_usage"; exit ;;
   -q | -quiet | --quiet | --quie | --qui | --qu | --q \
   | -silent | --silent | --silen | --sile | --sil | --si | --s)
@@ -5221,6 +5239,7 @@  cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 for ac_config_target in $ac_config_targets
 do
   case $ac_config_target in
+    "auto-target.h") CONFIG_HEADERS="$CONFIG_HEADERS auto-target.h:config.in" ;;
     "enable-execute-stack.c") CONFIG_LINKS="$CONFIG_LINKS enable-execute-stack.c:$enable_execute_stack" ;;
     "unwind.h") CONFIG_LINKS="$CONFIG_LINKS unwind.h:$unwind_header" ;;
     "md-unwind-support.h") CONFIG_LINKS="$CONFIG_LINKS md-unwind-support.h:config/$md_unwind_header" ;;
@@ -5240,6 +5259,7 @@  done
 # bizarre bug on SunOS 4.1.3.
 if $ac_need_defaults; then
   test "${CONFIG_FILES+set}" = set || CONFIG_FILES=$config_files
+  test "${CONFIG_HEADERS+set}" = set || CONFIG_HEADERS=$config_headers
   test "${CONFIG_LINKS+set}" = set || CONFIG_LINKS=$config_links
   test "${CONFIG_COMMANDS+set}" = set || CONFIG_COMMANDS=$config_commands
 fi
@@ -5419,8 +5439,116 @@  fi
 cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 fi # test -n "$CONFIG_FILES"
 
+# Set up the scripts for CONFIG_HEADERS section.
+# No need to generate them if there are no CONFIG_HEADERS.
+# This happens for instance with `./config.status Makefile'.
+if test -n "$CONFIG_HEADERS"; then
+cat >"$tmp/defines.awk" <<\_ACAWK ||
+BEGIN {
+_ACEOF
 
-eval set X "  :F $CONFIG_FILES    :L $CONFIG_LINKS  :C $CONFIG_COMMANDS"
+# Transform confdefs.h into an awk script `defines.awk', embedded as
+# here-document in config.status, that substitutes the proper values into
+# config.h.in to produce config.h.
+
+# Create a delimiter string that does not exist in confdefs.h, to ease
+# handling of long lines.
+ac_delim='%!_!# '
+for ac_last_try in false false :; do
+  ac_t=`sed -n "/$ac_delim/p" confdefs.h`
+  if test -z "$ac_t"; then
+    break
+  elif $ac_last_try; then
+    as_fn_error "could not make $CONFIG_HEADERS" "$LINENO" 5
+  else
+    ac_delim="$ac_delim!$ac_delim _$ac_delim!! "
+  fi
+done
+
+# For the awk script, D is an array of macro values keyed by name,
+# likewise P contains macro parameters if any.  Preserve backslash
+# newline sequences.
+
+ac_word_re=[_$as_cr_Letters][_$as_cr_alnum]*
+sed -n '
+s/.\{148\}/&'"$ac_delim"'/g
+t rset
+:rset
+s/^[	 ]*#[	 ]*define[	 ][	 ]*/ /
+t def
+d
+:def
+s/\\$//
+t bsnl
+s/["\\]/\\&/g
+s/^ \('"$ac_word_re"'\)\(([^()]*)\)[	 ]*\(.*\)/P["\1"]="\2"\
+D["\1"]=" \3"/p
+s/^ \('"$ac_word_re"'\)[	 ]*\(.*\)/D["\1"]=" \2"/p
+d
+:bsnl
+s/["\\]/\\&/g
+s/^ \('"$ac_word_re"'\)\(([^()]*)\)[	 ]*\(.*\)/P["\1"]="\2"\
+D["\1"]=" \3\\\\\\n"\\/p
+t cont
+s/^ \('"$ac_word_re"'\)[	 ]*\(.*\)/D["\1"]=" \2\\\\\\n"\\/p
+t cont
+d
+:cont
+n
+s/.\{148\}/&'"$ac_delim"'/g
+t clear
+:clear
+s/\\$//
+t bsnlc
+s/["\\]/\\&/g; s/^/"/; s/$/"/p
+d
+:bsnlc
+s/["\\]/\\&/g; s/^/"/; s/$/\\\\\\n"\\/p
+b cont
+' <confdefs.h | sed '
+s/'"$ac_delim"'/"\\\
+"/g' >>$CONFIG_STATUS || ac_write_fail=1
+
+cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
+  for (key in D) D_is_set[key] = 1
+  FS = ""
+}
+/^[\t ]*#[\t ]*(define|undef)[\t ]+$ac_word_re([\t (]|\$)/ {
+  line = \$ 0
+  split(line, arg, " ")
+  if (arg[1] == "#") {
+    defundef = arg[2]
+    mac1 = arg[3]
+  } else {
+    defundef = substr(arg[1], 2)
+    mac1 = arg[2]
+  }
+  split(mac1, mac2, "(") #)
+  macro = mac2[1]
+  prefix = substr(line, 1, index(line, defundef) - 1)
+  if (D_is_set[macro]) {
+    # Preserve the white space surrounding the "#".
+    print prefix "define", macro P[macro] D[macro]
+    next
+  } else {
+    # Replace #undef with comments.  This is necessary, for example,
+    # in the case of _POSIX_SOURCE, which is predefined and required
+    # on some systems where configure will not decide to define it.
+    if (defundef == "undef") {
+      print "/*", prefix defundef, macro, "*/"
+      next
+    }
+  }
+}
+{ print }
+_ACAWK
+_ACEOF
+cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
+  as_fn_error "could not setup config headers machinery" "$LINENO" 5
+fi # test -n "$CONFIG_HEADERS"
+
+
+eval set X "  :F $CONFIG_FILES  :H $CONFIG_HEADERS  :L $CONFIG_LINKS  :C $CONFIG_COMMANDS"
 shift
 for ac_tag
 do
@@ -5632,7 +5760,30 @@  which seems to be undefined.  Please make sure it
   esac \
   || as_fn_error "could not create $ac_file" "$LINENO" 5
  ;;
-
+  :H)
+  #
+  # CONFIG_HEADER
+  #
+  if test x"$ac_file" != x-; then
+    {
+      $as_echo "/* $configure_input  */" \
+      && eval '$AWK -f "$tmp/defines.awk"' "$ac_file_inputs"
+    } >"$tmp/config.h" \
+      || as_fn_error "could not create $ac_file" "$LINENO" 5
+    if diff "$ac_file" "$tmp/config.h" >/dev/null 2>&1; then
+      { $as_echo "$as_me:${as_lineno-$LINENO}: $ac_file is unchanged" >&5
+$as_echo "$as_me: $ac_file is unchanged" >&6;}
+    else
+      rm -f "$ac_file"
+      mv "$tmp/config.h" "$ac_file" \
+	|| as_fn_error "could not create $ac_file" "$LINENO" 5
+    fi
+  else
+    $as_echo "/* $configure_input  */" \
+      && eval '$AWK -f "$tmp/defines.awk"' "$ac_file_inputs" \
+      || as_fn_error "could not create -" "$LINENO" 5
+  fi
+ ;;
   :L)
   #
   # CONFIG_LINK
Index: libgcc/ChangeLog.google-4_7
===================================================================
--- libgcc/ChangeLog.google-4_7	(revision 190625)
+++ libgcc/ChangeLog.google-4_7	(working copy)
@@ -1,3 +1,62 @@ 
+2012-08-23  Sriraman Tallam  <tmsriram@google.com>
+
+	Backport r186789,r186795,r186855,r187365,r188417
+
+	r186789:
+	2012-04-24  Sriraman Tallam  <tmsriram@google.com>
+
+		* libgcc/config/i386/i386-cpuinfo.c: New file.
+		* libgcc/config/i386/t-cpuinfo: New file.
+		* libgcc/config.host: Include t-cpuinfo.
+		* libgcc/config/i386/libgcc-glibc.ver: Version symbol __cpu_model.
+
+	r186795:
+	2012-04-24  Sriraman Tallam  <tmsriram@google.com>
+		* libgcc/config/i386/i386-cpuinfo.c: Set __cpu_vendor always.
+
+	r186855:
+	2012-04-25  Sriraman Tallam  <tmsriram@google.com>
+
+		* config/i386/i386-cpuinfo.c (FEATURE_AVX2): New enum value.
+		(get_available_features): New argument. Check for AVX2.
+		(__cpu_indicator_init): Modify call to get_available_features.
+
+	r187365:
+	2012-05-10  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
+
+		* config.host (i[34567]86-*-linux*, x86_64-*-linux*)
+		(i[34567]86-*-kfreebsd*-gnu, x86_64-*-kfreebsd*-gnu)
+		(i[34567]86-*-knetbsd*-gnu, i[34567]86-*-gnu*): Move
+		i386/t-cpuinfo ...
+		(i[34567]86-*-*, x86_64-*-*): ... here.
+
+		* config/i386/libgcc-bsd.ver (GCC_4.8.0): New version.
+		* config/i386/libgcc-sol2.ver (GCC_4.8.0): New version.
+
+		* config/i386/i386-cpuinfo.c: Rename to ...
+		* config/i386/cpuinfo.c: ... this.
+		* config/i386/t-cpuinfo (LIB2ADD): Reflect this.
+
+		* configure.ac (AC_CONFIG_HEADER): Call for auto-target.h.
+		(libgcc_cv_init_priority): New test.
+		* configure: Regenerate.
+		* config.in: New file.
+		* Makefile.in (clean): Rename config.h to auto-target.h.
+		(config.h): Likewise.
+		(stamp-h): Likewise.
+
+		* config/i386/cpuinfo.c (auto-target.h): Include.
+		(CONSTRUCTOR_PRIORITY): Define.
+		(__cpu_indicator_init): Use it.
+
+	r188417:
+	2012-06-11  Sriraman Tallam  <tmsriram@google.com>
+
+		* config/i386/libgcc-bsd.ver: Version symbol __cpu_indicator_init.
+		* config/i386/libgcc-sol2.ver: Ditto.
+		* config/i386/libgcc-glibc.ver: Ditto.
+	
+
 2012-08-13  Chris Manghane  <cmang@google.com>
 
 	Backport r190247 from google/main

Property changes on: gcc
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk/gcc:r186789,186795,186855,187365,188417

Index: gcc/doc/extend.texi
===================================================================
--- gcc/doc/extend.texi	(revision 190625)
+++ gcc/doc/extend.texi	(working copy)
@@ -9458,6 +9458,147 @@  Similar to @code{__builtin_huge_val}, except the r
 @findex __builtin_huge_valq
 @end table
 
+The following built-in functions are always available and can be used to
+check the target platform type.
+
+@deftypefn {Built-in Function} void __builtin_cpu_init (void)
+This function runs the CPU detection code to check the type of CPU and the
+features supported.  This builtin needs to be invoked along with the builtins
+to check CPU type and features, @code{__builtin_cpu_is} and
+@code{__builtin_cpu_supports}, only when used in a function that will be
+executed before any constructors are called.  The CPU detection code is
+automatically executed in a very high priority constructor.
+
+For example, this function has to be used in @code{ifunc} resolvers which
+check for CPU type using the builtins @code{__builtin_cpu_is}
+and @code{__builtin_cpu_supports}, or in constructors on targets which
+don't support constructor priority.
+@smallexample
+
+static void (*resolve_memcpy (void)) (void)
+@{
+  // ifunc resolvers fire before constructors, explicitly call the init
+  // function.
+  __builtin_cpu_init ();
+  if (__builtin_cpu_supports ("ssse3"))
+    return ssse3_memcpy; // super fast memcpy with ssse3 instructions.
+  else
+    return default_memcpy;
+@}
+
+void *memcpy (void *, const void *, size_t)
+     __attribute__ ((ifunc ("resolve_memcpy")));
+@end smallexample
+
+@end deftypefn
+
+@deftypefn {Built-in Function} int __builtin_cpu_is (const char *@var{cpuname})
+This function returns a positive integer if the runtime cpu is of type @var{cpuname}
+ and returns @code{0} otherwise. The following cpu names can be detected:
+
+@table @samp
+@item intel
+Intel CPU.
+
+@item atom
+Intel ATOM CPU.
+
+@item core2
+Intel Core2 CPU.
+
+@item corei7
+Intel Corei7 CPU.
+
+@item nehalem
+Intel Corei7 Nehalem CPU.
+
+@item westmere
+Intel Corei7 Westmere CPU.
+
+@item sandybridge
+Intel Corei7 Sandybridge CPU.
+
+@item amd
+AMD CPU.
+
+@item amdfam10h
+AMD family 10h CPU.
+
+@item barcelona
+AMD family 10h Barcelona CPU.
+
+@item shanghai
+AMD family 10h Shanghai CPU.
+
+@item istanbul
+AMD family 10h Istanbul CPU.
+
+@item amdfam15h
+AMD family 15h CPU.
+
+@item bdver1
+AMD family 15h Bulldozer version 1.
+
+@item bdver2
+AMD family 15h Bulldozer version 2.
+@end table
+
+Here is an example:
+@smallexample
+if (__builtin_cpu_is ("corei7"))
+  @{
+     do_corei7 (); //Corei7 specific implementation.
+  @}
+else
+  @{
+     do_generic (); //Generic implementation.
+  @}
+@end smallexample
+@end deftypefn
+
+@deftypefn {Built-in Function} int __builtin_cpu_supports (const char *@var{feature})
+This function returns a postive integer if the runtime cpu supports @var{feature}
+ and returns @code{0} otherwise. The following features can be detected:
+
+@table @samp
+@item cmov
+CMOV instruction.
+@item mmx
+MMX instructions.
+@item popcnt
+POPCNT instruction.
+@item sse
+SSE instructions.
+@item sse2
+SSE2 instructions.
+@item sse3
+SSE3 instructions.
+@item ssse3
+SSSE3 instructions.
+@item sse4.1
+SSE4.1 instructions.
+@item sse4.2
+SSE4.2 instructions.
+@item avx
+AVX instructions.
+@item avx2
+AVX2 instructions.
+@end table
+
+Here is an example:
+@smallexample
+if (__builtin_cpu_supports ("popcnt"))
+  @{
+     asm("popcnt %1,%0" : "=r"(count) : "rm"(n) : "cc");
+  @}
+else
+  @{
+     count = generic_countbits (n); //generic implementation.
+  @}
+@end smallexample
+@end deftypefn
+
+
 The following built-in functions are made available by @option{-mmmx}.
 All of them generate the machine instruction that is part of the name.
 

Property changes on: gcc/testsuite/gcc.target/powerpc/ppc-round.c
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk/gcc/testsuite/gcc.target/powerpc/ppc-round.c:r186789,186795,186855,187365,188417

Index: gcc/testsuite/gcc.target/i386/builtin_target.c
===================================================================
--- gcc/testsuite/gcc.target/i386/builtin_target.c	(revision 190625)
+++ gcc/testsuite/gcc.target/i386/builtin_target.c	(working copy)
@@ -1,13 +1,229 @@ 
 /* This test checks if the __builtin_cpu_is and __builtin_cpu_supports calls
-   are recognized. */
+   are recognized.  It also independently uses CPUID to get cpu type and
+   features supported and checks if the builtins correctly identify the
+   platform.  The code to do the identification is adapted from
+   libgcc/config/i386/cpuinfo.c.  */
 
 /* { dg-do run } */
 
 #include <assert.h>
+#include "cpuid.h"
 
-int
-fn1 ()
+enum vendor_signatures
 {
+  SIG_INTEL =	0x756e6547 /* Genu */,
+  SIG_AMD =	0x68747541 /* Auth */
+};
+
+/* Check if the Intel CPU model and sub-model are identified.  */
+static void
+check_intel_cpu_model (unsigned int family, unsigned int model,
+		       unsigned int brand_id)
+{
+  /* Parse family and model only if brand ID is 0. */
+  if (brand_id == 0)
+    {
+      switch (family)
+	{
+	case 0x5:
+	  /* Pentium.  */
+	  break;
+	case 0x6:
+	  switch (model)
+	    {
+	    case 0x1c:
+	    case 0x26:
+	      /* Atom.  */
+	      assert (__builtin_cpu_is ("atom"));
+	      break;
+	    case 0x1a:
+	    case 0x1e:
+	    case 0x1f:
+	    case 0x2e:
+	      /* Nehalem.  */
+	      assert (__builtin_cpu_is ("corei7"));
+	      assert (__builtin_cpu_is ("nehalem"));
+	      break;
+	    case 0x25:
+	    case 0x2c:
+	    case 0x2f:
+	      /* Westmere.  */
+	      assert (__builtin_cpu_is ("corei7"));
+	      assert (__builtin_cpu_is ("westmere"));
+	      break;
+	    case 0x2a:
+	      /* Sandy Bridge.  */
+	      assert (__builtin_cpu_is ("corei7"));
+	      assert (__builtin_cpu_is ("sandybridge"));
+	      break;
+	    case 0x17:
+	    case 0x1d:
+	      /* Penryn.  */
+	    case 0x0f:
+	      /* Merom.  */
+	      assert (__builtin_cpu_is ("core2"));
+	      break;
+	    default:
+	      break;
+	    }
+	  break;
+	default:
+	  /* We have no idea.  */
+	  break;
+	}
+    }
+}
+
+/* Check if the AMD CPU model and sub-model are identified.  */
+static void
+check_amd_cpu_model (unsigned int family, unsigned int model)
+{
+  switch (family)
+    {
+    /* AMD Family 10h.  */
+    case 0x10:
+      switch (model)
+	{
+	case 0x2:
+	  /* Barcelona.  */
+	  assert (__builtin_cpu_is ("amdfam10h"));
+	  assert (__builtin_cpu_is ("barcelona"));
+	  break;
+	case 0x4:
+	  /* Shanghai.  */
+	  assert (__builtin_cpu_is ("amdfam10h"));
+	  assert (__builtin_cpu_is ("shanghai"));
+	  break;
+	case 0x8:
+	  /* Istanbul.  */
+	  assert (__builtin_cpu_is ("amdfam10h"));
+	  assert (__builtin_cpu_is ("istanbul"));
+	  break;
+	default:
+	  break;
+	}
+      break;
+    /* AMD Family 15h.  */
+    case 0x15:
+      assert (__builtin_cpu_is ("amdfam15h"));
+      /* Bulldozer version 1.  */
+      if ( model <= 0xf)
+	assert (__builtin_cpu_is ("bdver1"));
+      /* Bulldozer version 2.  */
+      if (model >= 0x10 && model <= 0x1f)
+	assert (__builtin_cpu_is ("bdver2"));
+      break;
+    default:
+      break;
+    }
+}
+
+/* Check if the ISA features are identified.  */
+static void
+check_features (unsigned int ecx, unsigned int edx,
+		int max_cpuid_level)
+{
+  if (edx & bit_CMOV)
+    assert (__builtin_cpu_supports ("cmov"));
+  if (edx & bit_MMX)
+    assert (__builtin_cpu_supports ("mmx"));
+  if (edx & bit_SSE)
+    assert (__builtin_cpu_supports ("sse"));
+  if (edx & bit_SSE2)
+    assert (__builtin_cpu_supports ("sse2"));
+  if (ecx & bit_POPCNT)
+    assert (__builtin_cpu_supports ("popcnt"));
+  if (ecx & bit_SSE3)
+    assert (__builtin_cpu_supports ("sse3"));
+  if (ecx & bit_SSSE3)
+    assert (__builtin_cpu_supports ("ssse3"));
+  if (ecx & bit_SSE4_1)
+    assert (__builtin_cpu_supports ("sse4.1"));
+  if (ecx & bit_SSE4_2)
+    assert (__builtin_cpu_supports ("sse4.2"));
+  if (ecx & bit_AVX)
+    assert (__builtin_cpu_supports ("avx"));
+
+  /* Get advanced features at level 7 (eax = 7, ecx = 0).  */
+  if (max_cpuid_level >= 7)
+    {
+      unsigned int eax, ebx, ecx, edx;
+      __cpuid_count (7, 0, eax, ebx, ecx, edx);
+      if (ebx & bit_AVX2)
+	assert (__builtin_cpu_supports ("avx2"));
+    }
+}
+
+static int __attribute__ ((noinline))
+__get_cpuid_output (unsigned int __level,
+		    unsigned int *__eax, unsigned int *__ebx,
+		    unsigned int *__ecx, unsigned int *__edx)
+{
+  return __get_cpuid (__level, __eax, __ebx, __ecx, __edx);
+}
+
+static int
+check_detailed ()
+{
+  unsigned int eax, ebx, ecx, edx;
+
+  int max_level; 
+  unsigned int vendor;
+  unsigned int model, family, brand_id;
+  unsigned int extended_model, extended_family;
+
+  /* Assume cpuid insn present. Run in level 0 to get vendor id. */
+  if (!__get_cpuid_output (0, &eax, &ebx, &ecx, &edx))
+    return 0;
+
+  vendor = ebx;
+  max_level = eax;
+
+  if (max_level < 1)
+    return 0;
+
+  if (!__get_cpuid_output (1, &eax, &ebx, &ecx, &edx))
+    return 0;
+
+  model = (eax >> 4) & 0x0f;
+  family = (eax >> 8) & 0x0f;
+  brand_id = ebx & 0xff;
+  extended_model = (eax >> 12) & 0xf0;
+  extended_family = (eax >> 20) & 0xff;
+
+  if (vendor == SIG_INTEL)
+    {
+      assert (__builtin_cpu_is ("intel"));
+      /* Adjust family and model for Intel CPUs.  */
+      if (family == 0x0f)
+	{
+	  family += extended_family;
+	  model += extended_model;
+	}
+      else if (family == 0x06)
+	model += extended_model;
+      check_intel_cpu_model (family, model, brand_id);
+      check_features (ecx, edx, max_level);
+    }
+  else if (vendor == SIG_AMD)
+    {
+      assert (__builtin_cpu_is ("amd"));
+      /* Adjust model and family for AMD CPUS. */
+      if (family == 0x0f)
+	{
+	  family += extended_family;
+	  model += (extended_model << 4);
+	}
+      check_amd_cpu_model (family, model);
+      check_features (ecx, edx, max_level);
+    }
+
+  return 0;
+}
+
+static int
+quick_check ()
+{
   /* Check CPU Features.  */
   assert (__builtin_cpu_supports ("cmov") >= 0);
 
@@ -29,6 +245,8 @@ 
 
   assert (__builtin_cpu_supports ("avx") >= 0);
 
+  assert (__builtin_cpu_supports ("avx2") >= 0);
+
   /* Check CPU type.  */
   assert (__builtin_cpu_is ("amd") >= 0);
 
@@ -66,5 +284,7 @@ 
 int main ()
 {
   __builtin_cpu_init ();
-  return fn1 ();
+  quick_check ();
+  check_detailed ();
+  return 0;
 }
Index: gcc/ChangeLog.google-4_7
===================================================================
--- gcc/ChangeLog.google-4_7	(revision 190625)
+++ gcc/ChangeLog.google-4_7	(working copy)
@@ -1,3 +1,57 @@ 
+2012-08-23  Sriraman Tallam  <tmsriram@google.com>
+
+	Backport r186789,r186795,r186855,r187365,r188417
+
+	r186789:
+	2012-04-24  Sriraman Tallam  <tmsriram@google.com>
+
+		* config/i386/i386.c (build_processor_model_struct): New function.
+		(make_var_decl): New function.
+		(fold_builtin_cpu): New function.
+		(ix86_fold_builtin): New function.
+		(make_cpu_type_builtin): New function.
+		(ix86_init_platform_type_builtins): New function.
+		(ix86_expand_builtin): Expand new builtins by folding them.
+		(ix86_init_builtins): Make new builtins to detect CPU type.
+		(TARGET_FOLD_BUILTIN): New macro.
+		(IX86_BUILTIN_CPU_INIT): New enum value.
+		(IX86_BUILTIN_CPU_IS): New enum value.
+		(IX86_BUILTIN_CPU_SUPPORTS): New enum value.
+		* config/i386/i386-builtin-types.def: New function type.
+		* testsuite/gcc.target/builtin_target.c: New testcase.
+		* doc/extend.texi: Document builtins.
+
+	r186795:
+	No changes.
+
+	r186855:
+	2012-04-25  Sriraman Tallam  <tmsriram@google.com>
+
+		* doc/extend.texi: Document avx2 support.
+		* config/i386/i386.c (fold_builtin_cpu): Add avx2.
+		* testsuite/gcc.target/i386/builtin_target.c: Check avx2.
+
+	r187365:
+	2012-05-10  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
+
+		* doc/extend.texi (X86 Built-in Functions, __builtin_cpu_init):
+		Document requirement to call in constructors.
+
+		* config/i386/i386.c: Update comments for i386-cpuinfo.c name
+		change.
+
+	r188417:
+	2012-06-11  Sriraman Tallam  <tmsriram@google.com>
+
+		* testsuite/gcc.target/i386/builtin_target.c (vendor_signatures): New enum.
+		(check_intel_cpu_model): New function.
+		(check_amd_cpu_model): New function.
+		(check_features): New function.
+		(__get_cpuid_output): New function.
+		(check_detailed): New function.
+		(fn1): Rename to quick_check.
+		(main): Update to call quick_check and call check_detailed.
+
 2012-08-22   Cary Coutant  <ccoutant@google.com>
 
 	* dwarf2out.c (should_move_die_to_comdat): A type definition
Index: gcc/config/i386/i386-builtin-types.def
===================================================================
--- gcc/config/i386/i386-builtin-types.def	(revision 190625)
+++ gcc/config/i386/i386-builtin-types.def	(working copy)
@@ -155,6 +155,7 @@  DEF_FUNCTION_TYPE (INT, V4SF)
 DEF_FUNCTION_TYPE (INT, V8QI)
 DEF_FUNCTION_TYPE (INT, V8SF)
 DEF_FUNCTION_TYPE (INT, V32QI)
+DEF_FUNCTION_TYPE (INT, PCCHAR)
 DEF_FUNCTION_TYPE (INT64, INT64)
 DEF_FUNCTION_TYPE (INT64, V2DF)
 DEF_FUNCTION_TYPE (INT64, V4SF)
Index: gcc/config/i386/i386.c
===================================================================
--- gcc/config/i386/i386.c	(revision 190625)
+++ gcc/config/i386/i386.c	(working copy)
@@ -25978,6 +25978,11 @@  enum ix86_builtins
   /* CFString built-in for darwin */
   IX86_BUILTIN_CFSTRING,
 
+  /* Builtins to get CPU type and supported features. */
+  IX86_BUILTIN_CPU_INIT,
+  IX86_BUILTIN_CPU_IS,
+  IX86_BUILTIN_CPU_SUPPORTS,
+
   IX86_BUILTIN_MAX
 };
 
@@ -27811,6 +27816,335 @@  ix86_slow_unaligned_vector_memop (void)
   return false;
 }
 
+/* This builds the processor_model struct type defined in
+   libgcc/config/i386/cpuinfo.c  */
+
+static tree
+build_processor_model_struct (void)
+{
+  const char *field_name[] = {"__cpu_vendor", "__cpu_type", "__cpu_subtype",
+			      "__cpu_features"};
+  tree field = NULL_TREE, field_chain = NULL_TREE;
+  int i;
+  tree type = make_node (RECORD_TYPE);
+
+  /* The first 3 fields are unsigned int.  */
+  for (i = 0; i < 3; ++i)
+    {
+      field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
+			  get_identifier (field_name[i]), unsigned_type_node);
+      if (field_chain != NULL_TREE)
+	DECL_CHAIN (field) = field_chain;
+      field_chain = field;
+    }
+
+  /* The last field is an array of unsigned integers of size one.  */
+  field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
+		      get_identifier (field_name[3]),
+		      build_array_type (unsigned_type_node,
+					build_index_type (size_one_node)));
+  if (field_chain != NULL_TREE)
+    DECL_CHAIN (field) = field_chain;
+  field_chain = field;
+
+  finish_builtin_struct (type, "__processor_model", field_chain, NULL_TREE);
+  return type;
+}
+
+/* Returns a extern, comdat VAR_DECL of type TYPE and name NAME. */
+
+static tree
+make_var_decl (tree type, const char *name)
+{
+  tree new_decl;
+
+  new_decl = build_decl (UNKNOWN_LOCATION,
+	                 VAR_DECL,
+	  	         get_identifier(name),
+		         type);
+
+  DECL_EXTERNAL (new_decl) = 1;
+  TREE_STATIC (new_decl) = 1;
+  TREE_PUBLIC (new_decl) = 1;
+  DECL_INITIAL (new_decl) = 0;
+  DECL_ARTIFICIAL (new_decl) = 0;
+  DECL_PRESERVE_P (new_decl) = 1;
+
+  make_decl_one_only (new_decl, DECL_ASSEMBLER_NAME (new_decl));
+  assemble_variable (new_decl, 0, 0, 0);
+
+  return new_decl;
+}
+
+/* FNDECL is a __builtin_cpu_is or a __builtin_cpu_supports call that is folded
+   into an integer defined in libgcc/config/i386/cpuinfo.c */
+
+static tree
+fold_builtin_cpu (tree fndecl, tree *args)
+{
+  unsigned int i;
+  enum ix86_builtins fn_code = (enum ix86_builtins)
+				DECL_FUNCTION_CODE (fndecl);
+  tree param_string_cst = NULL;
+
+  /* This is the order of bit-fields in __processor_features in cpuinfo.c */
+  enum processor_features
+  {
+    F_CMOV = 0,
+    F_MMX,
+    F_POPCNT,
+    F_SSE,
+    F_SSE2,
+    F_SSE3,
+    F_SSSE3,
+    F_SSE4_1,
+    F_SSE4_2,
+    F_AVX,
+    F_AVX2,
+    F_MAX
+  };
+
+  /* These are the values for vendor types and cpu types  and subtypes
+     in cpuinfo.c.  Cpu types and subtypes should be subtracted by
+     the corresponding start value.  */
+  enum processor_model
+  {
+    M_INTEL = 1,
+    M_AMD,
+    M_CPU_TYPE_START,
+    M_INTEL_ATOM,
+    M_INTEL_CORE2,
+    M_INTEL_COREI7,
+    M_AMDFAM10H,
+    M_AMDFAM15H,
+    M_CPU_SUBTYPE_START,
+    M_INTEL_COREI7_NEHALEM,
+    M_INTEL_COREI7_WESTMERE,
+    M_INTEL_COREI7_SANDYBRIDGE,
+    M_AMDFAM10H_BARCELONA,
+    M_AMDFAM10H_SHANGHAI,
+    M_AMDFAM10H_ISTANBUL,
+    M_AMDFAM15H_BDVER1,
+    M_AMDFAM15H_BDVER2
+  };
+
+  static struct _arch_names_table
+    {
+      const char *const name;
+      const enum processor_model model;
+    }
+  const arch_names_table[] =
+    {
+      {"amd", M_AMD},
+      {"intel", M_INTEL},
+      {"atom", M_INTEL_ATOM},
+      {"core2", M_INTEL_CORE2},
+      {"corei7", M_INTEL_COREI7},
+      {"nehalem", M_INTEL_COREI7_NEHALEM},
+      {"westmere", M_INTEL_COREI7_WESTMERE},
+      {"sandybridge", M_INTEL_COREI7_SANDYBRIDGE},
+      {"amdfam10h", M_AMDFAM10H},
+      {"barcelona", M_AMDFAM10H_BARCELONA},
+      {"shanghai", M_AMDFAM10H_SHANGHAI},
+      {"istanbul", M_AMDFAM10H_ISTANBUL},
+      {"amdfam15h", M_AMDFAM15H},
+      {"bdver1", M_AMDFAM15H_BDVER1},
+      {"bdver2", M_AMDFAM15H_BDVER2},
+    };
+
+  static struct _isa_names_table
+    {
+      const char *const name;
+      const enum processor_features feature;
+    }
+  const isa_names_table[] =
+    {
+      {"cmov",   F_CMOV},
+      {"mmx",    F_MMX},
+      {"popcnt", F_POPCNT},
+      {"sse",    F_SSE},
+      {"sse2",   F_SSE2},
+      {"sse3",   F_SSE3},
+      {"ssse3",  F_SSSE3},
+      {"sse4.1", F_SSE4_1},
+      {"sse4.2", F_SSE4_2},
+      {"avx",    F_AVX},
+      {"avx2",   F_AVX2}
+    };
+
+  static tree __processor_model_type = NULL_TREE;
+  static tree __cpu_model_var = NULL_TREE;
+
+  if (__processor_model_type == NULL_TREE)
+    __processor_model_type = build_processor_model_struct ();
+
+  if (__cpu_model_var == NULL_TREE)
+    __cpu_model_var = make_var_decl (__processor_model_type,
+				     "__cpu_model");
+
+  gcc_assert ((args != NULL) && (*args != NULL));
+
+  param_string_cst = *args;
+  while (param_string_cst
+	 && TREE_CODE (param_string_cst) !=  STRING_CST)
+    {
+      /* *args must be a expr that can contain other EXPRS leading to a
+	 STRING_CST.   */
+      if (!EXPR_P (param_string_cst))
+ 	{
+	  error ("Parameter to builtin must be a string constant or literal");
+	  return integer_zero_node;
+	}
+      param_string_cst = TREE_OPERAND (EXPR_CHECK (param_string_cst), 0);
+    }
+
+  gcc_assert (param_string_cst);
+
+  if (fn_code == IX86_BUILTIN_CPU_IS)
+    {
+      tree ref;
+      tree field;
+      unsigned int field_val = 0;
+      unsigned int NUM_ARCH_NAMES
+	= sizeof (arch_names_table) / sizeof (struct _arch_names_table);
+
+      for (i = 0; i < NUM_ARCH_NAMES; i++)
+	if (strcmp (arch_names_table[i].name,
+	    TREE_STRING_POINTER (param_string_cst)) == 0)
+	  break;
+
+      if (i == NUM_ARCH_NAMES)
+	{
+	  error ("Parameter to builtin not valid: %s",
+	         TREE_STRING_POINTER (param_string_cst));
+	  return integer_zero_node;
+	}
+
+      field = TYPE_FIELDS (__processor_model_type);
+      field_val = arch_names_table[i].model;
+
+      /* CPU types are stored in the next field.  */
+      if (field_val > M_CPU_TYPE_START
+	  && field_val < M_CPU_SUBTYPE_START)
+	{
+	  field = DECL_CHAIN (field);
+	  field_val -= M_CPU_TYPE_START;
+	}
+
+      /* CPU subtypes are stored in the next field.  */
+      if (field_val > M_CPU_SUBTYPE_START)
+	{
+	  field = DECL_CHAIN ( DECL_CHAIN (field));
+	  field_val -= M_CPU_SUBTYPE_START;
+	}
+
+      /* Get the appropriate field in __cpu_model.  */
+      ref =  build3 (COMPONENT_REF, TREE_TYPE (field), __cpu_model_var,
+		     field, NULL_TREE);
+
+      /* Check the value.  */
+      return build2 (EQ_EXPR, unsigned_type_node, ref,
+		     build_int_cstu (unsigned_type_node, field_val));
+    }
+  else if (fn_code == IX86_BUILTIN_CPU_SUPPORTS)
+    {
+      tree ref;
+      tree array_elt;
+      tree field;
+      unsigned int field_val = 0;
+      unsigned int NUM_ISA_NAMES
+	= sizeof (isa_names_table) / sizeof (struct _isa_names_table);
+
+      for (i = 0; i < NUM_ISA_NAMES; i++)
+	if (strcmp (isa_names_table[i].name,
+	    TREE_STRING_POINTER (param_string_cst)) == 0)
+	  break;
+
+      if (i == NUM_ISA_NAMES)
+	{
+	  error ("Parameter to builtin not valid: %s",
+	       	 TREE_STRING_POINTER (param_string_cst));
+	  return integer_zero_node;
+	}
+
+      field = TYPE_FIELDS (__processor_model_type);
+      /* Get the last field, which is __cpu_features.  */
+      while (DECL_CHAIN (field))
+        field = DECL_CHAIN (field);
+
+      /* Get the appropriate field: __cpu_model.__cpu_features  */
+      ref =  build3 (COMPONENT_REF, TREE_TYPE (field), __cpu_model_var,
+		     field, NULL_TREE);
+
+      /* Access the 0th element of __cpu_features array.  */
+      array_elt = build4 (ARRAY_REF, unsigned_type_node, ref,
+			  integer_zero_node, NULL_TREE, NULL_TREE);
+
+      field_val = (1 << isa_names_table[i].feature);
+      /* Return __cpu_model.__cpu_features[0] & field_val  */
+      return build2 (BIT_AND_EXPR, unsigned_type_node, array_elt,
+		     build_int_cstu (unsigned_type_node, field_val));
+    }
+  gcc_unreachable ();
+}
+
+static tree
+ix86_fold_builtin (tree fndecl, int n_args,
+		   tree *args, bool ignore ATTRIBUTE_UNUSED)
+{
+  if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
+    {
+      enum ix86_builtins fn_code = (enum ix86_builtins)
+				   DECL_FUNCTION_CODE (fndecl);
+      if (fn_code ==  IX86_BUILTIN_CPU_IS
+	  || fn_code == IX86_BUILTIN_CPU_SUPPORTS)
+	{
+	  gcc_assert (n_args == 1);
+          return fold_builtin_cpu (fndecl, args);
+	}
+    }
+
+  return NULL_TREE;
+}
+
+/* Make builtins to detect cpu type and features supported.  NAME is
+   the builtin name, CODE is the builtin code, and FTYPE is the function
+   type of the builtin.  */
+
+static void
+make_cpu_type_builtin (const char* name, int code,
+		       enum ix86_builtin_func_type ftype, bool is_const)
+{
+  tree decl;
+  tree type;
+
+  type = ix86_get_builtin_func_type (ftype);
+  decl = add_builtin_function (name, type, code, BUILT_IN_MD,
+			       NULL, NULL_TREE);
+  gcc_assert (decl != NULL_TREE);
+  ix86_builtins[(int) code] = decl;
+  TREE_READONLY (decl) = is_const;
+}
+
+/* Make builtins to get CPU type and features supported.  The created
+   builtins are :
+
+   __builtin_cpu_init (), to detect cpu type and features,
+   __builtin_cpu_is ("<CPUNAME>"), to check if cpu is of type <CPUNAME>,
+   __builtin_cpu_supports ("<FEATURE>"), to check if cpu supports <FEATURE>
+   */
+
+static void
+ix86_init_platform_type_builtins (void)
+{
+  make_cpu_type_builtin ("__builtin_cpu_init", IX86_BUILTIN_CPU_INIT,
+			 INT_FTYPE_VOID, false);
+  make_cpu_type_builtin ("__builtin_cpu_is", IX86_BUILTIN_CPU_IS,
+			 INT_FTYPE_PCCHAR, true);
+  make_cpu_type_builtin ("__builtin_cpu_supports", IX86_BUILTIN_CPU_SUPPORTS,
+			 INT_FTYPE_PCCHAR, true);
+}
+
 /* Internal method for ix86_init_builtins.  */
 
 static void
@@ -27894,6 +28228,9 @@  ix86_init_builtins (void)
 
   ix86_init_builtin_types ();
 
+  /* Builtins to get CPU type and features. */
+  ix86_init_platform_type_builtins ();
+
   /* TFmode support builtins.  */
   def_builtin_const (0, "__builtin_infq",
 		     FLOAT128_FTYPE_VOID, IX86_BUILTIN_INFQ);
@@ -29512,6 +29849,28 @@  ix86_expand_builtin (tree exp, rtx target, rtx sub
   enum machine_mode mode0, mode1, mode2, mode3, mode4;
   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
 
+  /* For CPU builtins that can be folded, fold first and expand the fold.  */
+  switch (fcode)
+    {
+    case IX86_BUILTIN_CPU_INIT:
+      {
+	/* Make it call __cpu_indicator_init in libgcc. */
+	tree call_expr, fndecl, type;
+        type = build_function_type_list (integer_type_node, NULL_TREE); 
+	fndecl = build_fn_decl ("__cpu_indicator_init", type);
+	call_expr = build_call_expr (fndecl, 0); 
+	return expand_expr (call_expr, target, mode, EXPAND_NORMAL);
+      }
+    case IX86_BUILTIN_CPU_IS:
+    case IX86_BUILTIN_CPU_SUPPORTS:
+      {
+	tree arg0 = CALL_EXPR_ARG (exp, 0);
+	tree fold_expr = fold_builtin_cpu (fndecl, &arg0);
+	gcc_assert (fold_expr != NULL_TREE);
+	return expand_expr (fold_expr, target, mode, EXPAND_NORMAL);
+      }
+    }
+
   /* Determine whether the builtin function is available under the current ISA.
      Originally the builtin was not created if it wasn't applicable to the
      current ISA based on the command line switches.  With function specific
@@ -39047,6 +39406,9 @@  ix86_memmodel_check (unsigned HOST_WIDE_INT val)
 #undef TARGET_SLOW_UNALIGNED_VECTOR_MEMOP
 #define TARGET_SLOW_UNALIGNED_VECTOR_MEMOP ix86_slow_unaligned_vector_memop
 
+#undef TARGET_FOLD_BUILTIN
+#define TARGET_FOLD_BUILTIN ix86_fold_builtin
+
 #undef TARGET_ENUM_VA_LIST_P
 #define TARGET_ENUM_VA_LIST_P ix86_enum_va_list