blob: 797fa427c5000676e8c95b362c38b766ef375c7f [file] [log] [blame]
/* Support routines for Value Range Propagation (VRP).
Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
Contributed by Diego Novillo <dnovillo@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "ggc.h"
#include "flags.h"
#include "tree.h"
#include "basic-block.h"
#include "tree-flow.h"
#include "tree-pass.h"
#include "tree-dump.h"
#include "timevar.h"
#include "diagnostic.h"
#include "toplev.h"
#include "intl.h"
#include "cfgloop.h"
#include "tree-scalar-evolution.h"
#include "tree-ssa-propagate.h"
#include "tree-chrec.h"
/* Set of SSA names found live during the RPO traversal of the function
for still active basic-blocks. */
static sbitmap *live;
/* Return true if the SSA name NAME is live on the edge E. */
static bool
live_on_edge (edge e, tree name)
{
return (live[e->dest->index]
&& TEST_BIT (live[e->dest->index], SSA_NAME_VERSION (name)));
}
/* Local functions. */
static int compare_values (tree val1, tree val2);
static int compare_values_warnv (tree val1, tree val2, bool *);
static void vrp_meet (value_range_t *, value_range_t *);
static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
tree, tree, bool, bool *,
bool *);
/* Location information for ASSERT_EXPRs. Each instance of this
structure describes an ASSERT_EXPR for an SSA name. Since a single
SSA name may have more than one assertion associated with it, these
locations are kept in a linked list attached to the corresponding
SSA name. */
struct assert_locus_d
{
/* Basic block where the assertion would be inserted. */
basic_block bb;
/* Some assertions need to be inserted on an edge (e.g., assertions
generated by COND_EXPRs). In those cases, BB will be NULL. */
edge e;
/* Pointer to the statement that generated this assertion. */
gimple_stmt_iterator si;
/* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
enum tree_code comp_code;
/* Value being compared against. */
tree val;
/* Expression to compare. */
tree expr;
/* Next node in the linked list. */
struct assert_locus_d *next;
};
typedef struct assert_locus_d *assert_locus_t;
/* If bit I is present, it means that SSA name N_i has a list of
assertions that should be inserted in the IL. */
static bitmap need_assert_for;
/* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
holds a list of ASSERT_LOCUS_T nodes that describe where
ASSERT_EXPRs for SSA name N_I should be inserted. */
static assert_locus_t *asserts_for;
/* Value range array. After propagation, VR_VALUE[I] holds the range
of values that SSA name N_I may take. */
static value_range_t **vr_value;
/* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
number of executable edges we saw the last time we visited the
node. */
static int *vr_phi_edge_counts;
typedef struct {
gimple stmt;
tree vec;
} switch_update;
static VEC (edge, heap) *to_remove_edges;
DEF_VEC_O(switch_update);
DEF_VEC_ALLOC_O(switch_update, heap);
static VEC (switch_update, heap) *to_update_switch_stmts;
/* Return the maximum value for TYPEs base type. */
static inline tree
vrp_val_max (const_tree type)
{
if (!INTEGRAL_TYPE_P (type))
return NULL_TREE;
/* For integer sub-types the values for the base type are relevant. */
if (TREE_TYPE (type))
type = TREE_TYPE (type);
return TYPE_MAX_VALUE (type);
}
/* Return the minimum value for TYPEs base type. */
static inline tree
vrp_val_min (const_tree type)
{
if (!INTEGRAL_TYPE_P (type))
return NULL_TREE;
/* For integer sub-types the values for the base type are relevant. */
if (TREE_TYPE (type))
type = TREE_TYPE (type);
return TYPE_MIN_VALUE (type);
}
/* 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
simple equality comparison with TYPE_MAX_VALUE because C typedefs
and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
to the integer constant with the same value in the type. */
static inline bool
vrp_val_is_max (const_tree val)
{
tree type_max = vrp_val_max (TREE_TYPE (val));
return (val == type_max
|| (type_max != NULL_TREE
&& operand_equal_p (val, type_max, 0)));
}
/* Return whether VAL is equal to the minimum value of its type. This
will be true for a negative overflow infinity. */
static inline bool
vrp_val_is_min (const_tree val)
{
tree type_min = vrp_val_min (TREE_TYPE (val));
return (val == type_min
|| (type_min != NULL_TREE
&& operand_equal_p (val, type_min, 0)));
}
/* Return whether TYPE should use an overflow infinity distinct from
TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
represent a signed overflow during VRP computations. An infinity
is distinct from a half-range, which will go from some number to
TYPE_{MIN,MAX}_VALUE. */
static inline bool
needs_overflow_infinity (const_tree type)
{
return (INTEGRAL_TYPE_P (type)
&& !TYPE_OVERFLOW_WRAPS (type)
/* Integer sub-types never overflow as they are never
operands of arithmetic operators. */
&& !(TREE_TYPE (type) && TREE_TYPE (type) != type));
}
/* Return whether TYPE can support our overflow infinity
representation: we use the TREE_OVERFLOW flag, which only exists
for constants. If TYPE doesn't support this, we don't optimize
cases which would require signed overflow--we drop them to
VARYING. */
static inline bool
supports_overflow_infinity (const_tree type)
{
tree min = vrp_val_min (type), max = vrp_val_max (type);
#ifdef ENABLE_CHECKING
gcc_assert (needs_overflow_infinity (type));
#endif
return (min != NULL_TREE
&& CONSTANT_CLASS_P (min)
&& max != NULL_TREE
&& CONSTANT_CLASS_P (max));
}
/* VAL is the maximum or minimum value of a type. Return a
corresponding overflow infinity. */
static inline tree
make_overflow_infinity (tree val)
{
#ifdef ENABLE_CHECKING
gcc_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
#endif
val = copy_node (val);
TREE_OVERFLOW (val) = 1;
return val;
}
/* Return a negative overflow infinity for TYPE. */
static inline tree
negative_overflow_infinity (tree type)
{
#ifdef ENABLE_CHECKING
gcc_assert (supports_overflow_infinity (type));
#endif
return make_overflow_infinity (vrp_val_min (type));
}
/* Return a positive overflow infinity for TYPE. */
static inline tree
positive_overflow_infinity (tree type)
{
#ifdef ENABLE_CHECKING
gcc_assert (supports_overflow_infinity (type));
#endif
return make_overflow_infinity (vrp_val_max (type));
}
/* Return whether VAL is a negative overflow infinity. */
static inline bool
is_negative_overflow_infinity (const_tree val)
{
return (needs_overflow_infinity (TREE_TYPE (val))
&& CONSTANT_CLASS_P (val)
&& TREE_OVERFLOW (val)
&& vrp_val_is_min (val));
}
/* Return whether VAL is a positive overflow infinity. */
static inline bool
is_positive_overflow_infinity (const_tree val)
{
return (needs_overflow_infinity (TREE_TYPE (val))
&& CONSTANT_CLASS_P (val)
&& TREE_OVERFLOW (val)
&& vrp_val_is_max (val));
}
/* Return whether VAL is a positive or negative overflow infinity. */
static inline bool
is_overflow_infinity (const_tree val)
{
return (needs_overflow_infinity (TREE_TYPE (val))
&& CONSTANT_CLASS_P (val)
&& TREE_OVERFLOW (val)
&& (vrp_val_is_min (val) || vrp_val_is_max (val)));
}
/* Return whether STMT has a constant rhs that is_overflow_infinity. */
static inline bool
stmt_overflow_infinity (gimple stmt)
{
if (is_gimple_assign (stmt)
&& get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
GIMPLE_SINGLE_RHS)
return is_overflow_infinity (gimple_assign_rhs1 (stmt));
return false;
}
/* 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 (vrp_val_is_max (val))
return vrp_val_max (TREE_TYPE (val));
else
{
#ifdef ENABLE_CHECKING
gcc_assert (vrp_val_is_min (val));
#endif
return vrp_val_min (TREE_TYPE (val));
}
}
/* Return true if ARG is marked with the nonnull attribute in the
current function signature. */
static bool
nonnull_arg_p (const_tree arg)
{
tree t, attrs, fntype;
unsigned HOST_WIDE_INT arg_num;
gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
/* The static chain decl is always non null. */
if (arg == cfun->static_chain_decl)
return true;
fntype = TREE_TYPE (current_function_decl);
attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype));
/* If "nonnull" wasn't specified, we know nothing about the argument. */
if (attrs == NULL_TREE)
return false;
/* If "nonnull" applies to all the arguments, then ARG is non-null. */
if (TREE_VALUE (attrs) == NULL_TREE)
return true;
/* Get the position number for ARG in the function signature. */
for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
t;
t = TREE_CHAIN (t), arg_num++)
{
if (t == arg)
break;
}
gcc_assert (t == arg);
/* Now see if ARG_NUM is mentioned in the nonnull list. */
for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
{
if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
return true;
}
return false;
}
/* Set value range VR to VR_VARYING. */
static inline void
set_value_range_to_varying (value_range_t *vr)
{
vr->type = VR_VARYING;
vr->min = vr->max = NULL_TREE;
if (vr->equiv)
bitmap_clear (vr->equiv);
}
/* Set value range VR to {T, MIN, MAX, EQUIV}. */
static void
set_value_range (value_range_t *vr, enum value_range_type t, tree min,
tree max, bitmap equiv)
{
#if defined ENABLE_CHECKING
/* Check the validity of the range. */
if (t == VR_RANGE || t == VR_ANTI_RANGE)
{
int cmp;
gcc_assert (min && max);
if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
cmp = compare_values (min, max);
gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
if (needs_overflow_infinity (TREE_TYPE (min)))
gcc_assert (!is_overflow_infinity (min)
|| !is_overflow_infinity (max));
}
if (t == VR_UNDEFINED || t == VR_VARYING)
gcc_assert (min == NULL_TREE && max == NULL_TREE);
if (t == VR_UNDEFINED || t == VR_VARYING)
gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
#endif
vr->type = t;
vr->min = min;
vr->max = max;
/* Since updating the equivalence set involves deep copying the
bitmaps, only do it if absolutely necessary. */
if (vr->equiv == NULL
&& equiv != NULL)
vr->equiv = BITMAP_ALLOC (NULL);
if (equiv != vr->equiv)
{
if (equiv && !bitmap_empty_p (equiv))
bitmap_copy (vr->equiv, equiv);
else
bitmap_clear (vr->equiv);
}
}
/* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
This means adjusting T, MIN and MAX representing the case of a
wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
In corner cases where MAX+1 or MIN-1 wraps this will fall back
to varying.
This routine exists to ease canonicalization in the case where we
extract ranges from var + CST op limit. */
static void
set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
tree min, tree max, bitmap equiv)
{
/* Nothing to canonicalize for symbolic or unknown or varying ranges. */
if ((t != VR_RANGE
&& t != VR_ANTI_RANGE)
|| TREE_CODE (min) != INTEGER_CST
|| TREE_CODE (max) != INTEGER_CST)
{
set_value_range (vr, t, min, max, equiv);
return;
}
/* Wrong order for min and max, to swap them and the VR type we need
to adjust them. */
if (tree_int_cst_lt (max, min))
{
tree one = build_int_cst (TREE_TYPE (min), 1);
tree tmp = int_const_binop (PLUS_EXPR, max, one, 0);
max = int_const_binop (MINUS_EXPR, min, one, 0);
min = tmp;
/* There's one corner case, if we had [C+1, C] before we now have
that again. But this represents an empty value range, so drop
to varying in this case. */
if (tree_int_cst_lt (max, min))
{
set_value_range_to_varying (vr);
return;
}
t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
}
/* Anti-ranges that can be represented as ranges should be so. */
if (t == VR_ANTI_RANGE)
{
bool is_min = vrp_val_is_min (min);
bool is_max = vrp_val_is_max (max);
if (is_min && is_max)
{
/* We cannot deal with empty ranges, drop to varying. */
set_value_range_to_varying (vr);
return;
}
else if (is_min
/* As a special exception preserve non-null ranges. */
&& !(TYPE_UNSIGNED (TREE_TYPE (min))
&& integer_zerop (max)))
{
tree one = build_int_cst (TREE_TYPE (max), 1);
min = int_const_binop (PLUS_EXPR, max, one, 0);
max = vrp_val_max (TREE_TYPE (max));
t = VR_RANGE;
}
else if (is_max)
{
tree one = build_int_cst (TREE_TYPE (min), 1);
max = int_const_binop (MINUS_EXPR, min, one, 0);
min = vrp_val_min (TREE_TYPE (min));
t = VR_RANGE;
}
}
set_value_range (vr, t, min, max, equiv);
}
/* Copy value range FROM into value range TO. */
static inline void
copy_value_range (value_range_t *to, value_range_t *from)
{
set_value_range (to, from->type, from->min, from->max, from->equiv);
}
/* Set value range VR to a single value. This function is only called
with values we get from statements, and exists to clear the
TREE_OVERFLOW flag so that we don't think we have an overflow
infinity when we shouldn't. */
static inline void
set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
{
gcc_assert (is_gimple_min_invariant (val));
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.
OVERFLOW_INFINITY indicates whether to use an overflow infinity
rather than TYPE_MAX_VALUE; this should be true if we determine
that the range is nonnegative based on the assumption that signed
overflow does not occur. */
static inline void
set_value_range_to_nonnegative (value_range_t *vr, tree type,
bool overflow_infinity)
{
tree zero;
if (overflow_infinity && !supports_overflow_infinity (type))
{
set_value_range_to_varying (vr);
return;
}
zero = build_int_cst (type, 0);
set_value_range (vr, VR_RANGE, zero,
(overflow_infinity
? positive_overflow_infinity (type)
: TYPE_MAX_VALUE (type)),
vr->equiv);
}
/* Set value range VR to a non-NULL range of type TYPE. */
static inline void
set_value_range_to_nonnull (value_range_t *vr, tree type)
{
tree zero = build_int_cst (type, 0);
set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
}
/* Set value range VR to a NULL range of type TYPE. */
static inline void
set_value_range_to_null (value_range_t *vr, tree type)
{
set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
}
/* Set value range VR to a range of a truthvalue of type TYPE. */
static inline void
set_value_range_to_truthvalue (value_range_t *vr, tree type)
{
if (TYPE_PRECISION (type) == 1)
set_value_range_to_varying (vr);
else
set_value_range (vr, VR_RANGE,
build_int_cst (type, 0), build_int_cst (type, 1),
vr->equiv);
}
/* Set value range VR to VR_UNDEFINED. */
static inline void
set_value_range_to_undefined (value_range_t *vr)
{
vr->type = VR_UNDEFINED;
vr->min = vr->max = NULL_TREE;
if (vr->equiv)
bitmap_clear (vr->equiv);
}
/* If abs (min) < abs (max), set VR to [-max, max], if
abs (min) >= abs (max), set VR to [-min, min]. */
static void
abs_extent_range (value_range_t *vr, tree min, tree max)
{
int cmp;
gcc_assert (TREE_CODE (min) == INTEGER_CST);
gcc_assert (TREE_CODE (max) == INTEGER_CST);
gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
{
set_value_range_to_varying (vr);
return;
}
cmp = compare_values (min, max);
if (cmp == -1)
min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
else if (cmp == 0 || cmp == 1)
{
max = min;
min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
}
else
{
set_value_range_to_varying (vr);
return;
}
set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
}
/* Return value range information for VAR.
If we have no values ranges recorded (ie, VRP is not running), then
return NULL. Otherwise create an empty range if none existed for VAR. */
static value_range_t *
get_value_range (const_tree var)
{
value_range_t *vr;
tree sym;
unsigned ver = SSA_NAME_VERSION (var);
/* If we have no recorded ranges, then return NULL. */
if (! vr_value)
return NULL;
vr = vr_value[ver];
if (vr)
return vr;
/* Create a default value range. */
vr_value[ver] = vr = XCNEW (value_range_t);
/* Defer allocating the equivalence set. */
vr->equiv = NULL;
/* If VAR is a default definition, the variable can take any value
in VAR's type. */
sym = SSA_NAME_VAR (var);
if (SSA_NAME_IS_DEFAULT_DEF (var))
{
/* Try to use the "nonnull" attribute to create ~[0, 0]
anti-ranges for pointers. Note that this is only valid with
default definitions of PARM_DECLs. */
if (TREE_CODE (sym) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (sym))
&& nonnull_arg_p (sym))
set_value_range_to_nonnull (vr, TREE_TYPE (sym));
else
set_value_range_to_varying (vr);
}
return vr;
}
/* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
static inline bool
vrp_operand_equal_p (const_tree val1, const_tree val2)
{
if (val1 == val2)
return true;
if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
return false;
if (is_overflow_infinity (val1))
return is_overflow_infinity (val2);
return true;
}
/* Return true, if the bitmaps B1 and B2 are equal. */
static inline bool
vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
{
return (b1 == b2
|| (b1 && b2
&& bitmap_equal_p (b1, b2)));
}
/* Update the value range and equivalence set for variable VAR to
NEW_VR. Return true if NEW_VR is different from VAR's previous
value.
NOTE: This function assumes that NEW_VR is a temporary value range
object created for the sole purpose of updating VAR's range. The
storage used by the equivalence set from NEW_VR will be freed by
this function. Do not call update_value_range when NEW_VR
is the range object associated with another SSA name. */
static inline bool
update_value_range (const_tree var, value_range_t *new_vr)
{
value_range_t *old_vr;
bool is_new;
/* Update the value range, if necessary. */
old_vr = get_value_range (var);
is_new = old_vr->type != new_vr->type
|| !vrp_operand_equal_p (old_vr->min, new_vr->min)
|| !vrp_operand_equal_p (old_vr->max, new_vr->max)
|| !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
if (is_new)
set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
new_vr->equiv);
BITMAP_FREE (new_vr->equiv);
return is_new;
}
/* Add VAR and VAR's equivalence set to EQUIV. This is the central
point where equivalence processing can be turned on/off. */
static void
add_equivalence (bitmap *equiv, const_tree var)
{
unsigned ver = SSA_NAME_VERSION (var);
value_range_t *vr = vr_value[ver];
if (*equiv == NULL)
*equiv = BITMAP_ALLOC (NULL);
bitmap_set_bit (*equiv, ver);
if (vr && vr->equiv)
bitmap_ior_into (*equiv, vr->equiv);
}
/* Return true if VR is ~[0, 0]. */
static inline bool
range_is_nonnull (value_range_t *vr)
{
return vr->type == VR_ANTI_RANGE
&& integer_zerop (vr->min)
&& integer_zerop (vr->max);
}
/* Return true if VR is [0, 0]. */
static inline bool
range_is_null (value_range_t *vr)
{
return vr->type == VR_RANGE
&& integer_zerop (vr->min)
&& integer_zerop (vr->max);
}
/* Return true if value range VR involves at least one symbol. */
static inline bool
symbolic_range_p (value_range_t *vr)
{
return (!is_gimple_min_invariant (vr->min)
|| !is_gimple_min_invariant (vr->max));
}
/* Return true if value range VR uses an overflow infinity. */
static inline bool
overflow_infinity_range_p (value_range_t *vr)
{
return (vr->type == VR_RANGE
&& (is_overflow_infinity (vr->min)
|| is_overflow_infinity (vr->max)));
}
/* Return false if we can not make a valid comparison based on VR;
this will be the case if it uses an overflow infinity and overflow
is not undefined (i.e., -fno-strict-overflow is in effect).
Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
uses an overflow infinity. */
static bool
usable_range_p (value_range_t *vr, bool *strict_overflow_p)
{
gcc_assert (vr->type == VR_RANGE);
if (is_overflow_infinity (vr->min))
{
*strict_overflow_p = true;
if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
return false;
}
if (is_overflow_infinity (vr->max))
{
*strict_overflow_p = true;
if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
return false;
}
return true;
}
/* Like tree_expr_nonnegative_warnv_p, but this function uses value
ranges obtained so far. */
static bool
vrp_expr_computes_nonnegative (tree expr, bool *strict_overflow_p)
{
return (tree_expr_nonnegative_warnv_p (expr, strict_overflow_p)
|| (TREE_CODE (expr) == SSA_NAME
&& ssa_name_nonnegative_p (expr)));
}
/* Return true if the result of assignment STMT is know to be non-negative.
If the return value is based on the assumption that signed overflow is
undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
*STRICT_OVERFLOW_P.*/
static bool
gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
{
enum tree_code code = gimple_assign_rhs_code (stmt);
switch (get_gimple_rhs_class (code))
{
case GIMPLE_UNARY_RHS:
return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
gimple_expr_type (stmt),
gimple_assign_rhs1 (stmt),
strict_overflow_p);
case GIMPLE_BINARY_RHS:
return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
gimple_expr_type (stmt),
gimple_assign_rhs1 (stmt),
gimple_assign_rhs2 (stmt),
strict_overflow_p);
case GIMPLE_SINGLE_RHS:
return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
strict_overflow_p);
case GIMPLE_INVALID_RHS:
gcc_unreachable ();
default:
gcc_unreachable ();
}
}
/* Return true if return value of call STMT is know to be non-negative.
If the return value is based on the assumption that signed overflow is
undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
*STRICT_OVERFLOW_P.*/
static bool
gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
{
tree arg0 = gimple_call_num_args (stmt) > 0 ?
gimple_call_arg (stmt, 0) : NULL_TREE;
tree arg1 = gimple_call_num_args (stmt) > 1 ?
gimple_call_arg (stmt, 1) : NULL_TREE;
return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
gimple_call_fndecl (stmt),
arg0,
arg1,
strict_overflow_p);
}
/* Return true if STMT is know to to compute a non-negative value.
If the return value is based on the assumption that signed overflow is
undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
*STRICT_OVERFLOW_P.*/
static bool
gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
{
switch (gimple_code (stmt))
{
case GIMPLE_ASSIGN:
return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
case GIMPLE_CALL:
return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
default:
gcc_unreachable ();
}
}
/* Return true if the result of assignment STMT is know to be non-zero.
If the return value is based on the assumption that signed overflow is
undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
*STRICT_OVERFLOW_P.*/
static bool
gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
{
enum tree_code code = gimple_assign_rhs_code (stmt);
switch (get_gimple_rhs_class (code))
{
case GIMPLE_UNARY_RHS:
return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
gimple_expr_type (stmt),
gimple_assign_rhs1 (stmt),
strict_overflow_p);
case GIMPLE_BINARY_RHS:
return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
gimple_expr_type (stmt),
gimple_assign_rhs1 (stmt),
gimple_assign_rhs2 (stmt),
strict_overflow_p);
case GIMPLE_SINGLE_RHS:
return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
strict_overflow_p);
case GIMPLE_INVALID_RHS:
gcc_unreachable ();
default:
gcc_unreachable ();
}
}
/* Return true if STMT is know to to compute a non-zero value.
If the return value is based on the assumption that signed overflow is
undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
*STRICT_OVERFLOW_P.*/
static bool
gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
{
switch (gimple_code (stmt))
{
case GIMPLE_ASSIGN:
return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
case GIMPLE_CALL:
return gimple_alloca_call_p (stmt);
default:
gcc_unreachable ();
}
}
/* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
obtained so far. */
static bool
vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
{
if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
return true;
/* If we have an expression of the form &X->a, then the expression
is nonnull if X is nonnull. */
if (is_gimple_assign (stmt)
&& gimple_assign_rhs_code (stmt) == ADDR_EXPR)
{
tree expr = gimple_assign_rhs1 (stmt);
tree base = get_base_address (TREE_OPERAND (expr, 0));
if (base != NULL_TREE
&& TREE_CODE (base) == INDIRECT_REF
&& TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
{
value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
if (range_is_nonnull (vr))
return true;
}
}
return false;
}
/* Returns true if EXPR is a valid value (as expected by compare_values) --
a gimple invariant, or SSA_NAME +- CST. */
static bool
valid_value_p (tree expr)
{
if (TREE_CODE (expr) == SSA_NAME)
return true;
if (TREE_CODE (expr) == PLUS_EXPR
|| TREE_CODE (expr) == MINUS_EXPR)
return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
&& TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
return is_gimple_min_invariant (expr);
}
/* Return
1 if VAL < VAL2
0 if !(VAL < VAL2)
-2 if those are incomparable. */
static inline int
operand_less_p (tree val, tree val2)
{
/* LT is folded faster than GE and others. Inline the common case. */
if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
{
if (TYPE_UNSIGNED (TREE_TYPE (val)))
return INT_CST_LT_UNSIGNED (val, val2);
else
{
if (INT_CST_LT (val, val2))
return 1;
}
}
else
{
tree tcmp;
fold_defer_overflow_warnings ();
tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
fold_undefer_and_ignore_overflow_warnings ();
if (!tcmp
|| TREE_CODE (tcmp) != INTEGER_CST)
return -2;
if (!integer_zerop (tcmp))
return 1;
}
/* val >= val2, not considering overflow infinity. */
if (is_negative_overflow_infinity (val))
return is_negative_overflow_infinity (val2) ? 0 : 1;
else if (is_positive_overflow_infinity (val2))
return is_positive_overflow_infinity (val) ? 0 : 1;
return 0;
}
/* Compare two values VAL1 and VAL2. Return
-2 if VAL1 and VAL2 cannot be compared at compile-time,
-1 if VAL1 < VAL2,
0 if VAL1 == VAL2,
+1 if VAL1 > VAL2, and
+2 if VAL1 != VAL2
This is similar to tree_int_cst_compare but supports pointer values
and values that cannot be compared at compile time.
If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
true if the return value is only valid if we assume that signed
overflow is undefined. */
static int
compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
{
if (val1 == val2)
return 0;
/* Below we rely on the fact that VAL1 and VAL2 are both pointers or
both integers. */
gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
== POINTER_TYPE_P (TREE_TYPE (val2)));
/* Convert the two values into the same type. This is needed because
sizetype causes sign extension even for unsigned types. */
val2 = fold_convert (TREE_TYPE (val1), val2);
STRIP_USELESS_TYPE_CONVERSION (val2);
if ((TREE_CODE (val1) == SSA_NAME
|| TREE_CODE (val1) == PLUS_EXPR
|| TREE_CODE (val1) == MINUS_EXPR)
&& (TREE_CODE (val2) == SSA_NAME
|| TREE_CODE (val2) == PLUS_EXPR
|| TREE_CODE (val2) == MINUS_EXPR))
{
tree n1, c1, n2, c2;
enum tree_code code1, code2;
/* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
same name, return -2. */
if (TREE_CODE (val1) == SSA_NAME)
{
code1 = SSA_NAME;
n1 = val1;
c1 = NULL_TREE;
}
else
{
code1 = TREE_CODE (val1);
n1 = TREE_OPERAND (val1, 0);
c1 = TREE_OPERAND (val1, 1);
if (tree_int_cst_sgn (c1) == -1)
{
if (is_negative_overflow_infinity (c1))
return -2;
c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
if (!c1)
return -2;
code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
}
}
if (TREE_CODE (val2) == SSA_NAME)
{
code2 = SSA_NAME;
n2 = val2;
c2 = NULL_TREE;
}
else
{
code2 = TREE_CODE (val2);
n2 = TREE_OPERAND (val2, 0);
c2 = TREE_OPERAND (val2, 1);
if (tree_int_cst_sgn (c2) == -1)
{
if (is_negative_overflow_infinity (c2))
return -2;
c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
if (!c2)
return -2;
code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
}
}
/* Both values must use the same name. */
if (n1 != n2)
return -2;
if (code1 == SSA_NAME
&& code2 == SSA_NAME)
/* NAME == NAME */
return 0;
/* If overflow is defined we cannot simplify more. */
if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
return -2;
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)
{
if (code2 == PLUS_EXPR)
/* NAME < NAME + CST */
return -1;
else if (code2 == MINUS_EXPR)
/* NAME > NAME - CST */
return 1;
}
else if (code1 == PLUS_EXPR)
{
if (code2 == SSA_NAME)
/* NAME + CST > NAME */
return 1;
else if (code2 == PLUS_EXPR)
/* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
return compare_values_warnv (c1, c2, strict_overflow_p);
else if (code2 == MINUS_EXPR)
/* NAME + CST1 > NAME - CST2 */
return 1;
}
else if (code1 == MINUS_EXPR)
{
if (code2 == SSA_NAME)
/* NAME - CST < NAME */
return -1;
else if (code2 == PLUS_EXPR)
/* NAME - CST1 < NAME + CST2 */
return -1;
else if (code2 == MINUS_EXPR)
/* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
C1 and C2 are swapped in the call to compare_values. */
return compare_values_warnv (c2, c1, strict_overflow_p);
}
gcc_unreachable ();
}
/* We cannot compare non-constants. */
if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
return -2;
if (!POINTER_TYPE_P (TREE_TYPE (val1)))
{
/* We cannot compare overflowed values, except for overflow
infinities. */
if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
{
if (strict_overflow_p != NULL)
*strict_overflow_p = true;
if (is_negative_overflow_infinity (val1))
return is_negative_overflow_infinity (val2) ? 0 : -1;
else if (is_negative_overflow_infinity (val2))
return 1;
else if (is_positive_overflow_infinity (val1))
return is_positive_overflow_infinity (val2) ? 0 : 1;
else if (is_positive_overflow_infinity (val2))
return -1;
return -2;
}
return tree_int_cst_compare (val1, val2);
}
else
{
tree t;
/* First see if VAL1 and VAL2 are not the same. */
if (val1 == val2 || operand_equal_p (val1, val2, 0))
return 0;
/* If VAL1 is a lower address than VAL2, return -1. */
if (operand_less_p (val1, val2) == 1)
return -1;
/* If VAL1 is a higher address than VAL2, return +1. */
if (operand_less_p (val2, val1) == 1)
return 1;
/* If VAL1 is different than VAL2, return +2.
For integer constants we either have already returned -1 or 1
or they are equivalent. We still might succeed in proving
something about non-trivial operands. */
if (TREE_CODE (val1) != INTEGER_CST
|| TREE_CODE (val2) != INTEGER_CST)
{
t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
if (t && integer_onep (t))
return 2;
}
return -2;
}
}
/* Compare values like compare_values_warnv, but treat comparisons of
nonconstants which rely on undefined overflow as incomparable. */
static int
compare_values (tree val1, tree val2)
{
bool sop;
int ret;
sop = false;
ret = compare_values_warnv (val1, val2, &sop);
if (sop
&& (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
ret = -2;
return ret;
}
/* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
0 if VAL is not inside VR,
-2 if we cannot tell either way.
FIXME, the current semantics of this functions are a bit quirky
when taken in the context of VRP. In here we do not care
about VR's type. If VR is the anti-range ~[3, 5] the call
value_inside_range (4, VR) will return 1.
This is counter-intuitive in a strict sense, but the callers
currently expect this. They are calling the function
merely to determine whether VR->MIN <= VAL <= VR->MAX. The
callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
themselves.
This also applies to value_ranges_intersect_p and
range_includes_zero_p. The semantics of VR_RANGE and
VR_ANTI_RANGE should be encoded here, but that also means
adapting the users of these functions to the new semantics.
Benchmark compile/20001226-1.c compilation time after changing this
function. */
static inline int
value_inside_range (tree val, value_range_t * vr)
{
int cmp1, cmp2;
cmp1 = operand_less_p (val, vr->min);
if (cmp1 == -2)
return -2;
if (cmp1 == 1)
return 0;
cmp2 = operand_less_p (vr->max, val);
if (cmp2 == -2)
return -2;
return !cmp2;
}
/* Return true if value ranges VR0 and VR1 have a non-empty
intersection.
Benchmark compile/20001226-1.c compilation time after changing this
function.
*/
static inline bool
value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
{
/* The value ranges do not intersect if the maximum of the first range is
less than the minimum of the second range or vice versa.
When those relations are unknown, we can't do any better. */
if (operand_less_p (vr0->max, vr1->min) != 0)
return false;
if (operand_less_p (vr1->max, vr0->min) != 0)
return false;
return true;
}
/* Return true if VR includes the value zero, false otherwise. FIXME,
currently this will return false for an anti-range like ~[-4, 3].
This will be wrong when the semantics of value_inside_range are
modified (currently the users of this function expect these
semantics). */
static inline bool
range_includes_zero_p (value_range_t *vr)
{
tree zero;
gcc_assert (vr->type != VR_UNDEFINED
&& vr->type != VR_VARYING
&& !symbolic_range_p (vr));
zero = build_int_cst (TREE_TYPE (vr->min), 0);
return (value_inside_range (zero, vr) == 1);
}
/* Return true if T, an SSA_NAME, is known to be nonnegative. Return
false otherwise or if no value range information is available. */
bool
ssa_name_nonnegative_p (const_tree t)
{
value_range_t *vr = get_value_range (t);
if (!vr)
return false;
/* Testing for VR_ANTI_RANGE is not useful here as any anti-range
which would return a useful value should be encoded as a VR_RANGE. */
if (vr->type == VR_RANGE)
{
int result = compare_values (vr->min, integer_zero_node);
return (result == 0 || result == 1);
}
return false;
}
/* Return true if T, an SSA_NAME, is known to be nonzero. Return
false otherwise or if no value range information is available. */
bool
ssa_name_nonzero_p (const_tree t)
{
value_range_t *vr = get_value_range (t);
if (!vr)
return false;
/* A VR_RANGE which does not include zero is a nonzero value. */
if (vr->type == VR_RANGE && !symbolic_range_p (vr))
return ! range_includes_zero_p (vr);
/* A VR_ANTI_RANGE which does include zero is a nonzero value. */
if (vr->type == VR_ANTI_RANGE && !symbolic_range_p (vr))
return range_includes_zero_p (vr);
return false;
}
/* If OP has a value range with a single constant value return that,
otherwise return NULL_TREE. This returns OP itself if OP is a
constant. */
static tree
op_with_constant_singleton_value_range (tree op)
{
value_range_t *vr;
if (is_gimple_min_invariant (op))
return op;
if (TREE_CODE (op) != SSA_NAME)
return NULL_TREE;
vr = get_value_range (op);
if (vr->type == VR_RANGE
&& operand_equal_p (vr->min, vr->max, 0)
&& is_gimple_min_invariant (vr->min))
return vr->min;
return NULL_TREE;
}
/* Extract value range information from an ASSERT_EXPR EXPR and store
it in *VR_P. */
static void
extract_range_from_assert (value_range_t *vr_p, tree expr)
{
tree var, cond, limit, min, max, type;
value_range_t *var_vr, *limit_vr;
enum tree_code cond_code;
var = ASSERT_EXPR_VAR (expr);
cond = ASSERT_EXPR_COND (expr);
gcc_assert (COMPARISON_CLASS_P (cond));
/* Find VAR in the ASSERT_EXPR conditional. */
if (var == TREE_OPERAND (cond, 0)
|| TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
|| TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
{
/* If the predicate is of the form VAR COMP LIMIT, then we just
take LIMIT from the RHS and use the same comparison code. */
cond_code = TREE_CODE (cond);
limit = TREE_OPERAND (cond, 1);
cond = TREE_OPERAND (cond, 0);
}
else
{
/* If the predicate is of the form LIMIT COMP VAR, then we need
to flip around the comparison code to create the proper range
for VAR. */
cond_code = swap_tree_comparison (TREE_CODE (cond));
limit = TREE_OPERAND (cond, 0);
cond = TREE_OPERAND (cond, 1);
}
limit = avoid_overflow_infinity (limit);
type = TREE_TYPE (limit);
gcc_assert (limit != var);
/* For pointer arithmetic, we only keep track of pointer equality
and inequality. */
if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
{
set_value_range_to_varying (vr_p);
return;
}
/* If LIMIT is another SSA name and LIMIT has a range of its own,
try to use LIMIT's range to avoid creating symbolic ranges
unnecessarily. */
limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
/* LIMIT's range is only interesting if it has any useful information. */
if (limit_vr
&& (limit_vr->type == VR_UNDEFINED
|| limit_vr->type == VR_VARYING
|| symbolic_range_p (limit_vr)))
limit_vr = NULL;
/* Initially, the new range has the same set of equivalences of
VAR's range. This will be revised before returning the final
value. Since assertions may be chained via mutually exclusive
predicates, we will need to trim the set of equivalences before
we are done. */
gcc_assert (vr_p->equiv == NULL);
add_equivalence (&vr_p->equiv, var);
/* Extract a new range based on the asserted comparison for VAR and
LIMIT's value range. Notice that if LIMIT has an anti-range, we
will only use it for equality comparisons (EQ_EXPR). For any
other kind of assertion, we cannot derive a range from LIMIT's
anti-range that can be used to describe the new range. For
instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
no single range for x_2 that could describe LE_EXPR, so we might
as well build the range [b_4, +INF] for it.
One special case we handle is extracting a range from a
range test encoded as (unsigned)var + CST <= limit. */
if (TREE_CODE (cond) == NOP_EXPR
|| TREE_CODE (cond) == PLUS_EXPR)
{
if (TREE_CODE (cond) == PLUS_EXPR)
{
min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
TREE_OPERAND (cond, 1));
max = int_const_binop (PLUS_EXPR, limit, min, 0);
cond = TREE_OPERAND (cond, 0);
}
else
{
min = build_int_cst (TREE_TYPE (var), 0);
max = limit;
}
/* Make sure to not set TREE_OVERFLOW on the final type
conversion. We are willingly interpreting large positive
unsigned values as negative singed values here. */
min = force_fit_type_double (TREE_TYPE (var), TREE_INT_CST_LOW (min),
TREE_INT_CST_HIGH (min), 0, false);
max = force_fit_type_double (TREE_TYPE (var), TREE_INT_CST_LOW (max),
TREE_INT_CST_HIGH (max), 0, false);
/* We can transform a max, min range to an anti-range or
vice-versa. Use set_and_canonicalize_value_range which does
this for us. */
if (cond_code == LE_EXPR)
set_and_canonicalize_value_range (vr_p, VR_RANGE,
min, max, vr_p->equiv);
else if (cond_code == GT_EXPR)
set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
min, max, vr_p->equiv);
else
gcc_unreachable ();
}
else if (cond_code == EQ_EXPR)
{
enum value_range_type range_type;
if (limit_vr)
{
range_type = limit_vr->type;
min = limit_vr->min;
max = limit_vr->max;
}
else
{
range_type = VR_RANGE;
min = limit;
max = limit;
}
set_value_range (vr_p, range_type, min, max, vr_p->equiv);
/* When asserting the equality VAR == LIMIT and LIMIT is another
SSA name, the new range will also inherit the equivalence set
from LIMIT. */
if (TREE_CODE (limit) == SSA_NAME)
add_equivalence (&vr_p->equiv, limit);
}
else if (cond_code == NE_EXPR)
{
/* As described above, when LIMIT's range is an anti-range and
this assertion is an inequality (NE_EXPR), then we cannot
derive anything from the anti-range. For instance, if
LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
not imply that VAR's range is [0, 0]. So, in the case of
anti-ranges, we just assert the inequality using LIMIT and
not its anti-range.
If LIMIT_VR is a range, we can only use it to build a new
anti-range if LIMIT_VR is a single-valued range. For
instance, if LIMIT_VR is [0, 1], the predicate
VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
Rather, it means that for value 0 VAR should be ~[0, 0]
and for value 1, VAR should be ~[1, 1]. We cannot
represent these ranges.
The only situation in which we can build a valid
anti-range is when LIMIT_VR is a single-valued range
(i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
if (limit_vr
&& limit_vr->type == VR_RANGE
&& compare_values (limit_vr->min, limit_vr->max) == 0)
{
min = limit_vr->min;
max = limit_vr->max;
}
else
{
/* In any other case, we cannot use LIMIT's range to build a
valid anti-range. */
min = max = limit;
}
/* If MIN and MAX cover the whole range for their type, then
just use the original LIMIT. */
if (INTEGRAL_TYPE_P (type)
&& vrp_val_is_min (min)
&& vrp_val_is_max (max))
min = max = limit;
set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
}
else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
{
min = TYPE_MIN_VALUE (type);
if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
max = limit;
else
{
/* If LIMIT_VR is of the form [N1, N2], we need to build the
range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
LT_EXPR. */
max = limit_vr->max;
}
/* If the maximum value forces us to be out of bounds, simply punt.
It would be pointless to try and do anything more since this
all should be optimized away above us. */
if ((cond_code == LT_EXPR
&& compare_values (max, min) == 0)
|| (CONSTANT_CLASS_P (max) && TREE_OVERFLOW (max)))
set_value_range_to_varying (vr_p);
else
{
/* For LT_EXPR, we create the range [MIN, MAX - 1]. */
if (cond_code == LT_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);
}
}
else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
{
max = TYPE_MAX_VALUE (type);
if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
min = limit;
else
{
/* If LIMIT_VR is of the form [N1, N2], we need to build the
range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
GT_EXPR. */
min = limit_vr->min;
}
/* If the minimum value forces us to be out of bounds, simply punt.
It would be pointless to try and do anything more since this
all should be optimized away above us. */
if ((cond_code == GT_EXPR
&& compare_values (min, max) == 0)
|| (CONSTANT_CLASS_P (min) && TREE_OVERFLOW (min)))
set_value_range_to_varying (vr_p);
else
{
/* For GT_EXPR, we create the range [MIN + 1, MAX]. */
if (cond_code == GT_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);
}
}
else
gcc_unreachable ();
/* If VAR already had a known range, it may happen that the new
range we have computed and VAR's range are not compatible. For
instance,
if (p_5 == NULL)
p_6 = ASSERT_EXPR <p_5, p_5 == NULL>;
x_7 = p_6->fld;
p_8 = ASSERT_EXPR <p_6, p_6 != NULL>;
While the above comes from a faulty program, it will cause an ICE
later because p_8 and p_6 will have incompatible ranges and at
the same time will be considered equivalent. A similar situation
would arise from
if (i_5 > 10)
i_6 = ASSERT_EXPR <i_5, i_5 > 10>;
if (i_5 < 5)
i_7 = ASSERT_EXPR <i_6, i_6 < 5>;
Again i_6 and i_7 will have incompatible ranges. It would be
pointless to try and do anything with i_7's range because
anything dominated by 'if (i_5 < 5)' will be optimized away.
Note, due to the wa in which simulation proceeds, the statement
i_7 = ASSERT_EXPR <...> we would never be visited because the
conditional 'if (i_5 < 5)' always evaluates to false. However,
this extra check does not hurt and may protect against future
changes to VRP that may get into a situation similar to the
NULL pointer dereference example.
Note that these compatibility tests are only needed when dealing
with ranges or a mix of range and anti-range. If VAR_VR and VR_P
are both anti-ranges, they will always be compatible, because two
anti-ranges will always have a non-empty intersection. */
var_vr = get_value_range (var);
/* We may need to make adjustments when VR_P and VAR_VR are numeric
ranges or anti-ranges. */
if (vr_p->type == VR_VARYING
|| vr_p->type == VR_UNDEFINED
|| var_vr->type == VR_VARYING
|| var_vr->type == VR_UNDEFINED
|| symbolic_range_p (vr_p)
|| symbolic_range_p (var_vr))
return;
if (var_vr->type == VR_RANGE && vr_p->type == VR_RANGE)
{
/* If the two ranges have a non-empty intersection, we can
refine the resulting range. Since the assert expression
creates an equivalency and at the same time it asserts a
predicate, we can take the intersection of the two ranges to
get better precision. */
if (value_ranges_intersect_p (var_vr, vr_p))
{
/* Use the larger of the two minimums. */
if (compare_values (vr_p->min, var_vr->min) == -1)
min = var_vr->min;
else
min = vr_p->min;
/* Use the smaller of the two maximums. */
if (compare_values (vr_p->max, var_vr->max) == 1)
max = var_vr->max;
else
max = vr_p->max;
set_value_range (vr_p, vr_p->type, min, max, vr_p->equiv);
}
else
{
/* The two ranges do not intersect, set the new range to
VARYING, because we will not be able to do anything
meaningful with it. */
set_value_range_to_varying (vr_p);
}
}
else if ((var_vr->type == VR_RANGE && vr_p->type == VR_ANTI_RANGE)
|| (var_vr->type == VR_ANTI_RANGE && vr_p->type == VR_RANGE))
{
/* A range and an anti-range will cancel each other only if
their ends are the same. For instance, in the example above,
p_8's range ~[0, 0] and p_6's range [0, 0] are incompatible,
so VR_P should be set to VR_VARYING. */
if (compare_values (var_vr->min, vr_p->min) == 0
&& compare_values (var_vr->max, vr_p->max) == 0)
set_value_range_to_varying (vr_p);
else
{
tree min, max, anti_min, anti_max, real_min, real_max;
int cmp;
/* We want to compute the logical AND of the two ranges;
there are three cases to consider.
1. The VR_ANTI_RANGE range is completely within the
VR_RANGE and the endpoints of the ranges are
different. In that case the resulting range
should be whichever range is more precise.
Typically that will be the VR_RANGE.
2. The VR_ANTI_RANGE is completely disjoint from
the VR_RANGE. In this case the resulting range
should be the VR_RANGE.
3. There is some overlap between the VR_ANTI_RANGE
and the VR_RANGE.
3a. If the high limit of the VR_ANTI_RANGE resides
within the VR_RANGE, then the result is a new
VR_RANGE starting at the high limit of the
VR_ANTI_RANGE + 1 and extending to the
high limit of the original VR_RANGE.
3b. If the low limit of the VR_ANTI_RANGE resides
within the VR_RANGE, then the result is a new
VR_RANGE starting at the low limit of the original
VR_RANGE and extending to the low limit of the
VR_ANTI_RANGE - 1. */
if (vr_p->type == VR_ANTI_RANGE)
{
anti_min = vr_p->min;
anti_max = vr_p->max;
real_min = var_vr->min;
real_max = var_vr->max;
}
else
{
anti_min = var_vr->min;
anti_max = var_vr->max;
real_min = vr_p->min;
real_max = vr_p->max;
}
/* Case 1, VR_ANTI_RANGE completely within VR_RANGE,
not including any endpoints. */
if (compare_values (anti_max, real_max) == -1
&& compare_values (anti_min, real_min) == 1)
{
/* If the range is covering the whole valid range of
the type keep the anti-range. */
if (!vrp_val_is_min (real_min)
|| !vrp_val_is_max (real_max))
set_value_range (vr_p, VR_RANGE, real_min,
real_max, vr_p->equiv);
}
/* Case 2, VR_ANTI_RANGE completely disjoint from
VR_RANGE. */
else if (compare_values (anti_min, real_max) == 1
|| compare_values (anti_max, real_min) == -1)
{
set_value_range (vr_p, VR_RANGE, real_min,
real_max, vr_p->equiv);
}
/* Case 3a, the anti-range extends into the low
part of the real range. Thus creating a new
low for the real range. */
else if (((cmp = compare_values (anti_max, real_min)) == 1
|| cmp == 0)
&& compare_values (anti_max, real_max) == -1)
{
gcc_assert (!is_positive_overflow_infinity (anti_max));
if (needs_overflow_infinity (TREE_TYPE (anti_max))
&& vrp_val_is_max (anti_max))
{
if (!supports_overflow_infinity (TREE_TYPE (var_vr->min)))
{
set_value_range_to_varying (vr_p);
return;
}
min = positive_overflow_infinity (TREE_TYPE (var_vr->min));
}
else if (!POINTER_TYPE_P (TREE_TYPE (var_vr->min)))
min = fold_build2 (PLUS_EXPR, TREE_TYPE (var_vr->min),
anti_max,
build_int_cst (TREE_TYPE (var_vr->min), 1));
else
min = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (var_vr->min),
anti_max, size_int (1));
max = real_max;
set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
}
/* Case 3b, the anti-range extends into the high
part of the real range. Thus creating a new
higher for the real range. */
else if (compare_values (anti_min, real_min) == 1
&& ((cmp = compare_values (anti_min, real_max)) == -1
|| cmp == 0))
{
gcc_assert (!is_negative_overflow_infinity (anti_min));
if (needs_overflow_infinity (TREE_TYPE (anti_min))
&& vrp_val_is_min (anti_min))
{
if (!supports_overflow_infinity (TREE_TYPE (var_vr->min)))
{
set_value_range_to_varying (vr_p);
return;
}
max = negative_overflow_infinity (TREE_TYPE (var_vr->min));
}
else if (!POINTER_TYPE_P (TREE_TYPE (var_vr->min)))
max = fold_build2 (MINUS_EXPR, TREE_TYPE (var_vr->min),
anti_min,
build_int_cst (TREE_TYPE (var_vr->min), 1));
else
max = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (var_vr->min),
anti_min,
size_int (-1));
min = real_min;
set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
}
}
}
}
/* Extract range information from SSA name VAR and store it in VR. If
VAR has an interesting range, use it. Otherwise, create the
range [VAR, VAR] and return it. This is useful in situations where
we may have conditionals testing values of VARYING names. For
instance,
x_3 = y_5;
if (x_3 > y_5)
...
Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
always false. */
static void
extract_range_from_ssa_name (value_range_t *vr, tree var)
{
value_range_t *var_vr = get_value_range (var);
if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
copy_value_range (vr, var_vr);
else
set_value_range (vr, VR_RANGE, var, var, NULL);
add_equivalence (&vr->equiv, var);
}
/* Wrapper around int_const_binop. If the operation overflows and we
are not using wrapping arithmetic, then adjust the result to be
-INF or +INF depending on CODE, VAL1 and VAL2. This can return
NULL_TREE if we need to use an overflow infinity representation but
the type does not support it. */
static tree
vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
{
tree res;
res = int_const_binop (code, val1, val2, 0);
/* If we are not using wrapping arithmetic, operate symbolically
on -INF and +INF. */
if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
{
int checkz = compare_values (res, val1);
bool overflow = false;
/* Ensure that res = val1 [+*] val2 >= val1
or that res = val1 - val2 <= val1. */
if ((code == PLUS_EXPR
&& !(checkz == 1 || checkz == 0))
|| (code == MINUS_EXPR
&& !(checkz == 0 || checkz == -1)))
{
overflow = true;
}
/* Checking for multiplication overflow is done by dividing the
output of the multiplication by the first input of the
multiplication. If the result of that division operation is
not equal to the second input of the multiplication, then the
multiplication overflowed. */
else if (code == MULT_EXPR && !integer_zerop (val1))
{
tree tmp = int_const_binop (TRUNC_DIV_EXPR,
res,
val1, 0);
int check = compare_values (tmp, val2);
if (check != 0)
overflow = true;
}
if (overflow)
{
res = copy_node (res);
TREE_OVERFLOW (res) = 1;
}
}
else if ((TREE_OVERFLOW (res)
&& !TREE_OVERFLOW (val1)
&& !TREE_OVERFLOW (val2))
|| is_overflow_infinity (val1)
|| is_overflow_infinity (val2))
{
/* If the operation overflowed but neither VAL1 nor VAL2 are
overflown, return -INF or +INF depending on the operation
and the combination of signs of the operands. */
int sgn1 = tree_int_cst_sgn (val1);
int sgn2 = tree_int_cst_sgn (val2);
if (needs_overflow_infinity (TREE_TYPE (res))
&& !supports_overflow_infinity (TREE_TYPE (res)))
return NULL_TREE;
/* We have to punt on adding infinities of different signs,
since we can't tell what the sign of the result should be.
Likewise for subtracting infinities of the same sign. */
if (((code == PLUS_EXPR && sgn1 != sgn2)
|| (code == MINUS_EXPR && sgn1 == sgn2))
&& is_overflow_infinity (val1)
&& is_overflow_infinity (val2))
return NULL_TREE;
/* Don't try to handle division or shifting of infinities. */
if ((code == TRUNC_DIV_EXPR
|| code == FLOOR_DIV_EXPR
|| code == CEIL_DIV_EXPR
|| code == EXACT_DIV_EXPR
|| code == ROUND_DIV_EXPR
|| code == RSHIFT_EXPR)
&& (is_overflow_infinity (val1)
|| is_overflow_infinity (val2)))
return NULL_TREE;
/* Notice that we only need to handle the restricted set of
operations handled by extract_range_from_binary_expr.
Among them, only multiplication, addition and subtraction
can yield overflow without overflown operands because we
are working with integral types only... except in the
case VAL1 = -INF and VAL2 = -1 which overflows to +INF
for division too. */
/* For multiplication, the sign of the overflow is given
by the comparison of the signs of the operands. */
if ((code == MULT_EXPR && sgn1 == sgn2)
/* For addition, the operands must be of the same sign
to yield an overflow. Its sign is therefore that
of one of the operands, for example the first. For
infinite operands X + -INF is negative, not positive. */
|| (code == PLUS_EXPR
&& (sgn1 >= 0
? !is_negative_overflow_infinity (val2)
: is_positive_overflow_infinity (val2)))
/* For subtraction, non-infinite operands must be of
different signs to yield an overflow. Its sign is
therefore that of the first operand or the opposite of
that of the second operand. A first operand of 0 counts
as positive here, for the corner case 0 - (-INF), which
overflows, but must yield +INF. For infinite operands 0
- INF is negative, not positive. */
|| (code == MINUS_EXPR
&& (sgn1 >= 0
? !is_positive_overflow_infinity (val2)
: is_negative_overflow_infinity (val2)))
/* We only get in here with positive shift count, so the
overflow direction is the same as the sign of val1.
Actually rshift does not overflow at all, but we only
handle the case of shifting overflowed -INF and +INF. */
|| (code == RSHIFT_EXPR
&& sgn1 >= 0)
/* For division, the only case is -INF / -1 = +INF. */
|| code == TRUNC_DIV_EXPR
|| code == FLOOR_DIV_EXPR
|| code == CEIL_DIV_EXPR
|| code == EXACT_DIV_EXPR
|| code == ROUND_DIV_EXPR)
return (needs_overflow_infinity (TREE_TYPE (res))
? positive_overflow_infinity (TREE_TYPE (res))
: TYPE_MAX_VALUE (TREE_TYPE (res)));
else
return (needs_overflow_infinity (TREE_TYPE (res))
? negative_overflow_infinity (TREE_TYPE (res))
: TYPE_MIN_VALUE (TREE_TYPE (res)));
}
return res;
}
/* Extract range information from a binary expression EXPR based on
the ranges of each of its operands and the expression code. */
static void
extract_range_from_binary_expr (value_range_t *vr,
enum tree_code code,
tree expr_type, tree op0, tree op1)
{
enum value_range_type type;
tree min, max;
int cmp;
value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
value_range_t vr1 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
/* Not all binary expressions can be applied to ranges in a
meaningful way. Handle only arithmetic operations. */
if (code != PLUS_EXPR
&& code != MINUS_EXPR
&& code != POINTER_PLUS_EXPR
&& code != MULT_EXPR
&& code != TRUNC_DIV_EXPR
&& code != FLOOR_DIV_EXPR
&& code != CEIL_DIV_EXPR
&& code != EXACT_DIV_EXPR
&& code != ROUND_DIV_EXPR
&& code != RSHIFT_EXPR
&& code != MIN_EXPR
&& code != MAX_EXPR
&& code != BIT_AND_EXPR
&& code != BIT_IOR_EXPR
&& code != TRUTH_AND_EXPR
&& code != TRUTH_OR_EXPR)
{
/* We can still do constant propagation here. */
tree const_op0 = op_with_constant_singleton_value_range (op0);
tree const_op1 = op_with_constant_singleton_value_range (op1);
if (const_op0 || const_op1)
{
tree tem = fold_binary (code, expr_type,
const_op0 ? const_op0 : op0,
const_op1 ? const_op1 : op1);
if (tem
&& is_gimple_min_invariant (tem)
&& !is_overflow_infinity (tem))
{
set_value_range (vr, VR_RANGE, tem, tem, NULL);
return;
}
}
set_value_range_to_varying (vr);
return;
}
/* Get value ranges for each operand. For constant operands, create
a new value range with the operand to simplify processing. */
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, NULL);
else
set_value_range_to_varying (&vr0);
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, NULL);
else
set_value_range_to_varying (&vr1);
/* If either range is UNDEFINED, so is the result. */
if (vr0.type == VR_UNDEFINED || vr1.type == VR_UNDEFINED)
{
set_value_range_to_undefined (vr);
return;
}
/* The type of the resulting value range defaults to VR0.TYPE. */
type = vr0.type;
/* Refuse to operate on VARYING ranges, ranges of different kinds
and symbolic ranges. As an exception, we allow BIT_AND_EXPR
because we may be able to derive a useful range even if one of
the operands is VR_VARYING or symbolic range. Similarly for
divisions. TODO, we may be able to derive anti-ranges in
some cases. */
if (code != BIT_AND_EXPR
&& code != TRUTH_AND_EXPR
&& code != TRUTH_OR_EXPR
&& code != TRUNC_DIV_EXPR
&& code != FLOOR_DIV_EXPR
&& code != CEIL_DIV_EXPR
&& code != EXACT_DIV_EXPR
&& code != ROUND_DIV_EXPR
&& (vr0.type == VR_VARYING
|| vr1.type == VR_VARYING
|| vr0.type != vr1.type
|| symbolic_range_p (&vr0)
|| symbolic_range_p (&vr1)))
{
set_value_range_to_varying (vr);
return;
}
/* Now evaluate the expression to determine the new range. */
if (POINTER_TYPE_P (expr_type)
|| POINTER_TYPE_P (TREE_TYPE (op0))
|| POINTER_TYPE_P (TREE_TYPE (op1)))
{
if (code == MIN_EXPR || code == MAX_EXPR)
{
/* For MIN/MAX expressions with pointers, we only care about
nullness, if both are non null, then the result is nonnull.
If both are null, then the result is null. Otherwise they
are varying. */
if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
set_value_range_to_nonnull (vr, expr_type);
else if (range_is_null (&vr0) && range_is_null (&vr1))
set_value_range_to_null (vr, expr_type);
else
set_value_range_to_varying (vr);
return;
}
gcc_assert (code == POINTER_PLUS_EXPR);
/* For pointer types, we are really only interested in asserting
whether the expression evaluates to non-NULL. */
if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
set_value_range_to_nonnull (vr, expr_type);
else if (range_is_null (&vr0) && range_is_null (&vr1))
set_value_range_to_null (vr, expr_type);
else
set_value_range_to_varying (vr);
return;
}
/* For integer ranges, apply the operation to each end of the
range and see what we end up with. */
if (code == TRUTH_AND_EXPR
|| code == TRUTH_OR_EXPR)
{
/* If one of the operands is zero, we know that the whole
expression evaluates zero. */
if (code == TRUTH_AND_EXPR
&& ((vr0.type == VR_RANGE
&& integer_zerop (vr0.min)
&& integer_zerop (vr0.max))
|| (vr1.type == VR_RANGE
&& integer_zerop (vr1.min)
&& integer_zerop (vr1.max))))
{
type = VR_RANGE;
min = max = build_int_cst (expr_type, 0);
}
/* If one of the operands is one, we know that the whole
expression evaluates one. */
else if (code == TRUTH_OR_EXPR
&& ((vr0.type == VR_RANGE
&& integer_onep (vr0.min)
&& integer_onep (vr0.max))
|| (vr1.type == VR_RANGE
&& integer_onep (vr1.min)
&& integer_onep (vr1.max))))
{
type = VR_RANGE;
min = max = build_int_cst (expr_type, 1);
}
else if (vr0.type != VR_VARYING
&& vr1.type != VR_VARYING
&& vr0.type == vr1.type
&& !symbolic_range_p (&vr0)
&& !overflow_infinity_range_p (&vr0)
&& !symbolic_range_p (&vr1)
&& !overflow_infinity_range_p (&vr1))
{
/* Boolean expressions cannot be folded with int_const_binop. */
min = fold_binary (code, expr_type, vr0.min, vr1.min);
max = fold_binary (code, expr_type, vr0.max, vr1.max);
}
else
{
/* The result of a TRUTH_*_EXPR is always true or false. */
set_value_range_to_truthvalue (vr, expr_type);
return;
}
}
else if (code == PLUS_EXPR
|| code == MIN_EXPR
|| code == MAX_EXPR)
{
/* If we have a PLUS_EXPR with two VR_ANTI_RANGEs, drop to
VR_VARYING. It would take more effort to compute a precise
range for such a case. For example, if we have op0 == 1 and
op1 == -1 with their ranges both being ~[0,0], we would have
op0 + op1 == 0, so we cannot claim that the sum is in ~[0,0].
Note that we are guaranteed to have vr0.type == vr1.type at
this point. */
if (code == PLUS_EXPR && vr0.type == VR_ANTI_RANGE)
{
set_value_range_to_varying (vr);
return;
}
/* For operations that make the resulting range directly
proportional to the original ranges, apply the operation to
the same end of each range. */
min = vrp_int_const_binop (code, vr0.min, vr1.min);
max = vrp_int_const_binop (code, vr0.max, vr1.max);
}
else if (code == MULT_EXPR
|| code == TRUNC_DIV_EXPR
|| code == FLOOR_DIV_EXPR
|| code == CEIL_DIV_EXPR
|| code == EXACT_DIV_EXPR
|| code == ROUND_DIV_EXPR
|| code == RSHIFT_EXPR)
{
tree val[4];
size_t i;
bool sop;
/* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
drop to VR_VARYING. It would take more effort to compute a
precise range for such a case. For example, if we have
op0 == 65536 and op1 == 65536 with their ranges both being
~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
we cannot claim that the product is in ~[0,0]. Note that we
are guaranteed to have vr0.type == vr1.type at this
point. */
if (code == MULT_EXPR
&& vr0.type == VR_ANTI_RANGE
&& !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
{
set_value_range_to_varying (vr);
return;
}
/* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
then drop to VR_VARYING. Outside of this range we get undefined
behavior from the shift operation. We cannot even trust
SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
shifts, and the operation at the tree level may be widened. */
if (code == RSHIFT_EXPR)
{
if (vr1.type == VR_ANTI_RANGE
|| !vrp_expr_computes_nonnegative (op1, &sop)
|| (operand_less_p
(build_int_cst (TREE_TYPE (vr1.max),
TYPE_PRECISION (expr_type) - 1),
vr1.max) != 0))
{
set_value_range_to_varying (vr);
return;
}
}
else if ((code == TRUNC_DIV_EXPR
|| code == FLOOR_DIV_EXPR
|| code == CEIL_DIV_EXPR
|| code == EXACT_DIV_EXPR
|| code == ROUND_DIV_EXPR)
&& (vr0.type != VR_RANGE || symbolic_range_p (&vr0)))
{
/* For division, if op1 has VR_RANGE but op0 does not, something
can be deduced just from that range. Say [min, max] / [4, max]
gives [min / 4, max / 4] range. */
if (vr1.type == VR_RANGE
&& !symbolic_range_p (&vr1)
&& !range_includes_zero_p (&vr1))
{
vr0.type = type = VR_RANGE;
vr0.min = vrp_val_min (TREE_TYPE (op0));
vr0.max = vrp_val_max (TREE_TYPE (op1));
}
else
{
set_value_range_to_varying (vr);
return;
}
}
/* For divisions, if op0 is VR_RANGE, we can deduce a range
even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
include 0. */
if ((code == TRUNC_DIV_EXPR
|| code == FLOOR_DIV_EXPR
|| code == CEIL_DIV_EXPR
|| code == EXACT_DIV_EXPR
|| code == ROUND_DIV_EXPR)
&& vr0.type == VR_RANGE
&& (vr1.type != VR_RANGE
|| symbolic_range_p (&vr1)
|| range_includes_zero_p (&vr1)))
{
tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
int cmp;
sop = false;
min = NULL_TREE;
max = NULL_TREE;
if (vrp_expr_computes_nonnegative (op1, &sop) && !sop)
{
/* For unsigned division or when divisor is known
to be non-negative, the range has to cover
all numbers from 0 to max for positive max
and all numbers from min to 0 for negative min. */
cmp = compare_values (vr0.max, zero);
if (cmp == -1)
max = zero;
else if (cmp == 0 || cmp == 1)
max = vr0.max;
else
type = VR_VARYING;
cmp = compare_values (vr0.min, zero);
if (cmp == 1)
min = zero;
else if (cmp == 0 || cmp == -1)
min = vr0.min;
else
type = VR_VARYING;
}
else
{
/* Otherwise the range is -max .. max or min .. -min
depending on which bound is bigger in absolute value,
as the division can change the sign. */
abs_extent_range (vr, vr0.min, vr0.max);
return;
}
if (type == VR_VARYING)
{
set_value_range_to_varying (vr);
return;
}
}
/* Multiplications and divisions are a bit tricky to handle,
depending on the mix of signs we have in the two ranges, we
need to operate on different values to get the minimum and
maximum values for the new range. One approach is to figure
out all the variations of range combinations and do the
operations.
However, this involves several calls to compare_values and it
is pretty convoluted. It's simpler to do the 4 operations
(MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
MAX1) and then figure the smallest and largest values to form
the new range. */
else
{
gcc_assert ((vr0.type == VR_RANGE
|| (code == MULT_EXPR && vr0.type == VR_ANTI_RANGE))
&& vr0.type == vr1.type);
/* Compute the 4 cross operations. */
sop = false;
val[0] = vrp_int_const_binop (code, vr0.min, vr1.min);
if (val[0] == NULL_TREE)
sop = true;
if (vr1.max == vr1.min)
val[1] = NULL_TREE;
else
{
val[1] = vrp_int_const_binop (code, vr0.min, vr1.max);
if (val[1] == NULL_TREE)
sop = true;
}
if (vr0.max == vr0.min)
val[2] = NULL_TREE;
else
{
val[2] = vrp_int_const_binop (code, vr0.max, vr1.min);
if (val[2] == NULL_TREE)
sop = true;
}
if (vr0.min == vr0.max || vr1.min == vr1.max)
val[3] = NULL_TREE;
else
{
val[3] = vrp_int_const_binop (code, vr0.max, vr1.max);
if (val[3] == NULL_TREE)
sop = true;
}
if (sop)
{
set_value_range_to_varying (vr);
return;
}
/* Set MIN to the minimum of VAL[i] and MAX to the maximum
of VAL[i]. */
min = val[0];
max = val[0];
for (i = 1; i < 4; i++)
{
if (!is_gimple_min_invariant (min)
|| (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
|| !is_gimple_min_invariant (max)
|| (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
break;
if (val[i])
{
if (!is_gimple_min_invariant (val[i])
|| (TREE_OVERFLOW (val[i])
&& !is_overflow_infinity (val[i])))
{
/* If we found an overflowed value, set MIN and MAX
to it so that we set the resulting range to
VARYING. */
min = max = val[i];
break;
}
if (compare_values (val[i], min) == -1)
min = val[i];
if (compare_values (val[i], max) == 1)
max = val[i];
}
}
}
}
else if (code == MINUS_EXPR)
{
/* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
VR_VARYING. It would take more effort to compute a precise
range for such a case. For example, if we have op0 == 1 and
op1 == 1 with their ranges both being ~[0,0], we would have
op0 - op1 == 0, so we cannot claim that the difference is in
~[0,0]. Note that we are guaranteed to have
vr0.type == vr1.type at this point. */
if (vr0.type == VR_ANTI_RANGE)
{
set_value_range_to_varying (vr);
return;
}
/* For MINUS_EXPR, apply the operation to the opposite ends of
each range. */
min = vrp_int_const_binop (code, vr0.min, vr1.max);
max = vrp_int_const_binop (code, vr0.max, vr1.min);
}
else if (code == BIT_AND_EXPR)
{
if (vr0.type == VR_RANGE
&& vr0.min == vr0.max
&& TREE_CODE (vr0.max) == INTEGER_CST
&& !TREE_OVERFLOW (vr0.max)
&& tree_int_cst_sgn (vr0.max) >= 0)
{
min = build_int_cst (expr_type, 0);
max = vr0.max;
}
else if (vr1.type == VR_RANGE
&& vr1.min == vr1.max
&& TREE_CODE (vr1.max) == INTEGER_CST
&& !TREE_OVERFLOW (vr1.max)
&& tree_int_cst_sgn (vr1.max) >= 0)
{
type = VR_RANGE;
min = build_int_cst (expr_type, 0);
max = vr1.max;
}
else
{
set_value_range_to_varying (vr);
return;
}
}
else if (code == BIT_IOR_EXPR)
{
if (vr0.type == VR_RANGE
&& vr1.type == VR_RANGE
&& TREE_CODE (vr0.min) == INTEGER_CST
&& TREE_CODE (vr1.min) == INTEGER_CST
&& TREE_CODE (vr0.max) == INTEGER_CST
&& TREE_CODE (vr1.max) == INTEGER_CST
&& tree_int_cst_sgn (vr0.min) >= 0
&& tree_int_cst_sgn (vr1.min) >= 0)
{
double_int vr0_max = tree_to_double_int (vr0.max);
double_int vr1_max = tree_to_double_int (vr1.max);
double_int ior_max;
/* Set all bits to the right of the most significant one to 1.
For example, [0, 4] | [4, 4] = [4, 7]. */
ior_max.low = vr0_max.low | vr1_max.low;
ior_max.high = vr0_max.high | vr1_max.high;
if (ior_max.high != 0)
{
ior_max.low = ~(unsigned HOST_WIDE_INT)0u;
ior_max.high |= ((HOST_WIDE_INT) 1
<< floor_log2 (ior_max.high)) - 1;
}
else if (ior_max.low != 0)
ior_max.low |= ((unsigned HOST_WIDE_INT) 1u
<< floor_log2 (ior_max.low)) - 1;
/* Both of these endpoints are conservative. */
min = vrp_int_const_binop (MAX_EXPR, vr0.min, vr1.min);
max = double_int_to_tree (expr_type, ior_max);
}
else
{
set_value_range_to_varying (vr);
return;
}
}
else
gcc_unreachable ();
/* If either MIN or MAX overflowed, then set the resulting range to
VARYING. But we do accept an overflow infinity
representation. */
if (min == NULL_TREE
|| !is_gimple_min_invariant (min)
|| (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
|| max == NULL_TREE
|| !is_gimple_min_invariant (max)
|| (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
{
set_value_range_to_varying (vr);
return;
}
/* We punt if:
1) [-INF, +INF]
2) [-INF, +-INF(OVF)]
3) [+-INF(OVF), +INF]
4) [+-INF(OVF), +-INF(OVF)]
We learn nothing when we have INF and INF(OVF) on both sides.
Note that we do accept [-INF, -INF] and [+INF, +INF] without
overflow. */
if ((vrp_val_is_min (min) || is_overflow_infinity (min))
&& (vrp_val_is_max (max) || is_overflow_infinity (max)))
{
set_value_range_to_varying (vr);
return;
}
cmp = compare_values (min, max);
if (cmp == -2 || cmp == 1)
{
/* If the new range has its limits swapped around (MIN > MAX),
then the operation caused one of them to wrap around, mark
the new range VARYING. */
set_value_range_to_varying (vr);
}
else
set_value_range (vr, type, min, max, NULL);
}
/* Extract range information from a unary expression EXPR based on
the range of its operand and the expression code. */
static void
extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
tree type, tree op0)
{
tree min, max;
int cmp;
value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
/* Refuse to operate on certain unary expressions for which we
cannot easily determine a resulting range. */
if (code == FIX_TRUNC_EXPR
|| code == FLOAT_EXPR
|| code == BIT_NOT_EXPR
|| code == CONJ_EXPR)
{
/* We can still do constant propagation here. */
if ((op0 = op_with_constant_singleton_value_range (op0)) != NULL_TREE)
{
tree tem = fold_unary (code, type, op0);
if (tem
&& is_gimple_min_invariant (tem)
&& !is_overflow_infinity (tem))
{
set_value_range (vr, VR_RANGE, tem, tem, NULL);
return;
}
}
set_value_range_to_varying (vr);
return;
}
/* Get value ranges for the operand. For constant operands, create
a new value range with the operand to simplify processing. */
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, NULL);
else
set_value_range_to_varying (&vr0);
/* If VR0 is UNDEFINED, so is the result. */
if (vr0.type == VR_UNDEFINED)
{
set_value_range_to_undefined (vr);
return;
}
/* Refuse to operate on symbolic ranges, or if neither operand is
a pointer or integral type. */
if ((!INTEGRAL_TYPE_P (TREE_TYPE (op0))
&& !POINTER_TYPE_P (TREE_TYPE (op0)))
|| (vr0.type != VR_VARYING
&& symbolic_range_p (&vr0)))
{
set_value_range_to_varying (vr);
return;
}
/* If the expression involves pointers, we are only interested in
determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
if (POINTER_TYPE_P (type) || POINTER_TYPE_P (TREE_TYPE (op0)))
{
bool sop;
sop = false;
if (range_is_nonnull (&vr0)
|| (tree_unary_nonzero_warnv_p (code, type, op0, &sop)
&& !sop))
set_value_range_to_nonnull (vr, type);
else if (range_is_null (&vr0))
set_value_range_to_null (vr, type);
else
set_value_range_to_varying (vr);
return;
}
/* Handle unary expressions on integer ranges. */
if (CONVERT_EXPR_CODE_P (code)
&& INTEGRAL_TYPE_P (type)
&& INTEGRAL_TYPE_P (TREE_TYPE (op0)))
{
tree inner_type = TREE_TYPE (op0);
tree outer_type = type;
/* Always use base-types here. This is important for the
correct signedness. */
if (TREE_TYPE (inner_type))
inner_type = TREE_TYPE (inner_type);
if (TREE_TYPE (outer_type))
outer_type = TREE_TYPE (outer_type);
/* If VR0 is varying and we increase the type precision, assume
a full range for the following transformation. */
if (vr0.type == VR_VARYING
&& TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
{
vr0.type = VR_RANGE;
vr0.min = TYPE_MIN_VALUE (inner_type);
vr0.max = TYPE_MAX_VALUE (inner_type);
}
/* If VR0 is a constant range or anti-range and the conversion is
not truncating we can convert the min and max values and
canonicalize the resulting range. Otherwise we can do the
conversion if the size of the range is less than what the
precision of the target type can represent and the range is
not an anti-range. */
if ((vr0.type == VR_RANGE
|| vr0.type == VR_ANTI_RANGE)
&& TREE_CODE (vr0.min) == INTEGER_CST
&& TREE_CODE (vr0.max) == INTEGER_CST
&& !is_overflow_infinity (vr0.min)
&& !is_overflow_infinity (vr0.max)
&& (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
|| (vr0.type == VR_RANGE
&& integer_zerop (int_const_binop (RSHIFT_EXPR,
int_const_binop (MINUS_EXPR, vr0.max, vr0.min, 0),
size_int (TYPE_PRECISION (outer_type)), 0)))))
{
tree new_min, new_max;
new_min = force_fit_type_double (outer_type,
TREE_INT_CST_LOW (vr0.min),
TREE_INT_CST_HIGH (vr0.min), 0, 0);
new_max = force_fit_type_double (outer_type,
TREE_INT_CST_LOW (vr0.max),
TREE_INT_CST_HIGH (vr0.max), 0, 0);
set_and_canonicalize_value_range (vr, vr0.type,
new_min, new_max, NULL);
return;
}
set_value_range_to_varying (vr);
return;
}
/* Conversion of a VR_VARYING value to a wider type can result
in a usable range. So wait until after we've handled conversions
before dropping the result to VR_VARYING if we had a source
operand that is VR_VARYING. */
if (vr0.type == VR_VARYING)
{
set_value_range_to_varying (vr);
return;
}
/* Apply the operation to each end of the range and see what we end
up with. */
if (code == NEGATE_EXPR
&& !TYPE_UNSIGNED (type))
{
/* NEGATE_EXPR flips the range around. We need to treat
TYPE_MIN_VALUE specially. */
if (is_positive_overflow_infinity (vr0.max))
min = negative_overflow_infinity (type);
else if (is_negative_overflow_infinity (vr0.max))
min = positive_overflow_infinity (type);
else if (!vrp_val_is_min (vr0.max))
min = fold_unary_to_constant (code, type, vr0.max);
else if (needs_overflow_infinity (type))
{
if (supports_overflow_infinity (type)
&& !is_overflow_infinity (vr0.min)
&& !vrp_val_is_min (vr0.min))
min = positive_overflow_infinity (type);
else
{
set_value_range_to_varying (vr);
return;
}
}
else
min = TYPE_MIN_VALUE (type);
if (is_positive_overflow_infinity (vr0.min))
max = negative_overflow_infinity (type);
else if (is_negative_overflow_infinity (vr0.min))
max = positive_overflow_infinity (type);
else if (!vrp_val_is_min (vr0.min))
max = fold_unary_to_constant (code, type, vr0.min);
else if (needs_overflow_infinity (type))
{
if (supports_overflow_infinity (type))
max = positive_overflow_infinity (type);
else
{
set_value_range_to_varying (vr);
return;
}
}
else
max = TYPE_MIN_VALUE (type);
}
else if (code == NEGATE_EXPR
&& TYPE_UNSIGNED (type))
{
if (!range_includes_zero_p (&vr0))
{
max = fold_unary_to_constant (code, type, vr0.min);
min = fold_unary_to_constant (code, type, vr0.max);
}
else
{
if (range_is_null (&vr0))
set_value_range_to_null (vr, type);
else
set_value_range_to_varying (vr);
return;
}
}
else if (code == ABS_EXPR
&& !TYPE_UNSIGNED (type))
{
/* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
useful range. */
if (!TYPE_OVERFLOW_UNDEFINED (type)
&& ((vr0.type == VR_RANGE
&& vrp_val_is_min (vr0.min))
|| (vr0.type == VR_ANTI_RANGE
&& !vrp_val_is_min (vr0.min)
&& !range_includes_zero_p (&vr0))))
{
set_value_range_to_varying (vr);
return;
}
/* ABS_EXPR may flip the range around, if the original range
included negative values. */
if (is_overflow_infinity (vr0.min))
min = positive_overflow_infinity (type);
else if (!vrp_val_is_min (vr0.min))
min = fold_unary_to_constant (code, type, vr0.min);
else if (!needs_overflow_infinity (type))
min = TYPE_MAX_VALUE (type);
else if (supports_overflow_infinity (type))
min = positive_overflow_infinity (type);
else
{
set_value_range_to_varying (vr);
return;
}
if (is_overflow_infinity (vr0.max))
max = positive_overflow_infinity (type);
else if (!vrp_val_is_min (vr0.max))
max = fold_unary_to_constant (code, type, vr0.max);
else if (!needs_overflow_infinity (type))
max = TYPE_MAX_VALUE (type);
else if (supports_overflow_infinity (type)
/* We shouldn't generate [+INF, +INF] as set_value_range