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.
This commit is contained in:
Andrew MacLeod
2025-12-18 10:56:44 -05:00
parent 72430fff7b
commit bf297afaec
4 changed files with 32 additions and 2 deletions

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

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

View 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

View File

@@ -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" } } */