From 6e4f11689b8ac84b5f3eb618e88b0b3a9cc74008 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hongjiu.lu@intel.com>
Date: Sun, 18 Jan 2009 16:26:26 +0000
Subject: [PATCH] re PR target/38736 (-mavx can change the ABI via
 BIGGEST_ALIGNMENT)

gcc/

2009-01-18  H.J. Lu  <hongjiu.lu@intel.com>

	PR target/38736
	* c-common.c (handle_aligned_attribute): Use
	ATTRIBUTE_ALIGNED_VALUE instead of BIGGEST_ALIGNMENT for
	default alignment value.

	* c-cppbuiltin.c (c_cpp_builtins): Define __BIGGEST_ALIGNMENT__.

	* defaults.h (ATTRIBUTE_ALIGNED_VALUE): New.
	* config/i386/i386.h (ATTRIBUTE_ALIGNED_VALUE): Likewise.

	* doc/extend.texi: Update __attribute__ ((aligned)).  Document
	__BIGGEST_ALIGNMENT__.

	* doc/tm.texi: Document ATTRIBUTE_ALIGNED_VALUE.

gcc/testsuite/

2009-01-18  H.J. Lu  <hongjiu.lu@intel.com>

	PR target/38736
	* g++.dg/compat/abi/pr38736_main.C: New.
	* g++.dg/compat/abi/pr38736_x.C: Likewise.
	* g++.dg/compat/abi/pr38736_y.C: Likewise.
	* g++.dg/other/macro-1.C: Likewise.
	* gcc.dg/macro-1.c: Likewise.
	* gcc.dg/compat/pr38736_main.c: Likewise.
	* gcc.dg/compat/pr38736_x.c: Likewise.
	* gcc.dg/compat/pr38736_y.c: Likewise.

From-SVN: r143486
---
 gcc/ChangeLog                                 | 17 +++++++++++
 gcc/c-common.c                                |  2 +-
 gcc/c-cppbuiltin.c                            |  3 ++
 gcc/config/i386/i386.h                        |  4 +++
 gcc/defaults.h                                |  5 ++++
 gcc/doc/extend.texi                           | 28 +++++++++++--------
 gcc/doc/tm.texi                               |  5 ++++
 gcc/testsuite/ChangeLog                       | 12 ++++++++
 .../g++.dg/compat/abi/pr38736_main.C          | 18 ++++++++++++
 gcc/testsuite/g++.dg/compat/abi/pr38736_x.C   | 15 ++++++++++
 gcc/testsuite/g++.dg/compat/abi/pr38736_y.C   |  6 ++++
 gcc/testsuite/g++.dg/other/macro-1.C          |  3 ++
 gcc/testsuite/gcc.dg/compat/pr38736_main.c    | 18 ++++++++++++
 gcc/testsuite/gcc.dg/compat/pr38736_x.c       | 15 ++++++++++
 gcc/testsuite/gcc.dg/compat/pr38736_y.c       |  6 ++++
 gcc/testsuite/gcc.dg/macro-1.c                |  3 ++
 16 files changed, 148 insertions(+), 12 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/compat/abi/pr38736_main.C
 create mode 100644 gcc/testsuite/g++.dg/compat/abi/pr38736_x.C
 create mode 100644 gcc/testsuite/g++.dg/compat/abi/pr38736_y.C
 create mode 100644 gcc/testsuite/g++.dg/other/macro-1.C
 create mode 100644 gcc/testsuite/gcc.dg/compat/pr38736_main.c
 create mode 100644 gcc/testsuite/gcc.dg/compat/pr38736_x.c
 create mode 100644 gcc/testsuite/gcc.dg/compat/pr38736_y.c
 create mode 100644 gcc/testsuite/gcc.dg/macro-1.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 1448c0db3a86..fed14be4e611 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,20 @@
+2009-01-18  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR target/38736
+	* c-common.c (handle_aligned_attribute): Use
+	ATTRIBUTE_ALIGNED_VALUE instead of BIGGEST_ALIGNMENT for
+	default alignment value.
+
+	* c-cppbuiltin.c (c_cpp_builtins): Define __BIGGEST_ALIGNMENT__.
+
+	* defaults.h (ATTRIBUTE_ALIGNED_VALUE): New.
+	* config/i386/i386.h (ATTRIBUTE_ALIGNED_VALUE): Likewise.
+
+	* doc/extend.texi: Update __attribute__ ((aligned)).  Document
+	__BIGGEST_ALIGNMENT__.
+
+	* doc/tm.texi: Document ATTRIBUTE_ALIGNED_VALUE.
+
 2009-01-18  Richard Guenther  <rguenther@suse.de>
 
 	PR tree-optimization/38819
diff --git a/gcc/c-common.c b/gcc/c-common.c
index b94ac1b7037b..5eed1dc89e1b 100644
--- a/gcc/c-common.c
+++ b/gcc/c-common.c
@@ -5906,7 +5906,7 @@ handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
   tree *type = NULL;
   int is_type = 0;
   tree align_expr = (args ? TREE_VALUE (args)
-		     : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
+		     : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT));
   int i;
 
   if (DECL_P (*node))
diff --git a/gcc/c-cppbuiltin.c b/gcc/c-cppbuiltin.c
index 58b1796e797e..c6112d8afcae 100644
--- a/gcc/c-cppbuiltin.c
+++ b/gcc/c-cppbuiltin.c
@@ -762,6 +762,9 @@ c_cpp_builtins (cpp_reader *pfile)
      format.  */
   if (ENABLE_DECIMAL_FLOAT && ENABLE_DECIMAL_BID_FORMAT)
     cpp_define (pfile, "__DECIMAL_BID_FORMAT__");
+
+  builtin_define_with_int_value ("__BIGGEST_ALIGNMENT__",
+				 BIGGEST_ALIGNMENT / BITS_PER_UNIT);
 }
 
 /* Pass an object-like macro.  If it doesn't lie in the user's
diff --git a/gcc/config/i386/i386.h b/gcc/config/i386/i386.h
index 55935985a292..14e7438c4a51 100644
--- a/gcc/config/i386/i386.h
+++ b/gcc/config/i386/i386.h
@@ -710,6 +710,10 @@ enum target_cpu_default
 /* Maximum stack alignment.  */
 #define MAX_STACK_ALIGNMENT MAX_OFILE_ALIGNMENT
 
+/* Alignment value for attribute ((aligned)).  It is a constant since
+   it is the part of the ABI.  We shouldn't change it with -mavx.  */
+#define ATTRIBUTE_ALIGNED_VALUE 128
+
 /* Decide whether a variable of mode MODE should be 128 bit aligned.  */
 #define ALIGN_MODE_128(MODE) \
  ((MODE) == XFmode || SSE_REG_MODE_P (MODE))
diff --git a/gcc/defaults.h b/gcc/defaults.h
index 282a14ac00a0..c9097349795f 100644
--- a/gcc/defaults.h
+++ b/gcc/defaults.h
@@ -944,4 +944,9 @@ along with GCC; see the file COPYING3.  If not see
   ((TYPE) ? LOCAL_ALIGNMENT ((TYPE), (ALIGN)) : (ALIGN))
 #endif
 
+/* Alignment value for attribute ((aligned)).  */
+#ifndef ATTRIBUTE_ALIGNED_VALUE
+#define ATTRIBUTE_ALIGNED_VALUE BIGGEST_ALIGNMENT
+#endif
+
 #endif  /* ! GCC_DEFAULTS_H */
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index f131a4da2ba2..5a7e2d83e290 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -3697,21 +3697,27 @@ that forces the union to be double-word aligned.
 As in the preceding examples, you can explicitly specify the alignment
 (in bytes) that you wish the compiler to use for a given variable or
 structure field.  Alternatively, you can leave out the alignment factor
-and just ask the compiler to align a variable or field to the maximum
-useful alignment for the target machine you are compiling for.  For
-example, you could write:
+and just ask the compiler to align a variable or field to the
+default alignment for the target architecture you are compiling for.
+The default alignment is sufficient for all scalar types, but may not be
+enough for all vector types on a target which supports vector operations.
+The default alignment is fixed for a particular target ABI.
+
+Gcc also provides a target specific macro @code{__BIGGEST_ALIGNMENT__},
+which is the largest alignment ever used for any data type on the
+target machine you are compiling for.  For example, you could write:
 
 @smallexample
-short array[3] __attribute__ ((aligned));
+short array[3] __attribute__ ((aligned (__BIGGEST_ALIGNMENT__)));
 @end smallexample
 
-Whenever you leave out the alignment factor in an @code{aligned} attribute
-specification, the compiler automatically sets the alignment for the declared
-variable or field to the largest alignment which is ever used for any data
-type on the target machine you are compiling for.  Doing this can often make
-copy operations more efficient, because the compiler can use whatever
-instructions copy the biggest chunks of memory when performing copies to
-or from the variables or fields that you have aligned this way.
+The compiler automatically sets the alignment for the declared
+variable or field to @code{__BIGGEST_ALIGNMENT__}.  Doing this can
+often make copy operations more efficient, because the compiler can
+use whatever instructions copy the biggest chunks of memory when
+performing copies to or from the variables or fields that you have
+aligned this way.  Note that the value of @code{__BIGGEST_ALIGNMENT__}
+may change depending on command line options.
 
 When used on a struct, or struct member, the @code{aligned} attribute can
 only increase the alignment; in order to decrease it, the @code{packed}
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index d748a754e2d1..92de6e5b8ac4 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -1105,6 +1105,11 @@ Alignment, in bits, a C conformant malloc implementation has to
 provide.  If not defined, the default value is @code{BITS_PER_WORD}.
 @end defmac
 
+@defmac ATTRIBUTE_ALIGNED_VALUE
+Alignment used by the @code{__attribute__ ((aligned))} construct.  If
+not defined, the default value is @code{BIGGEST_ALIGNMENT}.
+@end defmac
+
 @defmac MINIMUM_ATOMIC_ALIGNMENT
 If defined, the smallest alignment, in bits, that can be given to an
 object that can be referenced in one operation, without disturbing any
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 43fe9a9396bb..8f42d58e6c6c 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,15 @@
+2009-01-18  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR target/38736
+	* g++.dg/compat/abi/pr38736_main.C: New.
+	* g++.dg/compat/abi/pr38736_x.C: Likewise.
+	* g++.dg/compat/abi/pr38736_y.C: Likewise.
+	* g++.dg/other/macro-1.C: Likewise.
+	* gcc.dg/macro-1.c: Likewise.
+	* gcc.dg/compat/pr38736_main.c: Likewise.
+	* gcc.dg/compat/pr38736_x.c: Likewise.
+	* gcc.dg/compat/pr38736_y.c: Likewise.
+
 2009-01-18  Richard Guenther  <rguenther@suse.de>
 
 	PR tree-optimization/38819
diff --git a/gcc/testsuite/g++.dg/compat/abi/pr38736_main.C b/gcc/testsuite/g++.dg/compat/abi/pr38736_main.C
new file mode 100644
index 000000000000..b9f6ac898ffa
--- /dev/null
+++ b/gcc/testsuite/g++.dg/compat/abi/pr38736_main.C
@@ -0,0 +1,18 @@
+/* PR target/38736 */
+/* { dg-skip-if "attribute ((aligned))" { ! { i?86-*-* x86_64-*-* } } } */
+/* { dg-require-effective-target avx } */
+
+/* Test compatibility of attribute ((aligned)) with and without -mavx.  */
+
+extern int aligned_x (void);
+extern int aligned_y_avx (void);
+extern "C" void abort (void);
+
+int
+main ()
+{
+  if (aligned_x () != aligned_y_avx ())
+    abort ();
+
+  return 0;
+}
diff --git a/gcc/testsuite/g++.dg/compat/abi/pr38736_x.C b/gcc/testsuite/g++.dg/compat/abi/pr38736_x.C
new file mode 100644
index 000000000000..aeab257aae9f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/compat/abi/pr38736_x.C
@@ -0,0 +1,15 @@
+/* PR target/38736 */
+/* { dg-options "-O2" } */
+
+struct alignment_test_struct
+{
+  char space[4] __attribute__((__aligned__));
+};
+
+extern int aligned_x (void);
+
+int
+aligned_x (void)
+{
+  return __alignof__(struct alignment_test_struct);
+}
diff --git a/gcc/testsuite/g++.dg/compat/abi/pr38736_y.C b/gcc/testsuite/g++.dg/compat/abi/pr38736_y.C
new file mode 100644
index 000000000000..bade1b2927be
--- /dev/null
+++ b/gcc/testsuite/g++.dg/compat/abi/pr38736_y.C
@@ -0,0 +1,6 @@
+/* PR target/38736 */
+/* { dg-options "-O2 -mavx" } */
+
+#define aligned_x aligned_y_avx
+
+#include "pr38736_x.C"
diff --git a/gcc/testsuite/g++.dg/other/macro-1.C b/gcc/testsuite/g++.dg/other/macro-1.C
new file mode 100644
index 000000000000..e1d9d34cd031
--- /dev/null
+++ b/gcc/testsuite/g++.dg/other/macro-1.C
@@ -0,0 +1,3 @@
+/* { dg-do compile } */
+
+short array[3] __attribute__ ((aligned (__BIGGEST_ALIGNMENT__)));
diff --git a/gcc/testsuite/gcc.dg/compat/pr38736_main.c b/gcc/testsuite/gcc.dg/compat/pr38736_main.c
new file mode 100644
index 000000000000..88c3586db4b8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/compat/pr38736_main.c
@@ -0,0 +1,18 @@
+/* PR target/38736 */
+/* { dg-skip-if "attribute ((aligned))" { ! { i?86-*-* x86_64-*-* } } } */
+/* { dg-require-effective-target avx } */
+
+/* Test compatibility of attribute ((aligned)) with and without -mavx.  */
+
+extern int aligned_x (void);
+extern int aligned_y_avx (void);
+extern void abort (void);
+
+int
+main ()
+{
+  if (aligned_x () != aligned_y_avx ())
+    abort ();
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/compat/pr38736_x.c b/gcc/testsuite/gcc.dg/compat/pr38736_x.c
new file mode 100644
index 000000000000..aeab257aae9f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/compat/pr38736_x.c
@@ -0,0 +1,15 @@
+/* PR target/38736 */
+/* { dg-options "-O2" } */
+
+struct alignment_test_struct
+{
+  char space[4] __attribute__((__aligned__));
+};
+
+extern int aligned_x (void);
+
+int
+aligned_x (void)
+{
+  return __alignof__(struct alignment_test_struct);
+}
diff --git a/gcc/testsuite/gcc.dg/compat/pr38736_y.c b/gcc/testsuite/gcc.dg/compat/pr38736_y.c
new file mode 100644
index 000000000000..7e46afa8d175
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/compat/pr38736_y.c
@@ -0,0 +1,6 @@
+/* PR target/38736 */
+/* { dg-options "-O2 -mavx" } */
+
+#define aligned_x aligned_y_avx
+
+#include "pr38736_x.c"
diff --git a/gcc/testsuite/gcc.dg/macro-1.c b/gcc/testsuite/gcc.dg/macro-1.c
new file mode 100644
index 000000000000..e1d9d34cd031
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/macro-1.c
@@ -0,0 +1,3 @@
+/* { dg-do compile } */
+
+short array[3] __attribute__ ((aligned (__BIGGEST_ALIGNMENT__)));
-- 
GitLab