On Sat, Sep 20, 2025 at 5:15 AM Andrew Pinski
<[email protected]> wrote:
>
> This is the first patch in removing fold_all_builtins pass.
> We want to fold __builtin_constant_p into 0 if we know the argument can't be
> a constant. So currently that is done in fab pass (though ranger handles it 
> now too).
> Instead of having fab do it we can check PROP_last_full_fold if set and set it
> to 0 instead.
>
> Note for -Og, fab was the only place which did this conversion, so we need to
> set PROP_last_full_fold for it; later on fab will be removed and isel will do
> it instead but that is for another day.
>
> Also instead of going through fold_call_stmt to call fold_builtin_constant_p,
> fold_builtin_constant_p is called directly from 
> gimple_fold_builtin_constant_p.
> This should speed up the compiling slight :).
>
> Note fab was originally added to do this transformation during the development
> of the ssa branch.

OK.

> Bootstrapped and tested on x86_64-linux-gnu.
>
>         PR tree-optimization/121762
> gcc/ChangeLog:
>
>         * builtins.cc (fold_builtin_constant_p): Make non-static.
>         * builtins.h (fold_builtin_constant_p): New declaration.
>         * gimple-fold.cc (gimple_fold_builtin_constant_p): New function.
>         (gimple_fold_builtin): Call gimple_fold_builtin_constant_p
>         for BUILT_IN_CONSTANT_P.
>         * tree-ssa-ccp.cc (pass_fold_builtins::execute): Set 
> PROP_last_full_fold
>         on curr_properties. Remove handling of BUILT_IN_CONSTANT_P.
>
> Signed-off-by: Andrew Pinski <[email protected]>
> ---
>  gcc/builtins.cc     |  3 +--
>  gcc/builtins.h      |  1 +
>  gcc/gimple-fold.cc  | 31 +++++++++++++++++++++++++++++++
>  gcc/tree-ssa-ccp.cc |  9 +++------
>  4 files changed, 36 insertions(+), 8 deletions(-)
>
> diff --git a/gcc/builtins.cc b/gcc/builtins.cc
> index de3e389c5d4..78b561529f5 100644
> --- a/gcc/builtins.cc
> +++ b/gcc/builtins.cc
> @@ -156,7 +156,6 @@ static rtx expand_builtin_stack_address ();
>  static tree stabilize_va_list_loc (location_t, tree, int);
>  static rtx expand_builtin_expect (tree, rtx);
>  static rtx expand_builtin_expect_with_probability (tree, rtx);
> -static tree fold_builtin_constant_p (tree);
>  static tree fold_builtin_classify_type (tree);
>  static tree fold_builtin_strlen (location_t, tree, tree, tree);
>  static tree fold_builtin_inf (location_t, tree, int);
> @@ -9150,7 +9149,7 @@ builtin_mathfn_code (const_tree t)
>  /* Fold a call to __builtin_constant_p, if we know its argument ARG will
>     evaluate to a constant.  */
>
> -static tree
> +tree
>  fold_builtin_constant_p (tree arg)
>  {
>    /* We return 1 for a numeric type that's known to be a constant
> diff --git a/gcc/builtins.h b/gcc/builtins.h
> index 5a553a9c836..4f23afad9ee 100644
> --- a/gcc/builtins.h
> +++ b/gcc/builtins.h
> @@ -139,6 +139,7 @@ extern rtx expand_builtin_crc_table_based (internal_fn, 
> scalar_mode,
>  extern rtx expand_builtin (tree, rtx, rtx, machine_mode, int);
>  extern enum built_in_function builtin_mathfn_code (const_tree);
>  extern tree fold_builtin_expect (location_t, tree, tree, tree, tree);
> +extern tree fold_builtin_constant_p (tree);
>  extern bool avoid_folding_inline_builtin (tree);
>  extern tree fold_call_expr (location_t, tree, bool);
>  extern tree fold_builtin_call_array (location_t, tree, tree, int, tree *);
> diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
> index a52ca945317..70e2d26291a 100644
> --- a/gcc/gimple-fold.cc
> +++ b/gcc/gimple-fold.cc
> @@ -69,6 +69,7 @@ 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.  */
> @@ -5203,6 +5204,33 @@ gimple_fold_builtin_clear_padding 
> (gimple_stmt_iterator *gsi)
>    return true;
>  }
>
> +/* Fold __builtin_constant_p builtin.  */
> +
> +static bool
> +gimple_fold_builtin_constant_p (gimple_stmt_iterator *gsi)
> +{
> +  gcall *call = as_a<gcall*>(gsi_stmt (*gsi));
> +
> +  if (gimple_call_num_args (call) != 1)
> +    return false;
> +
> +  tree arg = gimple_call_arg (call, 0);
> +  tree result = fold_builtin_constant_p (arg);
> +
> +  /* 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))
> +    result = integer_zero_node;
> +
> +  if (!result)
> +    return false;
> +
> +  gimplify_and_update_call_from_tree (gsi, result);
> +  return true;
> +}
> +
>  /* Fold the non-target builtin at *GSI and return whether any simplification
>     was made.  */
>
> @@ -5372,6 +5400,9 @@ gimple_fold_builtin (gimple_stmt_iterator *gsi)
>      case BUILT_IN_CLEAR_PADDING:
>        return gimple_fold_builtin_clear_padding (gsi);
>
> +    case BUILT_IN_CONSTANT_P:
> +      return gimple_fold_builtin_constant_p (gsi);
> +
>      default:;
>      }
>
> diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc
> index 546cafbf7c6..c74f7cc9d0c 100644
> --- a/gcc/tree-ssa-ccp.cc
> +++ b/gcc/tree-ssa-ccp.cc
> @@ -4242,6 +4242,9 @@ pass_fold_builtins::execute (function *fun)
>    basic_block bb;
>    unsigned int todoflags = 0;
>
> +  /* Set last full fold prop if not already set. */
> +  fun->curr_properties |= PROP_last_full_fold;
> +
>    FOR_EACH_BB_FN (bb, fun)
>      {
>        gimple_stmt_iterator i;
> @@ -4280,12 +4283,6 @@ pass_fold_builtins::execute (function *fun)
>               tree result = NULL_TREE;
>               switch (DECL_FUNCTION_CODE (callee))
>                 {
> -               case BUILT_IN_CONSTANT_P:
> -                 /* 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.  */
> -                 result = integer_zero_node;
> -                 break;
>
>                 case BUILT_IN_ASSUME_ALIGNED:
>                   /* Remove __builtin_assume_aligned.  */
> --
> 2.43.0
>

Reply via email to