Without -m[no-]strict-align, MASK_STRICT_ALIGN is set for slow
misaligned access but never cleared, so a target attribute or pragma
cannot allow misaligned access.

Clear it, like MASK_DIV.  Also set riscv_user_wants_strict_align only
for an explicit -mstrict-align, so a pragma does not define
__riscv_misaligned_avoid.

gcc/ChangeLog:

        * config/riscv/riscv.cc (riscv_override_options_internal): Clear
        MASK_STRICT_ALIGN when it is not given.  Set
        riscv_user_wants_strict_align only for an explicit -mstrict-align.
        Set riscv_slow_unaligned_access_p after MASK_STRICT_ALIGN.

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/strict-align-attr-1.c: New test.
        * gcc.target/riscv/strict-align-attr-2.c: New test.
        * gcc.target/riscv/strict-align-pragma-1.c: New test.
        * gcc.target/riscv/strict-align-pragma-2.c: New test.
---
 gcc/config/riscv/riscv.cc                     | 27 ++++++++++++-------
 .../gcc.target/riscv/strict-align-attr-1.c    | 14 ++++++++++
 .../gcc.target/riscv/strict-align-attr-2.c    | 13 +++++++++
 .../gcc.target/riscv/strict-align-pragma-1.c  | 17 ++++++++++++
 .../gcc.target/riscv/strict-align-pragma-2.c  | 13 +++++++++
 5 files changed, 74 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/strict-align-attr-1.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/strict-align-attr-2.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/strict-align-pragma-1.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/strict-align-pragma-2.c

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index f77f62b393f..4791d7950ff 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -12573,12 +12573,22 @@ riscv_override_options_internal (struct gcc_options 
*opts)
        opts->x_str_align_functions = tune_param->function_align;
     }
 
+  /* If -m[no-]strict-align is left unspecified, heed -mtune's advice.
+     Clear the flag too, since OPTS may come from a target attribute or
+     pragma that started from options where it was set.  */
+  if ((target_flags_explicit & MASK_STRICT_ALIGN) == 0)
+    {
+      if (cpu->tune_param->slow_unaligned_access)
+       opts->x_target_flags |= MASK_STRICT_ALIGN;
+      else
+       opts->x_target_flags &= ~MASK_STRICT_ALIGN;
+    }
+
   /* Use -mtune's setting for slow_unaligned_access, even when optimizing
      for size.  For architectures that trap and emulate unaligned accesses,
-     the performance cost is too great, even for -Os.  Similarly, if
-     -m[no-]strict-align is left unspecified, heed -mtune's advice.  */
+     the performance cost is too great, even for -Os.  */
   riscv_slow_unaligned_access_p = (cpu->tune_param->slow_unaligned_access
-                                  || TARGET_STRICT_ALIGN);
+                                  || TARGET_STRICT_ALIGN_OPTS_P (opts));
 
   /* By default, when -mno-vector-strict-align is not specified, do not allow
      unaligned vector memory accesses except if -mtune's setting explicitly
@@ -12587,13 +12597,10 @@ riscv_override_options_internal (struct gcc_options 
*opts)
     || cpu->tune_param->vector_unaligned_access;
 
   /* Make a note if user explicitly passed -mstrict-align for later
-     builtin macro generation.  Can't use target_flags_explicitly since
-     it is set even for -mno-strict-align.  */
-  riscv_user_wants_strict_align = TARGET_STRICT_ALIGN_OPTS_P (opts);
-
-  if ((target_flags_explicit & MASK_STRICT_ALIGN) == 0
-      && cpu->tune_param->slow_unaligned_access)
-    opts->x_target_flags |= MASK_STRICT_ALIGN;
+     builtin macro generation.  */
+  riscv_user_wants_strict_align
+    = ((target_flags_explicit & MASK_STRICT_ALIGN) != 0
+       && TARGET_STRICT_ALIGN_OPTS_P (opts));
 
   /* If the user hasn't specified a branch cost, use the processor's
      default.  */
diff --git a/gcc/testsuite/gcc.target/riscv/strict-align-attr-1.c 
b/gcc/testsuite/gcc.target/riscv/strict-align-attr-1.c
new file mode 100644
index 00000000000..1fe18387440
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/strict-align-attr-1.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target rv64 } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O2 -mtune=rocket" } */
+
+typedef int __attribute__ ((aligned (1))) uint_u;
+
+__attribute__ ((target ("cpu=thead-c906;tune=thead-c906"))) int
+f (uint_u *p)
+{
+  return *p;
+}
+
+/* { dg-final { scan-assembler {\mlw\M} } } */
+/* { dg-final { scan-assembler-not {\mlbu\M} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/strict-align-attr-2.c 
b/gcc/testsuite/gcc.target/riscv/strict-align-attr-2.c
new file mode 100644
index 00000000000..c14aafc007d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/strict-align-attr-2.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target rv64 } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -O2 -mtune=rocket -mstrict-align" } 
*/
+
+typedef int __attribute__ ((aligned (1))) uint_u;
+
+__attribute__ ((target ("cpu=thead-c906;tune=thead-c906"))) int
+f (uint_u *p)
+{
+  return *p;
+}
+
+/* { dg-final { scan-assembler-not {\mlw\M} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/strict-align-pragma-1.c 
b/gcc/testsuite/gcc.target/riscv/strict-align-pragma-1.c
new file mode 100644
index 00000000000..36c514a68d1
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/strict-align-pragma-1.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-mtune=rocket" } */
+
+#pragma GCC target ("cpu=thead-c906;tune=thead-c906")
+
+int main() {
+
+#if !defined(__riscv_misaligned_fast)
+#error "__riscv_misaligned_fast is not set"
+#endif
+
+#if defined(__riscv_misaligned_avoid)
+#error "__riscv_misaligned_avoid is unexpectedly set"
+#endif
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/riscv/strict-align-pragma-2.c 
b/gcc/testsuite/gcc.target/riscv/strict-align-pragma-2.c
new file mode 100644
index 00000000000..b7a13d9e873
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/strict-align-pragma-2.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-mtune=thead-c906 -mstrict-align" } */
+
+#pragma GCC target ("cpu=thead-c906;tune=thead-c906")
+
+int main() {
+
+#if !defined(__riscv_misaligned_avoid)
+#error "__riscv_misaligned_avoid is not set"
+#endif
+
+  return 0;
+}
-- 
2.54.0

Reply via email to