]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
VRP should only recompute known statements.
authorAndrew MacLeod <amacleod@redhat.com>
Thu, 18 Dec 2025 15:56:44 +0000 (10:56 -0500)
committerAndrew MacLeod <amacleod@redhat.com>
Fri, 19 Dec 2025 14:49:52 +0000 (09:49 -0500)
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.

gcc/gimple-range-gori.cc
gcc/gimple-range-op.cc
gcc/gimple-range-op.h
gcc/testsuite/gcc.dg/pr123205.c [new file with mode: 0644]

index 9761abffc00573192e8e0ddd599261aaedbe290f..91fd4784ce8a46ba0b2363b97e3836512a9622cd 100644 (file)
@@ -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)
index 3a22606180bb89904aa194262df5eb309fad1bdc..e563d054135d69398aa82d184b4e8dc76375ae2d 100644 (file)
@@ -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):
index 239740d18e56bc9fdeb3cb30adb4a255fe330d85..2bdacd5a80b778c5acd57e46c8c108788811fd56 100644 (file)
@@ -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 (file)
index 0000000..80d2ca0
--- /dev/null
@@ -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" } } */