1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "coretypes.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
34 #include "diagnostic.h"
36 #include "tree-scalar-evolution.h"
37 #include "tree-ssa-propagate.h"
38 #include "tree-chrec.h"
40 /* Set of SSA names found during the dominator traversal of a
41 sub-graph in find_assert_locations. */
42 static sbitmap found_in_subgraph;
44 /* Local functions. */
45 static int compare_values (tree val1, tree val2);
47 /* Location information for ASSERT_EXPRs. Each instance of this
48 structure describes an ASSERT_EXPR for an SSA name. Since a single
49 SSA name may have more than one assertion associated with it, these
50 locations are kept in a linked list attached to the corresponding
54 /* Basic block where the assertion would be inserted. */
57 /* Some assertions need to be inserted on an edge (e.g., assertions
58 generated by COND_EXPRs). In those cases, BB will be NULL. */
61 /* Pointer to the statement that generated this assertion. */
62 block_stmt_iterator si;
64 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
65 enum tree_code comp_code;
67 /* Value being compared against. */
70 /* Next node in the linked list. */
71 struct assert_locus_d *next;
74 typedef struct assert_locus_d *assert_locus_t;
76 /* If bit I is present, it means that SSA name N_i has a list of
77 assertions that should be inserted in the IL. */
78 static bitmap need_assert_for;
80 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
81 holds a list of ASSERT_LOCUS_T nodes that describe where
82 ASSERT_EXPRs for SSA name N_I should be inserted. */
83 static assert_locus_t *asserts_for;
85 /* Set of blocks visited in find_assert_locations. Used to avoid
86 visiting the same block more than once. */
87 static sbitmap blocks_visited;
89 /* Value range array. After propagation, VR_VALUE[I] holds the range
90 of values that SSA name N_I may take. */
91 static value_range_t **vr_value;
94 /* Return true if ARG is marked with the nonnull attribute in the
95 current function signature. */
98 nonnull_arg_p (tree arg)
100 tree t, attrs, fntype;
101 unsigned HOST_WIDE_INT arg_num;
103 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
105 fntype = TREE_TYPE (current_function_decl);
106 attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype));
108 /* If "nonnull" wasn't specified, we know nothing about the argument. */
109 if (attrs == NULL_TREE)
112 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
113 if (TREE_VALUE (attrs) == NULL_TREE)
116 /* Get the position number for ARG in the function signature. */
117 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
119 t = TREE_CHAIN (t), arg_num++)
125 gcc_assert (t == arg);
127 /* Now see if ARG_NUM is mentioned in the nonnull list. */
128 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
130 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
138 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
141 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
142 tree max, bitmap equiv)
144 #if defined ENABLE_CHECKING
145 /* Check the validity of the range. */
146 if (t == VR_RANGE || t == VR_ANTI_RANGE)
150 gcc_assert (min && max);
152 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
153 gcc_assert (min != TYPE_MIN_VALUE (TREE_TYPE (min))
154 || max != TYPE_MAX_VALUE (TREE_TYPE (max)));
156 cmp = compare_values (min, max);
157 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
160 if (t == VR_UNDEFINED || t == VR_VARYING)
161 gcc_assert (min == NULL_TREE && max == NULL_TREE);
163 if (t == VR_UNDEFINED || t == VR_VARYING)
164 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
171 /* Since updating the equivalence set involves deep copying the
172 bitmaps, only do it if absolutely necessary. */
173 if (vr->equiv == NULL)
174 vr->equiv = BITMAP_ALLOC (NULL);
176 if (equiv != vr->equiv)
178 if (equiv && !bitmap_empty_p (equiv))
179 bitmap_copy (vr->equiv, equiv);
181 bitmap_clear (vr->equiv);
186 /* Copy value range FROM into value range TO. */
189 copy_value_range (value_range_t *to, value_range_t *from)
191 set_value_range (to, from->type, from->min, from->max, from->equiv);
194 /* Set value range VR to a non-negative range of type TYPE. */
197 set_value_range_to_nonnegative (value_range_t *vr, tree type)
199 tree zero = build_int_cst (type, 0);
200 set_value_range (vr, VR_RANGE, zero, TYPE_MAX_VALUE (type), vr->equiv);
203 /* Set value range VR to a non-NULL range of type TYPE. */
206 set_value_range_to_nonnull (value_range_t *vr, tree type)
208 tree zero = build_int_cst (type, 0);
209 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
213 /* Set value range VR to a NULL range of type TYPE. */
216 set_value_range_to_null (value_range_t *vr, tree type)
218 tree zero = build_int_cst (type, 0);
219 set_value_range (vr, VR_RANGE, zero, zero, vr->equiv);
223 /* Set value range VR to VR_VARYING. */
226 set_value_range_to_varying (value_range_t *vr)
228 vr->type = VR_VARYING;
229 vr->min = vr->max = NULL_TREE;
231 bitmap_clear (vr->equiv);
235 /* Set value range VR to VR_UNDEFINED. */
238 set_value_range_to_undefined (value_range_t *vr)
240 vr->type = VR_UNDEFINED;
241 vr->min = vr->max = NULL_TREE;
243 bitmap_clear (vr->equiv);
247 /* Return value range information for VAR.
249 If we have no values ranges recorded (ie, VRP is not running), then
250 return NULL. Otherwise create an empty range if none existed for VAR. */
252 static value_range_t *
253 get_value_range (tree var)
257 unsigned ver = SSA_NAME_VERSION (var);
259 /* If we have no recorded ranges, then return NULL. */
267 /* Create a default value range. */
268 vr_value[ver] = vr = XCNEW (value_range_t);
270 /* Allocate an equivalence set. */
271 vr->equiv = BITMAP_ALLOC (NULL);
273 /* If VAR is a default definition, the variable can take any value
275 sym = SSA_NAME_VAR (var);
276 if (var == gimple_default_def (cfun, sym))
278 /* Try to use the "nonnull" attribute to create ~[0, 0]
279 anti-ranges for pointers. Note that this is only valid with
280 default definitions of PARM_DECLs. */
281 if (TREE_CODE (sym) == PARM_DECL
282 && POINTER_TYPE_P (TREE_TYPE (sym))
283 && nonnull_arg_p (sym))
284 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
286 set_value_range_to_varying (vr);
292 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
295 vrp_operand_equal_p (tree val1, tree val2)
299 && operand_equal_p (val1, val2, 0)));
302 /* Return true, if the bitmaps B1 and B2 are equal. */
305 vrp_bitmap_equal_p (bitmap b1, bitmap b2)
309 && bitmap_equal_p (b1, b2)));
312 /* Update the value range and equivalence set for variable VAR to
313 NEW_VR. Return true if NEW_VR is different from VAR's previous
316 NOTE: This function assumes that NEW_VR is a temporary value range
317 object created for the sole purpose of updating VAR's range. The
318 storage used by the equivalence set from NEW_VR will be freed by
319 this function. Do not call update_value_range when NEW_VR
320 is the range object associated with another SSA name. */
323 update_value_range (tree var, value_range_t *new_vr)
325 value_range_t *old_vr;
328 /* Update the value range, if necessary. */
329 old_vr = get_value_range (var);
330 is_new = old_vr->type != new_vr->type
331 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
332 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
333 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
336 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
339 BITMAP_FREE (new_vr->equiv);
340 new_vr->equiv = NULL;
346 /* Add VAR and VAR's equivalence set to EQUIV. */
349 add_equivalence (bitmap equiv, tree var)
351 unsigned ver = SSA_NAME_VERSION (var);
352 value_range_t *vr = vr_value[ver];
354 bitmap_set_bit (equiv, ver);
356 bitmap_ior_into (equiv, vr->equiv);
360 /* Return true if VR is ~[0, 0]. */
363 range_is_nonnull (value_range_t *vr)
365 return vr->type == VR_ANTI_RANGE
366 && integer_zerop (vr->min)
367 && integer_zerop (vr->max);
371 /* Return true if VR is [0, 0]. */
374 range_is_null (value_range_t *vr)
376 return vr->type == VR_RANGE
377 && integer_zerop (vr->min)
378 && integer_zerop (vr->max);
382 /* Return true if value range VR involves at least one symbol. */
385 symbolic_range_p (value_range_t *vr)
387 return (!is_gimple_min_invariant (vr->min)
388 || !is_gimple_min_invariant (vr->max));
391 /* Like tree_expr_nonnegative_p, but this function uses value ranges
395 vrp_expr_computes_nonnegative (tree expr)
397 return tree_expr_nonnegative_p (expr);
400 /* Like tree_expr_nonzero_p, but this function uses value ranges
404 vrp_expr_computes_nonzero (tree expr)
406 if (tree_expr_nonzero_p (expr))
409 /* If we have an expression of the form &X->a, then the expression
410 is nonnull if X is nonnull. */
411 if (TREE_CODE (expr) == ADDR_EXPR)
413 tree base = get_base_address (TREE_OPERAND (expr, 0));
415 if (base != NULL_TREE
416 && TREE_CODE (base) == INDIRECT_REF
417 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
419 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
420 if (range_is_nonnull (vr))
428 /* Returns true if EXPR is a valid value (as expected by compare_values) --
429 a gimple invariant, or SSA_NAME +- CST. */
432 valid_value_p (tree expr)
434 if (TREE_CODE (expr) == SSA_NAME)
437 if (TREE_CODE (expr) == PLUS_EXPR
438 || TREE_CODE (expr) == MINUS_EXPR)
439 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
440 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
442 return is_gimple_min_invariant (expr);
445 /* Compare two values VAL1 and VAL2. Return
447 -2 if VAL1 and VAL2 cannot be compared at compile-time,
450 +1 if VAL1 > VAL2, and
453 This is similar to tree_int_cst_compare but supports pointer values
454 and values that cannot be compared at compile time. */
457 compare_values (tree val1, tree val2)
462 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
464 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
465 == POINTER_TYPE_P (TREE_TYPE (val2)));
467 if ((TREE_CODE (val1) == SSA_NAME
468 || TREE_CODE (val1) == PLUS_EXPR
469 || TREE_CODE (val1) == MINUS_EXPR)
470 && (TREE_CODE (val2) == SSA_NAME
471 || TREE_CODE (val2) == PLUS_EXPR
472 || TREE_CODE (val2) == MINUS_EXPR))
475 enum tree_code code1, code2;
477 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
478 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
479 same name, return -2. */
480 if (TREE_CODE (val1) == SSA_NAME)
488 code1 = TREE_CODE (val1);
489 n1 = TREE_OPERAND (val1, 0);
490 c1 = TREE_OPERAND (val1, 1);
491 if (tree_int_cst_sgn (c1) == -1)
493 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
496 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
500 if (TREE_CODE (val2) == SSA_NAME)
508 code2 = TREE_CODE (val2);
509 n2 = TREE_OPERAND (val2, 0);
510 c2 = TREE_OPERAND (val2, 1);
511 if (tree_int_cst_sgn (c2) == -1)
513 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
516 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
520 /* Both values must use the same name. */
524 if (code1 == SSA_NAME
525 && code2 == SSA_NAME)
529 /* If overflow is defined we cannot simplify more. */
530 if (TYPE_UNSIGNED (TREE_TYPE (val1))
534 if (code1 == SSA_NAME)
536 if (code2 == PLUS_EXPR)
537 /* NAME < NAME + CST */
539 else if (code2 == MINUS_EXPR)
540 /* NAME > NAME - CST */
543 else if (code1 == PLUS_EXPR)
545 if (code2 == SSA_NAME)
546 /* NAME + CST > NAME */
548 else if (code2 == PLUS_EXPR)
549 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
550 return compare_values (c1, c2);
551 else if (code2 == MINUS_EXPR)
552 /* NAME + CST1 > NAME - CST2 */
555 else if (code1 == MINUS_EXPR)
557 if (code2 == SSA_NAME)
558 /* NAME - CST < NAME */
560 else if (code2 == PLUS_EXPR)
561 /* NAME - CST1 < NAME + CST2 */
563 else if (code2 == MINUS_EXPR)
564 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
565 C1 and C2 are swapped in the call to compare_values. */
566 return compare_values (c2, c1);
572 /* We cannot compare non-constants. */
573 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
576 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
578 /* We cannot compare overflowed values. */
579 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
582 return tree_int_cst_compare (val1, val2);
588 /* First see if VAL1 and VAL2 are not the same. */
589 if (val1 == val2 || operand_equal_p (val1, val2, 0))
592 /* If VAL1 is a lower address than VAL2, return -1. */
593 t = fold_binary (LT_EXPR, boolean_type_node, val1, val2);
594 if (t == boolean_true_node)
597 /* If VAL1 is a higher address than VAL2, return +1. */
598 t = fold_binary (GT_EXPR, boolean_type_node, val1, val2);
599 if (t == boolean_true_node)
602 /* If VAL1 is different than VAL2, return +2. */
603 t = fold_binary (NE_EXPR, boolean_type_node, val1, val2);
604 if (t == boolean_true_node)
612 /* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
613 0 if VAL is not inside VR,
614 -2 if we cannot tell either way.
616 FIXME, the current semantics of this functions are a bit quirky
617 when taken in the context of VRP. In here we do not care
618 about VR's type. If VR is the anti-range ~[3, 5] the call
619 value_inside_range (4, VR) will return 1.
621 This is counter-intuitive in a strict sense, but the callers
622 currently expect this. They are calling the function
623 merely to determine whether VR->MIN <= VAL <= VR->MAX. The
624 callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
627 This also applies to value_ranges_intersect_p and
628 range_includes_zero_p. The semantics of VR_RANGE and
629 VR_ANTI_RANGE should be encoded here, but that also means
630 adapting the users of these functions to the new semantics. */
633 value_inside_range (tree val, value_range_t *vr)
637 cmp1 = fold_binary_to_constant (GE_EXPR, boolean_type_node, val, vr->min);
641 cmp2 = fold_binary_to_constant (LE_EXPR, boolean_type_node, val, vr->max);
645 return cmp1 == boolean_true_node && cmp2 == boolean_true_node;
649 /* Return true if value ranges VR0 and VR1 have a non-empty
653 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
655 return (value_inside_range (vr1->min, vr0) == 1
656 || value_inside_range (vr1->max, vr0) == 1
657 || value_inside_range (vr0->min, vr1) == 1
658 || value_inside_range (vr0->max, vr1) == 1);
662 /* Return true if VR includes the value zero, false otherwise. FIXME,
663 currently this will return false for an anti-range like ~[-4, 3].
664 This will be wrong when the semantics of value_inside_range are
665 modified (currently the users of this function expect these
669 range_includes_zero_p (value_range_t *vr)
673 gcc_assert (vr->type != VR_UNDEFINED
674 && vr->type != VR_VARYING
675 && !symbolic_range_p (vr));
677 zero = build_int_cst (TREE_TYPE (vr->min), 0);
678 return (value_inside_range (zero, vr) == 1);
681 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
682 false otherwise or if no value range information is available. */
685 ssa_name_nonnegative_p (tree t)
687 value_range_t *vr = get_value_range (t);
692 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
693 which would return a useful value should be encoded as a VR_RANGE. */
694 if (vr->type == VR_RANGE)
696 int result = compare_values (vr->min, integer_zero_node);
698 return (result == 0 || result == 1);
703 /* Return true if T, an SSA_NAME, is known to be nonzero. Return
704 false otherwise or if no value range information is available. */
707 ssa_name_nonzero_p (tree t)
709 value_range_t *vr = get_value_range (t);
714 /* A VR_RANGE which does not include zero is a nonzero value. */
715 if (vr->type == VR_RANGE && !symbolic_range_p (vr))
716 return ! range_includes_zero_p (vr);
718 /* A VR_ANTI_RANGE which does include zero is a nonzero value. */
719 if (vr->type == VR_ANTI_RANGE && !symbolic_range_p (vr))
720 return range_includes_zero_p (vr);
726 /* Extract value range information from an ASSERT_EXPR EXPR and store
730 extract_range_from_assert (value_range_t *vr_p, tree expr)
732 tree var, cond, limit, min, max, type;
733 value_range_t *var_vr, *limit_vr;
734 enum tree_code cond_code;
736 var = ASSERT_EXPR_VAR (expr);
737 cond = ASSERT_EXPR_COND (expr);
739 gcc_assert (COMPARISON_CLASS_P (cond));
741 /* Find VAR in the ASSERT_EXPR conditional. */
742 if (var == TREE_OPERAND (cond, 0))
744 /* If the predicate is of the form VAR COMP LIMIT, then we just
745 take LIMIT from the RHS and use the same comparison code. */
746 limit = TREE_OPERAND (cond, 1);
747 cond_code = TREE_CODE (cond);
751 /* If the predicate is of the form LIMIT COMP VAR, then we need
752 to flip around the comparison code to create the proper range
754 limit = TREE_OPERAND (cond, 0);
755 cond_code = swap_tree_comparison (TREE_CODE (cond));
758 type = TREE_TYPE (limit);
759 gcc_assert (limit != var);
761 /* For pointer arithmetic, we only keep track of pointer equality
763 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
765 set_value_range_to_varying (vr_p);
769 /* If LIMIT is another SSA name and LIMIT has a range of its own,
770 try to use LIMIT's range to avoid creating symbolic ranges
772 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
774 /* LIMIT's range is only interesting if it has any useful information. */
776 && (limit_vr->type == VR_UNDEFINED
777 || limit_vr->type == VR_VARYING
778 || symbolic_range_p (limit_vr)))
781 /* Initially, the new range has the same set of equivalences of
782 VAR's range. This will be revised before returning the final
783 value. Since assertions may be chained via mutually exclusive
784 predicates, we will need to trim the set of equivalences before
786 gcc_assert (vr_p->equiv == NULL);
787 vr_p->equiv = BITMAP_ALLOC (NULL);
788 add_equivalence (vr_p->equiv, var);
790 /* Extract a new range based on the asserted comparison for VAR and
791 LIMIT's value range. Notice that if LIMIT has an anti-range, we
792 will only use it for equality comparisons (EQ_EXPR). For any
793 other kind of assertion, we cannot derive a range from LIMIT's
794 anti-range that can be used to describe the new range. For
795 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
796 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
797 no single range for x_2 that could describe LE_EXPR, so we might
798 as well build the range [b_4, +INF] for it. */
799 if (cond_code == EQ_EXPR)
801 enum value_range_type range_type;
805 range_type = limit_vr->type;
811 range_type = VR_RANGE;
816 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
818 /* When asserting the equality VAR == LIMIT and LIMIT is another
819 SSA name, the new range will also inherit the equivalence set
821 if (TREE_CODE (limit) == SSA_NAME)
822 add_equivalence (vr_p->equiv, limit);
824 else if (cond_code == NE_EXPR)
826 /* As described above, when LIMIT's range is an anti-range and
827 this assertion is an inequality (NE_EXPR), then we cannot
828 derive anything from the anti-range. For instance, if
829 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
830 not imply that VAR's range is [0, 0]. So, in the case of
831 anti-ranges, we just assert the inequality using LIMIT and
834 If LIMIT_VR is a range, we can only use it to build a new
835 anti-range if LIMIT_VR is a single-valued range. For
836 instance, if LIMIT_VR is [0, 1], the predicate
837 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
838 Rather, it means that for value 0 VAR should be ~[0, 0]
839 and for value 1, VAR should be ~[1, 1]. We cannot
840 represent these ranges.
842 The only situation in which we can build a valid
843 anti-range is when LIMIT_VR is a single-valued range
844 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
845 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
847 && limit_vr->type == VR_RANGE
848 && compare_values (limit_vr->min, limit_vr->max) == 0)
855 /* In any other case, we cannot use LIMIT's range to build a
860 /* If MIN and MAX cover the whole range for their type, then
861 just use the original LIMIT. */
862 if (INTEGRAL_TYPE_P (type)
863 && min == TYPE_MIN_VALUE (type)
864 && max == TYPE_MAX_VALUE (type))
867 set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
869 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
871 min = TYPE_MIN_VALUE (type);
873 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
877 /* If LIMIT_VR is of the form [N1, N2], we need to build the
878 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
883 /* If the maximum value forces us to be out of bounds, simply punt.
884 It would be pointless to try and do anything more since this
885 all should be optimized away above us. */
886 if (cond_code == LT_EXPR && compare_values (max, min) == 0)
887 set_value_range_to_varying (vr_p);
890 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
891 if (cond_code == LT_EXPR)
893 tree one = build_int_cst (type, 1);
894 max = fold_build2 (MINUS_EXPR, type, max, one);
897 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
900 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
902 max = TYPE_MAX_VALUE (type);
904 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
908 /* If LIMIT_VR is of the form [N1, N2], we need to build the
909 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
914 /* If the minimum value forces us to be out of bounds, simply punt.
915 It would be pointless to try and do anything more since this
916 all should be optimized away above us. */
917 if (cond_code == GT_EXPR && compare_values (min, max) == 0)
918 set_value_range_to_varying (vr_p);
921 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
922 if (cond_code == GT_EXPR)
924 tree one = build_int_cst (type, 1);
925 min = fold_build2 (PLUS_EXPR, type, min, one);
928 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
934 /* If VAR already had a known range, it may happen that the new
935 range we have computed and VAR's range are not compatible. For
939 p_6 = ASSERT_EXPR <p_5, p_5 == NULL>;
941 p_8 = ASSERT_EXPR <p_6, p_6 != NULL>;
943 While the above comes from a faulty program, it will cause an ICE
944 later because p_8 and p_6 will have incompatible ranges and at
945 the same time will be considered equivalent. A similar situation
949 i_6 = ASSERT_EXPR <i_5, i_5 > 10>;
951 i_7 = ASSERT_EXPR <i_6, i_6 < 5>;
953 Again i_6 and i_7 will have incompatible ranges. It would be
954 pointless to try and do anything with i_7's range because
955 anything dominated by 'if (i_5 < 5)' will be optimized away.
956 Note, due to the wa in which simulation proceeds, the statement
957 i_7 = ASSERT_EXPR <...> we would never be visited because the
958 conditional 'if (i_5 < 5)' always evaluates to false. However,
959 this extra check does not hurt and may protect against future
960 changes to VRP that may get into a situation similar to the
961 NULL pointer dereference example.
963 Note that these compatibility tests are only needed when dealing
964 with ranges or a mix of range and anti-range. If VAR_VR and VR_P
965 are both anti-ranges, they will always be compatible, because two
966 anti-ranges will always have a non-empty intersection. */
968 var_vr = get_value_range (var);
970 /* We may need to make adjustments when VR_P and VAR_VR are numeric
971 ranges or anti-ranges. */
972 if (vr_p->type == VR_VARYING
973 || vr_p->type == VR_UNDEFINED
974 || var_vr->type == VR_VARYING
975 || var_vr->type == VR_UNDEFINED
976 || symbolic_range_p (vr_p)
977 || symbolic_range_p (var_vr))
980 if (var_vr->type == VR_RANGE && vr_p->type == VR_RANGE)
982 /* If the two ranges have a non-empty intersection, we can
983 refine the resulting range. Since the assert expression
984 creates an equivalency and at the same time it asserts a
985 predicate, we can take the intersection of the two ranges to
986 get better precision. */
987 if (value_ranges_intersect_p (var_vr, vr_p))
989 /* Use the larger of the two minimums. */
990 if (compare_values (vr_p->min, var_vr->min) == -1)
995 /* Use the smaller of the two maximums. */
996 if (compare_values (vr_p->max, var_vr->max) == 1)
1001 set_value_range (vr_p, vr_p->type, min, max, vr_p->equiv);
1005 /* The two ranges do not intersect, set the new range to
1006 VARYING, because we will not be able to do anything
1007 meaningful with it. */
1008 set_value_range_to_varying (vr_p);
1011 else if ((var_vr->type == VR_RANGE && vr_p->type == VR_ANTI_RANGE)
1012 || (var_vr->type == VR_ANTI_RANGE && vr_p->type == VR_RANGE))
1014 /* A range and an anti-range will cancel each other only if
1015 their ends are the same. For instance, in the example above,
1016 p_8's range ~[0, 0] and p_6's range [0, 0] are incompatible,
1017 so VR_P should be set to VR_VARYING. */
1018 if (compare_values (var_vr->min, vr_p->min) == 0
1019 && compare_values (var_vr->max, vr_p->max) == 0)
1020 set_value_range_to_varying (vr_p);
1023 tree min, max, anti_min, anti_max, real_min, real_max;
1025 /* We want to compute the logical AND of the two ranges;
1026 there are three cases to consider.
1029 1. The VR_ANTI_RANGE range is completely within the
1030 VR_RANGE and the endpoints of the ranges are
1031 different. In that case the resulting range
1032 should be whichever range is more precise.
1033 Typically that will be the VR_RANGE.
1035 2. The VR_ANTI_RANGE is completely disjoint from
1036 the VR_RANGE. In this case the resulting range
1037 should be the VR_RANGE.
1039 3. There is some overlap between the VR_ANTI_RANGE
1042 3a. If the high limit of the VR_ANTI_RANGE resides
1043 within the VR_RANGE, then the result is a new
1044 VR_RANGE starting at the high limit of the
1045 the VR_ANTI_RANGE + 1 and extending to the
1046 high limit of the original VR_RANGE.
1048 3b. If the low limit of the VR_ANTI_RANGE resides
1049 within the VR_RANGE, then the result is a new
1050 VR_RANGE starting at the low limit of the original
1051 VR_RANGE and extending to the low limit of the
1052 VR_ANTI_RANGE - 1. */
1053 if (vr_p->type == VR_ANTI_RANGE)
1055 anti_min = vr_p->min;
1056 anti_max = vr_p->max;
1057 real_min = var_vr->min;
1058 real_max = var_vr->max;
1062 anti_min = var_vr->min;
1063 anti_max = var_vr->max;
1064 real_min = vr_p->min;
1065 real_max = vr_p->max;
1069 /* Case 1, VR_ANTI_RANGE completely within VR_RANGE,
1070 not including any endpoints. */
1071 if (compare_values (anti_max, real_max) == -1
1072 && compare_values (anti_min, real_min) == 1)
1074 set_value_range (vr_p, VR_RANGE, real_min,
1075 real_max, vr_p->equiv);
1077 /* Case 2, VR_ANTI_RANGE completely disjoint from
1079 else if (compare_values (anti_min, real_max) == 1
1080 || compare_values (anti_max, real_min) == -1)
1082 set_value_range (vr_p, VR_RANGE, real_min,
1083 real_max, vr_p->equiv);
1085 /* Case 3a, the anti-range extends into the low
1086 part of the real range. Thus creating a new
1087 low for the real range. */
1088 else if ((compare_values (anti_max, real_min) == 1
1089 || compare_values (anti_max, real_min) == 0)
1090 && compare_values (anti_max, real_max) == -1)
1092 min = fold_build2 (PLUS_EXPR, TREE_TYPE (var_vr->min),
1094 build_int_cst (TREE_TYPE (var_vr->min), 1));
1096 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1098 /* Case 3b, the anti-range extends into the high
1099 part of the real range. Thus creating a new
1100 higher for the real range. */
1101 else if (compare_values (anti_min, real_min) == 1
1102 && (compare_values (anti_min, real_max) == -1
1103 || compare_values (anti_min, real_max) == 0))
1105 max = fold_build2 (MINUS_EXPR, TREE_TYPE (var_vr->min),
1107 build_int_cst (TREE_TYPE (var_vr->min), 1));
1109 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1116 /* Extract range information from SSA name VAR and store it in VR. If
1117 VAR has an interesting range, use it. Otherwise, create the
1118 range [VAR, VAR] and return it. This is useful in situations where
1119 we may have conditionals testing values of VARYING names. For
1126 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1130 extract_range_from_ssa_name (value_range_t *vr, tree var)
1132 value_range_t *var_vr = get_value_range (var);
1134 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1135 copy_value_range (vr, var_vr);
1137 set_value_range (vr, VR_RANGE, var, var, NULL);
1139 add_equivalence (vr->equiv, var);
1143 /* Wrapper around int_const_binop. If the operation overflows and we
1144 are not using wrapping arithmetic, then adjust the result to be
1145 -INF or +INF depending on CODE, VAL1 and VAL2. */
1148 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1152 res = int_const_binop (code, val1, val2, 0);
1154 /* If we are not using wrapping arithmetic, operate symbolically
1155 on -INF and +INF. */
1156 if (TYPE_UNSIGNED (TREE_TYPE (val1))
1159 int checkz = compare_values (res, val1);
1160 bool overflow = false;
1162 /* Ensure that res = val1 [+*] val2 >= val1
1163 or that res = val1 - val2 <= val1. */
1164 if ((code == PLUS_EXPR
1165 && !(checkz == 1 || checkz == 0))
1166 || (code == MINUS_EXPR
1167 && !(checkz == 0 || checkz == -1)))
1171 /* Checking for multiplication overflow is done by dividing the
1172 output of the multiplication by the first input of the
1173 multiplication. If the result of that division operation is
1174 not equal to the second input of the multiplication, then the
1175 multiplication overflowed. */
1176 else if (code == MULT_EXPR && !integer_zerop (val1))
1178 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1181 int check = compare_values (tmp, val2);
1189 res = copy_node (res);
1190 TREE_OVERFLOW (res) = 1;
1194 else if (TREE_OVERFLOW (res)
1195 && !TREE_OVERFLOW (val1)
1196 && !TREE_OVERFLOW (val2))
1198 /* If the operation overflowed but neither VAL1 nor VAL2 are
1199 overflown, return -INF or +INF depending on the operation
1200 and the combination of signs of the operands. */
1201 int sgn1 = tree_int_cst_sgn (val1);
1202 int sgn2 = tree_int_cst_sgn (val2);
1204 /* Notice that we only need to handle the restricted set of
1205 operations handled by extract_range_from_binary_expr.
1206 Among them, only multiplication, addition and subtraction
1207 can yield overflow without overflown operands because we
1208 are working with integral types only... except in the
1209 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1210 for division too. */
1212 /* For multiplication, the sign of the overflow is given
1213 by the comparison of the signs of the operands. */
1214 if ((code == MULT_EXPR && sgn1 == sgn2)
1215 /* For addition, the operands must be of the same sign
1216 to yield an overflow. Its sign is therefore that
1217 of one of the operands, for example the first. */
1218 || (code == PLUS_EXPR && sgn1 > 0)
1219 /* For subtraction, the operands must be of different
1220 signs to yield an overflow. Its sign is therefore
1221 that of the first operand or the opposite of that
1222 of the second operand. A first operand of 0 counts
1223 as positive here, for the corner case 0 - (-INF),
1224 which overflows, but must yield +INF. */
1225 || (code == MINUS_EXPR && sgn1 >= 0)
1226 /* For division, the only case is -INF / -1 = +INF. */
1227 || code == TRUNC_DIV_EXPR
1228 || code == FLOOR_DIV_EXPR
1229 || code == CEIL_DIV_EXPR
1230 || code == EXACT_DIV_EXPR
1231 || code == ROUND_DIV_EXPR)
1232 return TYPE_MAX_VALUE (TREE_TYPE (res));
1234 return TYPE_MIN_VALUE (TREE_TYPE (res));
1241 /* Extract range information from a binary expression EXPR based on
1242 the ranges of each of its operands and the expression code. */
1245 extract_range_from_binary_expr (value_range_t *vr, tree expr)
1247 enum tree_code code = TREE_CODE (expr);
1248 enum value_range_type type;
1249 tree op0, op1, min, max;
1251 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1252 value_range_t vr1 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1254 /* Not all binary expressions can be applied to ranges in a
1255 meaningful way. Handle only arithmetic operations. */
1256 if (code != PLUS_EXPR
1257 && code != MINUS_EXPR
1258 && code != MULT_EXPR
1259 && code != TRUNC_DIV_EXPR
1260 && code != FLOOR_DIV_EXPR
1261 && code != CEIL_DIV_EXPR
1262 && code != EXACT_DIV_EXPR
1263 && code != ROUND_DIV_EXPR
1266 && code != BIT_AND_EXPR
1267 && code != TRUTH_ANDIF_EXPR
1268 && code != TRUTH_ORIF_EXPR
1269 && code != TRUTH_AND_EXPR
1270 && code != TRUTH_OR_EXPR)
1272 set_value_range_to_varying (vr);
1276 /* Get value ranges for each operand. For constant operands, create
1277 a new value range with the operand to simplify processing. */
1278 op0 = TREE_OPERAND (expr, 0);
1279 if (TREE_CODE (op0) == SSA_NAME)
1280 vr0 = *(get_value_range (op0));
1281 else if (is_gimple_min_invariant (op0))
1282 set_value_range (&vr0, VR_RANGE, op0, op0, NULL);
1284 set_value_range_to_varying (&vr0);
1286 op1 = TREE_OPERAND (expr, 1);
1287 if (TREE_CODE (op1) == SSA_NAME)
1288 vr1 = *(get_value_range (op1));
1289 else if (is_gimple_min_invariant (op1))
1290 set_value_range (&vr1, VR_RANGE, op1, op1, NULL);
1292 set_value_range_to_varying (&vr1);
1294 /* If either range is UNDEFINED, so is the result. */
1295 if (vr0.type == VR_UNDEFINED || vr1.type == VR_UNDEFINED)
1297 set_value_range_to_undefined (vr);
1301 /* The type of the resulting value range defaults to VR0.TYPE. */
1304 /* Refuse to operate on VARYING ranges, ranges of different kinds
1305 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
1306 because we may be able to derive a useful range even if one of
1307 the operands is VR_VARYING or symbolic range. TODO, we may be
1308 able to derive anti-ranges in some cases. */
1309 if (code != BIT_AND_EXPR
1310 && code != TRUTH_AND_EXPR
1311 && code != TRUTH_OR_EXPR
1312 && (vr0.type == VR_VARYING
1313 || vr1.type == VR_VARYING
1314 || vr0.type != vr1.type
1315 || symbolic_range_p (&vr0)
1316 || symbolic_range_p (&vr1)))
1318 set_value_range_to_varying (vr);
1322 /* Now evaluate the expression to determine the new range. */
1323 if (POINTER_TYPE_P (TREE_TYPE (expr))
1324 || POINTER_TYPE_P (TREE_TYPE (op0))
1325 || POINTER_TYPE_P (TREE_TYPE (op1)))
1327 /* For pointer types, we are really only interested in asserting
1328 whether the expression evaluates to non-NULL. FIXME, we used
1329 to gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR), but
1330 ivopts is generating expressions with pointer multiplication
1332 if (code == PLUS_EXPR)
1334 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
1335 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1336 else if (range_is_null (&vr0) && range_is_null (&vr1))
1337 set_value_range_to_null (vr, TREE_TYPE (expr));
1339 set_value_range_to_varying (vr);
1343 /* Subtracting from a pointer, may yield 0, so just drop the
1344 resulting range to varying. */
1345 set_value_range_to_varying (vr);
1351 /* For integer ranges, apply the operation to each end of the
1352 range and see what we end up with. */
1353 if (code == TRUTH_ANDIF_EXPR
1354 || code == TRUTH_ORIF_EXPR
1355 || code == TRUTH_AND_EXPR
1356 || code == TRUTH_OR_EXPR)
1358 /* If one of the operands is zero, we know that the whole
1359 expression evaluates zero. */
1360 if (code == TRUTH_AND_EXPR
1361 && ((vr0.type == VR_RANGE
1362 && integer_zerop (vr0.min)
1363 && integer_zerop (vr0.max))
1364 || (vr1.type == VR_RANGE
1365 && integer_zerop (vr1.min)
1366 && integer_zerop (vr1.max))))
1369 min = max = build_int_cst (TREE_TYPE (expr), 0);
1371 /* If one of the operands is one, we know that the whole
1372 expression evaluates one. */
1373 else if (code == TRUTH_OR_EXPR
1374 && ((vr0.type == VR_RANGE
1375 && integer_onep (vr0.min)
1376 && integer_onep (vr0.max))
1377 || (vr1.type == VR_RANGE
1378 && integer_onep (vr1.min)
1379 && integer_onep (vr1.max))))
1382 min = max = build_int_cst (TREE_TYPE (expr), 1);
1384 else if (vr0.type != VR_VARYING
1385 && vr1.type != VR_VARYING
1386 && vr0.type == vr1.type
1387 && !symbolic_range_p (&vr0)
1388 && !symbolic_range_p (&vr1))
1390 /* Boolean expressions cannot be folded with int_const_binop. */
1391 min = fold_binary (code, TREE_TYPE (expr), vr0.min, vr1.min);
1392 max = fold_binary (code, TREE_TYPE (expr), vr0.max, vr1.max);
1396 set_value_range_to_varying (vr);
1400 else if (code == PLUS_EXPR
1402 || code == MAX_EXPR)
1404 /* If we have a PLUS_EXPR with two VR_ANTI_RANGEs, drop to
1405 VR_VARYING. It would take more effort to compute a precise
1406 range for such a case. For example, if we have op0 == 1 and
1407 op1 == -1 with their ranges both being ~[0,0], we would have
1408 op0 + op1 == 0, so we cannot claim that the sum is in ~[0,0].
1409 Note that we are guaranteed to have vr0.type == vr1.type at
1411 if (code == PLUS_EXPR && vr0.type == VR_ANTI_RANGE)
1413 set_value_range_to_varying (vr);
1417 /* For operations that make the resulting range directly
1418 proportional to the original ranges, apply the operation to
1419 the same end of each range. */
1420 min = vrp_int_const_binop (code, vr0.min, vr1.min);
1421 max = vrp_int_const_binop (code, vr0.max, vr1.max);
1423 else if (code == MULT_EXPR
1424 || code == TRUNC_DIV_EXPR
1425 || code == FLOOR_DIV_EXPR
1426 || code == CEIL_DIV_EXPR
1427 || code == EXACT_DIV_EXPR
1428 || code == ROUND_DIV_EXPR)
1433 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1434 drop to VR_VARYING. It would take more effort to compute a
1435 precise range for such a case. For example, if we have
1436 op0 == 65536 and op1 == 65536 with their ranges both being
1437 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1438 we cannot claim that the product is in ~[0,0]. Note that we
1439 are guaranteed to have vr0.type == vr1.type at this
1441 if (code == MULT_EXPR
1442 && vr0.type == VR_ANTI_RANGE
1443 && (flag_wrapv || TYPE_UNSIGNED (TREE_TYPE (op0))))
1445 set_value_range_to_varying (vr);
1449 /* Multiplications and divisions are a bit tricky to handle,
1450 depending on the mix of signs we have in the two ranges, we
1451 need to operate on different values to get the minimum and
1452 maximum values for the new range. One approach is to figure
1453 out all the variations of range combinations and do the
1456 However, this involves several calls to compare_values and it
1457 is pretty convoluted. It's simpler to do the 4 operations
1458 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1459 MAX1) and then figure the smallest and largest values to form
1462 /* Divisions by zero result in a VARYING value. */
1463 if (code != MULT_EXPR
1464 && (vr0.type == VR_ANTI_RANGE || range_includes_zero_p (&vr1)))
1466 set_value_range_to_varying (vr);
1470 /* Compute the 4 cross operations. */
1471 val[0] = vrp_int_const_binop (code, vr0.min, vr1.min);
1473 val[1] = (vr1.max != vr1.min)
1474 ? vrp_int_const_binop (code, vr0.min, vr1.max)
1477 val[2] = (vr0.max != vr0.min)
1478 ? vrp_int_const_binop (code, vr0.max, vr1.min)
1481 val[3] = (vr0.min != vr0.max && vr1.min != vr1.max)
1482 ? vrp_int_const_binop (code, vr0.max, vr1.max)
1485 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
1489 for (i = 1; i < 4; i++)
1491 if (!is_gimple_min_invariant (min) || TREE_OVERFLOW (min)
1492 || !is_gimple_min_invariant (max) || TREE_OVERFLOW (max))
1497 if (!is_gimple_min_invariant (val[i]) || TREE_OVERFLOW (val[i]))
1499 /* If we found an overflowed value, set MIN and MAX
1500 to it so that we set the resulting range to
1506 if (compare_values (val[i], min) == -1)
1509 if (compare_values (val[i], max) == 1)
1514 else if (code == MINUS_EXPR)
1516 /* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
1517 VR_VARYING. It would take more effort to compute a precise
1518 range for such a case. For example, if we have op0 == 1 and
1519 op1 == 1 with their ranges both being ~[0,0], we would have
1520 op0 - op1 == 0, so we cannot claim that the difference is in
1521 ~[0,0]. Note that we are guaranteed to have
1522 vr0.type == vr1.type at this point. */
1523 if (vr0.type == VR_ANTI_RANGE)
1525 set_value_range_to_varying (vr);
1529 /* For MINUS_EXPR, apply the operation to the opposite ends of
1531 min = vrp_int_const_binop (code, vr0.min, vr1.max);
1532 max = vrp_int_const_binop (code, vr0.max, vr1.min);
1534 else if (code == BIT_AND_EXPR)
1536 if (vr0.type == VR_RANGE
1537 && vr0.min == vr0.max
1538 && tree_expr_nonnegative_p (vr0.max)
1539 && TREE_CODE (vr0.max) == INTEGER_CST)
1541 min = build_int_cst (TREE_TYPE (expr), 0);
1544 else if (vr1.type == VR_RANGE
1545 && vr1.min == vr1.max
1546 && tree_expr_nonnegative_p (vr1.max)
1547 && TREE_CODE (vr1.max) == INTEGER_CST)
1550 min = build_int_cst (TREE_TYPE (expr), 0);
1555 set_value_range_to_varying (vr);
1562 /* If either MIN or MAX overflowed, then set the resulting range to
1564 if (!is_gimple_min_invariant (min) || TREE_OVERFLOW (min)
1565 || !is_gimple_min_invariant (max) || TREE_OVERFLOW (max))
1567 set_value_range_to_varying (vr);
1571 cmp = compare_values (min, max);
1572 if (cmp == -2 || cmp == 1)
1574 /* If the new range has its limits swapped around (MIN > MAX),
1575 then the operation caused one of them to wrap around, mark
1576 the new range VARYING. */
1577 set_value_range_to_varying (vr);
1580 set_value_range (vr, type, min, max, NULL);
1584 /* Extract range information from a unary expression EXPR based on
1585 the range of its operand and the expression code. */
1588 extract_range_from_unary_expr (value_range_t *vr, tree expr)
1590 enum tree_code code = TREE_CODE (expr);
1593 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1595 /* Refuse to operate on certain unary expressions for which we
1596 cannot easily determine a resulting range. */
1597 if (code == FIX_TRUNC_EXPR
1598 || code == FLOAT_EXPR
1599 || code == BIT_NOT_EXPR
1600 || code == NON_LVALUE_EXPR
1601 || code == CONJ_EXPR)
1603 set_value_range_to_varying (vr);
1607 /* Get value ranges for the operand. For constant operands, create
1608 a new value range with the operand to simplify processing. */
1609 op0 = TREE_OPERAND (expr, 0);
1610 if (TREE_CODE (op0) == SSA_NAME)
1611 vr0 = *(get_value_range (op0));
1612 else if (is_gimple_min_invariant (op0))
1613 set_value_range (&vr0, VR_RANGE, op0, op0, NULL);
1615 set_value_range_to_varying (&vr0);
1617 /* If VR0 is UNDEFINED, so is the result. */
1618 if (vr0.type == VR_UNDEFINED)
1620 set_value_range_to_undefined (vr);
1624 /* Refuse to operate on symbolic ranges, or if neither operand is
1625 a pointer or integral type. */
1626 if ((!INTEGRAL_TYPE_P (TREE_TYPE (op0))
1627 && !POINTER_TYPE_P (TREE_TYPE (op0)))
1628 || (vr0.type != VR_VARYING
1629 && symbolic_range_p (&vr0)))
1631 set_value_range_to_varying (vr);
1635 /* If the expression involves pointers, we are only interested in
1636 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
1637 if (POINTER_TYPE_P (TREE_TYPE (expr)) || POINTER_TYPE_P (TREE_TYPE (op0)))
1639 if (range_is_nonnull (&vr0) || tree_expr_nonzero_p (expr))
1640 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1641 else if (range_is_null (&vr0))
1642 set_value_range_to_null (vr, TREE_TYPE (expr));
1644 set_value_range_to_varying (vr);
1649 /* Handle unary expressions on integer ranges. */
1650 if (code == NOP_EXPR || code == CONVERT_EXPR)
1652 tree inner_type = TREE_TYPE (op0);
1653 tree outer_type = TREE_TYPE (expr);
1655 /* If VR0 represents a simple range, then try to convert
1656 the min and max values for the range to the same type
1657 as OUTER_TYPE. If the results compare equal to VR0's
1658 min and max values and the new min is still less than
1659 or equal to the new max, then we can safely use the newly
1660 computed range for EXPR. This allows us to compute
1661 accurate ranges through many casts. */
1662 if (vr0.type == VR_RANGE
1663 || (vr0.type == VR_VARYING
1664 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)))
1666 tree new_min, new_max, orig_min, orig_max;
1668 /* Convert the input operand min/max to OUTER_TYPE. If
1669 the input has no range information, then use the min/max
1670 for the input's type. */
1671 if (vr0.type == VR_RANGE)
1678 orig_min = TYPE_MIN_VALUE (inner_type);
1679 orig_max = TYPE_MAX_VALUE (inner_type);
1682 new_min = fold_convert (outer_type, orig_min);
1683 new_max = fold_convert (outer_type, orig_max);
1685 /* Verify the new min/max values are gimple values and
1686 that they compare equal to the original input's
1688 if (is_gimple_val (new_min)
1689 && is_gimple_val (new_max)
1690 && tree_int_cst_equal (new_min, orig_min)
1691 && tree_int_cst_equal (new_max, orig_max)
1692 && compare_values (new_min, new_max) <= 0
1693 && compare_values (new_min, new_max) >= -1)
1695 set_value_range (vr, VR_RANGE, new_min, new_max, vr->equiv);
1700 /* When converting types of different sizes, set the result to
1701 VARYING. Things like sign extensions and precision loss may
1702 change the range. For instance, if x_3 is of type 'long long
1703 int' and 'y_5 = (unsigned short) x_3', if x_3 is ~[0, 0], it
1704 is impossible to know at compile time whether y_5 will be
1706 if (TYPE_SIZE (inner_type) != TYPE_SIZE (outer_type)
1707 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
1709 set_value_range_to_varying (vr);
1714 /* Conversion of a VR_VARYING value to a wider type can result
1715 in a usable range. So wait until after we've handled conversions
1716 before dropping the result to VR_VARYING if we had a source
1717 operand that is VR_VARYING. */
1718 if (vr0.type == VR_VARYING)
1720 set_value_range_to_varying (vr);
1724 /* Apply the operation to each end of the range and see what we end
1726 if (code == NEGATE_EXPR
1727 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
1729 /* NEGATE_EXPR flips the range around. We need to treat
1730 TYPE_MIN_VALUE specially dependent on wrapping, range type
1731 and if it was used as minimum or maximum value:
1732 -~[MIN, MIN] == ~[MIN, MIN]
1733 -[MIN, 0] == [0, MAX] for -fno-wrapv
1734 -[MIN, 0] == [0, MIN] for -fwrapv (will be set to varying later) */
1735 min = vr0.max == TYPE_MIN_VALUE (TREE_TYPE (expr))
1736 ? TYPE_MIN_VALUE (TREE_TYPE (expr))
1737 : fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1739 max = vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr))
1740 ? (vr0.type == VR_ANTI_RANGE || flag_wrapv
1741 ? TYPE_MIN_VALUE (TREE_TYPE (expr))
1742 : TYPE_MAX_VALUE (TREE_TYPE (expr)))
1743 : fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
1746 else if (code == NEGATE_EXPR
1747 && TYPE_UNSIGNED (TREE_TYPE (expr)))
1749 if (!range_includes_zero_p (&vr0))
1751 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
1752 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1756 if (range_is_null (&vr0))
1757 set_value_range_to_null (vr, TREE_TYPE (expr));
1759 set_value_range_to_varying (vr);
1763 else if (code == ABS_EXPR
1764 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
1766 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
1769 && ((vr0.type == VR_RANGE
1770 && vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr)))
1771 || (vr0.type == VR_ANTI_RANGE
1772 && vr0.min != TYPE_MIN_VALUE (TREE_TYPE (expr))
1773 && !range_includes_zero_p (&vr0))))
1775 set_value_range_to_varying (vr);
1779 /* ABS_EXPR may flip the range around, if the original range
1780 included negative values. */
1781 min = (vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr)))
1782 ? TYPE_MAX_VALUE (TREE_TYPE (expr))
1783 : fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
1785 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1787 cmp = compare_values (min, max);
1789 /* If a VR_ANTI_RANGEs contains zero, then we have
1790 ~[-INF, min(MIN, MAX)]. */
1791 if (vr0.type == VR_ANTI_RANGE)
1793 if (range_includes_zero_p (&vr0))
1795 tree type_min_value = TYPE_MIN_VALUE (TREE_TYPE (expr));
1797 /* Take the lower of the two values. */
1801 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
1802 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
1803 flag_wrapv is set and the original anti-range doesn't include
1804 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
1805 min = (flag_wrapv && vr0.min != type_min_value
1806 ? int_const_binop (PLUS_EXPR,
1808 integer_one_node, 0)
1813 /* All else has failed, so create the range [0, INF], even for
1814 flag_wrapv since TYPE_MIN_VALUE is in the original
1816 vr0.type = VR_RANGE;
1817 min = build_int_cst (TREE_TYPE (expr), 0);
1818 max = TYPE_MAX_VALUE (TREE_TYPE (expr));
1822 /* If the range contains zero then we know that the minimum value in the
1823 range will be zero. */
1824 else if (range_includes_zero_p (&vr0))
1828 min = build_int_cst (TREE_TYPE (expr), 0);
1832 /* If the range was reversed, swap MIN and MAX. */
1843 /* Otherwise, operate on each end of the range. */
1844 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
1845 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1848 cmp = compare_values (min, max);
1849 if (cmp == -2 || cmp == 1)
1851 /* If the new range has its limits swapped around (MIN > MAX),
1852 then the operation caused one of them to wrap around, mark
1853 the new range VARYING. */
1854 set_value_range_to_varying (vr);
1857 set_value_range (vr, vr0.type, min, max, NULL);
1861 /* Extract range information from a comparison expression EXPR based
1862 on the range of its operand and the expression code. */
1865 extract_range_from_comparison (value_range_t *vr, tree expr)
1867 tree val = vrp_evaluate_conditional (expr, false);
1870 /* Since this expression was found on the RHS of an assignment,
1871 its type may be different from _Bool. Convert VAL to EXPR's
1873 val = fold_convert (TREE_TYPE (expr), val);
1874 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
1877 set_value_range_to_varying (vr);
1881 /* Try to compute a useful range out of expression EXPR and store it
1885 extract_range_from_expr (value_range_t *vr, tree expr)
1887 enum tree_code code = TREE_CODE (expr);
1889 if (code == ASSERT_EXPR)
1890 extract_range_from_assert (vr, expr);
1891 else if (code == SSA_NAME)
1892 extract_range_from_ssa_name (vr, expr);
1893 else if (TREE_CODE_CLASS (code) == tcc_binary
1894 || code == TRUTH_ANDIF_EXPR
1895 || code == TRUTH_ORIF_EXPR
1896 || code == TRUTH_AND_EXPR
1897 || code == TRUTH_OR_EXPR
1898 || code == TRUTH_XOR_EXPR)
1899 extract_range_from_binary_expr (vr, expr);
1900 else if (TREE_CODE_CLASS (code) == tcc_unary)
1901 extract_range_from_unary_expr (vr, expr);
1902 else if (TREE_CODE_CLASS (code) == tcc_comparison)
1903 extract_range_from_comparison (vr, expr);
1904 else if (is_gimple_min_invariant (expr))
1905 set_value_range (vr, VR_RANGE, expr, expr, NULL);
1907 set_value_range_to_varying (vr);
1909 /* If we got a varying range from the tests above, try a final
1910 time to derive a nonnegative or nonzero range. This time
1911 relying primarily on generic routines in fold in conjunction
1913 if (vr->type == VR_VARYING)
1915 if (INTEGRAL_TYPE_P (TREE_TYPE (expr))
1916 && vrp_expr_computes_nonnegative (expr))
1917 set_value_range_to_nonnegative (vr, TREE_TYPE (expr));
1918 else if (vrp_expr_computes_nonzero (expr))
1919 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1923 /* Given a range VR, a LOOP and a variable VAR, determine whether it
1924 would be profitable to adjust VR using scalar evolution information
1925 for VAR. If so, update VR with the new limits. */
1928 adjust_range_with_scev (value_range_t *vr, struct loop *loop, tree stmt,
1931 tree init, step, chrec, tmin, tmax, min, max, type;
1932 enum ev_direction dir;
1934 /* TODO. Don't adjust anti-ranges. An anti-range may provide
1935 better opportunities than a regular range, but I'm not sure. */
1936 if (vr->type == VR_ANTI_RANGE)
1939 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
1940 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
1943 init = initial_condition_in_loop_num (chrec, loop->num);
1944 step = evolution_part_in_loop_num (chrec, loop->num);
1946 /* If STEP is symbolic, we can't know whether INIT will be the
1947 minimum or maximum value in the range. Also, unless INIT is
1948 a simple expression, compare_values and possibly other functions
1949 in tree-vrp won't be able to handle it. */
1950 if (step == NULL_TREE
1951 || !is_gimple_min_invariant (step)
1952 || !valid_value_p (init))
1955 dir = scev_direction (chrec);
1956 if (/* Do not adjust ranges if we do not know whether the iv increases
1957 or decreases, ... */
1958 dir == EV_DIR_UNKNOWN
1959 /* ... or if it may wrap. */
1960 || scev_probably_wraps_p (init, step, stmt,
1961 current_loops->parray[CHREC_VARIABLE (chrec)],
1965 type = TREE_TYPE (var);
1966 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
1967 tmin = lower_bound_in_type (type, type);
1969 tmin = TYPE_MIN_VALUE (type);
1970 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
1971 tmax = upper_bound_in_type (type, type);
1973 tmax = TYPE_MAX_VALUE (type);
1975 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
1980 /* For VARYING or UNDEFINED ranges, just about anything we get
1981 from scalar evolutions should be better. */
1983 if (dir == EV_DIR_DECREASES)
1988 /* If we would create an invalid range, then just assume we
1989 know absolutely nothing. This may be over-conservative,
1990 but it's clearly safe, and should happen only in unreachable
1991 parts of code, or for invalid programs. */
1992 if (compare_values (min, max) == 1)
1995 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
1997 else if (vr->type == VR_RANGE)
2002 if (dir == EV_DIR_DECREASES)
2004 /* INIT is the maximum value. If INIT is lower than VR->MAX
2005 but no smaller than VR->MIN, set VR->MAX to INIT. */
2006 if (compare_values (init, max) == -1)
2010 /* If we just created an invalid range with the minimum
2011 greater than the maximum, we fail conservatively.
2012 This should happen only in unreachable
2013 parts of code, or for invalid programs. */
2014 if (compare_values (min, max) == 1)
2020 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
2021 if (compare_values (init, min) == 1)
2025 /* Again, avoid creating invalid range by failing. */
2026 if (compare_values (min, max) == 1)
2031 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2036 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
2038 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
2039 all the values in the ranges.
2041 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
2043 - Return NULL_TREE if it is not always possible to determine the
2044 value of the comparison. */
2048 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1)
2050 /* VARYING or UNDEFINED ranges cannot be compared. */
2051 if (vr0->type == VR_VARYING
2052 || vr0->type == VR_UNDEFINED
2053 || vr1->type == VR_VARYING
2054 || vr1->type == VR_UNDEFINED)
2057 /* Anti-ranges need to be handled separately. */
2058 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
2060 /* If both are anti-ranges, then we cannot compute any
2062 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
2065 /* These comparisons are never statically computable. */
2072 /* Equality can be computed only between a range and an
2073 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
2074 if (vr0->type == VR_RANGE)
2076 /* To simplify processing, make VR0 the anti-range. */
2077 value_range_t *tmp = vr0;
2082 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
2084 if (compare_values (vr0->min, vr1->min) == 0
2085 && compare_values (vr0->max, vr1->max) == 0)
2086 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2091 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
2092 operands around and change the comparison code. */
2093 if (comp == GT_EXPR || comp == GE_EXPR)
2096 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
2102 if (comp == EQ_EXPR)
2104 /* Equality may only be computed if both ranges represent
2105 exactly one value. */
2106 if (compare_values (vr0->min, vr0->max) == 0
2107 && compare_values (vr1->min, vr1->max) == 0)
2109 int cmp_min = compare_values (vr0->min, vr1->min);
2110 int cmp_max = compare_values (vr0->max, vr1->max);
2111 if (cmp_min == 0 && cmp_max == 0)
2112 return boolean_true_node;
2113 else if (cmp_min != -2 && cmp_max != -2)
2114 return boolean_false_node;
2116 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
2117 else if (compare_values (vr0->min, vr1->max) == 1
2118 || compare_values (vr1->min, vr0->max) == 1)
2119 return boolean_false_node;
2123 else if (comp == NE_EXPR)
2127 /* If VR0 is completely to the left or completely to the right
2128 of VR1, they are always different. Notice that we need to
2129 make sure that both comparisons yield similar results to
2130 avoid comparing values that cannot be compared at
2132 cmp1 = compare_values (vr0->max, vr1->min);
2133 cmp2 = compare_values (vr0->min, vr1->max);
2134 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
2135 return boolean_true_node;
2137 /* If VR0 and VR1 represent a single value and are identical,
2139 else if (compare_values (vr0->min, vr0->max) == 0
2140 && compare_values (vr1->min, vr1->max) == 0
2141 && compare_values (vr0->min, vr1->min) == 0
2142 && compare_values (vr0->max, vr1->max) == 0)
2143 return boolean_false_node;
2145 /* Otherwise, they may or may not be different. */
2149 else if (comp == LT_EXPR || comp == LE_EXPR)
2153 /* If VR0 is to the left of VR1, return true. */
2154 tst = compare_values (vr0->max, vr1->min);
2155 if ((comp == LT_EXPR && tst == -1)
2156 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2157 return boolean_true_node;
2159 /* If VR0 is to the right of VR1, return false. */
2160 tst = compare_values (vr0->min, vr1->max);
2161 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2162 || (comp == LE_EXPR && tst == 1))
2163 return boolean_false_node;
2165 /* Otherwise, we don't know. */
2173 /* Given a value range VR, a value VAL and a comparison code COMP, return
2174 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
2175 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
2176 always returns false. Return NULL_TREE if it is not always
2177 possible to determine the value of the comparison. */
2180 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val)
2182 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2185 /* Anti-ranges need to be handled separately. */
2186 if (vr->type == VR_ANTI_RANGE)
2188 /* For anti-ranges, the only predicates that we can compute at
2189 compile time are equality and inequality. */
2196 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
2197 if (value_inside_range (val, vr) == 1)
2198 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2203 if (comp == EQ_EXPR)
2205 /* EQ_EXPR may only be computed if VR represents exactly
2207 if (compare_values (vr->min, vr->max) == 0)
2209 int cmp = compare_values (vr->min, val);
2211 return boolean_true_node;
2212 else if (cmp == -1 || cmp == 1 || cmp == 2)
2213 return boolean_false_node;
2215 else if (compare_values (val, vr->min) == -1
2216 || compare_values (vr->max, val) == -1)
2217 return boolean_false_node;
2221 else if (comp == NE_EXPR)
2223 /* If VAL is not inside VR, then they are always different. */
2224 if (compare_values (vr->max, val) == -1
2225 || compare_values (vr->min, val) == 1)
2226 return boolean_true_node;
2228 /* If VR represents exactly one value equal to VAL, then return
2230 if (compare_values (vr->min, vr->max) == 0
2231 && compare_values (vr->min, val) == 0)
2232 return boolean_false_node;
2234 /* Otherwise, they may or may not be different. */
2237 else if (comp == LT_EXPR || comp == LE_EXPR)
2241 /* If VR is to the left of VAL, return true. */
2242 tst = compare_values (vr->max, val);
2243 if ((comp == LT_EXPR && tst == -1)
2244 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2245 return boolean_true_node;
2247 /* If VR is to the right of VAL, return false. */
2248 tst = compare_values (vr->min, val);
2249 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2250 || (comp == LE_EXPR && tst == 1))
2251 return boolean_false_node;
2253 /* Otherwise, we don't know. */
2256 else if (comp == GT_EXPR || comp == GE_EXPR)
2260 /* If VR is to the right of VAL, return true. */
2261 tst = compare_values (vr->min, val);
2262 if ((comp == GT_EXPR && tst == 1)
2263 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
2264 return boolean_true_node;
2266 /* If VR is to the left of VAL, return false. */
2267 tst = compare_values (vr->max, val);
2268 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
2269 || (comp == GE_EXPR && tst == -1))
2270 return boolean_false_node;
2272 /* Otherwise, we don't know. */
2280 /* Debugging dumps. */
2282 void dump_value_range (FILE *, value_range_t *);
2283 void debug_value_range (value_range_t *);
2284 void dump_all_value_ranges (FILE *);
2285 void debug_all_value_ranges (void);
2286 void dump_vr_equiv (FILE *, bitmap);
2287 void debug_vr_equiv (bitmap);
2290 /* Dump value range VR to FILE. */
2293 dump_value_range (FILE *file, value_range_t *vr)
2296 fprintf (file, "[]");
2297 else if (vr->type == VR_UNDEFINED)
2298 fprintf (file, "UNDEFINED");
2299 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
2301 tree type = TREE_TYPE (vr->min);
2303 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
2305 if (INTEGRAL_TYPE_P (type)
2306 && !TYPE_UNSIGNED (type)
2307 && vr->min == TYPE_MIN_VALUE (type))
2308 fprintf (file, "-INF");
2310 print_generic_expr (file, vr->min, 0);
2312 fprintf (file, ", ");
2314 if (INTEGRAL_TYPE_P (type)
2315 && vr->max == TYPE_MAX_VALUE (type))
2316 fprintf (file, "+INF");
2318 print_generic_expr (file, vr->max, 0);
2320 fprintf (file, "]");
2327 fprintf (file, " EQUIVALENCES: { ");
2329 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
2331 print_generic_expr (file, ssa_name (i), 0);
2332 fprintf (file, " ");
2336 fprintf (file, "} (%u elements)", c);
2339 else if (vr->type == VR_VARYING)
2340 fprintf (file, "VARYING");
2342 fprintf (file, "INVALID RANGE");
2346 /* Dump value range VR to stderr. */
2349 debug_value_range (value_range_t *vr)
2351 dump_value_range (stderr, vr);
2352 fprintf (stderr, "\n");
2356 /* Dump value ranges of all SSA_NAMEs to FILE. */
2359 dump_all_value_ranges (FILE *file)
2363 for (i = 0; i < num_ssa_names; i++)
2367 print_generic_expr (file, ssa_name (i), 0);
2368 fprintf (file, ": ");
2369 dump_value_range (file, vr_value[i]);
2370 fprintf (file, "\n");
2374 fprintf (file, "\n");
2378 /* Dump all value ranges to stderr. */
2381 debug_all_value_ranges (void)
2383 dump_all_value_ranges (stderr);
2387 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2388 create a new SSA name N and return the assertion assignment
2389 'V = ASSERT_EXPR <V, V OP W>'. */
2392 build_assert_expr_for (tree cond, tree v)
2396 gcc_assert (TREE_CODE (v) == SSA_NAME);
2397 n = duplicate_ssa_name (v, NULL_TREE);
2399 if (COMPARISON_CLASS_P (cond))
2401 tree a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
2402 assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, a);
2404 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
2406 /* Given !V, build the assignment N = false. */
2407 tree op0 = TREE_OPERAND (cond, 0);
2408 gcc_assert (op0 == v);
2409 assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, boolean_false_node);
2411 else if (TREE_CODE (cond) == SSA_NAME)
2413 /* Given V, build the assignment N = true. */
2414 gcc_assert (v == cond);
2415 assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, boolean_true_node);
2420 SSA_NAME_DEF_STMT (n) = assertion;
2422 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2423 operand of the ASSERT_EXPR. Register the new name and the old one
2424 in the replacement table so that we can fix the SSA web after
2425 adding all the ASSERT_EXPRs. */
2426 register_new_name_mapping (n, v);
2432 /* Return false if EXPR is a predicate expression involving floating
2436 fp_predicate (tree expr)
2438 return (COMPARISON_CLASS_P (expr)
2439 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0))));
2443 /* If the range of values taken by OP can be inferred after STMT executes,
2444 return the comparison code (COMP_CODE_P) and value (VAL_P) that
2445 describes the inferred range. Return true if a range could be
2449 infer_value_range (tree stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
2452 *comp_code_p = ERROR_MARK;
2454 /* Do not attempt to infer anything in names that flow through
2456 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
2459 /* Similarly, don't infer anything from statements that may throw
2461 if (tree_could_throw_p (stmt))
2464 /* If STMT is the last statement of a basic block with no
2465 successors, there is no point inferring anything about any of its
2466 operands. We would not be able to find a proper insertion point
2467 for the assertion, anyway. */
2468 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (bb_for_stmt (stmt)->succs) == 0)
2471 /* We can only assume that a pointer dereference will yield
2472 non-NULL if -fdelete-null-pointer-checks is enabled. */
2473 if (flag_delete_null_pointer_checks && POINTER_TYPE_P (TREE_TYPE (op)))
2476 unsigned num_uses, num_derefs;
2478 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs, &is_store);
2481 *val_p = build_int_cst (TREE_TYPE (op), 0);
2482 *comp_code_p = NE_EXPR;
2491 void dump_asserts_for (FILE *, tree);
2492 void debug_asserts_for (tree);
2493 void dump_all_asserts (FILE *);
2494 void debug_all_asserts (void);
2496 /* Dump all the registered assertions for NAME to FILE. */
2499 dump_asserts_for (FILE *file, tree name)
2503 fprintf (file, "Assertions to be inserted for ");
2504 print_generic_expr (file, name, 0);
2505 fprintf (file, "\n");
2507 loc = asserts_for[SSA_NAME_VERSION (name)];
2510 fprintf (file, "\t");
2511 print_generic_expr (file, bsi_stmt (loc->si), 0);
2512 fprintf (file, "\n\tBB #%d", loc->bb->index);
2515 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
2516 loc->e->dest->index);
2517 dump_edge_info (file, loc->e, 0);
2519 fprintf (file, "\n\tPREDICATE: ");
2520 print_generic_expr (file, name, 0);
2521 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
2522 print_generic_expr (file, loc->val, 0);
2523 fprintf (file, "\n\n");
2527 fprintf (file, "\n");
2531 /* Dump all the registered assertions for NAME to stderr. */
2534 debug_asserts_for (tree name)
2536 dump_asserts_for (stderr, name);
2540 /* Dump all the registered assertions for all the names to FILE. */
2543 dump_all_asserts (FILE *file)
2548 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
2549 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
2550 dump_asserts_for (file, ssa_name (i));
2551 fprintf (file, "\n");
2555 /* Dump all the registered assertions for all the names to stderr. */
2558 debug_all_asserts (void)
2560 dump_all_asserts (stderr);
2564 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2565 'NAME COMP_CODE VAL' at a location that dominates block BB or
2566 E->DEST, then register this location as a possible insertion point
2567 for ASSERT_EXPR <NAME, NAME COMP_CODE VAL>.
2569 BB, E and SI provide the exact insertion point for the new
2570 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2571 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2572 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2573 must not be NULL. */
2576 register_new_assert_for (tree name,
2577 enum tree_code comp_code,
2581 block_stmt_iterator si)
2583 assert_locus_t n, loc, last_loc;
2585 basic_block dest_bb;
2587 #if defined ENABLE_CHECKING
2588 gcc_assert (bb == NULL || e == NULL);
2591 gcc_assert (TREE_CODE (bsi_stmt (si)) != COND_EXPR
2592 && TREE_CODE (bsi_stmt (si)) != SWITCH_EXPR);
2595 /* The new assertion A will be inserted at BB or E. We need to
2596 determine if the new location is dominated by a previously
2597 registered location for A. If we are doing an edge insertion,
2598 assume that A will be inserted at E->DEST. Note that this is not
2601 If E is a critical edge, it will be split. But even if E is
2602 split, the new block will dominate the same set of blocks that
2605 The reverse, however, is not true, blocks dominated by E->DEST
2606 will not be dominated by the new block created to split E. So,
2607 if the insertion location is on a critical edge, we will not use
2608 the new location to move another assertion previously registered
2609 at a block dominated by E->DEST. */
2610 dest_bb = (bb) ? bb : e->dest;
2612 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2613 VAL at a block dominating DEST_BB, then we don't need to insert a new
2614 one. Similarly, if the same assertion already exists at a block
2615 dominated by DEST_BB and the new location is not on a critical
2616 edge, then update the existing location for the assertion (i.e.,
2617 move the assertion up in the dominance tree).
2619 Note, this is implemented as a simple linked list because there
2620 should not be more than a handful of assertions registered per
2621 name. If this becomes a performance problem, a table hashed by
2622 COMP_CODE and VAL could be implemented. */
2623 loc = asserts_for[SSA_NAME_VERSION (name)];
2628 if (loc->comp_code == comp_code
2630 || operand_equal_p (loc->val, val, 0)))
2632 /* If the assertion NAME COMP_CODE VAL has already been
2633 registered at a basic block that dominates DEST_BB, then
2634 we don't need to insert the same assertion again. Note
2635 that we don't check strict dominance here to avoid
2636 replicating the same assertion inside the same basic
2637 block more than once (e.g., when a pointer is
2638 dereferenced several times inside a block).
2640 An exception to this rule are edge insertions. If the
2641 new assertion is to be inserted on edge E, then it will
2642 dominate all the other insertions that we may want to
2643 insert in DEST_BB. So, if we are doing an edge
2644 insertion, don't do this dominance check. */
2646 && dominated_by_p (CDI_DOMINATORS, dest_bb, loc->bb))
2649 /* Otherwise, if E is not a critical edge and DEST_BB
2650 dominates the existing location for the assertion, move
2651 the assertion up in the dominance tree by updating its
2652 location information. */
2653 if ((e == NULL || !EDGE_CRITICAL_P (e))
2654 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
2663 /* Update the last node of the list and move to the next one. */
2668 /* If we didn't find an assertion already registered for
2669 NAME COMP_CODE VAL, add a new one at the end of the list of
2670 assertions associated with NAME. */
2671 n = XNEW (struct assert_locus_d);
2675 n->comp_code = comp_code;
2682 asserts_for[SSA_NAME_VERSION (name)] = n;
2684 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
2687 /* COND is a predicate which uses NAME. Extract a suitable test code
2688 and value and store them into *CODE_P and *VAL_P so the predicate
2689 is normalized to NAME *CODE_P *VAL_P.
2691 If no extraction was possible, return FALSE, otherwise return TRUE.
2693 If INVERT is true, then we invert the result stored into *CODE_P. */
2696 extract_code_and_val_from_cond (tree name, tree cond, bool invert,
2697 enum tree_code *code_p, tree *val_p)
2699 enum tree_code comp_code;
2702 /* Predicates may be a single SSA name or NAME OP VAL. */
2705 /* If the predicate is a name, it must be NAME, in which
2706 case we create the predicate NAME == true or
2707 NAME == false accordingly. */
2708 comp_code = EQ_EXPR;
2709 val = invert ? boolean_false_node : boolean_true_node;
2713 /* Otherwise, we have a comparison of the form NAME COMP VAL
2714 or VAL COMP NAME. */
2715 if (name == TREE_OPERAND (cond, 1))
2717 /* If the predicate is of the form VAL COMP NAME, flip
2718 COMP around because we need to register NAME as the
2719 first operand in the predicate. */
2720 comp_code = swap_tree_comparison (TREE_CODE (cond));
2721 val = TREE_OPERAND (cond, 0);
2725 /* The comparison is of the form NAME COMP VAL, so the
2726 comparison code remains unchanged. */
2727 comp_code = TREE_CODE (cond);
2728 val = TREE_OPERAND (cond, 1);
2731 /* Invert the comparison code as necessary. */
2733 comp_code = invert_tree_comparison (comp_code, 0);
2735 /* VRP does not handle float types. */
2736 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
2739 /* Do not register always-false predicates.
2740 FIXME: this works around a limitation in fold() when dealing with
2741 enumerations. Given 'enum { N1, N2 } x;', fold will not
2742 fold 'if (x > N2)' to 'if (0)'. */
2743 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
2744 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2746 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
2747 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
2749 if (comp_code == GT_EXPR
2751 || compare_values (val, max) == 0))
2754 if (comp_code == LT_EXPR
2756 || compare_values (val, min) == 0))
2760 *code_p = comp_code;
2765 /* OP is an operand of a truth value expression which is known to have
2766 a particular value. Register any asserts for OP and for any
2767 operands in OP's defining statement.
2769 If CODE is EQ_EXPR, then we want to register OP is zero (false),
2770 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
2773 register_edge_assert_for_1 (tree op, enum tree_code code,
2774 edge e, block_stmt_iterator bsi)
2776 bool retval = false;
2777 tree op_def, rhs, val;
2779 /* We only care about SSA_NAMEs. */
2780 if (TREE_CODE (op) != SSA_NAME)
2783 /* We know that OP will have a zero or nonzero value. If OP is used
2784 more than once go ahead and register an assert for OP.
2786 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
2787 it will always be set for OP (because OP is used in a COND_EXPR in
2789 if (!has_single_use (op))
2791 val = build_int_cst (TREE_TYPE (op), 0);
2792 register_new_assert_for (op, code, val, NULL, e, bsi);
2796 /* Now look at how OP is set. If it's set from a comparison,
2797 a truth operation or some bit operations, then we may be able
2798 to register information about the operands of that assignment. */
2799 op_def = SSA_NAME_DEF_STMT (op);
2800 if (TREE_CODE (op_def) != MODIFY_EXPR)
2803 rhs = TREE_OPERAND (op_def, 1);
2805 if (COMPARISON_CLASS_P (rhs))
2807 bool invert = (code == EQ_EXPR ? true : false);
2808 tree op0 = TREE_OPERAND (rhs, 0);
2809 tree op1 = TREE_OPERAND (rhs, 1);
2811 /* Conditionally register an assert for each SSA_NAME in the
2813 if (TREE_CODE (op0) == SSA_NAME
2814 && !has_single_use (op0)
2815 && extract_code_and_val_from_cond (op0, rhs,
2816 invert, &code, &val))
2818 register_new_assert_for (op0, code, val, NULL, e, bsi);
2822 /* Similarly for the second operand of the comparison. */
2823 if (TREE_CODE (op1) == SSA_NAME
2824 && !has_single_use (op1)
2825 && extract_code_and_val_from_cond (op1, rhs,
2826 invert, &code, &val))
2828 register_new_assert_for (op1, code, val, NULL, e, bsi);
2832 else if ((code == NE_EXPR
2833 && (TREE_CODE (rhs) == TRUTH_AND_EXPR
2834 || TREE_CODE (rhs) == BIT_AND_EXPR))
2836 && (TREE_CODE (rhs) == TRUTH_OR_EXPR
2837 || TREE_CODE (rhs) == BIT_IOR_EXPR)))
2839 /* Recurse on each operand. */
2840 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 0),
2842 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 1),
2845 else if (TREE_CODE (rhs) == TRUTH_NOT_EXPR)
2847 /* Recurse, flipping CODE. */
2848 code = invert_tree_comparison (code, false);
2849 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 0),
2852 else if (TREE_CODE (rhs) == SSA_NAME)
2854 /* Recurse through the copy. */
2855 retval |= register_edge_assert_for_1 (rhs, code, e, bsi);
2857 else if (TREE_CODE (rhs) == NOP_EXPR
2858 || TREE_CODE (rhs) == CONVERT_EXPR
2859 || TREE_CODE (rhs) == VIEW_CONVERT_EXPR
2860 || TREE_CODE (rhs) == NON_LVALUE_EXPR)
2862 /* Recurse through the type conversion. */
2863 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 0),
2870 /* Try to register an edge assertion for SSA name NAME on edge E for
2871 the condition COND contributing to the conditional jump pointed to by SI.
2872 Return true if an assertion for NAME could be registered. */
2875 register_edge_assert_for (tree name, edge e, block_stmt_iterator si, tree cond)
2878 enum tree_code comp_code;
2879 bool retval = false;
2880 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
2882 /* Do not attempt to infer anything in names that flow through
2884 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
2887 if (!extract_code_and_val_from_cond (name, cond, is_else_edge,
2891 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
2892 reachable from E. */
2893 if (TEST_BIT (found_in_subgraph, SSA_NAME_VERSION (name)))
2895 register_new_assert_for (name, comp_code, val, NULL, e, si);
2899 /* If COND is effectively an equality test of an SSA_NAME against
2900 the value zero or one, then we may be able to assert values
2901 for SSA_NAMEs which flow into COND. */
2903 /* In the case of NAME == 1 or NAME != 0, for TRUTH_AND_EXPR defining
2904 statement of NAME we can assert both operands of the TRUTH_AND_EXPR
2905 have non-zero value. */
2906 if (((comp_code == EQ_EXPR && integer_onep (val))
2907 || (comp_code == NE_EXPR && integer_zerop (val))))
2909 tree def_stmt = SSA_NAME_DEF_STMT (name);
2911 if (TREE_CODE (def_stmt) == MODIFY_EXPR
2912 && (TREE_CODE (TREE_OPERAND (def_stmt, 1)) == TRUTH_AND_EXPR
2913 || TREE_CODE (TREE_OPERAND (def_stmt, 1)) == BIT_AND_EXPR))
2915 tree op0 = TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0);
2916 tree op1 = TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 1);
2917 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
2918 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
2922 /* In the case of NAME == 0 or NAME != 1, for TRUTH_OR_EXPR defining
2923 statement of NAME we can assert both operands of the TRUTH_OR_EXPR
2925 if (((comp_code == EQ_EXPR && integer_zerop (val))
2926 || (comp_code == NE_EXPR && integer_onep (val))))
2928 tree def_stmt = SSA_NAME_DEF_STMT (name);
2930 if (TREE_CODE (def_stmt) == MODIFY_EXPR
2931 && (TREE_CODE (TREE_OPERAND (def_stmt, 1)) == TRUTH_OR_EXPR
2932 || TREE_CODE (TREE_OPERAND (def_stmt, 1)) == BIT_IOR_EXPR))
2934 tree op0 = TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0);
2935 tree op1 = TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 1);
2936 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
2937 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
2945 static bool find_assert_locations (basic_block bb);
2947 /* Determine whether the outgoing edges of BB should receive an
2948 ASSERT_EXPR for each of the operands of BB's LAST statement.
2949 The last statement of BB must be a COND_EXPR or a SWITCH_EXPR.
2951 If any of the sub-graphs rooted at BB have an interesting use of
2952 the predicate operands, an assert location node is added to the
2953 list of assertions for the corresponding operands. */
2956 find_conditional_asserts (basic_block bb, tree last)
2959 block_stmt_iterator bsi;
2965 need_assert = false;
2966 bsi = bsi_for_stmt (last);
2968 /* Look for uses of the operands in each of the sub-graphs
2969 rooted at BB. We need to check each of the outgoing edges
2970 separately, so that we know what kind of ASSERT_EXPR to
2972 FOR_EACH_EDGE (e, ei, bb->succs)
2977 /* Remove the COND_EXPR operands from the FOUND_IN_SUBGRAPH bitmap.
2978 Otherwise, when we finish traversing each of the sub-graphs, we
2979 won't know whether the variables were found in the sub-graphs or
2980 if they had been found in a block upstream from BB.
2982 This is actually a bad idea is some cases, particularly jump
2983 threading. Consider a CFG like the following:
2993 Assume that one or more operands in the conditional at the
2994 end of block 0 are used in a conditional in block 2, but not
2995 anywhere in block 1. In this case we will not insert any
2996 assert statements in block 1, which may cause us to miss
2997 opportunities to optimize, particularly for jump threading. */
2998 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
2999 RESET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3001 /* Traverse the strictly dominated sub-graph rooted at E->DEST
3002 to determine if any of the operands in the conditional
3003 predicate are used. */
3005 need_assert |= find_assert_locations (e->dest);
3007 /* Register the necessary assertions for each operand in the
3008 conditional predicate. */
3009 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3010 need_assert |= register_edge_assert_for (op, e, bsi,
3011 COND_EXPR_COND (last));
3014 /* Finally, indicate that we have found the operands in the
3016 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3017 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3023 /* Traverse all the statements in block BB looking for statements that
3024 may generate useful assertions for the SSA names in their operand.
3025 If a statement produces a useful assertion A for name N_i, then the
3026 list of assertions already generated for N_i is scanned to
3027 determine if A is actually needed.
3029 If N_i already had the assertion A at a location dominating the
3030 current location, then nothing needs to be done. Otherwise, the
3031 new location for A is recorded instead.
3033 1- For every statement S in BB, all the variables used by S are
3034 added to bitmap FOUND_IN_SUBGRAPH.
3036 2- If statement S uses an operand N in a way that exposes a known
3037 value range for N, then if N was not already generated by an
3038 ASSERT_EXPR, create a new assert location for N. For instance,
3039 if N is a pointer and the statement dereferences it, we can
3040 assume that N is not NULL.
3042 3- COND_EXPRs are a special case of #2. We can derive range
3043 information from the predicate but need to insert different
3044 ASSERT_EXPRs for each of the sub-graphs rooted at the
3045 conditional block. If the last statement of BB is a conditional
3046 expression of the form 'X op Y', then
3048 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3050 b) If the conditional is the only entry point to the sub-graph
3051 corresponding to the THEN_CLAUSE, recurse into it. On
3052 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3053 an ASSERT_EXPR is added for the corresponding variable.
3055 c) Repeat step (b) on the ELSE_CLAUSE.
3057 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3066 In this case, an assertion on the THEN clause is useful to
3067 determine that 'a' is always 9 on that edge. However, an assertion
3068 on the ELSE clause would be unnecessary.
3070 4- If BB does not end in a conditional expression, then we recurse
3071 into BB's dominator children.
3073 At the end of the recursive traversal, every SSA name will have a
3074 list of locations where ASSERT_EXPRs should be added. When a new
3075 location for name N is found, it is registered by calling
3076 register_new_assert_for. That function keeps track of all the
3077 registered assertions to prevent adding unnecessary assertions.
3078 For instance, if a pointer P_4 is dereferenced more than once in a
3079 dominator tree, only the location dominating all the dereference of
3080 P_4 will receive an ASSERT_EXPR.
3082 If this function returns true, then it means that there are names
3083 for which we need to generate ASSERT_EXPRs. Those assertions are
3084 inserted by process_assert_insertions.
3086 TODO. Handle SWITCH_EXPR. */
3089 find_assert_locations (basic_block bb)
3091 block_stmt_iterator si;
3096 if (TEST_BIT (blocks_visited, bb->index))
3099 SET_BIT (blocks_visited, bb->index);
3101 need_assert = false;
3103 /* Traverse all PHI nodes in BB marking used operands. */
3104 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3106 use_operand_p arg_p;
3109 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
3111 tree arg = USE_FROM_PTR (arg_p);
3112 if (TREE_CODE (arg) == SSA_NAME)
3114 gcc_assert (is_gimple_reg (PHI_RESULT (phi)));
3115 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (arg));
3120 /* Traverse all the statements in BB marking used names and looking
3121 for statements that may infer assertions for their used operands. */
3123 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3128 stmt = bsi_stmt (si);
3130 /* See if we can derive an assertion for any of STMT's operands. */
3131 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
3134 enum tree_code comp_code;
3136 /* Mark OP in bitmap FOUND_IN_SUBGRAPH. If STMT is inside
3137 the sub-graph of a conditional block, when we return from
3138 this recursive walk, our parent will use the
3139 FOUND_IN_SUBGRAPH bitset to determine if one of the
3140 operands it was looking for was present in the sub-graph. */
3141 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3143 /* If OP is used in such a way that we can infer a value
3144 range for it, and we don't find a previous assertion for
3145 it, create a new assertion location node for OP. */
3146 if (infer_value_range (stmt, op, &comp_code, &value))
3148 /* If we are able to infer a nonzero value range for OP,
3149 then walk backwards through the use-def chain to see if OP
3150 was set via a typecast.
3152 If so, then we can also infer a nonzero value range
3153 for the operand of the NOP_EXPR. */
3154 if (comp_code == NE_EXPR && integer_zerop (value))
3157 tree def_stmt = SSA_NAME_DEF_STMT (t);
3159 while (TREE_CODE (def_stmt) == MODIFY_EXPR
3160 && TREE_CODE (TREE_OPERAND (def_stmt, 1)) == NOP_EXPR
3161 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0)) == SSA_NAME
3162 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0))))
3164 t = TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0);
3165 def_stmt = SSA_NAME_DEF_STMT (t);
3167 /* Note we want to register the assert for the
3168 operand of the NOP_EXPR after SI, not after the
3170 if (! has_single_use (t))
3172 register_new_assert_for (t, comp_code, value,
3179 /* If OP is used only once, namely in this STMT, don't
3180 bother creating an ASSERT_EXPR for it. Such an
3181 ASSERT_EXPR would do nothing but increase compile time. */
3182 if (!has_single_use (op))
3184 register_new_assert_for (op, comp_code, value, bb, NULL, si);
3190 /* Remember the last statement of the block. */
3194 /* If BB's last statement is a conditional expression
3195 involving integer operands, recurse into each of the sub-graphs
3196 rooted at BB to determine if we need to add ASSERT_EXPRs. */
3198 && TREE_CODE (last) == COND_EXPR
3199 && !fp_predicate (COND_EXPR_COND (last))
3200 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
3201 need_assert |= find_conditional_asserts (bb, last);
3203 /* Recurse into the dominator children of BB. */
3204 for (son = first_dom_son (CDI_DOMINATORS, bb);
3206 son = next_dom_son (CDI_DOMINATORS, son))
3207 need_assert |= find_assert_locations (son);
3213 /* Create an ASSERT_EXPR for NAME and insert it in the location
3214 indicated by LOC. Return true if we made any edge insertions. */
3217 process_assert_insertions_for (tree name, assert_locus_t loc)
3219 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3220 tree stmt, cond, assert_expr;
3224 cond = build2 (loc->comp_code, boolean_type_node, name, loc->val);
3225 assert_expr = build_assert_expr_for (cond, name);
3229 /* We have been asked to insert the assertion on an edge. This
3230 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3231 #if defined ENABLE_CHECKING
3232 gcc_assert (TREE_CODE (bsi_stmt (loc->si)) == COND_EXPR
3233 || TREE_CODE (bsi_stmt (loc->si)) == SWITCH_EXPR);
3236 bsi_insert_on_edge (loc->e, assert_expr);
3240 /* Otherwise, we can insert right after LOC->SI iff the
3241 statement must not be the last statement in the block. */
3242 stmt = bsi_stmt (loc->si);
3243 if (!stmt_ends_bb_p (stmt))
3245 bsi_insert_after (&loc->si, assert_expr, BSI_SAME_STMT);
3249 /* If STMT must be the last statement in BB, we can only insert new
3250 assertions on the non-abnormal edge out of BB. Note that since
3251 STMT is not control flow, there may only be one non-abnormal edge
3253 FOR_EACH_EDGE (e, ei, loc->bb->succs)
3254 if (!(e->flags & EDGE_ABNORMAL))
3256 bsi_insert_on_edge (e, assert_expr);
3264 /* Process all the insertions registered for every name N_i registered
3265 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3266 found in ASSERTS_FOR[i]. */
3269 process_assert_insertions (void)
3273 bool update_edges_p = false;
3274 int num_asserts = 0;
3276 if (dump_file && (dump_flags & TDF_DETAILS))
3277 dump_all_asserts (dump_file);
3279 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3281 assert_locus_t loc = asserts_for[i];
3286 assert_locus_t next = loc->next;
3287 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
3295 bsi_commit_edge_inserts ();
3297 if (dump_file && (dump_flags & TDF_STATS))
3298 fprintf (dump_file, "\nNumber of ASSERT_EXPR expressions inserted: %d\n\n",
3303 /* Traverse the flowgraph looking for conditional jumps to insert range
3304 expressions. These range expressions are meant to provide information
3305 to optimizations that need to reason in terms of value ranges. They
3306 will not be expanded into RTL. For instance, given:
3315 this pass will transform the code into:
3321 x = ASSERT_EXPR <x, x < y>
3326 y = ASSERT_EXPR <y, x <= y>
3330 The idea is that once copy and constant propagation have run, other
3331 optimizations will be able to determine what ranges of values can 'x'
3332 take in different paths of the code, simply by checking the reaching
3333 definition of 'x'. */
3336 insert_range_assertions (void)
3342 found_in_subgraph = sbitmap_alloc (num_ssa_names);
3343 sbitmap_zero (found_in_subgraph);
3345 blocks_visited = sbitmap_alloc (last_basic_block);
3346 sbitmap_zero (blocks_visited);
3348 need_assert_for = BITMAP_ALLOC (NULL);
3349 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
3351 calculate_dominance_info (CDI_DOMINATORS);
3353 update_ssa_p = false;
3354 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3355 if (find_assert_locations (e->dest))
3356 update_ssa_p = true;
3360 process_assert_insertions ();
3361 update_ssa (TODO_update_ssa_no_phi);
3364 if (dump_file && (dump_flags & TDF_DETAILS))
3366 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
3367 dump_function_to_file (current_function_decl, dump_file, dump_flags);
3370 sbitmap_free (found_in_subgraph);
3372 BITMAP_FREE (need_assert_for);
3376 /* Convert range assertion expressions into the implied copies and
3377 copy propagate away the copies. Doing the trivial copy propagation
3378 here avoids the need to run the full copy propagation pass after
3381 FIXME, this will eventually lead to copy propagation removing the
3382 names that had useful range information attached to them. For
3383 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
3384 then N_i will have the range [3, +INF].
3386 However, by converting the assertion into the implied copy
3387 operation N_i = N_j, we will then copy-propagate N_j into the uses
3388 of N_i and lose the range information. We may want to hold on to
3389 ASSERT_EXPRs a little while longer as the ranges could be used in
3390 things like jump threading.
3392 The problem with keeping ASSERT_EXPRs around is that passes after
3393 VRP need to handle them appropriately.
3395 Another approach would be to make the range information a first
3396 class property of the SSA_NAME so that it can be queried from
3397 any pass. This is made somewhat more complex by the need for
3398 multiple ranges to be associated with one SSA_NAME. */
3401 remove_range_assertions (void)
3404 block_stmt_iterator si;
3406 /* Note that the BSI iterator bump happens at the bottom of the
3407 loop and no bump is necessary if we're removing the statement
3408 referenced by the current BSI. */
3410 for (si = bsi_start (bb); !bsi_end_p (si);)
3412 tree stmt = bsi_stmt (si);
3415 if (TREE_CODE (stmt) == MODIFY_EXPR
3416 && TREE_CODE (TREE_OPERAND (stmt, 1)) == ASSERT_EXPR)
3418 tree rhs = TREE_OPERAND (stmt, 1), var;
3419 tree cond = fold (ASSERT_EXPR_COND (rhs));
3420 use_operand_p use_p;
3421 imm_use_iterator iter;
3423 gcc_assert (cond != boolean_false_node);
3425 /* Propagate the RHS into every use of the LHS. */
3426 var = ASSERT_EXPR_VAR (rhs);
3427 FOR_EACH_IMM_USE_STMT (use_stmt, iter, TREE_OPERAND (stmt, 0))
3428 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3430 SET_USE (use_p, var);
3431 gcc_assert (TREE_CODE (var) == SSA_NAME);
3434 /* And finally, remove the copy, it is not needed. */
3435 bsi_remove (&si, true);
3441 sbitmap_free (blocks_visited);
3445 /* Return true if STMT is interesting for VRP. */
3448 stmt_interesting_for_vrp (tree stmt)
3450 if (TREE_CODE (stmt) == PHI_NODE
3451 && is_gimple_reg (PHI_RESULT (stmt))
3452 && (INTEGRAL_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))
3453 || POINTER_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))))
3455 else if (TREE_CODE (stmt) == MODIFY_EXPR)
3457 tree lhs = TREE_OPERAND (stmt, 0);
3458 tree rhs = TREE_OPERAND (stmt, 1);
3460 /* In general, assignments with virtual operands are not useful
3461 for deriving ranges, with the obvious exception of calls to
3462 builtin functions. */
3463 if (TREE_CODE (lhs) == SSA_NAME
3464 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
3465 || POINTER_TYPE_P (TREE_TYPE (lhs)))
3466 && ((TREE_CODE (rhs) == CALL_EXPR
3467 && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
3468 && DECL_P (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
3469 && DECL_IS_BUILTIN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
3470 || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS)))
3473 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
3480 /* Initialize local data structures for VRP. */
3483 vrp_initialize (void)
3487 vr_value = XCNEWVEC (value_range_t *, num_ssa_names);
3491 block_stmt_iterator si;
3494 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3496 if (!stmt_interesting_for_vrp (phi))
3498 tree lhs = PHI_RESULT (phi);
3499 set_value_range_to_varying (get_value_range (lhs));
3500 DONT_SIMULATE_AGAIN (phi) = true;
3503 DONT_SIMULATE_AGAIN (phi) = false;
3506 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3508 tree stmt = bsi_stmt (si);
3510 if (!stmt_interesting_for_vrp (stmt))