138fd1498Szrj /* Support routines for Value Range Propagation (VRP).
238fd1498Szrj Copyright (C) 2005-2018 Free Software Foundation, Inc.
338fd1498Szrj Contributed by Diego Novillo <dnovillo@redhat.com>.
438fd1498Szrj
538fd1498Szrj This file is part of GCC.
638fd1498Szrj
738fd1498Szrj GCC is free software; you can redistribute it and/or modify
838fd1498Szrj it under the terms of the GNU General Public License as published by
938fd1498Szrj the Free Software Foundation; either version 3, or (at your option)
1038fd1498Szrj any later version.
1138fd1498Szrj
1238fd1498Szrj GCC is distributed in the hope that it will be useful,
1338fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
1438fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1538fd1498Szrj GNU General Public License for more details.
1638fd1498Szrj
1738fd1498Szrj You should have received a copy of the GNU General Public License
1838fd1498Szrj along with GCC; see the file COPYING3. If not see
1938fd1498Szrj <http://www.gnu.org/licenses/>. */
2038fd1498Szrj
2138fd1498Szrj #include "config.h"
2238fd1498Szrj #include "system.h"
2338fd1498Szrj #include "coretypes.h"
2438fd1498Szrj #include "backend.h"
2538fd1498Szrj #include "insn-codes.h"
2638fd1498Szrj #include "rtl.h"
2738fd1498Szrj #include "tree.h"
2838fd1498Szrj #include "gimple.h"
2938fd1498Szrj #include "cfghooks.h"
3038fd1498Szrj #include "tree-pass.h"
3138fd1498Szrj #include "ssa.h"
3238fd1498Szrj #include "optabs-tree.h"
3338fd1498Szrj #include "gimple-pretty-print.h"
3438fd1498Szrj #include "diagnostic-core.h"
3538fd1498Szrj #include "flags.h"
3638fd1498Szrj #include "fold-const.h"
3738fd1498Szrj #include "stor-layout.h"
3838fd1498Szrj #include "calls.h"
3938fd1498Szrj #include "cfganal.h"
4038fd1498Szrj #include "gimple-fold.h"
4138fd1498Szrj #include "tree-eh.h"
4238fd1498Szrj #include "gimple-iterator.h"
4338fd1498Szrj #include "gimple-walk.h"
4438fd1498Szrj #include "tree-cfg.h"
4538fd1498Szrj #include "tree-dfa.h"
4638fd1498Szrj #include "tree-ssa-loop-manip.h"
4738fd1498Szrj #include "tree-ssa-loop-niter.h"
4838fd1498Szrj #include "tree-ssa-loop.h"
4938fd1498Szrj #include "tree-into-ssa.h"
5038fd1498Szrj #include "tree-ssa.h"
5138fd1498Szrj #include "intl.h"
5238fd1498Szrj #include "cfgloop.h"
5338fd1498Szrj #include "tree-scalar-evolution.h"
5438fd1498Szrj #include "tree-ssa-propagate.h"
5538fd1498Szrj #include "tree-chrec.h"
5638fd1498Szrj #include "tree-ssa-threadupdate.h"
5738fd1498Szrj #include "tree-ssa-scopedtables.h"
5838fd1498Szrj #include "tree-ssa-threadedge.h"
5938fd1498Szrj #include "omp-general.h"
6038fd1498Szrj #include "target.h"
6138fd1498Szrj #include "case-cfn-macros.h"
6238fd1498Szrj #include "params.h"
6338fd1498Szrj #include "alloc-pool.h"
6438fd1498Szrj #include "domwalk.h"
6538fd1498Szrj #include "tree-cfgcleanup.h"
6638fd1498Szrj #include "stringpool.h"
6738fd1498Szrj #include "attribs.h"
6838fd1498Szrj #include "vr-values.h"
6938fd1498Szrj #include "builtins.h"
7038fd1498Szrj
7138fd1498Szrj /* Set of SSA names found live during the RPO traversal of the function
7238fd1498Szrj for still active basic-blocks. */
7338fd1498Szrj static sbitmap *live;
7438fd1498Szrj
7538fd1498Szrj /* Return true if the SSA name NAME is live on the edge E. */
7638fd1498Szrj
7738fd1498Szrj static bool
live_on_edge(edge e,tree name)7838fd1498Szrj live_on_edge (edge e, tree name)
7938fd1498Szrj {
8038fd1498Szrj return (live[e->dest->index]
8138fd1498Szrj && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
8238fd1498Szrj }
8338fd1498Szrj
8438fd1498Szrj /* Location information for ASSERT_EXPRs. Each instance of this
8538fd1498Szrj structure describes an ASSERT_EXPR for an SSA name. Since a single
8638fd1498Szrj SSA name may have more than one assertion associated with it, these
8738fd1498Szrj locations are kept in a linked list attached to the corresponding
8838fd1498Szrj SSA name. */
8938fd1498Szrj struct assert_locus
9038fd1498Szrj {
9138fd1498Szrj /* Basic block where the assertion would be inserted. */
9238fd1498Szrj basic_block bb;
9338fd1498Szrj
9438fd1498Szrj /* Some assertions need to be inserted on an edge (e.g., assertions
9538fd1498Szrj generated by COND_EXPRs). In those cases, BB will be NULL. */
9638fd1498Szrj edge e;
9738fd1498Szrj
9838fd1498Szrj /* Pointer to the statement that generated this assertion. */
9938fd1498Szrj gimple_stmt_iterator si;
10038fd1498Szrj
10138fd1498Szrj /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
10238fd1498Szrj enum tree_code comp_code;
10338fd1498Szrj
10438fd1498Szrj /* Value being compared against. */
10538fd1498Szrj tree val;
10638fd1498Szrj
10738fd1498Szrj /* Expression to compare. */
10838fd1498Szrj tree expr;
10938fd1498Szrj
11038fd1498Szrj /* Next node in the linked list. */
11138fd1498Szrj assert_locus *next;
11238fd1498Szrj };
11338fd1498Szrj
11438fd1498Szrj /* If bit I is present, it means that SSA name N_i has a list of
11538fd1498Szrj assertions that should be inserted in the IL. */
11638fd1498Szrj static bitmap need_assert_for;
11738fd1498Szrj
11838fd1498Szrj /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
11938fd1498Szrj holds a list of ASSERT_LOCUS_T nodes that describe where
12038fd1498Szrj ASSERT_EXPRs for SSA name N_I should be inserted. */
12138fd1498Szrj static assert_locus **asserts_for;
12238fd1498Szrj
12338fd1498Szrj vec<edge> to_remove_edges;
12438fd1498Szrj vec<switch_update> to_update_switch_stmts;
12538fd1498Szrj
12638fd1498Szrj
12738fd1498Szrj /* Return the maximum value for TYPE. */
12838fd1498Szrj
12938fd1498Szrj tree
vrp_val_max(const_tree type)13038fd1498Szrj vrp_val_max (const_tree type)
13138fd1498Szrj {
13238fd1498Szrj if (!INTEGRAL_TYPE_P (type))
13338fd1498Szrj return NULL_TREE;
13438fd1498Szrj
13538fd1498Szrj return TYPE_MAX_VALUE (type);
13638fd1498Szrj }
13738fd1498Szrj
13838fd1498Szrj /* Return the minimum value for TYPE. */
13938fd1498Szrj
14038fd1498Szrj tree
vrp_val_min(const_tree type)14138fd1498Szrj vrp_val_min (const_tree type)
14238fd1498Szrj {
14338fd1498Szrj if (!INTEGRAL_TYPE_P (type))
14438fd1498Szrj return NULL_TREE;
14538fd1498Szrj
14638fd1498Szrj return TYPE_MIN_VALUE (type);
14738fd1498Szrj }
14838fd1498Szrj
14938fd1498Szrj /* Return whether VAL is equal to the maximum value of its type.
15038fd1498Szrj We can't do a simple equality comparison with TYPE_MAX_VALUE because
15138fd1498Szrj C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
15238fd1498Szrj is not == to the integer constant with the same value in the type. */
15338fd1498Szrj
15438fd1498Szrj bool
vrp_val_is_max(const_tree val)15538fd1498Szrj vrp_val_is_max (const_tree val)
15638fd1498Szrj {
15738fd1498Szrj tree type_max = vrp_val_max (TREE_TYPE (val));
15838fd1498Szrj return (val == type_max
15938fd1498Szrj || (type_max != NULL_TREE
16038fd1498Szrj && operand_equal_p (val, type_max, 0)));
16138fd1498Szrj }
16238fd1498Szrj
16338fd1498Szrj /* Return whether VAL is equal to the minimum value of its type. */
16438fd1498Szrj
16538fd1498Szrj bool
vrp_val_is_min(const_tree val)16638fd1498Szrj vrp_val_is_min (const_tree val)
16738fd1498Szrj {
16838fd1498Szrj tree type_min = vrp_val_min (TREE_TYPE (val));
16938fd1498Szrj return (val == type_min
17038fd1498Szrj || (type_min != NULL_TREE
17138fd1498Szrj && operand_equal_p (val, type_min, 0)));
17238fd1498Szrj }
17338fd1498Szrj
17438fd1498Szrj /* VR_TYPE describes a range with mininum value *MIN and maximum
17538fd1498Szrj value *MAX. Restrict the range to the set of values that have
17638fd1498Szrj no bits set outside NONZERO_BITS. Update *MIN and *MAX and
17738fd1498Szrj return the new range type.
17838fd1498Szrj
17938fd1498Szrj SGN gives the sign of the values described by the range. */
18038fd1498Szrj
18138fd1498Szrj enum value_range_type
intersect_range_with_nonzero_bits(enum value_range_type vr_type,wide_int * min,wide_int * max,const wide_int & nonzero_bits,signop sgn)18238fd1498Szrj intersect_range_with_nonzero_bits (enum value_range_type vr_type,
18338fd1498Szrj wide_int *min, wide_int *max,
18438fd1498Szrj const wide_int &nonzero_bits,
18538fd1498Szrj signop sgn)
18638fd1498Szrj {
18738fd1498Szrj if (vr_type == VR_ANTI_RANGE)
18838fd1498Szrj {
18938fd1498Szrj /* The VR_ANTI_RANGE is equivalent to the union of the ranges
19038fd1498Szrj A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
19138fd1498Szrj to create an inclusive upper bound for A and an inclusive lower
19238fd1498Szrj bound for B. */
19338fd1498Szrj wide_int a_max = wi::round_down_for_mask (*min - 1, nonzero_bits);
19438fd1498Szrj wide_int b_min = wi::round_up_for_mask (*max + 1, nonzero_bits);
19538fd1498Szrj
19638fd1498Szrj /* If the calculation of A_MAX wrapped, A is effectively empty
19738fd1498Szrj and A_MAX is the highest value that satisfies NONZERO_BITS.
19838fd1498Szrj Likewise if the calculation of B_MIN wrapped, B is effectively
19938fd1498Szrj empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
20038fd1498Szrj bool a_empty = wi::ge_p (a_max, *min, sgn);
20138fd1498Szrj bool b_empty = wi::le_p (b_min, *max, sgn);
20238fd1498Szrj
20338fd1498Szrj /* If both A and B are empty, there are no valid values. */
20438fd1498Szrj if (a_empty && b_empty)
20538fd1498Szrj return VR_UNDEFINED;
20638fd1498Szrj
20738fd1498Szrj /* If exactly one of A or B is empty, return a VR_RANGE for the
20838fd1498Szrj other one. */
20938fd1498Szrj if (a_empty || b_empty)
21038fd1498Szrj {
21138fd1498Szrj *min = b_min;
21238fd1498Szrj *max = a_max;
21338fd1498Szrj gcc_checking_assert (wi::le_p (*min, *max, sgn));
21438fd1498Szrj return VR_RANGE;
21538fd1498Szrj }
21638fd1498Szrj
21738fd1498Szrj /* Update the VR_ANTI_RANGE bounds. */
21838fd1498Szrj *min = a_max + 1;
21938fd1498Szrj *max = b_min - 1;
22038fd1498Szrj gcc_checking_assert (wi::le_p (*min, *max, sgn));
22138fd1498Szrj
22238fd1498Szrj /* Now check whether the excluded range includes any values that
22338fd1498Szrj satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
22438fd1498Szrj if (wi::round_up_for_mask (*min, nonzero_bits) == b_min)
22538fd1498Szrj {
22638fd1498Szrj unsigned int precision = min->get_precision ();
22738fd1498Szrj *min = wi::min_value (precision, sgn);
22838fd1498Szrj *max = wi::max_value (precision, sgn);
22938fd1498Szrj vr_type = VR_RANGE;
23038fd1498Szrj }
23138fd1498Szrj }
23238fd1498Szrj if (vr_type == VR_RANGE)
23338fd1498Szrj {
23438fd1498Szrj *max = wi::round_down_for_mask (*max, nonzero_bits);
23538fd1498Szrj
23638fd1498Szrj /* Check that the range contains at least one valid value. */
23738fd1498Szrj if (wi::gt_p (*min, *max, sgn))
23838fd1498Szrj return VR_UNDEFINED;
23938fd1498Szrj
24038fd1498Szrj *min = wi::round_up_for_mask (*min, nonzero_bits);
24138fd1498Szrj gcc_checking_assert (wi::le_p (*min, *max, sgn));
24238fd1498Szrj }
24338fd1498Szrj return vr_type;
24438fd1498Szrj }
24538fd1498Szrj
24638fd1498Szrj /* Set value range VR to VR_UNDEFINED. */
24738fd1498Szrj
24838fd1498Szrj static inline void
set_value_range_to_undefined(value_range * vr)24938fd1498Szrj set_value_range_to_undefined (value_range *vr)
25038fd1498Szrj {
25138fd1498Szrj vr->type = VR_UNDEFINED;
25238fd1498Szrj vr->min = vr->max = NULL_TREE;
25338fd1498Szrj if (vr->equiv)
25438fd1498Szrj bitmap_clear (vr->equiv);
25538fd1498Szrj }
25638fd1498Szrj
25738fd1498Szrj /* Set value range VR to VR_VARYING. */
25838fd1498Szrj
25938fd1498Szrj void
set_value_range_to_varying(value_range * vr)26038fd1498Szrj set_value_range_to_varying (value_range *vr)
26138fd1498Szrj {
26238fd1498Szrj vr->type = VR_VARYING;
26338fd1498Szrj vr->min = vr->max = NULL_TREE;
26438fd1498Szrj if (vr->equiv)
26538fd1498Szrj bitmap_clear (vr->equiv);
26638fd1498Szrj }
26738fd1498Szrj
26838fd1498Szrj /* Set value range VR to {T, MIN, MAX, EQUIV}. */
26938fd1498Szrj
27038fd1498Szrj void
set_value_range(value_range * vr,enum value_range_type t,tree min,tree max,bitmap equiv)27138fd1498Szrj set_value_range (value_range *vr, enum value_range_type t, tree min,
27238fd1498Szrj tree max, bitmap equiv)
27338fd1498Szrj {
27438fd1498Szrj /* Check the validity of the range. */
27538fd1498Szrj if (flag_checking
27638fd1498Szrj && (t == VR_RANGE || t == VR_ANTI_RANGE))
27738fd1498Szrj {
27838fd1498Szrj int cmp;
27938fd1498Szrj
28038fd1498Szrj gcc_assert (min && max);
28138fd1498Szrj
28238fd1498Szrj gcc_assert (!TREE_OVERFLOW_P (min) && !TREE_OVERFLOW_P (max));
28338fd1498Szrj
28438fd1498Szrj if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
28538fd1498Szrj gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
28638fd1498Szrj
28738fd1498Szrj cmp = compare_values (min, max);
28838fd1498Szrj gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
28938fd1498Szrj }
29038fd1498Szrj
29138fd1498Szrj if (flag_checking
29238fd1498Szrj && (t == VR_UNDEFINED || t == VR_VARYING))
29338fd1498Szrj {
29438fd1498Szrj gcc_assert (min == NULL_TREE && max == NULL_TREE);
29538fd1498Szrj gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
29638fd1498Szrj }
29738fd1498Szrj
29838fd1498Szrj vr->type = t;
29938fd1498Szrj vr->min = min;
30038fd1498Szrj vr->max = max;
30138fd1498Szrj
30238fd1498Szrj /* Since updating the equivalence set involves deep copying the
30338fd1498Szrj bitmaps, only do it if absolutely necessary.
30438fd1498Szrj
30538fd1498Szrj All equivalence bitmaps are allocated from the same obstack. So
30638fd1498Szrj we can use the obstack associated with EQUIV to allocate vr->equiv. */
30738fd1498Szrj if (vr->equiv == NULL
30838fd1498Szrj && equiv != NULL)
30938fd1498Szrj vr->equiv = BITMAP_ALLOC (equiv->obstack);
31038fd1498Szrj
31138fd1498Szrj if (equiv != vr->equiv)
31238fd1498Szrj {
31338fd1498Szrj if (equiv && !bitmap_empty_p (equiv))
31438fd1498Szrj bitmap_copy (vr->equiv, equiv);
31538fd1498Szrj else
31638fd1498Szrj bitmap_clear (vr->equiv);
31738fd1498Szrj }
31838fd1498Szrj }
31938fd1498Szrj
32038fd1498Szrj
32138fd1498Szrj /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
32238fd1498Szrj This means adjusting T, MIN and MAX representing the case of a
32338fd1498Szrj wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
32438fd1498Szrj as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
32538fd1498Szrj In corner cases where MAX+1 or MIN-1 wraps this will fall back
32638fd1498Szrj to varying.
32738fd1498Szrj This routine exists to ease canonicalization in the case where we
32838fd1498Szrj extract ranges from var + CST op limit. */
32938fd1498Szrj
33038fd1498Szrj void
set_and_canonicalize_value_range(value_range * vr,enum value_range_type t,tree min,tree max,bitmap equiv)33138fd1498Szrj set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
33238fd1498Szrj tree min, tree max, bitmap equiv)
33338fd1498Szrj {
33438fd1498Szrj /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
33538fd1498Szrj if (t == VR_UNDEFINED)
33638fd1498Szrj {
33738fd1498Szrj set_value_range_to_undefined (vr);
33838fd1498Szrj return;
33938fd1498Szrj }
34038fd1498Szrj else if (t == VR_VARYING)
34138fd1498Szrj {
34238fd1498Szrj set_value_range_to_varying (vr);
34338fd1498Szrj return;
34438fd1498Szrj }
34538fd1498Szrj
34638fd1498Szrj /* Nothing to canonicalize for symbolic ranges. */
34738fd1498Szrj if (TREE_CODE (min) != INTEGER_CST
34838fd1498Szrj || TREE_CODE (max) != INTEGER_CST)
34938fd1498Szrj {
35038fd1498Szrj set_value_range (vr, t, min, max, equiv);
35138fd1498Szrj return;
35238fd1498Szrj }
35338fd1498Szrj
35438fd1498Szrj /* Wrong order for min and max, to swap them and the VR type we need
35538fd1498Szrj to adjust them. */
35638fd1498Szrj if (tree_int_cst_lt (max, min))
35738fd1498Szrj {
35838fd1498Szrj tree one, tmp;
35938fd1498Szrj
36038fd1498Szrj /* For one bit precision if max < min, then the swapped
36138fd1498Szrj range covers all values, so for VR_RANGE it is varying and
36238fd1498Szrj for VR_ANTI_RANGE empty range, so drop to varying as well. */
36338fd1498Szrj if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
36438fd1498Szrj {
36538fd1498Szrj set_value_range_to_varying (vr);
36638fd1498Szrj return;
36738fd1498Szrj }
36838fd1498Szrj
36938fd1498Szrj one = build_int_cst (TREE_TYPE (min), 1);
37038fd1498Szrj tmp = int_const_binop (PLUS_EXPR, max, one);
37138fd1498Szrj max = int_const_binop (MINUS_EXPR, min, one);
37238fd1498Szrj min = tmp;
37338fd1498Szrj
37438fd1498Szrj /* There's one corner case, if we had [C+1, C] before we now have
37538fd1498Szrj that again. But this represents an empty value range, so drop
37638fd1498Szrj to varying in this case. */
37738fd1498Szrj if (tree_int_cst_lt (max, min))
37838fd1498Szrj {
37938fd1498Szrj set_value_range_to_varying (vr);
38038fd1498Szrj return;
38138fd1498Szrj }
38238fd1498Szrj
38338fd1498Szrj t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
38438fd1498Szrj }
38538fd1498Szrj
38638fd1498Szrj /* Anti-ranges that can be represented as ranges should be so. */
38738fd1498Szrj if (t == VR_ANTI_RANGE)
38838fd1498Szrj {
38938fd1498Szrj /* For -fstrict-enums we may receive out-of-range ranges so consider
39038fd1498Szrj values < -INF and values > INF as -INF/INF as well. */
39138fd1498Szrj tree type = TREE_TYPE (min);
39238fd1498Szrj bool is_min = (INTEGRAL_TYPE_P (type)
39338fd1498Szrj && tree_int_cst_compare (min, TYPE_MIN_VALUE (type)) <= 0);
39438fd1498Szrj bool is_max = (INTEGRAL_TYPE_P (type)
39538fd1498Szrj && tree_int_cst_compare (max, TYPE_MAX_VALUE (type)) >= 0);
39638fd1498Szrj
39738fd1498Szrj if (is_min && is_max)
39838fd1498Szrj {
39938fd1498Szrj /* We cannot deal with empty ranges, drop to varying.
40038fd1498Szrj ??? This could be VR_UNDEFINED instead. */
40138fd1498Szrj set_value_range_to_varying (vr);
40238fd1498Szrj return;
40338fd1498Szrj }
40438fd1498Szrj else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
40538fd1498Szrj && (is_min || is_max))
40638fd1498Szrj {
40738fd1498Szrj /* Non-empty boolean ranges can always be represented
40838fd1498Szrj as a singleton range. */
40938fd1498Szrj if (is_min)
41038fd1498Szrj min = max = vrp_val_max (TREE_TYPE (min));
41138fd1498Szrj else
41238fd1498Szrj min = max = vrp_val_min (TREE_TYPE (min));
41338fd1498Szrj t = VR_RANGE;
41438fd1498Szrj }
41538fd1498Szrj else if (is_min
41638fd1498Szrj /* As a special exception preserve non-null ranges. */
41738fd1498Szrj && !(TYPE_UNSIGNED (TREE_TYPE (min))
41838fd1498Szrj && integer_zerop (max)))
41938fd1498Szrj {
42038fd1498Szrj tree one = build_int_cst (TREE_TYPE (max), 1);
42138fd1498Szrj min = int_const_binop (PLUS_EXPR, max, one);
42238fd1498Szrj max = vrp_val_max (TREE_TYPE (max));
42338fd1498Szrj t = VR_RANGE;
42438fd1498Szrj }
42538fd1498Szrj else if (is_max)
42638fd1498Szrj {
42738fd1498Szrj tree one = build_int_cst (TREE_TYPE (min), 1);
42838fd1498Szrj max = int_const_binop (MINUS_EXPR, min, one);
42938fd1498Szrj min = vrp_val_min (TREE_TYPE (min));
43038fd1498Szrj t = VR_RANGE;
43138fd1498Szrj }
43238fd1498Szrj }
43338fd1498Szrj
43438fd1498Szrj /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
43538fd1498Szrj to make sure VRP iteration terminates, otherwise we can get into
43638fd1498Szrj oscillations. */
43738fd1498Szrj
43838fd1498Szrj set_value_range (vr, t, min, max, equiv);
43938fd1498Szrj }
44038fd1498Szrj
44138fd1498Szrj /* Copy value range FROM into value range TO. */
44238fd1498Szrj
44338fd1498Szrj void
copy_value_range(value_range * to,value_range * from)44438fd1498Szrj copy_value_range (value_range *to, value_range *from)
44538fd1498Szrj {
44638fd1498Szrj set_value_range (to, from->type, from->min, from->max, from->equiv);
44738fd1498Szrj }
44838fd1498Szrj
44938fd1498Szrj /* Set value range VR to a single value. This function is only called
45038fd1498Szrj with values we get from statements, and exists to clear the
45138fd1498Szrj TREE_OVERFLOW flag. */
45238fd1498Szrj
45338fd1498Szrj void
set_value_range_to_value(value_range * vr,tree val,bitmap equiv)45438fd1498Szrj set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
45538fd1498Szrj {
45638fd1498Szrj gcc_assert (is_gimple_min_invariant (val));
45738fd1498Szrj if (TREE_OVERFLOW_P (val))
45838fd1498Szrj val = drop_tree_overflow (val);
45938fd1498Szrj set_value_range (vr, VR_RANGE, val, val, equiv);
46038fd1498Szrj }
46138fd1498Szrj
46238fd1498Szrj /* Set value range VR to a non-NULL range of type TYPE. */
46338fd1498Szrj
46438fd1498Szrj void
set_value_range_to_nonnull(value_range * vr,tree type)46538fd1498Szrj set_value_range_to_nonnull (value_range *vr, tree type)
46638fd1498Szrj {
46738fd1498Szrj tree zero = build_int_cst (type, 0);
46838fd1498Szrj set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
46938fd1498Szrj }
47038fd1498Szrj
47138fd1498Szrj
47238fd1498Szrj /* Set value range VR to a NULL range of type TYPE. */
47338fd1498Szrj
47438fd1498Szrj void
set_value_range_to_null(value_range * vr,tree type)47538fd1498Szrj set_value_range_to_null (value_range *vr, tree type)
47638fd1498Szrj {
47738fd1498Szrj set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
47838fd1498Szrj }
47938fd1498Szrj
48038fd1498Szrj
48138fd1498Szrj /* If abs (min) < abs (max), set VR to [-max, max], if
48238fd1498Szrj abs (min) >= abs (max), set VR to [-min, min]. */
48338fd1498Szrj
48438fd1498Szrj static void
abs_extent_range(value_range * vr,tree min,tree max)48538fd1498Szrj abs_extent_range (value_range *vr, tree min, tree max)
48638fd1498Szrj {
48738fd1498Szrj int cmp;
48838fd1498Szrj
48938fd1498Szrj gcc_assert (TREE_CODE (min) == INTEGER_CST);
49038fd1498Szrj gcc_assert (TREE_CODE (max) == INTEGER_CST);
49138fd1498Szrj gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
49238fd1498Szrj gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
49338fd1498Szrj min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
49438fd1498Szrj max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
49538fd1498Szrj if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
49638fd1498Szrj {
49738fd1498Szrj set_value_range_to_varying (vr);
49838fd1498Szrj return;
49938fd1498Szrj }
50038fd1498Szrj cmp = compare_values (min, max);
50138fd1498Szrj if (cmp == -1)
50238fd1498Szrj min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
50338fd1498Szrj else if (cmp == 0 || cmp == 1)
50438fd1498Szrj {
50538fd1498Szrj max = min;
50638fd1498Szrj min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
50738fd1498Szrj }
50838fd1498Szrj else
50938fd1498Szrj {
51038fd1498Szrj set_value_range_to_varying (vr);
51138fd1498Szrj return;
51238fd1498Szrj }
51338fd1498Szrj set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
51438fd1498Szrj }
51538fd1498Szrj
51638fd1498Szrj /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
51738fd1498Szrj
51838fd1498Szrj bool
vrp_operand_equal_p(const_tree val1,const_tree val2)51938fd1498Szrj vrp_operand_equal_p (const_tree val1, const_tree val2)
52038fd1498Szrj {
52138fd1498Szrj if (val1 == val2)
52238fd1498Szrj return true;
52338fd1498Szrj if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
52438fd1498Szrj return false;
52538fd1498Szrj return true;
52638fd1498Szrj }
52738fd1498Szrj
52838fd1498Szrj /* Return true, if the bitmaps B1 and B2 are equal. */
52938fd1498Szrj
53038fd1498Szrj bool
vrp_bitmap_equal_p(const_bitmap b1,const_bitmap b2)53138fd1498Szrj vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
53238fd1498Szrj {
53338fd1498Szrj return (b1 == b2
53438fd1498Szrj || ((!b1 || bitmap_empty_p (b1))
53538fd1498Szrj && (!b2 || bitmap_empty_p (b2)))
53638fd1498Szrj || (b1 && b2
53738fd1498Szrj && bitmap_equal_p (b1, b2)));
53838fd1498Szrj }
53938fd1498Szrj
54038fd1498Szrj /* Return true if VR is ~[0, 0]. */
54138fd1498Szrj
54238fd1498Szrj bool
range_is_nonnull(value_range * vr)54338fd1498Szrj range_is_nonnull (value_range *vr)
54438fd1498Szrj {
54538fd1498Szrj return vr->type == VR_ANTI_RANGE
54638fd1498Szrj && integer_zerop (vr->min)
54738fd1498Szrj && integer_zerop (vr->max);
54838fd1498Szrj }
54938fd1498Szrj
55038fd1498Szrj
55138fd1498Szrj /* Return true if VR is [0, 0]. */
55238fd1498Szrj
55338fd1498Szrj static inline bool
range_is_null(value_range * vr)55438fd1498Szrj range_is_null (value_range *vr)
55538fd1498Szrj {
55638fd1498Szrj return vr->type == VR_RANGE
55738fd1498Szrj && integer_zerop (vr->min)
55838fd1498Szrj && integer_zerop (vr->max);
55938fd1498Szrj }
56038fd1498Szrj
56138fd1498Szrj /* Return true if max and min of VR are INTEGER_CST. It's not necessary
56238fd1498Szrj a singleton. */
56338fd1498Szrj
56438fd1498Szrj bool
range_int_cst_p(value_range * vr)56538fd1498Szrj range_int_cst_p (value_range *vr)
56638fd1498Szrj {
56738fd1498Szrj return (vr->type == VR_RANGE
56838fd1498Szrj && TREE_CODE (vr->max) == INTEGER_CST
56938fd1498Szrj && TREE_CODE (vr->min) == INTEGER_CST);
57038fd1498Szrj }
57138fd1498Szrj
57238fd1498Szrj /* Return true if VR is a INTEGER_CST singleton. */
57338fd1498Szrj
57438fd1498Szrj bool
range_int_cst_singleton_p(value_range * vr)57538fd1498Szrj range_int_cst_singleton_p (value_range *vr)
57638fd1498Szrj {
57738fd1498Szrj return (range_int_cst_p (vr)
57838fd1498Szrj && tree_int_cst_equal (vr->min, vr->max));
57938fd1498Szrj }
58038fd1498Szrj
58138fd1498Szrj /* Return true if value range VR involves at least one symbol. */
58238fd1498Szrj
58338fd1498Szrj bool
symbolic_range_p(value_range * vr)58438fd1498Szrj symbolic_range_p (value_range *vr)
58538fd1498Szrj {
58638fd1498Szrj return (!is_gimple_min_invariant (vr->min)
58738fd1498Szrj || !is_gimple_min_invariant (vr->max));
58838fd1498Szrj }
58938fd1498Szrj
59038fd1498Szrj /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
59138fd1498Szrj otherwise. We only handle additive operations and set NEG to true if the
59238fd1498Szrj symbol is negated and INV to the invariant part, if any. */
59338fd1498Szrj
59438fd1498Szrj tree
get_single_symbol(tree t,bool * neg,tree * inv)59538fd1498Szrj get_single_symbol (tree t, bool *neg, tree *inv)
59638fd1498Szrj {
59738fd1498Szrj bool neg_;
59838fd1498Szrj tree inv_;
59938fd1498Szrj
60038fd1498Szrj *inv = NULL_TREE;
60138fd1498Szrj *neg = false;
60238fd1498Szrj
60338fd1498Szrj if (TREE_CODE (t) == PLUS_EXPR
60438fd1498Szrj || TREE_CODE (t) == POINTER_PLUS_EXPR
60538fd1498Szrj || TREE_CODE (t) == MINUS_EXPR)
60638fd1498Szrj {
60738fd1498Szrj if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
60838fd1498Szrj {
60938fd1498Szrj neg_ = (TREE_CODE (t) == MINUS_EXPR);
61038fd1498Szrj inv_ = TREE_OPERAND (t, 0);
61138fd1498Szrj t = TREE_OPERAND (t, 1);
61238fd1498Szrj }
61338fd1498Szrj else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
61438fd1498Szrj {
61538fd1498Szrj neg_ = false;
61638fd1498Szrj inv_ = TREE_OPERAND (t, 1);
61738fd1498Szrj t = TREE_OPERAND (t, 0);
61838fd1498Szrj }
61938fd1498Szrj else
62038fd1498Szrj return NULL_TREE;
62138fd1498Szrj }
62238fd1498Szrj else
62338fd1498Szrj {
62438fd1498Szrj neg_ = false;
62538fd1498Szrj inv_ = NULL_TREE;
62638fd1498Szrj }
62738fd1498Szrj
62838fd1498Szrj if (TREE_CODE (t) == NEGATE_EXPR)
62938fd1498Szrj {
63038fd1498Szrj t = TREE_OPERAND (t, 0);
63138fd1498Szrj neg_ = !neg_;
63238fd1498Szrj }
63338fd1498Szrj
63438fd1498Szrj if (TREE_CODE (t) != SSA_NAME)
63538fd1498Szrj return NULL_TREE;
63638fd1498Szrj
63738fd1498Szrj if (inv_ && TREE_OVERFLOW_P (inv_))
63838fd1498Szrj inv_ = drop_tree_overflow (inv_);
63938fd1498Szrj
64038fd1498Szrj *neg = neg_;
64138fd1498Szrj *inv = inv_;
64238fd1498Szrj return t;
64338fd1498Szrj }
64438fd1498Szrj
64538fd1498Szrj /* The reverse operation: build a symbolic expression with TYPE
64638fd1498Szrj from symbol SYM, negated according to NEG, and invariant INV. */
64738fd1498Szrj
64838fd1498Szrj static tree
build_symbolic_expr(tree type,tree sym,bool neg,tree inv)64938fd1498Szrj build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
65038fd1498Szrj {
65138fd1498Szrj const bool pointer_p = POINTER_TYPE_P (type);
65238fd1498Szrj tree t = sym;
65338fd1498Szrj
65438fd1498Szrj if (neg)
65538fd1498Szrj t = build1 (NEGATE_EXPR, type, t);
65638fd1498Szrj
65738fd1498Szrj if (integer_zerop (inv))
65838fd1498Szrj return t;
65938fd1498Szrj
66038fd1498Szrj return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
66138fd1498Szrj }
66238fd1498Szrj
66338fd1498Szrj /* Return
66438fd1498Szrj 1 if VAL < VAL2
66538fd1498Szrj 0 if !(VAL < VAL2)
66638fd1498Szrj -2 if those are incomparable. */
66738fd1498Szrj int
operand_less_p(tree val,tree val2)66838fd1498Szrj operand_less_p (tree val, tree val2)
66938fd1498Szrj {
67038fd1498Szrj /* LT is folded faster than GE and others. Inline the common case. */
67138fd1498Szrj if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
67238fd1498Szrj return tree_int_cst_lt (val, val2);
67338fd1498Szrj else
67438fd1498Szrj {
67538fd1498Szrj tree tcmp;
67638fd1498Szrj
67738fd1498Szrj fold_defer_overflow_warnings ();
67838fd1498Szrj
67938fd1498Szrj tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
68038fd1498Szrj
68138fd1498Szrj fold_undefer_and_ignore_overflow_warnings ();
68238fd1498Szrj
68338fd1498Szrj if (!tcmp
68438fd1498Szrj || TREE_CODE (tcmp) != INTEGER_CST)
68538fd1498Szrj return -2;
68638fd1498Szrj
68738fd1498Szrj if (!integer_zerop (tcmp))
68838fd1498Szrj return 1;
68938fd1498Szrj }
69038fd1498Szrj
69138fd1498Szrj return 0;
69238fd1498Szrj }
69338fd1498Szrj
69438fd1498Szrj /* Compare two values VAL1 and VAL2. Return
69538fd1498Szrj
69638fd1498Szrj -2 if VAL1 and VAL2 cannot be compared at compile-time,
69738fd1498Szrj -1 if VAL1 < VAL2,
69838fd1498Szrj 0 if VAL1 == VAL2,
69938fd1498Szrj +1 if VAL1 > VAL2, and
70038fd1498Szrj +2 if VAL1 != VAL2
70138fd1498Szrj
70238fd1498Szrj This is similar to tree_int_cst_compare but supports pointer values
70338fd1498Szrj and values that cannot be compared at compile time.
70438fd1498Szrj
70538fd1498Szrj If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
70638fd1498Szrj true if the return value is only valid if we assume that signed
70738fd1498Szrj overflow is undefined. */
70838fd1498Szrj
70938fd1498Szrj int
compare_values_warnv(tree val1,tree val2,bool * strict_overflow_p)71038fd1498Szrj compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
71138fd1498Szrj {
71238fd1498Szrj if (val1 == val2)
71338fd1498Szrj return 0;
71438fd1498Szrj
71538fd1498Szrj /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
71638fd1498Szrj both integers. */
71738fd1498Szrj gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
71838fd1498Szrj == POINTER_TYPE_P (TREE_TYPE (val2)));
71938fd1498Szrj
72038fd1498Szrj /* Convert the two values into the same type. This is needed because
72138fd1498Szrj sizetype causes sign extension even for unsigned types. */
72238fd1498Szrj val2 = fold_convert (TREE_TYPE (val1), val2);
72338fd1498Szrj STRIP_USELESS_TYPE_CONVERSION (val2);
72438fd1498Szrj
72538fd1498Szrj const bool overflow_undefined
72638fd1498Szrj = INTEGRAL_TYPE_P (TREE_TYPE (val1))
72738fd1498Szrj && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
72838fd1498Szrj tree inv1, inv2;
72938fd1498Szrj bool neg1, neg2;
73038fd1498Szrj tree sym1 = get_single_symbol (val1, &neg1, &inv1);
73138fd1498Szrj tree sym2 = get_single_symbol (val2, &neg2, &inv2);
73238fd1498Szrj
73338fd1498Szrj /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
73438fd1498Szrj accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
73538fd1498Szrj if (sym1 && sym2)
73638fd1498Szrj {
73738fd1498Szrj /* Both values must use the same name with the same sign. */
73838fd1498Szrj if (sym1 != sym2 || neg1 != neg2)
73938fd1498Szrj return -2;
74038fd1498Szrj
74138fd1498Szrj /* [-]NAME + CST == [-]NAME + CST. */
74238fd1498Szrj if (inv1 == inv2)
74338fd1498Szrj return 0;
74438fd1498Szrj
74538fd1498Szrj /* If overflow is defined we cannot simplify more. */
74638fd1498Szrj if (!overflow_undefined)
74738fd1498Szrj return -2;
74838fd1498Szrj
74938fd1498Szrj if (strict_overflow_p != NULL
75038fd1498Szrj /* Symbolic range building sets TREE_NO_WARNING to declare
75138fd1498Szrj that overflow doesn't happen. */
75238fd1498Szrj && (!inv1 || !TREE_NO_WARNING (val1))
75338fd1498Szrj && (!inv2 || !TREE_NO_WARNING (val2)))
75438fd1498Szrj *strict_overflow_p = true;
75538fd1498Szrj
75638fd1498Szrj if (!inv1)
75738fd1498Szrj inv1 = build_int_cst (TREE_TYPE (val1), 0);
75838fd1498Szrj if (!inv2)
75938fd1498Szrj inv2 = build_int_cst (TREE_TYPE (val2), 0);
76038fd1498Szrj
76138fd1498Szrj return wi::cmp (wi::to_wide (inv1), wi::to_wide (inv2),
76238fd1498Szrj TYPE_SIGN (TREE_TYPE (val1)));
76338fd1498Szrj }
76438fd1498Szrj
76538fd1498Szrj const bool cst1 = is_gimple_min_invariant (val1);
76638fd1498Szrj const bool cst2 = is_gimple_min_invariant (val2);
76738fd1498Szrj
76838fd1498Szrj /* If one is of the form '[-]NAME + CST' and the other is constant, then
76938fd1498Szrj it might be possible to say something depending on the constants. */
77038fd1498Szrj if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
77138fd1498Szrj {
77238fd1498Szrj if (!overflow_undefined)
77338fd1498Szrj return -2;
77438fd1498Szrj
77538fd1498Szrj if (strict_overflow_p != NULL
77638fd1498Szrj /* Symbolic range building sets TREE_NO_WARNING to declare
77738fd1498Szrj that overflow doesn't happen. */
77838fd1498Szrj && (!sym1 || !TREE_NO_WARNING (val1))
77938fd1498Szrj && (!sym2 || !TREE_NO_WARNING (val2)))
78038fd1498Szrj *strict_overflow_p = true;
78138fd1498Szrj
78238fd1498Szrj const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
78338fd1498Szrj tree cst = cst1 ? val1 : val2;
78438fd1498Szrj tree inv = cst1 ? inv2 : inv1;
78538fd1498Szrj
78638fd1498Szrj /* Compute the difference between the constants. If it overflows or
78738fd1498Szrj underflows, this means that we can trivially compare the NAME with
78838fd1498Szrj it and, consequently, the two values with each other. */
78938fd1498Szrj wide_int diff = wi::to_wide (cst) - wi::to_wide (inv);
79038fd1498Szrj if (wi::cmp (0, wi::to_wide (inv), sgn)
79138fd1498Szrj != wi::cmp (diff, wi::to_wide (cst), sgn))
79238fd1498Szrj {
79338fd1498Szrj const int res = wi::cmp (wi::to_wide (cst), wi::to_wide (inv), sgn);
79438fd1498Szrj return cst1 ? res : -res;
79538fd1498Szrj }
79638fd1498Szrj
79738fd1498Szrj return -2;
79838fd1498Szrj }
79938fd1498Szrj
80038fd1498Szrj /* We cannot say anything more for non-constants. */
80138fd1498Szrj if (!cst1 || !cst2)
80238fd1498Szrj return -2;
80338fd1498Szrj
80438fd1498Szrj if (!POINTER_TYPE_P (TREE_TYPE (val1)))
80538fd1498Szrj {
80638fd1498Szrj /* We cannot compare overflowed values. */
80738fd1498Szrj if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
80838fd1498Szrj return -2;
80938fd1498Szrj
81038fd1498Szrj if (TREE_CODE (val1) == INTEGER_CST
81138fd1498Szrj && TREE_CODE (val2) == INTEGER_CST)
81238fd1498Szrj return tree_int_cst_compare (val1, val2);
81338fd1498Szrj
81438fd1498Szrj if (poly_int_tree_p (val1) && poly_int_tree_p (val2))
81538fd1498Szrj {
81638fd1498Szrj if (known_eq (wi::to_poly_widest (val1),
81738fd1498Szrj wi::to_poly_widest (val2)))
81838fd1498Szrj return 0;
81938fd1498Szrj if (known_lt (wi::to_poly_widest (val1),
82038fd1498Szrj wi::to_poly_widest (val2)))
82138fd1498Szrj return -1;
82238fd1498Szrj if (known_gt (wi::to_poly_widest (val1),
82338fd1498Szrj wi::to_poly_widest (val2)))
82438fd1498Szrj return 1;
82538fd1498Szrj }
82638fd1498Szrj
82738fd1498Szrj return -2;
82838fd1498Szrj }
82938fd1498Szrj else
83038fd1498Szrj {
83138fd1498Szrj tree t;
83238fd1498Szrj
83338fd1498Szrj /* First see if VAL1 and VAL2 are not the same. */
83438fd1498Szrj if (val1 == val2 || operand_equal_p (val1, val2, 0))
83538fd1498Szrj return 0;
83638fd1498Szrj
83738fd1498Szrj /* If VAL1 is a lower address than VAL2, return -1. */
83838fd1498Szrj if (operand_less_p (val1, val2) == 1)
83938fd1498Szrj return -1;
84038fd1498Szrj
84138fd1498Szrj /* If VAL1 is a higher address than VAL2, return +1. */
84238fd1498Szrj if (operand_less_p (val2, val1) == 1)
84338fd1498Szrj return 1;
84438fd1498Szrj
84538fd1498Szrj /* If VAL1 is different than VAL2, return +2.
84638fd1498Szrj For integer constants we either have already returned -1 or 1
84738fd1498Szrj or they are equivalent. We still might succeed in proving
84838fd1498Szrj something about non-trivial operands. */
84938fd1498Szrj if (TREE_CODE (val1) != INTEGER_CST
85038fd1498Szrj || TREE_CODE (val2) != INTEGER_CST)
85138fd1498Szrj {
85238fd1498Szrj t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
85338fd1498Szrj if (t && integer_onep (t))
85438fd1498Szrj return 2;
85538fd1498Szrj }
85638fd1498Szrj
85738fd1498Szrj return -2;
85838fd1498Szrj }
85938fd1498Szrj }
86038fd1498Szrj
86138fd1498Szrj /* Compare values like compare_values_warnv. */
86238fd1498Szrj
86338fd1498Szrj int
compare_values(tree val1,tree val2)86438fd1498Szrj compare_values (tree val1, tree val2)
86538fd1498Szrj {
86638fd1498Szrj bool sop;
86738fd1498Szrj return compare_values_warnv (val1, val2, &sop);
86838fd1498Szrj }
86938fd1498Szrj
87038fd1498Szrj
87138fd1498Szrj /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
87238fd1498Szrj 0 if VAL is not inside [MIN, MAX],
87338fd1498Szrj -2 if we cannot tell either way.
87438fd1498Szrj
87538fd1498Szrj Benchmark compile/20001226-1.c compilation time after changing this
87638fd1498Szrj function. */
87738fd1498Szrj
87838fd1498Szrj int
value_inside_range(tree val,tree min,tree max)87938fd1498Szrj value_inside_range (tree val, tree min, tree max)
88038fd1498Szrj {
88138fd1498Szrj int cmp1, cmp2;
88238fd1498Szrj
88338fd1498Szrj cmp1 = operand_less_p (val, min);
88438fd1498Szrj if (cmp1 == -2)
88538fd1498Szrj return -2;
88638fd1498Szrj if (cmp1 == 1)
88738fd1498Szrj return 0;
88838fd1498Szrj
88938fd1498Szrj cmp2 = operand_less_p (max, val);
89038fd1498Szrj if (cmp2 == -2)
89138fd1498Szrj return -2;
89238fd1498Szrj
89338fd1498Szrj return !cmp2;
89438fd1498Szrj }
89538fd1498Szrj
89638fd1498Szrj
89738fd1498Szrj /* Return true if value ranges VR0 and VR1 have a non-empty
89838fd1498Szrj intersection.
89938fd1498Szrj
90038fd1498Szrj Benchmark compile/20001226-1.c compilation time after changing this
90138fd1498Szrj function.
90238fd1498Szrj */
90338fd1498Szrj
90438fd1498Szrj static inline bool
value_ranges_intersect_p(value_range * vr0,value_range * vr1)90538fd1498Szrj value_ranges_intersect_p (value_range *vr0, value_range *vr1)
90638fd1498Szrj {
90738fd1498Szrj /* The value ranges do not intersect if the maximum of the first range is
90838fd1498Szrj less than the minimum of the second range or vice versa.
90938fd1498Szrj When those relations are unknown, we can't do any better. */
91038fd1498Szrj if (operand_less_p (vr0->max, vr1->min) != 0)
91138fd1498Szrj return false;
91238fd1498Szrj if (operand_less_p (vr1->max, vr0->min) != 0)
91338fd1498Szrj return false;
91438fd1498Szrj return true;
91538fd1498Szrj }
91638fd1498Szrj
91738fd1498Szrj
91838fd1498Szrj /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
91938fd1498Szrj include the value zero, -2 if we cannot tell. */
92038fd1498Szrj
92138fd1498Szrj int
range_includes_zero_p(tree min,tree max)92238fd1498Szrj range_includes_zero_p (tree min, tree max)
92338fd1498Szrj {
92438fd1498Szrj tree zero = build_int_cst (TREE_TYPE (min), 0);
92538fd1498Szrj return value_inside_range (zero, min, max);
92638fd1498Szrj }
92738fd1498Szrj
92838fd1498Szrj /* Return true if *VR is know to only contain nonnegative values. */
92938fd1498Szrj
93038fd1498Szrj static inline bool
value_range_nonnegative_p(value_range * vr)93138fd1498Szrj value_range_nonnegative_p (value_range *vr)
93238fd1498Szrj {
93338fd1498Szrj /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
93438fd1498Szrj which would return a useful value should be encoded as a
93538fd1498Szrj VR_RANGE. */
93638fd1498Szrj if (vr->type == VR_RANGE)
93738fd1498Szrj {
93838fd1498Szrj int result = compare_values (vr->min, integer_zero_node);
93938fd1498Szrj return (result == 0 || result == 1);
94038fd1498Szrj }
94138fd1498Szrj
94238fd1498Szrj return false;
94338fd1498Szrj }
94438fd1498Szrj
94538fd1498Szrj /* If *VR has a value rante that is a single constant value return that,
94638fd1498Szrj otherwise return NULL_TREE. */
94738fd1498Szrj
94838fd1498Szrj tree
value_range_constant_singleton(value_range * vr)94938fd1498Szrj value_range_constant_singleton (value_range *vr)
95038fd1498Szrj {
95138fd1498Szrj if (vr->type == VR_RANGE
95238fd1498Szrj && vrp_operand_equal_p (vr->min, vr->max)
95338fd1498Szrj && is_gimple_min_invariant (vr->min))
95438fd1498Szrj return vr->min;
95538fd1498Szrj
95638fd1498Szrj return NULL_TREE;
95738fd1498Szrj }
95838fd1498Szrj
95938fd1498Szrj /* Wrapper around int_const_binop. Return true if we can compute the
96038fd1498Szrj result; i.e. if the operation doesn't overflow or if the overflow is
96138fd1498Szrj undefined. In the latter case (if the operation overflows and
96238fd1498Szrj overflow is undefined), then adjust the result to be -INF or +INF
96338fd1498Szrj depending on CODE, VAL1 and VAL2. Return the value in *RES.
96438fd1498Szrj
96538fd1498Szrj Return false for division by zero, for which the result is
96638fd1498Szrj indeterminate. */
96738fd1498Szrj
96838fd1498Szrj static bool
vrp_int_const_binop(enum tree_code code,tree val1,tree val2,wide_int * res)96938fd1498Szrj vrp_int_const_binop (enum tree_code code, tree val1, tree val2, wide_int *res)
97038fd1498Szrj {
97138fd1498Szrj bool overflow = false;
97238fd1498Szrj signop sign = TYPE_SIGN (TREE_TYPE (val1));
97338fd1498Szrj
97438fd1498Szrj switch (code)
97538fd1498Szrj {
97638fd1498Szrj case RSHIFT_EXPR:
97738fd1498Szrj case LSHIFT_EXPR:
97838fd1498Szrj {
97938fd1498Szrj wide_int wval2 = wi::to_wide (val2, TYPE_PRECISION (TREE_TYPE (val1)));
98038fd1498Szrj if (wi::neg_p (wval2))
98138fd1498Szrj {
98238fd1498Szrj wval2 = -wval2;
98338fd1498Szrj if (code == RSHIFT_EXPR)
98438fd1498Szrj code = LSHIFT_EXPR;
98538fd1498Szrj else
98638fd1498Szrj code = RSHIFT_EXPR;
98738fd1498Szrj }
98838fd1498Szrj
98938fd1498Szrj if (code == RSHIFT_EXPR)
99038fd1498Szrj /* It's unclear from the C standard whether shifts can overflow.
99138fd1498Szrj The following code ignores overflow; perhaps a C standard
99238fd1498Szrj interpretation ruling is needed. */
99338fd1498Szrj *res = wi::rshift (wi::to_wide (val1), wval2, sign);
99438fd1498Szrj else
99538fd1498Szrj *res = wi::lshift (wi::to_wide (val1), wval2);
99638fd1498Szrj break;
99738fd1498Szrj }
99838fd1498Szrj
99938fd1498Szrj case MULT_EXPR:
100038fd1498Szrj *res = wi::mul (wi::to_wide (val1),
100138fd1498Szrj wi::to_wide (val2), sign, &overflow);
100238fd1498Szrj break;
100338fd1498Szrj
100438fd1498Szrj case TRUNC_DIV_EXPR:
100538fd1498Szrj case EXACT_DIV_EXPR:
100638fd1498Szrj if (val2 == 0)
100738fd1498Szrj return false;
100838fd1498Szrj else
100938fd1498Szrj *res = wi::div_trunc (wi::to_wide (val1),
101038fd1498Szrj wi::to_wide (val2), sign, &overflow);
101138fd1498Szrj break;
101238fd1498Szrj
101338fd1498Szrj case FLOOR_DIV_EXPR:
101438fd1498Szrj if (val2 == 0)
101538fd1498Szrj return false;
101638fd1498Szrj *res = wi::div_floor (wi::to_wide (val1),
101738fd1498Szrj wi::to_wide (val2), sign, &overflow);
101838fd1498Szrj break;
101938fd1498Szrj
102038fd1498Szrj case CEIL_DIV_EXPR:
102138fd1498Szrj if (val2 == 0)
102238fd1498Szrj return false;
102338fd1498Szrj *res = wi::div_ceil (wi::to_wide (val1),
102438fd1498Szrj wi::to_wide (val2), sign, &overflow);
102538fd1498Szrj break;
102638fd1498Szrj
102738fd1498Szrj case ROUND_DIV_EXPR:
102838fd1498Szrj if (val2 == 0)
102938fd1498Szrj return false;
103038fd1498Szrj *res = wi::div_round (wi::to_wide (val1),
103138fd1498Szrj wi::to_wide (val2), sign, &overflow);
103238fd1498Szrj break;
103338fd1498Szrj
103438fd1498Szrj default:
103538fd1498Szrj gcc_unreachable ();
103638fd1498Szrj }
103738fd1498Szrj
103838fd1498Szrj if (overflow
103938fd1498Szrj && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
104038fd1498Szrj {
104138fd1498Szrj /* If the operation overflowed return -INF or +INF depending
104238fd1498Szrj on the operation and the combination of signs of the operands. */
104338fd1498Szrj int sgn1 = tree_int_cst_sgn (val1);
104438fd1498Szrj int sgn2 = tree_int_cst_sgn (val2);
104538fd1498Szrj
104638fd1498Szrj /* Notice that we only need to handle the restricted set of
104738fd1498Szrj operations handled by extract_range_from_binary_expr.
104838fd1498Szrj Among them, only multiplication, addition and subtraction
104938fd1498Szrj can yield overflow without overflown operands because we
105038fd1498Szrj are working with integral types only... except in the
105138fd1498Szrj case VAL1 = -INF and VAL2 = -1 which overflows to +INF
105238fd1498Szrj for division too. */
105338fd1498Szrj
105438fd1498Szrj /* For multiplication, the sign of the overflow is given
105538fd1498Szrj by the comparison of the signs of the operands. */
105638fd1498Szrj if ((code == MULT_EXPR && sgn1 == sgn2)
105738fd1498Szrj /* For addition, the operands must be of the same sign
105838fd1498Szrj to yield an overflow. Its sign is therefore that
105938fd1498Szrj of one of the operands, for example the first. */
106038fd1498Szrj || (code == PLUS_EXPR && sgn1 >= 0)
106138fd1498Szrj /* For subtraction, operands must be of
106238fd1498Szrj different signs to yield an overflow. Its sign is
106338fd1498Szrj therefore that of the first operand or the opposite of
106438fd1498Szrj that of the second operand. A first operand of 0 counts
106538fd1498Szrj as positive here, for the corner case 0 - (-INF), which
106638fd1498Szrj overflows, but must yield +INF. */
106738fd1498Szrj || (code == MINUS_EXPR && sgn1 >= 0)
106838fd1498Szrj /* For division, the only case is -INF / -1 = +INF. */
106938fd1498Szrj || code == TRUNC_DIV_EXPR
107038fd1498Szrj || code == FLOOR_DIV_EXPR
107138fd1498Szrj || code == CEIL_DIV_EXPR
107238fd1498Szrj || code == EXACT_DIV_EXPR
107338fd1498Szrj || code == ROUND_DIV_EXPR)
107438fd1498Szrj *res = wi::max_value (TYPE_PRECISION (TREE_TYPE (val1)),
107538fd1498Szrj TYPE_SIGN (TREE_TYPE (val1)));
107638fd1498Szrj else
107738fd1498Szrj *res = wi::min_value (TYPE_PRECISION (TREE_TYPE (val1)),
107838fd1498Szrj TYPE_SIGN (TREE_TYPE (val1)));
107938fd1498Szrj return true;
108038fd1498Szrj }
108138fd1498Szrj
108238fd1498Szrj return !overflow;
108338fd1498Szrj }
108438fd1498Szrj
108538fd1498Szrj
108638fd1498Szrj /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
108738fd1498Szrj bitmask if some bit is unset, it means for all numbers in the range
108838fd1498Szrj the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
108938fd1498Szrj bitmask if some bit is set, it means for all numbers in the range
109038fd1498Szrj the bit is 1, otherwise it might be 0 or 1. */
109138fd1498Szrj
109238fd1498Szrj bool
zero_nonzero_bits_from_vr(const tree expr_type,value_range * vr,wide_int * may_be_nonzero,wide_int * must_be_nonzero)109338fd1498Szrj zero_nonzero_bits_from_vr (const tree expr_type,
109438fd1498Szrj value_range *vr,
109538fd1498Szrj wide_int *may_be_nonzero,
109638fd1498Szrj wide_int *must_be_nonzero)
109738fd1498Szrj {
109838fd1498Szrj *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
109938fd1498Szrj *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
110038fd1498Szrj if (!range_int_cst_p (vr))
110138fd1498Szrj return false;
110238fd1498Szrj
110338fd1498Szrj if (range_int_cst_singleton_p (vr))
110438fd1498Szrj {
110538fd1498Szrj *may_be_nonzero = wi::to_wide (vr->min);
110638fd1498Szrj *must_be_nonzero = *may_be_nonzero;
110738fd1498Szrj }
110838fd1498Szrj else if (tree_int_cst_sgn (vr->min) >= 0
110938fd1498Szrj || tree_int_cst_sgn (vr->max) < 0)
111038fd1498Szrj {
111138fd1498Szrj wide_int xor_mask = wi::to_wide (vr->min) ^ wi::to_wide (vr->max);
111238fd1498Szrj *may_be_nonzero = wi::to_wide (vr->min) | wi::to_wide (vr->max);
111338fd1498Szrj *must_be_nonzero = wi::to_wide (vr->min) & wi::to_wide (vr->max);
111438fd1498Szrj if (xor_mask != 0)
111538fd1498Szrj {
111638fd1498Szrj wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
111738fd1498Szrj may_be_nonzero->get_precision ());
111838fd1498Szrj *may_be_nonzero = *may_be_nonzero | mask;
111938fd1498Szrj *must_be_nonzero = wi::bit_and_not (*must_be_nonzero, mask);
112038fd1498Szrj }
112138fd1498Szrj }
112238fd1498Szrj
112338fd1498Szrj return true;
112438fd1498Szrj }
112538fd1498Szrj
112638fd1498Szrj /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
112738fd1498Szrj so that *VR0 U *VR1 == *AR. Returns true if that is possible,
112838fd1498Szrj false otherwise. If *AR can be represented with a single range
112938fd1498Szrj *VR1 will be VR_UNDEFINED. */
113038fd1498Szrj
113138fd1498Szrj static bool
ranges_from_anti_range(value_range * ar,value_range * vr0,value_range * vr1)113238fd1498Szrj ranges_from_anti_range (value_range *ar,
113338fd1498Szrj value_range *vr0, value_range *vr1)
113438fd1498Szrj {
113538fd1498Szrj tree type = TREE_TYPE (ar->min);
113638fd1498Szrj
113738fd1498Szrj vr0->type = VR_UNDEFINED;
113838fd1498Szrj vr1->type = VR_UNDEFINED;
113938fd1498Szrj
114038fd1498Szrj if (ar->type != VR_ANTI_RANGE
114138fd1498Szrj || TREE_CODE (ar->min) != INTEGER_CST
114238fd1498Szrj || TREE_CODE (ar->max) != INTEGER_CST
114338fd1498Szrj || !vrp_val_min (type)
114438fd1498Szrj || !vrp_val_max (type))
114538fd1498Szrj return false;
114638fd1498Szrj
114738fd1498Szrj if (!vrp_val_is_min (ar->min))
114838fd1498Szrj {
114938fd1498Szrj vr0->type = VR_RANGE;
115038fd1498Szrj vr0->min = vrp_val_min (type);
115138fd1498Szrj vr0->max = wide_int_to_tree (type, wi::to_wide (ar->min) - 1);
115238fd1498Szrj }
115338fd1498Szrj if (!vrp_val_is_max (ar->max))
115438fd1498Szrj {
115538fd1498Szrj vr1->type = VR_RANGE;
115638fd1498Szrj vr1->min = wide_int_to_tree (type, wi::to_wide (ar->max) + 1);
115738fd1498Szrj vr1->max = vrp_val_max (type);
115838fd1498Szrj }
115938fd1498Szrj if (vr0->type == VR_UNDEFINED)
116038fd1498Szrj {
116138fd1498Szrj *vr0 = *vr1;
116238fd1498Szrj vr1->type = VR_UNDEFINED;
116338fd1498Szrj }
116438fd1498Szrj
116538fd1498Szrj return vr0->type != VR_UNDEFINED;
116638fd1498Szrj }
116738fd1498Szrj
116838fd1498Szrj /* Helper to extract a value-range *VR for a multiplicative operation
116938fd1498Szrj *VR0 CODE *VR1. */
117038fd1498Szrj
117138fd1498Szrj static void
extract_range_from_multiplicative_op_1(value_range * vr,enum tree_code code,value_range * vr0,value_range * vr1)117238fd1498Szrj extract_range_from_multiplicative_op_1 (value_range *vr,
117338fd1498Szrj enum tree_code code,
117438fd1498Szrj value_range *vr0, value_range *vr1)
117538fd1498Szrj {
117638fd1498Szrj enum value_range_type rtype;
117738fd1498Szrj wide_int val, min, max;
117838fd1498Szrj tree type;
117938fd1498Szrj
118038fd1498Szrj /* Multiplications, divisions and shifts are a bit tricky to handle,
118138fd1498Szrj depending on the mix of signs we have in the two ranges, we
118238fd1498Szrj need to operate on different values to get the minimum and
118338fd1498Szrj maximum values for the new range. One approach is to figure
118438fd1498Szrj out all the variations of range combinations and do the
118538fd1498Szrj operations.
118638fd1498Szrj
118738fd1498Szrj However, this involves several calls to compare_values and it
118838fd1498Szrj is pretty convoluted. It's simpler to do the 4 operations
118938fd1498Szrj (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
119038fd1498Szrj MAX1) and then figure the smallest and largest values to form
119138fd1498Szrj the new range. */
119238fd1498Szrj gcc_assert (code == MULT_EXPR
119338fd1498Szrj || code == TRUNC_DIV_EXPR
119438fd1498Szrj || code == FLOOR_DIV_EXPR
119538fd1498Szrj || code == CEIL_DIV_EXPR
119638fd1498Szrj || code == EXACT_DIV_EXPR
119738fd1498Szrj || code == ROUND_DIV_EXPR
119838fd1498Szrj || code == RSHIFT_EXPR
119938fd1498Szrj || code == LSHIFT_EXPR);
120038fd1498Szrj gcc_assert (vr0->type == VR_RANGE
120138fd1498Szrj && vr0->type == vr1->type);
120238fd1498Szrj
120338fd1498Szrj rtype = vr0->type;
120438fd1498Szrj type = TREE_TYPE (vr0->min);
120538fd1498Szrj signop sgn = TYPE_SIGN (type);
120638fd1498Szrj
120738fd1498Szrj /* Compute the 4 cross operations and their minimum and maximum value. */
120838fd1498Szrj if (!vrp_int_const_binop (code, vr0->min, vr1->min, &val))
120938fd1498Szrj {
121038fd1498Szrj set_value_range_to_varying (vr);
121138fd1498Szrj return;
121238fd1498Szrj }
121338fd1498Szrj min = max = val;
121438fd1498Szrj
121538fd1498Szrj if (vr1->max != vr1->min)
121638fd1498Szrj {
121738fd1498Szrj if (!vrp_int_const_binop (code, vr0->min, vr1->max, &val))
121838fd1498Szrj {
121938fd1498Szrj set_value_range_to_varying (vr);
122038fd1498Szrj return;
122138fd1498Szrj }
122238fd1498Szrj if (wi::lt_p (val, min, sgn))
122338fd1498Szrj min = val;
122438fd1498Szrj else if (wi::gt_p (val, max, sgn))
122538fd1498Szrj max = val;
122638fd1498Szrj }
122738fd1498Szrj
122838fd1498Szrj if (vr0->max != vr0->min)
122938fd1498Szrj {
123038fd1498Szrj if (!vrp_int_const_binop (code, vr0->max, vr1->min, &val))
123138fd1498Szrj {
123238fd1498Szrj set_value_range_to_varying (vr);
123338fd1498Szrj return;
123438fd1498Szrj }
123538fd1498Szrj if (wi::lt_p (val, min, sgn))
123638fd1498Szrj min = val;
123738fd1498Szrj else if (wi::gt_p (val, max, sgn))
123838fd1498Szrj max = val;
123938fd1498Szrj }
124038fd1498Szrj
124138fd1498Szrj if (vr0->min != vr0->max && vr1->min != vr1->max)
124238fd1498Szrj {
124338fd1498Szrj if (!vrp_int_const_binop (code, vr0->max, vr1->max, &val))
124438fd1498Szrj {
124538fd1498Szrj set_value_range_to_varying (vr);
124638fd1498Szrj return;
124738fd1498Szrj }
124838fd1498Szrj if (wi::lt_p (val, min, sgn))
124938fd1498Szrj min = val;
125038fd1498Szrj else if (wi::gt_p (val, max, sgn))
125138fd1498Szrj max = val;
125238fd1498Szrj }
125338fd1498Szrj
125438fd1498Szrj /* If the new range has its limits swapped around (MIN > MAX),
125538fd1498Szrj then the operation caused one of them to wrap around, mark
125638fd1498Szrj the new range VARYING. */
125738fd1498Szrj if (wi::gt_p (min, max, sgn))
125838fd1498Szrj {
125938fd1498Szrj set_value_range_to_varying (vr);
126038fd1498Szrj return;
126138fd1498Szrj }
126238fd1498Szrj
126338fd1498Szrj /* We punt for [-INF, +INF].
126438fd1498Szrj We learn nothing when we have INF on both sides.
126538fd1498Szrj Note that we do accept [-INF, -INF] and [+INF, +INF]. */
126638fd1498Szrj if (wi::eq_p (min, wi::min_value (TYPE_PRECISION (type), sgn))
126738fd1498Szrj && wi::eq_p (max, wi::max_value (TYPE_PRECISION (type), sgn)))
126838fd1498Szrj {
126938fd1498Szrj set_value_range_to_varying (vr);
127038fd1498Szrj return;
127138fd1498Szrj }
127238fd1498Szrj
127338fd1498Szrj set_value_range (vr, rtype,
127438fd1498Szrj wide_int_to_tree (type, min),
127538fd1498Szrj wide_int_to_tree (type, max), NULL);
127638fd1498Szrj }
127738fd1498Szrj
127838fd1498Szrj /* Extract range information from a binary operation CODE based on
127938fd1498Szrj the ranges of each of its operands *VR0 and *VR1 with resulting
128038fd1498Szrj type EXPR_TYPE. The resulting range is stored in *VR. */
128138fd1498Szrj
128238fd1498Szrj void
extract_range_from_binary_expr_1(value_range * vr,enum tree_code code,tree expr_type,value_range * vr0_,value_range * vr1_)128338fd1498Szrj extract_range_from_binary_expr_1 (value_range *vr,
128438fd1498Szrj enum tree_code code, tree expr_type,
128538fd1498Szrj value_range *vr0_, value_range *vr1_)
128638fd1498Szrj {
128738fd1498Szrj value_range vr0 = *vr0_, vr1 = *vr1_;
128838fd1498Szrj value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
128938fd1498Szrj enum value_range_type type;
129038fd1498Szrj tree min = NULL_TREE, max = NULL_TREE;
129138fd1498Szrj int cmp;
129238fd1498Szrj
129338fd1498Szrj if (!INTEGRAL_TYPE_P (expr_type)
129438fd1498Szrj && !POINTER_TYPE_P (expr_type))
129538fd1498Szrj {
129638fd1498Szrj set_value_range_to_varying (vr);
129738fd1498Szrj return;
129838fd1498Szrj }
129938fd1498Szrj
130038fd1498Szrj /* Not all binary expressions can be applied to ranges in a
130138fd1498Szrj meaningful way. Handle only arithmetic operations. */
130238fd1498Szrj if (code != PLUS_EXPR
130338fd1498Szrj && code != MINUS_EXPR
130438fd1498Szrj && code != POINTER_PLUS_EXPR
130538fd1498Szrj && code != MULT_EXPR
130638fd1498Szrj && code != TRUNC_DIV_EXPR
130738fd1498Szrj && code != FLOOR_DIV_EXPR
130838fd1498Szrj && code != CEIL_DIV_EXPR
130938fd1498Szrj && code != EXACT_DIV_EXPR
131038fd1498Szrj && code != ROUND_DIV_EXPR
131138fd1498Szrj && code != TRUNC_MOD_EXPR
131238fd1498Szrj && code != RSHIFT_EXPR
131338fd1498Szrj && code != LSHIFT_EXPR
131438fd1498Szrj && code != MIN_EXPR
131538fd1498Szrj && code != MAX_EXPR
131638fd1498Szrj && code != BIT_AND_EXPR
131738fd1498Szrj && code != BIT_IOR_EXPR
131838fd1498Szrj && code != BIT_XOR_EXPR)
131938fd1498Szrj {
132038fd1498Szrj set_value_range_to_varying (vr);
132138fd1498Szrj return;
132238fd1498Szrj }
132338fd1498Szrj
132438fd1498Szrj /* If both ranges are UNDEFINED, so is the result. */
132538fd1498Szrj if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
132638fd1498Szrj {
132738fd1498Szrj set_value_range_to_undefined (vr);
132838fd1498Szrj return;
132938fd1498Szrj }
133038fd1498Szrj /* If one of the ranges is UNDEFINED drop it to VARYING for the following
133138fd1498Szrj code. At some point we may want to special-case operations that
133238fd1498Szrj have UNDEFINED result for all or some value-ranges of the not UNDEFINED
133338fd1498Szrj operand. */
133438fd1498Szrj else if (vr0.type == VR_UNDEFINED)
133538fd1498Szrj set_value_range_to_varying (&vr0);
133638fd1498Szrj else if (vr1.type == VR_UNDEFINED)
133738fd1498Szrj set_value_range_to_varying (&vr1);
133838fd1498Szrj
133938fd1498Szrj /* We get imprecise results from ranges_from_anti_range when
134038fd1498Szrj code is EXACT_DIV_EXPR. We could mask out bits in the resulting
134138fd1498Szrj range, but then we also need to hack up vrp_meet. It's just
134238fd1498Szrj easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
134338fd1498Szrj if (code == EXACT_DIV_EXPR
134438fd1498Szrj && vr0.type == VR_ANTI_RANGE
134538fd1498Szrj && vr0.min == vr0.max
134638fd1498Szrj && integer_zerop (vr0.min))
134738fd1498Szrj {
134838fd1498Szrj set_value_range_to_nonnull (vr, expr_type);
134938fd1498Szrj return;
135038fd1498Szrj }
135138fd1498Szrj
135238fd1498Szrj /* Now canonicalize anti-ranges to ranges when they are not symbolic
135338fd1498Szrj and express ~[] op X as ([]' op X) U ([]'' op X). */
135438fd1498Szrj if (vr0.type == VR_ANTI_RANGE
135538fd1498Szrj && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
135638fd1498Szrj {
135738fd1498Szrj extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
135838fd1498Szrj if (vrtem1.type != VR_UNDEFINED)
135938fd1498Szrj {
136038fd1498Szrj value_range vrres = VR_INITIALIZER;
136138fd1498Szrj extract_range_from_binary_expr_1 (&vrres, code, expr_type,
136238fd1498Szrj &vrtem1, vr1_);
136338fd1498Szrj vrp_meet (vr, &vrres);
136438fd1498Szrj }
136538fd1498Szrj return;
136638fd1498Szrj }
136738fd1498Szrj /* Likewise for X op ~[]. */
136838fd1498Szrj if (vr1.type == VR_ANTI_RANGE
136938fd1498Szrj && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
137038fd1498Szrj {
137138fd1498Szrj extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
137238fd1498Szrj if (vrtem1.type != VR_UNDEFINED)
137338fd1498Szrj {
137438fd1498Szrj value_range vrres = VR_INITIALIZER;
137538fd1498Szrj extract_range_from_binary_expr_1 (&vrres, code, expr_type,
137638fd1498Szrj vr0_, &vrtem1);
137738fd1498Szrj vrp_meet (vr, &vrres);
137838fd1498Szrj }
137938fd1498Szrj return;
138038fd1498Szrj }
138138fd1498Szrj
138238fd1498Szrj /* The type of the resulting value range defaults to VR0.TYPE. */
138338fd1498Szrj type = vr0.type;
138438fd1498Szrj
138538fd1498Szrj /* Refuse to operate on VARYING ranges, ranges of different kinds
138638fd1498Szrj and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
138738fd1498Szrj because we may be able to derive a useful range even if one of
138838fd1498Szrj the operands is VR_VARYING or symbolic range. Similarly for
138938fd1498Szrj divisions, MIN/MAX and PLUS/MINUS.
139038fd1498Szrj
139138fd1498Szrj TODO, we may be able to derive anti-ranges in some cases. */
139238fd1498Szrj if (code != BIT_AND_EXPR
139338fd1498Szrj && code != BIT_IOR_EXPR
139438fd1498Szrj && code != TRUNC_DIV_EXPR
139538fd1498Szrj && code != FLOOR_DIV_EXPR
139638fd1498Szrj && code != CEIL_DIV_EXPR
139738fd1498Szrj && code != EXACT_DIV_EXPR
139838fd1498Szrj && code != ROUND_DIV_EXPR
139938fd1498Szrj && code != TRUNC_MOD_EXPR
140038fd1498Szrj && code != MIN_EXPR
140138fd1498Szrj && code != MAX_EXPR
140238fd1498Szrj && code != PLUS_EXPR
140338fd1498Szrj && code != MINUS_EXPR
140438fd1498Szrj && code != RSHIFT_EXPR
140538fd1498Szrj && (vr0.type == VR_VARYING
140638fd1498Szrj || vr1.type == VR_VARYING
140738fd1498Szrj || vr0.type != vr1.type
140838fd1498Szrj || symbolic_range_p (&vr0)
140938fd1498Szrj || symbolic_range_p (&vr1)))
141038fd1498Szrj {
141138fd1498Szrj set_value_range_to_varying (vr);
141238fd1498Szrj return;
141338fd1498Szrj }
141438fd1498Szrj
141538fd1498Szrj /* Now evaluate the expression to determine the new range. */
141638fd1498Szrj if (POINTER_TYPE_P (expr_type))
141738fd1498Szrj {
141838fd1498Szrj if (code == MIN_EXPR || code == MAX_EXPR)
141938fd1498Szrj {
142038fd1498Szrj /* For MIN/MAX expressions with pointers, we only care about
142138fd1498Szrj nullness, if both are non null, then the result is nonnull.
142238fd1498Szrj If both are null, then the result is null. Otherwise they
142338fd1498Szrj are varying. */
142438fd1498Szrj if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
142538fd1498Szrj set_value_range_to_nonnull (vr, expr_type);
142638fd1498Szrj else if (range_is_null (&vr0) && range_is_null (&vr1))
142738fd1498Szrj set_value_range_to_null (vr, expr_type);
142838fd1498Szrj else
142938fd1498Szrj set_value_range_to_varying (vr);
143038fd1498Szrj }
143138fd1498Szrj else if (code == POINTER_PLUS_EXPR)
143238fd1498Szrj {
143338fd1498Szrj /* For pointer types, we are really only interested in asserting
143438fd1498Szrj whether the expression evaluates to non-NULL. */
143538fd1498Szrj if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
143638fd1498Szrj set_value_range_to_nonnull (vr, expr_type);
143738fd1498Szrj else if (range_is_null (&vr0) && range_is_null (&vr1))
143838fd1498Szrj set_value_range_to_null (vr, expr_type);
143938fd1498Szrj else
144038fd1498Szrj set_value_range_to_varying (vr);
144138fd1498Szrj }
144238fd1498Szrj else if (code == BIT_AND_EXPR)
144338fd1498Szrj {
144438fd1498Szrj /* For pointer types, we are really only interested in asserting
144538fd1498Szrj whether the expression evaluates to non-NULL. */
144638fd1498Szrj if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
144738fd1498Szrj set_value_range_to_nonnull (vr, expr_type);
144838fd1498Szrj else if (range_is_null (&vr0) || range_is_null (&vr1))
144938fd1498Szrj set_value_range_to_null (vr, expr_type);
145038fd1498Szrj else
145138fd1498Szrj set_value_range_to_varying (vr);
145238fd1498Szrj }
145338fd1498Szrj else
145438fd1498Szrj set_value_range_to_varying (vr);
145538fd1498Szrj
145638fd1498Szrj return;
145738fd1498Szrj }
145838fd1498Szrj
145938fd1498Szrj /* For integer ranges, apply the operation to each end of the
146038fd1498Szrj range and see what we end up with. */
146138fd1498Szrj if (code == PLUS_EXPR || code == MINUS_EXPR)
146238fd1498Szrj {
146338fd1498Szrj const bool minus_p = (code == MINUS_EXPR);
146438fd1498Szrj tree min_op0 = vr0.min;
146538fd1498Szrj tree min_op1 = minus_p ? vr1.max : vr1.min;
146638fd1498Szrj tree max_op0 = vr0.max;
146738fd1498Szrj tree max_op1 = minus_p ? vr1.min : vr1.max;
146838fd1498Szrj tree sym_min_op0 = NULL_TREE;
146938fd1498Szrj tree sym_min_op1 = NULL_TREE;
147038fd1498Szrj tree sym_max_op0 = NULL_TREE;
147138fd1498Szrj tree sym_max_op1 = NULL_TREE;
147238fd1498Szrj bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
147338fd1498Szrj
147438fd1498Szrj /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
147538fd1498Szrj single-symbolic ranges, try to compute the precise resulting range,
147638fd1498Szrj but only if we know that this resulting range will also be constant
147738fd1498Szrj or single-symbolic. */
147838fd1498Szrj if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
147938fd1498Szrj && (TREE_CODE (min_op0) == INTEGER_CST
148038fd1498Szrj || (sym_min_op0
148138fd1498Szrj = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
148238fd1498Szrj && (TREE_CODE (min_op1) == INTEGER_CST
148338fd1498Szrj || (sym_min_op1
148438fd1498Szrj = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
148538fd1498Szrj && (!(sym_min_op0 && sym_min_op1)
148638fd1498Szrj || (sym_min_op0 == sym_min_op1
148738fd1498Szrj && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
148838fd1498Szrj && (TREE_CODE (max_op0) == INTEGER_CST
148938fd1498Szrj || (sym_max_op0
149038fd1498Szrj = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
149138fd1498Szrj && (TREE_CODE (max_op1) == INTEGER_CST
149238fd1498Szrj || (sym_max_op1
149338fd1498Szrj = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
149438fd1498Szrj && (!(sym_max_op0 && sym_max_op1)
149538fd1498Szrj || (sym_max_op0 == sym_max_op1
149638fd1498Szrj && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
149738fd1498Szrj {
149838fd1498Szrj const signop sgn = TYPE_SIGN (expr_type);
149938fd1498Szrj const unsigned int prec = TYPE_PRECISION (expr_type);
150038fd1498Szrj wide_int type_min, type_max, wmin, wmax;
150138fd1498Szrj int min_ovf = 0;
150238fd1498Szrj int max_ovf = 0;
150338fd1498Szrj
150438fd1498Szrj /* Get the lower and upper bounds of the type. */
150538fd1498Szrj if (TYPE_OVERFLOW_WRAPS (expr_type))
150638fd1498Szrj {
150738fd1498Szrj type_min = wi::min_value (prec, sgn);
150838fd1498Szrj type_max = wi::max_value (prec, sgn);
150938fd1498Szrj }
151038fd1498Szrj else
151138fd1498Szrj {
151238fd1498Szrj type_min = wi::to_wide (vrp_val_min (expr_type));
151338fd1498Szrj type_max = wi::to_wide (vrp_val_max (expr_type));
151438fd1498Szrj }
151538fd1498Szrj
151638fd1498Szrj /* Combine the lower bounds, if any. */
151738fd1498Szrj if (min_op0 && min_op1)
151838fd1498Szrj {
151938fd1498Szrj if (minus_p)
152038fd1498Szrj {
152138fd1498Szrj wmin = wi::to_wide (min_op0) - wi::to_wide (min_op1);
152238fd1498Szrj
152338fd1498Szrj /* Check for overflow. */
152438fd1498Szrj if (wi::cmp (0, wi::to_wide (min_op1), sgn)
152538fd1498Szrj != wi::cmp (wmin, wi::to_wide (min_op0), sgn))
152638fd1498Szrj min_ovf = wi::cmp (wi::to_wide (min_op0),
152738fd1498Szrj wi::to_wide (min_op1), sgn);
152838fd1498Szrj }
152938fd1498Szrj else
153038fd1498Szrj {
153138fd1498Szrj wmin = wi::to_wide (min_op0) + wi::to_wide (min_op1);
153238fd1498Szrj
153338fd1498Szrj /* Check for overflow. */
153438fd1498Szrj if (wi::cmp (wi::to_wide (min_op1), 0, sgn)
153538fd1498Szrj != wi::cmp (wmin, wi::to_wide (min_op0), sgn))
153638fd1498Szrj min_ovf = wi::cmp (wi::to_wide (min_op0), wmin, sgn);
153738fd1498Szrj }
153838fd1498Szrj }
153938fd1498Szrj else if (min_op0)
154038fd1498Szrj wmin = wi::to_wide (min_op0);
154138fd1498Szrj else if (min_op1)
154238fd1498Szrj {
154338fd1498Szrj if (minus_p)
154438fd1498Szrj {
154538fd1498Szrj wmin = -wi::to_wide (min_op1);
154638fd1498Szrj
154738fd1498Szrj /* Check for overflow. */
154838fd1498Szrj if (sgn == SIGNED
154938fd1498Szrj && wi::neg_p (wi::to_wide (min_op1))
155038fd1498Szrj && wi::neg_p (wmin))
155138fd1498Szrj min_ovf = 1;
155238fd1498Szrj else if (sgn == UNSIGNED && wi::to_wide (min_op1) != 0)
155338fd1498Szrj min_ovf = -1;
155438fd1498Szrj }
155538fd1498Szrj else
155638fd1498Szrj wmin = wi::to_wide (min_op1);
155738fd1498Szrj }
155838fd1498Szrj else
155938fd1498Szrj wmin = wi::shwi (0, prec);
156038fd1498Szrj
156138fd1498Szrj /* Combine the upper bounds, if any. */
156238fd1498Szrj if (max_op0 && max_op1)
156338fd1498Szrj {
156438fd1498Szrj if (minus_p)
156538fd1498Szrj {
156638fd1498Szrj wmax = wi::to_wide (max_op0) - wi::to_wide (max_op1);
156738fd1498Szrj
156838fd1498Szrj /* Check for overflow. */
156938fd1498Szrj if (wi::cmp (0, wi::to_wide (max_op1), sgn)
157038fd1498Szrj != wi::cmp (wmax, wi::to_wide (max_op0), sgn))
157138fd1498Szrj max_ovf = wi::cmp (wi::to_wide (max_op0),
157238fd1498Szrj wi::to_wide (max_op1), sgn);
157338fd1498Szrj }
157438fd1498Szrj else
157538fd1498Szrj {
157638fd1498Szrj wmax = wi::to_wide (max_op0) + wi::to_wide (max_op1);
157738fd1498Szrj
157838fd1498Szrj if (wi::cmp (wi::to_wide (max_op1), 0, sgn)
157938fd1498Szrj != wi::cmp (wmax, wi::to_wide (max_op0), sgn))
158038fd1498Szrj max_ovf = wi::cmp (wi::to_wide (max_op0), wmax, sgn);
158138fd1498Szrj }
158238fd1498Szrj }
158338fd1498Szrj else if (max_op0)
158438fd1498Szrj wmax = wi::to_wide (max_op0);
158538fd1498Szrj else if (max_op1)
158638fd1498Szrj {
158738fd1498Szrj if (minus_p)
158838fd1498Szrj {
158938fd1498Szrj wmax = -wi::to_wide (max_op1);
159038fd1498Szrj
159138fd1498Szrj /* Check for overflow. */
159238fd1498Szrj if (sgn == SIGNED
159338fd1498Szrj && wi::neg_p (wi::to_wide (max_op1))
159438fd1498Szrj && wi::neg_p (wmax))
159538fd1498Szrj max_ovf = 1;
159638fd1498Szrj else if (sgn == UNSIGNED && wi::to_wide (max_op1) != 0)
159738fd1498Szrj max_ovf = -1;
159838fd1498Szrj }
159938fd1498Szrj else
160038fd1498Szrj wmax = wi::to_wide (max_op1);
160138fd1498Szrj }
160238fd1498Szrj else
160338fd1498Szrj wmax = wi::shwi (0, prec);
160438fd1498Szrj
160538fd1498Szrj /* Check for type overflow. */
160638fd1498Szrj if (min_ovf == 0)
160738fd1498Szrj {
160838fd1498Szrj if (wi::cmp (wmin, type_min, sgn) == -1)
160938fd1498Szrj min_ovf = -1;
161038fd1498Szrj else if (wi::cmp (wmin, type_max, sgn) == 1)
161138fd1498Szrj min_ovf = 1;
161238fd1498Szrj }
161338fd1498Szrj if (max_ovf == 0)
161438fd1498Szrj {
161538fd1498Szrj if (wi::cmp (wmax, type_min, sgn) == -1)
161638fd1498Szrj max_ovf = -1;
161738fd1498Szrj else if (wi::cmp (wmax, type_max, sgn) == 1)
161838fd1498Szrj max_ovf = 1;
161938fd1498Szrj }
162038fd1498Szrj
162138fd1498Szrj /* If we have overflow for the constant part and the resulting
162238fd1498Szrj range will be symbolic, drop to VR_VARYING. */
162338fd1498Szrj if ((min_ovf && sym_min_op0 != sym_min_op1)
162438fd1498Szrj || (max_ovf && sym_max_op0 != sym_max_op1))
162538fd1498Szrj {
162638fd1498Szrj set_value_range_to_varying (vr);
162738fd1498Szrj return;
162838fd1498Szrj }
162938fd1498Szrj
163038fd1498Szrj if (TYPE_OVERFLOW_WRAPS (expr_type))
163138fd1498Szrj {
163238fd1498Szrj /* If overflow wraps, truncate the values and adjust the
163338fd1498Szrj range kind and bounds appropriately. */
163438fd1498Szrj wide_int tmin = wide_int::from (wmin, prec, sgn);
163538fd1498Szrj wide_int tmax = wide_int::from (wmax, prec, sgn);
163638fd1498Szrj if (min_ovf == max_ovf)
163738fd1498Szrj {
163838fd1498Szrj /* No overflow or both overflow or underflow. The
163938fd1498Szrj range kind stays VR_RANGE. */
164038fd1498Szrj min = wide_int_to_tree (expr_type, tmin);
164138fd1498Szrj max = wide_int_to_tree (expr_type, tmax);
164238fd1498Szrj }
164338fd1498Szrj else if ((min_ovf == -1 && max_ovf == 0)
164438fd1498Szrj || (max_ovf == 1 && min_ovf == 0))
164538fd1498Szrj {
164638fd1498Szrj /* Min underflow or max overflow. The range kind
164738fd1498Szrj changes to VR_ANTI_RANGE. */
164838fd1498Szrj bool covers = false;
164938fd1498Szrj wide_int tem = tmin;
165038fd1498Szrj type = VR_ANTI_RANGE;
165138fd1498Szrj tmin = tmax + 1;
165238fd1498Szrj if (wi::cmp (tmin, tmax, sgn) < 0)
165338fd1498Szrj covers = true;
165438fd1498Szrj tmax = tem - 1;
165538fd1498Szrj if (wi::cmp (tmax, tem, sgn) > 0)
165638fd1498Szrj covers = true;
165738fd1498Szrj /* If the anti-range would cover nothing, drop to varying.
165838fd1498Szrj Likewise if the anti-range bounds are outside of the
165938fd1498Szrj types values. */
166038fd1498Szrj if (covers || wi::cmp (tmin, tmax, sgn) > 0)
166138fd1498Szrj {
166238fd1498Szrj set_value_range_to_varying (vr);
166338fd1498Szrj return;
166438fd1498Szrj }
166538fd1498Szrj min = wide_int_to_tree (expr_type, tmin);
166638fd1498Szrj max = wide_int_to_tree (expr_type, tmax);
166738fd1498Szrj }
166838fd1498Szrj else
166938fd1498Szrj {
167038fd1498Szrj /* Other underflow and/or overflow, drop to VR_VARYING. */
167138fd1498Szrj set_value_range_to_varying (vr);
167238fd1498Szrj return;
167338fd1498Szrj }
167438fd1498Szrj }
167538fd1498Szrj else
167638fd1498Szrj {
167738fd1498Szrj /* If overflow does not wrap, saturate to the types min/max
167838fd1498Szrj value. */
167938fd1498Szrj if (min_ovf == -1)
168038fd1498Szrj min = wide_int_to_tree (expr_type, type_min);
168138fd1498Szrj else if (min_ovf == 1)
168238fd1498Szrj min = wide_int_to_tree (expr_type, type_max);
168338fd1498Szrj else
168438fd1498Szrj min = wide_int_to_tree (expr_type, wmin);
168538fd1498Szrj
168638fd1498Szrj if (max_ovf == -1)
168738fd1498Szrj max = wide_int_to_tree (expr_type, type_min);
168838fd1498Szrj else if (max_ovf == 1)
168938fd1498Szrj max = wide_int_to_tree (expr_type, type_max);
169038fd1498Szrj else
169138fd1498Szrj max = wide_int_to_tree (expr_type, wmax);
169238fd1498Szrj }
169338fd1498Szrj
169438fd1498Szrj /* If the result lower bound is constant, we're done;
169538fd1498Szrj otherwise, build the symbolic lower bound. */
169638fd1498Szrj if (sym_min_op0 == sym_min_op1)
169738fd1498Szrj ;
169838fd1498Szrj else if (sym_min_op0)
169938fd1498Szrj min = build_symbolic_expr (expr_type, sym_min_op0,
170038fd1498Szrj neg_min_op0, min);
170138fd1498Szrj else if (sym_min_op1)
170238fd1498Szrj {
170338fd1498Szrj /* We may not negate if that might introduce
170438fd1498Szrj undefined overflow. */
170538fd1498Szrj if (! minus_p
170638fd1498Szrj || neg_min_op1
170738fd1498Szrj || TYPE_OVERFLOW_WRAPS (expr_type))
170838fd1498Szrj min = build_symbolic_expr (expr_type, sym_min_op1,
170938fd1498Szrj neg_min_op1 ^ minus_p, min);
171038fd1498Szrj else
171138fd1498Szrj min = NULL_TREE;
171238fd1498Szrj }
171338fd1498Szrj
171438fd1498Szrj /* Likewise for the upper bound. */
171538fd1498Szrj if (sym_max_op0 == sym_max_op1)
171638fd1498Szrj ;
171738fd1498Szrj else if (sym_max_op0)
171838fd1498Szrj max = build_symbolic_expr (expr_type, sym_max_op0,
171938fd1498Szrj neg_max_op0, max);
172038fd1498Szrj else if (sym_max_op1)
172138fd1498Szrj {
172238fd1498Szrj /* We may not negate if that might introduce
172338fd1498Szrj undefined overflow. */
172438fd1498Szrj if (! minus_p
172538fd1498Szrj || neg_max_op1
172638fd1498Szrj || TYPE_OVERFLOW_WRAPS (expr_type))
172738fd1498Szrj max = build_symbolic_expr (expr_type, sym_max_op1,
172838fd1498Szrj neg_max_op1 ^ minus_p, max);
172938fd1498Szrj else
173038fd1498Szrj max = NULL_TREE;
173138fd1498Szrj }
173238fd1498Szrj }
173338fd1498Szrj else
173438fd1498Szrj {
173538fd1498Szrj /* For other cases, for example if we have a PLUS_EXPR with two
173638fd1498Szrj VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
173738fd1498Szrj to compute a precise range for such a case.
173838fd1498Szrj ??? General even mixed range kind operations can be expressed
173938fd1498Szrj by for example transforming ~[3, 5] + [1, 2] to range-only
174038fd1498Szrj operations and a union primitive:
174138fd1498Szrj [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
174238fd1498Szrj [-INF+1, 4] U [6, +INF(OVF)]
174338fd1498Szrj though usually the union is not exactly representable with
174438fd1498Szrj a single range or anti-range as the above is
174538fd1498Szrj [-INF+1, +INF(OVF)] intersected with ~[5, 5]
174638fd1498Szrj but one could use a scheme similar to equivalences for this. */
174738fd1498Szrj set_value_range_to_varying (vr);
174838fd1498Szrj return;
174938fd1498Szrj }
175038fd1498Szrj }
175138fd1498Szrj else if (code == MIN_EXPR
175238fd1498Szrj || code == MAX_EXPR)
175338fd1498Szrj {
175438fd1498Szrj if (vr0.type == VR_RANGE
175538fd1498Szrj && !symbolic_range_p (&vr0))
175638fd1498Szrj {
175738fd1498Szrj type = VR_RANGE;
175838fd1498Szrj if (vr1.type == VR_RANGE
175938fd1498Szrj && !symbolic_range_p (&vr1))
176038fd1498Szrj {
176138fd1498Szrj /* For operations that make the resulting range directly
176238fd1498Szrj proportional to the original ranges, apply the operation to
176338fd1498Szrj the same end of each range. */
176438fd1498Szrj min = int_const_binop (code, vr0.min, vr1.min);
176538fd1498Szrj max = int_const_binop (code, vr0.max, vr1.max);
176638fd1498Szrj }
176738fd1498Szrj else if (code == MIN_EXPR)
176838fd1498Szrj {
176938fd1498Szrj min = vrp_val_min (expr_type);
177038fd1498Szrj max = vr0.max;
177138fd1498Szrj }
177238fd1498Szrj else if (code == MAX_EXPR)
177338fd1498Szrj {
177438fd1498Szrj min = vr0.min;
177538fd1498Szrj max = vrp_val_max (expr_type);
177638fd1498Szrj }
177738fd1498Szrj }
177838fd1498Szrj else if (vr1.type == VR_RANGE
177938fd1498Szrj && !symbolic_range_p (&vr1))
178038fd1498Szrj {
178138fd1498Szrj type = VR_RANGE;
178238fd1498Szrj if (code == MIN_EXPR)
178338fd1498Szrj {
178438fd1498Szrj min = vrp_val_min (expr_type);
178538fd1498Szrj max = vr1.max;
178638fd1498Szrj }
178738fd1498Szrj else if (code == MAX_EXPR)
178838fd1498Szrj {
178938fd1498Szrj min = vr1.min;
179038fd1498Szrj max = vrp_val_max (expr_type);
179138fd1498Szrj }
179238fd1498Szrj }
179338fd1498Szrj else
179438fd1498Szrj {
179538fd1498Szrj set_value_range_to_varying (vr);
179638fd1498Szrj return;
179738fd1498Szrj }
179838fd1498Szrj }
179938fd1498Szrj else if (code == MULT_EXPR)
180038fd1498Szrj {
180138fd1498Szrj /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
180238fd1498Szrj drop to varying. This test requires 2*prec bits if both
180338fd1498Szrj operands are signed and 2*prec + 2 bits if either is not. */
180438fd1498Szrj
180538fd1498Szrj signop sign = TYPE_SIGN (expr_type);
180638fd1498Szrj unsigned int prec = TYPE_PRECISION (expr_type);
180738fd1498Szrj
180838fd1498Szrj if (!range_int_cst_p (&vr0)
180938fd1498Szrj || !range_int_cst_p (&vr1))
181038fd1498Szrj {
181138fd1498Szrj set_value_range_to_varying (vr);
181238fd1498Szrj return;
181338fd1498Szrj }
181438fd1498Szrj
181538fd1498Szrj if (TYPE_OVERFLOW_WRAPS (expr_type))
181638fd1498Szrj {
181738fd1498Szrj typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
181838fd1498Szrj typedef generic_wide_int
181938fd1498Szrj <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
182038fd1498Szrj vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
182138fd1498Szrj vrp_int size = sizem1 + 1;
182238fd1498Szrj
182338fd1498Szrj /* Extend the values using the sign of the result to PREC2.
182438fd1498Szrj From here on out, everthing is just signed math no matter
182538fd1498Szrj what the input types were. */
182638fd1498Szrj vrp_int min0 = vrp_int_cst (vr0.min);
182738fd1498Szrj vrp_int max0 = vrp_int_cst (vr0.max);
182838fd1498Szrj vrp_int min1 = vrp_int_cst (vr1.min);
182938fd1498Szrj vrp_int max1 = vrp_int_cst (vr1.max);
183038fd1498Szrj /* Canonicalize the intervals. */
183138fd1498Szrj if (sign == UNSIGNED)
183238fd1498Szrj {
183338fd1498Szrj if (wi::ltu_p (size, min0 + max0))
183438fd1498Szrj {
183538fd1498Szrj min0 -= size;
183638fd1498Szrj max0 -= size;
183738fd1498Szrj }
183838fd1498Szrj
183938fd1498Szrj if (wi::ltu_p (size, min1 + max1))
184038fd1498Szrj {
184138fd1498Szrj min1 -= size;
184238fd1498Szrj max1 -= size;
184338fd1498Szrj }
184438fd1498Szrj }
184538fd1498Szrj
184638fd1498Szrj vrp_int prod0 = min0 * min1;
184738fd1498Szrj vrp_int prod1 = min0 * max1;
184838fd1498Szrj vrp_int prod2 = max0 * min1;
184938fd1498Szrj vrp_int prod3 = max0 * max1;
185038fd1498Szrj
185138fd1498Szrj /* Sort the 4 products so that min is in prod0 and max is in
185238fd1498Szrj prod3. */
185338fd1498Szrj /* min0min1 > max0max1 */
185438fd1498Szrj if (prod0 > prod3)
185538fd1498Szrj std::swap (prod0, prod3);
185638fd1498Szrj
185738fd1498Szrj /* min0max1 > max0min1 */
185838fd1498Szrj if (prod1 > prod2)
185938fd1498Szrj std::swap (prod1, prod2);
186038fd1498Szrj
186138fd1498Szrj if (prod0 > prod1)
186238fd1498Szrj std::swap (prod0, prod1);
186338fd1498Szrj
186438fd1498Szrj if (prod2 > prod3)
186538fd1498Szrj std::swap (prod2, prod3);
186638fd1498Szrj
186738fd1498Szrj /* diff = max - min. */
186838fd1498Szrj prod2 = prod3 - prod0;
186938fd1498Szrj if (wi::geu_p (prod2, sizem1))
187038fd1498Szrj {
187138fd1498Szrj /* the range covers all values. */
187238fd1498Szrj set_value_range_to_varying (vr);
187338fd1498Szrj return;
187438fd1498Szrj }
187538fd1498Szrj
187638fd1498Szrj /* The following should handle the wrapping and selecting
187738fd1498Szrj VR_ANTI_RANGE for us. */
187838fd1498Szrj min = wide_int_to_tree (expr_type, prod0);
187938fd1498Szrj max = wide_int_to_tree (expr_type, prod3);
188038fd1498Szrj set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
188138fd1498Szrj return;
188238fd1498Szrj }
188338fd1498Szrj
188438fd1498Szrj /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
188538fd1498Szrj drop to VR_VARYING. It would take more effort to compute a
188638fd1498Szrj precise range for such a case. For example, if we have
188738fd1498Szrj op0 == 65536 and op1 == 65536 with their ranges both being
188838fd1498Szrj ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
188938fd1498Szrj we cannot claim that the product is in ~[0,0]. Note that we
189038fd1498Szrj are guaranteed to have vr0.type == vr1.type at this
189138fd1498Szrj point. */
189238fd1498Szrj if (vr0.type == VR_ANTI_RANGE
189338fd1498Szrj && !TYPE_OVERFLOW_UNDEFINED (expr_type))
189438fd1498Szrj {
189538fd1498Szrj set_value_range_to_varying (vr);
189638fd1498Szrj return;
189738fd1498Szrj }
189838fd1498Szrj
189938fd1498Szrj extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
190038fd1498Szrj return;
190138fd1498Szrj }
190238fd1498Szrj else if (code == RSHIFT_EXPR
190338fd1498Szrj || code == LSHIFT_EXPR)
190438fd1498Szrj {
190538fd1498Szrj /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
190638fd1498Szrj then drop to VR_VARYING. Outside of this range we get undefined
190738fd1498Szrj behavior from the shift operation. We cannot even trust
190838fd1498Szrj SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
190938fd1498Szrj shifts, and the operation at the tree level may be widened. */
191038fd1498Szrj if (range_int_cst_p (&vr1)
191138fd1498Szrj && compare_tree_int (vr1.min, 0) >= 0
191238fd1498Szrj && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
191338fd1498Szrj {
191438fd1498Szrj if (code == RSHIFT_EXPR)
191538fd1498Szrj {
191638fd1498Szrj /* Even if vr0 is VARYING or otherwise not usable, we can derive
191738fd1498Szrj useful ranges just from the shift count. E.g.
191838fd1498Szrj x >> 63 for signed 64-bit x is always [-1, 0]. */
191938fd1498Szrj if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
192038fd1498Szrj {
192138fd1498Szrj vr0.type = type = VR_RANGE;
192238fd1498Szrj vr0.min = vrp_val_min (expr_type);
192338fd1498Szrj vr0.max = vrp_val_max (expr_type);
192438fd1498Szrj }
192538fd1498Szrj extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
192638fd1498Szrj return;
192738fd1498Szrj }
192838fd1498Szrj /* We can map lshifts by constants to MULT_EXPR handling. */
192938fd1498Szrj else if (code == LSHIFT_EXPR
193038fd1498Szrj && range_int_cst_singleton_p (&vr1))
193138fd1498Szrj {
193238fd1498Szrj bool saved_flag_wrapv;
193338fd1498Szrj value_range vr1p = VR_INITIALIZER;
193438fd1498Szrj vr1p.type = VR_RANGE;
193538fd1498Szrj vr1p.min = (wide_int_to_tree
193638fd1498Szrj (expr_type,
193738fd1498Szrj wi::set_bit_in_zero (tree_to_shwi (vr1.min),
193838fd1498Szrj TYPE_PRECISION (expr_type))));
193938fd1498Szrj vr1p.max = vr1p.min;
194038fd1498Szrj /* We have to use a wrapping multiply though as signed overflow
194138fd1498Szrj on lshifts is implementation defined in C89. */
194238fd1498Szrj saved_flag_wrapv = flag_wrapv;
194338fd1498Szrj flag_wrapv = 1;
194438fd1498Szrj extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
194538fd1498Szrj &vr0, &vr1p);
194638fd1498Szrj flag_wrapv = saved_flag_wrapv;
194738fd1498Szrj return;
194838fd1498Szrj }
194938fd1498Szrj else if (code == LSHIFT_EXPR
195038fd1498Szrj && range_int_cst_p (&vr0))
195138fd1498Szrj {
195238fd1498Szrj int prec = TYPE_PRECISION (expr_type);
195338fd1498Szrj int overflow_pos = prec;
195438fd1498Szrj int bound_shift;
195538fd1498Szrj wide_int low_bound, high_bound;
195638fd1498Szrj bool uns = TYPE_UNSIGNED (expr_type);
195738fd1498Szrj bool in_bounds = false;
195838fd1498Szrj
195938fd1498Szrj if (!uns)
196038fd1498Szrj overflow_pos -= 1;
196138fd1498Szrj
196238fd1498Szrj bound_shift = overflow_pos - tree_to_shwi (vr1.max);
196338fd1498Szrj /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
196438fd1498Szrj overflow. However, for that to happen, vr1.max needs to be
196538fd1498Szrj zero, which means vr1 is a singleton range of zero, which
196638fd1498Szrj means it should be handled by the previous LSHIFT_EXPR
196738fd1498Szrj if-clause. */
196838fd1498Szrj wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
196938fd1498Szrj wide_int complement = ~(bound - 1);
197038fd1498Szrj
197138fd1498Szrj if (uns)
197238fd1498Szrj {
197338fd1498Szrj low_bound = bound;
197438fd1498Szrj high_bound = complement;
197538fd1498Szrj if (wi::ltu_p (wi::to_wide (vr0.max), low_bound))
197638fd1498Szrj {
197738fd1498Szrj /* [5, 6] << [1, 2] == [10, 24]. */
197838fd1498Szrj /* We're shifting out only zeroes, the value increases
197938fd1498Szrj monotonically. */
198038fd1498Szrj in_bounds = true;
198138fd1498Szrj }
198238fd1498Szrj else if (wi::ltu_p (high_bound, wi::to_wide (vr0.min)))
198338fd1498Szrj {
198438fd1498Szrj /* [0xffffff00, 0xffffffff] << [1, 2]
198538fd1498Szrj == [0xfffffc00, 0xfffffffe]. */
198638fd1498Szrj /* We're shifting out only ones, the value decreases
198738fd1498Szrj monotonically. */
198838fd1498Szrj in_bounds = true;
198938fd1498Szrj }
199038fd1498Szrj }
199138fd1498Szrj else
199238fd1498Szrj {
199338fd1498Szrj /* [-1, 1] << [1, 2] == [-4, 4]. */
199438fd1498Szrj low_bound = complement;
199538fd1498Szrj high_bound = bound;
199638fd1498Szrj if (wi::lts_p (wi::to_wide (vr0.max), high_bound)
199738fd1498Szrj && wi::lts_p (low_bound, wi::to_wide (vr0.min)))
199838fd1498Szrj {
199938fd1498Szrj /* For non-negative numbers, we're shifting out only
200038fd1498Szrj zeroes, the value increases monotonically.
200138fd1498Szrj For negative numbers, we're shifting out only ones, the
200238fd1498Szrj value decreases monotomically. */
200338fd1498Szrj in_bounds = true;
200438fd1498Szrj }
200538fd1498Szrj }
200638fd1498Szrj
200738fd1498Szrj if (in_bounds)
200838fd1498Szrj {
200938fd1498Szrj extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
201038fd1498Szrj return;
201138fd1498Szrj }
201238fd1498Szrj }
201338fd1498Szrj }
201438fd1498Szrj set_value_range_to_varying (vr);
201538fd1498Szrj return;
201638fd1498Szrj }
201738fd1498Szrj else if (code == TRUNC_DIV_EXPR
201838fd1498Szrj || code == FLOOR_DIV_EXPR
201938fd1498Szrj || code == CEIL_DIV_EXPR
202038fd1498Szrj || code == EXACT_DIV_EXPR
202138fd1498Szrj || code == ROUND_DIV_EXPR)
202238fd1498Szrj {
202338fd1498Szrj if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
202438fd1498Szrj {
202538fd1498Szrj /* For division, if op1 has VR_RANGE but op0 does not, something
202638fd1498Szrj can be deduced just from that range. Say [min, max] / [4, max]
202738fd1498Szrj gives [min / 4, max / 4] range. */
202838fd1498Szrj if (vr1.type == VR_RANGE
202938fd1498Szrj && !symbolic_range_p (&vr1)
203038fd1498Szrj && range_includes_zero_p (vr1.min, vr1.max) == 0)
203138fd1498Szrj {
203238fd1498Szrj vr0.type = type = VR_RANGE;
203338fd1498Szrj vr0.min = vrp_val_min (expr_type);
203438fd1498Szrj vr0.max = vrp_val_max (expr_type);
203538fd1498Szrj }
203638fd1498Szrj else
203738fd1498Szrj {
203838fd1498Szrj set_value_range_to_varying (vr);
203938fd1498Szrj return;
204038fd1498Szrj }
204138fd1498Szrj }
204238fd1498Szrj
204338fd1498Szrj /* For divisions, if flag_non_call_exceptions is true, we must
204438fd1498Szrj not eliminate a division by zero. */
204538fd1498Szrj if (cfun->can_throw_non_call_exceptions
204638fd1498Szrj && (vr1.type != VR_RANGE
204738fd1498Szrj || range_includes_zero_p (vr1.min, vr1.max) != 0))
204838fd1498Szrj {
204938fd1498Szrj set_value_range_to_varying (vr);
205038fd1498Szrj return;
205138fd1498Szrj }
205238fd1498Szrj
205338fd1498Szrj /* For divisions, if op0 is VR_RANGE, we can deduce a range
205438fd1498Szrj even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
205538fd1498Szrj include 0. */
205638fd1498Szrj if (vr0.type == VR_RANGE
205738fd1498Szrj && (vr1.type != VR_RANGE
205838fd1498Szrj || range_includes_zero_p (vr1.min, vr1.max) != 0))
205938fd1498Szrj {
206038fd1498Szrj tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
206138fd1498Szrj int cmp;
206238fd1498Szrj
206338fd1498Szrj min = NULL_TREE;
206438fd1498Szrj max = NULL_TREE;
206538fd1498Szrj if (TYPE_UNSIGNED (expr_type)
206638fd1498Szrj || value_range_nonnegative_p (&vr1))
206738fd1498Szrj {
206838fd1498Szrj /* For unsigned division or when divisor is known
206938fd1498Szrj to be non-negative, the range has to cover
207038fd1498Szrj all numbers from 0 to max for positive max
207138fd1498Szrj and all numbers from min to 0 for negative min. */
207238fd1498Szrj cmp = compare_values (vr0.max, zero);
207338fd1498Szrj if (cmp == -1)
207438fd1498Szrj {
207538fd1498Szrj /* When vr0.max < 0, vr1.min != 0 and value
207638fd1498Szrj ranges for dividend and divisor are available. */
207738fd1498Szrj if (vr1.type == VR_RANGE
207838fd1498Szrj && !symbolic_range_p (&vr0)
207938fd1498Szrj && !symbolic_range_p (&vr1)
208038fd1498Szrj && compare_values (vr1.min, zero) != 0)
208138fd1498Szrj max = int_const_binop (code, vr0.max, vr1.min);
208238fd1498Szrj else
208338fd1498Szrj max = zero;
208438fd1498Szrj }
208538fd1498Szrj else if (cmp == 0 || cmp == 1)
208638fd1498Szrj max = vr0.max;
208738fd1498Szrj else
208838fd1498Szrj type = VR_VARYING;
208938fd1498Szrj cmp = compare_values (vr0.min, zero);
209038fd1498Szrj if (cmp == 1)
209138fd1498Szrj {
209238fd1498Szrj /* For unsigned division when value ranges for dividend
209338fd1498Szrj and divisor are available. */
209438fd1498Szrj if (vr1.type == VR_RANGE
209538fd1498Szrj && !symbolic_range_p (&vr0)
209638fd1498Szrj && !symbolic_range_p (&vr1)
209738fd1498Szrj && compare_values (vr1.max, zero) != 0)
209838fd1498Szrj min = int_const_binop (code, vr0.min, vr1.max);
209938fd1498Szrj else
210038fd1498Szrj min = zero;
210138fd1498Szrj }
210238fd1498Szrj else if (cmp == 0 || cmp == -1)
210338fd1498Szrj min = vr0.min;
210438fd1498Szrj else
210538fd1498Szrj type = VR_VARYING;
210638fd1498Szrj }
210738fd1498Szrj else
210838fd1498Szrj {
210938fd1498Szrj /* Otherwise the range is -max .. max or min .. -min
211038fd1498Szrj depending on which bound is bigger in absolute value,
211138fd1498Szrj as the division can change the sign. */
211238fd1498Szrj abs_extent_range (vr, vr0.min, vr0.max);
211338fd1498Szrj return;
211438fd1498Szrj }
211538fd1498Szrj if (type == VR_VARYING)
211638fd1498Szrj {
211738fd1498Szrj set_value_range_to_varying (vr);
211838fd1498Szrj return;
211938fd1498Szrj }
212038fd1498Szrj }
212138fd1498Szrj else if (range_int_cst_p (&vr0) && range_int_cst_p (&vr1))
212238fd1498Szrj {
212338fd1498Szrj extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
212438fd1498Szrj return;
212538fd1498Szrj }
212638fd1498Szrj }
212738fd1498Szrj else if (code == TRUNC_MOD_EXPR)
212838fd1498Szrj {
212938fd1498Szrj if (range_is_null (&vr1))
213038fd1498Szrj {
213138fd1498Szrj set_value_range_to_undefined (vr);
213238fd1498Szrj return;
213338fd1498Szrj }
213438fd1498Szrj /* ABS (A % B) < ABS (B) and either
213538fd1498Szrj 0 <= A % B <= A or A <= A % B <= 0. */
213638fd1498Szrj type = VR_RANGE;
213738fd1498Szrj signop sgn = TYPE_SIGN (expr_type);
213838fd1498Szrj unsigned int prec = TYPE_PRECISION (expr_type);
213938fd1498Szrj wide_int wmin, wmax, tmp;
214038fd1498Szrj if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
214138fd1498Szrj {
214238fd1498Szrj wmax = wi::to_wide (vr1.max) - 1;
214338fd1498Szrj if (sgn == SIGNED)
214438fd1498Szrj {
214538fd1498Szrj tmp = -1 - wi::to_wide (vr1.min);
214638fd1498Szrj wmax = wi::smax (wmax, tmp);
214738fd1498Szrj }
214838fd1498Szrj }
214938fd1498Szrj else
215038fd1498Szrj {
215138fd1498Szrj wmax = wi::max_value (prec, sgn);
215238fd1498Szrj /* X % INT_MIN may be INT_MAX. */
215338fd1498Szrj if (sgn == UNSIGNED)
215438fd1498Szrj wmax = wmax - 1;
215538fd1498Szrj }
215638fd1498Szrj
215738fd1498Szrj if (sgn == UNSIGNED)
215838fd1498Szrj wmin = wi::zero (prec);
215938fd1498Szrj else
216038fd1498Szrj {
216138fd1498Szrj wmin = -wmax;
216238fd1498Szrj if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
216338fd1498Szrj {
216438fd1498Szrj tmp = wi::to_wide (vr0.min);
216538fd1498Szrj if (wi::gts_p (tmp, 0))
216638fd1498Szrj tmp = wi::zero (prec);
216738fd1498Szrj wmin = wi::smax (wmin, tmp);
216838fd1498Szrj }
216938fd1498Szrj }
217038fd1498Szrj
217138fd1498Szrj if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
217238fd1498Szrj {
217338fd1498Szrj tmp = wi::to_wide (vr0.max);
217438fd1498Szrj if (sgn == SIGNED && wi::neg_p (tmp))
217538fd1498Szrj tmp = wi::zero (prec);
217638fd1498Szrj wmax = wi::min (wmax, tmp, sgn);
217738fd1498Szrj }
217838fd1498Szrj
217938fd1498Szrj min = wide_int_to_tree (expr_type, wmin);
218038fd1498Szrj max = wide_int_to_tree (expr_type, wmax);
218138fd1498Szrj }
218238fd1498Szrj else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
218338fd1498Szrj {
218438fd1498Szrj bool int_cst_range0, int_cst_range1;
218538fd1498Szrj wide_int may_be_nonzero0, may_be_nonzero1;
218638fd1498Szrj wide_int must_be_nonzero0, must_be_nonzero1;
218738fd1498Szrj
218838fd1498Szrj int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
218938fd1498Szrj &may_be_nonzero0,
219038fd1498Szrj &must_be_nonzero0);
219138fd1498Szrj int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
219238fd1498Szrj &may_be_nonzero1,
219338fd1498Szrj &must_be_nonzero1);
219438fd1498Szrj
219538fd1498Szrj if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR)
219638fd1498Szrj {
219738fd1498Szrj value_range *vr0p = NULL, *vr1p = NULL;
219838fd1498Szrj if (range_int_cst_singleton_p (&vr1))
219938fd1498Szrj {
220038fd1498Szrj vr0p = &vr0;
220138fd1498Szrj vr1p = &vr1;
220238fd1498Szrj }
220338fd1498Szrj else if (range_int_cst_singleton_p (&vr0))
220438fd1498Szrj {
220538fd1498Szrj vr0p = &vr1;
220638fd1498Szrj vr1p = &vr0;
220738fd1498Szrj }
220838fd1498Szrj /* For op & or | attempt to optimize:
220938fd1498Szrj [x, y] op z into [x op z, y op z]
221038fd1498Szrj if z is a constant which (for op | its bitwise not) has n
221138fd1498Szrj consecutive least significant bits cleared followed by m 1
221238fd1498Szrj consecutive bits set immediately above it and either
221338fd1498Szrj m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
221438fd1498Szrj The least significant n bits of all the values in the range are
221538fd1498Szrj cleared or set, the m bits above it are preserved and any bits
221638fd1498Szrj above these are required to be the same for all values in the
221738fd1498Szrj range. */
221838fd1498Szrj if (vr0p && range_int_cst_p (vr0p))
221938fd1498Szrj {
222038fd1498Szrj wide_int w = wi::to_wide (vr1p->min);
222138fd1498Szrj int m = 0, n = 0;
222238fd1498Szrj if (code == BIT_IOR_EXPR)
222338fd1498Szrj w = ~w;
222438fd1498Szrj if (wi::eq_p (w, 0))
222538fd1498Szrj n = TYPE_PRECISION (expr_type);
222638fd1498Szrj else
222738fd1498Szrj {
222838fd1498Szrj n = wi::ctz (w);
222938fd1498Szrj w = ~(w | wi::mask (n, false, w.get_precision ()));
223038fd1498Szrj if (wi::eq_p (w, 0))
223138fd1498Szrj m = TYPE_PRECISION (expr_type) - n;
223238fd1498Szrj else
223338fd1498Szrj m = wi::ctz (w) - n;
223438fd1498Szrj }
223538fd1498Szrj wide_int mask = wi::mask (m + n, true, w.get_precision ());
223638fd1498Szrj if ((mask & wi::to_wide (vr0p->min))
223738fd1498Szrj == (mask & wi::to_wide (vr0p->max)))
223838fd1498Szrj {
223938fd1498Szrj min = int_const_binop (code, vr0p->min, vr1p->min);
224038fd1498Szrj max = int_const_binop (code, vr0p->max, vr1p->min);
224138fd1498Szrj }
224238fd1498Szrj }
224338fd1498Szrj }
224438fd1498Szrj
224538fd1498Szrj type = VR_RANGE;
224638fd1498Szrj if (min && max)
224738fd1498Szrj /* Optimized above already. */;
224838fd1498Szrj else if (code == BIT_AND_EXPR)
224938fd1498Szrj {
225038fd1498Szrj min = wide_int_to_tree (expr_type,
225138fd1498Szrj must_be_nonzero0 & must_be_nonzero1);
225238fd1498Szrj wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
225338fd1498Szrj /* If both input ranges contain only negative values we can
225438fd1498Szrj truncate the result range maximum to the minimum of the
225538fd1498Szrj input range maxima. */
225638fd1498Szrj if (int_cst_range0 && int_cst_range1
225738fd1498Szrj && tree_int_cst_sgn (vr0.max) < 0
225838fd1498Szrj && tree_int_cst_sgn (vr1.max) < 0)
225938fd1498Szrj {
226038fd1498Szrj wmax = wi::min (wmax, wi::to_wide (vr0.max),
226138fd1498Szrj TYPE_SIGN (expr_type));
226238fd1498Szrj wmax = wi::min (wmax, wi::to_wide (vr1.max),
226338fd1498Szrj TYPE_SIGN (expr_type));
226438fd1498Szrj }
226538fd1498Szrj /* If either input range contains only non-negative values
226638fd1498Szrj we can truncate the result range maximum to the respective
226738fd1498Szrj maximum of the input range. */
226838fd1498Szrj if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
226938fd1498Szrj wmax = wi::min (wmax, wi::to_wide (vr0.max),
227038fd1498Szrj TYPE_SIGN (expr_type));
227138fd1498Szrj if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
227238fd1498Szrj wmax = wi::min (wmax, wi::to_wide (vr1.max),
227338fd1498Szrj TYPE_SIGN (expr_type));
227438fd1498Szrj max = wide_int_to_tree (expr_type, wmax);
227538fd1498Szrj cmp = compare_values (min, max);
227638fd1498Szrj /* PR68217: In case of signed & sign-bit-CST should
227738fd1498Szrj result in [-INF, 0] instead of [-INF, INF]. */
227838fd1498Szrj if (cmp == -2 || cmp == 1)
227938fd1498Szrj {
228038fd1498Szrj wide_int sign_bit
228138fd1498Szrj = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
228238fd1498Szrj TYPE_PRECISION (expr_type));
228338fd1498Szrj if (!TYPE_UNSIGNED (expr_type)
228438fd1498Szrj && ((int_cst_range0
228538fd1498Szrj && value_range_constant_singleton (&vr0)
228638fd1498Szrj && !wi::cmps (wi::to_wide (vr0.min), sign_bit))
228738fd1498Szrj || (int_cst_range1
228838fd1498Szrj && value_range_constant_singleton (&vr1)
228938fd1498Szrj && !wi::cmps (wi::to_wide (vr1.min), sign_bit))))
229038fd1498Szrj {
229138fd1498Szrj min = TYPE_MIN_VALUE (expr_type);
229238fd1498Szrj max = build_int_cst (expr_type, 0);
229338fd1498Szrj }
229438fd1498Szrj }
229538fd1498Szrj }
229638fd1498Szrj else if (code == BIT_IOR_EXPR)
229738fd1498Szrj {
229838fd1498Szrj max = wide_int_to_tree (expr_type,
229938fd1498Szrj may_be_nonzero0 | may_be_nonzero1);
230038fd1498Szrj wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
230138fd1498Szrj /* If the input ranges contain only positive values we can
230238fd1498Szrj truncate the minimum of the result range to the maximum
230338fd1498Szrj of the input range minima. */
230438fd1498Szrj if (int_cst_range0 && int_cst_range1
230538fd1498Szrj && tree_int_cst_sgn (vr0.min) >= 0
230638fd1498Szrj && tree_int_cst_sgn (vr1.min) >= 0)
230738fd1498Szrj {
230838fd1498Szrj wmin = wi::max (wmin, wi::to_wide (vr0.min),
230938fd1498Szrj TYPE_SIGN (expr_type));
231038fd1498Szrj wmin = wi::max (wmin, wi::to_wide (vr1.min),
231138fd1498Szrj TYPE_SIGN (expr_type));
231238fd1498Szrj }
231338fd1498Szrj /* If either input range contains only negative values
231438fd1498Szrj we can truncate the minimum of the result range to the
231538fd1498Szrj respective minimum range. */
231638fd1498Szrj if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
231738fd1498Szrj wmin = wi::max (wmin, wi::to_wide (vr0.min),
231838fd1498Szrj TYPE_SIGN (expr_type));
231938fd1498Szrj if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
232038fd1498Szrj wmin = wi::max (wmin, wi::to_wide (vr1.min),
232138fd1498Szrj TYPE_SIGN (expr_type));
232238fd1498Szrj min = wide_int_to_tree (expr_type, wmin);
232338fd1498Szrj }
232438fd1498Szrj else if (code == BIT_XOR_EXPR)
232538fd1498Szrj {
232638fd1498Szrj wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
232738fd1498Szrj | ~(may_be_nonzero0 | may_be_nonzero1));
232838fd1498Szrj wide_int result_one_bits
232938fd1498Szrj = (wi::bit_and_not (must_be_nonzero0, may_be_nonzero1)
233038fd1498Szrj | wi::bit_and_not (must_be_nonzero1, may_be_nonzero0));
233138fd1498Szrj max = wide_int_to_tree (expr_type, ~result_zero_bits);
233238fd1498Szrj min = wide_int_to_tree (expr_type, result_one_bits);
233338fd1498Szrj /* If the range has all positive or all negative values the
233438fd1498Szrj result is better than VARYING. */
233538fd1498Szrj if (tree_int_cst_sgn (min) < 0
233638fd1498Szrj || tree_int_cst_sgn (max) >= 0)
233738fd1498Szrj ;
233838fd1498Szrj else
233938fd1498Szrj max = min = NULL_TREE;
234038fd1498Szrj }
234138fd1498Szrj }
234238fd1498Szrj else
234338fd1498Szrj gcc_unreachable ();
234438fd1498Szrj
234538fd1498Szrj /* If either MIN or MAX overflowed, then set the resulting range to
234638fd1498Szrj VARYING. */
234738fd1498Szrj if (min == NULL_TREE
234838fd1498Szrj || TREE_OVERFLOW_P (min)
234938fd1498Szrj || max == NULL_TREE
235038fd1498Szrj || TREE_OVERFLOW_P (max))
235138fd1498Szrj {
235238fd1498Szrj set_value_range_to_varying (vr);
235338fd1498Szrj return;
235438fd1498Szrj }
235538fd1498Szrj
235638fd1498Szrj /* We punt for [-INF, +INF].
235738fd1498Szrj We learn nothing when we have INF on both sides.
235838fd1498Szrj Note that we do accept [-INF, -INF] and [+INF, +INF]. */
235938fd1498Szrj if (vrp_val_is_min (min) && vrp_val_is_max (max))
236038fd1498Szrj {
236138fd1498Szrj set_value_range_to_varying (vr);
236238fd1498Szrj return;
236338fd1498Szrj }
236438fd1498Szrj
236538fd1498Szrj cmp = compare_values (min, max);
236638fd1498Szrj if (cmp == -2 || cmp == 1)
236738fd1498Szrj {
236838fd1498Szrj /* If the new range has its limits swapped around (MIN > MAX),
236938fd1498Szrj then the operation caused one of them to wrap around, mark
237038fd1498Szrj the new range VARYING. */
237138fd1498Szrj set_value_range_to_varying (vr);
237238fd1498Szrj }
237338fd1498Szrj else
237438fd1498Szrj set_value_range (vr, type, min, max, NULL);
237538fd1498Szrj }
237638fd1498Szrj
237738fd1498Szrj /* Extract range information from a unary operation CODE based on
237838fd1498Szrj the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
237938fd1498Szrj The resulting range is stored in *VR. */
238038fd1498Szrj
238138fd1498Szrj void
extract_range_from_unary_expr(value_range * vr,enum tree_code code,tree type,value_range * vr0_,tree op0_type)238238fd1498Szrj extract_range_from_unary_expr (value_range *vr,
238338fd1498Szrj enum tree_code code, tree type,
238438fd1498Szrj value_range *vr0_, tree op0_type)
238538fd1498Szrj {
238638fd1498Szrj value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
238738fd1498Szrj
238838fd1498Szrj /* VRP only operates on integral and pointer types. */
238938fd1498Szrj if (!(INTEGRAL_TYPE_P (op0_type)
239038fd1498Szrj || POINTER_TYPE_P (op0_type))
239138fd1498Szrj || !(INTEGRAL_TYPE_P (type)
239238fd1498Szrj || POINTER_TYPE_P (type)))
239338fd1498Szrj {
239438fd1498Szrj set_value_range_to_varying (vr);
239538fd1498Szrj return;
239638fd1498Szrj }
239738fd1498Szrj
239838fd1498Szrj /* If VR0 is UNDEFINED, so is the result. */
239938fd1498Szrj if (vr0.type == VR_UNDEFINED)
240038fd1498Szrj {
240138fd1498Szrj set_value_range_to_undefined (vr);
240238fd1498Szrj return;
240338fd1498Szrj }
240438fd1498Szrj
240538fd1498Szrj /* Handle operations that we express in terms of others. */
240638fd1498Szrj if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
240738fd1498Szrj {
240838fd1498Szrj /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
240938fd1498Szrj copy_value_range (vr, &vr0);
241038fd1498Szrj return;
241138fd1498Szrj }
241238fd1498Szrj else if (code == NEGATE_EXPR)
241338fd1498Szrj {
241438fd1498Szrj /* -X is simply 0 - X, so re-use existing code that also handles
241538fd1498Szrj anti-ranges fine. */
241638fd1498Szrj value_range zero = VR_INITIALIZER;
241738fd1498Szrj set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
241838fd1498Szrj extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
241938fd1498Szrj return;
242038fd1498Szrj }
242138fd1498Szrj else if (code == BIT_NOT_EXPR)
242238fd1498Szrj {
242338fd1498Szrj /* ~X is simply -1 - X, so re-use existing code that also handles
242438fd1498Szrj anti-ranges fine. */
242538fd1498Szrj value_range minusone = VR_INITIALIZER;
242638fd1498Szrj set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
242738fd1498Szrj extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
242838fd1498Szrj type, &minusone, &vr0);
242938fd1498Szrj return;
243038fd1498Szrj }
243138fd1498Szrj
243238fd1498Szrj /* Now canonicalize anti-ranges to ranges when they are not symbolic
243338fd1498Szrj and express op ~[] as (op []') U (op []''). */
243438fd1498Szrj if (vr0.type == VR_ANTI_RANGE
243538fd1498Szrj && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
243638fd1498Szrj {
243738fd1498Szrj extract_range_from_unary_expr (vr, code, type, &vrtem0, op0_type);
243838fd1498Szrj if (vrtem1.type != VR_UNDEFINED)
243938fd1498Szrj {
244038fd1498Szrj value_range vrres = VR_INITIALIZER;
244138fd1498Szrj extract_range_from_unary_expr (&vrres, code, type,
244238fd1498Szrj &vrtem1, op0_type);
244338fd1498Szrj vrp_meet (vr, &vrres);
244438fd1498Szrj }
244538fd1498Szrj return;
244638fd1498Szrj }
244738fd1498Szrj
244838fd1498Szrj if (CONVERT_EXPR_CODE_P (code))
244938fd1498Szrj {
245038fd1498Szrj tree inner_type = op0_type;
245138fd1498Szrj tree outer_type = type;
245238fd1498Szrj
245338fd1498Szrj /* If the expression evaluates to a pointer, we are only interested in
245438fd1498Szrj determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
245538fd1498Szrj if (POINTER_TYPE_P (type))
245638fd1498Szrj {
245738fd1498Szrj if (range_is_nonnull (&vr0))
245838fd1498Szrj set_value_range_to_nonnull (vr, type);
245938fd1498Szrj else if (range_is_null (&vr0))
246038fd1498Szrj set_value_range_to_null (vr, type);
246138fd1498Szrj else
246238fd1498Szrj set_value_range_to_varying (vr);
246338fd1498Szrj return;
246438fd1498Szrj }
246538fd1498Szrj
246638fd1498Szrj /* If VR0 is varying and we increase the type precision, assume
246738fd1498Szrj a full range for the following transformation. */
246838fd1498Szrj if (vr0.type == VR_VARYING
246938fd1498Szrj && INTEGRAL_TYPE_P (inner_type)
247038fd1498Szrj && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
247138fd1498Szrj {
247238fd1498Szrj vr0.type = VR_RANGE;
247338fd1498Szrj vr0.min = TYPE_MIN_VALUE (inner_type);
247438fd1498Szrj vr0.max = TYPE_MAX_VALUE (inner_type);
247538fd1498Szrj }
247638fd1498Szrj
247738fd1498Szrj /* If VR0 is a constant range or anti-range and the conversion is
247838fd1498Szrj not truncating we can convert the min and max values and
247938fd1498Szrj canonicalize the resulting range. Otherwise we can do the
248038fd1498Szrj conversion if the size of the range is less than what the
248138fd1498Szrj precision of the target type can represent and the range is
248238fd1498Szrj not an anti-range. */
248338fd1498Szrj if ((vr0.type == VR_RANGE
248438fd1498Szrj || vr0.type == VR_ANTI_RANGE)
248538fd1498Szrj && TREE_CODE (vr0.min) == INTEGER_CST
248638fd1498Szrj && TREE_CODE (vr0.max) == INTEGER_CST
248738fd1498Szrj && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
248838fd1498Szrj || (vr0.type == VR_RANGE
248938fd1498Szrj && integer_zerop (int_const_binop (RSHIFT_EXPR,
249038fd1498Szrj int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
249138fd1498Szrj size_int (TYPE_PRECISION (outer_type)))))))
249238fd1498Szrj {
249338fd1498Szrj tree new_min, new_max;
249438fd1498Szrj new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
249538fd1498Szrj 0, false);
249638fd1498Szrj new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
249738fd1498Szrj 0, false);
249838fd1498Szrj set_and_canonicalize_value_range (vr, vr0.type,
249938fd1498Szrj new_min, new_max, NULL);
250038fd1498Szrj return;
250138fd1498Szrj }
250238fd1498Szrj
250338fd1498Szrj set_value_range_to_varying (vr);
250438fd1498Szrj return;
250538fd1498Szrj }
250638fd1498Szrj else if (code == ABS_EXPR)
250738fd1498Szrj {
250838fd1498Szrj tree min, max;
250938fd1498Szrj int cmp;
251038fd1498Szrj
251138fd1498Szrj /* Pass through vr0 in the easy cases. */
251238fd1498Szrj if (TYPE_UNSIGNED (type)
251338fd1498Szrj || value_range_nonnegative_p (&vr0))
251438fd1498Szrj {
251538fd1498Szrj copy_value_range (vr, &vr0);
251638fd1498Szrj return;
251738fd1498Szrj }
251838fd1498Szrj
251938fd1498Szrj /* For the remaining varying or symbolic ranges we can't do anything
252038fd1498Szrj useful. */
252138fd1498Szrj if (vr0.type == VR_VARYING
252238fd1498Szrj || symbolic_range_p (&vr0))
252338fd1498Szrj {
252438fd1498Szrj set_value_range_to_varying (vr);
252538fd1498Szrj return;
252638fd1498Szrj }
252738fd1498Szrj
252838fd1498Szrj /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
252938fd1498Szrj useful range. */
253038fd1498Szrj if (!TYPE_OVERFLOW_UNDEFINED (type)
253138fd1498Szrj && ((vr0.type == VR_RANGE
253238fd1498Szrj && vrp_val_is_min (vr0.min))
253338fd1498Szrj || (vr0.type == VR_ANTI_RANGE
253438fd1498Szrj && !vrp_val_is_min (vr0.min))))
253538fd1498Szrj {
253638fd1498Szrj set_value_range_to_varying (vr);
253738fd1498Szrj return;
253838fd1498Szrj }
253938fd1498Szrj
254038fd1498Szrj /* ABS_EXPR may flip the range around, if the original range
254138fd1498Szrj included negative values. */
254238fd1498Szrj if (!vrp_val_is_min (vr0.min))
254338fd1498Szrj min = fold_unary_to_constant (code, type, vr0.min);
254438fd1498Szrj else
254538fd1498Szrj min = TYPE_MAX_VALUE (type);
254638fd1498Szrj
254738fd1498Szrj if (!vrp_val_is_min (vr0.max))
254838fd1498Szrj max = fold_unary_to_constant (code, type, vr0.max);
254938fd1498Szrj else
255038fd1498Szrj max = TYPE_MAX_VALUE (type);
255138fd1498Szrj
255238fd1498Szrj cmp = compare_values (min, max);
255338fd1498Szrj
255438fd1498Szrj /* If a VR_ANTI_RANGEs contains zero, then we have
255538fd1498Szrj ~[-INF, min(MIN, MAX)]. */
255638fd1498Szrj if (vr0.type == VR_ANTI_RANGE)
255738fd1498Szrj {
255838fd1498Szrj if (range_includes_zero_p (vr0.min, vr0.max) == 1)
255938fd1498Szrj {
256038fd1498Szrj /* Take the lower of the two values. */
256138fd1498Szrj if (cmp != 1)
256238fd1498Szrj max = min;
256338fd1498Szrj
256438fd1498Szrj /* Create ~[-INF, min (abs(MIN), abs(MAX))]
256538fd1498Szrj or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
256638fd1498Szrj flag_wrapv is set and the original anti-range doesn't include
256738fd1498Szrj TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
256838fd1498Szrj if (TYPE_OVERFLOW_WRAPS (type))
256938fd1498Szrj {
257038fd1498Szrj tree type_min_value = TYPE_MIN_VALUE (type);
257138fd1498Szrj
257238fd1498Szrj min = (vr0.min != type_min_value
257338fd1498Szrj ? int_const_binop (PLUS_EXPR, type_min_value,
257438fd1498Szrj build_int_cst (TREE_TYPE (type_min_value), 1))
257538fd1498Szrj : type_min_value);
257638fd1498Szrj }
257738fd1498Szrj else
257838fd1498Szrj min = TYPE_MIN_VALUE (type);
257938fd1498Szrj }
258038fd1498Szrj else
258138fd1498Szrj {
258238fd1498Szrj /* All else has failed, so create the range [0, INF], even for
258338fd1498Szrj flag_wrapv since TYPE_MIN_VALUE is in the original
258438fd1498Szrj anti-range. */
258538fd1498Szrj vr0.type = VR_RANGE;
258638fd1498Szrj min = build_int_cst (type, 0);
258738fd1498Szrj max = TYPE_MAX_VALUE (type);
258838fd1498Szrj }
258938fd1498Szrj }
259038fd1498Szrj
259138fd1498Szrj /* If the range contains zero then we know that the minimum value in the
259238fd1498Szrj range will be zero. */
259338fd1498Szrj else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
259438fd1498Szrj {
259538fd1498Szrj if (cmp == 1)
259638fd1498Szrj max = min;
259738fd1498Szrj min = build_int_cst (type, 0);
259838fd1498Szrj }
259938fd1498Szrj else
260038fd1498Szrj {
260138fd1498Szrj /* If the range was reversed, swap MIN and MAX. */
260238fd1498Szrj if (cmp == 1)
260338fd1498Szrj std::swap (min, max);
260438fd1498Szrj }
260538fd1498Szrj
260638fd1498Szrj cmp = compare_values (min, max);
260738fd1498Szrj if (cmp == -2 || cmp == 1)
260838fd1498Szrj {
260938fd1498Szrj /* If the new range has its limits swapped around (MIN > MAX),
261038fd1498Szrj then the operation caused one of them to wrap around, mark
261138fd1498Szrj the new range VARYING. */
261238fd1498Szrj set_value_range_to_varying (vr);
261338fd1498Szrj }
261438fd1498Szrj else
261538fd1498Szrj set_value_range (vr, vr0.type, min, max, NULL);
261638fd1498Szrj return;
261738fd1498Szrj }
261838fd1498Szrj
261938fd1498Szrj /* For unhandled operations fall back to varying. */
262038fd1498Szrj set_value_range_to_varying (vr);
262138fd1498Szrj return;
262238fd1498Szrj }
262338fd1498Szrj
262438fd1498Szrj /* Debugging dumps. */
262538fd1498Szrj
262638fd1498Szrj void dump_value_range (FILE *, const value_range *);
262738fd1498Szrj void debug_value_range (value_range *);
262838fd1498Szrj void dump_all_value_ranges (FILE *);
262938fd1498Szrj void dump_vr_equiv (FILE *, bitmap);
263038fd1498Szrj void debug_vr_equiv (bitmap);
263138fd1498Szrj
263238fd1498Szrj
263338fd1498Szrj /* Dump value range VR to FILE. */
263438fd1498Szrj
263538fd1498Szrj void
dump_value_range(FILE * file,const value_range * vr)263638fd1498Szrj dump_value_range (FILE *file, const value_range *vr)
263738fd1498Szrj {
263838fd1498Szrj if (vr == NULL)
263938fd1498Szrj fprintf (file, "[]");
264038fd1498Szrj else if (vr->type == VR_UNDEFINED)
264138fd1498Szrj fprintf (file, "UNDEFINED");
264238fd1498Szrj else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
264338fd1498Szrj {
264438fd1498Szrj tree type = TREE_TYPE (vr->min);
264538fd1498Szrj
264638fd1498Szrj fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
264738fd1498Szrj
264838fd1498Szrj if (INTEGRAL_TYPE_P (type)
264938fd1498Szrj && !TYPE_UNSIGNED (type)
265038fd1498Szrj && vrp_val_is_min (vr->min))
265138fd1498Szrj fprintf (file, "-INF");
265238fd1498Szrj else
265338fd1498Szrj print_generic_expr (file, vr->min);
265438fd1498Szrj
265538fd1498Szrj fprintf (file, ", ");
265638fd1498Szrj
265738fd1498Szrj if (INTEGRAL_TYPE_P (type)
265838fd1498Szrj && vrp_val_is_max (vr->max))
265938fd1498Szrj fprintf (file, "+INF");
266038fd1498Szrj else
266138fd1498Szrj print_generic_expr (file, vr->max);
266238fd1498Szrj
266338fd1498Szrj fprintf (file, "]");
266438fd1498Szrj
266538fd1498Szrj if (vr->equiv)
266638fd1498Szrj {
266738fd1498Szrj bitmap_iterator bi;
266838fd1498Szrj unsigned i, c = 0;
266938fd1498Szrj
267038fd1498Szrj fprintf (file, " EQUIVALENCES: { ");
267138fd1498Szrj
267238fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
267338fd1498Szrj {
267438fd1498Szrj print_generic_expr (file, ssa_name (i));
267538fd1498Szrj fprintf (file, " ");
267638fd1498Szrj c++;
267738fd1498Szrj }
267838fd1498Szrj
267938fd1498Szrj fprintf (file, "} (%u elements)", c);
268038fd1498Szrj }
268138fd1498Szrj }
268238fd1498Szrj else if (vr->type == VR_VARYING)
268338fd1498Szrj fprintf (file, "VARYING");
268438fd1498Szrj else
268538fd1498Szrj fprintf (file, "INVALID RANGE");
268638fd1498Szrj }
268738fd1498Szrj
268838fd1498Szrj
268938fd1498Szrj /* Dump value range VR to stderr. */
269038fd1498Szrj
269138fd1498Szrj DEBUG_FUNCTION void
debug_value_range(value_range * vr)269238fd1498Szrj debug_value_range (value_range *vr)
269338fd1498Szrj {
269438fd1498Szrj dump_value_range (stderr, vr);
269538fd1498Szrj fprintf (stderr, "\n");
269638fd1498Szrj }
269738fd1498Szrj
269838fd1498Szrj
269938fd1498Szrj /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
270038fd1498Szrj create a new SSA name N and return the assertion assignment
270138fd1498Szrj 'N = ASSERT_EXPR <V, V OP W>'. */
270238fd1498Szrj
270338fd1498Szrj static gimple *
build_assert_expr_for(tree cond,tree v)270438fd1498Szrj build_assert_expr_for (tree cond, tree v)
270538fd1498Szrj {
270638fd1498Szrj tree a;
270738fd1498Szrj gassign *assertion;
270838fd1498Szrj
270938fd1498Szrj gcc_assert (TREE_CODE (v) == SSA_NAME
271038fd1498Szrj && COMPARISON_CLASS_P (cond));
271138fd1498Szrj
271238fd1498Szrj a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
271338fd1498Szrj assertion = gimple_build_assign (NULL_TREE, a);
271438fd1498Szrj
271538fd1498Szrj /* The new ASSERT_EXPR, creates a new SSA name that replaces the
271638fd1498Szrj operand of the ASSERT_EXPR. Create it so the new name and the old one
271738fd1498Szrj are registered in the replacement table so that we can fix the SSA web
271838fd1498Szrj after adding all the ASSERT_EXPRs. */
271938fd1498Szrj tree new_def = create_new_def_for (v, assertion, NULL);
272038fd1498Szrj /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
272138fd1498Szrj given we have to be able to fully propagate those out to re-create
272238fd1498Szrj valid SSA when removing the asserts. */
272338fd1498Szrj if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v))
272438fd1498Szrj SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def) = 1;
272538fd1498Szrj
272638fd1498Szrj return assertion;
272738fd1498Szrj }
272838fd1498Szrj
272938fd1498Szrj
273038fd1498Szrj /* Return false if EXPR is a predicate expression involving floating
273138fd1498Szrj point values. */
273238fd1498Szrj
273338fd1498Szrj static inline bool
fp_predicate(gimple * stmt)273438fd1498Szrj fp_predicate (gimple *stmt)
273538fd1498Szrj {
273638fd1498Szrj GIMPLE_CHECK (stmt, GIMPLE_COND);
273738fd1498Szrj
273838fd1498Szrj return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
273938fd1498Szrj }
274038fd1498Szrj
274138fd1498Szrj /* If the range of values taken by OP can be inferred after STMT executes,
274238fd1498Szrj return the comparison code (COMP_CODE_P) and value (VAL_P) that
274338fd1498Szrj describes the inferred range. Return true if a range could be
274438fd1498Szrj inferred. */
274538fd1498Szrj
274638fd1498Szrj bool
infer_value_range(gimple * stmt,tree op,tree_code * comp_code_p,tree * val_p)274738fd1498Szrj infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
274838fd1498Szrj {
274938fd1498Szrj *val_p = NULL_TREE;
275038fd1498Szrj *comp_code_p = ERROR_MARK;
275138fd1498Szrj
275238fd1498Szrj /* Do not attempt to infer anything in names that flow through
275338fd1498Szrj abnormal edges. */
275438fd1498Szrj if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
275538fd1498Szrj return false;
275638fd1498Szrj
275738fd1498Szrj /* If STMT is the last statement of a basic block with no normal
275838fd1498Szrj successors, there is no point inferring anything about any of its
275938fd1498Szrj operands. We would not be able to find a proper insertion point
276038fd1498Szrj for the assertion, anyway. */
276138fd1498Szrj if (stmt_ends_bb_p (stmt))
276238fd1498Szrj {
276338fd1498Szrj edge_iterator ei;
276438fd1498Szrj edge e;
276538fd1498Szrj
276638fd1498Szrj FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
276738fd1498Szrj if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
276838fd1498Szrj break;
276938fd1498Szrj if (e == NULL)
277038fd1498Szrj return false;
277138fd1498Szrj }
277238fd1498Szrj
277338fd1498Szrj if (infer_nonnull_range (stmt, op))
277438fd1498Szrj {
277538fd1498Szrj *val_p = build_int_cst (TREE_TYPE (op), 0);
277638fd1498Szrj *comp_code_p = NE_EXPR;
277738fd1498Szrj return true;
277838fd1498Szrj }
277938fd1498Szrj
278038fd1498Szrj return false;
278138fd1498Szrj }
278238fd1498Szrj
278338fd1498Szrj
278438fd1498Szrj void dump_asserts_for (FILE *, tree);
278538fd1498Szrj void debug_asserts_for (tree);
278638fd1498Szrj void dump_all_asserts (FILE *);
278738fd1498Szrj void debug_all_asserts (void);
278838fd1498Szrj
278938fd1498Szrj /* Dump all the registered assertions for NAME to FILE. */
279038fd1498Szrj
279138fd1498Szrj void
dump_asserts_for(FILE * file,tree name)279238fd1498Szrj dump_asserts_for (FILE *file, tree name)
279338fd1498Szrj {
279438fd1498Szrj assert_locus *loc;
279538fd1498Szrj
279638fd1498Szrj fprintf (file, "Assertions to be inserted for ");
279738fd1498Szrj print_generic_expr (file, name);
279838fd1498Szrj fprintf (file, "\n");
279938fd1498Szrj
280038fd1498Szrj loc = asserts_for[SSA_NAME_VERSION (name)];
280138fd1498Szrj while (loc)
280238fd1498Szrj {
280338fd1498Szrj fprintf (file, "\t");
280438fd1498Szrj print_gimple_stmt (file, gsi_stmt (loc->si), 0);
280538fd1498Szrj fprintf (file, "\n\tBB #%d", loc->bb->index);
280638fd1498Szrj if (loc->e)
280738fd1498Szrj {
280838fd1498Szrj fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
280938fd1498Szrj loc->e->dest->index);
281038fd1498Szrj dump_edge_info (file, loc->e, dump_flags, 0);
281138fd1498Szrj }
281238fd1498Szrj fprintf (file, "\n\tPREDICATE: ");
281338fd1498Szrj print_generic_expr (file, loc->expr);
281438fd1498Szrj fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
281538fd1498Szrj print_generic_expr (file, loc->val);
281638fd1498Szrj fprintf (file, "\n\n");
281738fd1498Szrj loc = loc->next;
281838fd1498Szrj }
281938fd1498Szrj
282038fd1498Szrj fprintf (file, "\n");
282138fd1498Szrj }
282238fd1498Szrj
282338fd1498Szrj
282438fd1498Szrj /* Dump all the registered assertions for NAME to stderr. */
282538fd1498Szrj
282638fd1498Szrj DEBUG_FUNCTION void
debug_asserts_for(tree name)282738fd1498Szrj debug_asserts_for (tree name)
282838fd1498Szrj {
282938fd1498Szrj dump_asserts_for (stderr, name);
283038fd1498Szrj }
283138fd1498Szrj
283238fd1498Szrj
283338fd1498Szrj /* Dump all the registered assertions for all the names to FILE. */
283438fd1498Szrj
283538fd1498Szrj void
dump_all_asserts(FILE * file)283638fd1498Szrj dump_all_asserts (FILE *file)
283738fd1498Szrj {
283838fd1498Szrj unsigned i;
283938fd1498Szrj bitmap_iterator bi;
284038fd1498Szrj
284138fd1498Szrj fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
284238fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
284338fd1498Szrj dump_asserts_for (file, ssa_name (i));
284438fd1498Szrj fprintf (file, "\n");
284538fd1498Szrj }
284638fd1498Szrj
284738fd1498Szrj
284838fd1498Szrj /* Dump all the registered assertions for all the names to stderr. */
284938fd1498Szrj
285038fd1498Szrj DEBUG_FUNCTION void
debug_all_asserts(void)285138fd1498Szrj debug_all_asserts (void)
285238fd1498Szrj {
285338fd1498Szrj dump_all_asserts (stderr);
285438fd1498Szrj }
285538fd1498Szrj
285638fd1498Szrj /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
285738fd1498Szrj
285838fd1498Szrj static void
add_assert_info(vec<assert_info> & asserts,tree name,tree expr,enum tree_code comp_code,tree val)285938fd1498Szrj add_assert_info (vec<assert_info> &asserts,
286038fd1498Szrj tree name, tree expr, enum tree_code comp_code, tree val)
286138fd1498Szrj {
286238fd1498Szrj assert_info info;
286338fd1498Szrj info.comp_code = comp_code;
286438fd1498Szrj info.name = name;
286538fd1498Szrj if (TREE_OVERFLOW_P (val))
286638fd1498Szrj val = drop_tree_overflow (val);
286738fd1498Szrj info.val = val;
286838fd1498Szrj info.expr = expr;
286938fd1498Szrj asserts.safe_push (info);
287038fd1498Szrj }
287138fd1498Szrj
287238fd1498Szrj /* If NAME doesn't have an ASSERT_EXPR registered for asserting
287338fd1498Szrj 'EXPR COMP_CODE VAL' at a location that dominates block BB or
287438fd1498Szrj E->DEST, then register this location as a possible insertion point
287538fd1498Szrj for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
287638fd1498Szrj
287738fd1498Szrj BB, E and SI provide the exact insertion point for the new
287838fd1498Szrj ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
287938fd1498Szrj on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
288038fd1498Szrj BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
288138fd1498Szrj must not be NULL. */
288238fd1498Szrj
288338fd1498Szrj static void
register_new_assert_for(tree name,tree expr,enum tree_code comp_code,tree val,basic_block bb,edge e,gimple_stmt_iterator si)288438fd1498Szrj register_new_assert_for (tree name, tree expr,
288538fd1498Szrj enum tree_code comp_code,
288638fd1498Szrj tree val,
288738fd1498Szrj basic_block bb,
288838fd1498Szrj edge e,
288938fd1498Szrj gimple_stmt_iterator si)
289038fd1498Szrj {
289138fd1498Szrj assert_locus *n, *loc, *last_loc;
289238fd1498Szrj basic_block dest_bb;
289338fd1498Szrj
289438fd1498Szrj gcc_checking_assert (bb == NULL || e == NULL);
289538fd1498Szrj
289638fd1498Szrj if (e == NULL)
289738fd1498Szrj gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
289838fd1498Szrj && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
289938fd1498Szrj
290038fd1498Szrj /* Never build an assert comparing against an integer constant with
290138fd1498Szrj TREE_OVERFLOW set. This confuses our undefined overflow warning
290238fd1498Szrj machinery. */
290338fd1498Szrj if (TREE_OVERFLOW_P (val))
290438fd1498Szrj val = drop_tree_overflow (val);
290538fd1498Szrj
290638fd1498Szrj /* The new assertion A will be inserted at BB or E. We need to
290738fd1498Szrj determine if the new location is dominated by a previously
290838fd1498Szrj registered location for A. If we are doing an edge insertion,
290938fd1498Szrj assume that A will be inserted at E->DEST. Note that this is not
291038fd1498Szrj necessarily true.
291138fd1498Szrj
291238fd1498Szrj If E is a critical edge, it will be split. But even if E is
291338fd1498Szrj split, the new block will dominate the same set of blocks that
291438fd1498Szrj E->DEST dominates.
291538fd1498Szrj
291638fd1498Szrj The reverse, however, is not true, blocks dominated by E->DEST
291738fd1498Szrj will not be dominated by the new block created to split E. So,
291838fd1498Szrj if the insertion location is on a critical edge, we will not use
291938fd1498Szrj the new location to move another assertion previously registered
292038fd1498Szrj at a block dominated by E->DEST. */
292138fd1498Szrj dest_bb = (bb) ? bb : e->dest;
292238fd1498Szrj
292338fd1498Szrj /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
292438fd1498Szrj VAL at a block dominating DEST_BB, then we don't need to insert a new
292538fd1498Szrj one. Similarly, if the same assertion already exists at a block
292638fd1498Szrj dominated by DEST_BB and the new location is not on a critical
292738fd1498Szrj edge, then update the existing location for the assertion (i.e.,
292838fd1498Szrj move the assertion up in the dominance tree).
292938fd1498Szrj
293038fd1498Szrj Note, this is implemented as a simple linked list because there
293138fd1498Szrj should not be more than a handful of assertions registered per
293238fd1498Szrj name. If this becomes a performance problem, a table hashed by
293338fd1498Szrj COMP_CODE and VAL could be implemented. */
293438fd1498Szrj loc = asserts_for[SSA_NAME_VERSION (name)];
293538fd1498Szrj last_loc = loc;
293638fd1498Szrj while (loc)
293738fd1498Szrj {
293838fd1498Szrj if (loc->comp_code == comp_code
293938fd1498Szrj && (loc->val == val
294038fd1498Szrj || operand_equal_p (loc->val, val, 0))
294138fd1498Szrj && (loc->expr == expr
294238fd1498Szrj || operand_equal_p (loc->expr, expr, 0)))
294338fd1498Szrj {
294438fd1498Szrj /* If E is not a critical edge and DEST_BB
294538fd1498Szrj dominates the existing location for the assertion, move
294638fd1498Szrj the assertion up in the dominance tree by updating its
294738fd1498Szrj location information. */
294838fd1498Szrj if ((e == NULL || !EDGE_CRITICAL_P (e))
294938fd1498Szrj && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
295038fd1498Szrj {
295138fd1498Szrj loc->bb = dest_bb;
295238fd1498Szrj loc->e = e;
295338fd1498Szrj loc->si = si;
295438fd1498Szrj return;
295538fd1498Szrj }
295638fd1498Szrj }
295738fd1498Szrj
295838fd1498Szrj /* Update the last node of the list and move to the next one. */
295938fd1498Szrj last_loc = loc;
296038fd1498Szrj loc = loc->next;
296138fd1498Szrj }
296238fd1498Szrj
296338fd1498Szrj /* If we didn't find an assertion already registered for
296438fd1498Szrj NAME COMP_CODE VAL, add a new one at the end of the list of
296538fd1498Szrj assertions associated with NAME. */
296638fd1498Szrj n = XNEW (struct assert_locus);
296738fd1498Szrj n->bb = dest_bb;
296838fd1498Szrj n->e = e;
296938fd1498Szrj n->si = si;
297038fd1498Szrj n->comp_code = comp_code;
297138fd1498Szrj n->val = val;
297238fd1498Szrj n->expr = expr;
297338fd1498Szrj n->next = NULL;
297438fd1498Szrj
297538fd1498Szrj if (last_loc)
297638fd1498Szrj last_loc->next = n;
297738fd1498Szrj else
297838fd1498Szrj asserts_for[SSA_NAME_VERSION (name)] = n;
297938fd1498Szrj
298038fd1498Szrj bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
298138fd1498Szrj }
298238fd1498Szrj
298338fd1498Szrj /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
298438fd1498Szrj Extract a suitable test code and value and store them into *CODE_P and
298538fd1498Szrj *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
298638fd1498Szrj
298738fd1498Szrj If no extraction was possible, return FALSE, otherwise return TRUE.
298838fd1498Szrj
298938fd1498Szrj If INVERT is true, then we invert the result stored into *CODE_P. */
299038fd1498Szrj
299138fd1498Szrj static bool
extract_code_and_val_from_cond_with_ops(tree name,enum tree_code cond_code,tree cond_op0,tree cond_op1,bool invert,enum tree_code * code_p,tree * val_p)299238fd1498Szrj extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
299338fd1498Szrj tree cond_op0, tree cond_op1,
299438fd1498Szrj bool invert, enum tree_code *code_p,
299538fd1498Szrj tree *val_p)
299638fd1498Szrj {
299738fd1498Szrj enum tree_code comp_code;
299838fd1498Szrj tree val;
299938fd1498Szrj
300038fd1498Szrj /* Otherwise, we have a comparison of the form NAME COMP VAL
300138fd1498Szrj or VAL COMP NAME. */
300238fd1498Szrj if (name == cond_op1)
300338fd1498Szrj {
300438fd1498Szrj /* If the predicate is of the form VAL COMP NAME, flip
300538fd1498Szrj COMP around because we need to register NAME as the
300638fd1498Szrj first operand in the predicate. */
300738fd1498Szrj comp_code = swap_tree_comparison (cond_code);
300838fd1498Szrj val = cond_op0;
300938fd1498Szrj }
301038fd1498Szrj else if (name == cond_op0)
301138fd1498Szrj {
301238fd1498Szrj /* The comparison is of the form NAME COMP VAL, so the
301338fd1498Szrj comparison code remains unchanged. */
301438fd1498Szrj comp_code = cond_code;
301538fd1498Szrj val = cond_op1;
301638fd1498Szrj }
301738fd1498Szrj else
301838fd1498Szrj gcc_unreachable ();
301938fd1498Szrj
302038fd1498Szrj /* Invert the comparison code as necessary. */
302138fd1498Szrj if (invert)
302238fd1498Szrj comp_code = invert_tree_comparison (comp_code, 0);
302338fd1498Szrj
302438fd1498Szrj /* VRP only handles integral and pointer types. */
302538fd1498Szrj if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
302638fd1498Szrj && ! POINTER_TYPE_P (TREE_TYPE (val)))
302738fd1498Szrj return false;
302838fd1498Szrj
302938fd1498Szrj /* Do not register always-false predicates.
303038fd1498Szrj FIXME: this works around a limitation in fold() when dealing with
303138fd1498Szrj enumerations. Given 'enum { N1, N2 } x;', fold will not
303238fd1498Szrj fold 'if (x > N2)' to 'if (0)'. */
303338fd1498Szrj if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
303438fd1498Szrj && INTEGRAL_TYPE_P (TREE_TYPE (val)))
303538fd1498Szrj {
303638fd1498Szrj tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
303738fd1498Szrj tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
303838fd1498Szrj
303938fd1498Szrj if (comp_code == GT_EXPR
304038fd1498Szrj && (!max
304138fd1498Szrj || compare_values (val, max) == 0))
304238fd1498Szrj return false;
304338fd1498Szrj
304438fd1498Szrj if (comp_code == LT_EXPR
304538fd1498Szrj && (!min
304638fd1498Szrj || compare_values (val, min) == 0))
304738fd1498Szrj return false;
304838fd1498Szrj }
304938fd1498Szrj *code_p = comp_code;
305038fd1498Szrj *val_p = val;
305138fd1498Szrj return true;
305238fd1498Szrj }
305338fd1498Szrj
305438fd1498Szrj /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
305538fd1498Szrj (otherwise return VAL). VAL and MASK must be zero-extended for
305638fd1498Szrj precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
305738fd1498Szrj (to transform signed values into unsigned) and at the end xor
305838fd1498Szrj SGNBIT back. */
305938fd1498Szrj
306038fd1498Szrj static wide_int
masked_increment(const wide_int & val_in,const wide_int & mask,const wide_int & sgnbit,unsigned int prec)306138fd1498Szrj masked_increment (const wide_int &val_in, const wide_int &mask,
306238fd1498Szrj const wide_int &sgnbit, unsigned int prec)
306338fd1498Szrj {
306438fd1498Szrj wide_int bit = wi::one (prec), res;
306538fd1498Szrj unsigned int i;
306638fd1498Szrj
306738fd1498Szrj wide_int val = val_in ^ sgnbit;
306838fd1498Szrj for (i = 0; i < prec; i++, bit += bit)
306938fd1498Szrj {
307038fd1498Szrj res = mask;
307138fd1498Szrj if ((res & bit) == 0)
307238fd1498Szrj continue;
307338fd1498Szrj res = bit - 1;
307438fd1498Szrj res = wi::bit_and_not (val + bit, res);
307538fd1498Szrj res &= mask;
307638fd1498Szrj if (wi::gtu_p (res, val))
307738fd1498Szrj return res ^ sgnbit;
307838fd1498Szrj }
307938fd1498Szrj return val ^ sgnbit;
308038fd1498Szrj }
308138fd1498Szrj
308238fd1498Szrj /* Helper for overflow_comparison_p
308338fd1498Szrj
308438fd1498Szrj OP0 CODE OP1 is a comparison. Examine the comparison and potentially
308538fd1498Szrj OP1's defining statement to see if it ultimately has the form
308638fd1498Szrj OP0 CODE (OP0 PLUS INTEGER_CST)
308738fd1498Szrj
308838fd1498Szrj If so, return TRUE indicating this is an overflow test and store into
308938fd1498Szrj *NEW_CST an updated constant that can be used in a narrowed range test.
309038fd1498Szrj
309138fd1498Szrj REVERSED indicates if the comparison was originally:
309238fd1498Szrj
309338fd1498Szrj OP1 CODE' OP0.
309438fd1498Szrj
309538fd1498Szrj This affects how we build the updated constant. */
309638fd1498Szrj
309738fd1498Szrj static bool
overflow_comparison_p_1(enum tree_code code,tree op0,tree op1,bool follow_assert_exprs,bool reversed,tree * new_cst)309838fd1498Szrj overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1,
309938fd1498Szrj bool follow_assert_exprs, bool reversed, tree *new_cst)
310038fd1498Szrj {
310138fd1498Szrj /* See if this is a relational operation between two SSA_NAMES with
310238fd1498Szrj unsigned, overflow wrapping values. If so, check it more deeply. */
310338fd1498Szrj if ((code == LT_EXPR || code == LE_EXPR
310438fd1498Szrj || code == GE_EXPR || code == GT_EXPR)
310538fd1498Szrj && TREE_CODE (op0) == SSA_NAME
310638fd1498Szrj && TREE_CODE (op1) == SSA_NAME
310738fd1498Szrj && INTEGRAL_TYPE_P (TREE_TYPE (op0))
310838fd1498Szrj && TYPE_UNSIGNED (TREE_TYPE (op0))
310938fd1498Szrj && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
311038fd1498Szrj {
311138fd1498Szrj gimple *op1_def = SSA_NAME_DEF_STMT (op1);
311238fd1498Szrj
311338fd1498Szrj /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
311438fd1498Szrj if (follow_assert_exprs)
311538fd1498Szrj {
311638fd1498Szrj while (gimple_assign_single_p (op1_def)
311738fd1498Szrj && TREE_CODE (gimple_assign_rhs1 (op1_def)) == ASSERT_EXPR)
311838fd1498Szrj {
311938fd1498Szrj op1 = TREE_OPERAND (gimple_assign_rhs1 (op1_def), 0);
312038fd1498Szrj if (TREE_CODE (op1) != SSA_NAME)
312138fd1498Szrj break;
312238fd1498Szrj op1_def = SSA_NAME_DEF_STMT (op1);
312338fd1498Szrj }
312438fd1498Szrj }
312538fd1498Szrj
312638fd1498Szrj /* Now look at the defining statement of OP1 to see if it adds
312738fd1498Szrj or subtracts a nonzero constant from another operand. */
312838fd1498Szrj if (op1_def
312938fd1498Szrj && is_gimple_assign (op1_def)
313038fd1498Szrj && gimple_assign_rhs_code (op1_def) == PLUS_EXPR
313138fd1498Szrj && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST
313238fd1498Szrj && !integer_zerop (gimple_assign_rhs2 (op1_def)))
313338fd1498Szrj {
313438fd1498Szrj tree target = gimple_assign_rhs1 (op1_def);
313538fd1498Szrj
313638fd1498Szrj /* If requested, follow ASSERT_EXPRs backwards for op0 looking
313738fd1498Szrj for one where TARGET appears on the RHS. */
313838fd1498Szrj if (follow_assert_exprs)
313938fd1498Szrj {
314038fd1498Szrj /* Now see if that "other operand" is op0, following the chain
314138fd1498Szrj of ASSERT_EXPRs if necessary. */
314238fd1498Szrj gimple *op0_def = SSA_NAME_DEF_STMT (op0);
314338fd1498Szrj while (op0 != target
314438fd1498Szrj && gimple_assign_single_p (op0_def)
314538fd1498Szrj && TREE_CODE (gimple_assign_rhs1 (op0_def)) == ASSERT_EXPR)
314638fd1498Szrj {
314738fd1498Szrj op0 = TREE_OPERAND (gimple_assign_rhs1 (op0_def), 0);
314838fd1498Szrj if (TREE_CODE (op0) != SSA_NAME)
314938fd1498Szrj break;
315038fd1498Szrj op0_def = SSA_NAME_DEF_STMT (op0);
315138fd1498Szrj }
315238fd1498Szrj }
315338fd1498Szrj
315438fd1498Szrj /* If we did not find our target SSA_NAME, then this is not
315538fd1498Szrj an overflow test. */
315638fd1498Szrj if (op0 != target)
315738fd1498Szrj return false;
315838fd1498Szrj
315938fd1498Szrj tree type = TREE_TYPE (op0);
316038fd1498Szrj wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
316138fd1498Szrj tree inc = gimple_assign_rhs2 (op1_def);
316238fd1498Szrj if (reversed)
316338fd1498Szrj *new_cst = wide_int_to_tree (type, max + wi::to_wide (inc));
316438fd1498Szrj else
316538fd1498Szrj *new_cst = wide_int_to_tree (type, max - wi::to_wide (inc));
316638fd1498Szrj return true;
316738fd1498Szrj }
316838fd1498Szrj }
316938fd1498Szrj return false;
317038fd1498Szrj }
317138fd1498Szrj
317238fd1498Szrj /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
317338fd1498Szrj OP1's defining statement to see if it ultimately has the form
317438fd1498Szrj OP0 CODE (OP0 PLUS INTEGER_CST)
317538fd1498Szrj
317638fd1498Szrj If so, return TRUE indicating this is an overflow test and store into
317738fd1498Szrj *NEW_CST an updated constant that can be used in a narrowed range test.
317838fd1498Szrj
317938fd1498Szrj These statements are left as-is in the IL to facilitate discovery of
318038fd1498Szrj {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
318138fd1498Szrj the alternate range representation is often useful within VRP. */
318238fd1498Szrj
318338fd1498Szrj bool
overflow_comparison_p(tree_code code,tree name,tree val,bool use_equiv_p,tree * new_cst)318438fd1498Szrj overflow_comparison_p (tree_code code, tree name, tree val,
318538fd1498Szrj bool use_equiv_p, tree *new_cst)
318638fd1498Szrj {
318738fd1498Szrj if (overflow_comparison_p_1 (code, name, val, use_equiv_p, false, new_cst))
318838fd1498Szrj return true;
318938fd1498Szrj return overflow_comparison_p_1 (swap_tree_comparison (code), val, name,
319038fd1498Szrj use_equiv_p, true, new_cst);
319138fd1498Szrj }
319238fd1498Szrj
319338fd1498Szrj
319438fd1498Szrj /* Try to register an edge assertion for SSA name NAME on edge E for
319538fd1498Szrj the condition COND contributing to the conditional jump pointed to by BSI.
319638fd1498Szrj Invert the condition COND if INVERT is true. */
319738fd1498Szrj
319838fd1498Szrj static void
register_edge_assert_for_2(tree name,edge e,enum tree_code cond_code,tree cond_op0,tree cond_op1,bool invert,vec<assert_info> & asserts)319938fd1498Szrj register_edge_assert_for_2 (tree name, edge e,
320038fd1498Szrj enum tree_code cond_code,
320138fd1498Szrj tree cond_op0, tree cond_op1, bool invert,
320238fd1498Szrj vec<assert_info> &asserts)
320338fd1498Szrj {
320438fd1498Szrj tree val;
320538fd1498Szrj enum tree_code comp_code;
320638fd1498Szrj
320738fd1498Szrj if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
320838fd1498Szrj cond_op0,
320938fd1498Szrj cond_op1,
321038fd1498Szrj invert, &comp_code, &val))
321138fd1498Szrj return;
321238fd1498Szrj
321338fd1498Szrj /* Queue the assert. */
321438fd1498Szrj tree x;
321538fd1498Szrj if (overflow_comparison_p (comp_code, name, val, false, &x))
321638fd1498Szrj {
321738fd1498Szrj enum tree_code new_code = ((comp_code == GT_EXPR || comp_code == GE_EXPR)
321838fd1498Szrj ? GT_EXPR : LE_EXPR);
321938fd1498Szrj add_assert_info (asserts, name, name, new_code, x);
322038fd1498Szrj }
322138fd1498Szrj add_assert_info (asserts, name, name, comp_code, val);
322238fd1498Szrj
322338fd1498Szrj /* In the case of NAME <= CST and NAME being defined as
322438fd1498Szrj NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
322538fd1498Szrj and NAME2 <= CST - CST2. We can do the same for NAME > CST.
322638fd1498Szrj This catches range and anti-range tests. */
322738fd1498Szrj if ((comp_code == LE_EXPR
322838fd1498Szrj || comp_code == GT_EXPR)
322938fd1498Szrj && TREE_CODE (val) == INTEGER_CST
323038fd1498Szrj && TYPE_UNSIGNED (TREE_TYPE (val)))
323138fd1498Szrj {
323238fd1498Szrj gimple *def_stmt = SSA_NAME_DEF_STMT (name);
323338fd1498Szrj tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
323438fd1498Szrj
323538fd1498Szrj /* Extract CST2 from the (optional) addition. */
323638fd1498Szrj if (is_gimple_assign (def_stmt)
323738fd1498Szrj && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
323838fd1498Szrj {
323938fd1498Szrj name2 = gimple_assign_rhs1 (def_stmt);
324038fd1498Szrj cst2 = gimple_assign_rhs2 (def_stmt);
324138fd1498Szrj if (TREE_CODE (name2) == SSA_NAME
324238fd1498Szrj && TREE_CODE (cst2) == INTEGER_CST)
324338fd1498Szrj def_stmt = SSA_NAME_DEF_STMT (name2);
324438fd1498Szrj }
324538fd1498Szrj
324638fd1498Szrj /* Extract NAME2 from the (optional) sign-changing cast. */
324738fd1498Szrj if (gimple_assign_cast_p (def_stmt))
324838fd1498Szrj {
324938fd1498Szrj if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
325038fd1498Szrj && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
325138fd1498Szrj && (TYPE_PRECISION (gimple_expr_type (def_stmt))
325238fd1498Szrj == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
325338fd1498Szrj name3 = gimple_assign_rhs1 (def_stmt);
325438fd1498Szrj }
325538fd1498Szrj
325638fd1498Szrj /* If name3 is used later, create an ASSERT_EXPR for it. */
325738fd1498Szrj if (name3 != NULL_TREE
325838fd1498Szrj && TREE_CODE (name3) == SSA_NAME
325938fd1498Szrj && (cst2 == NULL_TREE
326038fd1498Szrj || TREE_CODE (cst2) == INTEGER_CST)
326138fd1498Szrj && INTEGRAL_TYPE_P (TREE_TYPE (name3)))
326238fd1498Szrj {
326338fd1498Szrj tree tmp;
326438fd1498Szrj
326538fd1498Szrj /* Build an expression for the range test. */
326638fd1498Szrj tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
326738fd1498Szrj if (cst2 != NULL_TREE)
326838fd1498Szrj tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
326938fd1498Szrj
327038fd1498Szrj if (dump_file)
327138fd1498Szrj {
327238fd1498Szrj fprintf (dump_file, "Adding assert for ");
327338fd1498Szrj print_generic_expr (dump_file, name3);
327438fd1498Szrj fprintf (dump_file, " from ");
327538fd1498Szrj print_generic_expr (dump_file, tmp);
327638fd1498Szrj fprintf (dump_file, "\n");
327738fd1498Szrj }
327838fd1498Szrj
327938fd1498Szrj add_assert_info (asserts, name3, tmp, comp_code, val);
328038fd1498Szrj }
328138fd1498Szrj
328238fd1498Szrj /* If name2 is used later, create an ASSERT_EXPR for it. */
328338fd1498Szrj if (name2 != NULL_TREE
328438fd1498Szrj && TREE_CODE (name2) == SSA_NAME
328538fd1498Szrj && TREE_CODE (cst2) == INTEGER_CST
328638fd1498Szrj && INTEGRAL_TYPE_P (TREE_TYPE (name2)))
328738fd1498Szrj {
328838fd1498Szrj tree tmp;
328938fd1498Szrj
329038fd1498Szrj /* Build an expression for the range test. */
329138fd1498Szrj tmp = name2;
329238fd1498Szrj if (TREE_TYPE (name) != TREE_TYPE (name2))
329338fd1498Szrj tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
329438fd1498Szrj if (cst2 != NULL_TREE)
329538fd1498Szrj tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
329638fd1498Szrj
329738fd1498Szrj if (dump_file)
329838fd1498Szrj {
329938fd1498Szrj fprintf (dump_file, "Adding assert for ");
330038fd1498Szrj print_generic_expr (dump_file, name2);
330138fd1498Szrj fprintf (dump_file, " from ");
330238fd1498Szrj print_generic_expr (dump_file, tmp);
330338fd1498Szrj fprintf (dump_file, "\n");
330438fd1498Szrj }
330538fd1498Szrj
330638fd1498Szrj add_assert_info (asserts, name2, tmp, comp_code, val);
330738fd1498Szrj }
330838fd1498Szrj }
330938fd1498Szrj
331038fd1498Szrj /* In the case of post-in/decrement tests like if (i++) ... and uses
331138fd1498Szrj of the in/decremented value on the edge the extra name we want to
331238fd1498Szrj assert for is not on the def chain of the name compared. Instead
331338fd1498Szrj it is in the set of use stmts.
331438fd1498Szrj Similar cases happen for conversions that were simplified through
331538fd1498Szrj fold_{sign_changed,widened}_comparison. */
331638fd1498Szrj if ((comp_code == NE_EXPR
331738fd1498Szrj || comp_code == EQ_EXPR)
331838fd1498Szrj && TREE_CODE (val) == INTEGER_CST)
331938fd1498Szrj {
332038fd1498Szrj imm_use_iterator ui;
332138fd1498Szrj gimple *use_stmt;
332238fd1498Szrj FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
332338fd1498Szrj {
332438fd1498Szrj if (!is_gimple_assign (use_stmt))
332538fd1498Szrj continue;
332638fd1498Szrj
332738fd1498Szrj /* Cut off to use-stmts that are dominating the predecessor. */
332838fd1498Szrj if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
332938fd1498Szrj continue;
333038fd1498Szrj
333138fd1498Szrj tree name2 = gimple_assign_lhs (use_stmt);
333238fd1498Szrj if (TREE_CODE (name2) != SSA_NAME)
333338fd1498Szrj continue;
333438fd1498Szrj
333538fd1498Szrj enum tree_code code = gimple_assign_rhs_code (use_stmt);
333638fd1498Szrj tree cst;
333738fd1498Szrj if (code == PLUS_EXPR
333838fd1498Szrj || code == MINUS_EXPR)
333938fd1498Szrj {
334038fd1498Szrj cst = gimple_assign_rhs2 (use_stmt);
334138fd1498Szrj if (TREE_CODE (cst) != INTEGER_CST)
334238fd1498Szrj continue;
334338fd1498Szrj cst = int_const_binop (code, val, cst);
334438fd1498Szrj }
334538fd1498Szrj else if (CONVERT_EXPR_CODE_P (code))
334638fd1498Szrj {
334738fd1498Szrj /* For truncating conversions we cannot record
334838fd1498Szrj an inequality. */
334938fd1498Szrj if (comp_code == NE_EXPR
335038fd1498Szrj && (TYPE_PRECISION (TREE_TYPE (name2))
335138fd1498Szrj < TYPE_PRECISION (TREE_TYPE (name))))
335238fd1498Szrj continue;
335338fd1498Szrj cst = fold_convert (TREE_TYPE (name2), val);
335438fd1498Szrj }
335538fd1498Szrj else
335638fd1498Szrj continue;
335738fd1498Szrj
335838fd1498Szrj if (TREE_OVERFLOW_P (cst))
335938fd1498Szrj cst = drop_tree_overflow (cst);
336038fd1498Szrj add_assert_info (asserts, name2, name2, comp_code, cst);
336138fd1498Szrj }
336238fd1498Szrj }
336338fd1498Szrj
336438fd1498Szrj if (TREE_CODE_CLASS (comp_code) == tcc_comparison
336538fd1498Szrj && TREE_CODE (val) == INTEGER_CST)
336638fd1498Szrj {
336738fd1498Szrj gimple *def_stmt = SSA_NAME_DEF_STMT (name);
336838fd1498Szrj tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
336938fd1498Szrj tree val2 = NULL_TREE;
337038fd1498Szrj unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
337138fd1498Szrj wide_int mask = wi::zero (prec);
337238fd1498Szrj unsigned int nprec = prec;
337338fd1498Szrj enum tree_code rhs_code = ERROR_MARK;
337438fd1498Szrj
337538fd1498Szrj if (is_gimple_assign (def_stmt))
337638fd1498Szrj rhs_code = gimple_assign_rhs_code (def_stmt);
337738fd1498Szrj
337838fd1498Szrj /* In the case of NAME != CST1 where NAME = A +- CST2 we can
337938fd1498Szrj assert that A != CST1 -+ CST2. */
338038fd1498Szrj if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
338138fd1498Szrj && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
338238fd1498Szrj {
338338fd1498Szrj tree op0 = gimple_assign_rhs1 (def_stmt);
338438fd1498Szrj tree op1 = gimple_assign_rhs2 (def_stmt);
338538fd1498Szrj if (TREE_CODE (op0) == SSA_NAME
338638fd1498Szrj && TREE_CODE (op1) == INTEGER_CST)
338738fd1498Szrj {
338838fd1498Szrj enum tree_code reverse_op = (rhs_code == PLUS_EXPR
338938fd1498Szrj ? MINUS_EXPR : PLUS_EXPR);
339038fd1498Szrj op1 = int_const_binop (reverse_op, val, op1);
339138fd1498Szrj if (TREE_OVERFLOW (op1))
339238fd1498Szrj op1 = drop_tree_overflow (op1);
339338fd1498Szrj add_assert_info (asserts, op0, op0, comp_code, op1);
339438fd1498Szrj }
339538fd1498Szrj }
339638fd1498Szrj
339738fd1498Szrj /* Add asserts for NAME cmp CST and NAME being defined
339838fd1498Szrj as NAME = (int) NAME2. */
339938fd1498Szrj if (!TYPE_UNSIGNED (TREE_TYPE (val))
340038fd1498Szrj && (comp_code == LE_EXPR || comp_code == LT_EXPR
340138fd1498Szrj || comp_code == GT_EXPR || comp_code == GE_EXPR)
340238fd1498Szrj && gimple_assign_cast_p (def_stmt))
340338fd1498Szrj {
340438fd1498Szrj name2 = gimple_assign_rhs1 (def_stmt);
340538fd1498Szrj if (CONVERT_EXPR_CODE_P (rhs_code)
340638fd1498Szrj && INTEGRAL_TYPE_P (TREE_TYPE (name2))
340738fd1498Szrj && TYPE_UNSIGNED (TREE_TYPE (name2))
340838fd1498Szrj && prec == TYPE_PRECISION (TREE_TYPE (name2))
340938fd1498Szrj && (comp_code == LE_EXPR || comp_code == GT_EXPR
341038fd1498Szrj || !tree_int_cst_equal (val,
341138fd1498Szrj TYPE_MIN_VALUE (TREE_TYPE (val)))))
341238fd1498Szrj {
341338fd1498Szrj tree tmp, cst;
341438fd1498Szrj enum tree_code new_comp_code = comp_code;
341538fd1498Szrj
341638fd1498Szrj cst = fold_convert (TREE_TYPE (name2),
341738fd1498Szrj TYPE_MIN_VALUE (TREE_TYPE (val)));
341838fd1498Szrj /* Build an expression for the range test. */
341938fd1498Szrj tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
342038fd1498Szrj cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
342138fd1498Szrj fold_convert (TREE_TYPE (name2), val));
342238fd1498Szrj if (comp_code == LT_EXPR || comp_code == GE_EXPR)
342338fd1498Szrj {
342438fd1498Szrj new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
342538fd1498Szrj cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
342638fd1498Szrj build_int_cst (TREE_TYPE (name2), 1));
342738fd1498Szrj }
342838fd1498Szrj
342938fd1498Szrj if (dump_file)
343038fd1498Szrj {
343138fd1498Szrj fprintf (dump_file, "Adding assert for ");
343238fd1498Szrj print_generic_expr (dump_file, name2);
343338fd1498Szrj fprintf (dump_file, " from ");
343438fd1498Szrj print_generic_expr (dump_file, tmp);
343538fd1498Szrj fprintf (dump_file, "\n");
343638fd1498Szrj }
343738fd1498Szrj
343838fd1498Szrj add_assert_info (asserts, name2, tmp, new_comp_code, cst);
343938fd1498Szrj }
344038fd1498Szrj }
344138fd1498Szrj
344238fd1498Szrj /* Add asserts for NAME cmp CST and NAME being defined as
344338fd1498Szrj NAME = NAME2 >> CST2.
344438fd1498Szrj
344538fd1498Szrj Extract CST2 from the right shift. */
344638fd1498Szrj if (rhs_code == RSHIFT_EXPR)
344738fd1498Szrj {
344838fd1498Szrj name2 = gimple_assign_rhs1 (def_stmt);
344938fd1498Szrj cst2 = gimple_assign_rhs2 (def_stmt);
345038fd1498Szrj if (TREE_CODE (name2) == SSA_NAME
345138fd1498Szrj && tree_fits_uhwi_p (cst2)
345238fd1498Szrj && INTEGRAL_TYPE_P (TREE_TYPE (name2))
345338fd1498Szrj && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
345438fd1498Szrj && type_has_mode_precision_p (TREE_TYPE (val)))
345538fd1498Szrj {
345638fd1498Szrj mask = wi::mask (tree_to_uhwi (cst2), false, prec);
345738fd1498Szrj val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
345838fd1498Szrj }
345938fd1498Szrj }
346038fd1498Szrj if (val2 != NULL_TREE
346138fd1498Szrj && TREE_CODE (val2) == INTEGER_CST
346238fd1498Szrj && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
346338fd1498Szrj TREE_TYPE (val),
346438fd1498Szrj val2, cst2), val))
346538fd1498Szrj {
346638fd1498Szrj enum tree_code new_comp_code = comp_code;
346738fd1498Szrj tree tmp, new_val;
346838fd1498Szrj
346938fd1498Szrj tmp = name2;
347038fd1498Szrj if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
347138fd1498Szrj {
347238fd1498Szrj if (!TYPE_UNSIGNED (TREE_TYPE (val)))
347338fd1498Szrj {
347438fd1498Szrj tree type = build_nonstandard_integer_type (prec, 1);
347538fd1498Szrj tmp = build1 (NOP_EXPR, type, name2);
347638fd1498Szrj val2 = fold_convert (type, val2);
347738fd1498Szrj }
347838fd1498Szrj tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
347938fd1498Szrj new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
348038fd1498Szrj new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
348138fd1498Szrj }
348238fd1498Szrj else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
348338fd1498Szrj {
348438fd1498Szrj wide_int minval
348538fd1498Szrj = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
348638fd1498Szrj new_val = val2;
348738fd1498Szrj if (minval == wi::to_wide (new_val))
348838fd1498Szrj new_val = NULL_TREE;
348938fd1498Szrj }
349038fd1498Szrj else
349138fd1498Szrj {
349238fd1498Szrj wide_int maxval
349338fd1498Szrj = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
349438fd1498Szrj mask |= wi::to_wide (val2);
349538fd1498Szrj if (wi::eq_p (mask, maxval))
349638fd1498Szrj new_val = NULL_TREE;
349738fd1498Szrj else
349838fd1498Szrj new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
349938fd1498Szrj }
350038fd1498Szrj
350138fd1498Szrj if (new_val)
350238fd1498Szrj {
350338fd1498Szrj if (dump_file)
350438fd1498Szrj {
350538fd1498Szrj fprintf (dump_file, "Adding assert for ");
350638fd1498Szrj print_generic_expr (dump_file, name2);
350738fd1498Szrj fprintf (dump_file, " from ");
350838fd1498Szrj print_generic_expr (dump_file, tmp);
350938fd1498Szrj fprintf (dump_file, "\n");
351038fd1498Szrj }
351138fd1498Szrj
351238fd1498Szrj add_assert_info (asserts, name2, tmp, new_comp_code, new_val);
351338fd1498Szrj }
351438fd1498Szrj }
351538fd1498Szrj
351638fd1498Szrj /* Add asserts for NAME cmp CST and NAME being defined as
351738fd1498Szrj NAME = NAME2 & CST2.
351838fd1498Szrj
351938fd1498Szrj Extract CST2 from the and.
352038fd1498Szrj
352138fd1498Szrj Also handle
352238fd1498Szrj NAME = (unsigned) NAME2;
352338fd1498Szrj casts where NAME's type is unsigned and has smaller precision
352438fd1498Szrj than NAME2's type as if it was NAME = NAME2 & MASK. */
352538fd1498Szrj names[0] = NULL_TREE;
352638fd1498Szrj names[1] = NULL_TREE;
352738fd1498Szrj cst2 = NULL_TREE;
352838fd1498Szrj if (rhs_code == BIT_AND_EXPR
352938fd1498Szrj || (CONVERT_EXPR_CODE_P (rhs_code)
353038fd1498Szrj && INTEGRAL_TYPE_P (TREE_TYPE (val))
353138fd1498Szrj && TYPE_UNSIGNED (TREE_TYPE (val))
353238fd1498Szrj && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
353338fd1498Szrj > prec))
353438fd1498Szrj {
353538fd1498Szrj name2 = gimple_assign_rhs1 (def_stmt);
353638fd1498Szrj if (rhs_code == BIT_AND_EXPR)
353738fd1498Szrj cst2 = gimple_assign_rhs2 (def_stmt);
353838fd1498Szrj else
353938fd1498Szrj {
354038fd1498Szrj cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
354138fd1498Szrj nprec = TYPE_PRECISION (TREE_TYPE (name2));
354238fd1498Szrj }
354338fd1498Szrj if (TREE_CODE (name2) == SSA_NAME
354438fd1498Szrj && INTEGRAL_TYPE_P (TREE_TYPE (name2))
354538fd1498Szrj && TREE_CODE (cst2) == INTEGER_CST
354638fd1498Szrj && !integer_zerop (cst2)
354738fd1498Szrj && (nprec > 1
354838fd1498Szrj || TYPE_UNSIGNED (TREE_TYPE (val))))
354938fd1498Szrj {
355038fd1498Szrj gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
355138fd1498Szrj if (gimple_assign_cast_p (def_stmt2))
355238fd1498Szrj {
355338fd1498Szrj names[1] = gimple_assign_rhs1 (def_stmt2);
355438fd1498Szrj if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
355538fd1498Szrj || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
355638fd1498Szrj || (TYPE_PRECISION (TREE_TYPE (name2))
355738fd1498Szrj != TYPE_PRECISION (TREE_TYPE (names[1]))))
355838fd1498Szrj names[1] = NULL_TREE;
355938fd1498Szrj }
356038fd1498Szrj names[0] = name2;
356138fd1498Szrj }
356238fd1498Szrj }
356338fd1498Szrj if (names[0] || names[1])
356438fd1498Szrj {
356538fd1498Szrj wide_int minv, maxv, valv, cst2v;
356638fd1498Szrj wide_int tem, sgnbit;
356738fd1498Szrj bool valid_p = false, valn, cst2n;
356838fd1498Szrj enum tree_code ccode = comp_code;
356938fd1498Szrj
357038fd1498Szrj valv = wide_int::from (wi::to_wide (val), nprec, UNSIGNED);
357138fd1498Szrj cst2v = wide_int::from (wi::to_wide (cst2), nprec, UNSIGNED);
357238fd1498Szrj valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
357338fd1498Szrj cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
357438fd1498Szrj /* If CST2 doesn't have most significant bit set,
357538fd1498Szrj but VAL is negative, we have comparison like
357638fd1498Szrj if ((x & 0x123) > -4) (always true). Just give up. */
357738fd1498Szrj if (!cst2n && valn)
357838fd1498Szrj ccode = ERROR_MARK;
357938fd1498Szrj if (cst2n)
358038fd1498Szrj sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
358138fd1498Szrj else
358238fd1498Szrj sgnbit = wi::zero (nprec);
358338fd1498Szrj minv = valv & cst2v;
358438fd1498Szrj switch (ccode)
358538fd1498Szrj {
358638fd1498Szrj case EQ_EXPR:
358738fd1498Szrj /* Minimum unsigned value for equality is VAL & CST2
358838fd1498Szrj (should be equal to VAL, otherwise we probably should
358938fd1498Szrj have folded the comparison into false) and
359038fd1498Szrj maximum unsigned value is VAL | ~CST2. */
359138fd1498Szrj maxv = valv | ~cst2v;
359238fd1498Szrj valid_p = true;
359338fd1498Szrj break;
359438fd1498Szrj
359538fd1498Szrj case NE_EXPR:
359638fd1498Szrj tem = valv | ~cst2v;
359738fd1498Szrj /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
359838fd1498Szrj if (valv == 0)
359938fd1498Szrj {
360038fd1498Szrj cst2n = false;
360138fd1498Szrj sgnbit = wi::zero (nprec);
360238fd1498Szrj goto gt_expr;
360338fd1498Szrj }
360438fd1498Szrj /* If (VAL | ~CST2) is all ones, handle it as
360538fd1498Szrj (X & CST2) < VAL. */
360638fd1498Szrj if (tem == -1)
360738fd1498Szrj {
360838fd1498Szrj cst2n = false;
360938fd1498Szrj valn = false;
361038fd1498Szrj sgnbit = wi::zero (nprec);
361138fd1498Szrj goto lt_expr;
361238fd1498Szrj }
361338fd1498Szrj if (!cst2n && wi::neg_p (cst2v))
361438fd1498Szrj sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
361538fd1498Szrj if (sgnbit != 0)
361638fd1498Szrj {
361738fd1498Szrj if (valv == sgnbit)
361838fd1498Szrj {
361938fd1498Szrj cst2n = true;
362038fd1498Szrj valn = true;
362138fd1498Szrj goto gt_expr;
362238fd1498Szrj }
362338fd1498Szrj if (tem == wi::mask (nprec - 1, false, nprec))
362438fd1498Szrj {
362538fd1498Szrj cst2n = true;
362638fd1498Szrj goto lt_expr;
362738fd1498Szrj }
362838fd1498Szrj if (!cst2n)
362938fd1498Szrj sgnbit = wi::zero (nprec);
363038fd1498Szrj }
363138fd1498Szrj break;
363238fd1498Szrj
363338fd1498Szrj case GE_EXPR:
363438fd1498Szrj /* Minimum unsigned value for >= if (VAL & CST2) == VAL
363538fd1498Szrj is VAL and maximum unsigned value is ~0. For signed
363638fd1498Szrj comparison, if CST2 doesn't have most significant bit
363738fd1498Szrj set, handle it similarly. If CST2 has MSB set,
363838fd1498Szrj the minimum is the same, and maximum is ~0U/2. */
363938fd1498Szrj if (minv != valv)
364038fd1498Szrj {
364138fd1498Szrj /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
364238fd1498Szrj VAL. */
364338fd1498Szrj minv = masked_increment (valv, cst2v, sgnbit, nprec);
364438fd1498Szrj if (minv == valv)
364538fd1498Szrj break;
364638fd1498Szrj }
364738fd1498Szrj maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
364838fd1498Szrj valid_p = true;
364938fd1498Szrj break;
365038fd1498Szrj
365138fd1498Szrj case GT_EXPR:
365238fd1498Szrj gt_expr:
365338fd1498Szrj /* Find out smallest MINV where MINV > VAL
365438fd1498Szrj && (MINV & CST2) == MINV, if any. If VAL is signed and
365538fd1498Szrj CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
365638fd1498Szrj minv = masked_increment (valv, cst2v, sgnbit, nprec);
365738fd1498Szrj if (minv == valv)
365838fd1498Szrj break;
365938fd1498Szrj maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
366038fd1498Szrj valid_p = true;
366138fd1498Szrj break;
366238fd1498Szrj
366338fd1498Szrj case LE_EXPR:
366438fd1498Szrj /* Minimum unsigned value for <= is 0 and maximum
366538fd1498Szrj unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
366638fd1498Szrj Otherwise, find smallest VAL2 where VAL2 > VAL
366738fd1498Szrj && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
366838fd1498Szrj as maximum.
366938fd1498Szrj For signed comparison, if CST2 doesn't have most
367038fd1498Szrj significant bit set, handle it similarly. If CST2 has
367138fd1498Szrj MSB set, the maximum is the same and minimum is INT_MIN. */
367238fd1498Szrj if (minv == valv)
367338fd1498Szrj maxv = valv;
367438fd1498Szrj else
367538fd1498Szrj {
367638fd1498Szrj maxv = masked_increment (valv, cst2v, sgnbit, nprec);
367738fd1498Szrj if (maxv == valv)
367838fd1498Szrj break;
367938fd1498Szrj maxv -= 1;
368038fd1498Szrj }
368138fd1498Szrj maxv |= ~cst2v;
368238fd1498Szrj minv = sgnbit;
368338fd1498Szrj valid_p = true;
368438fd1498Szrj break;
368538fd1498Szrj
368638fd1498Szrj case LT_EXPR:
368738fd1498Szrj lt_expr:
368838fd1498Szrj /* Minimum unsigned value for < is 0 and maximum
368938fd1498Szrj unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
369038fd1498Szrj Otherwise, find smallest VAL2 where VAL2 > VAL
369138fd1498Szrj && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
369238fd1498Szrj as maximum.
369338fd1498Szrj For signed comparison, if CST2 doesn't have most
369438fd1498Szrj significant bit set, handle it similarly. If CST2 has
369538fd1498Szrj MSB set, the maximum is the same and minimum is INT_MIN. */
369638fd1498Szrj if (minv == valv)
369738fd1498Szrj {
369838fd1498Szrj if (valv == sgnbit)
369938fd1498Szrj break;
370038fd1498Szrj maxv = valv;
370138fd1498Szrj }
370238fd1498Szrj else
370338fd1498Szrj {
370438fd1498Szrj maxv = masked_increment (valv, cst2v, sgnbit, nprec);
370538fd1498Szrj if (maxv == valv)
370638fd1498Szrj break;
370738fd1498Szrj }
370838fd1498Szrj maxv -= 1;
370938fd1498Szrj maxv |= ~cst2v;
371038fd1498Szrj minv = sgnbit;
371138fd1498Szrj valid_p = true;
371238fd1498Szrj break;
371338fd1498Szrj
371438fd1498Szrj default:
371538fd1498Szrj break;
371638fd1498Szrj }
371738fd1498Szrj if (valid_p
371838fd1498Szrj && (maxv - minv) != -1)
371938fd1498Szrj {
372038fd1498Szrj tree tmp, new_val, type;
372138fd1498Szrj int i;
372238fd1498Szrj
372338fd1498Szrj for (i = 0; i < 2; i++)
372438fd1498Szrj if (names[i])
372538fd1498Szrj {
372638fd1498Szrj wide_int maxv2 = maxv;
372738fd1498Szrj tmp = names[i];
372838fd1498Szrj type = TREE_TYPE (names[i]);
372938fd1498Szrj if (!TYPE_UNSIGNED (type))
373038fd1498Szrj {
373138fd1498Szrj type = build_nonstandard_integer_type (nprec, 1);
373238fd1498Szrj tmp = build1 (NOP_EXPR, type, names[i]);
373338fd1498Szrj }
373438fd1498Szrj if (minv != 0)
373538fd1498Szrj {
373638fd1498Szrj tmp = build2 (PLUS_EXPR, type, tmp,
373738fd1498Szrj wide_int_to_tree (type, -minv));
373838fd1498Szrj maxv2 = maxv - minv;
373938fd1498Szrj }
374038fd1498Szrj new_val = wide_int_to_tree (type, maxv2);
374138fd1498Szrj
374238fd1498Szrj if (dump_file)
374338fd1498Szrj {
374438fd1498Szrj fprintf (dump_file, "Adding assert for ");
374538fd1498Szrj print_generic_expr (dump_file, names[i]);
374638fd1498Szrj fprintf (dump_file, " from ");
374738fd1498Szrj print_generic_expr (dump_file, tmp);
374838fd1498Szrj fprintf (dump_file, "\n");
374938fd1498Szrj }
375038fd1498Szrj
375138fd1498Szrj add_assert_info (asserts, names[i], tmp, LE_EXPR, new_val);
375238fd1498Szrj }
375338fd1498Szrj }
375438fd1498Szrj }
375538fd1498Szrj }
375638fd1498Szrj }
375738fd1498Szrj
375838fd1498Szrj /* OP is an operand of a truth value expression which is known to have
375938fd1498Szrj a particular value. Register any asserts for OP and for any
376038fd1498Szrj operands in OP's defining statement.
376138fd1498Szrj
376238fd1498Szrj If CODE is EQ_EXPR, then we want to register OP is zero (false),
376338fd1498Szrj if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
376438fd1498Szrj
376538fd1498Szrj static void
register_edge_assert_for_1(tree op,enum tree_code code,edge e,vec<assert_info> & asserts)376638fd1498Szrj register_edge_assert_for_1 (tree op, enum tree_code code,
376738fd1498Szrj edge e, vec<assert_info> &asserts)
376838fd1498Szrj {
376938fd1498Szrj gimple *op_def;
377038fd1498Szrj tree val;
377138fd1498Szrj enum tree_code rhs_code;
377238fd1498Szrj
377338fd1498Szrj /* We only care about SSA_NAMEs. */
377438fd1498Szrj if (TREE_CODE (op) != SSA_NAME)
377538fd1498Szrj return;
377638fd1498Szrj
377738fd1498Szrj /* We know that OP will have a zero or nonzero value. */
377838fd1498Szrj val = build_int_cst (TREE_TYPE (op), 0);
377938fd1498Szrj add_assert_info (asserts, op, op, code, val);
378038fd1498Szrj
378138fd1498Szrj /* Now look at how OP is set. If it's set from a comparison,
378238fd1498Szrj a truth operation or some bit operations, then we may be able
378338fd1498Szrj to register information about the operands of that assignment. */
378438fd1498Szrj op_def = SSA_NAME_DEF_STMT (op);
378538fd1498Szrj if (gimple_code (op_def) != GIMPLE_ASSIGN)
378638fd1498Szrj return;
378738fd1498Szrj
378838fd1498Szrj rhs_code = gimple_assign_rhs_code (op_def);
378938fd1498Szrj
379038fd1498Szrj if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
379138fd1498Szrj {
379238fd1498Szrj bool invert = (code == EQ_EXPR ? true : false);
379338fd1498Szrj tree op0 = gimple_assign_rhs1 (op_def);
379438fd1498Szrj tree op1 = gimple_assign_rhs2 (op_def);
379538fd1498Szrj
379638fd1498Szrj if (TREE_CODE (op0) == SSA_NAME)
379738fd1498Szrj register_edge_assert_for_2 (op0, e, rhs_code, op0, op1, invert, asserts);
379838fd1498Szrj if (TREE_CODE (op1) == SSA_NAME)
379938fd1498Szrj register_edge_assert_for_2 (op1, e, rhs_code, op0, op1, invert, asserts);
380038fd1498Szrj }
380138fd1498Szrj else if ((code == NE_EXPR
380238fd1498Szrj && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
380338fd1498Szrj || (code == EQ_EXPR
380438fd1498Szrj && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
380538fd1498Szrj {
380638fd1498Szrj /* Recurse on each operand. */
380738fd1498Szrj tree op0 = gimple_assign_rhs1 (op_def);
380838fd1498Szrj tree op1 = gimple_assign_rhs2 (op_def);
380938fd1498Szrj if (TREE_CODE (op0) == SSA_NAME
381038fd1498Szrj && has_single_use (op0))
381138fd1498Szrj register_edge_assert_for_1 (op0, code, e, asserts);
381238fd1498Szrj if (TREE_CODE (op1) == SSA_NAME
381338fd1498Szrj && has_single_use (op1))
381438fd1498Szrj register_edge_assert_for_1 (op1, code, e, asserts);
381538fd1498Szrj }
381638fd1498Szrj else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
381738fd1498Szrj && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
381838fd1498Szrj {
381938fd1498Szrj /* Recurse, flipping CODE. */
382038fd1498Szrj code = invert_tree_comparison (code, false);
382138fd1498Szrj register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
382238fd1498Szrj }
382338fd1498Szrj else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
382438fd1498Szrj {
382538fd1498Szrj /* Recurse through the copy. */
382638fd1498Szrj register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
382738fd1498Szrj }
382838fd1498Szrj else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
382938fd1498Szrj {
383038fd1498Szrj /* Recurse through the type conversion, unless it is a narrowing
383138fd1498Szrj conversion or conversion from non-integral type. */
383238fd1498Szrj tree rhs = gimple_assign_rhs1 (op_def);
383338fd1498Szrj if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
383438fd1498Szrj && (TYPE_PRECISION (TREE_TYPE (rhs))
383538fd1498Szrj <= TYPE_PRECISION (TREE_TYPE (op))))
383638fd1498Szrj register_edge_assert_for_1 (rhs, code, e, asserts);
383738fd1498Szrj }
383838fd1498Szrj }
383938fd1498Szrj
384038fd1498Szrj /* Check if comparison
384138fd1498Szrj NAME COND_OP INTEGER_CST
384238fd1498Szrj has a form of
384338fd1498Szrj (X & 11...100..0) COND_OP XX...X00...0
384438fd1498Szrj Such comparison can yield assertions like
384538fd1498Szrj X >= XX...X00...0
384638fd1498Szrj X <= XX...X11...1
3847*58e805e6Szrj in case of COND_OP being EQ_EXPR or
384838fd1498Szrj X < XX...X00...0
384938fd1498Szrj X > XX...X11...1
3850*58e805e6Szrj in case of NE_EXPR. */
385138fd1498Szrj
385238fd1498Szrj static bool
is_masked_range_test(tree name,tree valt,enum tree_code cond_code,tree * new_name,tree * low,enum tree_code * low_code,tree * high,enum tree_code * high_code)385338fd1498Szrj is_masked_range_test (tree name, tree valt, enum tree_code cond_code,
385438fd1498Szrj tree *new_name, tree *low, enum tree_code *low_code,
385538fd1498Szrj tree *high, enum tree_code *high_code)
385638fd1498Szrj {
385738fd1498Szrj gimple *def_stmt = SSA_NAME_DEF_STMT (name);
385838fd1498Szrj
385938fd1498Szrj if (!is_gimple_assign (def_stmt)
386038fd1498Szrj || gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
386138fd1498Szrj return false;
386238fd1498Szrj
386338fd1498Szrj tree t = gimple_assign_rhs1 (def_stmt);
386438fd1498Szrj tree maskt = gimple_assign_rhs2 (def_stmt);
386538fd1498Szrj if (TREE_CODE (t) != SSA_NAME || TREE_CODE (maskt) != INTEGER_CST)
386638fd1498Szrj return false;
386738fd1498Szrj
386838fd1498Szrj wi::tree_to_wide_ref mask = wi::to_wide (maskt);
386938fd1498Szrj wide_int inv_mask = ~mask;
3870*58e805e6Szrj /* Must have been removed by now so don't bother optimizing. */
3871*58e805e6Szrj if (mask == 0 || inv_mask == 0)
3872*58e805e6Szrj return false;
3873*58e805e6Szrj
387438fd1498Szrj /* Assume VALT is INTEGER_CST. */
387538fd1498Szrj wi::tree_to_wide_ref val = wi::to_wide (valt);
387638fd1498Szrj
387738fd1498Szrj if ((inv_mask & (inv_mask + 1)) != 0
387838fd1498Szrj || (val & mask) != val)
387938fd1498Szrj return false;
388038fd1498Szrj
388138fd1498Szrj bool is_range = cond_code == EQ_EXPR;
388238fd1498Szrj
388338fd1498Szrj tree type = TREE_TYPE (t);
388438fd1498Szrj wide_int min = wi::min_value (type),
388538fd1498Szrj max = wi::max_value (type);
388638fd1498Szrj
388738fd1498Szrj if (is_range)
388838fd1498Szrj {
388938fd1498Szrj *low_code = val == min ? ERROR_MARK : GE_EXPR;
389038fd1498Szrj *high_code = val == max ? ERROR_MARK : LE_EXPR;
389138fd1498Szrj }
389238fd1498Szrj else
389338fd1498Szrj {
389438fd1498Szrj /* We can still generate assertion if one of alternatives
389538fd1498Szrj is known to always be false. */
389638fd1498Szrj if (val == min)
389738fd1498Szrj {
389838fd1498Szrj *low_code = (enum tree_code) 0;
389938fd1498Szrj *high_code = GT_EXPR;
390038fd1498Szrj }
390138fd1498Szrj else if ((val | inv_mask) == max)
390238fd1498Szrj {
390338fd1498Szrj *low_code = LT_EXPR;
390438fd1498Szrj *high_code = (enum tree_code) 0;
390538fd1498Szrj }
390638fd1498Szrj else
390738fd1498Szrj return false;
390838fd1498Szrj }
390938fd1498Szrj
391038fd1498Szrj *new_name = t;
391138fd1498Szrj *low = wide_int_to_tree (type, val);
391238fd1498Szrj *high = wide_int_to_tree (type, val | inv_mask);
391338fd1498Szrj
391438fd1498Szrj return true;
391538fd1498Szrj }
391638fd1498Szrj
391738fd1498Szrj /* Try to register an edge assertion for SSA name NAME on edge E for
391838fd1498Szrj the condition COND contributing to the conditional jump pointed to by
391938fd1498Szrj SI. */
392038fd1498Szrj
392138fd1498Szrj void
register_edge_assert_for(tree name,edge e,enum tree_code cond_code,tree cond_op0,tree cond_op1,vec<assert_info> & asserts)392238fd1498Szrj register_edge_assert_for (tree name, edge e,
392338fd1498Szrj enum tree_code cond_code, tree cond_op0,
392438fd1498Szrj tree cond_op1, vec<assert_info> &asserts)
392538fd1498Szrj {
392638fd1498Szrj tree val;
392738fd1498Szrj enum tree_code comp_code;
392838fd1498Szrj bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
392938fd1498Szrj
393038fd1498Szrj /* Do not attempt to infer anything in names that flow through
393138fd1498Szrj abnormal edges. */
393238fd1498Szrj if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
393338fd1498Szrj return;
393438fd1498Szrj
393538fd1498Szrj if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
393638fd1498Szrj cond_op0, cond_op1,
393738fd1498Szrj is_else_edge,
393838fd1498Szrj &comp_code, &val))
393938fd1498Szrj return;
394038fd1498Szrj
394138fd1498Szrj /* Register ASSERT_EXPRs for name. */
394238fd1498Szrj register_edge_assert_for_2 (name, e, cond_code, cond_op0,
394338fd1498Szrj cond_op1, is_else_edge, asserts);
394438fd1498Szrj
394538fd1498Szrj
394638fd1498Szrj /* If COND is effectively an equality test of an SSA_NAME against
394738fd1498Szrj the value zero or one, then we may be able to assert values
394838fd1498Szrj for SSA_NAMEs which flow into COND. */
394938fd1498Szrj
395038fd1498Szrj /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
395138fd1498Szrj statement of NAME we can assert both operands of the BIT_AND_EXPR
395238fd1498Szrj have nonzero value. */
395338fd1498Szrj if (((comp_code == EQ_EXPR && integer_onep (val))
395438fd1498Szrj || (comp_code == NE_EXPR && integer_zerop (val))))
395538fd1498Szrj {
395638fd1498Szrj gimple *def_stmt = SSA_NAME_DEF_STMT (name);
395738fd1498Szrj
395838fd1498Szrj if (is_gimple_assign (def_stmt)
395938fd1498Szrj && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
396038fd1498Szrj {
396138fd1498Szrj tree op0 = gimple_assign_rhs1 (def_stmt);
396238fd1498Szrj tree op1 = gimple_assign_rhs2 (def_stmt);
396338fd1498Szrj register_edge_assert_for_1 (op0, NE_EXPR, e, asserts);
396438fd1498Szrj register_edge_assert_for_1 (op1, NE_EXPR, e, asserts);
396538fd1498Szrj }
396638fd1498Szrj }
396738fd1498Szrj
396838fd1498Szrj /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
396938fd1498Szrj statement of NAME we can assert both operands of the BIT_IOR_EXPR
397038fd1498Szrj have zero value. */
397138fd1498Szrj if (((comp_code == EQ_EXPR && integer_zerop (val))
397238fd1498Szrj || (comp_code == NE_EXPR && integer_onep (val))))
397338fd1498Szrj {
397438fd1498Szrj gimple *def_stmt = SSA_NAME_DEF_STMT (name);
397538fd1498Szrj
397638fd1498Szrj /* For BIT_IOR_EXPR only if NAME == 0 both operands have
397738fd1498Szrj necessarily zero value, or if type-precision is one. */
397838fd1498Szrj if (is_gimple_assign (def_stmt)
397938fd1498Szrj && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
398038fd1498Szrj && (TYPE_PRECISION (TREE_TYPE (name)) == 1
398138fd1498Szrj || comp_code == EQ_EXPR)))
398238fd1498Szrj {
398338fd1498Szrj tree op0 = gimple_assign_rhs1 (def_stmt);
398438fd1498Szrj tree op1 = gimple_assign_rhs2 (def_stmt);
398538fd1498Szrj register_edge_assert_for_1 (op0, EQ_EXPR, e, asserts);
398638fd1498Szrj register_edge_assert_for_1 (op1, EQ_EXPR, e, asserts);
398738fd1498Szrj }
398838fd1498Szrj }
398938fd1498Szrj
399038fd1498Szrj /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
399138fd1498Szrj if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
399238fd1498Szrj && TREE_CODE (val) == INTEGER_CST)
399338fd1498Szrj {
399438fd1498Szrj enum tree_code low_code, high_code;
399538fd1498Szrj tree low, high;
399638fd1498Szrj if (is_masked_range_test (name, val, comp_code, &name, &low,
399738fd1498Szrj &low_code, &high, &high_code))
399838fd1498Szrj {
399938fd1498Szrj if (low_code != ERROR_MARK)
400038fd1498Szrj register_edge_assert_for_2 (name, e, low_code, name,
400138fd1498Szrj low, /*invert*/false, asserts);
400238fd1498Szrj if (high_code != ERROR_MARK)
400338fd1498Szrj register_edge_assert_for_2 (name, e, high_code, name,
400438fd1498Szrj high, /*invert*/false, asserts);
400538fd1498Szrj }
400638fd1498Szrj }
400738fd1498Szrj }
400838fd1498Szrj
400938fd1498Szrj /* Finish found ASSERTS for E and register them at GSI. */
401038fd1498Szrj
401138fd1498Szrj static void
finish_register_edge_assert_for(edge e,gimple_stmt_iterator gsi,vec<assert_info> & asserts)401238fd1498Szrj finish_register_edge_assert_for (edge e, gimple_stmt_iterator gsi,
401338fd1498Szrj vec<assert_info> &asserts)
401438fd1498Szrj {
401538fd1498Szrj for (unsigned i = 0; i < asserts.length (); ++i)
401638fd1498Szrj /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
401738fd1498Szrj reachable from E. */
401838fd1498Szrj if (live_on_edge (e, asserts[i].name))
401938fd1498Szrj register_new_assert_for (asserts[i].name, asserts[i].expr,
402038fd1498Szrj asserts[i].comp_code, asserts[i].val,
402138fd1498Szrj NULL, e, gsi);
402238fd1498Szrj }
402338fd1498Szrj
402438fd1498Szrj
402538fd1498Szrj
402638fd1498Szrj /* Determine whether the outgoing edges of BB should receive an
402738fd1498Szrj ASSERT_EXPR for each of the operands of BB's LAST statement.
402838fd1498Szrj The last statement of BB must be a COND_EXPR.
402938fd1498Szrj
403038fd1498Szrj If any of the sub-graphs rooted at BB have an interesting use of
403138fd1498Szrj the predicate operands, an assert location node is added to the
403238fd1498Szrj list of assertions for the corresponding operands. */
403338fd1498Szrj
403438fd1498Szrj static void
find_conditional_asserts(basic_block bb,gcond * last)403538fd1498Szrj find_conditional_asserts (basic_block bb, gcond *last)
403638fd1498Szrj {
403738fd1498Szrj gimple_stmt_iterator bsi;
403838fd1498Szrj tree op;
403938fd1498Szrj edge_iterator ei;
404038fd1498Szrj edge e;
404138fd1498Szrj ssa_op_iter iter;
404238fd1498Szrj
404338fd1498Szrj bsi = gsi_for_stmt (last);
404438fd1498Szrj
404538fd1498Szrj /* Look for uses of the operands in each of the sub-graphs
404638fd1498Szrj rooted at BB. We need to check each of the outgoing edges
404738fd1498Szrj separately, so that we know what kind of ASSERT_EXPR to
404838fd1498Szrj insert. */
404938fd1498Szrj FOR_EACH_EDGE (e, ei, bb->succs)
405038fd1498Szrj {
405138fd1498Szrj if (e->dest == bb)
405238fd1498Szrj continue;
405338fd1498Szrj
405438fd1498Szrj /* Register the necessary assertions for each operand in the
405538fd1498Szrj conditional predicate. */
405638fd1498Szrj auto_vec<assert_info, 8> asserts;
405738fd1498Szrj FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
405838fd1498Szrj register_edge_assert_for (op, e,
405938fd1498Szrj gimple_cond_code (last),
406038fd1498Szrj gimple_cond_lhs (last),
406138fd1498Szrj gimple_cond_rhs (last), asserts);
406238fd1498Szrj finish_register_edge_assert_for (e, bsi, asserts);
406338fd1498Szrj }
406438fd1498Szrj }
406538fd1498Szrj
406638fd1498Szrj struct case_info
406738fd1498Szrj {
406838fd1498Szrj tree expr;
406938fd1498Szrj basic_block bb;
407038fd1498Szrj };
407138fd1498Szrj
407238fd1498Szrj /* Compare two case labels sorting first by the destination bb index
407338fd1498Szrj and then by the case value. */
407438fd1498Szrj
407538fd1498Szrj static int
compare_case_labels(const void * p1,const void * p2)407638fd1498Szrj compare_case_labels (const void *p1, const void *p2)
407738fd1498Szrj {
407838fd1498Szrj const struct case_info *ci1 = (const struct case_info *) p1;
407938fd1498Szrj const struct case_info *ci2 = (const struct case_info *) p2;
408038fd1498Szrj int idx1 = ci1->bb->index;
408138fd1498Szrj int idx2 = ci2->bb->index;
408238fd1498Szrj
408338fd1498Szrj if (idx1 < idx2)
408438fd1498Szrj return -1;
408538fd1498Szrj else if (idx1 == idx2)
408638fd1498Szrj {
408738fd1498Szrj /* Make sure the default label is first in a group. */
408838fd1498Szrj if (!CASE_LOW (ci1->expr))
408938fd1498Szrj return -1;
409038fd1498Szrj else if (!CASE_LOW (ci2->expr))
409138fd1498Szrj return 1;
409238fd1498Szrj else
409338fd1498Szrj return tree_int_cst_compare (CASE_LOW (ci1->expr),
409438fd1498Szrj CASE_LOW (ci2->expr));
409538fd1498Szrj }
409638fd1498Szrj else
409738fd1498Szrj return 1;
409838fd1498Szrj }
409938fd1498Szrj
410038fd1498Szrj /* Determine whether the outgoing edges of BB should receive an
410138fd1498Szrj ASSERT_EXPR for each of the operands of BB's LAST statement.
410238fd1498Szrj The last statement of BB must be a SWITCH_EXPR.
410338fd1498Szrj
410438fd1498Szrj If any of the sub-graphs rooted at BB have an interesting use of
410538fd1498Szrj the predicate operands, an assert location node is added to the
410638fd1498Szrj list of assertions for the corresponding operands. */
410738fd1498Szrj
410838fd1498Szrj static void
find_switch_asserts(basic_block bb,gswitch * last)410938fd1498Szrj find_switch_asserts (basic_block bb, gswitch *last)
411038fd1498Szrj {
411138fd1498Szrj gimple_stmt_iterator bsi;
411238fd1498Szrj tree op;
411338fd1498Szrj edge e;
411438fd1498Szrj struct case_info *ci;
411538fd1498Szrj size_t n = gimple_switch_num_labels (last);
411638fd1498Szrj #if GCC_VERSION >= 4000
411738fd1498Szrj unsigned int idx;
411838fd1498Szrj #else
411938fd1498Szrj /* Work around GCC 3.4 bug (PR 37086). */
412038fd1498Szrj volatile unsigned int idx;
412138fd1498Szrj #endif
412238fd1498Szrj
412338fd1498Szrj bsi = gsi_for_stmt (last);
412438fd1498Szrj op = gimple_switch_index (last);
412538fd1498Szrj if (TREE_CODE (op) != SSA_NAME)
412638fd1498Szrj return;
412738fd1498Szrj
412838fd1498Szrj /* Build a vector of case labels sorted by destination label. */
412938fd1498Szrj ci = XNEWVEC (struct case_info, n);
413038fd1498Szrj for (idx = 0; idx < n; ++idx)
413138fd1498Szrj {
413238fd1498Szrj ci[idx].expr = gimple_switch_label (last, idx);
413338fd1498Szrj ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
413438fd1498Szrj }
413538fd1498Szrj edge default_edge = find_edge (bb, ci[0].bb);
413638fd1498Szrj qsort (ci, n, sizeof (struct case_info), compare_case_labels);
413738fd1498Szrj
413838fd1498Szrj for (idx = 0; idx < n; ++idx)
413938fd1498Szrj {
414038fd1498Szrj tree min, max;
414138fd1498Szrj tree cl = ci[idx].expr;
414238fd1498Szrj basic_block cbb = ci[idx].bb;
414338fd1498Szrj
414438fd1498Szrj min = CASE_LOW (cl);
414538fd1498Szrj max = CASE_HIGH (cl);
414638fd1498Szrj
414738fd1498Szrj /* If there are multiple case labels with the same destination
414838fd1498Szrj we need to combine them to a single value range for the edge. */
414938fd1498Szrj if (idx + 1 < n && cbb == ci[idx + 1].bb)
415038fd1498Szrj {
415138fd1498Szrj /* Skip labels until the last of the group. */
415238fd1498Szrj do {
415338fd1498Szrj ++idx;
415438fd1498Szrj } while (idx < n && cbb == ci[idx].bb);
415538fd1498Szrj --idx;
415638fd1498Szrj
415738fd1498Szrj /* Pick up the maximum of the case label range. */
415838fd1498Szrj if (CASE_HIGH (ci[idx].expr))
415938fd1498Szrj max = CASE_HIGH (ci[idx].expr);
416038fd1498Szrj else
416138fd1498Szrj max = CASE_LOW (ci[idx].expr);
416238fd1498Szrj }
416338fd1498Szrj
416438fd1498Szrj /* Can't extract a useful assertion out of a range that includes the
416538fd1498Szrj default label. */
416638fd1498Szrj if (min == NULL_TREE)
416738fd1498Szrj continue;
416838fd1498Szrj
416938fd1498Szrj /* Find the edge to register the assert expr on. */
417038fd1498Szrj e = find_edge (bb, cbb);
417138fd1498Szrj
417238fd1498Szrj /* Register the necessary assertions for the operand in the
417338fd1498Szrj SWITCH_EXPR. */
417438fd1498Szrj auto_vec<assert_info, 8> asserts;
417538fd1498Szrj register_edge_assert_for (op, e,
417638fd1498Szrj max ? GE_EXPR : EQ_EXPR,
417738fd1498Szrj op, fold_convert (TREE_TYPE (op), min),
417838fd1498Szrj asserts);
417938fd1498Szrj if (max)
418038fd1498Szrj register_edge_assert_for (op, e, LE_EXPR, op,
418138fd1498Szrj fold_convert (TREE_TYPE (op), max),
418238fd1498Szrj asserts);
418338fd1498Szrj finish_register_edge_assert_for (e, bsi, asserts);
418438fd1498Szrj }
418538fd1498Szrj
418638fd1498Szrj XDELETEVEC (ci);
418738fd1498Szrj
418838fd1498Szrj if (!live_on_edge (default_edge, op))
418938fd1498Szrj return;
419038fd1498Szrj
419138fd1498Szrj /* Now register along the default label assertions that correspond to the
419238fd1498Szrj anti-range of each label. */
419338fd1498Szrj int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
419438fd1498Szrj if (insertion_limit == 0)
419538fd1498Szrj return;
419638fd1498Szrj
419738fd1498Szrj /* We can't do this if the default case shares a label with another case. */
419838fd1498Szrj tree default_cl = gimple_switch_default_label (last);
419938fd1498Szrj for (idx = 1; idx < n; idx++)
420038fd1498Szrj {
420138fd1498Szrj tree min, max;
420238fd1498Szrj tree cl = gimple_switch_label (last, idx);
420338fd1498Szrj if (CASE_LABEL (cl) == CASE_LABEL (default_cl))
420438fd1498Szrj continue;
420538fd1498Szrj
420638fd1498Szrj min = CASE_LOW (cl);
420738fd1498Szrj max = CASE_HIGH (cl);
420838fd1498Szrj
420938fd1498Szrj /* Combine contiguous case ranges to reduce the number of assertions
421038fd1498Szrj to insert. */
421138fd1498Szrj for (idx = idx + 1; idx < n; idx++)
421238fd1498Szrj {
421338fd1498Szrj tree next_min, next_max;
421438fd1498Szrj tree next_cl = gimple_switch_label (last, idx);
421538fd1498Szrj if (CASE_LABEL (next_cl) == CASE_LABEL (default_cl))
421638fd1498Szrj break;
421738fd1498Szrj
421838fd1498Szrj next_min = CASE_LOW (next_cl);
421938fd1498Szrj next_max = CASE_HIGH (next_cl);
422038fd1498Szrj
422138fd1498Szrj wide_int difference = (wi::to_wide (next_min)
422238fd1498Szrj - wi::to_wide (max ? max : min));
422338fd1498Szrj if (wi::eq_p (difference, 1))
422438fd1498Szrj max = next_max ? next_max : next_min;
422538fd1498Szrj else
422638fd1498Szrj break;
422738fd1498Szrj }
422838fd1498Szrj idx--;
422938fd1498Szrj
423038fd1498Szrj if (max == NULL_TREE)
423138fd1498Szrj {
423238fd1498Szrj /* Register the assertion OP != MIN. */
423338fd1498Szrj auto_vec<assert_info, 8> asserts;
423438fd1498Szrj min = fold_convert (TREE_TYPE (op), min);
423538fd1498Szrj register_edge_assert_for (op, default_edge, NE_EXPR, op, min,
423638fd1498Szrj asserts);
423738fd1498Szrj finish_register_edge_assert_for (default_edge, bsi, asserts);
423838fd1498Szrj }
423938fd1498Szrj else
424038fd1498Szrj {
424138fd1498Szrj /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
424238fd1498Szrj which will give OP the anti-range ~[MIN,MAX]. */
424338fd1498Szrj tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
424438fd1498Szrj min = fold_convert (TREE_TYPE (uop), min);
424538fd1498Szrj max = fold_convert (TREE_TYPE (uop), max);
424638fd1498Szrj
424738fd1498Szrj tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
424838fd1498Szrj tree rhs = int_const_binop (MINUS_EXPR, max, min);
424938fd1498Szrj register_new_assert_for (op, lhs, GT_EXPR, rhs,
425038fd1498Szrj NULL, default_edge, bsi);
425138fd1498Szrj }
425238fd1498Szrj
425338fd1498Szrj if (--insertion_limit == 0)
425438fd1498Szrj break;
425538fd1498Szrj }
425638fd1498Szrj }
425738fd1498Szrj
425838fd1498Szrj
425938fd1498Szrj /* Traverse all the statements in block BB looking for statements that
426038fd1498Szrj may generate useful assertions for the SSA names in their operand.
426138fd1498Szrj If a statement produces a useful assertion A for name N_i, then the
426238fd1498Szrj list of assertions already generated for N_i is scanned to
426338fd1498Szrj determine if A is actually needed.
426438fd1498Szrj
426538fd1498Szrj If N_i already had the assertion A at a location dominating the
426638fd1498Szrj current location, then nothing needs to be done. Otherwise, the
426738fd1498Szrj new location for A is recorded instead.
426838fd1498Szrj
426938fd1498Szrj 1- For every statement S in BB, all the variables used by S are
427038fd1498Szrj added to bitmap FOUND_IN_SUBGRAPH.
427138fd1498Szrj
427238fd1498Szrj 2- If statement S uses an operand N in a way that exposes a known
427338fd1498Szrj value range for N, then if N was not already generated by an
427438fd1498Szrj ASSERT_EXPR, create a new assert location for N. For instance,
427538fd1498Szrj if N is a pointer and the statement dereferences it, we can
427638fd1498Szrj assume that N is not NULL.
427738fd1498Szrj
427838fd1498Szrj 3- COND_EXPRs are a special case of #2. We can derive range
427938fd1498Szrj information from the predicate but need to insert different
428038fd1498Szrj ASSERT_EXPRs for each of the sub-graphs rooted at the
428138fd1498Szrj conditional block. If the last statement of BB is a conditional
428238fd1498Szrj expression of the form 'X op Y', then
428338fd1498Szrj
428438fd1498Szrj a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
428538fd1498Szrj
428638fd1498Szrj b) If the conditional is the only entry point to the sub-graph
428738fd1498Szrj corresponding to the THEN_CLAUSE, recurse into it. On
428838fd1498Szrj return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
428938fd1498Szrj an ASSERT_EXPR is added for the corresponding variable.
429038fd1498Szrj
429138fd1498Szrj c) Repeat step (b) on the ELSE_CLAUSE.
429238fd1498Szrj
429338fd1498Szrj d) Mark X and Y in FOUND_IN_SUBGRAPH.
429438fd1498Szrj
429538fd1498Szrj For instance,
429638fd1498Szrj
429738fd1498Szrj if (a == 9)
429838fd1498Szrj b = a;
429938fd1498Szrj else
430038fd1498Szrj b = c + 1;
430138fd1498Szrj
430238fd1498Szrj In this case, an assertion on the THEN clause is useful to
430338fd1498Szrj determine that 'a' is always 9 on that edge. However, an assertion
430438fd1498Szrj on the ELSE clause would be unnecessary.
430538fd1498Szrj
430638fd1498Szrj 4- If BB does not end in a conditional expression, then we recurse
430738fd1498Szrj into BB's dominator children.
430838fd1498Szrj
430938fd1498Szrj At the end of the recursive traversal, every SSA name will have a
431038fd1498Szrj list of locations where ASSERT_EXPRs should be added. When a new
431138fd1498Szrj location for name N is found, it is registered by calling
431238fd1498Szrj register_new_assert_for. That function keeps track of all the
431338fd1498Szrj registered assertions to prevent adding unnecessary assertions.
431438fd1498Szrj For instance, if a pointer P_4 is dereferenced more than once in a
431538fd1498Szrj dominator tree, only the location dominating all the dereference of
431638fd1498Szrj P_4 will receive an ASSERT_EXPR. */
431738fd1498Szrj
431838fd1498Szrj static void
find_assert_locations_1(basic_block bb,sbitmap live)431938fd1498Szrj find_assert_locations_1 (basic_block bb, sbitmap live)
432038fd1498Szrj {
432138fd1498Szrj gimple *last;
432238fd1498Szrj
432338fd1498Szrj last = last_stmt (bb);
432438fd1498Szrj
432538fd1498Szrj /* If BB's last statement is a conditional statement involving integer
432638fd1498Szrj operands, determine if we need to add ASSERT_EXPRs. */
432738fd1498Szrj if (last
432838fd1498Szrj && gimple_code (last) == GIMPLE_COND
432938fd1498Szrj && !fp_predicate (last)
433038fd1498Szrj && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
433138fd1498Szrj find_conditional_asserts (bb, as_a <gcond *> (last));
433238fd1498Szrj
433338fd1498Szrj /* If BB's last statement is a switch statement involving integer
433438fd1498Szrj operands, determine if we need to add ASSERT_EXPRs. */
433538fd1498Szrj if (last
433638fd1498Szrj && gimple_code (last) == GIMPLE_SWITCH
433738fd1498Szrj && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
433838fd1498Szrj find_switch_asserts (bb, as_a <gswitch *> (last));
433938fd1498Szrj
434038fd1498Szrj /* Traverse all the statements in BB marking used names and looking
434138fd1498Szrj for statements that may infer assertions for their used operands. */
434238fd1498Szrj for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
434338fd1498Szrj gsi_prev (&si))
434438fd1498Szrj {
434538fd1498Szrj gimple *stmt;
434638fd1498Szrj tree op;
434738fd1498Szrj ssa_op_iter i;
434838fd1498Szrj
434938fd1498Szrj stmt = gsi_stmt (si);
435038fd1498Szrj
435138fd1498Szrj if (is_gimple_debug (stmt))
435238fd1498Szrj continue;
435338fd1498Szrj
435438fd1498Szrj /* See if we can derive an assertion for any of STMT's operands. */
435538fd1498Szrj FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
435638fd1498Szrj {
435738fd1498Szrj tree value;
435838fd1498Szrj enum tree_code comp_code;
435938fd1498Szrj
436038fd1498Szrj /* If op is not live beyond this stmt, do not bother to insert
436138fd1498Szrj asserts for it. */
436238fd1498Szrj if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
436338fd1498Szrj continue;
436438fd1498Szrj
436538fd1498Szrj /* If OP is used in such a way that we can infer a value
436638fd1498Szrj range for it, and we don't find a previous assertion for
436738fd1498Szrj it, create a new assertion location node for OP. */
436838fd1498Szrj if (infer_value_range (stmt, op, &comp_code, &value))
436938fd1498Szrj {
437038fd1498Szrj /* If we are able to infer a nonzero value range for OP,
437138fd1498Szrj then walk backwards through the use-def chain to see if OP
437238fd1498Szrj was set via a typecast.
437338fd1498Szrj
437438fd1498Szrj If so, then we can also infer a nonzero value range
437538fd1498Szrj for the operand of the NOP_EXPR. */
437638fd1498Szrj if (comp_code == NE_EXPR && integer_zerop (value))
437738fd1498Szrj {
437838fd1498Szrj tree t = op;
437938fd1498Szrj gimple *def_stmt = SSA_NAME_DEF_STMT (t);
438038fd1498Szrj
438138fd1498Szrj while (is_gimple_assign (def_stmt)
438238fd1498Szrj && CONVERT_EXPR_CODE_P
438338fd1498Szrj (gimple_assign_rhs_code (def_stmt))
438438fd1498Szrj && TREE_CODE
438538fd1498Szrj (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
438638fd1498Szrj && POINTER_TYPE_P
438738fd1498Szrj (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
438838fd1498Szrj {
438938fd1498Szrj t = gimple_assign_rhs1 (def_stmt);
439038fd1498Szrj def_stmt = SSA_NAME_DEF_STMT (t);
439138fd1498Szrj
439238fd1498Szrj /* Note we want to register the assert for the
439338fd1498Szrj operand of the NOP_EXPR after SI, not after the
439438fd1498Szrj conversion. */
439538fd1498Szrj if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
439638fd1498Szrj register_new_assert_for (t, t, comp_code, value,
439738fd1498Szrj bb, NULL, si);
439838fd1498Szrj }
439938fd1498Szrj }
440038fd1498Szrj
440138fd1498Szrj register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
440238fd1498Szrj }
440338fd1498Szrj }
440438fd1498Szrj
440538fd1498Szrj /* Update live. */
440638fd1498Szrj FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
440738fd1498Szrj bitmap_set_bit (live, SSA_NAME_VERSION (op));
440838fd1498Szrj FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
440938fd1498Szrj bitmap_clear_bit (live, SSA_NAME_VERSION (op));
441038fd1498Szrj }
441138fd1498Szrj
441238fd1498Szrj /* Traverse all PHI nodes in BB, updating live. */
441338fd1498Szrj for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
441438fd1498Szrj gsi_next (&si))
441538fd1498Szrj {
441638fd1498Szrj use_operand_p arg_p;
441738fd1498Szrj ssa_op_iter i;
441838fd1498Szrj gphi *phi = si.phi ();
441938fd1498Szrj tree res = gimple_phi_result (phi);
442038fd1498Szrj
442138fd1498Szrj if (virtual_operand_p (res))
442238fd1498Szrj continue;
442338fd1498Szrj
442438fd1498Szrj FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
442538fd1498Szrj {
442638fd1498Szrj tree arg = USE_FROM_PTR (arg_p);
442738fd1498Szrj if (TREE_CODE (arg) == SSA_NAME)
442838fd1498Szrj bitmap_set_bit (live, SSA_NAME_VERSION (arg));
442938fd1498Szrj }
443038fd1498Szrj
443138fd1498Szrj bitmap_clear_bit (live, SSA_NAME_VERSION (res));
443238fd1498Szrj }
443338fd1498Szrj }
443438fd1498Szrj
443538fd1498Szrj /* Do an RPO walk over the function computing SSA name liveness
443638fd1498Szrj on-the-fly and deciding on assert expressions to insert. */
443738fd1498Szrj
443838fd1498Szrj static void
find_assert_locations(void)443938fd1498Szrj find_assert_locations (void)
444038fd1498Szrj {
444138fd1498Szrj int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
444238fd1498Szrj int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
444338fd1498Szrj int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
444438fd1498Szrj int rpo_cnt, i;
444538fd1498Szrj
444638fd1498Szrj live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
444738fd1498Szrj rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
444838fd1498Szrj for (i = 0; i < rpo_cnt; ++i)
444938fd1498Szrj bb_rpo[rpo[i]] = i;
445038fd1498Szrj
445138fd1498Szrj /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
445238fd1498Szrj the order we compute liveness and insert asserts we otherwise
445338fd1498Szrj fail to insert asserts into the loop latch. */
445438fd1498Szrj loop_p loop;
445538fd1498Szrj FOR_EACH_LOOP (loop, 0)
445638fd1498Szrj {
445738fd1498Szrj i = loop->latch->index;
445838fd1498Szrj unsigned int j = single_succ_edge (loop->latch)->dest_idx;
445938fd1498Szrj for (gphi_iterator gsi = gsi_start_phis (loop->header);
446038fd1498Szrj !gsi_end_p (gsi); gsi_next (&gsi))
446138fd1498Szrj {
446238fd1498Szrj gphi *phi = gsi.phi ();
446338fd1498Szrj if (virtual_operand_p (gimple_phi_result (phi)))
446438fd1498Szrj continue;
446538fd1498Szrj tree arg = gimple_phi_arg_def (phi, j);
446638fd1498Szrj if (TREE_CODE (arg) == SSA_NAME)
446738fd1498Szrj {
446838fd1498Szrj if (live[i] == NULL)
446938fd1498Szrj {
447038fd1498Szrj live[i] = sbitmap_alloc (num_ssa_names);
447138fd1498Szrj bitmap_clear (live[i]);
447238fd1498Szrj }
447338fd1498Szrj bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
447438fd1498Szrj }
447538fd1498Szrj }
447638fd1498Szrj }
447738fd1498Szrj
447838fd1498Szrj for (i = rpo_cnt - 1; i >= 0; --i)
447938fd1498Szrj {
448038fd1498Szrj basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
448138fd1498Szrj edge e;
448238fd1498Szrj edge_iterator ei;
448338fd1498Szrj
448438fd1498Szrj if (!live[rpo[i]])
448538fd1498Szrj {
448638fd1498Szrj live[rpo[i]] = sbitmap_alloc (num_ssa_names);
448738fd1498Szrj bitmap_clear (live[rpo[i]]);
448838fd1498Szrj }
448938fd1498Szrj
449038fd1498Szrj /* Process BB and update the live information with uses in
449138fd1498Szrj this block. */
449238fd1498Szrj find_assert_locations_1 (bb, live[rpo[i]]);
449338fd1498Szrj
449438fd1498Szrj /* Merge liveness into the predecessor blocks and free it. */
449538fd1498Szrj if (!bitmap_empty_p (live[rpo[i]]))
449638fd1498Szrj {
449738fd1498Szrj int pred_rpo = i;
449838fd1498Szrj FOR_EACH_EDGE (e, ei, bb->preds)
449938fd1498Szrj {
450038fd1498Szrj int pred = e->src->index;
450138fd1498Szrj if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
450238fd1498Szrj continue;
450338fd1498Szrj
450438fd1498Szrj if (!live[pred])
450538fd1498Szrj {
450638fd1498Szrj live[pred] = sbitmap_alloc (num_ssa_names);
450738fd1498Szrj bitmap_clear (live[pred]);
450838fd1498Szrj }
450938fd1498Szrj bitmap_ior (live[pred], live[pred], live[rpo[i]]);
451038fd1498Szrj
451138fd1498Szrj if (bb_rpo[pred] < pred_rpo)
451238fd1498Szrj pred_rpo = bb_rpo[pred];
451338fd1498Szrj }
451438fd1498Szrj
451538fd1498Szrj /* Record the RPO number of the last visited block that needs
451638fd1498Szrj live information from this block. */
451738fd1498Szrj last_rpo[rpo[i]] = pred_rpo;
451838fd1498Szrj }
451938fd1498Szrj else
452038fd1498Szrj {
452138fd1498Szrj sbitmap_free (live[rpo[i]]);
452238fd1498Szrj live[rpo[i]] = NULL;
452338fd1498Szrj }
452438fd1498Szrj
452538fd1498Szrj /* We can free all successors live bitmaps if all their
452638fd1498Szrj predecessors have been visited already. */
452738fd1498Szrj FOR_EACH_EDGE (e, ei, bb->succs)
452838fd1498Szrj if (last_rpo[e->dest->index] == i
452938fd1498Szrj && live[e->dest->index])
453038fd1498Szrj {
453138fd1498Szrj sbitmap_free (live[e->dest->index]);
453238fd1498Szrj live[e->dest->index] = NULL;
453338fd1498Szrj }
453438fd1498Szrj }
453538fd1498Szrj
453638fd1498Szrj XDELETEVEC (rpo);
453738fd1498Szrj XDELETEVEC (bb_rpo);
453838fd1498Szrj XDELETEVEC (last_rpo);
453938fd1498Szrj for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
454038fd1498Szrj if (live[i])
454138fd1498Szrj sbitmap_free (live[i]);
454238fd1498Szrj XDELETEVEC (live);
454338fd1498Szrj }
454438fd1498Szrj
454538fd1498Szrj /* Create an ASSERT_EXPR for NAME and insert it in the location
454638fd1498Szrj indicated by LOC. Return true if we made any edge insertions. */
454738fd1498Szrj
454838fd1498Szrj static bool
process_assert_insertions_for(tree name,assert_locus * loc)454938fd1498Szrj process_assert_insertions_for (tree name, assert_locus *loc)
455038fd1498Szrj {
455138fd1498Szrj /* Build the comparison expression NAME_i COMP_CODE VAL. */
455238fd1498Szrj gimple *stmt;
455338fd1498Szrj tree cond;
455438fd1498Szrj gimple *assert_stmt;
455538fd1498Szrj edge_iterator ei;
455638fd1498Szrj edge e;
455738fd1498Szrj
455838fd1498Szrj /* If we have X <=> X do not insert an assert expr for that. */
455938fd1498Szrj if (loc->expr == loc->val)
456038fd1498Szrj return false;
456138fd1498Szrj
456238fd1498Szrj cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
456338fd1498Szrj assert_stmt = build_assert_expr_for (cond, name);
456438fd1498Szrj if (loc->e)
456538fd1498Szrj {
456638fd1498Szrj /* We have been asked to insert the assertion on an edge. This
456738fd1498Szrj is used only by COND_EXPR and SWITCH_EXPR assertions. */
456838fd1498Szrj gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
456938fd1498Szrj || (gimple_code (gsi_stmt (loc->si))
457038fd1498Szrj == GIMPLE_SWITCH));
457138fd1498Szrj
457238fd1498Szrj gsi_insert_on_edge (loc->e, assert_stmt);
457338fd1498Szrj return true;
457438fd1498Szrj }
457538fd1498Szrj
457638fd1498Szrj /* If the stmt iterator points at the end then this is an insertion
457738fd1498Szrj at the beginning of a block. */
457838fd1498Szrj if (gsi_end_p (loc->si))
457938fd1498Szrj {
458038fd1498Szrj gimple_stmt_iterator si = gsi_after_labels (loc->bb);
458138fd1498Szrj gsi_insert_before (&si, assert_stmt, GSI_SAME_STMT);
458238fd1498Szrj return false;
458338fd1498Szrj
458438fd1498Szrj }
458538fd1498Szrj /* Otherwise, we can insert right after LOC->SI iff the
458638fd1498Szrj statement must not be the last statement in the block. */
458738fd1498Szrj stmt = gsi_stmt (loc->si);
458838fd1498Szrj if (!stmt_ends_bb_p (stmt))
458938fd1498Szrj {
459038fd1498Szrj gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
459138fd1498Szrj return false;
459238fd1498Szrj }
459338fd1498Szrj
459438fd1498Szrj /* If STMT must be the last statement in BB, we can only insert new
459538fd1498Szrj assertions on the non-abnormal edge out of BB. Note that since
459638fd1498Szrj STMT is not control flow, there may only be one non-abnormal/eh edge
459738fd1498Szrj out of BB. */
459838fd1498Szrj FOR_EACH_EDGE (e, ei, loc->bb->succs)
459938fd1498Szrj if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
460038fd1498Szrj {
460138fd1498Szrj gsi_insert_on_edge (e, assert_stmt);
460238fd1498Szrj return true;
460338fd1498Szrj }
460438fd1498Szrj
460538fd1498Szrj gcc_unreachable ();
460638fd1498Szrj }
460738fd1498Szrj
460838fd1498Szrj /* Qsort helper for sorting assert locations. If stable is true, don't
460938fd1498Szrj use iterative_hash_expr because it can be unstable for -fcompare-debug,
461038fd1498Szrj on the other side some pointers might be NULL. */
461138fd1498Szrj
461238fd1498Szrj template <bool stable>
461338fd1498Szrj static int
compare_assert_loc(const void * pa,const void * pb)461438fd1498Szrj compare_assert_loc (const void *pa, const void *pb)
461538fd1498Szrj {
461638fd1498Szrj assert_locus * const a = *(assert_locus * const *)pa;
461738fd1498Szrj assert_locus * const b = *(assert_locus * const *)pb;
461838fd1498Szrj
461938fd1498Szrj /* If stable, some asserts might be optimized away already, sort
462038fd1498Szrj them last. */
462138fd1498Szrj if (stable)
462238fd1498Szrj {
462338fd1498Szrj if (a == NULL)
462438fd1498Szrj return b != NULL;
462538fd1498Szrj else if (b == NULL)
462638fd1498Szrj return -1;
462738fd1498Szrj }
462838fd1498Szrj
462938fd1498Szrj if (a->e == NULL && b->e != NULL)
463038fd1498Szrj return 1;
463138fd1498Szrj else if (a->e != NULL && b->e == NULL)
463238fd1498Szrj return -1;
463338fd1498Szrj
463438fd1498Szrj /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
463538fd1498Szrj no need to test both a->e and b->e. */
463638fd1498Szrj
463738fd1498Szrj /* Sort after destination index. */
463838fd1498Szrj if (a->e == NULL)
463938fd1498Szrj ;
464038fd1498Szrj else if (a->e->dest->index > b->e->dest->index)
464138fd1498Szrj return 1;
464238fd1498Szrj else if (a->e->dest->index < b->e->dest->index)
464338fd1498Szrj return -1;
464438fd1498Szrj
464538fd1498Szrj /* Sort after comp_code. */
464638fd1498Szrj if (a->comp_code > b->comp_code)
464738fd1498Szrj return 1;
464838fd1498Szrj else if (a->comp_code < b->comp_code)
464938fd1498Szrj return -1;
465038fd1498Szrj
465138fd1498Szrj hashval_t ha, hb;
465238fd1498Szrj
465338fd1498Szrj /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
465438fd1498Szrj uses DECL_UID of the VAR_DECL, so sorting might differ between
465538fd1498Szrj -g and -g0. When doing the removal of redundant assert exprs
465638fd1498Szrj and commonization to successors, this does not matter, but for
465738fd1498Szrj the final sort needs to be stable. */
465838fd1498Szrj if (stable)
465938fd1498Szrj {
466038fd1498Szrj ha = 0;
466138fd1498Szrj hb = 0;
466238fd1498Szrj }
466338fd1498Szrj else
466438fd1498Szrj {
466538fd1498Szrj ha = iterative_hash_expr (a->expr, iterative_hash_expr (a->val, 0));
466638fd1498Szrj hb = iterative_hash_expr (b->expr, iterative_hash_expr (b->val, 0));
466738fd1498Szrj }
466838fd1498Szrj
466938fd1498Szrj /* Break the tie using hashing and source/bb index. */
467038fd1498Szrj if (ha == hb)
467138fd1498Szrj return (a->e != NULL
467238fd1498Szrj ? a->e->src->index - b->e->src->index
467338fd1498Szrj : a->bb->index - b->bb->index);
467438fd1498Szrj return ha > hb ? 1 : -1;
467538fd1498Szrj }
467638fd1498Szrj
467738fd1498Szrj /* Process all the insertions registered for every name N_i registered
467838fd1498Szrj in NEED_ASSERT_FOR. The list of assertions to be inserted are
467938fd1498Szrj found in ASSERTS_FOR[i]. */
468038fd1498Szrj
468138fd1498Szrj static void
process_assert_insertions(void)468238fd1498Szrj process_assert_insertions (void)
468338fd1498Szrj {
468438fd1498Szrj unsigned i;
468538fd1498Szrj bitmap_iterator bi;
468638fd1498Szrj bool update_edges_p = false;
468738fd1498Szrj int num_asserts = 0;
468838fd1498Szrj
468938fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
469038fd1498Szrj dump_all_asserts (dump_file);
469138fd1498Szrj
469238fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
469338fd1498Szrj {
469438fd1498Szrj assert_locus *loc = asserts_for[i];
469538fd1498Szrj gcc_assert (loc);
469638fd1498Szrj
469738fd1498Szrj auto_vec<assert_locus *, 16> asserts;
469838fd1498Szrj for (; loc; loc = loc->next)
469938fd1498Szrj asserts.safe_push (loc);
470038fd1498Szrj asserts.qsort (compare_assert_loc<false>);
470138fd1498Szrj
470238fd1498Szrj /* Push down common asserts to successors and remove redundant ones. */
470338fd1498Szrj unsigned ecnt = 0;
470438fd1498Szrj assert_locus *common = NULL;
470538fd1498Szrj unsigned commonj = 0;
470638fd1498Szrj for (unsigned j = 0; j < asserts.length (); ++j)
470738fd1498Szrj {
470838fd1498Szrj loc = asserts[j];
470938fd1498Szrj if (! loc->e)
471038fd1498Szrj common = NULL;
471138fd1498Szrj else if (! common
471238fd1498Szrj || loc->e->dest != common->e->dest
471338fd1498Szrj || loc->comp_code != common->comp_code
471438fd1498Szrj || ! operand_equal_p (loc->val, common->val, 0)
471538fd1498Szrj || ! operand_equal_p (loc->expr, common->expr, 0))
471638fd1498Szrj {
471738fd1498Szrj commonj = j;
471838fd1498Szrj common = loc;
471938fd1498Szrj ecnt = 1;
472038fd1498Szrj }
472138fd1498Szrj else if (loc->e == asserts[j-1]->e)
472238fd1498Szrj {
472338fd1498Szrj /* Remove duplicate asserts. */
472438fd1498Szrj if (commonj == j - 1)
472538fd1498Szrj {
472638fd1498Szrj commonj = j;
472738fd1498Szrj common = loc;
472838fd1498Szrj }
472938fd1498Szrj free (asserts[j-1]);
473038fd1498Szrj asserts[j-1] = NULL;
473138fd1498Szrj }
473238fd1498Szrj else
473338fd1498Szrj {
473438fd1498Szrj ecnt++;
473538fd1498Szrj if (EDGE_COUNT (common->e->dest->preds) == ecnt)
473638fd1498Szrj {
473738fd1498Szrj /* We have the same assertion on all incoming edges of a BB.
473838fd1498Szrj Insert it at the beginning of that block. */
473938fd1498Szrj loc->bb = loc->e->dest;
474038fd1498Szrj loc->e = NULL;
474138fd1498Szrj loc->si = gsi_none ();
474238fd1498Szrj common = NULL;
474338fd1498Szrj /* Clear asserts commoned. */
474438fd1498Szrj for (; commonj != j; ++commonj)
474538fd1498Szrj if (asserts[commonj])
474638fd1498Szrj {
474738fd1498Szrj free (asserts[commonj]);
474838fd1498Szrj asserts[commonj] = NULL;
474938fd1498Szrj }
475038fd1498Szrj }
475138fd1498Szrj }
475238fd1498Szrj }
475338fd1498Szrj
475438fd1498Szrj /* The asserts vector sorting above might be unstable for
475538fd1498Szrj -fcompare-debug, sort again to ensure a stable sort. */
475638fd1498Szrj asserts.qsort (compare_assert_loc<true>);
475738fd1498Szrj for (unsigned j = 0; j < asserts.length (); ++j)
475838fd1498Szrj {
475938fd1498Szrj loc = asserts[j];
476038fd1498Szrj if (! loc)
476138fd1498Szrj break;
476238fd1498Szrj update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
476338fd1498Szrj num_asserts++;
476438fd1498Szrj free (loc);
476538fd1498Szrj }
476638fd1498Szrj }
476738fd1498Szrj
476838fd1498Szrj if (update_edges_p)
476938fd1498Szrj gsi_commit_edge_inserts ();
477038fd1498Szrj
477138fd1498Szrj statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
477238fd1498Szrj num_asserts);
477338fd1498Szrj }
477438fd1498Szrj
477538fd1498Szrj
477638fd1498Szrj /* Traverse the flowgraph looking for conditional jumps to insert range
477738fd1498Szrj expressions. These range expressions are meant to provide information
477838fd1498Szrj to optimizations that need to reason in terms of value ranges. They
477938fd1498Szrj will not be expanded into RTL. For instance, given:
478038fd1498Szrj
478138fd1498Szrj x = ...
478238fd1498Szrj y = ...
478338fd1498Szrj if (x < y)
478438fd1498Szrj y = x - 2;
478538fd1498Szrj else
478638fd1498Szrj x = y + 3;
478738fd1498Szrj
478838fd1498Szrj this pass will transform the code into:
478938fd1498Szrj
479038fd1498Szrj x = ...
479138fd1498Szrj y = ...
479238fd1498Szrj if (x < y)
479338fd1498Szrj {
479438fd1498Szrj x = ASSERT_EXPR <x, x < y>
479538fd1498Szrj y = x - 2
479638fd1498Szrj }
479738fd1498Szrj else
479838fd1498Szrj {
479938fd1498Szrj y = ASSERT_EXPR <y, x >= y>
480038fd1498Szrj x = y + 3
480138fd1498Szrj }
480238fd1498Szrj
480338fd1498Szrj The idea is that once copy and constant propagation have run, other
480438fd1498Szrj optimizations will be able to determine what ranges of values can 'x'
480538fd1498Szrj take in different paths of the code, simply by checking the reaching
480638fd1498Szrj definition of 'x'. */
480738fd1498Szrj
480838fd1498Szrj static void
insert_range_assertions(void)480938fd1498Szrj insert_range_assertions (void)
481038fd1498Szrj {
481138fd1498Szrj need_assert_for = BITMAP_ALLOC (NULL);
481238fd1498Szrj asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
481338fd1498Szrj
481438fd1498Szrj calculate_dominance_info (CDI_DOMINATORS);
481538fd1498Szrj
481638fd1498Szrj find_assert_locations ();
481738fd1498Szrj if (!bitmap_empty_p (need_assert_for))
481838fd1498Szrj {
481938fd1498Szrj process_assert_insertions ();
482038fd1498Szrj update_ssa (TODO_update_ssa_no_phi);
482138fd1498Szrj }
482238fd1498Szrj
482338fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
482438fd1498Szrj {
482538fd1498Szrj fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
482638fd1498Szrj dump_function_to_file (current_function_decl, dump_file, dump_flags);
482738fd1498Szrj }
482838fd1498Szrj
482938fd1498Szrj free (asserts_for);
483038fd1498Szrj BITMAP_FREE (need_assert_for);
483138fd1498Szrj }
483238fd1498Szrj
483338fd1498Szrj class vrp_prop : public ssa_propagation_engine
483438fd1498Szrj {
483538fd1498Szrj public:
483638fd1498Szrj enum ssa_prop_result visit_stmt (gimple *, edge *, tree *) FINAL OVERRIDE;
483738fd1498Szrj enum ssa_prop_result visit_phi (gphi *) FINAL OVERRIDE;
483838fd1498Szrj
483938fd1498Szrj void vrp_initialize (void);
484038fd1498Szrj void vrp_finalize (bool);
484138fd1498Szrj void check_all_array_refs (void);
484238fd1498Szrj void check_array_ref (location_t, tree, bool);
484338fd1498Szrj void search_for_addr_array (tree, location_t);
484438fd1498Szrj
484538fd1498Szrj class vr_values vr_values;
484638fd1498Szrj /* Temporary delegator to minimize code churn. */
get_value_range(const_tree op)484738fd1498Szrj value_range *get_value_range (const_tree op)
484838fd1498Szrj { return vr_values.get_value_range (op); }
set_defs_to_varying(gimple * stmt)484938fd1498Szrj void set_defs_to_varying (gimple *stmt)
485038fd1498Szrj { return vr_values.set_defs_to_varying (stmt); }
extract_range_from_stmt(gimple * stmt,edge * taken_edge_p,tree * output_p,value_range * vr)485138fd1498Szrj void extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
485238fd1498Szrj tree *output_p, value_range *vr)
485338fd1498Szrj { vr_values.extract_range_from_stmt (stmt, taken_edge_p, output_p, vr); }
update_value_range(const_tree op,value_range * vr)485438fd1498Szrj bool update_value_range (const_tree op, value_range *vr)
485538fd1498Szrj { return vr_values.update_value_range (op, vr); }
extract_range_basic(value_range * vr,gimple * stmt)485638fd1498Szrj void extract_range_basic (value_range *vr, gimple *stmt)
485738fd1498Szrj { vr_values.extract_range_basic (vr, stmt); }
extract_range_from_phi_node(gphi * phi,value_range * vr)485838fd1498Szrj void extract_range_from_phi_node (gphi *phi, value_range *vr)
485938fd1498Szrj { vr_values.extract_range_from_phi_node (phi, vr); }
486038fd1498Szrj };
486138fd1498Szrj /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
486238fd1498Szrj and "struct" hacks. If VRP can determine that the
486338fd1498Szrj array subscript is a constant, check if it is outside valid
486438fd1498Szrj range. If the array subscript is a RANGE, warn if it is
486538fd1498Szrj non-overlapping with valid range.
486638fd1498Szrj IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
486738fd1498Szrj
486838fd1498Szrj void
check_array_ref(location_t location,tree ref,bool ignore_off_by_one)486938fd1498Szrj vrp_prop::check_array_ref (location_t location, tree ref,
487038fd1498Szrj bool ignore_off_by_one)
487138fd1498Szrj {
487238fd1498Szrj value_range *vr = NULL;
487338fd1498Szrj tree low_sub, up_sub;
487438fd1498Szrj tree low_bound, up_bound, up_bound_p1;
487538fd1498Szrj
487638fd1498Szrj if (TREE_NO_WARNING (ref))
487738fd1498Szrj return;
487838fd1498Szrj
487938fd1498Szrj low_sub = up_sub = TREE_OPERAND (ref, 1);
488038fd1498Szrj up_bound = array_ref_up_bound (ref);
488138fd1498Szrj
488238fd1498Szrj if (!up_bound
488338fd1498Szrj || TREE_CODE (up_bound) != INTEGER_CST
488438fd1498Szrj || (warn_array_bounds < 2
488538fd1498Szrj && array_at_struct_end_p (ref)))
488638fd1498Szrj {
488738fd1498Szrj /* Accesses to trailing arrays via pointers may access storage
488838fd1498Szrj beyond the types array bounds. For such arrays, or for flexible
488938fd1498Szrj array members, as well as for other arrays of an unknown size,
489038fd1498Szrj replace the upper bound with a more permissive one that assumes
489138fd1498Szrj the size of the largest object is PTRDIFF_MAX. */
489238fd1498Szrj tree eltsize = array_ref_element_size (ref);
489338fd1498Szrj
489438fd1498Szrj if (TREE_CODE (eltsize) != INTEGER_CST
489538fd1498Szrj || integer_zerop (eltsize))
489638fd1498Szrj {
489738fd1498Szrj up_bound = NULL_TREE;
489838fd1498Szrj up_bound_p1 = NULL_TREE;
489938fd1498Szrj }
490038fd1498Szrj else
490138fd1498Szrj {
490238fd1498Szrj tree maxbound = TYPE_MAX_VALUE (ptrdiff_type_node);
490338fd1498Szrj tree arg = TREE_OPERAND (ref, 0);
490438fd1498Szrj poly_int64 off;
490538fd1498Szrj
490638fd1498Szrj if (get_addr_base_and_unit_offset (arg, &off) && known_gt (off, 0))
490738fd1498Szrj maxbound = wide_int_to_tree (sizetype,
490838fd1498Szrj wi::sub (wi::to_wide (maxbound),
490938fd1498Szrj off));
491038fd1498Szrj else
491138fd1498Szrj maxbound = fold_convert (sizetype, maxbound);
491238fd1498Szrj
491338fd1498Szrj up_bound_p1 = int_const_binop (TRUNC_DIV_EXPR, maxbound, eltsize);
491438fd1498Szrj
491538fd1498Szrj up_bound = int_const_binop (MINUS_EXPR, up_bound_p1,
491638fd1498Szrj build_int_cst (ptrdiff_type_node, 1));
491738fd1498Szrj }
491838fd1498Szrj }
491938fd1498Szrj else
492038fd1498Szrj up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
492138fd1498Szrj build_int_cst (TREE_TYPE (up_bound), 1));
492238fd1498Szrj
492338fd1498Szrj low_bound = array_ref_low_bound (ref);
492438fd1498Szrj
492538fd1498Szrj tree artype = TREE_TYPE (TREE_OPERAND (ref, 0));
492638fd1498Szrj
492738fd1498Szrj /* Empty array. */
492838fd1498Szrj if (up_bound && tree_int_cst_equal (low_bound, up_bound_p1))
492938fd1498Szrj {
493038fd1498Szrj warning_at (location, OPT_Warray_bounds,
493138fd1498Szrj "array subscript %E is above array bounds of %qT",
493238fd1498Szrj low_bound, artype);
493338fd1498Szrj TREE_NO_WARNING (ref) = 1;
493438fd1498Szrj }
493538fd1498Szrj
493638fd1498Szrj if (TREE_CODE (low_sub) == SSA_NAME)
493738fd1498Szrj {
493838fd1498Szrj vr = get_value_range (low_sub);
493938fd1498Szrj if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
494038fd1498Szrj {
494138fd1498Szrj low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
494238fd1498Szrj up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
494338fd1498Szrj }
494438fd1498Szrj }
494538fd1498Szrj
494638fd1498Szrj if (vr && vr->type == VR_ANTI_RANGE)
494738fd1498Szrj {
494838fd1498Szrj if (up_bound
494938fd1498Szrj && TREE_CODE (up_sub) == INTEGER_CST
495038fd1498Szrj && (ignore_off_by_one
495138fd1498Szrj ? tree_int_cst_lt (up_bound, up_sub)
495238fd1498Szrj : tree_int_cst_le (up_bound, up_sub))
495338fd1498Szrj && TREE_CODE (low_sub) == INTEGER_CST
495438fd1498Szrj && tree_int_cst_le (low_sub, low_bound))
495538fd1498Szrj {
495638fd1498Szrj warning_at (location, OPT_Warray_bounds,
495738fd1498Szrj "array subscript [%E, %E] is outside array bounds of %qT",
495838fd1498Szrj low_sub, up_sub, artype);
495938fd1498Szrj TREE_NO_WARNING (ref) = 1;
496038fd1498Szrj }
496138fd1498Szrj }
496238fd1498Szrj else if (up_bound
496338fd1498Szrj && TREE_CODE (up_sub) == INTEGER_CST
496438fd1498Szrj && (ignore_off_by_one
496538fd1498Szrj ? !tree_int_cst_le (up_sub, up_bound_p1)
496638fd1498Szrj : !tree_int_cst_le (up_sub, up_bound)))
496738fd1498Szrj {
496838fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
496938fd1498Szrj {
497038fd1498Szrj fprintf (dump_file, "Array bound warning for ");
497138fd1498Szrj dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
497238fd1498Szrj fprintf (dump_file, "\n");
497338fd1498Szrj }
497438fd1498Szrj warning_at (location, OPT_Warray_bounds,
497538fd1498Szrj "array subscript %E is above array bounds of %qT",
497638fd1498Szrj up_sub, artype);
497738fd1498Szrj TREE_NO_WARNING (ref) = 1;
497838fd1498Szrj }
497938fd1498Szrj else if (TREE_CODE (low_sub) == INTEGER_CST
498038fd1498Szrj && tree_int_cst_lt (low_sub, low_bound))
498138fd1498Szrj {
498238fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
498338fd1498Szrj {
498438fd1498Szrj fprintf (dump_file, "Array bound warning for ");
498538fd1498Szrj dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
498638fd1498Szrj fprintf (dump_file, "\n");
498738fd1498Szrj }
498838fd1498Szrj warning_at (location, OPT_Warray_bounds,
498938fd1498Szrj "array subscript %E is below array bounds of %qT",
499038fd1498Szrj low_sub, artype);
499138fd1498Szrj TREE_NO_WARNING (ref) = 1;
499238fd1498Szrj }
499338fd1498Szrj }
499438fd1498Szrj
499538fd1498Szrj /* Searches if the expr T, located at LOCATION computes
499638fd1498Szrj address of an ARRAY_REF, and call check_array_ref on it. */
499738fd1498Szrj
499838fd1498Szrj void
search_for_addr_array(tree t,location_t location)499938fd1498Szrj vrp_prop::search_for_addr_array (tree t, location_t location)
500038fd1498Szrj {
500138fd1498Szrj /* Check each ARRAY_REFs in the reference chain. */
500238fd1498Szrj do
500338fd1498Szrj {
500438fd1498Szrj if (TREE_CODE (t) == ARRAY_REF)
500538fd1498Szrj check_array_ref (location, t, true /*ignore_off_by_one*/);
500638fd1498Szrj
500738fd1498Szrj t = TREE_OPERAND (t, 0);
500838fd1498Szrj }
500938fd1498Szrj while (handled_component_p (t));
501038fd1498Szrj
501138fd1498Szrj if (TREE_CODE (t) == MEM_REF
501238fd1498Szrj && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
501338fd1498Szrj && !TREE_NO_WARNING (t))
501438fd1498Szrj {
501538fd1498Szrj tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
501638fd1498Szrj tree low_bound, up_bound, el_sz;
501738fd1498Szrj offset_int idx;
501838fd1498Szrj if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
501938fd1498Szrj || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
502038fd1498Szrj || !TYPE_DOMAIN (TREE_TYPE (tem)))
502138fd1498Szrj return;
502238fd1498Szrj
502338fd1498Szrj low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
502438fd1498Szrj up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
502538fd1498Szrj el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
502638fd1498Szrj if (!low_bound
502738fd1498Szrj || TREE_CODE (low_bound) != INTEGER_CST
502838fd1498Szrj || !up_bound
502938fd1498Szrj || TREE_CODE (up_bound) != INTEGER_CST
503038fd1498Szrj || !el_sz
503138fd1498Szrj || TREE_CODE (el_sz) != INTEGER_CST)
503238fd1498Szrj return;
503338fd1498Szrj
503438fd1498Szrj if (!mem_ref_offset (t).is_constant (&idx))
503538fd1498Szrj return;
503638fd1498Szrj
503738fd1498Szrj idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
503838fd1498Szrj if (idx < 0)
503938fd1498Szrj {
504038fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
504138fd1498Szrj {
504238fd1498Szrj fprintf (dump_file, "Array bound warning for ");
504338fd1498Szrj dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
504438fd1498Szrj fprintf (dump_file, "\n");
504538fd1498Szrj }
504638fd1498Szrj warning_at (location, OPT_Warray_bounds,
504738fd1498Szrj "array subscript %wi is below array bounds of %qT",
504838fd1498Szrj idx.to_shwi (), TREE_TYPE (tem));
504938fd1498Szrj TREE_NO_WARNING (t) = 1;
505038fd1498Szrj }
505138fd1498Szrj else if (idx > (wi::to_offset (up_bound)
505238fd1498Szrj - wi::to_offset (low_bound) + 1))
505338fd1498Szrj {
505438fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
505538fd1498Szrj {
505638fd1498Szrj fprintf (dump_file, "Array bound warning for ");
505738fd1498Szrj dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
505838fd1498Szrj fprintf (dump_file, "\n");
505938fd1498Szrj }
506038fd1498Szrj warning_at (location, OPT_Warray_bounds,
506138fd1498Szrj "array subscript %wu is above array bounds of %qT",
506238fd1498Szrj idx.to_uhwi (), TREE_TYPE (tem));
506338fd1498Szrj TREE_NO_WARNING (t) = 1;
506438fd1498Szrj }
506538fd1498Szrj }
506638fd1498Szrj }
506738fd1498Szrj
506838fd1498Szrj /* walk_tree() callback that checks if *TP is
506938fd1498Szrj an ARRAY_REF inside an ADDR_EXPR (in which an array
507038fd1498Szrj subscript one outside the valid range is allowed). Call
507138fd1498Szrj check_array_ref for each ARRAY_REF found. The location is
507238fd1498Szrj passed in DATA. */
507338fd1498Szrj
507438fd1498Szrj static tree
check_array_bounds(tree * tp,int * walk_subtree,void * data)507538fd1498Szrj check_array_bounds (tree *tp, int *walk_subtree, void *data)
507638fd1498Szrj {
507738fd1498Szrj tree t = *tp;
507838fd1498Szrj struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
507938fd1498Szrj location_t location;
508038fd1498Szrj
508138fd1498Szrj if (EXPR_HAS_LOCATION (t))
508238fd1498Szrj location = EXPR_LOCATION (t);
508338fd1498Szrj else
508438fd1498Szrj location = gimple_location (wi->stmt);
508538fd1498Szrj
508638fd1498Szrj *walk_subtree = TRUE;
508738fd1498Szrj
508838fd1498Szrj vrp_prop *vrp_prop = (class vrp_prop *)wi->info;
508938fd1498Szrj if (TREE_CODE (t) == ARRAY_REF)
509038fd1498Szrj vrp_prop->check_array_ref (location, t, false /*ignore_off_by_one*/);
509138fd1498Szrj
509238fd1498Szrj else if (TREE_CODE (t) == ADDR_EXPR)
509338fd1498Szrj {
509438fd1498Szrj vrp_prop->search_for_addr_array (t, location);
509538fd1498Szrj *walk_subtree = FALSE;
509638fd1498Szrj }
509738fd1498Szrj
509838fd1498Szrj return NULL_TREE;
509938fd1498Szrj }
510038fd1498Szrj
510138fd1498Szrj /* A dom_walker subclass for use by vrp_prop::check_all_array_refs,
510238fd1498Szrj to walk over all statements of all reachable BBs and call
510338fd1498Szrj check_array_bounds on them. */
510438fd1498Szrj
510538fd1498Szrj class check_array_bounds_dom_walker : public dom_walker
510638fd1498Szrj {
510738fd1498Szrj public:
check_array_bounds_dom_walker(vrp_prop * prop)510838fd1498Szrj check_array_bounds_dom_walker (vrp_prop *prop)
510938fd1498Szrj : dom_walker (CDI_DOMINATORS,
511038fd1498Szrj /* Discover non-executable edges, preserving EDGE_EXECUTABLE
511138fd1498Szrj flags, so that we can merge in information on
511238fd1498Szrj non-executable edges from vrp_folder . */
511338fd1498Szrj REACHABLE_BLOCKS_PRESERVING_FLAGS),
511438fd1498Szrj m_prop (prop) {}
~check_array_bounds_dom_walker()511538fd1498Szrj ~check_array_bounds_dom_walker () {}
511638fd1498Szrj
511738fd1498Szrj edge before_dom_children (basic_block) FINAL OVERRIDE;
511838fd1498Szrj
511938fd1498Szrj private:
512038fd1498Szrj vrp_prop *m_prop;
512138fd1498Szrj };
512238fd1498Szrj
512338fd1498Szrj /* Implementation of dom_walker::before_dom_children.
512438fd1498Szrj
512538fd1498Szrj Walk over all statements of BB and call check_array_bounds on them,
512638fd1498Szrj and determine if there's a unique successor edge. */
512738fd1498Szrj
512838fd1498Szrj edge
before_dom_children(basic_block bb)512938fd1498Szrj check_array_bounds_dom_walker::before_dom_children (basic_block bb)
513038fd1498Szrj {
513138fd1498Szrj gimple_stmt_iterator si;
513238fd1498Szrj for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
513338fd1498Szrj {
513438fd1498Szrj gimple *stmt = gsi_stmt (si);
513538fd1498Szrj struct walk_stmt_info wi;
513638fd1498Szrj if (!gimple_has_location (stmt)
513738fd1498Szrj || is_gimple_debug (stmt))
513838fd1498Szrj continue;
513938fd1498Szrj
514038fd1498Szrj memset (&wi, 0, sizeof (wi));
514138fd1498Szrj
514238fd1498Szrj wi.info = m_prop;
514338fd1498Szrj
514438fd1498Szrj walk_gimple_op (stmt, check_array_bounds, &wi);
514538fd1498Szrj }
514638fd1498Szrj
514738fd1498Szrj /* Determine if there's a unique successor edge, and if so, return
514838fd1498Szrj that back to dom_walker, ensuring that we don't visit blocks that
514938fd1498Szrj became unreachable during the VRP propagation
515038fd1498Szrj (PR tree-optimization/83312). */
515138fd1498Szrj return find_taken_edge (bb, NULL_TREE);
515238fd1498Szrj }
515338fd1498Szrj
515438fd1498Szrj /* Walk over all statements of all reachable BBs and call check_array_bounds
515538fd1498Szrj on them. */
515638fd1498Szrj
515738fd1498Szrj void
check_all_array_refs()515838fd1498Szrj vrp_prop::check_all_array_refs ()
515938fd1498Szrj {
516038fd1498Szrj check_array_bounds_dom_walker w (this);
516138fd1498Szrj w.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
516238fd1498Szrj }
516338fd1498Szrj
516438fd1498Szrj /* Return true if all imm uses of VAR are either in STMT, or
516538fd1498Szrj feed (optionally through a chain of single imm uses) GIMPLE_COND
516638fd1498Szrj in basic block COND_BB. */
516738fd1498Szrj
516838fd1498Szrj static bool
all_imm_uses_in_stmt_or_feed_cond(tree var,gimple * stmt,basic_block cond_bb)516938fd1498Szrj all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
517038fd1498Szrj {
517138fd1498Szrj use_operand_p use_p, use2_p;
517238fd1498Szrj imm_use_iterator iter;
517338fd1498Szrj
517438fd1498Szrj FOR_EACH_IMM_USE_FAST (use_p, iter, var)
517538fd1498Szrj if (USE_STMT (use_p) != stmt)
517638fd1498Szrj {
517738fd1498Szrj gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
517838fd1498Szrj if (is_gimple_debug (use_stmt))
517938fd1498Szrj continue;
518038fd1498Szrj while (is_gimple_assign (use_stmt)
518138fd1498Szrj && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
518238fd1498Szrj && single_imm_use (gimple_assign_lhs (use_stmt),
518338fd1498Szrj &use2_p, &use_stmt2))
518438fd1498Szrj use_stmt = use_stmt2;
518538fd1498Szrj if (gimple_code (use_stmt) != GIMPLE_COND
518638fd1498Szrj || gimple_bb (use_stmt) != cond_bb)
518738fd1498Szrj return false;
518838fd1498Szrj }
518938fd1498Szrj return true;
519038fd1498Szrj }
519138fd1498Szrj
519238fd1498Szrj /* Handle
519338fd1498Szrj _4 = x_3 & 31;
519438fd1498Szrj if (_4 != 0)
519538fd1498Szrj goto <bb 6>;
519638fd1498Szrj else
519738fd1498Szrj goto <bb 7>;
519838fd1498Szrj <bb 6>:
519938fd1498Szrj __builtin_unreachable ();
520038fd1498Szrj <bb 7>:
520138fd1498Szrj x_5 = ASSERT_EXPR <x_3, ...>;
520238fd1498Szrj If x_3 has no other immediate uses (checked by caller),
520338fd1498Szrj var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
520438fd1498Szrj from the non-zero bitmask. */
520538fd1498Szrj
520638fd1498Szrj void
maybe_set_nonzero_bits(edge e,tree var)520738fd1498Szrj maybe_set_nonzero_bits (edge e, tree var)
520838fd1498Szrj {
520938fd1498Szrj basic_block cond_bb = e->src;
521038fd1498Szrj gimple *stmt = last_stmt (cond_bb);
521138fd1498Szrj tree cst;
521238fd1498Szrj
521338fd1498Szrj if (stmt == NULL
521438fd1498Szrj || gimple_code (stmt) != GIMPLE_COND
521538fd1498Szrj || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
521638fd1498Szrj ? EQ_EXPR : NE_EXPR)
521738fd1498Szrj || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
521838fd1498Szrj || !integer_zerop (gimple_cond_rhs (stmt)))
521938fd1498Szrj return;
522038fd1498Szrj
522138fd1498Szrj stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
522238fd1498Szrj if (!is_gimple_assign (stmt)
522338fd1498Szrj || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
522438fd1498Szrj || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
522538fd1498Szrj return;
522638fd1498Szrj if (gimple_assign_rhs1 (stmt) != var)
522738fd1498Szrj {
522838fd1498Szrj gimple *stmt2;
522938fd1498Szrj
523038fd1498Szrj if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
523138fd1498Szrj return;
523238fd1498Szrj stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
523338fd1498Szrj if (!gimple_assign_cast_p (stmt2)
523438fd1498Szrj || gimple_assign_rhs1 (stmt2) != var
523538fd1498Szrj || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
523638fd1498Szrj || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
523738fd1498Szrj != TYPE_PRECISION (TREE_TYPE (var))))
523838fd1498Szrj return;
523938fd1498Szrj }
524038fd1498Szrj cst = gimple_assign_rhs2 (stmt);
524138fd1498Szrj set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var),
524238fd1498Szrj wi::to_wide (cst)));
524338fd1498Szrj }
524438fd1498Szrj
524538fd1498Szrj /* Convert range assertion expressions into the implied copies and
524638fd1498Szrj copy propagate away the copies. Doing the trivial copy propagation
524738fd1498Szrj here avoids the need to run the full copy propagation pass after
524838fd1498Szrj VRP.
524938fd1498Szrj
525038fd1498Szrj FIXME, this will eventually lead to copy propagation removing the
525138fd1498Szrj names that had useful range information attached to them. For
525238fd1498Szrj instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
525338fd1498Szrj then N_i will have the range [3, +INF].
525438fd1498Szrj
525538fd1498Szrj However, by converting the assertion into the implied copy
525638fd1498Szrj operation N_i = N_j, we will then copy-propagate N_j into the uses
525738fd1498Szrj of N_i and lose the range information. We may want to hold on to
525838fd1498Szrj ASSERT_EXPRs a little while longer as the ranges could be used in
525938fd1498Szrj things like jump threading.
526038fd1498Szrj
526138fd1498Szrj The problem with keeping ASSERT_EXPRs around is that passes after
526238fd1498Szrj VRP need to handle them appropriately.
526338fd1498Szrj
526438fd1498Szrj Another approach would be to make the range information a first
526538fd1498Szrj class property of the SSA_NAME so that it can be queried from
526638fd1498Szrj any pass. This is made somewhat more complex by the need for
526738fd1498Szrj multiple ranges to be associated with one SSA_NAME. */
526838fd1498Szrj
526938fd1498Szrj static void
remove_range_assertions(void)527038fd1498Szrj remove_range_assertions (void)
527138fd1498Szrj {
527238fd1498Szrj basic_block bb;
527338fd1498Szrj gimple_stmt_iterator si;
527438fd1498Szrj /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
527538fd1498Szrj a basic block preceeded by GIMPLE_COND branching to it and
527638fd1498Szrj __builtin_trap, -1 if not yet checked, 0 otherwise. */
527738fd1498Szrj int is_unreachable;
527838fd1498Szrj
527938fd1498Szrj /* Note that the BSI iterator bump happens at the bottom of the
528038fd1498Szrj loop and no bump is necessary if we're removing the statement
528138fd1498Szrj referenced by the current BSI. */
528238fd1498Szrj FOR_EACH_BB_FN (bb, cfun)
528338fd1498Szrj for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
528438fd1498Szrj {
528538fd1498Szrj gimple *stmt = gsi_stmt (si);
528638fd1498Szrj
528738fd1498Szrj if (is_gimple_assign (stmt)
528838fd1498Szrj && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
528938fd1498Szrj {
529038fd1498Szrj tree lhs = gimple_assign_lhs (stmt);
529138fd1498Szrj tree rhs = gimple_assign_rhs1 (stmt);
529238fd1498Szrj tree var;
529338fd1498Szrj
529438fd1498Szrj var = ASSERT_EXPR_VAR (rhs);
529538fd1498Szrj
529638fd1498Szrj if (TREE_CODE (var) == SSA_NAME
529738fd1498Szrj && !POINTER_TYPE_P (TREE_TYPE (lhs))
529838fd1498Szrj && SSA_NAME_RANGE_INFO (lhs))
529938fd1498Szrj {
530038fd1498Szrj if (is_unreachable == -1)
530138fd1498Szrj {
530238fd1498Szrj is_unreachable = 0;
530338fd1498Szrj if (single_pred_p (bb)
530438fd1498Szrj && assert_unreachable_fallthru_edge_p
530538fd1498Szrj (single_pred_edge (bb)))
530638fd1498Szrj is_unreachable = 1;
530738fd1498Szrj }
530838fd1498Szrj /* Handle
530938fd1498Szrj if (x_7 >= 10 && x_7 < 20)
531038fd1498Szrj __builtin_unreachable ();
531138fd1498Szrj x_8 = ASSERT_EXPR <x_7, ...>;
531238fd1498Szrj if the only uses of x_7 are in the ASSERT_EXPR and
531338fd1498Szrj in the condition. In that case, we can copy the
531438fd1498Szrj range info from x_8 computed in this pass also
531538fd1498Szrj for x_7. */
531638fd1498Szrj if (is_unreachable
531738fd1498Szrj && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
531838fd1498Szrj single_pred (bb)))
531938fd1498Szrj {
532038fd1498Szrj set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
532138fd1498Szrj SSA_NAME_RANGE_INFO (lhs)->get_min (),
532238fd1498Szrj SSA_NAME_RANGE_INFO (lhs)->get_max ());
532338fd1498Szrj maybe_set_nonzero_bits (single_pred_edge (bb), var);
532438fd1498Szrj }
532538fd1498Szrj }
532638fd1498Szrj
532738fd1498Szrj /* Propagate the RHS into every use of the LHS. For SSA names
532838fd1498Szrj also propagate abnormals as it merely restores the original
532938fd1498Szrj IL in this case (an replace_uses_by would assert). */
533038fd1498Szrj if (TREE_CODE (var) == SSA_NAME)
533138fd1498Szrj {
533238fd1498Szrj imm_use_iterator iter;
533338fd1498Szrj use_operand_p use_p;
533438fd1498Szrj gimple *use_stmt;
533538fd1498Szrj FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
533638fd1498Szrj FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
533738fd1498Szrj SET_USE (use_p, var);
533838fd1498Szrj }
533938fd1498Szrj else
534038fd1498Szrj replace_uses_by (lhs, var);
534138fd1498Szrj
534238fd1498Szrj /* And finally, remove the copy, it is not needed. */
534338fd1498Szrj gsi_remove (&si, true);
534438fd1498Szrj release_defs (stmt);
534538fd1498Szrj }
534638fd1498Szrj else
534738fd1498Szrj {
534838fd1498Szrj if (!is_gimple_debug (gsi_stmt (si)))
534938fd1498Szrj is_unreachable = 0;
535038fd1498Szrj gsi_next (&si);
535138fd1498Szrj }
535238fd1498Szrj }
535338fd1498Szrj }
535438fd1498Szrj
535538fd1498Szrj /* Return true if STMT is interesting for VRP. */
535638fd1498Szrj
535738fd1498Szrj bool
stmt_interesting_for_vrp(gimple * stmt)535838fd1498Szrj stmt_interesting_for_vrp (gimple *stmt)
535938fd1498Szrj {
536038fd1498Szrj if (gimple_code (stmt) == GIMPLE_PHI)
536138fd1498Szrj {
536238fd1498Szrj tree res = gimple_phi_result (stmt);
536338fd1498Szrj return (!virtual_operand_p (res)
536438fd1498Szrj && (INTEGRAL_TYPE_P (TREE_TYPE (res))
536538fd1498Szrj || POINTER_TYPE_P (TREE_TYPE (res))));
536638fd1498Szrj }
536738fd1498Szrj else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
536838fd1498Szrj {
536938fd1498Szrj tree lhs = gimple_get_lhs (stmt);
537038fd1498Szrj
537138fd1498Szrj /* In general, assignments with virtual operands are not useful
537238fd1498Szrj for deriving ranges, with the obvious exception of calls to
537338fd1498Szrj builtin functions. */
537438fd1498Szrj if (lhs && TREE_CODE (lhs) == SSA_NAME
537538fd1498Szrj && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
537638fd1498Szrj || POINTER_TYPE_P (TREE_TYPE (lhs)))
537738fd1498Szrj && (is_gimple_call (stmt)
537838fd1498Szrj || !gimple_vuse (stmt)))
537938fd1498Szrj return true;
538038fd1498Szrj else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
538138fd1498Szrj switch (gimple_call_internal_fn (stmt))
538238fd1498Szrj {
538338fd1498Szrj case IFN_ADD_OVERFLOW:
538438fd1498Szrj case IFN_SUB_OVERFLOW:
538538fd1498Szrj case IFN_MUL_OVERFLOW:
538638fd1498Szrj case IFN_ATOMIC_COMPARE_EXCHANGE:
538738fd1498Szrj /* These internal calls return _Complex integer type,
538838fd1498Szrj but are interesting to VRP nevertheless. */
538938fd1498Szrj if (lhs && TREE_CODE (lhs) == SSA_NAME)
539038fd1498Szrj return true;
539138fd1498Szrj break;
539238fd1498Szrj default:
539338fd1498Szrj break;
539438fd1498Szrj }
539538fd1498Szrj }
539638fd1498Szrj else if (gimple_code (stmt) == GIMPLE_COND
539738fd1498Szrj || gimple_code (stmt) == GIMPLE_SWITCH)
539838fd1498Szrj return true;
539938fd1498Szrj
540038fd1498Szrj return false;
540138fd1498Szrj }
540238fd1498Szrj
540338fd1498Szrj /* Initialization required by ssa_propagate engine. */
540438fd1498Szrj
540538fd1498Szrj void
vrp_initialize()540638fd1498Szrj vrp_prop::vrp_initialize ()
540738fd1498Szrj {
540838fd1498Szrj basic_block bb;
540938fd1498Szrj
541038fd1498Szrj FOR_EACH_BB_FN (bb, cfun)
541138fd1498Szrj {
541238fd1498Szrj for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
541338fd1498Szrj gsi_next (&si))
541438fd1498Szrj {
541538fd1498Szrj gphi *phi = si.phi ();
541638fd1498Szrj if (!stmt_interesting_for_vrp (phi))
541738fd1498Szrj {
541838fd1498Szrj tree lhs = PHI_RESULT (phi);
541938fd1498Szrj set_value_range_to_varying (get_value_range (lhs));
542038fd1498Szrj prop_set_simulate_again (phi, false);
542138fd1498Szrj }
542238fd1498Szrj else
542338fd1498Szrj prop_set_simulate_again (phi, true);
542438fd1498Szrj }
542538fd1498Szrj
542638fd1498Szrj for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
542738fd1498Szrj gsi_next (&si))
542838fd1498Szrj {
542938fd1498Szrj gimple *stmt = gsi_stmt (si);
543038fd1498Szrj
543138fd1498Szrj /* If the statement is a control insn, then we do not
543238fd1498Szrj want to avoid simulating the statement once. Failure
543338fd1498Szrj to do so means that those edges will never get added. */
543438fd1498Szrj if (stmt_ends_bb_p (stmt))
543538fd1498Szrj prop_set_simulate_again (stmt, true);
543638fd1498Szrj else if (!stmt_interesting_for_vrp (stmt))
543738fd1498Szrj {
543838fd1498Szrj set_defs_to_varying (stmt);
543938fd1498Szrj prop_set_simulate_again (stmt, false);
544038fd1498Szrj }
544138fd1498Szrj else
544238fd1498Szrj prop_set_simulate_again (stmt, true);
544338fd1498Szrj }
544438fd1498Szrj }
544538fd1498Szrj }
544638fd1498Szrj
544738fd1498Szrj /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
544838fd1498Szrj that includes the value VAL. The search is restricted to the range
544938fd1498Szrj [START_IDX, n - 1] where n is the size of VEC.
545038fd1498Szrj
545138fd1498Szrj If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
545238fd1498Szrj returned.
545338fd1498Szrj
545438fd1498Szrj If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
545538fd1498Szrj it is placed in IDX and false is returned.
545638fd1498Szrj
545738fd1498Szrj If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
545838fd1498Szrj returned. */
545938fd1498Szrj
546038fd1498Szrj bool
find_case_label_index(gswitch * stmt,size_t start_idx,tree val,size_t * idx)546138fd1498Szrj find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
546238fd1498Szrj {
546338fd1498Szrj size_t n = gimple_switch_num_labels (stmt);
546438fd1498Szrj size_t low, high;
546538fd1498Szrj
546638fd1498Szrj /* Find case label for minimum of the value range or the next one.
546738fd1498Szrj At each iteration we are searching in [low, high - 1]. */
546838fd1498Szrj
546938fd1498Szrj for (low = start_idx, high = n; high != low; )
547038fd1498Szrj {
547138fd1498Szrj tree t;
547238fd1498Szrj int cmp;
547338fd1498Szrj /* Note that i != high, so we never ask for n. */
547438fd1498Szrj size_t i = (high + low) / 2;
547538fd1498Szrj t = gimple_switch_label (stmt, i);
547638fd1498Szrj
547738fd1498Szrj /* Cache the result of comparing CASE_LOW and val. */
547838fd1498Szrj cmp = tree_int_cst_compare (CASE_LOW (t), val);
547938fd1498Szrj
548038fd1498Szrj if (cmp == 0)
548138fd1498Szrj {
548238fd1498Szrj /* Ranges cannot be empty. */
548338fd1498Szrj *idx = i;
548438fd1498Szrj return true;
548538fd1498Szrj }
548638fd1498Szrj else if (cmp > 0)
548738fd1498Szrj high = i;
548838fd1498Szrj else
548938fd1498Szrj {
549038fd1498Szrj low = i + 1;
549138fd1498Szrj if (CASE_HIGH (t) != NULL
549238fd1498Szrj && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
549338fd1498Szrj {
549438fd1498Szrj *idx = i;
549538fd1498Szrj return true;
549638fd1498Szrj }
549738fd1498Szrj }
549838fd1498Szrj }
549938fd1498Szrj
550038fd1498Szrj *idx = high;
550138fd1498Szrj return false;
550238fd1498Szrj }
550338fd1498Szrj
550438fd1498Szrj /* Searches the case label vector VEC for the range of CASE_LABELs that is used
550538fd1498Szrj for values between MIN and MAX. The first index is placed in MIN_IDX. The
550638fd1498Szrj last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
550738fd1498Szrj then MAX_IDX < MIN_IDX.
550838fd1498Szrj Returns true if the default label is not needed. */
550938fd1498Szrj
551038fd1498Szrj bool
find_case_label_range(gswitch * stmt,tree min,tree max,size_t * min_idx,size_t * max_idx)551138fd1498Szrj find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
551238fd1498Szrj size_t *max_idx)
551338fd1498Szrj {
551438fd1498Szrj size_t i, j;
551538fd1498Szrj bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
551638fd1498Szrj bool max_take_default = !find_case_label_index (stmt, i, max, &j);
551738fd1498Szrj
551838fd1498Szrj if (i == j
551938fd1498Szrj && min_take_default
552038fd1498Szrj && max_take_default)
552138fd1498Szrj {
552238fd1498Szrj /* Only the default case label reached.
552338fd1498Szrj Return an empty range. */
552438fd1498Szrj *min_idx = 1;
552538fd1498Szrj *max_idx = 0;
552638fd1498Szrj return false;
552738fd1498Szrj }
552838fd1498Szrj else
552938fd1498Szrj {
553038fd1498Szrj bool take_default = min_take_default || max_take_default;
553138fd1498Szrj tree low, high;
553238fd1498Szrj size_t k;
553338fd1498Szrj
553438fd1498Szrj if (max_take_default)
553538fd1498Szrj j--;
553638fd1498Szrj
553738fd1498Szrj /* If the case label range is continuous, we do not need
553838fd1498Szrj the default case label. Verify that. */
553938fd1498Szrj high = CASE_LOW (gimple_switch_label (stmt, i));
554038fd1498Szrj if (CASE_HIGH (gimple_switch_label (stmt, i)))
554138fd1498Szrj high = CASE_HIGH (gimple_switch_label (stmt, i));
554238fd1498Szrj for (k = i + 1; k <= j; ++k)
554338fd1498Szrj {
554438fd1498Szrj low = CASE_LOW (gimple_switch_label (stmt, k));
554538fd1498Szrj if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
554638fd1498Szrj {
554738fd1498Szrj take_default = true;
554838fd1498Szrj break;
554938fd1498Szrj }
555038fd1498Szrj high = low;
555138fd1498Szrj if (CASE_HIGH (gimple_switch_label (stmt, k)))
555238fd1498Szrj high = CASE_HIGH (gimple_switch_label (stmt, k));
555338fd1498Szrj }
555438fd1498Szrj
555538fd1498Szrj *min_idx = i;
555638fd1498Szrj *max_idx = j;
555738fd1498Szrj return !take_default;
555838fd1498Szrj }
555938fd1498Szrj }
556038fd1498Szrj
556138fd1498Szrj /* Evaluate statement STMT. If the statement produces a useful range,
556238fd1498Szrj return SSA_PROP_INTERESTING and record the SSA name with the
556338fd1498Szrj interesting range into *OUTPUT_P.
556438fd1498Szrj
556538fd1498Szrj If STMT is a conditional branch and we can determine its truth
556638fd1498Szrj value, the taken edge is recorded in *TAKEN_EDGE_P.
556738fd1498Szrj
556838fd1498Szrj If STMT produces a varying value, return SSA_PROP_VARYING. */
556938fd1498Szrj
557038fd1498Szrj enum ssa_prop_result
visit_stmt(gimple * stmt,edge * taken_edge_p,tree * output_p)557138fd1498Szrj vrp_prop::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
557238fd1498Szrj {
557338fd1498Szrj value_range vr = VR_INITIALIZER;
557438fd1498Szrj tree lhs = gimple_get_lhs (stmt);
557538fd1498Szrj extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
557638fd1498Szrj
557738fd1498Szrj if (*output_p)
557838fd1498Szrj {
557938fd1498Szrj if (update_value_range (*output_p, &vr))
558038fd1498Szrj {
558138fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
558238fd1498Szrj {
558338fd1498Szrj fprintf (dump_file, "Found new range for ");
558438fd1498Szrj print_generic_expr (dump_file, *output_p);
558538fd1498Szrj fprintf (dump_file, ": ");
558638fd1498Szrj dump_value_range (dump_file, &vr);
558738fd1498Szrj fprintf (dump_file, "\n");
558838fd1498Szrj }
558938fd1498Szrj
559038fd1498Szrj if (vr.type == VR_VARYING)
559138fd1498Szrj return SSA_PROP_VARYING;
559238fd1498Szrj
559338fd1498Szrj return SSA_PROP_INTERESTING;
559438fd1498Szrj }
559538fd1498Szrj return SSA_PROP_NOT_INTERESTING;
559638fd1498Szrj }
559738fd1498Szrj
559838fd1498Szrj if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
559938fd1498Szrj switch (gimple_call_internal_fn (stmt))
560038fd1498Szrj {
560138fd1498Szrj case IFN_ADD_OVERFLOW:
560238fd1498Szrj case IFN_SUB_OVERFLOW:
560338fd1498Szrj case IFN_MUL_OVERFLOW:
560438fd1498Szrj case IFN_ATOMIC_COMPARE_EXCHANGE:
560538fd1498Szrj /* These internal calls return _Complex integer type,
560638fd1498Szrj which VRP does not track, but the immediate uses
560738fd1498Szrj thereof might be interesting. */
560838fd1498Szrj if (lhs && TREE_CODE (lhs) == SSA_NAME)
560938fd1498Szrj {
561038fd1498Szrj imm_use_iterator iter;
561138fd1498Szrj use_operand_p use_p;
561238fd1498Szrj enum ssa_prop_result res = SSA_PROP_VARYING;
561338fd1498Szrj
561438fd1498Szrj set_value_range_to_varying (get_value_range (lhs));
561538fd1498Szrj
561638fd1498Szrj FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
561738fd1498Szrj {
561838fd1498Szrj gimple *use_stmt = USE_STMT (use_p);
561938fd1498Szrj if (!is_gimple_assign (use_stmt))
562038fd1498Szrj continue;
562138fd1498Szrj enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
562238fd1498Szrj if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
562338fd1498Szrj continue;
562438fd1498Szrj tree rhs1 = gimple_assign_rhs1 (use_stmt);
562538fd1498Szrj tree use_lhs = gimple_assign_lhs (use_stmt);
562638fd1498Szrj if (TREE_CODE (rhs1) != rhs_code
562738fd1498Szrj || TREE_OPERAND (rhs1, 0) != lhs
562838fd1498Szrj || TREE_CODE (use_lhs) != SSA_NAME
562938fd1498Szrj || !stmt_interesting_for_vrp (use_stmt)
563038fd1498Szrj || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
563138fd1498Szrj || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
563238fd1498Szrj || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
563338fd1498Szrj continue;
563438fd1498Szrj
563538fd1498Szrj /* If there is a change in the value range for any of the
563638fd1498Szrj REALPART_EXPR/IMAGPART_EXPR immediate uses, return
563738fd1498Szrj SSA_PROP_INTERESTING. If there are any REALPART_EXPR
563838fd1498Szrj or IMAGPART_EXPR immediate uses, but none of them have
563938fd1498Szrj a change in their value ranges, return
564038fd1498Szrj SSA_PROP_NOT_INTERESTING. If there are no
564138fd1498Szrj {REAL,IMAG}PART_EXPR uses at all,
564238fd1498Szrj return SSA_PROP_VARYING. */
564338fd1498Szrj value_range new_vr = VR_INITIALIZER;
564438fd1498Szrj extract_range_basic (&new_vr, use_stmt);
564538fd1498Szrj value_range *old_vr = get_value_range (use_lhs);
564638fd1498Szrj if (old_vr->type != new_vr.type
564738fd1498Szrj || !vrp_operand_equal_p (old_vr->min, new_vr.min)
564838fd1498Szrj || !vrp_operand_equal_p (old_vr->max, new_vr.max)
564938fd1498Szrj || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
565038fd1498Szrj res = SSA_PROP_INTERESTING;
565138fd1498Szrj else
565238fd1498Szrj res = SSA_PROP_NOT_INTERESTING;
565338fd1498Szrj BITMAP_FREE (new_vr.equiv);
565438fd1498Szrj if (res == SSA_PROP_INTERESTING)
565538fd1498Szrj {
565638fd1498Szrj *output_p = lhs;
565738fd1498Szrj return res;
565838fd1498Szrj }
565938fd1498Szrj }
566038fd1498Szrj
566138fd1498Szrj return res;
566238fd1498Szrj }
566338fd1498Szrj break;
566438fd1498Szrj default:
566538fd1498Szrj break;
566638fd1498Szrj }
566738fd1498Szrj
566838fd1498Szrj /* All other statements produce nothing of interest for VRP, so mark
566938fd1498Szrj their outputs varying and prevent further simulation. */
567038fd1498Szrj set_defs_to_varying (stmt);
567138fd1498Szrj
567238fd1498Szrj return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
567338fd1498Szrj }
567438fd1498Szrj
567538fd1498Szrj /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
567638fd1498Szrj { VR1TYPE, VR0MIN, VR0MAX } and store the result
567738fd1498Szrj in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
567838fd1498Szrj possible such range. The resulting range is not canonicalized. */
567938fd1498Szrj
568038fd1498Szrj static void
union_ranges(enum value_range_type * vr0type,tree * vr0min,tree * vr0max,enum value_range_type vr1type,tree vr1min,tree vr1max)568138fd1498Szrj union_ranges (enum value_range_type *vr0type,
568238fd1498Szrj tree *vr0min, tree *vr0max,
568338fd1498Szrj enum value_range_type vr1type,
568438fd1498Szrj tree vr1min, tree vr1max)
568538fd1498Szrj {
568638fd1498Szrj bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
568738fd1498Szrj bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
568838fd1498Szrj
568938fd1498Szrj /* [] is vr0, () is vr1 in the following classification comments. */
569038fd1498Szrj if (mineq && maxeq)
569138fd1498Szrj {
569238fd1498Szrj /* [( )] */
569338fd1498Szrj if (*vr0type == vr1type)
569438fd1498Szrj /* Nothing to do for equal ranges. */
569538fd1498Szrj ;
569638fd1498Szrj else if ((*vr0type == VR_RANGE
569738fd1498Szrj && vr1type == VR_ANTI_RANGE)
569838fd1498Szrj || (*vr0type == VR_ANTI_RANGE
569938fd1498Szrj && vr1type == VR_RANGE))
570038fd1498Szrj {
570138fd1498Szrj /* For anti-range with range union the result is varying. */
570238fd1498Szrj goto give_up;
570338fd1498Szrj }
570438fd1498Szrj else
570538fd1498Szrj gcc_unreachable ();
570638fd1498Szrj }
570738fd1498Szrj else if (operand_less_p (*vr0max, vr1min) == 1
570838fd1498Szrj || operand_less_p (vr1max, *vr0min) == 1)
570938fd1498Szrj {
571038fd1498Szrj /* [ ] ( ) or ( ) [ ]
571138fd1498Szrj If the ranges have an empty intersection, result of the union
571238fd1498Szrj operation is the anti-range or if both are anti-ranges
571338fd1498Szrj it covers all. */
571438fd1498Szrj if (*vr0type == VR_ANTI_RANGE
571538fd1498Szrj && vr1type == VR_ANTI_RANGE)
571638fd1498Szrj goto give_up;
571738fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
571838fd1498Szrj && vr1type == VR_RANGE)
571938fd1498Szrj ;
572038fd1498Szrj else if (*vr0type == VR_RANGE
572138fd1498Szrj && vr1type == VR_ANTI_RANGE)
572238fd1498Szrj {
572338fd1498Szrj *vr0type = vr1type;
572438fd1498Szrj *vr0min = vr1min;
572538fd1498Szrj *vr0max = vr1max;
572638fd1498Szrj }
572738fd1498Szrj else if (*vr0type == VR_RANGE
572838fd1498Szrj && vr1type == VR_RANGE)
572938fd1498Szrj {
573038fd1498Szrj /* The result is the convex hull of both ranges. */
573138fd1498Szrj if (operand_less_p (*vr0max, vr1min) == 1)
573238fd1498Szrj {
573338fd1498Szrj /* If the result can be an anti-range, create one. */
573438fd1498Szrj if (TREE_CODE (*vr0max) == INTEGER_CST
573538fd1498Szrj && TREE_CODE (vr1min) == INTEGER_CST
573638fd1498Szrj && vrp_val_is_min (*vr0min)
573738fd1498Szrj && vrp_val_is_max (vr1max))
573838fd1498Szrj {
573938fd1498Szrj tree min = int_const_binop (PLUS_EXPR,
574038fd1498Szrj *vr0max,
574138fd1498Szrj build_int_cst (TREE_TYPE (*vr0max), 1));
574238fd1498Szrj tree max = int_const_binop (MINUS_EXPR,
574338fd1498Szrj vr1min,
574438fd1498Szrj build_int_cst (TREE_TYPE (vr1min), 1));
574538fd1498Szrj if (!operand_less_p (max, min))
574638fd1498Szrj {
574738fd1498Szrj *vr0type = VR_ANTI_RANGE;
574838fd1498Szrj *vr0min = min;
574938fd1498Szrj *vr0max = max;
575038fd1498Szrj }
575138fd1498Szrj else
575238fd1498Szrj *vr0max = vr1max;
575338fd1498Szrj }
575438fd1498Szrj else
575538fd1498Szrj *vr0max = vr1max;
575638fd1498Szrj }
575738fd1498Szrj else
575838fd1498Szrj {
575938fd1498Szrj /* If the result can be an anti-range, create one. */
576038fd1498Szrj if (TREE_CODE (vr1max) == INTEGER_CST
576138fd1498Szrj && TREE_CODE (*vr0min) == INTEGER_CST
576238fd1498Szrj && vrp_val_is_min (vr1min)
576338fd1498Szrj && vrp_val_is_max (*vr0max))
576438fd1498Szrj {
576538fd1498Szrj tree min = int_const_binop (PLUS_EXPR,
576638fd1498Szrj vr1max,
576738fd1498Szrj build_int_cst (TREE_TYPE (vr1max), 1));
576838fd1498Szrj tree max = int_const_binop (MINUS_EXPR,
576938fd1498Szrj *vr0min,
577038fd1498Szrj build_int_cst (TREE_TYPE (*vr0min), 1));
577138fd1498Szrj if (!operand_less_p (max, min))
577238fd1498Szrj {
577338fd1498Szrj *vr0type = VR_ANTI_RANGE;
577438fd1498Szrj *vr0min = min;
577538fd1498Szrj *vr0max = max;
577638fd1498Szrj }
577738fd1498Szrj else
577838fd1498Szrj *vr0min = vr1min;
577938fd1498Szrj }
578038fd1498Szrj else
578138fd1498Szrj *vr0min = vr1min;
578238fd1498Szrj }
578338fd1498Szrj }
578438fd1498Szrj else
578538fd1498Szrj gcc_unreachable ();
578638fd1498Szrj }
578738fd1498Szrj else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
578838fd1498Szrj && (mineq || operand_less_p (*vr0min, vr1min) == 1))
578938fd1498Szrj {
579038fd1498Szrj /* [ ( ) ] or [( ) ] or [ ( )] */
579138fd1498Szrj if (*vr0type == VR_RANGE
579238fd1498Szrj && vr1type == VR_RANGE)
579338fd1498Szrj ;
579438fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
579538fd1498Szrj && vr1type == VR_ANTI_RANGE)
579638fd1498Szrj {
579738fd1498Szrj *vr0type = vr1type;
579838fd1498Szrj *vr0min = vr1min;
579938fd1498Szrj *vr0max = vr1max;
580038fd1498Szrj }
580138fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
580238fd1498Szrj && vr1type == VR_RANGE)
580338fd1498Szrj {
580438fd1498Szrj /* Arbitrarily choose the right or left gap. */
580538fd1498Szrj if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
580638fd1498Szrj *vr0max = int_const_binop (MINUS_EXPR, vr1min,
580738fd1498Szrj build_int_cst (TREE_TYPE (vr1min), 1));
580838fd1498Szrj else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
580938fd1498Szrj *vr0min = int_const_binop (PLUS_EXPR, vr1max,
581038fd1498Szrj build_int_cst (TREE_TYPE (vr1max), 1));
581138fd1498Szrj else
581238fd1498Szrj goto give_up;
581338fd1498Szrj }
581438fd1498Szrj else if (*vr0type == VR_RANGE
581538fd1498Szrj && vr1type == VR_ANTI_RANGE)
581638fd1498Szrj /* The result covers everything. */
581738fd1498Szrj goto give_up;
581838fd1498Szrj else
581938fd1498Szrj gcc_unreachable ();
582038fd1498Szrj }
582138fd1498Szrj else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
582238fd1498Szrj && (mineq || operand_less_p (vr1min, *vr0min) == 1))
582338fd1498Szrj {
582438fd1498Szrj /* ( [ ] ) or ([ ] ) or ( [ ]) */
582538fd1498Szrj if (*vr0type == VR_RANGE
582638fd1498Szrj && vr1type == VR_RANGE)
582738fd1498Szrj {
582838fd1498Szrj *vr0type = vr1type;
582938fd1498Szrj *vr0min = vr1min;
583038fd1498Szrj *vr0max = vr1max;
583138fd1498Szrj }
583238fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
583338fd1498Szrj && vr1type == VR_ANTI_RANGE)
583438fd1498Szrj ;
583538fd1498Szrj else if (*vr0type == VR_RANGE
583638fd1498Szrj && vr1type == VR_ANTI_RANGE)
583738fd1498Szrj {
583838fd1498Szrj *vr0type = VR_ANTI_RANGE;
583938fd1498Szrj if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
584038fd1498Szrj {
584138fd1498Szrj *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
584238fd1498Szrj build_int_cst (TREE_TYPE (*vr0min), 1));
584338fd1498Szrj *vr0min = vr1min;
584438fd1498Szrj }
584538fd1498Szrj else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
584638fd1498Szrj {
584738fd1498Szrj *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
584838fd1498Szrj build_int_cst (TREE_TYPE (*vr0max), 1));
584938fd1498Szrj *vr0max = vr1max;
585038fd1498Szrj }
585138fd1498Szrj else
585238fd1498Szrj goto give_up;
585338fd1498Szrj }
585438fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
585538fd1498Szrj && vr1type == VR_RANGE)
585638fd1498Szrj /* The result covers everything. */
585738fd1498Szrj goto give_up;
585838fd1498Szrj else
585938fd1498Szrj gcc_unreachable ();
586038fd1498Szrj }
586138fd1498Szrj else if ((operand_less_p (vr1min, *vr0max) == 1
586238fd1498Szrj || operand_equal_p (vr1min, *vr0max, 0))
586338fd1498Szrj && operand_less_p (*vr0min, vr1min) == 1
586438fd1498Szrj && operand_less_p (*vr0max, vr1max) == 1)
586538fd1498Szrj {
586638fd1498Szrj /* [ ( ] ) or [ ]( ) */
586738fd1498Szrj if (*vr0type == VR_RANGE
586838fd1498Szrj && vr1type == VR_RANGE)
586938fd1498Szrj *vr0max = vr1max;
587038fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
587138fd1498Szrj && vr1type == VR_ANTI_RANGE)
587238fd1498Szrj *vr0min = vr1min;
587338fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
587438fd1498Szrj && vr1type == VR_RANGE)
587538fd1498Szrj {
587638fd1498Szrj if (TREE_CODE (vr1min) == INTEGER_CST)
587738fd1498Szrj *vr0max = int_const_binop (MINUS_EXPR, vr1min,
587838fd1498Szrj build_int_cst (TREE_TYPE (vr1min), 1));
587938fd1498Szrj else
588038fd1498Szrj goto give_up;
588138fd1498Szrj }
588238fd1498Szrj else if (*vr0type == VR_RANGE
588338fd1498Szrj && vr1type == VR_ANTI_RANGE)
588438fd1498Szrj {
588538fd1498Szrj if (TREE_CODE (*vr0max) == INTEGER_CST)
588638fd1498Szrj {
588738fd1498Szrj *vr0type = vr1type;
588838fd1498Szrj *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
588938fd1498Szrj build_int_cst (TREE_TYPE (*vr0max), 1));
589038fd1498Szrj *vr0max = vr1max;
589138fd1498Szrj }
589238fd1498Szrj else
589338fd1498Szrj goto give_up;
589438fd1498Szrj }
589538fd1498Szrj else
589638fd1498Szrj gcc_unreachable ();
589738fd1498Szrj }
589838fd1498Szrj else if ((operand_less_p (*vr0min, vr1max) == 1
589938fd1498Szrj || operand_equal_p (*vr0min, vr1max, 0))
590038fd1498Szrj && operand_less_p (vr1min, *vr0min) == 1
590138fd1498Szrj && operand_less_p (vr1max, *vr0max) == 1)
590238fd1498Szrj {
590338fd1498Szrj /* ( [ ) ] or ( )[ ] */
590438fd1498Szrj if (*vr0type == VR_RANGE
590538fd1498Szrj && vr1type == VR_RANGE)
590638fd1498Szrj *vr0min = vr1min;
590738fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
590838fd1498Szrj && vr1type == VR_ANTI_RANGE)
590938fd1498Szrj *vr0max = vr1max;
591038fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
591138fd1498Szrj && vr1type == VR_RANGE)
591238fd1498Szrj {
591338fd1498Szrj if (TREE_CODE (vr1max) == INTEGER_CST)
591438fd1498Szrj *vr0min = int_const_binop (PLUS_EXPR, vr1max,
591538fd1498Szrj build_int_cst (TREE_TYPE (vr1max), 1));
591638fd1498Szrj else
591738fd1498Szrj goto give_up;
591838fd1498Szrj }
591938fd1498Szrj else if (*vr0type == VR_RANGE
592038fd1498Szrj && vr1type == VR_ANTI_RANGE)
592138fd1498Szrj {
592238fd1498Szrj if (TREE_CODE (*vr0min) == INTEGER_CST)
592338fd1498Szrj {
592438fd1498Szrj *vr0type = vr1type;
592538fd1498Szrj *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
592638fd1498Szrj build_int_cst (TREE_TYPE (*vr0min), 1));
5927*58e805e6Szrj *vr0min = vr1min;
592838fd1498Szrj }
592938fd1498Szrj else
593038fd1498Szrj goto give_up;
593138fd1498Szrj }
593238fd1498Szrj else
593338fd1498Szrj gcc_unreachable ();
593438fd1498Szrj }
593538fd1498Szrj else
593638fd1498Szrj goto give_up;
593738fd1498Szrj
593838fd1498Szrj return;
593938fd1498Szrj
594038fd1498Szrj give_up:
594138fd1498Szrj *vr0type = VR_VARYING;
594238fd1498Szrj *vr0min = NULL_TREE;
594338fd1498Szrj *vr0max = NULL_TREE;
594438fd1498Szrj }
594538fd1498Szrj
594638fd1498Szrj /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
594738fd1498Szrj { VR1TYPE, VR0MIN, VR0MAX } and store the result
594838fd1498Szrj in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
594938fd1498Szrj possible such range. The resulting range is not canonicalized. */
595038fd1498Szrj
595138fd1498Szrj static void
intersect_ranges(enum value_range_type * vr0type,tree * vr0min,tree * vr0max,enum value_range_type vr1type,tree vr1min,tree vr1max)595238fd1498Szrj intersect_ranges (enum value_range_type *vr0type,
595338fd1498Szrj tree *vr0min, tree *vr0max,
595438fd1498Szrj enum value_range_type vr1type,
595538fd1498Szrj tree vr1min, tree vr1max)
595638fd1498Szrj {
595738fd1498Szrj bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
595838fd1498Szrj bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
595938fd1498Szrj
596038fd1498Szrj /* [] is vr0, () is vr1 in the following classification comments. */
596138fd1498Szrj if (mineq && maxeq)
596238fd1498Szrj {
596338fd1498Szrj /* [( )] */
596438fd1498Szrj if (*vr0type == vr1type)
596538fd1498Szrj /* Nothing to do for equal ranges. */
596638fd1498Szrj ;
596738fd1498Szrj else if ((*vr0type == VR_RANGE
596838fd1498Szrj && vr1type == VR_ANTI_RANGE)
596938fd1498Szrj || (*vr0type == VR_ANTI_RANGE
597038fd1498Szrj && vr1type == VR_RANGE))
597138fd1498Szrj {
597238fd1498Szrj /* For anti-range with range intersection the result is empty. */
597338fd1498Szrj *vr0type = VR_UNDEFINED;
597438fd1498Szrj *vr0min = NULL_TREE;
597538fd1498Szrj *vr0max = NULL_TREE;
597638fd1498Szrj }
597738fd1498Szrj else
597838fd1498Szrj gcc_unreachable ();
597938fd1498Szrj }
598038fd1498Szrj else if (operand_less_p (*vr0max, vr1min) == 1
598138fd1498Szrj || operand_less_p (vr1max, *vr0min) == 1)
598238fd1498Szrj {
598338fd1498Szrj /* [ ] ( ) or ( ) [ ]
598438fd1498Szrj If the ranges have an empty intersection, the result of the
598538fd1498Szrj intersect operation is the range for intersecting an
598638fd1498Szrj anti-range with a range or empty when intersecting two ranges. */
598738fd1498Szrj if (*vr0type == VR_RANGE
598838fd1498Szrj && vr1type == VR_ANTI_RANGE)
598938fd1498Szrj ;
599038fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
599138fd1498Szrj && vr1type == VR_RANGE)
599238fd1498Szrj {
599338fd1498Szrj *vr0type = vr1type;
599438fd1498Szrj *vr0min = vr1min;
599538fd1498Szrj *vr0max = vr1max;
599638fd1498Szrj }
599738fd1498Szrj else if (*vr0type == VR_RANGE
599838fd1498Szrj && vr1type == VR_RANGE)
599938fd1498Szrj {
600038fd1498Szrj *vr0type = VR_UNDEFINED;
600138fd1498Szrj *vr0min = NULL_TREE;
600238fd1498Szrj *vr0max = NULL_TREE;
600338fd1498Szrj }
600438fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
600538fd1498Szrj && vr1type == VR_ANTI_RANGE)
600638fd1498Szrj {
600738fd1498Szrj /* If the anti-ranges are adjacent to each other merge them. */
600838fd1498Szrj if (TREE_CODE (*vr0max) == INTEGER_CST
600938fd1498Szrj && TREE_CODE (vr1min) == INTEGER_CST
601038fd1498Szrj && operand_less_p (*vr0max, vr1min) == 1
601138fd1498Szrj && integer_onep (int_const_binop (MINUS_EXPR,
601238fd1498Szrj vr1min, *vr0max)))
601338fd1498Szrj *vr0max = vr1max;
601438fd1498Szrj else if (TREE_CODE (vr1max) == INTEGER_CST
601538fd1498Szrj && TREE_CODE (*vr0min) == INTEGER_CST
601638fd1498Szrj && operand_less_p (vr1max, *vr0min) == 1
601738fd1498Szrj && integer_onep (int_const_binop (MINUS_EXPR,
601838fd1498Szrj *vr0min, vr1max)))
601938fd1498Szrj *vr0min = vr1min;
602038fd1498Szrj /* Else arbitrarily take VR0. */
602138fd1498Szrj }
602238fd1498Szrj }
602338fd1498Szrj else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
602438fd1498Szrj && (mineq || operand_less_p (*vr0min, vr1min) == 1))
602538fd1498Szrj {
602638fd1498Szrj /* [ ( ) ] or [( ) ] or [ ( )] */
602738fd1498Szrj if (*vr0type == VR_RANGE
602838fd1498Szrj && vr1type == VR_RANGE)
602938fd1498Szrj {
603038fd1498Szrj /* If both are ranges the result is the inner one. */
603138fd1498Szrj *vr0type = vr1type;
603238fd1498Szrj *vr0min = vr1min;
603338fd1498Szrj *vr0max = vr1max;
603438fd1498Szrj }
603538fd1498Szrj else if (*vr0type == VR_RANGE
603638fd1498Szrj && vr1type == VR_ANTI_RANGE)
603738fd1498Szrj {
603838fd1498Szrj /* Choose the right gap if the left one is empty. */
603938fd1498Szrj if (mineq)
604038fd1498Szrj {
604138fd1498Szrj if (TREE_CODE (vr1max) != INTEGER_CST)
604238fd1498Szrj *vr0min = vr1max;
604338fd1498Szrj else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
604438fd1498Szrj && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
604538fd1498Szrj *vr0min
604638fd1498Szrj = int_const_binop (MINUS_EXPR, vr1max,
604738fd1498Szrj build_int_cst (TREE_TYPE (vr1max), -1));
604838fd1498Szrj else
604938fd1498Szrj *vr0min
605038fd1498Szrj = int_const_binop (PLUS_EXPR, vr1max,
605138fd1498Szrj build_int_cst (TREE_TYPE (vr1max), 1));
605238fd1498Szrj }
605338fd1498Szrj /* Choose the left gap if the right one is empty. */
605438fd1498Szrj else if (maxeq)
605538fd1498Szrj {
605638fd1498Szrj if (TREE_CODE (vr1min) != INTEGER_CST)
605738fd1498Szrj *vr0max = vr1min;
605838fd1498Szrj else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
605938fd1498Szrj && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
606038fd1498Szrj *vr0max
606138fd1498Szrj = int_const_binop (PLUS_EXPR, vr1min,
606238fd1498Szrj build_int_cst (TREE_TYPE (vr1min), -1));
606338fd1498Szrj else
606438fd1498Szrj *vr0max
606538fd1498Szrj = int_const_binop (MINUS_EXPR, vr1min,
606638fd1498Szrj build_int_cst (TREE_TYPE (vr1min), 1));
606738fd1498Szrj }
606838fd1498Szrj /* Choose the anti-range if the range is effectively varying. */
606938fd1498Szrj else if (vrp_val_is_min (*vr0min)
607038fd1498Szrj && vrp_val_is_max (*vr0max))
607138fd1498Szrj {
607238fd1498Szrj *vr0type = vr1type;
607338fd1498Szrj *vr0min = vr1min;
607438fd1498Szrj *vr0max = vr1max;
607538fd1498Szrj }
607638fd1498Szrj /* Else choose the range. */
607738fd1498Szrj }
607838fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
607938fd1498Szrj && vr1type == VR_ANTI_RANGE)
608038fd1498Szrj /* If both are anti-ranges the result is the outer one. */
608138fd1498Szrj ;
608238fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
608338fd1498Szrj && vr1type == VR_RANGE)
608438fd1498Szrj {
608538fd1498Szrj /* The intersection is empty. */
608638fd1498Szrj *vr0type = VR_UNDEFINED;
608738fd1498Szrj *vr0min = NULL_TREE;
608838fd1498Szrj *vr0max = NULL_TREE;
608938fd1498Szrj }
609038fd1498Szrj else
609138fd1498Szrj gcc_unreachable ();
609238fd1498Szrj }
609338fd1498Szrj else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
609438fd1498Szrj && (mineq || operand_less_p (vr1min, *vr0min) == 1))
609538fd1498Szrj {
609638fd1498Szrj /* ( [ ] ) or ([ ] ) or ( [ ]) */
609738fd1498Szrj if (*vr0type == VR_RANGE
609838fd1498Szrj && vr1type == VR_RANGE)
609938fd1498Szrj /* Choose the inner range. */
610038fd1498Szrj ;
610138fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
610238fd1498Szrj && vr1type == VR_RANGE)
610338fd1498Szrj {
610438fd1498Szrj /* Choose the right gap if the left is empty. */
610538fd1498Szrj if (mineq)
610638fd1498Szrj {
610738fd1498Szrj *vr0type = VR_RANGE;
610838fd1498Szrj if (TREE_CODE (*vr0max) != INTEGER_CST)
610938fd1498Szrj *vr0min = *vr0max;
611038fd1498Szrj else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
611138fd1498Szrj && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
611238fd1498Szrj *vr0min
611338fd1498Szrj = int_const_binop (MINUS_EXPR, *vr0max,
611438fd1498Szrj build_int_cst (TREE_TYPE (*vr0max), -1));
611538fd1498Szrj else
611638fd1498Szrj *vr0min
611738fd1498Szrj = int_const_binop (PLUS_EXPR, *vr0max,
611838fd1498Szrj build_int_cst (TREE_TYPE (*vr0max), 1));
611938fd1498Szrj *vr0max = vr1max;
612038fd1498Szrj }
612138fd1498Szrj /* Choose the left gap if the right is empty. */
612238fd1498Szrj else if (maxeq)
612338fd1498Szrj {
612438fd1498Szrj *vr0type = VR_RANGE;
612538fd1498Szrj if (TREE_CODE (*vr0min) != INTEGER_CST)
612638fd1498Szrj *vr0max = *vr0min;
612738fd1498Szrj else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
612838fd1498Szrj && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
612938fd1498Szrj *vr0max
613038fd1498Szrj = int_const_binop (PLUS_EXPR, *vr0min,
613138fd1498Szrj build_int_cst (TREE_TYPE (*vr0min), -1));
613238fd1498Szrj else
613338fd1498Szrj *vr0max
613438fd1498Szrj = int_const_binop (MINUS_EXPR, *vr0min,
613538fd1498Szrj build_int_cst (TREE_TYPE (*vr0min), 1));
613638fd1498Szrj *vr0min = vr1min;
613738fd1498Szrj }
613838fd1498Szrj /* Choose the anti-range if the range is effectively varying. */
613938fd1498Szrj else if (vrp_val_is_min (vr1min)
614038fd1498Szrj && vrp_val_is_max (vr1max))
614138fd1498Szrj ;
614238fd1498Szrj /* Choose the anti-range if it is ~[0,0], that range is special
614338fd1498Szrj enough to special case when vr1's range is relatively wide.
614438fd1498Szrj At least for types bigger than int - this covers pointers
614538fd1498Szrj and arguments to functions like ctz. */
614638fd1498Szrj else if (*vr0min == *vr0max
614738fd1498Szrj && integer_zerop (*vr0min)
614838fd1498Szrj && ((TYPE_PRECISION (TREE_TYPE (*vr0min))
614938fd1498Szrj >= TYPE_PRECISION (integer_type_node))
615038fd1498Szrj || POINTER_TYPE_P (TREE_TYPE (*vr0min)))
615138fd1498Szrj && TREE_CODE (vr1max) == INTEGER_CST
615238fd1498Szrj && TREE_CODE (vr1min) == INTEGER_CST
615338fd1498Szrj && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min))
615438fd1498Szrj < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
615538fd1498Szrj ;
615638fd1498Szrj /* Else choose the range. */
615738fd1498Szrj else
615838fd1498Szrj {
615938fd1498Szrj *vr0type = vr1type;
616038fd1498Szrj *vr0min = vr1min;
616138fd1498Szrj *vr0max = vr1max;
616238fd1498Szrj }
616338fd1498Szrj }
616438fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
616538fd1498Szrj && vr1type == VR_ANTI_RANGE)
616638fd1498Szrj {
616738fd1498Szrj /* If both are anti-ranges the result is the outer one. */
616838fd1498Szrj *vr0type = vr1type;
616938fd1498Szrj *vr0min = vr1min;
617038fd1498Szrj *vr0max = vr1max;
617138fd1498Szrj }
617238fd1498Szrj else if (vr1type == VR_ANTI_RANGE
617338fd1498Szrj && *vr0type == VR_RANGE)
617438fd1498Szrj {
617538fd1498Szrj /* The intersection is empty. */
617638fd1498Szrj *vr0type = VR_UNDEFINED;
617738fd1498Szrj *vr0min = NULL_TREE;
617838fd1498Szrj *vr0max = NULL_TREE;
617938fd1498Szrj }
618038fd1498Szrj else
618138fd1498Szrj gcc_unreachable ();
618238fd1498Szrj }
618338fd1498Szrj else if ((operand_less_p (vr1min, *vr0max) == 1
618438fd1498Szrj || operand_equal_p (vr1min, *vr0max, 0))
618538fd1498Szrj && operand_less_p (*vr0min, vr1min) == 1)
618638fd1498Szrj {
618738fd1498Szrj /* [ ( ] ) or [ ]( ) */
618838fd1498Szrj if (*vr0type == VR_ANTI_RANGE
618938fd1498Szrj && vr1type == VR_ANTI_RANGE)
619038fd1498Szrj *vr0max = vr1max;
619138fd1498Szrj else if (*vr0type == VR_RANGE
619238fd1498Szrj && vr1type == VR_RANGE)
619338fd1498Szrj *vr0min = vr1min;
619438fd1498Szrj else if (*vr0type == VR_RANGE
619538fd1498Szrj && vr1type == VR_ANTI_RANGE)
619638fd1498Szrj {
619738fd1498Szrj if (TREE_CODE (vr1min) == INTEGER_CST)
619838fd1498Szrj *vr0max = int_const_binop (MINUS_EXPR, vr1min,
619938fd1498Szrj build_int_cst (TREE_TYPE (vr1min), 1));
620038fd1498Szrj else
620138fd1498Szrj *vr0max = vr1min;
620238fd1498Szrj }
620338fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
620438fd1498Szrj && vr1type == VR_RANGE)
620538fd1498Szrj {
620638fd1498Szrj *vr0type = VR_RANGE;
620738fd1498Szrj if (TREE_CODE (*vr0max) == INTEGER_CST)
620838fd1498Szrj *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
620938fd1498Szrj build_int_cst (TREE_TYPE (*vr0max), 1));
621038fd1498Szrj else
621138fd1498Szrj *vr0min = *vr0max;
621238fd1498Szrj *vr0max = vr1max;
621338fd1498Szrj }
621438fd1498Szrj else
621538fd1498Szrj gcc_unreachable ();
621638fd1498Szrj }
621738fd1498Szrj else if ((operand_less_p (*vr0min, vr1max) == 1
621838fd1498Szrj || operand_equal_p (*vr0min, vr1max, 0))
621938fd1498Szrj && operand_less_p (vr1min, *vr0min) == 1)
622038fd1498Szrj {
622138fd1498Szrj /* ( [ ) ] or ( )[ ] */
622238fd1498Szrj if (*vr0type == VR_ANTI_RANGE
622338fd1498Szrj && vr1type == VR_ANTI_RANGE)
622438fd1498Szrj *vr0min = vr1min;
622538fd1498Szrj else if (*vr0type == VR_RANGE
622638fd1498Szrj && vr1type == VR_RANGE)
622738fd1498Szrj *vr0max = vr1max;
622838fd1498Szrj else if (*vr0type == VR_RANGE
622938fd1498Szrj && vr1type == VR_ANTI_RANGE)
623038fd1498Szrj {
623138fd1498Szrj if (TREE_CODE (vr1max) == INTEGER_CST)
623238fd1498Szrj *vr0min = int_const_binop (PLUS_EXPR, vr1max,
623338fd1498Szrj build_int_cst (TREE_TYPE (vr1max), 1));
623438fd1498Szrj else
623538fd1498Szrj *vr0min = vr1max;
623638fd1498Szrj }
623738fd1498Szrj else if (*vr0type == VR_ANTI_RANGE
623838fd1498Szrj && vr1type == VR_RANGE)
623938fd1498Szrj {
624038fd1498Szrj *vr0type = VR_RANGE;
624138fd1498Szrj if (TREE_CODE (*vr0min) == INTEGER_CST)
624238fd1498Szrj *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
624338fd1498Szrj build_int_cst (TREE_TYPE (*vr0min), 1));
624438fd1498Szrj else
624538fd1498Szrj *vr0max = *vr0min;
624638fd1498Szrj *vr0min = vr1min;
624738fd1498Szrj }
624838fd1498Szrj else
624938fd1498Szrj gcc_unreachable ();
625038fd1498Szrj }
625138fd1498Szrj
625238fd1498Szrj /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
625338fd1498Szrj result for the intersection. That's always a conservative
625438fd1498Szrj correct estimate unless VR1 is a constant singleton range
625538fd1498Szrj in which case we choose that. */
625638fd1498Szrj if (vr1type == VR_RANGE
625738fd1498Szrj && is_gimple_min_invariant (vr1min)
625838fd1498Szrj && vrp_operand_equal_p (vr1min, vr1max))
625938fd1498Szrj {
626038fd1498Szrj *vr0type = vr1type;
626138fd1498Szrj *vr0min = vr1min;
626238fd1498Szrj *vr0max = vr1max;
626338fd1498Szrj }
626438fd1498Szrj
626538fd1498Szrj return;
626638fd1498Szrj }
626738fd1498Szrj
626838fd1498Szrj
626938fd1498Szrj /* Intersect the two value-ranges *VR0 and *VR1 and store the result
627038fd1498Szrj in *VR0. This may not be the smallest possible such range. */
627138fd1498Szrj
627238fd1498Szrj static void
vrp_intersect_ranges_1(value_range * vr0,value_range * vr1)627338fd1498Szrj vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
627438fd1498Szrj {
627538fd1498Szrj value_range saved;
627638fd1498Szrj
627738fd1498Szrj /* If either range is VR_VARYING the other one wins. */
627838fd1498Szrj if (vr1->type == VR_VARYING)
627938fd1498Szrj return;
628038fd1498Szrj if (vr0->type == VR_VARYING)
628138fd1498Szrj {
628238fd1498Szrj copy_value_range (vr0, vr1);
628338fd1498Szrj return;
628438fd1498Szrj }
628538fd1498Szrj
628638fd1498Szrj /* When either range is VR_UNDEFINED the resulting range is
628738fd1498Szrj VR_UNDEFINED, too. */
628838fd1498Szrj if (vr0->type == VR_UNDEFINED)
628938fd1498Szrj return;
629038fd1498Szrj if (vr1->type == VR_UNDEFINED)
629138fd1498Szrj {
629238fd1498Szrj set_value_range_to_undefined (vr0);
629338fd1498Szrj return;
629438fd1498Szrj }
629538fd1498Szrj
629638fd1498Szrj /* Save the original vr0 so we can return it as conservative intersection
629738fd1498Szrj result when our worker turns things to varying. */
629838fd1498Szrj saved = *vr0;
629938fd1498Szrj intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
630038fd1498Szrj vr1->type, vr1->min, vr1->max);
630138fd1498Szrj /* Make sure to canonicalize the result though as the inversion of a
630238fd1498Szrj VR_RANGE can still be a VR_RANGE. */
630338fd1498Szrj set_and_canonicalize_value_range (vr0, vr0->type,
630438fd1498Szrj vr0->min, vr0->max, vr0->equiv);
630538fd1498Szrj /* If that failed, use the saved original VR0. */
630638fd1498Szrj if (vr0->type == VR_VARYING)
630738fd1498Szrj {
630838fd1498Szrj *vr0 = saved;
630938fd1498Szrj return;
631038fd1498Szrj }
631138fd1498Szrj /* If the result is VR_UNDEFINED there is no need to mess with
631238fd1498Szrj the equivalencies. */
631338fd1498Szrj if (vr0->type == VR_UNDEFINED)
631438fd1498Szrj return;
631538fd1498Szrj
631638fd1498Szrj /* The resulting set of equivalences for range intersection is the union of
631738fd1498Szrj the two sets. */
631838fd1498Szrj if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
631938fd1498Szrj bitmap_ior_into (vr0->equiv, vr1->equiv);
632038fd1498Szrj else if (vr1->equiv && !vr0->equiv)
632138fd1498Szrj {
632238fd1498Szrj /* All equivalence bitmaps are allocated from the same obstack. So
632338fd1498Szrj we can use the obstack associated with VR to allocate vr0->equiv. */
632438fd1498Szrj vr0->equiv = BITMAP_ALLOC (vr1->equiv->obstack);
632538fd1498Szrj bitmap_copy (vr0->equiv, vr1->equiv);
632638fd1498Szrj }
632738fd1498Szrj }
632838fd1498Szrj
632938fd1498Szrj void
vrp_intersect_ranges(value_range * vr0,value_range * vr1)633038fd1498Szrj vrp_intersect_ranges (value_range *vr0, value_range *vr1)
633138fd1498Szrj {
633238fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
633338fd1498Szrj {
633438fd1498Szrj fprintf (dump_file, "Intersecting\n ");
633538fd1498Szrj dump_value_range (dump_file, vr0);
633638fd1498Szrj fprintf (dump_file, "\nand\n ");
633738fd1498Szrj dump_value_range (dump_file, vr1);
633838fd1498Szrj fprintf (dump_file, "\n");
633938fd1498Szrj }
634038fd1498Szrj vrp_intersect_ranges_1 (vr0, vr1);
634138fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
634238fd1498Szrj {
634338fd1498Szrj fprintf (dump_file, "to\n ");
634438fd1498Szrj dump_value_range (dump_file, vr0);
634538fd1498Szrj fprintf (dump_file, "\n");
634638fd1498Szrj }
634738fd1498Szrj }
634838fd1498Szrj
634938fd1498Szrj /* Meet operation for value ranges. Given two value ranges VR0 and
635038fd1498Szrj VR1, store in VR0 a range that contains both VR0 and VR1. This
635138fd1498Szrj may not be the smallest possible such range. */
635238fd1498Szrj
635338fd1498Szrj static void
vrp_meet_1(value_range * vr0,const value_range * vr1)635438fd1498Szrj vrp_meet_1 (value_range *vr0, const value_range *vr1)
635538fd1498Szrj {
635638fd1498Szrj value_range saved;
635738fd1498Szrj
635838fd1498Szrj if (vr0->type == VR_UNDEFINED)
635938fd1498Szrj {
636038fd1498Szrj set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
636138fd1498Szrj return;
636238fd1498Szrj }
636338fd1498Szrj
636438fd1498Szrj if (vr1->type == VR_UNDEFINED)
636538fd1498Szrj {
636638fd1498Szrj /* VR0 already has the resulting range. */
636738fd1498Szrj return;
636838fd1498Szrj }
636938fd1498Szrj
637038fd1498Szrj if (vr0->type == VR_VARYING)
637138fd1498Szrj {
637238fd1498Szrj /* Nothing to do. VR0 already has the resulting range. */
637338fd1498Szrj return;
637438fd1498Szrj }
637538fd1498Szrj
637638fd1498Szrj if (vr1->type == VR_VARYING)
637738fd1498Szrj {
637838fd1498Szrj set_value_range_to_varying (vr0);
637938fd1498Szrj return;
638038fd1498Szrj }
638138fd1498Szrj
638238fd1498Szrj saved = *vr0;
638338fd1498Szrj union_ranges (&vr0->type, &vr0->min, &vr0->max,
638438fd1498Szrj vr1->type, vr1->min, vr1->max);
638538fd1498Szrj if (vr0->type == VR_VARYING)
638638fd1498Szrj {
638738fd1498Szrj /* Failed to find an efficient meet. Before giving up and setting
638838fd1498Szrj the result to VARYING, see if we can at least derive a useful
638938fd1498Szrj anti-range. FIXME, all this nonsense about distinguishing
639038fd1498Szrj anti-ranges from ranges is necessary because of the odd
639138fd1498Szrj semantics of range_includes_zero_p and friends. */
639238fd1498Szrj if (((saved.type == VR_RANGE
639338fd1498Szrj && range_includes_zero_p (saved.min, saved.max) == 0)
639438fd1498Szrj || (saved.type == VR_ANTI_RANGE
639538fd1498Szrj && range_includes_zero_p (saved.min, saved.max) == 1))
639638fd1498Szrj && ((vr1->type == VR_RANGE
639738fd1498Szrj && range_includes_zero_p (vr1->min, vr1->max) == 0)
639838fd1498Szrj || (vr1->type == VR_ANTI_RANGE
639938fd1498Szrj && range_includes_zero_p (vr1->min, vr1->max) == 1)))
640038fd1498Szrj {
640138fd1498Szrj set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
640238fd1498Szrj
640338fd1498Szrj /* Since this meet operation did not result from the meeting of
640438fd1498Szrj two equivalent names, VR0 cannot have any equivalences. */
640538fd1498Szrj if (vr0->equiv)
640638fd1498Szrj bitmap_clear (vr0->equiv);
640738fd1498Szrj return;
640838fd1498Szrj }
640938fd1498Szrj
641038fd1498Szrj set_value_range_to_varying (vr0);
641138fd1498Szrj return;
641238fd1498Szrj }
641338fd1498Szrj set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
641438fd1498Szrj vr0->equiv);
641538fd1498Szrj if (vr0->type == VR_VARYING)
641638fd1498Szrj return;
641738fd1498Szrj
641838fd1498Szrj /* The resulting set of equivalences is always the intersection of
641938fd1498Szrj the two sets. */
642038fd1498Szrj if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
642138fd1498Szrj bitmap_and_into (vr0->equiv, vr1->equiv);
642238fd1498Szrj else if (vr0->equiv && !vr1->equiv)
642338fd1498Szrj bitmap_clear (vr0->equiv);
642438fd1498Szrj }
642538fd1498Szrj
642638fd1498Szrj void
vrp_meet(value_range * vr0,const value_range * vr1)642738fd1498Szrj vrp_meet (value_range *vr0, const value_range *vr1)
642838fd1498Szrj {
642938fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
643038fd1498Szrj {
643138fd1498Szrj fprintf (dump_file, "Meeting\n ");
643238fd1498Szrj dump_value_range (dump_file, vr0);
643338fd1498Szrj fprintf (dump_file, "\nand\n ");
643438fd1498Szrj dump_value_range (dump_file, vr1);
643538fd1498Szrj fprintf (dump_file, "\n");
643638fd1498Szrj }
643738fd1498Szrj vrp_meet_1 (vr0, vr1);
643838fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
643938fd1498Szrj {
644038fd1498Szrj fprintf (dump_file, "to\n ");
644138fd1498Szrj dump_value_range (dump_file, vr0);
644238fd1498Szrj fprintf (dump_file, "\n");
644338fd1498Szrj }
644438fd1498Szrj }
644538fd1498Szrj
644638fd1498Szrj
644738fd1498Szrj /* Visit all arguments for PHI node PHI that flow through executable
644838fd1498Szrj edges. If a valid value range can be derived from all the incoming
644938fd1498Szrj value ranges, set a new range for the LHS of PHI. */
645038fd1498Szrj
645138fd1498Szrj enum ssa_prop_result
visit_phi(gphi * phi)645238fd1498Szrj vrp_prop::visit_phi (gphi *phi)
645338fd1498Szrj {
645438fd1498Szrj tree lhs = PHI_RESULT (phi);
645538fd1498Szrj value_range vr_result = VR_INITIALIZER;
645638fd1498Szrj extract_range_from_phi_node (phi, &vr_result);
645738fd1498Szrj if (update_value_range (lhs, &vr_result))
645838fd1498Szrj {
645938fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
646038fd1498Szrj {
646138fd1498Szrj fprintf (dump_file, "Found new range for ");
646238fd1498Szrj print_generic_expr (dump_file, lhs);
646338fd1498Szrj fprintf (dump_file, ": ");
646438fd1498Szrj dump_value_range (dump_file, &vr_result);
646538fd1498Szrj fprintf (dump_file, "\n");
646638fd1498Szrj }
646738fd1498Szrj
646838fd1498Szrj if (vr_result.type == VR_VARYING)
646938fd1498Szrj return SSA_PROP_VARYING;
647038fd1498Szrj
647138fd1498Szrj return SSA_PROP_INTERESTING;
647238fd1498Szrj }
647338fd1498Szrj
647438fd1498Szrj /* Nothing changed, don't add outgoing edges. */
647538fd1498Szrj return SSA_PROP_NOT_INTERESTING;
647638fd1498Szrj }
647738fd1498Szrj
647838fd1498Szrj class vrp_folder : public substitute_and_fold_engine
647938fd1498Szrj {
648038fd1498Szrj public:
648138fd1498Szrj tree get_value (tree) FINAL OVERRIDE;
648238fd1498Szrj bool fold_stmt (gimple_stmt_iterator *) FINAL OVERRIDE;
648338fd1498Szrj bool fold_predicate_in (gimple_stmt_iterator *);
648438fd1498Szrj
648538fd1498Szrj class vr_values *vr_values;
648638fd1498Szrj
648738fd1498Szrj /* Delegators. */
vrp_evaluate_conditional(tree_code code,tree op0,tree op1,gimple * stmt)648838fd1498Szrj tree vrp_evaluate_conditional (tree_code code, tree op0,
648938fd1498Szrj tree op1, gimple *stmt)
649038fd1498Szrj { return vr_values->vrp_evaluate_conditional (code, op0, op1, stmt); }
simplify_stmt_using_ranges(gimple_stmt_iterator * gsi)649138fd1498Szrj bool simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
649238fd1498Szrj { return vr_values->simplify_stmt_using_ranges (gsi); }
op_with_constant_singleton_value_range(tree op)649338fd1498Szrj tree op_with_constant_singleton_value_range (tree op)
649438fd1498Szrj { return vr_values->op_with_constant_singleton_value_range (op); }
649538fd1498Szrj };
649638fd1498Szrj
649738fd1498Szrj /* If the statement pointed by SI has a predicate whose value can be
649838fd1498Szrj computed using the value range information computed by VRP, compute
649938fd1498Szrj its value and return true. Otherwise, return false. */
650038fd1498Szrj
650138fd1498Szrj bool
fold_predicate_in(gimple_stmt_iterator * si)650238fd1498Szrj vrp_folder::fold_predicate_in (gimple_stmt_iterator *si)
650338fd1498Szrj {
650438fd1498Szrj bool assignment_p = false;
650538fd1498Szrj tree val;
650638fd1498Szrj gimple *stmt = gsi_stmt (*si);
650738fd1498Szrj
650838fd1498Szrj if (is_gimple_assign (stmt)
650938fd1498Szrj && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
651038fd1498Szrj {
651138fd1498Szrj assignment_p = true;
651238fd1498Szrj val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
651338fd1498Szrj gimple_assign_rhs1 (stmt),
651438fd1498Szrj gimple_assign_rhs2 (stmt),
651538fd1498Szrj stmt);
651638fd1498Szrj }
651738fd1498Szrj else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
651838fd1498Szrj val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
651938fd1498Szrj gimple_cond_lhs (cond_stmt),
652038fd1498Szrj gimple_cond_rhs (cond_stmt),
652138fd1498Szrj stmt);
652238fd1498Szrj else
652338fd1498Szrj return false;
652438fd1498Szrj
652538fd1498Szrj if (val)
652638fd1498Szrj {
652738fd1498Szrj if (assignment_p)
652838fd1498Szrj val = fold_convert (gimple_expr_type (stmt), val);
652938fd1498Szrj
653038fd1498Szrj if (dump_file)
653138fd1498Szrj {
653238fd1498Szrj fprintf (dump_file, "Folding predicate ");
653338fd1498Szrj print_gimple_expr (dump_file, stmt, 0);
653438fd1498Szrj fprintf (dump_file, " to ");
653538fd1498Szrj print_generic_expr (dump_file, val);
653638fd1498Szrj fprintf (dump_file, "\n");
653738fd1498Szrj }
653838fd1498Szrj
653938fd1498Szrj if (is_gimple_assign (stmt))
654038fd1498Szrj gimple_assign_set_rhs_from_tree (si, val);
654138fd1498Szrj else
654238fd1498Szrj {
654338fd1498Szrj gcc_assert (gimple_code (stmt) == GIMPLE_COND);
654438fd1498Szrj gcond *cond_stmt = as_a <gcond *> (stmt);
654538fd1498Szrj if (integer_zerop (val))
654638fd1498Szrj gimple_cond_make_false (cond_stmt);
654738fd1498Szrj else if (integer_onep (val))
654838fd1498Szrj gimple_cond_make_true (cond_stmt);
654938fd1498Szrj else
655038fd1498Szrj gcc_unreachable ();
655138fd1498Szrj }
655238fd1498Szrj
655338fd1498Szrj return true;
655438fd1498Szrj }
655538fd1498Szrj
655638fd1498Szrj return false;
655738fd1498Szrj }
655838fd1498Szrj
655938fd1498Szrj /* Callback for substitute_and_fold folding the stmt at *SI. */
656038fd1498Szrj
656138fd1498Szrj bool
fold_stmt(gimple_stmt_iterator * si)656238fd1498Szrj vrp_folder::fold_stmt (gimple_stmt_iterator *si)
656338fd1498Szrj {
656438fd1498Szrj if (fold_predicate_in (si))
656538fd1498Szrj return true;
656638fd1498Szrj
656738fd1498Szrj return simplify_stmt_using_ranges (si);
656838fd1498Szrj }
656938fd1498Szrj
657038fd1498Szrj /* If OP has a value range with a single constant value return that,
657138fd1498Szrj otherwise return NULL_TREE. This returns OP itself if OP is a
657238fd1498Szrj constant.
657338fd1498Szrj
657438fd1498Szrj Implemented as a pure wrapper right now, but this will change. */
657538fd1498Szrj
657638fd1498Szrj tree
get_value(tree op)657738fd1498Szrj vrp_folder::get_value (tree op)
657838fd1498Szrj {
657938fd1498Szrj return op_with_constant_singleton_value_range (op);
658038fd1498Szrj }
658138fd1498Szrj
658238fd1498Szrj /* Return the LHS of any ASSERT_EXPR where OP appears as the first
658338fd1498Szrj argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
658438fd1498Szrj BB. If no such ASSERT_EXPR is found, return OP. */
658538fd1498Szrj
658638fd1498Szrj static tree
lhs_of_dominating_assert(tree op,basic_block bb,gimple * stmt)658738fd1498Szrj lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt)
658838fd1498Szrj {
658938fd1498Szrj imm_use_iterator imm_iter;
659038fd1498Szrj gimple *use_stmt;
659138fd1498Szrj use_operand_p use_p;
659238fd1498Szrj
659338fd1498Szrj if (TREE_CODE (op) == SSA_NAME)
659438fd1498Szrj {
659538fd1498Szrj FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
659638fd1498Szrj {
659738fd1498Szrj use_stmt = USE_STMT (use_p);
659838fd1498Szrj if (use_stmt != stmt
659938fd1498Szrj && gimple_assign_single_p (use_stmt)
660038fd1498Szrj && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
660138fd1498Szrj && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
660238fd1498Szrj && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
660338fd1498Szrj return gimple_assign_lhs (use_stmt);
660438fd1498Szrj }
660538fd1498Szrj }
660638fd1498Szrj return op;
660738fd1498Szrj }
660838fd1498Szrj
660938fd1498Szrj /* A hack. */
661038fd1498Szrj static class vr_values *x_vr_values;
661138fd1498Szrj
661238fd1498Szrj /* A trivial wrapper so that we can present the generic jump threading
661338fd1498Szrj code with a simple API for simplifying statements. STMT is the
661438fd1498Szrj statement we want to simplify, WITHIN_STMT provides the location
661538fd1498Szrj for any overflow warnings. */
661638fd1498Szrj
661738fd1498Szrj static tree
simplify_stmt_for_jump_threading(gimple * stmt,gimple * within_stmt,class avail_exprs_stack * avail_exprs_stack ATTRIBUTE_UNUSED,basic_block bb)661838fd1498Szrj simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
661938fd1498Szrj class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED,
662038fd1498Szrj basic_block bb)
662138fd1498Szrj {
662238fd1498Szrj /* First see if the conditional is in the hash table. */
662338fd1498Szrj tree cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, false, true);
662438fd1498Szrj if (cached_lhs && is_gimple_min_invariant (cached_lhs))
662538fd1498Szrj return cached_lhs;
662638fd1498Szrj
662738fd1498Szrj vr_values *vr_values = x_vr_values;
662838fd1498Szrj if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
662938fd1498Szrj {
663038fd1498Szrj tree op0 = gimple_cond_lhs (cond_stmt);
663138fd1498Szrj op0 = lhs_of_dominating_assert (op0, bb, stmt);
663238fd1498Szrj
663338fd1498Szrj tree op1 = gimple_cond_rhs (cond_stmt);
663438fd1498Szrj op1 = lhs_of_dominating_assert (op1, bb, stmt);
663538fd1498Szrj
663638fd1498Szrj return vr_values->vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
663738fd1498Szrj op0, op1, within_stmt);
663838fd1498Szrj }
663938fd1498Szrj
664038fd1498Szrj /* We simplify a switch statement by trying to determine which case label
664138fd1498Szrj will be taken. If we are successful then we return the corresponding
664238fd1498Szrj CASE_LABEL_EXPR. */
664338fd1498Szrj if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
664438fd1498Szrj {
664538fd1498Szrj tree op = gimple_switch_index (switch_stmt);
664638fd1498Szrj if (TREE_CODE (op) != SSA_NAME)
664738fd1498Szrj return NULL_TREE;
664838fd1498Szrj
664938fd1498Szrj op = lhs_of_dominating_assert (op, bb, stmt);
665038fd1498Szrj
665138fd1498Szrj value_range *vr = vr_values->get_value_range (op);
665238fd1498Szrj if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE)
665338fd1498Szrj || symbolic_range_p (vr))
665438fd1498Szrj return NULL_TREE;
665538fd1498Szrj
665638fd1498Szrj if (vr->type == VR_RANGE)
665738fd1498Szrj {
665838fd1498Szrj size_t i, j;
665938fd1498Szrj /* Get the range of labels that contain a part of the operand's
666038fd1498Szrj value range. */
666138fd1498Szrj find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j);
666238fd1498Szrj
666338fd1498Szrj /* Is there only one such label? */
666438fd1498Szrj if (i == j)
666538fd1498Szrj {
666638fd1498Szrj tree label = gimple_switch_label (switch_stmt, i);
666738fd1498Szrj
666838fd1498Szrj /* The i'th label will be taken only if the value range of the
666938fd1498Szrj operand is entirely within the bounds of this label. */
667038fd1498Szrj if (CASE_HIGH (label) != NULL_TREE
667138fd1498Szrj ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0
667238fd1498Szrj && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0)
667338fd1498Szrj : (tree_int_cst_equal (CASE_LOW (label), vr->min)
667438fd1498Szrj && tree_int_cst_equal (vr->min, vr->max)))
667538fd1498Szrj return label;
667638fd1498Szrj }
667738fd1498Szrj
667838fd1498Szrj /* If there are no such labels then the default label will be
667938fd1498Szrj taken. */
668038fd1498Szrj if (i > j)
668138fd1498Szrj return gimple_switch_label (switch_stmt, 0);
668238fd1498Szrj }
668338fd1498Szrj
668438fd1498Szrj if (vr->type == VR_ANTI_RANGE)
668538fd1498Szrj {
668638fd1498Szrj unsigned n = gimple_switch_num_labels (switch_stmt);
668738fd1498Szrj tree min_label = gimple_switch_label (switch_stmt, 1);
668838fd1498Szrj tree max_label = gimple_switch_label (switch_stmt, n - 1);
668938fd1498Szrj
669038fd1498Szrj /* The default label will be taken only if the anti-range of the
669138fd1498Szrj operand is entirely outside the bounds of all the (non-default)
669238fd1498Szrj case labels. */
669338fd1498Szrj if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0
669438fd1498Szrj && (CASE_HIGH (max_label) != NULL_TREE
669538fd1498Szrj ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0
669638fd1498Szrj : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0))
669738fd1498Szrj return gimple_switch_label (switch_stmt, 0);
669838fd1498Szrj }
669938fd1498Szrj
670038fd1498Szrj return NULL_TREE;
670138fd1498Szrj }
670238fd1498Szrj
670338fd1498Szrj if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
670438fd1498Szrj {
670538fd1498Szrj tree lhs = gimple_assign_lhs (assign_stmt);
670638fd1498Szrj if (TREE_CODE (lhs) == SSA_NAME
670738fd1498Szrj && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
670838fd1498Szrj || POINTER_TYPE_P (TREE_TYPE (lhs)))
670938fd1498Szrj && stmt_interesting_for_vrp (stmt))
671038fd1498Szrj {
671138fd1498Szrj edge dummy_e;
671238fd1498Szrj tree dummy_tree;
671338fd1498Szrj value_range new_vr = VR_INITIALIZER;
671438fd1498Szrj vr_values->extract_range_from_stmt (stmt, &dummy_e,
671538fd1498Szrj &dummy_tree, &new_vr);
671638fd1498Szrj if (range_int_cst_singleton_p (&new_vr))
671738fd1498Szrj return new_vr.min;
671838fd1498Szrj }
671938fd1498Szrj }
672038fd1498Szrj
672138fd1498Szrj return NULL_TREE;
672238fd1498Szrj }
672338fd1498Szrj
672438fd1498Szrj class vrp_dom_walker : public dom_walker
672538fd1498Szrj {
672638fd1498Szrj public:
vrp_dom_walker(cdi_direction direction,class const_and_copies * const_and_copies,class avail_exprs_stack * avail_exprs_stack)672738fd1498Szrj vrp_dom_walker (cdi_direction direction,
672838fd1498Szrj class const_and_copies *const_and_copies,
672938fd1498Szrj class avail_exprs_stack *avail_exprs_stack)
673038fd1498Szrj : dom_walker (direction, REACHABLE_BLOCKS),
673138fd1498Szrj m_const_and_copies (const_and_copies),
673238fd1498Szrj m_avail_exprs_stack (avail_exprs_stack),
673338fd1498Szrj m_dummy_cond (NULL) {}
673438fd1498Szrj
673538fd1498Szrj virtual edge before_dom_children (basic_block);
673638fd1498Szrj virtual void after_dom_children (basic_block);
673738fd1498Szrj
673838fd1498Szrj class vr_values *vr_values;
673938fd1498Szrj
674038fd1498Szrj private:
674138fd1498Szrj class const_and_copies *m_const_and_copies;
674238fd1498Szrj class avail_exprs_stack *m_avail_exprs_stack;
674338fd1498Szrj
674438fd1498Szrj gcond *m_dummy_cond;
674538fd1498Szrj
674638fd1498Szrj };
674738fd1498Szrj
674838fd1498Szrj /* Called before processing dominator children of BB. We want to look
674938fd1498Szrj at ASSERT_EXPRs and record information from them in the appropriate
675038fd1498Szrj tables.
675138fd1498Szrj
675238fd1498Szrj We could look at other statements here. It's not seen as likely
675338fd1498Szrj to significantly increase the jump threads we discover. */
675438fd1498Szrj
675538fd1498Szrj edge
before_dom_children(basic_block bb)675638fd1498Szrj vrp_dom_walker::before_dom_children (basic_block bb)
675738fd1498Szrj {
675838fd1498Szrj gimple_stmt_iterator gsi;
675938fd1498Szrj
676038fd1498Szrj m_avail_exprs_stack->push_marker ();
676138fd1498Szrj m_const_and_copies->push_marker ();
676238fd1498Szrj for (gsi = gsi_start_nondebug_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
676338fd1498Szrj {
676438fd1498Szrj gimple *stmt = gsi_stmt (gsi);
676538fd1498Szrj if (gimple_assign_single_p (stmt)
676638fd1498Szrj && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
676738fd1498Szrj {
676838fd1498Szrj tree rhs1 = gimple_assign_rhs1 (stmt);
676938fd1498Szrj tree cond = TREE_OPERAND (rhs1, 1);
677038fd1498Szrj tree inverted = invert_truthvalue (cond);
677138fd1498Szrj vec<cond_equivalence> p;
677238fd1498Szrj p.create (3);
677338fd1498Szrj record_conditions (&p, cond, inverted);
677438fd1498Szrj for (unsigned int i = 0; i < p.length (); i++)
677538fd1498Szrj m_avail_exprs_stack->record_cond (&p[i]);
677638fd1498Szrj
677738fd1498Szrj tree lhs = gimple_assign_lhs (stmt);
677838fd1498Szrj m_const_and_copies->record_const_or_copy (lhs,
677938fd1498Szrj TREE_OPERAND (rhs1, 0));
678038fd1498Szrj p.release ();
678138fd1498Szrj continue;
678238fd1498Szrj }
678338fd1498Szrj break;
678438fd1498Szrj }
678538fd1498Szrj return NULL;
678638fd1498Szrj }
678738fd1498Szrj
678838fd1498Szrj /* Called after processing dominator children of BB. This is where we
678938fd1498Szrj actually call into the threader. */
679038fd1498Szrj void
after_dom_children(basic_block bb)679138fd1498Szrj vrp_dom_walker::after_dom_children (basic_block bb)
679238fd1498Szrj {
679338fd1498Szrj if (!m_dummy_cond)
679438fd1498Szrj m_dummy_cond = gimple_build_cond (NE_EXPR,
679538fd1498Szrj integer_zero_node, integer_zero_node,
679638fd1498Szrj NULL, NULL);
679738fd1498Szrj
679838fd1498Szrj x_vr_values = vr_values;
679938fd1498Szrj thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies,
680038fd1498Szrj m_avail_exprs_stack, NULL,
680138fd1498Szrj simplify_stmt_for_jump_threading);
680238fd1498Szrj x_vr_values = NULL;
680338fd1498Szrj
680438fd1498Szrj m_avail_exprs_stack->pop_to_marker ();
680538fd1498Szrj m_const_and_copies->pop_to_marker ();
680638fd1498Szrj }
680738fd1498Szrj
680838fd1498Szrj /* Blocks which have more than one predecessor and more than
680938fd1498Szrj one successor present jump threading opportunities, i.e.,
681038fd1498Szrj when the block is reached from a specific predecessor, we
681138fd1498Szrj may be able to determine which of the outgoing edges will
681238fd1498Szrj be traversed. When this optimization applies, we are able
681338fd1498Szrj to avoid conditionals at runtime and we may expose secondary
681438fd1498Szrj optimization opportunities.
681538fd1498Szrj
681638fd1498Szrj This routine is effectively a driver for the generic jump
681738fd1498Szrj threading code. It basically just presents the generic code
681838fd1498Szrj with edges that may be suitable for jump threading.
681938fd1498Szrj
682038fd1498Szrj Unlike DOM, we do not iterate VRP if jump threading was successful.
682138fd1498Szrj While iterating may expose new opportunities for VRP, it is expected
682238fd1498Szrj those opportunities would be very limited and the compile time cost
682338fd1498Szrj to expose those opportunities would be significant.
682438fd1498Szrj
682538fd1498Szrj As jump threading opportunities are discovered, they are registered
682638fd1498Szrj for later realization. */
682738fd1498Szrj
682838fd1498Szrj static void
identify_jump_threads(class vr_values * vr_values)682938fd1498Szrj identify_jump_threads (class vr_values *vr_values)
683038fd1498Szrj {
683138fd1498Szrj int i;
683238fd1498Szrj edge e;
683338fd1498Szrj
683438fd1498Szrj /* Ugh. When substituting values earlier in this pass we can
683538fd1498Szrj wipe the dominance information. So rebuild the dominator
683638fd1498Szrj information as we need it within the jump threading code. */
683738fd1498Szrj calculate_dominance_info (CDI_DOMINATORS);
683838fd1498Szrj
683938fd1498Szrj /* We do not allow VRP information to be used for jump threading
684038fd1498Szrj across a back edge in the CFG. Otherwise it becomes too
684138fd1498Szrj difficult to avoid eliminating loop exit tests. Of course
684238fd1498Szrj EDGE_DFS_BACK is not accurate at this time so we have to
684338fd1498Szrj recompute it. */
684438fd1498Szrj mark_dfs_back_edges ();
684538fd1498Szrj
684638fd1498Szrj /* Do not thread across edges we are about to remove. Just marking
684738fd1498Szrj them as EDGE_IGNORE will do. */
684838fd1498Szrj FOR_EACH_VEC_ELT (to_remove_edges, i, e)
684938fd1498Szrj e->flags |= EDGE_IGNORE;
685038fd1498Szrj
685138fd1498Szrj /* Allocate our unwinder stack to unwind any temporary equivalences
685238fd1498Szrj that might be recorded. */
685338fd1498Szrj const_and_copies *equiv_stack = new const_and_copies ();
685438fd1498Szrj
685538fd1498Szrj hash_table<expr_elt_hasher> *avail_exprs
685638fd1498Szrj = new hash_table<expr_elt_hasher> (1024);
685738fd1498Szrj avail_exprs_stack *avail_exprs_stack
685838fd1498Szrj = new class avail_exprs_stack (avail_exprs);
685938fd1498Szrj
686038fd1498Szrj vrp_dom_walker walker (CDI_DOMINATORS, equiv_stack, avail_exprs_stack);
686138fd1498Szrj walker.vr_values = vr_values;
686238fd1498Szrj walker.walk (cfun->cfg->x_entry_block_ptr);
686338fd1498Szrj
686438fd1498Szrj /* Clear EDGE_IGNORE. */
686538fd1498Szrj FOR_EACH_VEC_ELT (to_remove_edges, i, e)
686638fd1498Szrj e->flags &= ~EDGE_IGNORE;
686738fd1498Szrj
686838fd1498Szrj /* We do not actually update the CFG or SSA graphs at this point as
686938fd1498Szrj ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
687038fd1498Szrj handle ASSERT_EXPRs gracefully. */
687138fd1498Szrj delete equiv_stack;
687238fd1498Szrj delete avail_exprs;
687338fd1498Szrj delete avail_exprs_stack;
687438fd1498Szrj }
687538fd1498Szrj
687638fd1498Szrj /* Traverse all the blocks folding conditionals with known ranges. */
687738fd1498Szrj
687838fd1498Szrj void
vrp_finalize(bool warn_array_bounds_p)687938fd1498Szrj vrp_prop::vrp_finalize (bool warn_array_bounds_p)
688038fd1498Szrj {
688138fd1498Szrj size_t i;
688238fd1498Szrj
688338fd1498Szrj /* We have completed propagating through the lattice. */
688438fd1498Szrj vr_values.set_lattice_propagation_complete ();
688538fd1498Szrj
688638fd1498Szrj if (dump_file)
688738fd1498Szrj {
688838fd1498Szrj fprintf (dump_file, "\nValue ranges after VRP:\n\n");
688938fd1498Szrj vr_values.dump_all_value_ranges (dump_file);
689038fd1498Szrj fprintf (dump_file, "\n");
689138fd1498Szrj }
689238fd1498Szrj
689338fd1498Szrj /* Set value range to non pointer SSA_NAMEs. */
689438fd1498Szrj for (i = 0; i < num_ssa_names; i++)
689538fd1498Szrj {
689638fd1498Szrj tree name = ssa_name (i);
689738fd1498Szrj if (!name)
689838fd1498Szrj continue;
689938fd1498Szrj
690038fd1498Szrj value_range *vr = get_value_range (name);
690138fd1498Szrj if (!name
690238fd1498Szrj || (vr->type == VR_VARYING)
690338fd1498Szrj || (vr->type == VR_UNDEFINED)
690438fd1498Szrj || (TREE_CODE (vr->min) != INTEGER_CST)
690538fd1498Szrj || (TREE_CODE (vr->max) != INTEGER_CST))
690638fd1498Szrj continue;
690738fd1498Szrj
690838fd1498Szrj if (POINTER_TYPE_P (TREE_TYPE (name))
690938fd1498Szrj && ((vr->type == VR_RANGE
691038fd1498Szrj && range_includes_zero_p (vr->min, vr->max) == 0)
691138fd1498Szrj || (vr->type == VR_ANTI_RANGE
691238fd1498Szrj && range_includes_zero_p (vr->min, vr->max) == 1)))
691338fd1498Szrj set_ptr_nonnull (name);
691438fd1498Szrj else if (!POINTER_TYPE_P (TREE_TYPE (name)))
691538fd1498Szrj set_range_info (name, vr->type,
691638fd1498Szrj wi::to_wide (vr->min),
691738fd1498Szrj wi::to_wide (vr->max));
691838fd1498Szrj }
691938fd1498Szrj
692038fd1498Szrj /* If we're checking array refs, we want to merge information on
692138fd1498Szrj the executability of each edge between vrp_folder and the
692238fd1498Szrj check_array_bounds_dom_walker: each can clear the
692338fd1498Szrj EDGE_EXECUTABLE flag on edges, in different ways.
692438fd1498Szrj
692538fd1498Szrj Hence, if we're going to call check_all_array_refs, set
692638fd1498Szrj the flag on every edge now, rather than in
692738fd1498Szrj check_array_bounds_dom_walker's ctor; vrp_folder may clear
692838fd1498Szrj it from some edges. */
692938fd1498Szrj if (warn_array_bounds && warn_array_bounds_p)
693038fd1498Szrj set_all_edges_as_executable (cfun);
693138fd1498Szrj
693238fd1498Szrj class vrp_folder vrp_folder;
693338fd1498Szrj vrp_folder.vr_values = &vr_values;
693438fd1498Szrj vrp_folder.substitute_and_fold ();
693538fd1498Szrj
693638fd1498Szrj if (warn_array_bounds && warn_array_bounds_p)
693738fd1498Szrj check_all_array_refs ();
693838fd1498Szrj }
693938fd1498Szrj
694038fd1498Szrj /* Main entry point to VRP (Value Range Propagation). This pass is
694138fd1498Szrj loosely based on J. R. C. Patterson, ``Accurate Static Branch
694238fd1498Szrj Prediction by Value Range Propagation,'' in SIGPLAN Conference on
694338fd1498Szrj Programming Language Design and Implementation, pp. 67-78, 1995.
694438fd1498Szrj Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
694538fd1498Szrj
694638fd1498Szrj This is essentially an SSA-CCP pass modified to deal with ranges
694738fd1498Szrj instead of constants.
694838fd1498Szrj
694938fd1498Szrj While propagating ranges, we may find that two or more SSA name
695038fd1498Szrj have equivalent, though distinct ranges. For instance,
695138fd1498Szrj
695238fd1498Szrj 1 x_9 = p_3->a;
695338fd1498Szrj 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
695438fd1498Szrj 3 if (p_4 == q_2)
695538fd1498Szrj 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
695638fd1498Szrj 5 endif
695738fd1498Szrj 6 if (q_2)
695838fd1498Szrj
695938fd1498Szrj In the code above, pointer p_5 has range [q_2, q_2], but from the
696038fd1498Szrj code we can also determine that p_5 cannot be NULL and, if q_2 had
696138fd1498Szrj a non-varying range, p_5's range should also be compatible with it.
696238fd1498Szrj
696338fd1498Szrj These equivalences are created by two expressions: ASSERT_EXPR and
696438fd1498Szrj copy operations. Since p_5 is an assertion on p_4, and p_4 was the
696538fd1498Szrj result of another assertion, then we can use the fact that p_5 and
696638fd1498Szrj p_4 are equivalent when evaluating p_5's range.
696738fd1498Szrj
696838fd1498Szrj Together with value ranges, we also propagate these equivalences
696938fd1498Szrj between names so that we can take advantage of information from
697038fd1498Szrj multiple ranges when doing final replacement. Note that this
697138fd1498Szrj equivalency relation is transitive but not symmetric.
697238fd1498Szrj
697338fd1498Szrj In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
697438fd1498Szrj cannot assert that q_2 is equivalent to p_5 because q_2 may be used
697538fd1498Szrj in contexts where that assertion does not hold (e.g., in line 6).
697638fd1498Szrj
697738fd1498Szrj TODO, the main difference between this pass and Patterson's is that
697838fd1498Szrj we do not propagate edge probabilities. We only compute whether
697938fd1498Szrj edges can be taken or not. That is, instead of having a spectrum
698038fd1498Szrj of jump probabilities between 0 and 1, we only deal with 0, 1 and
698138fd1498Szrj DON'T KNOW. In the future, it may be worthwhile to propagate
698238fd1498Szrj probabilities to aid branch prediction. */
698338fd1498Szrj
698438fd1498Szrj static unsigned int
execute_vrp(bool warn_array_bounds_p)698538fd1498Szrj execute_vrp (bool warn_array_bounds_p)
698638fd1498Szrj {
698738fd1498Szrj int i;
698838fd1498Szrj edge e;
698938fd1498Szrj switch_update *su;
699038fd1498Szrj
699138fd1498Szrj loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
699238fd1498Szrj rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
699338fd1498Szrj scev_initialize ();
699438fd1498Szrj
699538fd1498Szrj /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
699638fd1498Szrj Inserting assertions may split edges which will invalidate
699738fd1498Szrj EDGE_DFS_BACK. */
699838fd1498Szrj insert_range_assertions ();
699938fd1498Szrj
700038fd1498Szrj to_remove_edges.create (10);
700138fd1498Szrj to_update_switch_stmts.create (5);
700238fd1498Szrj threadedge_initialize_values ();
700338fd1498Szrj
700438fd1498Szrj /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
700538fd1498Szrj mark_dfs_back_edges ();
700638fd1498Szrj
700738fd1498Szrj class vrp_prop vrp_prop;
700838fd1498Szrj vrp_prop.vrp_initialize ();
700938fd1498Szrj vrp_prop.ssa_propagate ();
701038fd1498Szrj vrp_prop.vrp_finalize (warn_array_bounds_p);
701138fd1498Szrj
701238fd1498Szrj /* We must identify jump threading opportunities before we release
701338fd1498Szrj the datastructures built by VRP. */
701438fd1498Szrj identify_jump_threads (&vrp_prop.vr_values);
701538fd1498Szrj
701638fd1498Szrj /* A comparison of an SSA_NAME against a constant where the SSA_NAME
701738fd1498Szrj was set by a type conversion can often be rewritten to use the
701838fd1498Szrj RHS of the type conversion.
701938fd1498Szrj
702038fd1498Szrj However, doing so inhibits jump threading through the comparison.
702138fd1498Szrj So that transformation is not performed until after jump threading
702238fd1498Szrj is complete. */
702338fd1498Szrj basic_block bb;
702438fd1498Szrj FOR_EACH_BB_FN (bb, cfun)
702538fd1498Szrj {
702638fd1498Szrj gimple *last = last_stmt (bb);
702738fd1498Szrj if (last && gimple_code (last) == GIMPLE_COND)
702838fd1498Szrj vrp_prop.vr_values.simplify_cond_using_ranges_2 (as_a <gcond *> (last));
702938fd1498Szrj }
703038fd1498Szrj
703138fd1498Szrj free_numbers_of_iterations_estimates (cfun);
703238fd1498Szrj
703338fd1498Szrj /* ASSERT_EXPRs must be removed before finalizing jump threads
703438fd1498Szrj as finalizing jump threads calls the CFG cleanup code which
703538fd1498Szrj does not properly handle ASSERT_EXPRs. */
703638fd1498Szrj remove_range_assertions ();
703738fd1498Szrj
703838fd1498Szrj /* If we exposed any new variables, go ahead and put them into
703938fd1498Szrj SSA form now, before we handle jump threading. This simplifies
704038fd1498Szrj interactions between rewriting of _DECL nodes into SSA form
704138fd1498Szrj and rewriting SSA_NAME nodes into SSA form after block
704238fd1498Szrj duplication and CFG manipulation. */
704338fd1498Szrj update_ssa (TODO_update_ssa);
704438fd1498Szrj
704538fd1498Szrj /* We identified all the jump threading opportunities earlier, but could
704638fd1498Szrj not transform the CFG at that time. This routine transforms the
704738fd1498Szrj CFG and arranges for the dominator tree to be rebuilt if necessary.
704838fd1498Szrj
704938fd1498Szrj Note the SSA graph update will occur during the normal TODO
705038fd1498Szrj processing by the pass manager. */
705138fd1498Szrj thread_through_all_blocks (false);
705238fd1498Szrj
705338fd1498Szrj /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
705438fd1498Szrj CFG in a broken state and requires a cfg_cleanup run. */
705538fd1498Szrj FOR_EACH_VEC_ELT (to_remove_edges, i, e)
705638fd1498Szrj remove_edge (e);
705738fd1498Szrj /* Update SWITCH_EXPR case label vector. */
705838fd1498Szrj FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
705938fd1498Szrj {
706038fd1498Szrj size_t j;
706138fd1498Szrj size_t n = TREE_VEC_LENGTH (su->vec);
706238fd1498Szrj tree label;
706338fd1498Szrj gimple_switch_set_num_labels (su->stmt, n);
706438fd1498Szrj for (j = 0; j < n; j++)
706538fd1498Szrj gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
706638fd1498Szrj /* As we may have replaced the default label with a regular one
706738fd1498Szrj make sure to make it a real default label again. This ensures
706838fd1498Szrj optimal expansion. */
706938fd1498Szrj label = gimple_switch_label (su->stmt, 0);
707038fd1498Szrj CASE_LOW (label) = NULL_TREE;
707138fd1498Szrj CASE_HIGH (label) = NULL_TREE;
707238fd1498Szrj }
707338fd1498Szrj
707438fd1498Szrj if (to_remove_edges.length () > 0)
707538fd1498Szrj {
707638fd1498Szrj free_dominance_info (CDI_DOMINATORS);
707738fd1498Szrj loops_state_set (LOOPS_NEED_FIXUP);
707838fd1498Szrj }
707938fd1498Szrj
708038fd1498Szrj to_remove_edges.release ();
708138fd1498Szrj to_update_switch_stmts.release ();
708238fd1498Szrj threadedge_finalize_values ();
708338fd1498Szrj
708438fd1498Szrj scev_finalize ();
708538fd1498Szrj loop_optimizer_finalize ();
708638fd1498Szrj return 0;
708738fd1498Szrj }
708838fd1498Szrj
708938fd1498Szrj namespace {
709038fd1498Szrj
709138fd1498Szrj const pass_data pass_data_vrp =
709238fd1498Szrj {
709338fd1498Szrj GIMPLE_PASS, /* type */
709438fd1498Szrj "vrp", /* name */
709538fd1498Szrj OPTGROUP_NONE, /* optinfo_flags */
709638fd1498Szrj TV_TREE_VRP, /* tv_id */
709738fd1498Szrj PROP_ssa, /* properties_required */
709838fd1498Szrj 0, /* properties_provided */
709938fd1498Szrj 0, /* properties_destroyed */
710038fd1498Szrj 0, /* todo_flags_start */
710138fd1498Szrj ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
710238fd1498Szrj };
710338fd1498Szrj
710438fd1498Szrj class pass_vrp : public gimple_opt_pass
710538fd1498Szrj {
710638fd1498Szrj public:
pass_vrp(gcc::context * ctxt)710738fd1498Szrj pass_vrp (gcc::context *ctxt)
710838fd1498Szrj : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
710938fd1498Szrj {}
711038fd1498Szrj
711138fd1498Szrj /* opt_pass methods: */
clone()711238fd1498Szrj opt_pass * clone () { return new pass_vrp (m_ctxt); }
set_pass_param(unsigned int n,bool param)711338fd1498Szrj void set_pass_param (unsigned int n, bool param)
711438fd1498Szrj {
711538fd1498Szrj gcc_assert (n == 0);
711638fd1498Szrj warn_array_bounds_p = param;
711738fd1498Szrj }
gate(function *)711838fd1498Szrj virtual bool gate (function *) { return flag_tree_vrp != 0; }
execute(function *)711938fd1498Szrj virtual unsigned int execute (function *)
712038fd1498Szrj { return execute_vrp (warn_array_bounds_p); }
712138fd1498Szrj
712238fd1498Szrj private:
712338fd1498Szrj bool warn_array_bounds_p;
712438fd1498Szrj }; // class pass_vrp
712538fd1498Szrj
712638fd1498Szrj } // anon namespace
712738fd1498Szrj
712838fd1498Szrj gimple_opt_pass *
make_pass_vrp(gcc::context * ctxt)712938fd1498Szrj make_pass_vrp (gcc::context *ctxt)
713038fd1498Szrj {
713138fd1498Szrj return new pass_vrp (ctxt);
713238fd1498Szrj }
7133