]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fab/gimple-fold: Move __builtin_constant_p folding to gimple-fold [PR121762]
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Fri, 19 Sep 2025 21:37:04 +0000 (14:37 -0700)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Mon, 22 Sep 2025 15:44:40 +0000 (08:44 -0700)
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.

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 <andrew.pinski@oss.qualcomm.com>
gcc/builtins.cc
gcc/builtins.h
gcc/gimple-fold.cc
gcc/tree-ssa-ccp.cc

index de3e389c5d4bf49abe009cb0966d68031a13d3c2..78b561529f530e02eeb1b8aa36082e55074803bc 100644 (file)
@@ -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
index 5a553a9c8365d9e6cfb9fa5cbf8b1003d3acd1ca..4f23afad9eed8257c9d9a0c8a6456d53c87482d2 100644 (file)
@@ -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 *);
index 7d130ea383291cc3490b4bfb47c5103c5b615680..229b1b301a4f7f56c619f21c56543ffae21d82d0 100644 (file)
@@ -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.  */
@@ -5206,6 +5207,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.  */
 
@@ -5375,6 +5403,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:;
     }
 
index 546cafbf7c69ebe474463b34c2d9ae51a8679db6..c74f7cc9d0c50c1598d1eb430a58e95da0f3397f 100644 (file)
@@ -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.  */