This is the next patch in the series of removing fab.
This one is simplier than builtin_constant_p because the only
time we want to simplify this builtin is at the final folding step.

Note align-5.c needs to change slightly as __builtin_assume_aligned
is no longer taken into account for the same reason as why PR 111875
is closed as invalid and why the testcase is failing at -Og
I added a new testcase align-5a.c where the pointer is explictly aligned
so that the check is gone there.
Note __builtin_assume_aligned should really be instrumented for UBSAN,
I filed PR 122038 for that.

Bootstrapped and tested on x86_64-linux-gnu.

        PR tree-optimization/121762
gcc/ChangeLog:

        * gimple-fold.cc (gimple_fold_builtin_assume_aligned): New function.
        (gimple_fold_builtin): Call gimple_fold_builtin_assume_aligned
        for BUILT_IN_ASSUME_ALIGNED.
        * tree-ssa-ccp.cc (pass_fold_builtins::execute): Remove handling
        of BUILT_IN_ASSUME_ALIGNED.

gcc/testsuite/ChangeLog:

        * c-c++-common/ubsan/align-5.c: Update as __builtin_assume_aligned
        is no longer taked into account.
        * c-c++-common/ubsan/align-5a.c: New test.

Signed-off-by: Andrew Pinski <[email protected]>
---
 gcc/gimple-fold.cc                          | 21 +++++++++++++++++++++
 gcc/testsuite/c-c++-common/ubsan/align-5.c  |  7 +++----
 gcc/testsuite/c-c++-common/ubsan/align-5a.c | 18 ++++++++++++++++++
 gcc/tree-ssa-ccp.cc                         |  5 -----
 4 files changed, 42 insertions(+), 9 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/ubsan/align-5a.c

diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
index 229b1b301a4..37ca0853ec8 100644
--- a/gcc/gimple-fold.cc
+++ b/gcc/gimple-fold.cc
@@ -5234,6 +5234,24 @@ gimple_fold_builtin_constant_p (gimple_stmt_iterator 
*gsi)
   return true;
 }
 
+/* Fold __builtin_assume_aligned builtin.  */
+
+static bool
+gimple_fold_builtin_assume_aligned (gimple_stmt_iterator *gsi)
+{
+  if (!(cfun->curr_properties & PROP_last_full_fold))
+    return false;
+
+  gcall *call = as_a<gcall*>(gsi_stmt (*gsi));
+
+  if (gimple_call_num_args (call) < 2)
+    return false;
+
+  gimplify_and_update_call_from_tree (gsi, gimple_call_arg (call, 0));
+
+  return true;
+}
+
 /* Fold the non-target builtin at *GSI and return whether any simplification
    was made.  */
 
@@ -5406,6 +5424,9 @@ gimple_fold_builtin (gimple_stmt_iterator *gsi)
     case BUILT_IN_CONSTANT_P:
       return gimple_fold_builtin_constant_p (gsi);
 
+    case BUILT_IN_ASSUME_ALIGNED:
+      return gimple_fold_builtin_assume_aligned (gsi);
+
     default:;
     }
 
diff --git a/gcc/testsuite/c-c++-common/ubsan/align-5.c 
b/gcc/testsuite/c-c++-common/ubsan/align-5.c
index b94e167bb67..484790134a6 100644
--- a/gcc/testsuite/c-c++-common/ubsan/align-5.c
+++ b/gcc/testsuite/c-c++-common/ubsan/align-5.c
@@ -1,8 +1,7 @@
 /* { dg-do compile } */
 /* { dg-options "-fno-sanitize=null -fsanitize=alignment -O2" } */
-/* Check that when optimizing if we know the alignment is right
-   and we are not doing -fsanitize=null instrumentation we don't
-   instrument the alignment check.  */
+/* __builtin_assume_aligned should be instrumented too. UBSAN alignment
+   should not depend on it.  */
 
 __attribute__((noinline, noclone)) int
 foo (char *p)
@@ -12,4 +11,4 @@ foo (char *p)
   return *q;
 }
 
-/* { dg-final { scan-assembler-not "__ubsan_handle" } } */
+/* { dg-final { scan-assembler "__ubsan_handle" } } */
diff --git a/gcc/testsuite/c-c++-common/ubsan/align-5a.c 
b/gcc/testsuite/c-c++-common/ubsan/align-5a.c
new file mode 100644
index 00000000000..d86e5377e03
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/ubsan/align-5a.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-fno-sanitize=null -fsanitize=alignment -O2" } */
+/* Check that when optimizing if we know the alignment is right
+   and we are not doing -fsanitize=null instrumentation we don't
+   instrument the alignment check.  */
+
+__attribute__((noinline, noclone)) int
+foo (int *p)
+{
+  /* Align the pointer explictly. */
+  __INTPTR_TYPE__ t = (__INTPTR_TYPE__)p;
+  t &= ~0xf;
+  p = (int*)t;
+
+  return *p;
+}
+
+/* { dg-final { scan-assembler-not "__ubsan_handle" } } */
diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc
index 4bdcd00219d..070289ca9f0 100644
--- a/gcc/tree-ssa-ccp.cc
+++ b/gcc/tree-ssa-ccp.cc
@@ -4317,11 +4317,6 @@ pass_fold_builtins::execute (function *fun)
              switch (DECL_FUNCTION_CODE (callee))
                {
 
-               case BUILT_IN_ASSUME_ALIGNED:
-                 /* Remove __builtin_assume_aligned.  */
-                 result = gimple_call_arg (stmt, 0);
-                 break;
-
                case BUILT_IN_STACK_RESTORE:
                  result = optimize_stack_restore (i);
                  if (result)
-- 
2.43.0

Reply via email to