summaryrefslogtreecommitdiff
path: root/contrib/gcc/tree-vrp.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/gcc/tree-vrp.c')
-rw-r--r--contrib/gcc/tree-vrp.c135
1 files changed, 109 insertions, 26 deletions
diff --git a/contrib/gcc/tree-vrp.c b/contrib/gcc/tree-vrp.c
index ec4c7663638c..4626485191ff 100644
--- a/contrib/gcc/tree-vrp.c
+++ b/contrib/gcc/tree-vrp.c
@@ -195,6 +195,27 @@ is_overflow_infinity (tree val)
|| operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0)));
}
+/* If VAL is now an overflow infinity, return VAL. Otherwise, return
+ the same value with TREE_OVERFLOW clear. This can be used to avoid
+ confusing a regular value with an overflow value. */
+
+static inline tree
+avoid_overflow_infinity (tree val)
+{
+ if (!is_overflow_infinity (val))
+ return val;
+
+ if (operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0))
+ return TYPE_MAX_VALUE (TREE_TYPE (val));
+ else
+ {
+#ifdef ENABLE_CHECKING
+ gcc_assert (operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0));
+#endif
+ return TYPE_MIN_VALUE (TREE_TYPE (val));
+ }
+}
+
/* Return whether VAL is equal to the maximum value of its type. This
will be true for a positive overflow infinity. We can't do a
@@ -351,23 +372,11 @@ set_value_range_to_varying (value_range_t *vr)
infinity when we shouldn't. */
static inline void
-set_value_range_to_value (value_range_t *vr, tree val)
+set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
{
gcc_assert (is_gimple_min_invariant (val));
- if (is_overflow_infinity (val))
- {
- if (operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0))
- val = TYPE_MAX_VALUE (TREE_TYPE (val));
- else
- {
-#ifdef ENABLE_CHECKING
- gcc_assert (operand_equal_p (val,
- TYPE_MIN_VALUE (TREE_TYPE (val)), 0));
-#endif
- val = TYPE_MIN_VALUE (TREE_TYPE (val));
- }
- }
- set_value_range (vr, VR_RANGE, val, val, NULL);
+ val = avoid_overflow_infinity (val);
+ set_value_range (vr, VR_RANGE, val, val, equiv);
}
/* Set value range VR to a non-negative range of type TYPE.
@@ -411,8 +420,7 @@ set_value_range_to_nonnull (value_range_t *vr, tree type)
static inline void
set_value_range_to_null (value_range_t *vr, tree type)
{
- tree zero = build_int_cst (type, 0);
- set_value_range (vr, VR_RANGE, zero, zero, vr->equiv);
+ set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
}
@@ -763,7 +771,9 @@ compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
return -2;
- if (strict_overflow_p != NULL)
+ if (strict_overflow_p != NULL
+ && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
+ && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
*strict_overflow_p = true;
if (code1 == SSA_NAME)
@@ -1028,6 +1038,8 @@ extract_range_from_assert (value_range_t *vr_p, tree expr)
cond_code = swap_tree_comparison (TREE_CODE (cond));
}
+ limit = avoid_overflow_infinity (limit);
+
type = TREE_TYPE (limit);
gcc_assert (limit != var);
@@ -1167,6 +1179,8 @@ extract_range_from_assert (value_range_t *vr_p, tree expr)
{
tree one = build_int_cst (type, 1);
max = fold_build2 (MINUS_EXPR, type, max, one);
+ if (EXPR_P (max))
+ TREE_NO_WARNING (max) = 1;
}
set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
@@ -1200,6 +1214,8 @@ extract_range_from_assert (value_range_t *vr_p, tree expr)
{
tree one = build_int_cst (type, 1);
min = fold_build2 (PLUS_EXPR, type, min, one);
+ if (EXPR_P (min))
+ TREE_NO_WARNING (min) = 1;
}
set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
@@ -1619,7 +1635,7 @@ extract_range_from_binary_expr (value_range_t *vr, tree expr)
if (TREE_CODE (op0) == SSA_NAME)
vr0 = *(get_value_range (op0));
else if (is_gimple_min_invariant (op0))
- set_value_range_to_value (&vr0, op0);
+ set_value_range_to_value (&vr0, op0, NULL);
else
set_value_range_to_varying (&vr0);
@@ -1627,7 +1643,7 @@ extract_range_from_binary_expr (value_range_t *vr, tree expr)
if (TREE_CODE (op1) == SSA_NAME)
vr1 = *(get_value_range (op1));
else if (is_gimple_min_invariant (op1))
- set_value_range_to_value (&vr1, op1);
+ set_value_range_to_value (&vr1, op1, NULL);
else
set_value_range_to_varying (&vr1);
@@ -2006,7 +2022,7 @@ extract_range_from_unary_expr (value_range_t *vr, tree expr)
if (TREE_CODE (op0) == SSA_NAME)
vr0 = *(get_value_range (op0));
else if (is_gimple_min_invariant (op0))
- set_value_range_to_value (&vr0, op0);
+ set_value_range_to_value (&vr0, op0, NULL);
else
set_value_range_to_varying (&vr0);
@@ -2091,6 +2107,8 @@ extract_range_from_unary_expr (value_range_t *vr, tree expr)
&& is_gimple_val (new_max)
&& tree_int_cst_equal (new_min, orig_min)
&& tree_int_cst_equal (new_max, orig_max)
+ && (!is_overflow_infinity (new_min)
+ || !is_overflow_infinity (new_max))
&& compare_values (new_min, new_max) <= 0
&& compare_values (new_min, new_max) >= -1)
{
@@ -2393,7 +2411,10 @@ extract_range_from_comparison (value_range_t *vr, tree expr)
its type may be different from _Bool. Convert VAL to EXPR's
type. */
val = fold_convert (TREE_TYPE (expr), val);
- set_value_range (vr, VR_RANGE, val, val, vr->equiv);
+ if (is_gimple_min_invariant (val))
+ set_value_range_to_value (vr, val, vr->equiv);
+ else
+ set_value_range (vr, VR_RANGE, val, val, vr->equiv);
}
else
set_value_range_to_varying (vr);
@@ -2424,7 +2445,7 @@ extract_range_from_expr (value_range_t *vr, tree expr)
else if (TREE_CODE_CLASS (code) == tcc_comparison)
extract_range_from_comparison (vr, expr);
else if (is_gimple_min_invariant (expr))
- set_value_range_to_value (vr, expr);
+ set_value_range_to_value (vr, expr, NULL);
else
set_value_range_to_varying (vr);
@@ -2545,6 +2566,13 @@ adjust_range_with_scev (value_range_t *vr, struct loop *loop, tree stmt,
if (compare_values (min, max) == 1)
return;
}
+
+ /* According to the loop information, the variable does not
+ overflow. If we think it does, probably because of an
+ overflow due to arithmetic on a different INF value,
+ reset now. */
+ if (is_negative_overflow_infinity (min))
+ min = tmin;
}
else
{
@@ -2557,12 +2585,61 @@ adjust_range_with_scev (value_range_t *vr, struct loop *loop, tree stmt,
if (compare_values (min, max) == 1)
return;
}
+
+ if (is_positive_overflow_infinity (max))
+ max = tmax;
}
set_value_range (vr, VR_RANGE, min, max, vr->equiv);
}
}
+/* Return true if VAR may overflow at STMT. This checks any available
+ loop information to see if we can determine that VAR does not
+ overflow. */
+
+static bool
+vrp_var_may_overflow (tree var, tree stmt)
+{
+ struct loop *l;
+ tree chrec, init, step;
+
+ if (current_loops == NULL)
+ return true;
+
+ l = loop_containing_stmt (stmt);
+ if (l == NULL)
+ return true;
+
+ chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
+ if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
+ return true;
+
+ init = initial_condition_in_loop_num (chrec, l->num);
+ step = evolution_part_in_loop_num (chrec, l->num);
+
+ if (step == NULL_TREE
+ || !is_gimple_min_invariant (step)
+ || !valid_value_p (init))
+ return true;
+
+ /* If we get here, we know something useful about VAR based on the
+ loop information. If it wraps, it may overflow. */
+
+ if (scev_probably_wraps_p (init, step, stmt,
+ current_loops->parray[CHREC_VARIABLE (chrec)],
+ true))
+ return true;
+
+ if (dump_file && (dump_flags & TDF_DETAILS) != 0)
+ {
+ print_generic_expr (dump_file, var, 0);
+ fprintf (dump_file, ": loop information indicates does not overflow\n");
+ }
+
+ return false;
+}
+
/* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
@@ -4156,7 +4233,7 @@ compare_names (enum tree_code comp, tree n1, tree n2,
t = retval = NULL_TREE;
EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
{
- bool sop;
+ bool sop = false;
value_range_t vr2 = *(vr_value[i2]);
@@ -4773,7 +4850,8 @@ vrp_visit_phi_node (tree phi)
if (vrp_val_is_max (vr_result.max))
goto varying;
- if (!needs_overflow_infinity (TREE_TYPE (vr_result.min)))
+ if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
+ || !vrp_var_may_overflow (lhs, phi))
vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
vr_result.min =
@@ -4791,7 +4869,8 @@ vrp_visit_phi_node (tree phi)
if (vrp_val_is_min (vr_result.min))
goto varying;
- if (!needs_overflow_infinity (TREE_TYPE (vr_result.max)))
+ if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
+ || !vrp_var_may_overflow (lhs, phi))
vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
vr_result.max =
@@ -4971,6 +5050,8 @@ test_for_singularity (enum tree_code cond_code, tree op0,
{
tree one = build_int_cst (TREE_TYPE (op0), 1);
max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
+ if (EXPR_P (max))
+ TREE_NO_WARNING (max) = 1;
}
}
else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
@@ -4984,6 +5065,8 @@ test_for_singularity (enum tree_code cond_code, tree op0,
{
tree one = build_int_cst (TREE_TYPE (op0), 1);
min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
+ if (EXPR_P (min))
+ TREE_NO_WARNING (min) = 1;
}
}