]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Introduce fold_before_rtl_expansion_p [PR122142]
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Fri, 3 Oct 2025 16:54:45 +0000 (09:54 -0700)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Mon, 6 Oct 2025 05:49:04 +0000 (22:49 -0700)
As requested in https://inbox.sourceware.org/gcc-patches/CAFiYyc1jzZSZNhTas-DdMBFOzH1p96oGN=OVj6fyjt8HzDUyCA@mail.gmail.com/T/#u.
This introduces fold_before_rtl_expansion_p to replace
`(cfun->curr_properties & PROP_last_full_fold) != 0`.
I am not a fan of include tree-pass.h in gimple-fold.h but that was the
only way to reduce the number of changes.

Bootrapped and tested on x86_64-linux-gnu.

PR tree-optimization/122142
gcc/ChangeLog:

* generic-match-head.cc: Include gimple-iterator.h
and gimple-fold.h.
* gimple-fold.cc (gimple_fold_builtin_constant_p): Use
fold_before_rtl_expansion_p.
(gimple_fold_builtin_assume_aligned): Likewise.
(gimple_fold_builtin_stdarg): Likewise.
(gimple_fold_call): Likewise.
* gimple-fold.h: Include "tree-pass.h".
(fold_before_rtl_expansion_p): New function.
* match.pd: Use fold_before_rtl_expansion_p
instead of `cfun->curr_properties & PROP_last_full_fold`.
* tree-ssa-forwprop.cc (simplify_builtin_memcmp): Likewise.
(optimize_stack_restore): Likewise.

Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
gcc/generic-match-head.cc
gcc/gimple-fold.cc
gcc/gimple-fold.h
gcc/match.pd
gcc/tree-ssa-forwprop.cc

index aa1f9b6d2c87e6a80519e415e3f2887420cba624..ea4a958686dbeeabdbd766b9b7fd05a0ad426bf4 100644 (file)
@@ -45,6 +45,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-pass.h"
 #include "attribs.h"
 #include "asan.h"
+#include "gimple-iterator.h"
+#include "gimple-fold.h"
 
 /* Routine to determine if the types T1 and T2 are effectively
    the same for GENERIC.  If T1 or T2 is not a type, the test
index 2f64de2fb4140a0562dc4fac0340f3fdc851d9e4..edcc04adc08cf15cff36d436caa31c0cfbff8f9a 100644 (file)
@@ -38,6 +38,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "stor-layout.h"
 #include "dumpfile.h"
 #include "gimple-iterator.h"
+#include "tree-pass.h"
 #include "gimple-fold.h"
 #include "gimplify.h"
 #include "tree-into-ssa.h"
@@ -69,7 +70,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "varasm.h"
 #include "internal-fn.h"
 #include "gimple-range.h"
-#include "tree-pass.h"
 
 enum strlen_range_kind {
   /* Compute the exact constant string length.  */
@@ -5223,8 +5223,7 @@ gimple_fold_builtin_constant_p (gimple_stmt_iterator *gsi)
   /* Resolve __builtin_constant_p.  If it hasn't been
      folded to integer_one_node by now, it's fairly
      certain that the value simply isn't constant.  */
-  if (!result
-      && (cfun->curr_properties & PROP_last_full_fold))
+  if (!result && fold_before_rtl_expansion_p ())
     result = integer_zero_node;
 
   if (!result)
@@ -5239,7 +5238,7 @@ gimple_fold_builtin_constant_p (gimple_stmt_iterator *gsi)
 static bool
 gimple_fold_builtin_assume_aligned (gimple_stmt_iterator *gsi)
 {
-  if (!(cfun->curr_properties & PROP_last_full_fold))
+  if (!fold_before_rtl_expansion_p ())
     return false;
 
   gcall *call = as_a<gcall*>(gsi_stmt (*gsi));
@@ -5261,7 +5260,7 @@ static bool
 gimple_fold_builtin_stdarg (gimple_stmt_iterator *gsi, gcall *call)
 {
   /* These shouldn't be folded before pass_stdarg.  */
-  if (!(cfun->curr_properties & PROP_last_full_fold))
+  if (!fold_before_rtl_expansion_p ())
     return false;
 
   tree callee, lhs, rhs, cfun_va_list;
@@ -6014,7 +6013,7 @@ gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
        case IFN_ASSUME:
          /* Remove .ASSUME calls during the last fold since it is no
             longer needed.  */
-         if (cfun->curr_properties & PROP_last_full_fold)
+         if (fold_before_rtl_expansion_p ())
            replace_call_with_value (gsi, NULL_TREE);
          break;
        case IFN_BUILTIN_EXPECT:
index 3f617d1c4cd7c52e9d6c0a0b586908814cf0d9b9..7244941722d86988c4e1ccd3edf598cd48d9d6f6 100644 (file)
@@ -22,6 +22,8 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_GIMPLE_FOLD_H
 #define GCC_GIMPLE_FOLD_H
 
+#include "tree-pass.h"
+
 extern tree canonicalize_constructor_val (tree, tree);
 extern tree get_symbol_constant_value (tree);
 struct c_strlen_data;
@@ -280,4 +282,13 @@ extern tree gimple_simplify (combined_fn, tree, tree, tree,
 extern tree gimple_simplify (combined_fn, tree, tree, tree, tree,
                             gimple_seq *, tree (*)(tree));
 
+/* Returns true if we are doing the fold before expansion to rtl.   */
+inline bool
+fold_before_rtl_expansion_p ()
+{
+  if (!cfun)
+    return false;
+  return (cfun->curr_properties & PROP_last_full_fold) != 0;
+}
+
 #endif  /* GCC_GIMPLE_FOLD_H */
index 60bdd3338c209347fb5899723e506d0c61852012..10a2c5e72d69788793b829a6205ba573b2b2633a 100644 (file)
@@ -5259,7 +5259,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
             expr_not_equal_to had a chance to match.  Otherwise we'd do
             pretty much always just the second case.  */
          && cfun
-         && ((cfun->curr_properties & PROP_last_full_fold) != 0
+         && (fold_before_rtl_expansion_p ()
              || !flag_tree_vrp
              || optimize_debug))
       (orotate @0
@@ -11830,7 +11830,7 @@ and,
   (plus:c
     (plus (rshift @0 integer_onep@1) (rshift @2 @1))
     (bit_and (bit_ior @0 @2) integer_onep@3))
-  (if (cfun && (cfun->curr_properties & PROP_last_full_fold) != 0
+  (if (fold_before_rtl_expansion_p ()
       && VECTOR_TYPE_P (type)
       && direct_internal_fn_supported_p (IFN_AVG_CEIL, type, OPTIMIZE_FOR_BOTH))
       (IFN_AVG_CEIL @0 @2)))
@@ -11839,7 +11839,7 @@ and,
   (minus
     (bit_ior @0 @2)
     (rshift (bit_xor @0 @2) integer_onep@1))
-  (if (cfun && (cfun->curr_properties & PROP_last_full_fold) != 0
+  (if (fold_before_rtl_expansion_p ()
       && VECTOR_TYPE_P (type)
       && direct_internal_fn_supported_p (IFN_AVG_CEIL, type, OPTIMIZE_FOR_BOTH))
       (IFN_AVG_CEIL @0 @2)))
index ad09f7334cc645f200504a82fbcc6ba6b967fc4a..ee3bb401f31ad4d38b026c1f30138b7e0cc7c1a4 100644 (file)
@@ -1846,7 +1846,7 @@ simplify_builtin_memcmp (gimple_stmt_iterator *gsi_p, gcall *stmt)
   /* Replace memcmp with memcmp_eq if the above fails. */
   if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_MEMCMP_EQ)
     return false;
-  if (!(cfun->curr_properties & (PROP_last_full_fold)))
+  if (!fold_before_rtl_expansion_p ())
     return false;
   gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
   update_stmt (stmt);
@@ -2147,7 +2147,7 @@ simplify_builtin_memcpy_memset (gimple_stmt_iterator *gsi_p, gcall *stmt2)
 static bool
 optimize_stack_restore (gimple_stmt_iterator *gsi, gimple *call)
 {
-  if (!(cfun->curr_properties & PROP_last_full_fold))
+  if (!fold_before_rtl_expansion_p ())
     return false;
   tree callee;
   gimple *stmt;