Introduce fold_before_rtl_expansion_p [PR122142]

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>
This commit is contained in:
Andrew Pinski 2025-10-03 09:54:45 -07:00
parent 7762d809bf
commit a7d8eca724
5 changed files with 23 additions and 11 deletions

View 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

View 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:

View 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 */

View 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)))

View 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;