public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-3810] tree-optimization/107389 - honor __builtin_assume_alignment at -O0
@ 2022-11-08 15:39 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2022-11-08 15:39 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:7899582a059a9d8c25bfff305cd236d219dc4f20

commit r13-3810-g7899582a059a9d8c25bfff305cd236d219dc4f20
Author: Richard Biener <rguenther@suse.de>
Date:   Tue Nov 8 13:49:16 2022 +0100

    tree-optimization/107389 - honor __builtin_assume_alignment at -O0
    
    The following makes sure to set alignment information on the LHS
    of __builtin_assume_alignment calls even when not optimizing so
    uses as arguments to builtin functions like memcpy or __atomic_load_n
    can be reflected at RTL expansion time.
    
            PR tree-optimization/107389
            * gimple-low.cc (lower_builtin_assume_aligned): New.
            (lower_stmt): Call it.
    
            * gcc.dg/pr107389.c: New testcase.

Diff:
---
 gcc/gimple-low.cc               | 41 +++++++++++++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/pr107389.c | 13 +++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/gcc/gimple-low.cc b/gcc/gimple-low.cc
index 512aa9feada..53df2e7b212 100644
--- a/gcc/gimple-low.cc
+++ b/gcc/gimple-low.cc
@@ -84,6 +84,7 @@ static void lower_try_catch (gimple_stmt_iterator *, struct lower_data *);
 static void lower_gimple_return (gimple_stmt_iterator *, struct lower_data *);
 static void lower_builtin_setjmp (gimple_stmt_iterator *);
 static void lower_builtin_posix_memalign (gimple_stmt_iterator *);
+static void lower_builtin_assume_aligned (gimple_stmt_iterator *);
 
 
 /* Lower the body of current_function_decl from High GIMPLE into Low
@@ -768,6 +769,14 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
 		lower_builtin_posix_memalign (gsi);
 		return;
 	      }
+	    else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_ASSUME_ALIGNED
+		     && !optimize)
+	      {
+		lower_builtin_assume_aligned (gsi);
+		data->cannot_fallthru = false;
+		gsi_next (gsi);
+		return;
+	      }
 	  }
 
 	if (decl && (flags_from_decl_or_type (decl) & ECF_NORETURN))
@@ -1310,6 +1319,38 @@ lower_builtin_posix_memalign (gimple_stmt_iterator *gsi)
   gsi_insert_after (gsi, stmt, GSI_NEW_STMT);
   gsi_insert_after (gsi, gimple_build_label (noalign_label), GSI_NEW_STMT);
 }
+
+/* Lower calls to __builtin_assume_aligned when not optimizing.  */
+
+static void
+lower_builtin_assume_aligned (gimple_stmt_iterator *gsi)
+{
+  gcall *call = as_a <gcall *> (gsi_stmt (*gsi));
+
+  tree lhs = gimple_call_lhs (call);
+  if (!lhs || !POINTER_TYPE_P (TREE_TYPE (lhs)) || TREE_CODE (lhs) != SSA_NAME)
+    return;
+
+  tree align = gimple_call_arg (call, 1);
+  tree misalign = (gimple_call_num_args (call) > 2
+		   ? gimple_call_arg (call, 2) : NULL_TREE);
+  if (!tree_fits_uhwi_p (align)
+      || (misalign && !tree_fits_uhwi_p (misalign)))
+    return;
+
+  unsigned aligni = TREE_INT_CST_LOW (align);
+  unsigned misaligni = misalign ? TREE_INT_CST_LOW (misalign) : 0;
+  if (aligni <= 1
+      || (aligni & (aligni - 1)) != 0
+      || (misaligni & ~(aligni - 1)) != 0)
+    return;
+
+  /* For lowering we simply transfer alignment information to the
+     result and leave the call otherwise unchanged, it will be elided
+     at RTL expansion time.  */
+  ptr_info_def *pi = get_ptr_info (lhs);
+  set_ptr_info_alignment (pi, aligni, misaligni);
+}
 \f
 
 /* Record the variables in VARS into function FN.  */
diff --git a/gcc/testsuite/gcc.dg/pr107389.c b/gcc/testsuite/gcc.dg/pr107389.c
new file mode 100644
index 00000000000..deb63380704
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr107389.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-optimized-alias" } */
+
+unsigned foo (void *p)
+{
+  unsigned i;
+  __builtin_memcpy (&i, __builtin_assume_aligned (p, 4), sizeof (unsigned));
+  return i;
+}
+
+/* Even when not optimizing we should have alignment info on the temporary
+   feeding the memcpy.  */
+/* { dg-final { scan-tree-dump "ALIGN = 4" "optimized" } } */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-08 15:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-08 15:39 [gcc r13-3810] tree-optimization/107389 - honor __builtin_assume_alignment at -O0 Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).