| /* Support routines for Value Range Propagation (VRP). |
| Copyright (C) 2005-2015 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 "flags.h" |
| #include "hash-set.h" |
| #include "machmode.h" |
| #include "vec.h" |
| #include "double-int.h" |
| #include "input.h" |
| #include "alias.h" |
| #include "symtab.h" |
| #include "wide-int.h" |
| #include "inchash.h" |
| #include "tree.h" |
| #include "fold-const.h" |
| #include "stor-layout.h" |
| #include "calls.h" |
| #include "predict.h" |
| #include "hard-reg-set.h" |
| #include "function.h" |
| #include "dominance.h" |
| #include "cfg.h" |
| #include "cfganal.h" |
| #include "basic-block.h" |
| #include "tree-ssa-alias.h" |
| #include "internal-fn.h" |
| #include "gimple-fold.h" |
| #include "tree-eh.h" |
| #include "gimple-expr.h" |
| #include "is-a.h" |
| #include "gimple.h" |
| #include "gimple-iterator.h" |
| #include "gimple-walk.h" |
| #include "gimple-ssa.h" |
| #include "tree-cfg.h" |
| #include "tree-phinodes.h" |
| #include "ssa-iterators.h" |
| #include "stringpool.h" |
| #include "tree-ssanames.h" |
| #include "tree-ssa-loop-manip.h" |
| #include "tree-ssa-loop-niter.h" |
| #include "tree-ssa-loop.h" |
| #include "tree-into-ssa.h" |
| #include "tree-ssa.h" |
| #include "tree-pass.h" |
| #include "tree-dump.h" |
| #include "gimple-pretty-print.h" |
| #include "diagnostic-core.h" |
| #include "intl.h" |
| #include "cfgloop.h" |
| #include "tree-scalar-evolution.h" |
| #include "tree-ssa-propagate.h" |
| #include "tree-chrec.h" |
| #include "tree-ssa-threadupdate.h" |
| #include "hashtab.h" |
| #include "rtl.h" |
| #include "statistics.h" |
| #include "real.h" |
| #include "fixed-value.h" |
| #include "insn-config.h" |
| #include "expmed.h" |
| #include "dojump.h" |
| #include "explow.h" |
| #include "emit-rtl.h" |
| #include "varasm.h" |
| #include "stmt.h" |
| #include "expr.h" |
| #include "insn-codes.h" |
| #include "optabs.h" |
| #include "tree-ssa-threadedge.h" |
| |
| |
| |
| /* Range of values that can be associated with an SSA_NAME after VRP |
| has executed. */ |
| struct value_range_d |
| { |
| /* Lattice value represented by this range. */ |
| enum value_range_type type; |
| |
| /* Minimum and maximum values represented by this range. These |
| values should be interpreted as follows: |
| |
| - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must |
| be NULL. |
| |
| - If TYPE == VR_RANGE then MIN holds the minimum value and |
| MAX holds the maximum value of the range [MIN, MAX]. |
| |
| - If TYPE == ANTI_RANGE the variable is known to NOT |
| take any values in the range [MIN, MAX]. */ |
| tree min; |
| tree max; |
| |
| /* Set of SSA names whose value ranges are equivalent to this one. |
| This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */ |
| bitmap equiv; |
| }; |
| |
| typedef struct value_range_d value_range_t; |
| |
| #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL } |
| |
| /* 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] |
| && bitmap_bit_p (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 void vrp_intersect_ranges (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 unsigned num_vr_values; |
| static value_range_t **vr_value; |
| static bool values_propagated; |
| |
| /* 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 { |
| gswitch *stmt; |
| tree vec; |
| } switch_update; |
| |
| static vec<edge> to_remove_edges; |
| static vec<switch_update> to_update_switch_stmts; |
| |
| |
| /* Return the maximum value for TYPE. */ |
| |
| static inline tree |
| vrp_val_max (const_tree type) |
| { |
| if (!INTEGRAL_TYPE_P (type)) |
| return NULL_TREE; |
| |
| return TYPE_MAX_VALUE (type); |
| } |
| |
| /* Return the minimum value for TYPE. */ |
| |
| static inline tree |
| vrp_val_min (const_tree type) |
| { |
| if (!INTEGRAL_TYPE_P (type)) |
| return NULL_TREE; |
| |
| 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); |
| } |
| |
| /* 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) |
| { |
| gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val)); |
| 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) |
| { |
| gcc_checking_assert (supports_overflow_infinity (type)); |
| return make_overflow_infinity (vrp_val_min (type)); |
| } |
| |
| /* Return a positive overflow infinity for TYPE. */ |
| |
| static inline tree |
| positive_overflow_infinity (tree type) |
| { |
| gcc_checking_assert (supports_overflow_infinity (type)); |
| 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 (TREE_OVERFLOW_P (val) |
| && needs_overflow_infinity (TREE_TYPE (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 (TREE_OVERFLOW_P (val) |
| && needs_overflow_infinity (TREE_TYPE (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 (TREE_OVERFLOW_P (val) |
| && needs_overflow_infinity (TREE_TYPE (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 |
| { |
| gcc_checking_assert (vrp_val_is_min (val)); |
| 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); |
| for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs)) |
| { |
| attrs = lookup_attribute ("nonnull", attrs); |
| |
| /* 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 = DECL_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_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); |
| } |
| |
| |
| /* 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); |
| |
| gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min)) |
| && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (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) |
| { |
| /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */ |
| if (t == VR_UNDEFINED) |
| { |
| set_value_range_to_undefined (vr); |
| return; |
| } |
| else if (t == VR_VARYING) |
| { |
| set_value_range_to_varying (vr); |
| return; |
| } |
| |
| /* Nothing to canonicalize for symbolic ranges. */ |
| if (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, tmp; |
| |
| /* For one bit precision if max < min, then the swapped |
| range covers all values, so for VR_RANGE it is varying and |
| for VR_ANTI_RANGE empty range, so drop to varying as well. */ |
| if (TYPE_PRECISION (TREE_TYPE (min)) == 1) |
| { |
| set_value_range_to_varying (vr); |
| return; |
| } |
| |
| one = build_int_cst (TREE_TYPE (min), 1); |
| tmp = int_const_binop (PLUS_EXPR, max, one); |
| max = int_const_binop (MINUS_EXPR, min, one); |
| 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. |
| ??? This could be VR_UNDEFINED instead. */ |
| set_value_range_to_varying (vr); |
| return; |
| } |
| else if (TYPE_PRECISION (TREE_TYPE (min)) == 1 |
| && (is_min || is_max)) |
| { |
| /* Non-empty boolean ranges can always be represented |
| as a singleton range. */ |
| if (is_min) |
| min = max = vrp_val_max (TREE_TYPE (min)); |
| else |
| min = max = vrp_val_min (TREE_TYPE (min)); |
| t = VR_RANGE; |
| } |
| 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); |
| 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); |
| min = vrp_val_min (TREE_TYPE (min)); |
| t = VR_RANGE; |
| } |
| } |
| |
| /* Drop [-INF(OVF), +INF(OVF)] to varying. */ |
| if (needs_overflow_infinity (TREE_TYPE (min)) |
| && is_overflow_infinity (min) |
| && is_overflow_infinity (max)) |
| { |
| set_value_range_to_varying (vr); |
| return; |
| } |
| |
| 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)); |
| if (TREE_OVERFLOW_P (val)) |
| val = drop_tree_overflow (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); |
| } |
| |
| |
| /* 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) |
| { |
| static const struct value_range_d vr_const_varying |
| = { VR_VARYING, NULL_TREE, NULL_TREE, NULL }; |
| 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; |
| |
| /* If we query the range for a new SSA name return an unmodifiable VARYING. |
| We should get here at most from the substitute-and-fold stage which |
| will never try to change values. */ |
| if (ver >= num_vr_values) |
| return CONST_CAST (value_range_t *, &vr_const_varying); |
| |
| vr = vr_value[ver]; |
| if (vr) |
| return vr; |
| |
| /* After propagation finished do not allocate new value-ranges. */ |
| if (values_propagated) |
| return CONST_CAST (value_range_t *, &vr_const_varying); |
| |
| /* 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 of a parameter, the variable can |
| take any value in VAR's type. */ |
| if (SSA_NAME_IS_DEFAULT_DEF (var)) |
| { |
| sym = SSA_NAME_VAR (var); |
| if (TREE_CODE (sym) == PARM_DECL) |
| { |
| /* 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 (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); |
| } |
| else if (TREE_CODE (sym) == RESULT_DECL |
| && DECL_BY_REFERENCE (sym)) |
| set_value_range_to_nonnull (vr, TREE_TYPE (sym)); |
| } |
| |
| return vr; |
| } |
| |
| /* Set value-ranges of all SSA names defined by STMT to varying. */ |
| |
| static void |
| set_defs_to_varying (gimple stmt) |
| { |
| ssa_op_iter i; |
| tree def; |
| FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF) |
| { |
| value_range_t *vr = get_value_range (def); |
| /* Avoid writing to vr_const_varying get_value_range may return. */ |
| if (vr->type != VR_VARYING) |
| set_value_range_to_varying (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; |
| return is_overflow_infinity (val1) == is_overflow_infinity (val2); |
| } |
| |
| /* 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 || bitmap_empty_p (b1)) |
| && (!b2 || bitmap_empty_p (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; |
| |
| /* If there is a value-range on the SSA name from earlier analysis |
| factor that in. */ |
| if (INTEGRAL_TYPE_P (TREE_TYPE (var))) |
| { |
| wide_int min, max; |
| value_range_type rtype = get_range_info (var, &min, &max); |
| if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE) |
| { |
| value_range_d nr; |
| nr.type = rtype; |
| nr.min = wide_int_to_tree (TREE_TYPE (var), min); |
| nr.max = wide_int_to_tree (TREE_TYPE (var), max); |
| nr.equiv = NULL; |
| vrp_intersect_ranges (new_vr, &nr); |
| } |
| } |
| |
| /* 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) |
| { |
| /* Do not allow transitions up the lattice. The following |
| is slightly more awkward than just new_vr->type < old_vr->type |
| because VR_RANGE and VR_ANTI_RANGE need to be considered |
| the same. We may not have is_new when transitioning to |
| UNDEFINED. If old_vr->type is VARYING, we shouldn't be |
| called. */ |
| if (new_vr->type == VR_UNDEFINED) |
| { |
| BITMAP_FREE (new_vr->equiv); |
| set_value_range_to_varying (old_vr); |
| set_value_range_to_varying (new_vr); |
| return true; |
| } |
| else |
| 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 max and min of VR are INTEGER_CST. It's not necessary |
| a singleton. */ |
| |
| static inline bool |
| range_int_cst_p (value_range_t *vr) |
| { |
| return (vr->type == VR_RANGE |
| && TREE_CODE (vr->max) == INTEGER_CST |
| && TREE_CODE (vr->min) == INTEGER_CST); |
| } |
| |
| /* Return true if VR is a INTEGER_CST singleton. */ |
| |
| static inline bool |
| range_int_cst_singleton_p (value_range_t *vr) |
| { |
| return (range_int_cst_p (vr) |
| && !is_overflow_infinity (vr->min) |
| && !is_overflow_infinity (vr->max) |
| && tree_int_cst_equal (vr->min, 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 the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE |
| otherwise. We only handle additive operations and set NEG to true if the |
| symbol is negated and INV to the invariant part, if any. */ |
| |
| static tree |
| get_single_symbol (tree t, bool *neg, tree *inv) |
| { |
| bool neg_; |
| tree inv_; |
| |
| if (TREE_CODE (t) == PLUS_EXPR |
| || TREE_CODE (t) == POINTER_PLUS_EXPR |
| || TREE_CODE (t) == MINUS_EXPR) |
| { |
| if (is_gimple_min_invariant (TREE_OPERAND (t, 0))) |
| { |
| neg_ = (TREE_CODE (t) == MINUS_EXPR); |
| inv_ = TREE_OPERAND (t, 0); |
| t = TREE_OPERAND (t, 1); |
| } |
| else if (is_gimple_min_invariant (TREE_OPERAND (t, 1))) |
| { |
| neg_ = false; |
| inv_ = TREE_OPERAND (t, 1); |
| t = TREE_OPERAND (t, 0); |
| } |
| else |
| return NULL_TREE; |
| } |
| else |
| { |
| neg_ = false; |
| inv_ = NULL_TREE; |
| } |
| |
| if (TREE_CODE (t) == NEGATE_EXPR) |
| { |
| t = TREE_OPERAND (t, 0); |
| neg_ = !neg_; |
| } |
| |
| if (TREE_CODE (t) != SSA_NAME) |
| return NULL_TREE; |
| |
| *neg = neg_; |
| *inv = inv_; |
| return t; |
| } |
| |
| /* The reverse operation: build a symbolic expression with TYPE |
| from symbol SYM, negated according to NEG, and invariant INV. */ |
| |
| static tree |
| build_symbolic_expr (tree type, tree sym, bool neg, tree inv) |
| { |
| const bool pointer_p = POINTER_TYPE_P (type); |
| tree t = sym; |
| |
| if (neg) |
| t = build1 (NEGATE_EXPR, type, t); |
| |
| if (integer_zerop (inv)) |
| return t; |
| |
| return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv); |
| } |
| |
| /* Return true if value range VR involves exactly one symbol SYM. */ |
| |
| static bool |
| symbolic_range_based_on_p (value_range_t *vr, const_tree sym) |
| { |
| bool neg, min_has_symbol, max_has_symbol; |
| tree inv; |
| |
| if (is_gimple_min_invariant (vr->min)) |
| min_has_symbol = false; |
| else if (get_single_symbol (vr->min, &neg, &inv) == sym) |
| min_has_symbol = true; |
| else |
| return false; |
| |
| if (is_gimple_min_invariant (vr->max)) |
| max_has_symbol = false; |
| else if (get_single_symbol (vr->max, &neg, &inv) == sym) |
| max_has_symbol = true; |
| else |
| return false; |
| |
| return (min_has_symbol || max_has_symbol); |
| } |
| |
| /* 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; |
| } |
| |
| |
| /* 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_TERNARY_RHS: |
| return false; |
| 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_TERNARY_RHS: |
| return false; |
| 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 known 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: |
| { |
| tree fndecl = gimple_call_fndecl (stmt); |
| if (!fndecl) return false; |
| if (flag_delete_null_pointer_checks && !flag_check_new |
| && DECL_IS_OPERATOR_NEW (fndecl) |
| && !TREE_NOTHROW (fndecl)) |
| return true; |
| if (flag_delete_null_pointer_checks && |
| lookup_attribute ("returns_nonnull", |
| TYPE_ATTRIBUTES (gimple_call_fntype (stmt)))) |
| return true; |
| 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) == MEM_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) |
| return tree_int_cst_lt (val, val2); |
| 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) == NEGATE_EXPR |
| && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME) |
| || TREE_CODE (val1) == PLUS_EXPR |
| || TREE_CODE (val1) == MINUS_EXPR) |
| && (TREE_CODE (val2) == SSA_NAME |
| || (TREE_CODE (val2) == NEGATE_EXPR |
| && TREE_CODE (TREE_OPERAND (val2, 0)) == 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 || TREE_CODE (val1) == NEGATE_EXPR) |
| { |
| 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 || TREE_CODE (val2) == NEGATE_EXPR) |
| { |
| 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 (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR) |
| { |
| n1 = TREE_OPERAND (n1, 0); |
| n2 = TREE_OPERAND (n2, 0); |
| } |
| 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 MIN <= VAL <= MAX, |
| 0 if VAL is not inside [MIN, MAX], |
| -2 if we cannot tell either way. |
| |
| Benchmark compile/20001226-1.c compilation time after changing this |
| function. */ |
| |
| static inline int |
| value_inside_range (tree val, tree min, tree max) |
| { |
| int cmp1, cmp2; |
| |
| cmp1 = operand_less_p (val, min); |
| if (cmp1 == -2) |
| return -2; |
| if (cmp1 == 1) |
| return 0; |
| |
| cmp2 = operand_less_p (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 1 if [MIN, MAX] includes the value zero, 0 if it does not |
| include the value zero, -2 if we cannot tell. */ |
| |
| static inline int |
| range_includes_zero_p (tree min, tree max) |
| { |
| tree zero = build_int_cst (TREE_TYPE (min), 0); |
| return value_inside_range (zero, min, max); |
| } |
| |
| /* Return true if *VR is know to only contain nonnegative values. */ |
| |
| static inline bool |
| value_range_nonnegative_p (value_range_t *vr) |
| { |
| /* 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; |
| } |
| |
| /* If *VR has a value rante that is a single constant value return that, |
| otherwise return NULL_TREE. */ |
| |
| static tree |
| value_range_constant_singleton (value_range_t *vr) |
| { |
| 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; |
| } |
| |
| /* 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) |
| { |
| if (is_gimple_min_invariant (op)) |
| return op; |
| |
| if (TREE_CODE (op) != SSA_NAME) |
| return NULL_TREE; |
| |
| return value_range_constant_singleton (get_value_range (op)); |
| } |
| |
| /* Return true if op is in a boolean [0, 1] value-range. */ |
| |
| static bool |
| op_with_boolean_value_range_p (tree op) |
| { |
| value_range_t *vr; |
| |
| if (TYPE_PRECISION (TREE_TYPE (op)) == 1) |
| return true; |
| |
| if (integer_zerop (op) |
| || integer_onep (op)) |
| return true; |
| |
| if (TREE_CODE (op) != SSA_NAME) |
| return false; |
| |
| vr = get_value_range (op); |
| return (vr->type == VR_RANGE |
| && integer_zerop (vr->min) |
| && integer_onep (vr->max)); |
| } |
| |
| /* 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 *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 (var); |
| 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); |
| 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 signed values here. */ |
| min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false); |
| max = force_fit_type (TREE_TYPE (var), wi::to_widest (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_and_canonicalize_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) |
| || is_overflow_infinity (max)) |
| set_value_range_to_varying (vr_p); |
| else |
| { |
| /* For LT_EXPR, we create the range [MIN, MAX - 1]. */ |
| if (cond_code == LT_EXPR) |
| { |
| if (TYPE_PRECISION (TREE_TYPE (max)) == 1 |
| && !TYPE_UNSIGNED (TREE_TYPE (max))) |
| max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max, |
| build_int_cst (TREE_TYPE (max), -1)); |
| else |
| max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, |
| build_int_cst (TREE_TYPE (max), 1)); |
| 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) |
| || is_overflow_infinity (min)) |
| set_value_range_to_varying (vr_p); |
| else |
| { |
| /* For GT_EXPR, we create the range [MIN + 1, MAX]. */ |
| if (cond_code == GT_EXPR) |
| { |
| if (TYPE_PRECISION (TREE_TYPE (min)) == 1 |
| && !TYPE_UNSIGNED (TREE_TYPE (min))) |
| min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min, |
| build_int_cst (TREE_TYPE (min), -1)); |
| else |
| min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min, |
| build_int_cst (TREE_TYPE (min), 1)); |
| if (EXPR_P (min)) |
| TREE_NO_WARNING (min) = 1; |
| } |
| |
| set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv); |
| } |
| } |
| else |
| gcc_unreachable (); |
| |
| /* Finally intersect the new range with what we already know about var. */ |
| vrp_intersect_ranges (vr_p, get_value_range (var)); |
| } |
| |
| |
| /* 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_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); |
| |
| /* If we are using unsigned arithmetic, operate symbolically |
| on -INF and +INF as int_const_binop only handles signed overflow. */ |
| if (TYPE_UNSIGNED (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); |
| int check = compare_values (tmp, val2); |
| |
| if (check != 0) |
| overflow = true; |
| } |
| |
| if (overflow) |
| { |
| res = copy_node (res); |
| TREE_OVERFLOW (res) = 1; |
| } |
| |
| } |
| else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1))) |
| /* If the singed operation wraps then int_const_binop has done |
| everything we want. */ |
| ; |
| /* Signed division of -1/0 overflows and by the time it gets here |
| returns NULL_TREE. */ |
| else if (!res) |
| return NULL_TREE; |
| 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; |
| } |
| |
| |
| /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO |
| bitmask if some bit is unset, it means for all numbers in the range |
| the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO |
| bitmask if some bit is set, it means for all numbers in the range |
| the bit is 1, otherwise it might be 0 or 1. */ |
| |
| static bool |
| zero_nonzero_bits_from_vr (const tree expr_type, |
| value_range_t *vr, |
| wide_int *may_be_nonzero, |
| wide_int *must_be_nonzero) |
| { |
| *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type)); |
| *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type)); |
| if (!range_int_cst_p (vr) |
| || is_overflow_infinity (vr->min) |
| || is_overflow_infinity (vr->max)) |
| return false; |
| |
| if (range_int_cst_singleton_p (vr)) |
| { |
| *may_be_nonzero = vr->min; |
| *must_be_nonzero = *may_be_nonzero; |
| } |
| else if (tree_int_cst_sgn (vr->min) >= 0 |
| || tree_int_cst_sgn (vr->max) < 0) |
| { |
| wide_int xor_mask = wi::bit_xor (vr->min, vr->max); |
| *may_be_nonzero = wi::bit_or (vr->min, vr->max); |
| *must_be_nonzero = wi::bit_and (vr->min, vr->max); |
| if (xor_mask != 0) |
| { |
| wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false, |
| may_be_nonzero->get_precision ()); |
| *may_be_nonzero = *may_be_nonzero | mask; |
| *must_be_nonzero = must_be_nonzero->and_not (mask); |
| } |
| } |
| |
| return true; |
| } |
| |
| /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR |
| so that *VR0 U *VR1 == *AR. Returns true if that is possible, |
| false otherwise. If *AR can be represented with a single range |
| *VR1 will be VR_UNDEFINED. */ |
| |
| static bool |
| ranges_from_anti_range (value_range_t *ar, |
| value_range_t *vr0, value_range_t *vr1) |
| { |
| tree type = TREE_TYPE (ar->min); |
| |
| vr0->type = VR_UNDEFINED; |
| vr1->type = VR_UNDEFINED; |
| |
| if (ar->type != VR_ANTI_RANGE |
| || TREE_CODE (ar->min) != INTEGER_CST |
| || TREE_CODE (ar->max) != INTEGER_CST |
| || !vrp_val_min (type) |
| || !vrp_val_max (type)) |
| return false; |
| |
| if (!vrp_val_is_min (ar->min)) |
| { |
| vr0->type = VR_RANGE; |
| vr0->min = vrp_val_min (type); |
| vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1)); |
| } |
| if (!vrp_val_is_max (ar->max)) |
| { |
| vr1->type = VR_RANGE; |
| vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1)); |
| vr1->max = vrp_val_max (type); |
| } |
| if (vr0->type == VR_UNDEFINED) |
| { |
| *vr0 = *vr1; |
| vr1->type = VR_UNDEFINED; |
| } |
| |
| return vr0->type != VR_UNDEFINED; |
| } |
| |
| /* Helper to extract a value-range *VR for a multiplicative operation |
| *VR0 CODE *VR1. */ |
| |
| static void |
| extract_range_from_multiplicative_op_1 (value_range_t *vr, |
| enum tree_code code, |
| value_range_t *vr0, value_range_t *vr1) |
| { |
| enum value_range_type type; |
| tree val[4]; |
| size_t i; |
| tree min, max; |
| bool sop; |
| int cmp; |
| |
| /* Multiplications, divisions and shifts 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. */ |
| gcc_assert (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 == LSHIFT_EXPR); |
| gcc_assert ((vr0->type == VR_RANGE |
| || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE)) |
| && vr0->type == vr1->type); |
| |
| type = vr0->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]; |
| } |
| } |
| |
| /* 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 binary operation CODE based on |
| the ranges of each of its operands *VR0 and *VR1 with resulting |
| type EXPR_TYPE. The resulting range is stored in *VR. */ |
| |
| static void |
| extract_range_from_binary_expr_1 (value_range_t *vr, |
| enum tree_code code, tree expr_type, |
| value_range_t *vr0_, value_range_t *vr1_) |
| { |
| value_range_t vr0 = *vr0_, vr1 = *vr1_; |
| value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER; |
| enum value_range_type type; |
| tree min = NULL_TREE, max = NULL_TREE; |
| int cmp; |
| |
| if (!INTEGRAL_TYPE_P (expr_type) |
| && !POINTER_TYPE_P (expr_type)) |
| { |
| set_value_range_to_varying (vr); |
| return; |
| } |
| |
| /* 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 != TRUNC_MOD_EXPR |
| && code != RSHIFT_EXPR |
| && code != LSHIFT_EXPR |
| && code != MIN_EXPR |
| && code != MAX_EXPR |
| && code != BIT_AND_EXPR |
| && code != BIT_IOR_EXPR |
| && code != BIT_XOR_EXPR) |
| { |
| set_value_range_to_varying (vr); |
| return; |
| } |
| |
| /* If both ranges are UNDEFINED, so is the result. */ |
| if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED) |
| { |
| set_value_range_to_undefined (vr); |
| return; |
| } |
| /* If one of the ranges is UNDEFINED drop it to VARYING for the following |
| code. At some point we may want to special-case operations that |
| have UNDEFINED result for all or some value-ranges of the not UNDEFINED |
| operand. */ |
| else if (vr0.type == VR_UNDEFINED) |
| set_value_range_to_varying (&vr0); |
| else if (vr1.type == VR_UNDEFINED) |
| set_value_range_to_varying (&vr1); |
| |
| /* Now canonicalize anti-ranges to ranges when they are not symbolic |
| and express ~[] op X as ([]' op X) U ([]'' op X). */ |
| if (vr0.type == VR_ANTI_RANGE |
| && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1)) |
| { |
| extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_); |
| if (vrtem1.type != VR_UNDEFINED) |
| { |
| value_range_t vrres = VR_INITIALIZER; |
| extract_range_from_binary_expr_1 (&vrres, code, expr_type, |
| &vrtem1, vr1_); |
| vrp_meet (vr, &vrres); |
| } |
| return; |
| } |
| /* Likewise for X op ~[]. */ |
| if (vr1.type == VR_ANTI_RANGE |
| && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1)) |
| { |
| extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0); |
| if (vrtem1.type != VR_UNDEFINED) |
| { |
| value_range_t vrres = VR_INITIALIZER; |
| extract_range_from_binary_expr_1 (&vrres, code, expr_type, |
| vr0_, &vrtem1); |
| vrp_meet (vr, &vrres); |
| } |
| 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,IOR} |
| 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, MIN/MAX and PLUS/MINUS. |
| |
| TODO, we may be able to derive anti-ranges in some cases. */ |
| if (code != BIT_AND_EXPR |
| && code != BIT_IOR_EXPR |
| && code != TRUNC_DIV_EXPR |
| && code != FLOOR_DIV_EXPR |
| && code != CEIL_DIV_EXPR |
| && code != EXACT_DIV_EXPR |
| && code != ROUND_DIV_EXPR |
| && code != TRUNC_MOD_EXPR |
| && code != MIN_EXPR |
| && code != MAX_EXPR |
| && code != PLUS_EXPR |
| && code != MINUS_EXPR |
| && code != RSHIFT_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)) |
| { |
| 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); |
| } |
| else if (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); |
| } |
| else if (code == BIT_AND_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); |
| } |
| 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 == PLUS_EXPR || code == MINUS_EXPR) |
| { |
| const bool minus_p = (code == MINUS_EXPR); |
| tree min_op0 = vr0.min; |
| tree min_op1 = minus_p ? vr1.max : vr1.min; |
| tree max_op0 = vr0.max; |
| tree max_op1 = minus_p ? vr1.min : vr1.max; |
| tree sym_min_op0 = NULL_TREE; |
| tree sym_min_op1 = NULL_TREE; |
| tree sym_max_op0 = NULL_TREE; |
| tree sym_max_op1 = NULL_TREE; |
| bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1; |
| |
| /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or |
| single-symbolic ranges, try to compute the precise resulting range, |
| but only if we know that this resulting range will also be constant |
| or single-symbolic. */ |
| if (vr0.type == VR_RANGE && vr1.type == VR_RANGE |
| && (TREE_CODE (min_op0) == INTEGER_CST |
| || (sym_min_op0 |
| = get_single_symbol (min_op0, &neg_min_op0, &min_op0))) |
| && (TREE_CODE (min_op1) == INTEGER_CST |
| || (sym_min_op1 |
| = get_single_symbol (min_op1, &neg_min_op1, &min_op1))) |
| && (!(sym_min_op0 && sym_min_op1) |
| || (sym_min_op0 == sym_min_op1 |
| && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1))) |
| && (TREE_CODE (max_op0) == INTEGER_CST |
| || (sym_max_op0 |
| = get_single_symbol (max_op0, &neg_max_op0, &max_op0))) |
| && (TREE_CODE (max_op1) == INTEGER_CST |
| || (sym_max_op1 |
| = get_single_symbol (max_op1, &neg_max_op1, &max_op1))) |
| && (!(sym_max_op0 && sym_max_op1) |
| || (sym_max_op0 == sym_max_op1 |
| && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1)))) |
| { |
| const signop sgn = TYPE_SIGN (expr_type); |
| const unsigned int prec = TYPE_PRECISION (expr_type); |
| wide_int type_min, type_max, wmin, wmax; |
| int min_ovf = 0; |
| int max_ovf = 0; |
| |
| /* Get the lower and upper bounds of the type. */ |
| if (TYPE_OVERFLOW_WRAPS (expr_type)) |
| { |
| type_min = wi::min_value (prec, sgn); |
| type_max = wi::max_value (prec, sgn); |
| } |
| else |
| { |
| type_min = vrp_val_min (expr_type); |
| type_max = vrp_val_max (expr_type); |
| } |
| |
| /* Combine the lower bounds, if any. */ |
| if (min_op0 && min_op1) |
| { |
| if (minus_p) |
| { |
| wmin = wi::sub (min_op0, min_op1); |
| |
| /* Check for overflow. */ |
| if (wi::cmp (0, min_op1, sgn) |
| != wi::cmp (wmin, min_op0, sgn)) |
| min_ovf = wi::cmp (min_op0, min_op1, sgn); |
| } |
| else |
| { |
| wmin = wi::add (min_op0, min_op1); |
| |
| /* Check for overflow. */ |
| if (wi::cmp (min_op1, 0, sgn) |
| != wi::cmp (wmin, min_op0, sgn)) |
| min_ovf = wi::cmp (min_op0, wmin, sgn); |
| } |
| } |
| else if (min_op0) |
| wmin = min_op0; |
| else if (min_op1) |
| wmin = minus_p ? wi::neg (min_op1) : min_op1; |
| else |
| wmin = wi::shwi (0, prec); |
| |
| /* Combine the upper bounds, if any. */ |
| if (max_op0 && max_op1) |
| { |
| if (minus_p) |
| { |
| wmax = wi::sub (max_op0, max_op1); |
| |
| /* Check for overflow. */ |
| if (wi::cmp (0, max_op1, sgn) |
| != wi::cmp (wmax, max_op0, sgn)) |
| max_ovf = wi::cmp (max_op0, max_op1, sgn); |
| } |
| else |
| { |
| wmax = wi::add (max_op0, max_op1); |
| |
| if (wi::cmp (max_op1, 0, sgn) |
| != wi::cmp (wmax, max_op0, sgn)) |
| max_ovf = wi::cmp (max_op0, wmax, sgn); |
| } |
| } |
| else if (max_op0) |
| wmax = max_op0; |
| else if (max_op1) |
| wmax = minus_p ? wi::neg (max_op1) : max_op1; |
| else |
| wmax = wi::shwi (0, prec); |
| |
| /* Check for type overflow. */ |
| if (min_ovf == 0) |
| { |
| if (wi::cmp (wmin, type_min, sgn) == -1) |
| min_ovf = -1; |
| else if (wi::cmp (wmin, type_max, sgn) == 1) |
| min_ovf = 1; |
| } |
| if (max_ovf == 0) |
| { |
| if (wi::cmp (wmax, type_min, sgn) == -1) |
| max_ovf = -1; |
| else if (wi::cmp (wmax, type_max, sgn) == 1) |
| max_ovf = 1; |
| } |
| |
| /* If we have overflow for the constant part and the resulting |
| range will be symbolic, drop to VR_VARYING. */ |
| if ((min_ovf && sym_min_op0 != sym_min_op1) |
| || (max_ovf && sym_max_op0 != sym_max_op1)) |
| { |
| set_value_range_to_varying (vr); |
| return; |
| } |
| |
| if (TYPE_OVERFLOW_WRAPS (expr_type)) |
| { |
| /* If overflow wraps, truncate the values and adjust the |
| range kind and bounds appropriately. */ |
| wide_int tmin = wide_int::from (wmin, prec, sgn); |
| wide_int tmax = wide_int::from (wmax, prec, sgn); |
| if (min_ovf == max_ovf) |
| { |
| /* No overflow or both overflow or underflow. The |
| range kind stays VR_RANGE. */ |
| min = wide_int_to_tree (expr_type, tmin); |
| max = wide_int_to_tree (expr_type, tmax); |
| } |
| else if (min_ovf == -1 && max_ovf == 1) |
| { |
| /* Underflow and overflow, drop to VR_VARYING. */ |
| set_value_range_to_varying (vr); |
| return; |
| } |
| else |
| { |
| /* Min underflow or max overflow. The range kind |
| changes to VR_ANTI_RANGE. */ |
| bool covers = false; |
| wide_int tem = tmin; |
| gcc_assert ((min_ovf == -1 && max_ovf == 0) |
| || (max_ovf == 1 && min_ovf == 0)); |
| type = VR_ANTI_RANGE; |
| tmin = tmax + 1; |
| if (wi::cmp (tmin, tmax, sgn) < 0) |
| covers = true; |
| tmax = tem - 1; |
| if (wi::cmp (tmax, tem, sgn) > 0) |
| covers = true; |
| /* If the anti-range would cover nothing, drop to varying. |
| Likewise if the anti-range bounds are outside of the |
| types values. */ |
| if (covers || wi::cmp (tmin, tmax, sgn) > 0) |
| { |
| set_value_range_to_varying (vr); |
| return; |
| } |
| min = wide_int_to_tree (expr_type, tmin); |
| max = wide_int_to_tree (expr_type, tmax); |
| } |
| } |
| else |
| { |
| /* If overflow does not wrap, saturate to the types min/max |
| value. */ |
| if (min_ovf == -1) |
| { |
| if (needs_overflow_infinity (expr_type) |
| && supports_overflow_infinity (expr_type)) |
| min = negative_overflow_infinity (expr_type); |
| else |
| min = wide_int_to_tree (expr_type, type_min); |
| } |
| else if (min_ovf == 1) |
| { |
| if (needs_overflow_infinity (expr_type) |
| && supports_overflow_infinity (expr_type)) |
| min = positive_overflow_infinity (expr_type); |
| else |
| min = wide_int_to_tree (expr_type, type_max); |
| } |
| else |
| min = wide_int_to_tree (expr_type, wmin); |
| |
| if (max_ovf == -1) |
| { |
| if (needs_overflow_infinity (expr_type) |
| && supports_overflow_infinity (expr_type)) |
| max = negative_overflow_infinity (expr_type); |
| else |
| max = wide_int_to_tree (expr_type, type_min); |
| } |
| else if (max_ovf == 1) |
| { |
| if (needs_overflow_infinity (expr_type) |
| && supports_overflow_infinity (expr_type)) |
| max = positive_overflow_infinity (expr_type); |
| else |
| max = wide_int_to_tree (expr_type, type_max); |
| } |
| else |
| max = wide_int_to_tree (expr_type, wmax); |
| } |
| |
| if (needs_overflow_infinity (expr_type) |
| && supports_overflow_infinity (expr_type)) |
| { |
| if ((min_op0 && is_negative_overflow_infinity (min_op0)) |
| || (min_op1 |
| && (minus_p |
| ? is_positive_overflow_infinity (min_op1) |
| : is_negative_overflow_infinity (min_op1)))) |
| min = negative_overflow_infinity (expr_type); |
| if ((max_op0 && is_positive_overflow_infinity (max_op0)) |
| || (max_op1 |
| && (minus_p |
| ? is_negative_overflow_infinity (max_op1) |
| : is_positive_overflow_infinity (max_op1)))) |
| max = positive_overflow_infinity (expr_type); |
| } |
| |
| /* If the result lower bound is constant, we're done; |
| otherwise, build the symbolic lower bound. */ |
| if (sym_min_op0 == sym_min_op1) |
| ; |
| else if (sym_min_op0) |
| min = build_symbolic_expr (expr_type, sym_min_op0, |
| neg_min_op0, min); |
| else if (sym_min_op1) |
| { |
| /* We may not negate if that might introduce |
| undefined overflow. */ |
| if (! minus_p |
| || neg_min_op1 |
| || TYPE_OVERFLOW_WRAPS (expr_type)) |
| min = build_symbolic_expr (expr_type, sym_min_op1, |
| neg_min_op1 ^ minus_p, min); |
| else |
| min = NULL_TREE; |
| } |
| |
| /* Likewise for the upper bound. */ |
| if (sym_max_op0 == sym_max_op1) |
| ; |
| else if (sym_max_op0) |
| max = build_symbolic_expr (expr_type, sym_max_op0, |
| neg_max_op0, max); |
| else if (sym_max_op1) |
| { |
| /* We may not negate if that might introduce |
| undefined overflow. */ |
| if (! minus_p |
| || neg_max_op1 |
| || TYPE_OVERFLOW_WRAPS (expr_type)) |
| max = build_symbolic_expr (expr_type, sym_max_op1, |
| neg_max_op1 ^ minus_p, max); |
| else |
| max = NULL_TREE; |
| } |
| } |
| else |
| { |
| /* For other cases, for example 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. |
| ??? General even mixed range kind operations can be expressed |
| by for example transforming ~[3, 5] + [1, 2] to range-only |
| operations and a union primitive: |
| [-INF, 2] + [1, 2] U [5, +INF] + [1, 2] |
| [-INF+1, 4] U [6, +INF(OVF)] |
| though usually the union is not exactly representable with |
| a single range or anti-range as the above is |
| [-INF+1, +INF(OVF)] intersected with ~[5, 5] |
| but one could use a scheme similar to equivalences for this. */ |
| set_value_range_to_varying (vr); |
| return; |
| } |
| } |
| else if (code == MIN_EXPR |
| || code == MAX_EXPR) |
| { |
| if (vr0.type == VR_RANGE |
| && !symbolic_range_p (&vr0)) |
| { |
| type = VR_RANGE; |
| if (vr1.type == VR_RANGE |
| && !symbolic_range_p (&vr1)) |
| { |
| /* 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 == MIN_EXPR) |
| { |
| min = vrp_val_min (expr_type); |
| max = vr0.max; |
| } |
| else if (code == MAX_EXPR) |
| { |
| min = vr0.min; |
| max = vrp_val_max (expr_type); |
| } |
| } |
| else if (vr1.type == VR_RANGE |
| && !symbolic_range_p (&vr1)) |
| { |
| type = VR_RANGE; |
| if (code == MIN_EXPR) |
| { |
| min = vrp_val_min (expr_type); |
| max = vr1.max; |
| } |
| else if (code == MAX_EXPR) |
| { |
| min = vr1.min; |
| max = vrp_val_max (expr_type); |
| } |
| } |
| else |
| { |
| set_value_range_to_varying (vr); |
| return; |
| } |
| } |
| else if (code == MULT_EXPR) |
| { |
| /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not |
| drop to varying. This test requires 2*prec bits if both |
| operands are signed and 2*prec + 2 bits if either is not. */ |
| |
| signop sign = TYPE_SIGN (expr_type); |
| unsigned int prec = TYPE_PRECISION (expr_type); |
| |
| if (range_int_cst_p (&vr0) |
| && range_int_cst_p (&vr1) |
| && TYPE_OVERFLOW_WRAPS (expr_type)) |
| { |
| typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int; |
| typedef generic_wide_int |
| <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst; |
| vrp_int sizem1 = wi::mask <vrp_int> (prec, false); |
| vrp_int size = sizem1 + 1; |
| |
| /* Extend the values using the sign of the result to PREC2. |
| From here on out, everthing is just signed math no matter |
| what the input types were. */ |
| vrp_int min0 = vrp_int_cst (vr0.min); |
| vrp_int max0 = vrp_int_cst (vr0.max); |
| vrp_int min1 = vrp_int_cst (vr1.min); |
| vrp_int max1 = vrp_int_cst (vr1.max); |
| /* Canonicalize the intervals. */ |
| if (sign == UNSIGNED) |
| { |
| if (wi::ltu_p (size, min0 + max0)) |
| { |
| min0 -= size; |
| max0 -= size; |
| } |
| |
| if (wi::ltu_p (size, min1 + max1)) |
| { |
| min1 -= size; |
| max1 -= size; |
| } |
| } |
| |
| vrp_int prod0 = min0 * min1; |
| vrp_int prod1 = min0 * max1; |
| vrp_int prod2 = max0 * min1; |
| vrp_int prod3 = max0 * max1; |
| |
| /* Sort the 4 products so that min is in prod0 and max is in |
| prod3. */ |
| /* min0min1 > max0max1 */ |
| if (wi::gts_p (prod0, prod3)) |
| { |
| vrp_int tmp = prod3; |
| prod3 = prod0; |
| prod0 = tmp; |
| } |
| |
| /* min0max1 > max0min1 */ |
| if (wi::gts_p (prod1, prod2)) |
| { |
| vrp_int tmp = prod2; |
| prod2 = prod1; |
| prod1 = tmp; |
| } |
| |
| if (wi::gts_p (prod0, prod1)) |
| { |
| vrp_int tmp = prod1; |
| prod1 = prod0; |
| prod0 = tmp; |
| } |
| |
| if (wi::gts_p (prod2, prod3)) |
| { |
| vrp_int tmp = prod3; |
| prod3 = prod2; |
| prod2 = tmp; |
| } |
| |
| /* diff = max - min. */ |
| prod2 = prod3 - prod0; |
| if (wi::geu_p (prod2, sizem1)) |
| { |
| /* the range covers all values. */ |
| set_value_range_to_varying (vr); |
| return; |
| } |
| |
| /* The following should handle the wrapping and selecting |
| VR_ANTI_RANGE for us. */ |
| min = wide_int_to_tree (expr_type, prod0); |
| max = wide_int_to_tree (expr_type, prod3); |
| set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL); |
| return; |
| } |
| |
| /* 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 (vr0.type == VR_ANTI_RANGE |
| && !TYPE_OVERFLOW_UNDEFINED (expr_type)) |
| { |
| set_value_range_to_varying (vr); |
| return; |
| } |
| |
| extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1); |
| return; |
| } |
| else if (code == RSHIFT_EXPR |
| || code == LSHIFT_EXPR) |
| { |
| /* 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 (range_int_cst_p (&vr1) |
| && compare_tree_int (vr1.min, 0) >= 0 |
| && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1) |
| { |
| if (code == RSHIFT_EXPR) |
| { |
| /* Even if vr0 is VARYING or otherwise not usable, we can derive |
| useful ranges just from the shift count. E.g. |
| x >> 63 for signed 64-bit x is always [-1, 0]. */ |
| if (vr0.type != VR_RANGE || symbolic_range_p (&vr0)) |
| { |
| vr0.type = type = VR_RANGE; |
| vr0.min = vrp_val_min (expr_type); |
| vr0.max = vrp_val_max (expr_type); |
| } |
| extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1); |
| return; |
| } |
| /* We can map lshifts by constants to MULT_EXPR handling. */ |
| else if (code == LSHIFT_EXPR |
| && range_int_cst_singleton_p (&vr1)) |
| { |
| bool saved_flag_wrapv; |
| value_range_t vr1p = VR_INITIALIZER; |
| vr1p.type = VR_RANGE; |
| vr1p.min = (wide_int_to_tree |
| (expr_type, |
| wi::set_bit_in_zero (tree_to_shwi (vr1.min), |
| TYPE_PRECISION (expr_type)))); |
| vr1p.max = vr1p.min; |
| /* We have to use a wrapping multiply though as signed overflow |
| on lshifts is implementation defined in C89. */ |
| saved_flag_wrapv = flag_wrapv; |
| flag_wrapv = 1; |
| extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type, |
| &vr0, &vr1p); |
| flag_wrapv = saved_flag_wrapv; |
| return; |
| } |
| else if (code == LSHIFT_EXPR |
| && range_int_cst_p (&vr0)) |
| { |
| int prec = TYPE_PRECISION (expr_type); |
| int overflow_pos = prec; |
| int bound_shift; |
| wide_int low_bound, high_bound; |
| bool uns = TYPE_UNSIGNED (expr_type); |
| bool in_bounds = false; |
| |
| if (!uns) |
| overflow_pos -= 1; |
| |
| bound_shift = overflow_pos - tree_to_shwi (vr1.max); |
| /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can |
| overflow. However, for that to happen, vr1.max needs to be |
| zero, which means vr1 is a singleton range of zero, which |
| means it should be handled by the previous LSHIFT_EXPR |
| if-clause. */ |
| wide_int bound = wi::set_bit_in_zero (bound_shift, prec); |
| wide_int complement = ~(bound - 1); |
| |
| if (uns) |
| { |
| low_bound = bound; |
| high_bound = complement; |
| if (wi::ltu_p (vr0.max, low_bound)) |
| { |
| /* [5, 6] << [1, 2] == [10, 24]. */ |
| /* We're shifting out only zeroes, the value increases |
| monotonically. */ |
| in_bounds = true; |
| } |
| else if (wi::ltu_p (high_bound, vr0.min)) |
| { |
| /* [0xffffff00, 0xffffffff] << [1, 2] |
| == [0xfffffc00, 0xfffffffe]. */ |
| /* We're shifting out only ones, the value decreases |
| monotonically. */ |
| in_bounds = true; |
| } |
| } |
| else |
| { |
| /* [-1, 1] << [1, 2] == [-4, 4]. */ |
| low_bound = complement; |
| high_bound = bound; |
| if (wi::lts_p (vr0.max, high_bound) |
| && wi::lts_p (low_bound, vr0.min)) |
| { |
| /* For non-negative numbers, we're shifting out only |
| zeroes, the value increases monotonically. |
| For negative numbers, we're shifting out only ones, the |
| value decreases monotomically. */ |
| in_bounds = true; |
| } |
| } |
| |
| if (in_bounds) |
| { |
| extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1); |
| return; |
| } |
| } |
| } |
| 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) |
| { |
| if (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.min, vr1.max) == 0) |
| { |
| vr0.type = type = VR_RANGE; |
| vr0.min = vrp_val_min (expr_type); |
| vr0.max = vrp_val_max (expr_type); |
| } |
| else |
| { |
| set_value_range_to_varying (vr); |
| return; |
| } |
| } |
| |
| /* For divisions, if flag_non_call_exceptions is true, we must |
| not eliminate a division by zero. */ |
| if (cfun->can_throw_non_call_exceptions |
| && (vr1.type != VR_RANGE |
| || range_includes_zero_p (vr1.min, vr1.max) != 0)) |
| { |
| 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 (vr0.type == VR_RANGE |
| && (vr1.type != VR_RANGE |
| || range_includes_zero_p (vr1.min, vr1.max) != 0)) |
| { |
| tree zero = build_int_cst (TREE_TYPE (vr0.min), 0); |
| int cmp; |
| |
| min = NULL_TREE; |
| max = NULL_TREE; |
| if (TYPE_UNSIGNED (expr_type) |
| || value_range_nonnegative_p (&vr1)) |
| { |
| /* 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;
|