https://gcc.gnu.org/g:bf297afaec57df4d5dd8407b50a9ca2cf8de0380

commit r16-6281-gbf297afaec57df4d5dd8407b50a9ca2cf8de0380
Author: Andrew MacLeod <[email protected]>
Date:   Thu Dec 18 10:56:44 2025 -0500

    VRP should only recompute known statements.
    
    GORI should only recompute ranges for range-op statements that are known
    to be safe.   Disable it for builtin_constant_p.
    
            PR tree-optimization/123205
            gcc/
            * gimple-range-gori.cc (gori_compute::may_recompute_p): Only
            recompute range-op statements.
            * gimple-range-op.cc (gimple_range_op_handler): Default
            recomputation to true.
            (maybe_builtin_call): CFN_BUILT_IN_CONSTANT_P should not be
            recomputable.
            * gimple-range-op.h (recomputable_p): New.
            (recomputable_p): New.
    
            gcc/testsuite/
            * gcc.dg/pr123205.c: New.

Diff:
---
 gcc/gimple-range-gori.cc        |  5 +++--
 gcc/gimple-range-op.cc          |  4 ++++
 gcc/gimple-range-op.h           |  2 ++
 gcc/testsuite/gcc.dg/pr123205.c | 23 +++++++++++++++++++++++
 4 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/gcc/gimple-range-gori.cc b/gcc/gimple-range-gori.cc
index 9761abffc005..91fd4784ce8a 100644
--- a/gcc/gimple-range-gori.cc
+++ b/gcc/gimple-range-gori.cc
@@ -1325,9 +1325,10 @@ gori_compute::may_recompute_p (tree name, basic_block 
bb, int depth)
   if (!dep1)
     return false;
 
-  // Don't recalculate PHIs or statements with side_effects.
+  // Only recalculate range-op statements that are recomputable.
   gimple *s = SSA_NAME_DEF_STMT (name);
-  if (is_a<gphi *> (s) || gimple_has_side_effects (s))
+  gimple_range_op_handler handler (s);
+  if (!handler || !handler.recomputable_p ())
     return false;
 
   if (!dep2)
diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc
index 3a22606180bb..e563d054135d 100644
--- a/gcc/gimple-range-op.cc
+++ b/gcc/gimple-range-op.cc
@@ -125,6 +125,8 @@ gimple_range_op_handler::gimple_range_op_handler (gimple *s)
   m_stmt = s;
   m_op1 = NULL_TREE;
   m_op2 = NULL_TREE;
+  // Recomputation defaults to TRUE.
+  m_recomputable = true;
 
   if (oper)
     switch (gimple_code (m_stmt))
@@ -1413,6 +1415,8 @@ gimple_range_op_handler::maybe_builtin_call ()
        m_operator = &op_cfn_constant_p;
       else if (frange::supports_p (TREE_TYPE (m_op1)))
        m_operator = &op_cfn_constant_float_p;
+      // builtin_constant_p should not be recomputed.  See PR 123205.
+      m_recomputable = false;
       break;
 
     CASE_FLT_FN (CFN_BUILT_IN_SIGNBIT):
diff --git a/gcc/gimple-range-op.h b/gcc/gimple-range-op.h
index 239740d18e56..2bdacd5a80b7 100644
--- a/gcc/gimple-range-op.h
+++ b/gcc/gimple-range-op.h
@@ -39,11 +39,13 @@ public:
                 relation_trio = TRIO_VARYING);
   bool calc_op2 (vrange &r, const vrange &lhs_range, const vrange &op1_range,
                 relation_trio = TRIO_VARYING);
+  inline bool recomputable_p () { return m_recomputable; }
 private:
   void maybe_builtin_call ();
   void maybe_non_standard ();
   gimple *m_stmt;
   tree m_op1, m_op2;
+  bool m_recomputable;
 };
 
 // Given stmt S, fill VEC, up to VEC_SIZE elements, with relevant ssa-names
diff --git a/gcc/testsuite/gcc.dg/pr123205.c b/gcc/testsuite/gcc.dg/pr123205.c
new file mode 100644
index 000000000000..80d2ca0fc657
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr123205.c
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+void isconst (int, int);
+void nonconst (int, int);
+
+int foo (int x)
+{
+ int y =  __builtin_constant_p (x);
+ if (y)
+   isconst (y, x);
+  else
+   nonconst (y, x);
+
+ if (x == 24)
+   {
+     /* Y should have the same value as earlier. */
+     if (y)
+       isconst (y, x);
+     else
+       nonconst (y, x);
+   }
+}
+/* { dg-final { scan-tree-dump-not "isconst"  "optimized" } } */

Reply via email to