On Fri, Oct 3, 2025 at 11:28 PM Andrew Pinski
<[email protected]> wrote:
>
> As requested in 
> https://inbox.sourceware.org/gcc-patches/CAFiYyc1jzZSZNhTas-DdMBFOzH1p96oGN=ovj6fyjt8hzdu...@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.

OK.

>         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 <[email protected]>
> ---
>  gcc/generic-match-head.cc |  2 ++
>  gcc/gimple-fold.cc        | 11 +++++------
>  gcc/gimple-fold.h         | 11 +++++++++++
>  gcc/match.pd              |  6 +++---
>  gcc/tree-ssa-forwprop.cc  |  4 ++--
>  5 files changed, 23 insertions(+), 11 deletions(-)
>
> diff --git a/gcc/generic-match-head.cc b/gcc/generic-match-head.cc
> index aa1f9b6d2c8..ea4a958686d 100644
> --- a/gcc/generic-match-head.cc
> +++ b/gcc/generic-match-head.cc
> @@ -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
> diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
> index 2f64de2fb41..edcc04adc08 100644
> --- a/gcc/gimple-fold.cc
> +++ b/gcc/gimple-fold.cc
> @@ -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:
> diff --git a/gcc/gimple-fold.h b/gcc/gimple-fold.h
> index 3f617d1c4cd..7244941722d 100644
> --- a/gcc/gimple-fold.h
> +++ b/gcc/gimple-fold.h
> @@ -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 */
> diff --git a/gcc/match.pd b/gcc/match.pd
> index b3fd26e18dd..bfe8078298d 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -5268,7 +5268,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
> @@ -11811,7 +11811,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)))
> @@ -11820,7 +11820,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)))
> diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
> index 2c6e1eab9cd..a2c8d103629 100644
> --- a/gcc/tree-ssa-forwprop.cc
> +++ b/gcc/tree-ssa-forwprop.cc
> @@ -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;
> --
> 2.43.0
>

Reply via email to