Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / contrib / gcc / tree-vrp.c
blob4626485191ff55f24ade078253ccdc4e2f00a8be
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006, 2007 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)
10 any later version.
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. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "flags.h"
28 #include "tree.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
33 #include "timevar.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "intl.h"
37 #include "cfgloop.h"
38 #include "tree-scalar-evolution.h"
39 #include "tree-ssa-propagate.h"
40 #include "tree-chrec.h"
42 /* Set of SSA names found during the dominator traversal of a
43 sub-graph in find_assert_locations. */
44 static sbitmap found_in_subgraph;
46 /* Local functions. */
47 static int compare_values (tree val1, tree val2);
48 static int compare_values_warnv (tree val1, tree val2, bool *);
49 static tree vrp_evaluate_conditional_warnv (tree, bool, bool *);
51 /* Location information for ASSERT_EXPRs. Each instance of this
52 structure describes an ASSERT_EXPR for an SSA name. Since a single
53 SSA name may have more than one assertion associated with it, these
54 locations are kept in a linked list attached to the corresponding
55 SSA name. */
56 struct assert_locus_d
58 /* Basic block where the assertion would be inserted. */
59 basic_block bb;
61 /* Some assertions need to be inserted on an edge (e.g., assertions
62 generated by COND_EXPRs). In those cases, BB will be NULL. */
63 edge e;
65 /* Pointer to the statement that generated this assertion. */
66 block_stmt_iterator si;
68 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
69 enum tree_code comp_code;
71 /* Value being compared against. */
72 tree val;
74 /* Next node in the linked list. */
75 struct assert_locus_d *next;
78 typedef struct assert_locus_d *assert_locus_t;
80 /* If bit I is present, it means that SSA name N_i has a list of
81 assertions that should be inserted in the IL. */
82 static bitmap need_assert_for;
84 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
85 holds a list of ASSERT_LOCUS_T nodes that describe where
86 ASSERT_EXPRs for SSA name N_I should be inserted. */
87 static assert_locus_t *asserts_for;
89 /* Set of blocks visited in find_assert_locations. Used to avoid
90 visiting the same block more than once. */
91 static sbitmap blocks_visited;
93 /* Value range array. After propagation, VR_VALUE[I] holds the range
94 of values that SSA name N_I may take. */
95 static value_range_t **vr_value;
98 /* Return whether TYPE should use an overflow infinity distinct from
99 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
100 represent a signed overflow during VRP computations. An infinity
101 is distinct from a half-range, which will go from some number to
102 TYPE_{MIN,MAX}_VALUE. */
104 static inline bool
105 needs_overflow_infinity (tree type)
107 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
110 /* Return whether TYPE can support our overflow infinity
111 representation: we use the TREE_OVERFLOW flag, which only exists
112 for constants. If TYPE doesn't support this, we don't optimize
113 cases which would require signed overflow--we drop them to
114 VARYING. */
116 static inline bool
117 supports_overflow_infinity (tree type)
119 #ifdef ENABLE_CHECKING
120 gcc_assert (needs_overflow_infinity (type));
121 #endif
122 return (TYPE_MIN_VALUE (type) != NULL_TREE
123 && CONSTANT_CLASS_P (TYPE_MIN_VALUE (type))
124 && TYPE_MAX_VALUE (type) != NULL_TREE
125 && CONSTANT_CLASS_P (TYPE_MAX_VALUE (type)));
128 /* VAL is the maximum or minimum value of a type. Return a
129 corresponding overflow infinity. */
131 static inline tree
132 make_overflow_infinity (tree val)
134 #ifdef ENABLE_CHECKING
135 gcc_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
136 #endif
137 val = copy_node (val);
138 TREE_OVERFLOW (val) = 1;
139 return val;
142 /* Return a negative overflow infinity for TYPE. */
144 static inline tree
145 negative_overflow_infinity (tree type)
147 #ifdef ENABLE_CHECKING
148 gcc_assert (supports_overflow_infinity (type));
149 #endif
150 return make_overflow_infinity (TYPE_MIN_VALUE (type));
153 /* Return a positive overflow infinity for TYPE. */
155 static inline tree
156 positive_overflow_infinity (tree type)
158 #ifdef ENABLE_CHECKING
159 gcc_assert (supports_overflow_infinity (type));
160 #endif
161 return make_overflow_infinity (TYPE_MAX_VALUE (type));
164 /* Return whether VAL is a negative overflow infinity. */
166 static inline bool
167 is_negative_overflow_infinity (tree val)
169 return (needs_overflow_infinity (TREE_TYPE (val))
170 && CONSTANT_CLASS_P (val)
171 && TREE_OVERFLOW (val)
172 && operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0));
175 /* Return whether VAL is a positive overflow infinity. */
177 static inline bool
178 is_positive_overflow_infinity (tree val)
180 return (needs_overflow_infinity (TREE_TYPE (val))
181 && CONSTANT_CLASS_P (val)
182 && TREE_OVERFLOW (val)
183 && operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0));
186 /* Return whether VAL is a positive or negative overflow infinity. */
188 static inline bool
189 is_overflow_infinity (tree val)
191 return (needs_overflow_infinity (TREE_TYPE (val))
192 && CONSTANT_CLASS_P (val)
193 && TREE_OVERFLOW (val)
194 && (operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0)
195 || operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0)));
198 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
199 the same value with TREE_OVERFLOW clear. This can be used to avoid
200 confusing a regular value with an overflow value. */
202 static inline tree
203 avoid_overflow_infinity (tree val)
205 if (!is_overflow_infinity (val))
206 return val;
208 if (operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0))
209 return TYPE_MAX_VALUE (TREE_TYPE (val));
210 else
212 #ifdef ENABLE_CHECKING
213 gcc_assert (operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0));
214 #endif
215 return TYPE_MIN_VALUE (TREE_TYPE (val));
220 /* Return whether VAL is equal to the maximum value of its type. This
221 will be true for a positive overflow infinity. We can't do a
222 simple equality comparison with TYPE_MAX_VALUE because C typedefs
223 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
224 to the integer constant with the same value in the type. */
226 static inline bool
227 vrp_val_is_max (tree val)
229 tree type_max = TYPE_MAX_VALUE (TREE_TYPE (val));
231 return (val == type_max
232 || (type_max != NULL_TREE
233 && operand_equal_p (val, type_max, 0)));
236 /* Return whether VAL is equal to the minimum value of its type. This
237 will be true for a negative overflow infinity. */
239 static inline bool
240 vrp_val_is_min (tree val)
242 tree type_min = TYPE_MIN_VALUE (TREE_TYPE (val));
244 return (val == type_min
245 || (type_min != NULL_TREE
246 && operand_equal_p (val, type_min, 0)));
250 /* Return true if ARG is marked with the nonnull attribute in the
251 current function signature. */
253 static bool
254 nonnull_arg_p (tree arg)
256 tree t, attrs, fntype;
257 unsigned HOST_WIDE_INT arg_num;
259 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
261 /* The static chain decl is always non null. */
262 if (arg == cfun->static_chain_decl)
263 return true;
265 fntype = TREE_TYPE (current_function_decl);
266 attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype));
268 /* If "nonnull" wasn't specified, we know nothing about the argument. */
269 if (attrs == NULL_TREE)
270 return false;
272 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
273 if (TREE_VALUE (attrs) == NULL_TREE)
274 return true;
276 /* Get the position number for ARG in the function signature. */
277 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
279 t = TREE_CHAIN (t), arg_num++)
281 if (t == arg)
282 break;
285 gcc_assert (t == arg);
287 /* Now see if ARG_NUM is mentioned in the nonnull list. */
288 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
290 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
291 return true;
294 return false;
298 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
300 static void
301 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
302 tree max, bitmap equiv)
304 #if defined ENABLE_CHECKING
305 /* Check the validity of the range. */
306 if (t == VR_RANGE || t == VR_ANTI_RANGE)
308 int cmp;
310 gcc_assert (min && max);
312 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
313 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
315 cmp = compare_values (min, max);
316 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
318 if (needs_overflow_infinity (TREE_TYPE (min)))
319 gcc_assert (!is_overflow_infinity (min)
320 || !is_overflow_infinity (max));
323 if (t == VR_UNDEFINED || t == VR_VARYING)
324 gcc_assert (min == NULL_TREE && max == NULL_TREE);
326 if (t == VR_UNDEFINED || t == VR_VARYING)
327 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
328 #endif
330 vr->type = t;
331 vr->min = min;
332 vr->max = max;
334 /* Since updating the equivalence set involves deep copying the
335 bitmaps, only do it if absolutely necessary. */
336 if (vr->equiv == NULL)
337 vr->equiv = BITMAP_ALLOC (NULL);
339 if (equiv != vr->equiv)
341 if (equiv && !bitmap_empty_p (equiv))
342 bitmap_copy (vr->equiv, equiv);
343 else
344 bitmap_clear (vr->equiv);
349 /* Copy value range FROM into value range TO. */
351 static inline void
352 copy_value_range (value_range_t *to, value_range_t *from)
354 set_value_range (to, from->type, from->min, from->max, from->equiv);
358 /* Set value range VR to VR_VARYING. */
360 static inline void
361 set_value_range_to_varying (value_range_t *vr)
363 vr->type = VR_VARYING;
364 vr->min = vr->max = NULL_TREE;
365 if (vr->equiv)
366 bitmap_clear (vr->equiv);
369 /* Set value range VR to a single value. This function is only called
370 with values we get from statements, and exists to clear the
371 TREE_OVERFLOW flag so that we don't think we have an overflow
372 infinity when we shouldn't. */
374 static inline void
375 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
377 gcc_assert (is_gimple_min_invariant (val));
378 val = avoid_overflow_infinity (val);
379 set_value_range (vr, VR_RANGE, val, val, equiv);
382 /* Set value range VR to a non-negative range of type TYPE.
383 OVERFLOW_INFINITY indicates whether to use a overflow infinity
384 rather than TYPE_MAX_VALUE; this should be true if we determine
385 that the range is nonnegative based on the assumption that signed
386 overflow does not occur. */
388 static inline void
389 set_value_range_to_nonnegative (value_range_t *vr, tree type,
390 bool overflow_infinity)
392 tree zero;
394 if (overflow_infinity && !supports_overflow_infinity (type))
396 set_value_range_to_varying (vr);
397 return;
400 zero = build_int_cst (type, 0);
401 set_value_range (vr, VR_RANGE, zero,
402 (overflow_infinity
403 ? positive_overflow_infinity (type)
404 : TYPE_MAX_VALUE (type)),
405 vr->equiv);
408 /* Set value range VR to a non-NULL range of type TYPE. */
410 static inline void
411 set_value_range_to_nonnull (value_range_t *vr, tree type)
413 tree zero = build_int_cst (type, 0);
414 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
418 /* Set value range VR to a NULL range of type TYPE. */
420 static inline void
421 set_value_range_to_null (value_range_t *vr, tree type)
423 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
427 /* Set value range VR to VR_UNDEFINED. */
429 static inline void
430 set_value_range_to_undefined (value_range_t *vr)
432 vr->type = VR_UNDEFINED;
433 vr->min = vr->max = NULL_TREE;
434 if (vr->equiv)
435 bitmap_clear (vr->equiv);
439 /* Return value range information for VAR.
441 If we have no values ranges recorded (ie, VRP is not running), then
442 return NULL. Otherwise create an empty range if none existed for VAR. */
444 static value_range_t *
445 get_value_range (tree var)
447 value_range_t *vr;
448 tree sym;
449 unsigned ver = SSA_NAME_VERSION (var);
451 /* If we have no recorded ranges, then return NULL. */
452 if (! vr_value)
453 return NULL;
455 vr = vr_value[ver];
456 if (vr)
457 return vr;
459 /* Create a default value range. */
460 vr_value[ver] = vr = XNEW (value_range_t);
461 memset (vr, 0, sizeof (*vr));
463 /* Allocate an equivalence set. */
464 vr->equiv = BITMAP_ALLOC (NULL);
466 /* If VAR is a default definition, the variable can take any value
467 in VAR's type. */
468 sym = SSA_NAME_VAR (var);
469 if (var == default_def (sym))
471 /* Try to use the "nonnull" attribute to create ~[0, 0]
472 anti-ranges for pointers. Note that this is only valid with
473 default definitions of PARM_DECLs. */
474 if (TREE_CODE (sym) == PARM_DECL
475 && POINTER_TYPE_P (TREE_TYPE (sym))
476 && nonnull_arg_p (sym))
477 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
478 else
479 set_value_range_to_varying (vr);
482 return vr;
485 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
487 static inline bool
488 vrp_operand_equal_p (tree val1, tree val2)
490 if (val1 == val2)
491 return true;
492 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
493 return false;
494 if (is_overflow_infinity (val1))
495 return is_overflow_infinity (val2);
496 return true;
499 /* Return true, if the bitmaps B1 and B2 are equal. */
501 static inline bool
502 vrp_bitmap_equal_p (bitmap b1, bitmap b2)
504 return (b1 == b2
505 || (b1 && b2
506 && bitmap_equal_p (b1, b2)));
509 /* Update the value range and equivalence set for variable VAR to
510 NEW_VR. Return true if NEW_VR is different from VAR's previous
511 value.
513 NOTE: This function assumes that NEW_VR is a temporary value range
514 object created for the sole purpose of updating VAR's range. The
515 storage used by the equivalence set from NEW_VR will be freed by
516 this function. Do not call update_value_range when NEW_VR
517 is the range object associated with another SSA name. */
519 static inline bool
520 update_value_range (tree var, value_range_t *new_vr)
522 value_range_t *old_vr;
523 bool is_new;
525 /* Update the value range, if necessary. */
526 old_vr = get_value_range (var);
527 is_new = old_vr->type != new_vr->type
528 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
529 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
530 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
532 if (is_new)
533 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
534 new_vr->equiv);
536 BITMAP_FREE (new_vr->equiv);
537 new_vr->equiv = NULL;
539 return is_new;
543 /* Add VAR and VAR's equivalence set to EQUIV. */
545 static void
546 add_equivalence (bitmap equiv, tree var)
548 unsigned ver = SSA_NAME_VERSION (var);
549 value_range_t *vr = vr_value[ver];
551 bitmap_set_bit (equiv, ver);
552 if (vr && vr->equiv)
553 bitmap_ior_into (equiv, vr->equiv);
557 /* Return true if VR is ~[0, 0]. */
559 static inline bool
560 range_is_nonnull (value_range_t *vr)
562 return vr->type == VR_ANTI_RANGE
563 && integer_zerop (vr->min)
564 && integer_zerop (vr->max);
568 /* Return true if VR is [0, 0]. */
570 static inline bool
571 range_is_null (value_range_t *vr)
573 return vr->type == VR_RANGE
574 && integer_zerop (vr->min)
575 && integer_zerop (vr->max);
579 /* Return true if value range VR involves at least one symbol. */
581 static inline bool
582 symbolic_range_p (value_range_t *vr)
584 return (!is_gimple_min_invariant (vr->min)
585 || !is_gimple_min_invariant (vr->max));
588 /* Return true if value range VR uses a overflow infinity. */
590 static inline bool
591 overflow_infinity_range_p (value_range_t *vr)
593 return (vr->type == VR_RANGE
594 && (is_overflow_infinity (vr->min)
595 || is_overflow_infinity (vr->max)));
598 /* Return false if we can not make a valid comparison based on VR;
599 this will be the case if it uses an overflow infinity and overflow
600 is not undefined (i.e., -fno-strict-overflow is in effect).
601 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
602 uses an overflow infinity. */
604 static bool
605 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
607 gcc_assert (vr->type == VR_RANGE);
608 if (is_overflow_infinity (vr->min))
610 *strict_overflow_p = true;
611 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
612 return false;
614 if (is_overflow_infinity (vr->max))
616 *strict_overflow_p = true;
617 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
618 return false;
620 return true;
624 /* Like tree_expr_nonnegative_warnv_p, but this function uses value
625 ranges obtained so far. */
627 static bool
628 vrp_expr_computes_nonnegative (tree expr, bool *strict_overflow_p)
630 return tree_expr_nonnegative_warnv_p (expr, strict_overflow_p);
633 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
634 obtained so far. */
636 static bool
637 vrp_expr_computes_nonzero (tree expr, bool *strict_overflow_p)
639 if (tree_expr_nonzero_warnv_p (expr, strict_overflow_p))
640 return true;
642 /* If we have an expression of the form &X->a, then the expression
643 is nonnull if X is nonnull. */
644 if (TREE_CODE (expr) == ADDR_EXPR)
646 tree base = get_base_address (TREE_OPERAND (expr, 0));
648 if (base != NULL_TREE
649 && TREE_CODE (base) == INDIRECT_REF
650 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
652 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
653 if (range_is_nonnull (vr))
654 return true;
658 return false;
661 /* Returns true if EXPR is a valid value (as expected by compare_values) --
662 a gimple invariant, or SSA_NAME +- CST. */
664 static bool
665 valid_value_p (tree expr)
667 if (TREE_CODE (expr) == SSA_NAME)
668 return true;
670 if (TREE_CODE (expr) == PLUS_EXPR
671 || TREE_CODE (expr) == MINUS_EXPR)
672 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
673 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
675 return is_gimple_min_invariant (expr);
678 /* Compare two values VAL1 and VAL2. Return
680 -2 if VAL1 and VAL2 cannot be compared at compile-time,
681 -1 if VAL1 < VAL2,
682 0 if VAL1 == VAL2,
683 +1 if VAL1 > VAL2, and
684 +2 if VAL1 != VAL2
686 This is similar to tree_int_cst_compare but supports pointer values
687 and values that cannot be compared at compile time.
689 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
690 true if the return value is only valid if we assume that signed
691 overflow is undefined. */
693 static int
694 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
696 if (val1 == val2)
697 return 0;
699 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
700 both integers. */
701 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
702 == POINTER_TYPE_P (TREE_TYPE (val2)));
704 if ((TREE_CODE (val1) == SSA_NAME
705 || TREE_CODE (val1) == PLUS_EXPR
706 || TREE_CODE (val1) == MINUS_EXPR)
707 && (TREE_CODE (val2) == SSA_NAME
708 || TREE_CODE (val2) == PLUS_EXPR
709 || TREE_CODE (val2) == MINUS_EXPR))
711 tree n1, c1, n2, c2;
712 enum tree_code code1, code2;
714 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
715 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
716 same name, return -2. */
717 if (TREE_CODE (val1) == SSA_NAME)
719 code1 = SSA_NAME;
720 n1 = val1;
721 c1 = NULL_TREE;
723 else
725 code1 = TREE_CODE (val1);
726 n1 = TREE_OPERAND (val1, 0);
727 c1 = TREE_OPERAND (val1, 1);
728 if (tree_int_cst_sgn (c1) == -1)
730 if (is_negative_overflow_infinity (c1))
731 return -2;
732 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
733 if (!c1)
734 return -2;
735 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
739 if (TREE_CODE (val2) == SSA_NAME)
741 code2 = SSA_NAME;
742 n2 = val2;
743 c2 = NULL_TREE;
745 else
747 code2 = TREE_CODE (val2);
748 n2 = TREE_OPERAND (val2, 0);
749 c2 = TREE_OPERAND (val2, 1);
750 if (tree_int_cst_sgn (c2) == -1)
752 if (is_negative_overflow_infinity (c2))
753 return -2;
754 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
755 if (!c2)
756 return -2;
757 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
761 /* Both values must use the same name. */
762 if (n1 != n2)
763 return -2;
765 if (code1 == SSA_NAME
766 && code2 == SSA_NAME)
767 /* NAME == NAME */
768 return 0;
770 /* If overflow is defined we cannot simplify more. */
771 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
772 return -2;
774 if (strict_overflow_p != NULL
775 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
776 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
777 *strict_overflow_p = true;
779 if (code1 == SSA_NAME)
781 if (code2 == PLUS_EXPR)
782 /* NAME < NAME + CST */
783 return -1;
784 else if (code2 == MINUS_EXPR)
785 /* NAME > NAME - CST */
786 return 1;
788 else if (code1 == PLUS_EXPR)
790 if (code2 == SSA_NAME)
791 /* NAME + CST > NAME */
792 return 1;
793 else if (code2 == PLUS_EXPR)
794 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
795 return compare_values_warnv (c1, c2, strict_overflow_p);
796 else if (code2 == MINUS_EXPR)
797 /* NAME + CST1 > NAME - CST2 */
798 return 1;
800 else if (code1 == MINUS_EXPR)
802 if (code2 == SSA_NAME)
803 /* NAME - CST < NAME */
804 return -1;
805 else if (code2 == PLUS_EXPR)
806 /* NAME - CST1 < NAME + CST2 */
807 return -1;
808 else if (code2 == MINUS_EXPR)
809 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
810 C1 and C2 are swapped in the call to compare_values. */
811 return compare_values_warnv (c2, c1, strict_overflow_p);
814 gcc_unreachable ();
817 /* We cannot compare non-constants. */
818 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
819 return -2;
821 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
823 /* We cannot compare overflowed values, except for overflow
824 infinities. */
825 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
827 if (strict_overflow_p != NULL)
828 *strict_overflow_p = true;
829 if (is_negative_overflow_infinity (val1))
830 return is_negative_overflow_infinity (val2) ? 0 : -1;
831 else if (is_negative_overflow_infinity (val2))
832 return 1;
833 else if (is_positive_overflow_infinity (val1))
834 return is_positive_overflow_infinity (val2) ? 0 : 1;
835 else if (is_positive_overflow_infinity (val2))
836 return -1;
837 return -2;
840 return tree_int_cst_compare (val1, val2);
842 else
844 tree t;
846 /* First see if VAL1 and VAL2 are not the same. */
847 if (val1 == val2 || operand_equal_p (val1, val2, 0))
848 return 0;
850 /* If VAL1 is a lower address than VAL2, return -1. */
851 t = fold_binary (LT_EXPR, boolean_type_node, val1, val2);
852 if (t == boolean_true_node)
853 return -1;
855 /* If VAL1 is a higher address than VAL2, return +1. */
856 t = fold_binary (GT_EXPR, boolean_type_node, val1, val2);
857 if (t == boolean_true_node)
858 return 1;
860 /* If VAL1 is different than VAL2, return +2. */
861 t = fold_binary (NE_EXPR, boolean_type_node, val1, val2);
862 if (t == boolean_true_node)
863 return 2;
865 return -2;
869 /* Compare values like compare_values_warnv, but treat comparisons of
870 nonconstants which rely on undefined overflow as incomparable. */
872 static int
873 compare_values (tree val1, tree val2)
875 bool sop;
876 int ret;
878 sop = false;
879 ret = compare_values_warnv (val1, val2, &sop);
880 if (sop
881 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
882 ret = -2;
883 return ret;
887 /* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
888 0 if VAL is not inside VR,
889 -2 if we cannot tell either way.
891 FIXME, the current semantics of this functions are a bit quirky
892 when taken in the context of VRP. In here we do not care
893 about VR's type. If VR is the anti-range ~[3, 5] the call
894 value_inside_range (4, VR) will return 1.
896 This is counter-intuitive in a strict sense, but the callers
897 currently expect this. They are calling the function
898 merely to determine whether VR->MIN <= VAL <= VR->MAX. The
899 callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
900 themselves.
902 This also applies to value_ranges_intersect_p and
903 range_includes_zero_p. The semantics of VR_RANGE and
904 VR_ANTI_RANGE should be encoded here, but that also means
905 adapting the users of these functions to the new semantics. */
907 static inline int
908 value_inside_range (tree val, value_range_t *vr)
910 tree cmp1, cmp2;
912 fold_defer_overflow_warnings ();
914 cmp1 = fold_binary_to_constant (GE_EXPR, boolean_type_node, val, vr->min);
915 if (!cmp1)
917 fold_undefer_and_ignore_overflow_warnings ();
918 return -2;
921 cmp2 = fold_binary_to_constant (LE_EXPR, boolean_type_node, val, vr->max);
923 fold_undefer_and_ignore_overflow_warnings ();
925 if (!cmp2)
926 return -2;
928 return cmp1 == boolean_true_node && cmp2 == boolean_true_node;
932 /* Return true if value ranges VR0 and VR1 have a non-empty
933 intersection. */
935 static inline bool
936 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
938 return (value_inside_range (vr1->min, vr0) == 1
939 || value_inside_range (vr1->max, vr0) == 1
940 || value_inside_range (vr0->min, vr1) == 1
941 || value_inside_range (vr0->max, vr1) == 1);
945 /* Return true if VR includes the value zero, false otherwise. FIXME,
946 currently this will return false for an anti-range like ~[-4, 3].
947 This will be wrong when the semantics of value_inside_range are
948 modified (currently the users of this function expect these
949 semantics). */
951 static inline bool
952 range_includes_zero_p (value_range_t *vr)
954 tree zero;
956 gcc_assert (vr->type != VR_UNDEFINED
957 && vr->type != VR_VARYING
958 && !symbolic_range_p (vr));
960 zero = build_int_cst (TREE_TYPE (vr->min), 0);
961 return (value_inside_range (zero, vr) == 1);
964 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
965 false otherwise or if no value range information is available. */
967 bool
968 ssa_name_nonnegative_p (tree t)
970 value_range_t *vr = get_value_range (t);
972 if (!vr)
973 return false;
975 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
976 which would return a useful value should be encoded as a VR_RANGE. */
977 if (vr->type == VR_RANGE)
979 int result = compare_values (vr->min, integer_zero_node);
981 return (result == 0 || result == 1);
983 return false;
986 /* Return true if T, an SSA_NAME, is known to be nonzero. Return
987 false otherwise or if no value range information is available. */
989 bool
990 ssa_name_nonzero_p (tree t)
992 value_range_t *vr = get_value_range (t);
994 if (!vr)
995 return false;
997 /* A VR_RANGE which does not include zero is a nonzero value. */
998 if (vr->type == VR_RANGE && !symbolic_range_p (vr))
999 return ! range_includes_zero_p (vr);
1001 /* A VR_ANTI_RANGE which does include zero is a nonzero value. */
1002 if (vr->type == VR_ANTI_RANGE && !symbolic_range_p (vr))
1003 return range_includes_zero_p (vr);
1005 return false;
1009 /* Extract value range information from an ASSERT_EXPR EXPR and store
1010 it in *VR_P. */
1012 static void
1013 extract_range_from_assert (value_range_t *vr_p, tree expr)
1015 tree var, cond, limit, min, max, type;
1016 value_range_t *var_vr, *limit_vr;
1017 enum tree_code cond_code;
1019 var = ASSERT_EXPR_VAR (expr);
1020 cond = ASSERT_EXPR_COND (expr);
1022 gcc_assert (COMPARISON_CLASS_P (cond));
1024 /* Find VAR in the ASSERT_EXPR conditional. */
1025 if (var == TREE_OPERAND (cond, 0))
1027 /* If the predicate is of the form VAR COMP LIMIT, then we just
1028 take LIMIT from the RHS and use the same comparison code. */
1029 limit = TREE_OPERAND (cond, 1);
1030 cond_code = TREE_CODE (cond);
1032 else
1034 /* If the predicate is of the form LIMIT COMP VAR, then we need
1035 to flip around the comparison code to create the proper range
1036 for VAR. */
1037 limit = TREE_OPERAND (cond, 0);
1038 cond_code = swap_tree_comparison (TREE_CODE (cond));
1041 limit = avoid_overflow_infinity (limit);
1043 type = TREE_TYPE (limit);
1044 gcc_assert (limit != var);
1046 /* For pointer arithmetic, we only keep track of pointer equality
1047 and inequality. */
1048 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1050 set_value_range_to_varying (vr_p);
1051 return;
1054 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1055 try to use LIMIT's range to avoid creating symbolic ranges
1056 unnecessarily. */
1057 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1059 /* LIMIT's range is only interesting if it has any useful information. */
1060 if (limit_vr
1061 && (limit_vr->type == VR_UNDEFINED
1062 || limit_vr->type == VR_VARYING
1063 || symbolic_range_p (limit_vr)))
1064 limit_vr = NULL;
1066 /* Initially, the new range has the same set of equivalences of
1067 VAR's range. This will be revised before returning the final
1068 value. Since assertions may be chained via mutually exclusive
1069 predicates, we will need to trim the set of equivalences before
1070 we are done. */
1071 gcc_assert (vr_p->equiv == NULL);
1072 vr_p->equiv = BITMAP_ALLOC (NULL);
1073 add_equivalence (vr_p->equiv, var);
1075 /* Extract a new range based on the asserted comparison for VAR and
1076 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1077 will only use it for equality comparisons (EQ_EXPR). For any
1078 other kind of assertion, we cannot derive a range from LIMIT's
1079 anti-range that can be used to describe the new range. For
1080 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1081 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1082 no single range for x_2 that could describe LE_EXPR, so we might
1083 as well build the range [b_4, +INF] for it. */
1084 if (cond_code == EQ_EXPR)
1086 enum value_range_type range_type;
1088 if (limit_vr)
1090 range_type = limit_vr->type;
1091 min = limit_vr->min;
1092 max = limit_vr->max;
1094 else
1096 range_type = VR_RANGE;
1097 min = limit;
1098 max = limit;
1101 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1103 /* When asserting the equality VAR == LIMIT and LIMIT is another
1104 SSA name, the new range will also inherit the equivalence set
1105 from LIMIT. */
1106 if (TREE_CODE (limit) == SSA_NAME)
1107 add_equivalence (vr_p->equiv, limit);
1109 else if (cond_code == NE_EXPR)
1111 /* As described above, when LIMIT's range is an anti-range and
1112 this assertion is an inequality (NE_EXPR), then we cannot
1113 derive anything from the anti-range. For instance, if
1114 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1115 not imply that VAR's range is [0, 0]. So, in the case of
1116 anti-ranges, we just assert the inequality using LIMIT and
1117 not its anti-range.
1119 If LIMIT_VR is a range, we can only use it to build a new
1120 anti-range if LIMIT_VR is a single-valued range. For
1121 instance, if LIMIT_VR is [0, 1], the predicate
1122 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1123 Rather, it means that for value 0 VAR should be ~[0, 0]
1124 and for value 1, VAR should be ~[1, 1]. We cannot
1125 represent these ranges.
1127 The only situation in which we can build a valid
1128 anti-range is when LIMIT_VR is a single-valued range
1129 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1130 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1131 if (limit_vr
1132 && limit_vr->type == VR_RANGE
1133 && compare_values (limit_vr->min, limit_vr->max) == 0)
1135 min = limit_vr->min;
1136 max = limit_vr->max;
1138 else
1140 /* In any other case, we cannot use LIMIT's range to build a
1141 valid anti-range. */
1142 min = max = limit;
1145 /* If MIN and MAX cover the whole range for their type, then
1146 just use the original LIMIT. */
1147 if (INTEGRAL_TYPE_P (type)
1148 && vrp_val_is_min (min)
1149 && vrp_val_is_max (max))
1150 min = max = limit;
1152 set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
1154 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1156 min = TYPE_MIN_VALUE (type);
1158 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1159 max = limit;
1160 else
1162 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1163 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1164 LT_EXPR. */
1165 max = limit_vr->max;
1168 /* If the maximum value forces us to be out of bounds, simply punt.
1169 It would be pointless to try and do anything more since this
1170 all should be optimized away above us. */
1171 if ((cond_code == LT_EXPR
1172 && compare_values (max, min) == 0)
1173 || is_overflow_infinity (max))
1174 set_value_range_to_varying (vr_p);
1175 else
1177 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1178 if (cond_code == LT_EXPR)
1180 tree one = build_int_cst (type, 1);
1181 max = fold_build2 (MINUS_EXPR, type, max, one);
1182 if (EXPR_P (max))
1183 TREE_NO_WARNING (max) = 1;
1186 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1189 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1191 max = TYPE_MAX_VALUE (type);
1193 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1194 min = limit;
1195 else
1197 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1198 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1199 GT_EXPR. */
1200 min = limit_vr->min;
1203 /* If the minimum value forces us to be out of bounds, simply punt.
1204 It would be pointless to try and do anything more since this
1205 all should be optimized away above us. */
1206 if ((cond_code == GT_EXPR
1207 && compare_values (min, max) == 0)
1208 || is_overflow_infinity (min))
1209 set_value_range_to_varying (vr_p);
1210 else
1212 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1213 if (cond_code == GT_EXPR)
1215 tree one = build_int_cst (type, 1);
1216 min = fold_build2 (PLUS_EXPR, type, min, one);
1217 if (EXPR_P (min))
1218 TREE_NO_WARNING (min) = 1;
1221 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1224 else
1225 gcc_unreachable ();
1227 /* If VAR already had a known range, it may happen that the new
1228 range we have computed and VAR's range are not compatible. For
1229 instance,
1231 if (p_5 == NULL)
1232 p_6 = ASSERT_EXPR <p_5, p_5 == NULL>;
1233 x_7 = p_6->fld;
1234 p_8 = ASSERT_EXPR <p_6, p_6 != NULL>;
1236 While the above comes from a faulty program, it will cause an ICE
1237 later because p_8 and p_6 will have incompatible ranges and at
1238 the same time will be considered equivalent. A similar situation
1239 would arise from
1241 if (i_5 > 10)
1242 i_6 = ASSERT_EXPR <i_5, i_5 > 10>;
1243 if (i_5 < 5)
1244 i_7 = ASSERT_EXPR <i_6, i_6 < 5>;
1246 Again i_6 and i_7 will have incompatible ranges. It would be
1247 pointless to try and do anything with i_7's range because
1248 anything dominated by 'if (i_5 < 5)' will be optimized away.
1249 Note, due to the wa in which simulation proceeds, the statement
1250 i_7 = ASSERT_EXPR <...> we would never be visited because the
1251 conditional 'if (i_5 < 5)' always evaluates to false. However,
1252 this extra check does not hurt and may protect against future
1253 changes to VRP that may get into a situation similar to the
1254 NULL pointer dereference example.
1256 Note that these compatibility tests are only needed when dealing
1257 with ranges or a mix of range and anti-range. If VAR_VR and VR_P
1258 are both anti-ranges, they will always be compatible, because two
1259 anti-ranges will always have a non-empty intersection. */
1261 var_vr = get_value_range (var);
1263 /* We may need to make adjustments when VR_P and VAR_VR are numeric
1264 ranges or anti-ranges. */
1265 if (vr_p->type == VR_VARYING
1266 || vr_p->type == VR_UNDEFINED
1267 || var_vr->type == VR_VARYING
1268 || var_vr->type == VR_UNDEFINED
1269 || symbolic_range_p (vr_p)
1270 || symbolic_range_p (var_vr))
1271 return;
1273 if (var_vr->type == VR_RANGE && vr_p->type == VR_RANGE)
1275 /* If the two ranges have a non-empty intersection, we can
1276 refine the resulting range. Since the assert expression
1277 creates an equivalency and at the same time it asserts a
1278 predicate, we can take the intersection of the two ranges to
1279 get better precision. */
1280 if (value_ranges_intersect_p (var_vr, vr_p))
1282 /* Use the larger of the two minimums. */
1283 if (compare_values (vr_p->min, var_vr->min) == -1)
1284 min = var_vr->min;
1285 else
1286 min = vr_p->min;
1288 /* Use the smaller of the two maximums. */
1289 if (compare_values (vr_p->max, var_vr->max) == 1)
1290 max = var_vr->max;
1291 else
1292 max = vr_p->max;
1294 set_value_range (vr_p, vr_p->type, min, max, vr_p->equiv);
1296 else
1298 /* The two ranges do not intersect, set the new range to
1299 VARYING, because we will not be able to do anything
1300 meaningful with it. */
1301 set_value_range_to_varying (vr_p);
1304 else if ((var_vr->type == VR_RANGE && vr_p->type == VR_ANTI_RANGE)
1305 || (var_vr->type == VR_ANTI_RANGE && vr_p->type == VR_RANGE))
1307 /* A range and an anti-range will cancel each other only if
1308 their ends are the same. For instance, in the example above,
1309 p_8's range ~[0, 0] and p_6's range [0, 0] are incompatible,
1310 so VR_P should be set to VR_VARYING. */
1311 if (compare_values (var_vr->min, vr_p->min) == 0
1312 && compare_values (var_vr->max, vr_p->max) == 0)
1313 set_value_range_to_varying (vr_p);
1314 else
1316 tree min, max, anti_min, anti_max, real_min, real_max;
1318 /* We want to compute the logical AND of the two ranges;
1319 there are three cases to consider.
1322 1. The VR_ANTI_RANGE range is completely within the
1323 VR_RANGE and the endpoints of the ranges are
1324 different. In that case the resulting range
1325 should be whichever range is more precise.
1326 Typically that will be the VR_RANGE.
1328 2. The VR_ANTI_RANGE is completely disjoint from
1329 the VR_RANGE. In this case the resulting range
1330 should be the VR_RANGE.
1332 3. There is some overlap between the VR_ANTI_RANGE
1333 and the VR_RANGE.
1335 3a. If the high limit of the VR_ANTI_RANGE resides
1336 within the VR_RANGE, then the result is a new
1337 VR_RANGE starting at the high limit of the
1338 the VR_ANTI_RANGE + 1 and extending to the
1339 high limit of the original VR_RANGE.
1341 3b. If the low limit of the VR_ANTI_RANGE resides
1342 within the VR_RANGE, then the result is a new
1343 VR_RANGE starting at the low limit of the original
1344 VR_RANGE and extending to the low limit of the
1345 VR_ANTI_RANGE - 1. */
1346 if (vr_p->type == VR_ANTI_RANGE)
1348 anti_min = vr_p->min;
1349 anti_max = vr_p->max;
1350 real_min = var_vr->min;
1351 real_max = var_vr->max;
1353 else
1355 anti_min = var_vr->min;
1356 anti_max = var_vr->max;
1357 real_min = vr_p->min;
1358 real_max = vr_p->max;
1362 /* Case 1, VR_ANTI_RANGE completely within VR_RANGE,
1363 not including any endpoints. */
1364 if (compare_values (anti_max, real_max) == -1
1365 && compare_values (anti_min, real_min) == 1)
1367 set_value_range (vr_p, VR_RANGE, real_min,
1368 real_max, vr_p->equiv);
1370 /* Case 2, VR_ANTI_RANGE completely disjoint from
1371 VR_RANGE. */
1372 else if (compare_values (anti_min, real_max) == 1
1373 || compare_values (anti_max, real_min) == -1)
1375 set_value_range (vr_p, VR_RANGE, real_min,
1376 real_max, vr_p->equiv);
1378 /* Case 3a, the anti-range extends into the low
1379 part of the real range. Thus creating a new
1380 low for the real range. */
1381 else if ((compare_values (anti_max, real_min) == 1
1382 || compare_values (anti_max, real_min) == 0)
1383 && compare_values (anti_max, real_max) == -1)
1385 gcc_assert (!is_positive_overflow_infinity (anti_max));
1386 if (needs_overflow_infinity (TREE_TYPE (anti_max))
1387 && vrp_val_is_max (anti_max))
1389 if (!supports_overflow_infinity (TREE_TYPE (var_vr->min)))
1391 set_value_range_to_varying (vr_p);
1392 return;
1394 min = positive_overflow_infinity (TREE_TYPE (var_vr->min));
1396 else
1397 min = fold_build2 (PLUS_EXPR, TREE_TYPE (var_vr->min),
1398 anti_max,
1399 build_int_cst (TREE_TYPE (var_vr->min), 1));
1400 max = real_max;
1401 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1403 /* Case 3b, the anti-range extends into the high
1404 part of the real range. Thus creating a new
1405 higher for the real range. */
1406 else if (compare_values (anti_min, real_min) == 1
1407 && (compare_values (anti_min, real_max) == -1
1408 || compare_values (anti_min, real_max) == 0))
1410 gcc_assert (!is_negative_overflow_infinity (anti_min));
1411 if (needs_overflow_infinity (TREE_TYPE (anti_min))
1412 && vrp_val_is_min (anti_min))
1414 if (!supports_overflow_infinity (TREE_TYPE (var_vr->min)))
1416 set_value_range_to_varying (vr_p);
1417 return;
1419 max = negative_overflow_infinity (TREE_TYPE (var_vr->min));
1421 else
1422 max = fold_build2 (MINUS_EXPR, TREE_TYPE (var_vr->min),
1423 anti_min,
1424 build_int_cst (TREE_TYPE (var_vr->min), 1));
1425 min = real_min;
1426 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1433 /* Extract range information from SSA name VAR and store it in VR. If
1434 VAR has an interesting range, use it. Otherwise, create the
1435 range [VAR, VAR] and return it. This is useful in situations where
1436 we may have conditionals testing values of VARYING names. For
1437 instance,
1439 x_3 = y_5;
1440 if (x_3 > y_5)
1443 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1444 always false. */
1446 static void
1447 extract_range_from_ssa_name (value_range_t *vr, tree var)
1449 value_range_t *var_vr = get_value_range (var);
1451 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1452 copy_value_range (vr, var_vr);
1453 else
1454 set_value_range (vr, VR_RANGE, var, var, NULL);
1456 add_equivalence (vr->equiv, var);
1460 /* Wrapper around int_const_binop. If the operation overflows and we
1461 are not using wrapping arithmetic, then adjust the result to be
1462 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1463 NULL_TREE if we need to use an overflow infinity representation but
1464 the type does not support it. */
1466 static tree
1467 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1469 tree res;
1471 res = int_const_binop (code, val1, val2, 0);
1473 /* If we are not using wrapping arithmetic, operate symbolically
1474 on -INF and +INF. */
1475 if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1477 int checkz = compare_values (res, val1);
1478 bool overflow = false;
1480 /* Ensure that res = val1 [+*] val2 >= val1
1481 or that res = val1 - val2 <= val1. */
1482 if ((code == PLUS_EXPR
1483 && !(checkz == 1 || checkz == 0))
1484 || (code == MINUS_EXPR
1485 && !(checkz == 0 || checkz == -1)))
1487 overflow = true;
1489 /* Checking for multiplication overflow is done by dividing the
1490 output of the multiplication by the first input of the
1491 multiplication. If the result of that division operation is
1492 not equal to the second input of the multiplication, then the
1493 multiplication overflowed. */
1494 else if (code == MULT_EXPR && !integer_zerop (val1))
1496 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1497 res,
1498 val1, 0);
1499 int check = compare_values (tmp, val2);
1501 if (check != 0)
1502 overflow = true;
1505 if (overflow)
1507 res = copy_node (res);
1508 TREE_OVERFLOW (res) = 1;
1512 else if ((TREE_OVERFLOW (res)
1513 && !TREE_OVERFLOW (val1)
1514 && !TREE_OVERFLOW (val2))
1515 || is_overflow_infinity (val1)
1516 || is_overflow_infinity (val2))
1518 /* If the operation overflowed but neither VAL1 nor VAL2 are
1519 overflown, return -INF or +INF depending on the operation
1520 and the combination of signs of the operands. */
1521 int sgn1 = tree_int_cst_sgn (val1);
1522 int sgn2 = tree_int_cst_sgn (val2);
1524 if (needs_overflow_infinity (TREE_TYPE (res))
1525 && !supports_overflow_infinity (TREE_TYPE (res)))
1526 return NULL_TREE;
1528 /* We have to punt on adding infinities of different signs,
1529 since we can't tell what the sign of the result should be.
1530 Likewise for subtracting infinities of the same sign. */
1531 if (((code == PLUS_EXPR && sgn1 != sgn2)
1532 || (code == MINUS_EXPR && sgn1 == sgn2))
1533 && is_overflow_infinity (val1)
1534 && is_overflow_infinity (val2))
1535 return NULL_TREE;
1537 /* Don't try to handle division or shifting of infinities. */
1538 if ((code == TRUNC_DIV_EXPR
1539 || code == FLOOR_DIV_EXPR
1540 || code == CEIL_DIV_EXPR
1541 || code == EXACT_DIV_EXPR
1542 || code == ROUND_DIV_EXPR
1543 || code == RSHIFT_EXPR)
1544 && (is_overflow_infinity (val1)
1545 || is_overflow_infinity (val2)))
1546 return NULL_TREE;
1548 /* Notice that we only need to handle the restricted set of
1549 operations handled by extract_range_from_binary_expr.
1550 Among them, only multiplication, addition and subtraction
1551 can yield overflow without overflown operands because we
1552 are working with integral types only... except in the
1553 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1554 for division too. */
1556 /* For multiplication, the sign of the overflow is given
1557 by the comparison of the signs of the operands. */
1558 if ((code == MULT_EXPR && sgn1 == sgn2)
1559 /* For addition, the operands must be of the same sign
1560 to yield an overflow. Its sign is therefore that
1561 of one of the operands, for example the first. For
1562 infinite operands X + -INF is negative, not positive. */
1563 || (code == PLUS_EXPR
1564 && (sgn1 >= 0
1565 ? !is_negative_overflow_infinity (val2)
1566 : is_positive_overflow_infinity (val2)))
1567 /* For subtraction, non-infinite operands must be of
1568 different signs to yield an overflow. Its sign is
1569 therefore that of the first operand or the opposite of
1570 that of the second operand. A first operand of 0 counts
1571 as positive here, for the corner case 0 - (-INF), which
1572 overflows, but must yield +INF. For infinite operands 0
1573 - INF is negative, not positive. */
1574 || (code == MINUS_EXPR
1575 && (sgn1 >= 0
1576 ? !is_positive_overflow_infinity (val2)
1577 : is_negative_overflow_infinity (val2)))
1578 /* For division, the only case is -INF / -1 = +INF. */
1579 || code == TRUNC_DIV_EXPR
1580 || code == FLOOR_DIV_EXPR
1581 || code == CEIL_DIV_EXPR
1582 || code == EXACT_DIV_EXPR
1583 || code == ROUND_DIV_EXPR)
1584 return (needs_overflow_infinity (TREE_TYPE (res))
1585 ? positive_overflow_infinity (TREE_TYPE (res))
1586 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1587 else
1588 return (needs_overflow_infinity (TREE_TYPE (res))
1589 ? negative_overflow_infinity (TREE_TYPE (res))
1590 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1593 return res;
1597 /* Extract range information from a binary expression EXPR based on
1598 the ranges of each of its operands and the expression code. */
1600 static void
1601 extract_range_from_binary_expr (value_range_t *vr, tree expr)
1603 enum tree_code code = TREE_CODE (expr);
1604 enum value_range_type type;
1605 tree op0, op1, min, max;
1606 int cmp;
1607 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1608 value_range_t vr1 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1610 /* Not all binary expressions can be applied to ranges in a
1611 meaningful way. Handle only arithmetic operations. */
1612 if (code != PLUS_EXPR
1613 && code != MINUS_EXPR
1614 && code != MULT_EXPR
1615 && code != TRUNC_DIV_EXPR
1616 && code != FLOOR_DIV_EXPR
1617 && code != CEIL_DIV_EXPR
1618 && code != EXACT_DIV_EXPR
1619 && code != ROUND_DIV_EXPR
1620 && code != MIN_EXPR
1621 && code != MAX_EXPR
1622 && code != BIT_AND_EXPR
1623 && code != TRUTH_ANDIF_EXPR
1624 && code != TRUTH_ORIF_EXPR
1625 && code != TRUTH_AND_EXPR
1626 && code != TRUTH_OR_EXPR)
1628 set_value_range_to_varying (vr);
1629 return;
1632 /* Get value ranges for each operand. For constant operands, create
1633 a new value range with the operand to simplify processing. */
1634 op0 = TREE_OPERAND (expr, 0);
1635 if (TREE_CODE (op0) == SSA_NAME)
1636 vr0 = *(get_value_range (op0));
1637 else if (is_gimple_min_invariant (op0))
1638 set_value_range_to_value (&vr0, op0, NULL);
1639 else
1640 set_value_range_to_varying (&vr0);
1642 op1 = TREE_OPERAND (expr, 1);
1643 if (TREE_CODE (op1) == SSA_NAME)
1644 vr1 = *(get_value_range (op1));
1645 else if (is_gimple_min_invariant (op1))
1646 set_value_range_to_value (&vr1, op1, NULL);
1647 else
1648 set_value_range_to_varying (&vr1);
1650 /* If either range is UNDEFINED, so is the result. */
1651 if (vr0.type == VR_UNDEFINED || vr1.type == VR_UNDEFINED)
1653 set_value_range_to_undefined (vr);
1654 return;
1657 /* The type of the resulting value range defaults to VR0.TYPE. */
1658 type = vr0.type;
1660 /* Refuse to operate on VARYING ranges, ranges of different kinds
1661 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
1662 because we may be able to derive a useful range even if one of
1663 the operands is VR_VARYING or symbolic range. TODO, we may be
1664 able to derive anti-ranges in some cases. */
1665 if (code != BIT_AND_EXPR
1666 && code != TRUTH_AND_EXPR
1667 && code != TRUTH_OR_EXPR
1668 && (vr0.type == VR_VARYING
1669 || vr1.type == VR_VARYING
1670 || vr0.type != vr1.type
1671 || symbolic_range_p (&vr0)
1672 || symbolic_range_p (&vr1)))
1674 set_value_range_to_varying (vr);
1675 return;
1678 /* Now evaluate the expression to determine the new range. */
1679 if (POINTER_TYPE_P (TREE_TYPE (expr))
1680 || POINTER_TYPE_P (TREE_TYPE (op0))
1681 || POINTER_TYPE_P (TREE_TYPE (op1)))
1683 /* For pointer types, we are really only interested in asserting
1684 whether the expression evaluates to non-NULL. FIXME, we used
1685 to gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR), but
1686 ivopts is generating expressions with pointer multiplication
1687 in them. */
1688 if (code == PLUS_EXPR)
1690 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
1691 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1692 else if (range_is_null (&vr0) && range_is_null (&vr1))
1693 set_value_range_to_null (vr, TREE_TYPE (expr));
1694 else
1695 set_value_range_to_varying (vr);
1697 else
1699 /* Subtracting from a pointer, may yield 0, so just drop the
1700 resulting range to varying. */
1701 set_value_range_to_varying (vr);
1704 return;
1707 /* For integer ranges, apply the operation to each end of the
1708 range and see what we end up with. */
1709 if (code == TRUTH_ANDIF_EXPR
1710 || code == TRUTH_ORIF_EXPR
1711 || code == TRUTH_AND_EXPR
1712 || code == TRUTH_OR_EXPR)
1714 /* If one of the operands is zero, we know that the whole
1715 expression evaluates zero. */
1716 if (code == TRUTH_AND_EXPR
1717 && ((vr0.type == VR_RANGE
1718 && integer_zerop (vr0.min)
1719 && integer_zerop (vr0.max))
1720 || (vr1.type == VR_RANGE
1721 && integer_zerop (vr1.min)
1722 && integer_zerop (vr1.max))))
1724 type = VR_RANGE;
1725 min = max = build_int_cst (TREE_TYPE (expr), 0);
1727 /* If one of the operands is one, we know that the whole
1728 expression evaluates one. */
1729 else if (code == TRUTH_OR_EXPR
1730 && ((vr0.type == VR_RANGE
1731 && integer_onep (vr0.min)
1732 && integer_onep (vr0.max))
1733 || (vr1.type == VR_RANGE
1734 && integer_onep (vr1.min)
1735 && integer_onep (vr1.max))))
1737 type = VR_RANGE;
1738 min = max = build_int_cst (TREE_TYPE (expr), 1);
1740 else if (vr0.type != VR_VARYING
1741 && vr1.type != VR_VARYING
1742 && vr0.type == vr1.type
1743 && !symbolic_range_p (&vr0)
1744 && !overflow_infinity_range_p (&vr0)
1745 && !symbolic_range_p (&vr1)
1746 && !overflow_infinity_range_p (&vr1))
1748 /* Boolean expressions cannot be folded with int_const_binop. */
1749 min = fold_binary (code, TREE_TYPE (expr), vr0.min, vr1.min);
1750 max = fold_binary (code, TREE_TYPE (expr), vr0.max, vr1.max);
1752 else
1754 set_value_range_to_varying (vr);
1755 return;
1758 else if (code == PLUS_EXPR
1759 || code == MIN_EXPR
1760 || code == MAX_EXPR)
1762 /* If we have a PLUS_EXPR with two VR_ANTI_RANGEs, drop to
1763 VR_VARYING. It would take more effort to compute a precise
1764 range for such a case. For example, if we have op0 == 1 and
1765 op1 == -1 with their ranges both being ~[0,0], we would have
1766 op0 + op1 == 0, so we cannot claim that the sum is in ~[0,0].
1767 Note that we are guaranteed to have vr0.type == vr1.type at
1768 this point. */
1769 if (code == PLUS_EXPR && vr0.type == VR_ANTI_RANGE)
1771 set_value_range_to_varying (vr);
1772 return;
1775 /* For operations that make the resulting range directly
1776 proportional to the original ranges, apply the operation to
1777 the same end of each range. */
1778 min = vrp_int_const_binop (code, vr0.min, vr1.min);
1779 max = vrp_int_const_binop (code, vr0.max, vr1.max);
1781 else if (code == MULT_EXPR
1782 || code == TRUNC_DIV_EXPR
1783 || code == FLOOR_DIV_EXPR
1784 || code == CEIL_DIV_EXPR
1785 || code == EXACT_DIV_EXPR
1786 || code == ROUND_DIV_EXPR)
1788 tree val[4];
1789 size_t i;
1790 bool sop;
1792 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1793 drop to VR_VARYING. It would take more effort to compute a
1794 precise range for such a case. For example, if we have
1795 op0 == 65536 and op1 == 65536 with their ranges both being
1796 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1797 we cannot claim that the product is in ~[0,0]. Note that we
1798 are guaranteed to have vr0.type == vr1.type at this
1799 point. */
1800 if (code == MULT_EXPR
1801 && vr0.type == VR_ANTI_RANGE
1802 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
1804 set_value_range_to_varying (vr);
1805 return;
1808 /* Multiplications and divisions are a bit tricky to handle,
1809 depending on the mix of signs we have in the two ranges, we
1810 need to operate on different values to get the minimum and
1811 maximum values for the new range. One approach is to figure
1812 out all the variations of range combinations and do the
1813 operations.
1815 However, this involves several calls to compare_values and it
1816 is pretty convoluted. It's simpler to do the 4 operations
1817 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1818 MAX1) and then figure the smallest and largest values to form
1819 the new range. */
1821 /* Divisions by zero result in a VARYING value. */
1822 if (code != MULT_EXPR
1823 && (vr0.type == VR_ANTI_RANGE || range_includes_zero_p (&vr1)))
1825 set_value_range_to_varying (vr);
1826 return;
1829 /* Compute the 4 cross operations. */
1830 sop = false;
1831 val[0] = vrp_int_const_binop (code, vr0.min, vr1.min);
1832 if (val[0] == NULL_TREE)
1833 sop = true;
1835 if (vr1.max == vr1.min)
1836 val[1] = NULL_TREE;
1837 else
1839 val[1] = vrp_int_const_binop (code, vr0.min, vr1.max);
1840 if (val[1] == NULL_TREE)
1841 sop = true;
1844 if (vr0.max == vr0.min)
1845 val[2] = NULL_TREE;
1846 else
1848 val[2] = vrp_int_const_binop (code, vr0.max, vr1.min);
1849 if (val[2] == NULL_TREE)
1850 sop = true;
1853 if (vr0.min == vr0.max || vr1.min == vr1.max)
1854 val[3] = NULL_TREE;
1855 else
1857 val[3] = vrp_int_const_binop (code, vr0.max, vr1.max);
1858 if (val[3] == NULL_TREE)
1859 sop = true;
1862 if (sop)
1864 set_value_range_to_varying (vr);
1865 return;
1868 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
1869 of VAL[i]. */
1870 min = val[0];
1871 max = val[0];
1872 for (i = 1; i < 4; i++)
1874 if (!is_gimple_min_invariant (min)
1875 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
1876 || !is_gimple_min_invariant (max)
1877 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
1878 break;
1880 if (val[i])
1882 if (!is_gimple_min_invariant (val[i])
1883 || (TREE_OVERFLOW (val[i])
1884 && !is_overflow_infinity (val[i])))
1886 /* If we found an overflowed value, set MIN and MAX
1887 to it so that we set the resulting range to
1888 VARYING. */
1889 min = max = val[i];
1890 break;
1893 if (compare_values (val[i], min) == -1)
1894 min = val[i];
1896 if (compare_values (val[i], max) == 1)
1897 max = val[i];
1901 else if (code == MINUS_EXPR)
1903 /* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
1904 VR_VARYING. It would take more effort to compute a precise
1905 range for such a case. For example, if we have op0 == 1 and
1906 op1 == 1 with their ranges both being ~[0,0], we would have
1907 op0 - op1 == 0, so we cannot claim that the difference is in
1908 ~[0,0]. Note that we are guaranteed to have
1909 vr0.type == vr1.type at this point. */
1910 if (vr0.type == VR_ANTI_RANGE)
1912 set_value_range_to_varying (vr);
1913 return;
1916 /* For MINUS_EXPR, apply the operation to the opposite ends of
1917 each range. */
1918 min = vrp_int_const_binop (code, vr0.min, vr1.max);
1919 max = vrp_int_const_binop (code, vr0.max, vr1.min);
1921 else if (code == BIT_AND_EXPR)
1923 if (vr0.type == VR_RANGE
1924 && vr0.min == vr0.max
1925 && TREE_CODE (vr0.max) == INTEGER_CST
1926 && !TREE_OVERFLOW (vr0.max)
1927 && tree_int_cst_sgn (vr0.max) >= 0)
1929 min = build_int_cst (TREE_TYPE (expr), 0);
1930 max = vr0.max;
1932 else if (vr1.type == VR_RANGE
1933 && vr1.min == vr1.max
1934 && TREE_CODE (vr1.max) == INTEGER_CST
1935 && !TREE_OVERFLOW (vr1.max)
1936 && tree_int_cst_sgn (vr1.max) >= 0)
1938 type = VR_RANGE;
1939 min = build_int_cst (TREE_TYPE (expr), 0);
1940 max = vr1.max;
1942 else
1944 set_value_range_to_varying (vr);
1945 return;
1948 else
1949 gcc_unreachable ();
1951 /* If either MIN or MAX overflowed, then set the resulting range to
1952 VARYING. But we do accept an overflow infinity
1953 representation. */
1954 if (min == NULL_TREE
1955 || !is_gimple_min_invariant (min)
1956 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
1957 || max == NULL_TREE
1958 || !is_gimple_min_invariant (max)
1959 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
1961 set_value_range_to_varying (vr);
1962 return;
1965 /* We punt if:
1966 1) [-INF, +INF]
1967 2) [-INF, +-INF(OVF)]
1968 3) [+-INF(OVF), +INF]
1969 4) [+-INF(OVF), +-INF(OVF)]
1970 We learn nothing when we have INF and INF(OVF) on both sides.
1971 Note that we do accept [-INF, -INF] and [+INF, +INF] without
1972 overflow. */
1973 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
1974 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
1976 set_value_range_to_varying (vr);
1977 return;
1980 cmp = compare_values (min, max);
1981 if (cmp == -2 || cmp == 1)
1983 /* If the new range has its limits swapped around (MIN > MAX),
1984 then the operation caused one of them to wrap around, mark
1985 the new range VARYING. */
1986 set_value_range_to_varying (vr);
1988 else
1989 set_value_range (vr, type, min, max, NULL);
1993 /* Extract range information from a unary expression EXPR based on
1994 the range of its operand and the expression code. */
1996 static void
1997 extract_range_from_unary_expr (value_range_t *vr, tree expr)
1999 enum tree_code code = TREE_CODE (expr);
2000 tree min, max, op0;
2001 int cmp;
2002 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
2004 /* Refuse to operate on certain unary expressions for which we
2005 cannot easily determine a resulting range. */
2006 if (code == FIX_TRUNC_EXPR
2007 || code == FIX_CEIL_EXPR
2008 || code == FIX_FLOOR_EXPR
2009 || code == FIX_ROUND_EXPR
2010 || code == FLOAT_EXPR
2011 || code == BIT_NOT_EXPR
2012 || code == NON_LVALUE_EXPR
2013 || code == CONJ_EXPR)
2015 set_value_range_to_varying (vr);
2016 return;
2019 /* Get value ranges for the operand. For constant operands, create
2020 a new value range with the operand to simplify processing. */
2021 op0 = TREE_OPERAND (expr, 0);
2022 if (TREE_CODE (op0) == SSA_NAME)
2023 vr0 = *(get_value_range (op0));
2024 else if (is_gimple_min_invariant (op0))
2025 set_value_range_to_value (&vr0, op0, NULL);
2026 else
2027 set_value_range_to_varying (&vr0);
2029 /* If VR0 is UNDEFINED, so is the result. */
2030 if (vr0.type == VR_UNDEFINED)
2032 set_value_range_to_undefined (vr);
2033 return;
2036 /* Refuse to operate on symbolic ranges, or if neither operand is
2037 a pointer or integral type. */
2038 if ((!INTEGRAL_TYPE_P (TREE_TYPE (op0))
2039 && !POINTER_TYPE_P (TREE_TYPE (op0)))
2040 || (vr0.type != VR_VARYING
2041 && symbolic_range_p (&vr0)))
2043 set_value_range_to_varying (vr);
2044 return;
2047 /* If the expression involves pointers, we are only interested in
2048 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
2049 if (POINTER_TYPE_P (TREE_TYPE (expr)) || POINTER_TYPE_P (TREE_TYPE (op0)))
2051 bool sop;
2053 sop = false;
2054 if (range_is_nonnull (&vr0)
2055 || (tree_expr_nonzero_warnv_p (expr, &sop)
2056 && !sop))
2057 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
2058 else if (range_is_null (&vr0))
2059 set_value_range_to_null (vr, TREE_TYPE (expr));
2060 else
2061 set_value_range_to_varying (vr);
2063 return;
2066 /* Handle unary expressions on integer ranges. */
2067 if (code == NOP_EXPR || code == CONVERT_EXPR)
2069 tree inner_type = TREE_TYPE (op0);
2070 tree outer_type = TREE_TYPE (expr);
2072 /* If VR0 represents a simple range, then try to convert
2073 the min and max values for the range to the same type
2074 as OUTER_TYPE. If the results compare equal to VR0's
2075 min and max values and the new min is still less than
2076 or equal to the new max, then we can safely use the newly
2077 computed range for EXPR. This allows us to compute
2078 accurate ranges through many casts. */
2079 if ((vr0.type == VR_RANGE
2080 && !overflow_infinity_range_p (&vr0))
2081 || (vr0.type == VR_VARYING
2082 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)))
2084 tree new_min, new_max, orig_min, orig_max;
2086 /* Convert the input operand min/max to OUTER_TYPE. If
2087 the input has no range information, then use the min/max
2088 for the input's type. */
2089 if (vr0.type == VR_RANGE)
2091 orig_min = vr0.min;
2092 orig_max = vr0.max;
2094 else
2096 orig_min = TYPE_MIN_VALUE (inner_type);
2097 orig_max = TYPE_MAX_VALUE (inner_type);
2100 new_min = fold_convert (outer_type, orig_min);
2101 new_max = fold_convert (outer_type, orig_max);
2103 /* Verify the new min/max values are gimple values and
2104 that they compare equal to the original input's
2105 min/max values. */
2106 if (is_gimple_val (new_min)
2107 && is_gimple_val (new_max)
2108 && tree_int_cst_equal (new_min, orig_min)
2109 && tree_int_cst_equal (new_max, orig_max)
2110 && (!is_overflow_infinity (new_min)
2111 || !is_overflow_infinity (new_max))
2112 && compare_values (new_min, new_max) <= 0
2113 && compare_values (new_min, new_max) >= -1)
2115 set_value_range (vr, VR_RANGE, new_min, new_max, vr->equiv);
2116 return;
2120 /* When converting types of different sizes, set the result to
2121 VARYING. Things like sign extensions and precision loss may
2122 change the range. For instance, if x_3 is of type 'long long
2123 int' and 'y_5 = (unsigned short) x_3', if x_3 is ~[0, 0], it
2124 is impossible to know at compile time whether y_5 will be
2125 ~[0, 0]. */
2126 if (TYPE_SIZE (inner_type) != TYPE_SIZE (outer_type)
2127 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
2129 set_value_range_to_varying (vr);
2130 return;
2134 /* Conversion of a VR_VARYING value to a wider type can result
2135 in a usable range. So wait until after we've handled conversions
2136 before dropping the result to VR_VARYING if we had a source
2137 operand that is VR_VARYING. */
2138 if (vr0.type == VR_VARYING)
2140 set_value_range_to_varying (vr);
2141 return;
2144 /* Apply the operation to each end of the range and see what we end
2145 up with. */
2146 if (code == NEGATE_EXPR
2147 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
2149 /* NEGATE_EXPR flips the range around. We need to treat
2150 TYPE_MIN_VALUE specially. */
2151 if (is_positive_overflow_infinity (vr0.max))
2152 min = negative_overflow_infinity (TREE_TYPE (expr));
2153 else if (is_negative_overflow_infinity (vr0.max))
2154 min = positive_overflow_infinity (TREE_TYPE (expr));
2155 else if (!vrp_val_is_min (vr0.max))
2156 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2157 else if (needs_overflow_infinity (TREE_TYPE (expr)))
2159 if (supports_overflow_infinity (TREE_TYPE (expr))
2160 && !is_overflow_infinity (vr0.min)
2161 && !vrp_val_is_min (vr0.min))
2162 min = positive_overflow_infinity (TREE_TYPE (expr));
2163 else
2165 set_value_range_to_varying (vr);
2166 return;
2169 else
2170 min = TYPE_MIN_VALUE (TREE_TYPE (expr));
2172 if (is_positive_overflow_infinity (vr0.min))
2173 max = negative_overflow_infinity (TREE_TYPE (expr));
2174 else if (is_negative_overflow_infinity (vr0.min))
2175 max = positive_overflow_infinity (TREE_TYPE (expr));
2176 else if (!vrp_val_is_min (vr0.min))
2177 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2178 else if (needs_overflow_infinity (TREE_TYPE (expr)))
2180 if (supports_overflow_infinity (TREE_TYPE (expr)))
2181 max = positive_overflow_infinity (TREE_TYPE (expr));
2182 else
2184 set_value_range_to_varying (vr);
2185 return;
2188 else
2189 max = TYPE_MIN_VALUE (TREE_TYPE (expr));
2191 else if (code == NEGATE_EXPR
2192 && TYPE_UNSIGNED (TREE_TYPE (expr)))
2194 if (!range_includes_zero_p (&vr0))
2196 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2197 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2199 else
2201 if (range_is_null (&vr0))
2202 set_value_range_to_null (vr, TREE_TYPE (expr));
2203 else
2204 set_value_range_to_varying (vr);
2205 return;
2208 else if (code == ABS_EXPR
2209 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
2211 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
2212 useful range. */
2213 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (expr))
2214 && ((vr0.type == VR_RANGE
2215 && vrp_val_is_min (vr0.min))
2216 || (vr0.type == VR_ANTI_RANGE
2217 && !vrp_val_is_min (vr0.min)
2218 && !range_includes_zero_p (&vr0))))
2220 set_value_range_to_varying (vr);
2221 return;
2224 /* ABS_EXPR may flip the range around, if the original range
2225 included negative values. */
2226 if (is_overflow_infinity (vr0.min))
2227 min = positive_overflow_infinity (TREE_TYPE (expr));
2228 else if (!vrp_val_is_min (vr0.min))
2229 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2230 else if (!needs_overflow_infinity (TREE_TYPE (expr)))
2231 min = TYPE_MAX_VALUE (TREE_TYPE (expr));
2232 else if (supports_overflow_infinity (TREE_TYPE (expr)))
2233 min = positive_overflow_infinity (TREE_TYPE (expr));
2234 else
2236 set_value_range_to_varying (vr);
2237 return;
2240 if (is_overflow_infinity (vr0.max))
2241 max = positive_overflow_infinity (TREE_TYPE (expr));
2242 else if (!vrp_val_is_min (vr0.max))
2243 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2244 else if (!needs_overflow_infinity (TREE_TYPE (expr)))
2245 max = TYPE_MAX_VALUE (TREE_TYPE (expr));
2246 else if (supports_overflow_infinity (TREE_TYPE (expr)))
2247 max = positive_overflow_infinity (TREE_TYPE (expr));
2248 else
2250 set_value_range_to_varying (vr);
2251 return;
2254 cmp = compare_values (min, max);
2256 /* If a VR_ANTI_RANGEs contains zero, then we have
2257 ~[-INF, min(MIN, MAX)]. */
2258 if (vr0.type == VR_ANTI_RANGE)
2260 if (range_includes_zero_p (&vr0))
2262 /* Take the lower of the two values. */
2263 if (cmp != 1)
2264 max = min;
2266 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
2267 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
2268 flag_wrapv is set and the original anti-range doesn't include
2269 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
2270 if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
2272 tree type_min_value = TYPE_MIN_VALUE (TREE_TYPE (expr));
2274 min = (vr0.min != type_min_value
2275 ? int_const_binop (PLUS_EXPR, type_min_value,
2276 integer_one_node, 0)
2277 : type_min_value);
2279 else
2281 if (overflow_infinity_range_p (&vr0))
2282 min = negative_overflow_infinity (TREE_TYPE (expr));
2283 else
2284 min = TYPE_MIN_VALUE (TREE_TYPE (expr));
2287 else
2289 /* All else has failed, so create the range [0, INF], even for
2290 flag_wrapv since TYPE_MIN_VALUE is in the original
2291 anti-range. */
2292 vr0.type = VR_RANGE;
2293 min = build_int_cst (TREE_TYPE (expr), 0);
2294 if (needs_overflow_infinity (TREE_TYPE (expr)))
2296 if (supports_overflow_infinity (TREE_TYPE (expr)))
2297 max = positive_overflow_infinity (TREE_TYPE (expr));
2298 else
2300 set_value_range_to_varying (vr);
2301 return;
2304 else
2305 max = TYPE_MAX_VALUE (TREE_TYPE (expr));
2309 /* If the range contains zero then we know that the minimum value in the
2310 range will be zero. */
2311 else if (range_includes_zero_p (&vr0))
2313 if (cmp == 1)
2314 max = min;
2315 min = build_int_cst (TREE_TYPE (expr), 0);
2317 else
2319 /* If the range was reversed, swap MIN and MAX. */
2320 if (cmp == 1)
2322 tree t = min;
2323 min = max;
2324 max = t;
2328 else
2330 /* Otherwise, operate on each end of the range. */
2331 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2332 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2334 if (needs_overflow_infinity (TREE_TYPE (expr)))
2336 gcc_assert (code != NEGATE_EXPR && code != ABS_EXPR);
2338 /* If both sides have overflowed, we don't know
2339 anything. */
2340 if ((is_overflow_infinity (vr0.min)
2341 || TREE_OVERFLOW (min))
2342 && (is_overflow_infinity (vr0.max)
2343 || TREE_OVERFLOW (max)))
2345 set_value_range_to_varying (vr);
2346 return;
2349 if (is_overflow_infinity (vr0.min))
2350 min = vr0.min;
2351 else if (TREE_OVERFLOW (min))
2353 if (supports_overflow_infinity (TREE_TYPE (expr)))
2354 min = (tree_int_cst_sgn (min) >= 0
2355 ? positive_overflow_infinity (TREE_TYPE (min))
2356 : negative_overflow_infinity (TREE_TYPE (min)));
2357 else
2359 set_value_range_to_varying (vr);
2360 return;
2364 if (is_overflow_infinity (vr0.max))
2365 max = vr0.max;
2366 else if (TREE_OVERFLOW (max))
2368 if (supports_overflow_infinity (TREE_TYPE (expr)))
2369 max = (tree_int_cst_sgn (max) >= 0
2370 ? positive_overflow_infinity (TREE_TYPE (max))
2371 : negative_overflow_infinity (TREE_TYPE (max)));
2372 else
2374 set_value_range_to_varying (vr);
2375 return;
2381 cmp = compare_values (min, max);
2382 if (cmp == -2 || cmp == 1)
2384 /* If the new range has its limits swapped around (MIN > MAX),
2385 then the operation caused one of them to wrap around, mark
2386 the new range VARYING. */
2387 set_value_range_to_varying (vr);
2389 else
2390 set_value_range (vr, vr0.type, min, max, NULL);
2394 /* Extract range information from a comparison expression EXPR based
2395 on the range of its operand and the expression code. */
2397 static void
2398 extract_range_from_comparison (value_range_t *vr, tree expr)
2400 bool sop = false;
2401 tree val = vrp_evaluate_conditional_warnv (expr, false, &sop);
2403 /* A disadvantage of using a special infinity as an overflow
2404 representation is that we lose the ability to record overflow
2405 when we don't have an infinity. So we have to ignore a result
2406 which relies on overflow. */
2408 if (val && !is_overflow_infinity (val) && !sop)
2410 /* Since this expression was found on the RHS of an assignment,
2411 its type may be different from _Bool. Convert VAL to EXPR's
2412 type. */
2413 val = fold_convert (TREE_TYPE (expr), val);
2414 if (is_gimple_min_invariant (val))
2415 set_value_range_to_value (vr, val, vr->equiv);
2416 else
2417 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
2419 else
2420 set_value_range_to_varying (vr);
2424 /* Try to compute a useful range out of expression EXPR and store it
2425 in *VR. */
2427 static void
2428 extract_range_from_expr (value_range_t *vr, tree expr)
2430 enum tree_code code = TREE_CODE (expr);
2432 if (code == ASSERT_EXPR)
2433 extract_range_from_assert (vr, expr);
2434 else if (code == SSA_NAME)
2435 extract_range_from_ssa_name (vr, expr);
2436 else if (TREE_CODE_CLASS (code) == tcc_binary
2437 || code == TRUTH_ANDIF_EXPR
2438 || code == TRUTH_ORIF_EXPR
2439 || code == TRUTH_AND_EXPR
2440 || code == TRUTH_OR_EXPR
2441 || code == TRUTH_XOR_EXPR)
2442 extract_range_from_binary_expr (vr, expr);
2443 else if (TREE_CODE_CLASS (code) == tcc_unary)
2444 extract_range_from_unary_expr (vr, expr);
2445 else if (TREE_CODE_CLASS (code) == tcc_comparison)
2446 extract_range_from_comparison (vr, expr);
2447 else if (is_gimple_min_invariant (expr))
2448 set_value_range_to_value (vr, expr, NULL);
2449 else
2450 set_value_range_to_varying (vr);
2452 /* If we got a varying range from the tests above, try a final
2453 time to derive a nonnegative or nonzero range. This time
2454 relying primarily on generic routines in fold in conjunction
2455 with range data. */
2456 if (vr->type == VR_VARYING)
2458 bool sop = false;
2460 if (INTEGRAL_TYPE_P (TREE_TYPE (expr))
2461 && vrp_expr_computes_nonnegative (expr, &sop))
2462 set_value_range_to_nonnegative (vr, TREE_TYPE (expr),
2463 sop || is_overflow_infinity (expr));
2464 else if (vrp_expr_computes_nonzero (expr, &sop)
2465 && !sop)
2466 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
2470 /* Given a range VR, a LOOP and a variable VAR, determine whether it
2471 would be profitable to adjust VR using scalar evolution information
2472 for VAR. If so, update VR with the new limits. */
2474 static void
2475 adjust_range_with_scev (value_range_t *vr, struct loop *loop, tree stmt,
2476 tree var)
2478 tree init, step, chrec, tmin, tmax, min, max, type;
2479 enum ev_direction dir;
2481 /* TODO. Don't adjust anti-ranges. An anti-range may provide
2482 better opportunities than a regular range, but I'm not sure. */
2483 if (vr->type == VR_ANTI_RANGE)
2484 return;
2486 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
2487 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2488 return;
2490 init = initial_condition_in_loop_num (chrec, loop->num);
2491 step = evolution_part_in_loop_num (chrec, loop->num);
2493 /* If STEP is symbolic, we can't know whether INIT will be the
2494 minimum or maximum value in the range. Also, unless INIT is
2495 a simple expression, compare_values and possibly other functions
2496 in tree-vrp won't be able to handle it. */
2497 if (step == NULL_TREE
2498 || !is_gimple_min_invariant (step)
2499 || !valid_value_p (init))
2500 return;
2502 dir = scev_direction (chrec);
2503 if (/* Do not adjust ranges if we do not know whether the iv increases
2504 or decreases, ... */
2505 dir == EV_DIR_UNKNOWN
2506 /* ... or if it may wrap. */
2507 || scev_probably_wraps_p (init, step, stmt,
2508 current_loops->parray[CHREC_VARIABLE (chrec)],
2509 true))
2510 return;
2512 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
2513 negative_overflow_infinity and positive_overflow_infinity,
2514 because we have concluded that the loop probably does not
2515 wrap. */
2517 type = TREE_TYPE (var);
2518 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
2519 tmin = lower_bound_in_type (type, type);
2520 else
2521 tmin = TYPE_MIN_VALUE (type);
2522 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
2523 tmax = upper_bound_in_type (type, type);
2524 else
2525 tmax = TYPE_MAX_VALUE (type);
2527 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2529 min = tmin;
2530 max = tmax;
2532 /* For VARYING or UNDEFINED ranges, just about anything we get
2533 from scalar evolutions should be better. */
2535 if (dir == EV_DIR_DECREASES)
2536 max = init;
2537 else
2538 min = init;
2540 /* If we would create an invalid range, then just assume we
2541 know absolutely nothing. This may be over-conservative,
2542 but it's clearly safe, and should happen only in unreachable
2543 parts of code, or for invalid programs. */
2544 if (compare_values (min, max) == 1)
2545 return;
2547 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2549 else if (vr->type == VR_RANGE)
2551 min = vr->min;
2552 max = vr->max;
2554 if (dir == EV_DIR_DECREASES)
2556 /* INIT is the maximum value. If INIT is lower than VR->MAX
2557 but no smaller than VR->MIN, set VR->MAX to INIT. */
2558 if (compare_values (init, max) == -1)
2560 max = init;
2562 /* If we just created an invalid range with the minimum
2563 greater than the maximum, we fail conservatively.
2564 This should happen only in unreachable
2565 parts of code, or for invalid programs. */
2566 if (compare_values (min, max) == 1)
2567 return;
2570 /* According to the loop information, the variable does not
2571 overflow. If we think it does, probably because of an
2572 overflow due to arithmetic on a different INF value,
2573 reset now. */
2574 if (is_negative_overflow_infinity (min))
2575 min = tmin;
2577 else
2579 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
2580 if (compare_values (init, min) == 1)
2582 min = init;
2584 /* Again, avoid creating invalid range by failing. */
2585 if (compare_values (min, max) == 1)
2586 return;
2589 if (is_positive_overflow_infinity (max))
2590 max = tmax;
2593 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2597 /* Return true if VAR may overflow at STMT. This checks any available
2598 loop information to see if we can determine that VAR does not
2599 overflow. */
2601 static bool
2602 vrp_var_may_overflow (tree var, tree stmt)
2604 struct loop *l;
2605 tree chrec, init, step;
2607 if (current_loops == NULL)
2608 return true;
2610 l = loop_containing_stmt (stmt);
2611 if (l == NULL)
2612 return true;
2614 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
2615 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2616 return true;
2618 init = initial_condition_in_loop_num (chrec, l->num);
2619 step = evolution_part_in_loop_num (chrec, l->num);
2621 if (step == NULL_TREE
2622 || !is_gimple_min_invariant (step)
2623 || !valid_value_p (init))
2624 return true;
2626 /* If we get here, we know something useful about VAR based on the
2627 loop information. If it wraps, it may overflow. */
2629 if (scev_probably_wraps_p (init, step, stmt,
2630 current_loops->parray[CHREC_VARIABLE (chrec)],
2631 true))
2632 return true;
2634 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2636 print_generic_expr (dump_file, var, 0);
2637 fprintf (dump_file, ": loop information indicates does not overflow\n");
2640 return false;
2644 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
2646 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
2647 all the values in the ranges.
2649 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
2651 - Return NULL_TREE if it is not always possible to determine the
2652 value of the comparison.
2654 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
2655 overflow infinity was used in the test. */
2658 static tree
2659 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
2660 bool *strict_overflow_p)
2662 /* VARYING or UNDEFINED ranges cannot be compared. */
2663 if (vr0->type == VR_VARYING
2664 || vr0->type == VR_UNDEFINED
2665 || vr1->type == VR_VARYING
2666 || vr1->type == VR_UNDEFINED)
2667 return NULL_TREE;
2669 /* Anti-ranges need to be handled separately. */
2670 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
2672 /* If both are anti-ranges, then we cannot compute any
2673 comparison. */
2674 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
2675 return NULL_TREE;
2677 /* These comparisons are never statically computable. */
2678 if (comp == GT_EXPR
2679 || comp == GE_EXPR
2680 || comp == LT_EXPR
2681 || comp == LE_EXPR)
2682 return NULL_TREE;
2684 /* Equality can be computed only between a range and an
2685 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
2686 if (vr0->type == VR_RANGE)
2688 /* To simplify processing, make VR0 the anti-range. */
2689 value_range_t *tmp = vr0;
2690 vr0 = vr1;
2691 vr1 = tmp;
2694 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
2696 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
2697 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
2698 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2700 return NULL_TREE;
2703 if (!usable_range_p (vr0, strict_overflow_p)
2704 || !usable_range_p (vr1, strict_overflow_p))
2705 return NULL_TREE;
2707 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
2708 operands around and change the comparison code. */
2709 if (comp == GT_EXPR || comp == GE_EXPR)
2711 value_range_t *tmp;
2712 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
2713 tmp = vr0;
2714 vr0 = vr1;
2715 vr1 = tmp;
2718 if (comp == EQ_EXPR)
2720 /* Equality may only be computed if both ranges represent
2721 exactly one value. */
2722 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
2723 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
2725 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
2726 strict_overflow_p);
2727 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
2728 strict_overflow_p);
2729 if (cmp_min == 0 && cmp_max == 0)
2730 return boolean_true_node;
2731 else if (cmp_min != -2 && cmp_max != -2)
2732 return boolean_false_node;
2734 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
2735 else if (compare_values_warnv (vr0->min, vr1->max,
2736 strict_overflow_p) == 1
2737 || compare_values_warnv (vr1->min, vr0->max,
2738 strict_overflow_p) == 1)
2739 return boolean_false_node;
2741 return NULL_TREE;
2743 else if (comp == NE_EXPR)
2745 int cmp1, cmp2;
2747 /* If VR0 is completely to the left or completely to the right
2748 of VR1, they are always different. Notice that we need to
2749 make sure that both comparisons yield similar results to
2750 avoid comparing values that cannot be compared at
2751 compile-time. */
2752 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
2753 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
2754 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
2755 return boolean_true_node;
2757 /* If VR0 and VR1 represent a single value and are identical,
2758 return false. */
2759 else if (compare_values_warnv (vr0->min, vr0->max,
2760 strict_overflow_p) == 0
2761 && compare_values_warnv (vr1->min, vr1->max,
2762 strict_overflow_p) == 0
2763 && compare_values_warnv (vr0->min, vr1->min,
2764 strict_overflow_p) == 0
2765 && compare_values_warnv (vr0->max, vr1->max,
2766 strict_overflow_p) == 0)
2767 return boolean_false_node;
2769 /* Otherwise, they may or may not be different. */
2770 else
2771 return NULL_TREE;
2773 else if (comp == LT_EXPR || comp == LE_EXPR)
2775 int tst;
2777 /* If VR0 is to the left of VR1, return true. */
2778 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
2779 if ((comp == LT_EXPR && tst == -1)
2780 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2782 if (overflow_infinity_range_p (vr0)
2783 || overflow_infinity_range_p (vr1))
2784 *strict_overflow_p = true;
2785 return boolean_true_node;
2788 /* If VR0 is to the right of VR1, return false. */
2789 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
2790 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2791 || (comp == LE_EXPR && tst == 1))
2793 if (overflow_infinity_range_p (vr0)
2794 || overflow_infinity_range_p (vr1))
2795 *strict_overflow_p = true;
2796 return boolean_false_node;
2799 /* Otherwise, we don't know. */
2800 return NULL_TREE;
2803 gcc_unreachable ();
2807 /* Given a value range VR, a value VAL and a comparison code COMP, return
2808 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
2809 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
2810 always returns false. Return NULL_TREE if it is not always
2811 possible to determine the value of the comparison. Also set
2812 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
2813 infinity was used in the test. */
2815 static tree
2816 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
2817 bool *strict_overflow_p)
2819 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2820 return NULL_TREE;
2822 /* Anti-ranges need to be handled separately. */
2823 if (vr->type == VR_ANTI_RANGE)
2825 /* For anti-ranges, the only predicates that we can compute at
2826 compile time are equality and inequality. */
2827 if (comp == GT_EXPR
2828 || comp == GE_EXPR
2829 || comp == LT_EXPR
2830 || comp == LE_EXPR)
2831 return NULL_TREE;
2833 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
2834 if (value_inside_range (val, vr) == 1)
2835 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2837 return NULL_TREE;
2840 if (!usable_range_p (vr, strict_overflow_p))
2841 return NULL_TREE;
2843 if (comp == EQ_EXPR)
2845 /* EQ_EXPR may only be computed if VR represents exactly
2846 one value. */
2847 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
2849 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
2850 if (cmp == 0)
2851 return boolean_true_node;
2852 else if (cmp == -1 || cmp == 1 || cmp == 2)
2853 return boolean_false_node;
2855 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
2856 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
2857 return boolean_false_node;
2859 return NULL_TREE;
2861 else if (comp == NE_EXPR)
2863 /* If VAL is not inside VR, then they are always different. */
2864 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
2865 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
2866 return boolean_true_node;
2868 /* If VR represents exactly one value equal to VAL, then return
2869 false. */
2870 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
2871 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
2872 return boolean_false_node;
2874 /* Otherwise, they may or may not be different. */
2875 return NULL_TREE;
2877 else if (comp == LT_EXPR || comp == LE_EXPR)
2879 int tst;
2881 /* If VR is to the left of VAL, return true. */
2882 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
2883 if ((comp == LT_EXPR && tst == -1)
2884 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2886 if (overflow_infinity_range_p (vr))
2887 *strict_overflow_p = true;
2888 return boolean_true_node;
2891 /* If VR is to the right of VAL, return false. */
2892 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
2893 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2894 || (comp == LE_EXPR && tst == 1))
2896 if (overflow_infinity_range_p (vr))
2897 *strict_overflow_p = true;
2898 return boolean_false_node;
2901 /* Otherwise, we don't know. */
2902 return NULL_TREE;
2904 else if (comp == GT_EXPR || comp == GE_EXPR)
2906 int tst;
2908 /* If VR is to the right of VAL, return true. */
2909 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
2910 if ((comp == GT_EXPR && tst == 1)
2911 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
2913 if (overflow_infinity_range_p (vr))
2914 *strict_overflow_p = true;
2915 return boolean_true_node;
2918 /* If VR is to the left of VAL, return false. */
2919 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
2920 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
2921 || (comp == GE_EXPR && tst == -1))
2923 if (overflow_infinity_range_p (vr))
2924 *strict_overflow_p = true;
2925 return boolean_false_node;
2928 /* Otherwise, we don't know. */
2929 return NULL_TREE;
2932 gcc_unreachable ();
2936 /* Debugging dumps. */
2938 void dump_value_range (FILE *, value_range_t *);
2939 void debug_value_range (value_range_t *);
2940 void dump_all_value_ranges (FILE *);
2941 void debug_all_value_ranges (void);
2942 void dump_vr_equiv (FILE *, bitmap);
2943 void debug_vr_equiv (bitmap);
2946 /* Dump value range VR to FILE. */
2948 void
2949 dump_value_range (FILE *file, value_range_t *vr)
2951 if (vr == NULL)
2952 fprintf (file, "[]");
2953 else if (vr->type == VR_UNDEFINED)
2954 fprintf (file, "UNDEFINED");
2955 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
2957 tree type = TREE_TYPE (vr->min);
2959 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
2961 if (is_negative_overflow_infinity (vr->min))
2962 fprintf (file, "-INF(OVF)");
2963 else if (INTEGRAL_TYPE_P (type)
2964 && !TYPE_UNSIGNED (type)
2965 && vrp_val_is_min (vr->min))
2966 fprintf (file, "-INF");
2967 else
2968 print_generic_expr (file, vr->min, 0);
2970 fprintf (file, ", ");
2972 if (is_positive_overflow_infinity (vr->max))
2973 fprintf (file, "+INF(OVF)");
2974 else if (INTEGRAL_TYPE_P (type)
2975 && vrp_val_is_max (vr->max))
2976 fprintf (file, "+INF");
2977 else
2978 print_generic_expr (file, vr->max, 0);
2980 fprintf (file, "]");
2982 if (vr->equiv)
2984 bitmap_iterator bi;
2985 unsigned i, c = 0;
2987 fprintf (file, " EQUIVALENCES: { ");
2989 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
2991 print_generic_expr (file, ssa_name (i), 0);
2992 fprintf (file, " ");
2993 c++;
2996 fprintf (file, "} (%u elements)", c);
2999 else if (vr->type == VR_VARYING)
3000 fprintf (file, "VARYING");
3001 else
3002 fprintf (file, "INVALID RANGE");
3006 /* Dump value range VR to stderr. */
3008 void
3009 debug_value_range (value_range_t *vr)
3011 dump_value_range (stderr, vr);
3012 fprintf (stderr, "\n");
3016 /* Dump value ranges of all SSA_NAMEs to FILE. */
3018 void
3019 dump_all_value_ranges (FILE *file)
3021 size_t i;
3023 for (i = 0; i < num_ssa_names; i++)
3025 if (vr_value[i])
3027 print_generic_expr (file, ssa_name (i), 0);
3028 fprintf (file, ": ");
3029 dump_value_range (file, vr_value[i]);
3030 fprintf (file, "\n");
3034 fprintf (file, "\n");
3038 /* Dump all value ranges to stderr. */
3040 void
3041 debug_all_value_ranges (void)
3043 dump_all_value_ranges (stderr);
3047 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
3048 create a new SSA name N and return the assertion assignment
3049 'V = ASSERT_EXPR <V, V OP W>'. */
3051 static tree
3052 build_assert_expr_for (tree cond, tree v)
3054 tree n, assertion;
3056 gcc_assert (TREE_CODE (v) == SSA_NAME);
3057 n = duplicate_ssa_name (v, NULL_TREE);
3059 if (COMPARISON_CLASS_P (cond))
3061 tree a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
3062 assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, a);
3064 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
3066 /* Given !V, build the assignment N = false. */
3067 tree op0 = TREE_OPERAND (cond, 0);
3068 gcc_assert (op0 == v);
3069 assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, boolean_false_node);
3071 else if (TREE_CODE (cond) == SSA_NAME)
3073 /* Given V, build the assignment N = true. */
3074 gcc_assert (v == cond);
3075 assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, boolean_true_node);
3077 else
3078 gcc_unreachable ();
3080 SSA_NAME_DEF_STMT (n) = assertion;
3082 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
3083 operand of the ASSERT_EXPR. Register the new name and the old one
3084 in the replacement table so that we can fix the SSA web after
3085 adding all the ASSERT_EXPRs. */
3086 register_new_name_mapping (n, v);
3088 return assertion;
3092 /* Return false if EXPR is a predicate expression involving floating
3093 point values. */
3095 static inline bool
3096 fp_predicate (tree expr)
3098 return (COMPARISON_CLASS_P (expr)
3099 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0))));
3103 /* If the range of values taken by OP can be inferred after STMT executes,
3104 return the comparison code (COMP_CODE_P) and value (VAL_P) that
3105 describes the inferred range. Return true if a range could be
3106 inferred. */
3108 static bool
3109 infer_value_range (tree stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
3111 *val_p = NULL_TREE;
3112 *comp_code_p = ERROR_MARK;
3114 /* Do not attempt to infer anything in names that flow through
3115 abnormal edges. */
3116 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
3117 return false;
3119 /* Similarly, don't infer anything from statements that may throw
3120 exceptions. */
3121 if (tree_could_throw_p (stmt))
3122 return false;
3124 /* If STMT is the last statement of a basic block with no
3125 successors, there is no point inferring anything about any of its
3126 operands. We would not be able to find a proper insertion point
3127 for the assertion, anyway. */
3128 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (bb_for_stmt (stmt)->succs) == 0)
3129 return false;
3131 /* We can only assume that a pointer dereference will yield
3132 non-NULL if -fdelete-null-pointer-checks is enabled. */
3133 if (flag_delete_null_pointer_checks && POINTER_TYPE_P (TREE_TYPE (op)))
3135 bool is_store;
3136 unsigned num_uses, num_derefs;
3138 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs, &is_store);
3139 if (num_derefs > 0)
3141 *val_p = build_int_cst (TREE_TYPE (op), 0);
3142 *comp_code_p = NE_EXPR;
3143 return true;
3147 return false;
3151 void dump_asserts_for (FILE *, tree);
3152 void debug_asserts_for (tree);
3153 void dump_all_asserts (FILE *);
3154 void debug_all_asserts (void);
3156 /* Dump all the registered assertions for NAME to FILE. */
3158 void
3159 dump_asserts_for (FILE *file, tree name)
3161 assert_locus_t loc;
3163 fprintf (file, "Assertions to be inserted for ");
3164 print_generic_expr (file, name, 0);
3165 fprintf (file, "\n");
3167 loc = asserts_for[SSA_NAME_VERSION (name)];
3168 while (loc)
3170 fprintf (file, "\t");
3171 print_generic_expr (file, bsi_stmt (loc->si), 0);
3172 fprintf (file, "\n\tBB #%d", loc->bb->index);
3173 if (loc->e)
3175 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
3176 loc->e->dest->index);
3177 dump_edge_info (file, loc->e, 0);
3179 fprintf (file, "\n\tPREDICATE: ");
3180 print_generic_expr (file, name, 0);
3181 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
3182 print_generic_expr (file, loc->val, 0);
3183 fprintf (file, "\n\n");
3184 loc = loc->next;
3187 fprintf (file, "\n");
3191 /* Dump all the registered assertions for NAME to stderr. */
3193 void
3194 debug_asserts_for (tree name)
3196 dump_asserts_for (stderr, name);
3200 /* Dump all the registered assertions for all the names to FILE. */
3202 void
3203 dump_all_asserts (FILE *file)
3205 unsigned i;
3206 bitmap_iterator bi;
3208 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
3209 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3210 dump_asserts_for (file, ssa_name (i));
3211 fprintf (file, "\n");
3215 /* Dump all the registered assertions for all the names to stderr. */
3217 void
3218 debug_all_asserts (void)
3220 dump_all_asserts (stderr);
3224 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
3225 'NAME COMP_CODE VAL' at a location that dominates block BB or
3226 E->DEST, then register this location as a possible insertion point
3227 for ASSERT_EXPR <NAME, NAME COMP_CODE VAL>.
3229 BB, E and SI provide the exact insertion point for the new
3230 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
3231 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
3232 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
3233 must not be NULL. */
3235 static void
3236 register_new_assert_for (tree name,
3237 enum tree_code comp_code,
3238 tree val,
3239 basic_block bb,
3240 edge e,
3241 block_stmt_iterator si)
3243 assert_locus_t n, loc, last_loc;
3244 bool found;
3245 basic_block dest_bb;
3247 #if defined ENABLE_CHECKING
3248 gcc_assert (bb == NULL || e == NULL);
3250 if (e == NULL)
3251 gcc_assert (TREE_CODE (bsi_stmt (si)) != COND_EXPR
3252 && TREE_CODE (bsi_stmt (si)) != SWITCH_EXPR);
3253 #endif
3255 /* The new assertion A will be inserted at BB or E. We need to
3256 determine if the new location is dominated by a previously
3257 registered location for A. If we are doing an edge insertion,
3258 assume that A will be inserted at E->DEST. Note that this is not
3259 necessarily true.
3261 If E is a critical edge, it will be split. But even if E is
3262 split, the new block will dominate the same set of blocks that
3263 E->DEST dominates.
3265 The reverse, however, is not true, blocks dominated by E->DEST
3266 will not be dominated by the new block created to split E. So,
3267 if the insertion location is on a critical edge, we will not use
3268 the new location to move another assertion previously registered
3269 at a block dominated by E->DEST. */
3270 dest_bb = (bb) ? bb : e->dest;
3272 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
3273 VAL at a block dominating DEST_BB, then we don't need to insert a new
3274 one. Similarly, if the same assertion already exists at a block
3275 dominated by DEST_BB and the new location is not on a critical
3276 edge, then update the existing location for the assertion (i.e.,
3277 move the assertion up in the dominance tree).
3279 Note, this is implemented as a simple linked list because there
3280 should not be more than a handful of assertions registered per
3281 name. If this becomes a performance problem, a table hashed by
3282 COMP_CODE and VAL could be implemented. */
3283 loc = asserts_for[SSA_NAME_VERSION (name)];
3284 last_loc = loc;
3285 found = false;
3286 while (loc)
3288 if (loc->comp_code == comp_code
3289 && (loc->val == val
3290 || operand_equal_p (loc->val, val, 0)))
3292 /* If the assertion NAME COMP_CODE VAL has already been
3293 registered at a basic block that dominates DEST_BB, then
3294 we don't need to insert the same assertion again. Note
3295 that we don't check strict dominance here to avoid
3296 replicating the same assertion inside the same basic
3297 block more than once (e.g., when a pointer is
3298 dereferenced several times inside a block).
3300 An exception to this rule are edge insertions. If the
3301 new assertion is to be inserted on edge E, then it will
3302 dominate all the other insertions that we may want to
3303 insert in DEST_BB. So, if we are doing an edge
3304 insertion, don't do this dominance check. */
3305 if (e == NULL
3306 && dominated_by_p (CDI_DOMINATORS, dest_bb, loc->bb))
3307 return;
3309 /* Otherwise, if E is not a critical edge and DEST_BB
3310 dominates the existing location for the assertion, move
3311 the assertion up in the dominance tree by updating its
3312 location information. */
3313 if ((e == NULL || !EDGE_CRITICAL_P (e))
3314 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
3316 loc->bb = dest_bb;
3317 loc->e = e;
3318 loc->si = si;
3319 return;
3323 /* Update the last node of the list and move to the next one. */
3324 last_loc = loc;
3325 loc = loc->next;
3328 /* If we didn't find an assertion already registered for
3329 NAME COMP_CODE VAL, add a new one at the end of the list of
3330 assertions associated with NAME. */
3331 n = XNEW (struct assert_locus_d);
3332 n->bb = dest_bb;
3333 n->e = e;
3334 n->si = si;
3335 n->comp_code = comp_code;
3336 n->val = val;
3337 n->next = NULL;
3339 if (last_loc)
3340 last_loc->next = n;
3341 else
3342 asserts_for[SSA_NAME_VERSION (name)] = n;
3344 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
3348 /* Try to register an edge assertion for SSA name NAME on edge E for
3349 the conditional jump pointed to by SI. Return true if an assertion
3350 for NAME could be registered. */
3352 static bool
3353 register_edge_assert_for (tree name, edge e, block_stmt_iterator si)
3355 tree val, stmt;
3356 enum tree_code comp_code;
3358 stmt = bsi_stmt (si);
3360 /* Do not attempt to infer anything in names that flow through
3361 abnormal edges. */
3362 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
3363 return false;
3365 /* If NAME was not found in the sub-graph reachable from E, then
3366 there's nothing to do. */
3367 if (!TEST_BIT (found_in_subgraph, SSA_NAME_VERSION (name)))
3368 return false;
3370 /* We found a use of NAME in the sub-graph rooted at E->DEST.
3371 Register an assertion for NAME according to the value that NAME
3372 takes on edge E. */
3373 if (TREE_CODE (stmt) == COND_EXPR)
3375 /* If BB ends in a COND_EXPR then NAME then we should insert
3376 the original predicate on EDGE_TRUE_VALUE and the
3377 opposite predicate on EDGE_FALSE_VALUE. */
3378 tree cond = COND_EXPR_COND (stmt);
3379 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
3381 /* Predicates may be a single SSA name or NAME OP VAL. */
3382 if (cond == name)
3384 /* If the predicate is a name, it must be NAME, in which
3385 case we create the predicate NAME == true or
3386 NAME == false accordingly. */
3387 comp_code = EQ_EXPR;
3388 val = (is_else_edge) ? boolean_false_node : boolean_true_node;
3390 else
3392 /* Otherwise, we have a comparison of the form NAME COMP VAL
3393 or VAL COMP NAME. */
3394 if (name == TREE_OPERAND (cond, 1))
3396 /* If the predicate is of the form VAL COMP NAME, flip
3397 COMP around because we need to register NAME as the
3398 first operand in the predicate. */
3399 comp_code = swap_tree_comparison (TREE_CODE (cond));
3400 val = TREE_OPERAND (cond, 0);
3402 else
3404 /* The comparison is of the form NAME COMP VAL, so the
3405 comparison code remains unchanged. */
3406 comp_code = TREE_CODE (cond);
3407 val = TREE_OPERAND (cond, 1);
3410 /* If we are inserting the assertion on the ELSE edge, we
3411 need to invert the sign comparison. */
3412 if (is_else_edge)
3413 comp_code = invert_tree_comparison (comp_code, 0);
3415 /* Do not register always-false predicates. FIXME, this
3416 works around a limitation in fold() when dealing with
3417 enumerations. Given 'enum { N1, N2 } x;', fold will not
3418 fold 'if (x > N2)' to 'if (0)'. */
3419 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
3420 && (INTEGRAL_TYPE_P (TREE_TYPE (val))
3421 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (val))))
3423 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
3424 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
3426 if (comp_code == GT_EXPR && compare_values (val, max) == 0)
3427 return false;
3429 if (comp_code == LT_EXPR && compare_values (val, min) == 0)
3430 return false;
3434 else
3436 /* FIXME. Handle SWITCH_EXPR. */
3437 gcc_unreachable ();
3440 register_new_assert_for (name, comp_code, val, NULL, e, si);
3441 return true;
3445 static bool find_assert_locations (basic_block bb);
3447 /* Determine whether the outgoing edges of BB should receive an
3448 ASSERT_EXPR for each of the operands of BB's last statement. The
3449 last statement of BB must be a COND_EXPR or a SWITCH_EXPR.
3451 If any of the sub-graphs rooted at BB have an interesting use of
3452 the predicate operands, an assert location node is added to the
3453 list of assertions for the corresponding operands. */
3455 static bool
3456 find_conditional_asserts (basic_block bb)
3458 bool need_assert;
3459 block_stmt_iterator last_si;
3460 tree op, last;
3461 edge_iterator ei;
3462 edge e;
3463 ssa_op_iter iter;
3465 need_assert = false;
3466 last_si = bsi_last (bb);
3467 last = bsi_stmt (last_si);
3469 /* Look for uses of the operands in each of the sub-graphs
3470 rooted at BB. We need to check each of the outgoing edges
3471 separately, so that we know what kind of ASSERT_EXPR to
3472 insert. */
3473 FOR_EACH_EDGE (e, ei, bb->succs)
3475 if (e->dest == bb)
3476 continue;
3478 /* Remove the COND_EXPR operands from the FOUND_IN_SUBGRAPH bitmap.
3479 Otherwise, when we finish traversing each of the sub-graphs, we
3480 won't know whether the variables were found in the sub-graphs or
3481 if they had been found in a block upstream from BB.
3483 This is actually a bad idea is some cases, particularly jump
3484 threading. Consider a CFG like the following:
3494 Assume that one or more operands in the conditional at the
3495 end of block 0 are used in a conditional in block 2, but not
3496 anywhere in block 1. In this case we will not insert any
3497 assert statements in block 1, which may cause us to miss
3498 opportunities to optimize, particularly for jump threading. */
3499 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3500 RESET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3502 /* Traverse the strictly dominated sub-graph rooted at E->DEST
3503 to determine if any of the operands in the conditional
3504 predicate are used. */
3505 if (e->dest != bb)
3506 need_assert |= find_assert_locations (e->dest);
3508 /* Register the necessary assertions for each operand in the
3509 conditional predicate. */
3510 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3511 need_assert |= register_edge_assert_for (op, e, last_si);
3514 /* Finally, indicate that we have found the operands in the
3515 conditional. */
3516 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3517 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3519 return need_assert;
3523 /* Traverse all the statements in block BB looking for statements that
3524 may generate useful assertions for the SSA names in their operand.
3525 If a statement produces a useful assertion A for name N_i, then the
3526 list of assertions already generated for N_i is scanned to
3527 determine if A is actually needed.
3529 If N_i already had the assertion A at a location dominating the
3530 current location, then nothing needs to be done. Otherwise, the
3531 new location for A is recorded instead.
3533 1- For every statement S in BB, all the variables used by S are
3534 added to bitmap FOUND_IN_SUBGRAPH.
3536 2- If statement S uses an operand N in a way that exposes a known
3537 value range for N, then if N was not already generated by an
3538 ASSERT_EXPR, create a new assert location for N. For instance,
3539 if N is a pointer and the statement dereferences it, we can
3540 assume that N is not NULL.
3542 3- COND_EXPRs are a special case of #2. We can derive range
3543 information from the predicate but need to insert different
3544 ASSERT_EXPRs for each of the sub-graphs rooted at the
3545 conditional block. If the last statement of BB is a conditional
3546 expression of the form 'X op Y', then
3548 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3550 b) If the conditional is the only entry point to the sub-graph
3551 corresponding to the THEN_CLAUSE, recurse into it. On
3552 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3553 an ASSERT_EXPR is added for the corresponding variable.
3555 c) Repeat step (b) on the ELSE_CLAUSE.
3557 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3559 For instance,
3561 if (a == 9)
3562 b = a;
3563 else
3564 b = c + 1;
3566 In this case, an assertion on the THEN clause is useful to
3567 determine that 'a' is always 9 on that edge. However, an assertion
3568 on the ELSE clause would be unnecessary.
3570 4- If BB does not end in a conditional expression, then we recurse
3571 into BB's dominator children.
3573 At the end of the recursive traversal, every SSA name will have a
3574 list of locations where ASSERT_EXPRs should be added. When a new
3575 location for name N is found, it is registered by calling
3576 register_new_assert_for. That function keeps track of all the
3577 registered assertions to prevent adding unnecessary assertions.
3578 For instance, if a pointer P_4 is dereferenced more than once in a
3579 dominator tree, only the location dominating all the dereference of
3580 P_4 will receive an ASSERT_EXPR.
3582 If this function returns true, then it means that there are names
3583 for which we need to generate ASSERT_EXPRs. Those assertions are
3584 inserted by process_assert_insertions.
3586 TODO. Handle SWITCH_EXPR. */
3588 static bool
3589 find_assert_locations (basic_block bb)
3591 block_stmt_iterator si;
3592 tree last, phi;
3593 bool need_assert;
3594 basic_block son;
3596 if (TEST_BIT (blocks_visited, bb->index))
3597 return false;
3599 SET_BIT (blocks_visited, bb->index);
3601 need_assert = false;
3603 /* Traverse all PHI nodes in BB marking used operands. */
3604 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3606 use_operand_p arg_p;
3607 ssa_op_iter i;
3609 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
3611 tree arg = USE_FROM_PTR (arg_p);
3612 if (TREE_CODE (arg) == SSA_NAME)
3614 gcc_assert (is_gimple_reg (PHI_RESULT (phi)));
3615 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (arg));
3620 /* Traverse all the statements in BB marking used names and looking
3621 for statements that may infer assertions for their used operands. */
3622 last = NULL_TREE;
3623 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3625 tree stmt, op;
3626 ssa_op_iter i;
3628 stmt = bsi_stmt (si);
3630 /* See if we can derive an assertion for any of STMT's operands. */
3631 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
3633 tree value;
3634 enum tree_code comp_code;
3636 /* Mark OP in bitmap FOUND_IN_SUBGRAPH. If STMT is inside
3637 the sub-graph of a conditional block, when we return from
3638 this recursive walk, our parent will use the
3639 FOUND_IN_SUBGRAPH bitset to determine if one of the
3640 operands it was looking for was present in the sub-graph. */
3641 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3643 /* If OP is used in such a way that we can infer a value
3644 range for it, and we don't find a previous assertion for
3645 it, create a new assertion location node for OP. */
3646 if (infer_value_range (stmt, op, &comp_code, &value))
3648 /* If we are able to infer a nonzero value range for OP,
3649 then walk backwards through the use-def chain to see if OP
3650 was set via a typecast.
3652 If so, then we can also infer a nonzero value range
3653 for the operand of the NOP_EXPR. */
3654 if (comp_code == NE_EXPR && integer_zerop (value))
3656 tree t = op;
3657 tree def_stmt = SSA_NAME_DEF_STMT (t);
3659 while (TREE_CODE (def_stmt) == MODIFY_EXPR
3660 && TREE_CODE (TREE_OPERAND (def_stmt, 1)) == NOP_EXPR
3661 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0)) == SSA_NAME
3662 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0))))
3664 t = TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0);
3665 def_stmt = SSA_NAME_DEF_STMT (t);
3667 /* Note we want to register the assert for the
3668 operand of the NOP_EXPR after SI, not after the
3669 conversion. */
3670 if (! has_single_use (t))
3672 register_new_assert_for (t, comp_code, value,
3673 bb, NULL, si);
3674 need_assert = true;
3679 /* If OP is used only once, namely in this STMT, don't
3680 bother creating an ASSERT_EXPR for it. Such an
3681 ASSERT_EXPR would do nothing but increase compile time. */
3682 if (!has_single_use (op))
3684 register_new_assert_for (op, comp_code, value, bb, NULL, si);
3685 need_assert = true;
3690 /* Remember the last statement of the block. */
3691 last = stmt;
3694 /* If BB's last statement is a conditional expression
3695 involving integer operands, recurse into each of the sub-graphs
3696 rooted at BB to determine if we need to add ASSERT_EXPRs. */
3697 if (last
3698 && TREE_CODE (last) == COND_EXPR
3699 && !fp_predicate (COND_EXPR_COND (last))
3700 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
3701 need_assert |= find_conditional_asserts (bb);
3703 /* Recurse into the dominator children of BB. */
3704 for (son = first_dom_son (CDI_DOMINATORS, bb);
3705 son;
3706 son = next_dom_son (CDI_DOMINATORS, son))
3707 need_assert |= find_assert_locations (son);
3709 return need_assert;
3713 /* Create an ASSERT_EXPR for NAME and insert it in the location
3714 indicated by LOC. Return true if we made any edge insertions. */
3716 static bool
3717 process_assert_insertions_for (tree name, assert_locus_t loc)
3719 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3720 tree stmt, cond, assert_expr;
3721 edge_iterator ei;
3722 edge e;
3724 cond = build2 (loc->comp_code, boolean_type_node, name, loc->val);
3725 assert_expr = build_assert_expr_for (cond, name);
3727 if (loc->e)
3729 /* We have been asked to insert the assertion on an edge. This
3730 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3731 #if defined ENABLE_CHECKING
3732 gcc_assert (TREE_CODE (bsi_stmt (loc->si)) == COND_EXPR
3733 || TREE_CODE (bsi_stmt (loc->si)) == SWITCH_EXPR);
3734 #endif
3736 bsi_insert_on_edge (loc->e, assert_expr);
3737 return true;
3740 /* Otherwise, we can insert right after LOC->SI iff the
3741 statement must not be the last statement in the block. */
3742 stmt = bsi_stmt (loc->si);
3743 if (!stmt_ends_bb_p (stmt))
3745 bsi_insert_after (&loc->si, assert_expr, BSI_SAME_STMT);
3746 return false;
3749 /* If STMT must be the last statement in BB, we can only insert new
3750 assertions on the non-abnormal edge out of BB. Note that since
3751 STMT is not control flow, there may only be one non-abnormal edge
3752 out of BB. */
3753 FOR_EACH_EDGE (e, ei, loc->bb->succs)
3754 if (!(e->flags & EDGE_ABNORMAL))
3756 bsi_insert_on_edge (e, assert_expr);
3757 return true;
3760 gcc_unreachable ();
3764 /* Process all the insertions registered for every name N_i registered
3765 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3766 found in ASSERTS_FOR[i]. */
3768 static void
3769 process_assert_insertions (void)
3771 unsigned i;
3772 bitmap_iterator bi;
3773 bool update_edges_p = false;
3774 int num_asserts = 0;
3776 if (dump_file && (dump_flags & TDF_DETAILS))
3777 dump_all_asserts (dump_file);
3779 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3781 assert_locus_t loc = asserts_for[i];
3782 gcc_assert (loc);
3784 while (loc)
3786 assert_locus_t next = loc->next;
3787 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
3788 free (loc);
3789 loc = next;
3790 num_asserts++;
3794 if (update_edges_p)
3795 bsi_commit_edge_inserts ();
3797 if (dump_file && (dump_flags & TDF_STATS))
3798 fprintf (dump_file, "\nNumber of ASSERT_EXPR expressions inserted: %d\n\n",
3799 num_asserts);
3803 /* Traverse the flowgraph looking for conditional jumps to insert range
3804 expressions. These range expressions are meant to provide information
3805 to optimizations that need to reason in terms of value ranges. They
3806 will not be expanded into RTL. For instance, given:
3808 x = ...
3809 y = ...
3810 if (x < y)
3811 y = x - 2;
3812 else
3813 x = y + 3;
3815 this pass will transform the code into:
3817 x = ...
3818 y = ...
3819 if (x < y)
3821 x = ASSERT_EXPR <x, x < y>
3822 y = x - 2
3824 else
3826 y = ASSERT_EXPR <y, x <= y>
3827 x = y + 3
3830 The idea is that once copy and constant propagation have run, other
3831 optimizations will be able to determine what ranges of values can 'x'
3832 take in different paths of the code, simply by checking the reaching
3833 definition of 'x'. */
3835 static void
3836 insert_range_assertions (void)
3838 edge e;
3839 edge_iterator ei;
3840 bool update_ssa_p;
3842 found_in_subgraph = sbitmap_alloc (num_ssa_names);
3843 sbitmap_zero (found_in_subgraph);
3845 blocks_visited = sbitmap_alloc (last_basic_block);
3846 sbitmap_zero (blocks_visited);
3848 need_assert_for = BITMAP_ALLOC (NULL);
3849 asserts_for = XNEWVEC (assert_locus_t, num_ssa_names);
3850 memset (asserts_for, 0, num_ssa_names * sizeof (assert_locus_t));
3852 calculate_dominance_info (CDI_DOMINATORS);
3854 update_ssa_p = false;
3855 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3856 if (find_assert_locations (e->dest))
3857 update_ssa_p = true;
3859 if (update_ssa_p)
3861 process_assert_insertions ();
3862 update_ssa (TODO_update_ssa_no_phi);
3865 if (dump_file && (dump_flags & TDF_DETAILS))
3867 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
3868 dump_function_to_file (current_function_decl, dump_file, dump_flags);
3871 sbitmap_free (found_in_subgraph);
3872 free (asserts_for);
3873 BITMAP_FREE (need_assert_for);
3877 /* Convert range assertion expressions into the implied copies and
3878 copy propagate away the copies. Doing the trivial copy propagation
3879 here avoids the need to run the full copy propagation pass after
3880 VRP.
3882 FIXME, this will eventually lead to copy propagation removing the
3883 names that had useful range information attached to them. For
3884 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
3885 then N_i will have the range [3, +INF].
3887 However, by converting the assertion into the implied copy
3888 operation N_i = N_j, we will then copy-propagate N_j into the uses
3889 of N_i and lose the range information. We may want to hold on to
3890 ASSERT_EXPRs a little while longer as the ranges could be used in
3891 things like jump threading.
3893 The problem with keeping ASSERT_EXPRs around is that passes after
3894 VRP need to handle them appropriately.
3896 Another approach would be to make the range information a first
3897 class property of the SSA_NAME so that it can be queried from
3898 any pass. This is made somewhat more complex by the need for
3899 multiple ranges to be associated with one SSA_NAME. */
3901 static void
3902 remove_range_assertions (void)
3904 basic_block bb;
3905 block_stmt_iterator si;
3907 /* Note that the BSI iterator bump happens at the bottom of the
3908 loop and no bump is necessary if we're removing the statement
3909 referenced by the current BSI. */
3910 FOR_EACH_BB (bb)
3911 for (si = bsi_start (bb); !bsi_end_p (si);)
3913 tree stmt = bsi_stmt (si);
3914 tree use_stmt;
3916 if (TREE_CODE (stmt) == MODIFY_EXPR
3917 && TREE_CODE (TREE_OPERAND (stmt, 1)) == ASSERT_EXPR)
3919 tree rhs = TREE_OPERAND (stmt, 1), var;
3920 tree cond = fold (ASSERT_EXPR_COND (rhs));
3921 use_operand_p use_p;
3922 imm_use_iterator iter;
3924 gcc_assert (cond != boolean_false_node);
3926 /* Propagate the RHS into every use of the LHS. */
3927 var = ASSERT_EXPR_VAR (rhs);
3928 FOR_EACH_IMM_USE_STMT (use_stmt, iter, TREE_OPERAND (stmt, 0))
3929 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3931 SET_USE (use_p, var);
3932 gcc_assert (TREE_CODE (var) == SSA_NAME);
3935 /* And finally, remove the copy, it is not needed. */
3936 bsi_remove (&si, true);
3938 else
3939 bsi_next (&si);
3942 sbitmap_free (blocks_visited);
3946 /* Return true if STMT is interesting for VRP. */
3948 static bool
3949 stmt_interesting_for_vrp (tree stmt)
3951 if (TREE_CODE (stmt) == PHI_NODE
3952 && is_gimple_reg (PHI_RESULT (stmt))
3953 && (INTEGRAL_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))
3954 || POINTER_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))))
3955 return true;
3956 else if (TREE_CODE (stmt) == MODIFY_EXPR)
3958 tree lhs = TREE_OPERAND (stmt, 0);
3959 tree rhs = TREE_OPERAND (stmt, 1);
3961 /* In general, assignments with virtual operands are not useful
3962 for deriving ranges, with the obvious exception of calls to
3963 builtin functions. */
3964 if (TREE_CODE (lhs) == SSA_NAME
3965 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
3966 || POINTER_TYPE_P (TREE_TYPE (lhs)))
3967 && ((TREE_CODE (rhs) == CALL_EXPR
3968 && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
3969 && DECL_P (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
3970 && DECL_IS_BUILTIN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
3971 || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS)))
3972 return true;
3974 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
3975 return true;
3977 return false;
3981 /* Initialize local data structures for VRP. */
3983 static void
3984 vrp_initialize (void)
3986 basic_block bb;
3988 vr_value = XNEWVEC (value_range_t *, num_ssa_names);
3989 memset (vr_value, 0, num_ssa_names * sizeof (value_range_t *));
3991 FOR_EACH_BB (bb)
3993 block_stmt_iterator si;
3994 tree phi;
3996 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3998 if (!stmt_interesting_for_vrp (phi))
4000 tree lhs = PHI_RESULT (phi);
4001 set_value_range_to_varying (get_value_range (lhs));
4002 DONT_SIMULATE_AGAIN (phi) = true;
4004 else
4005 DONT_SIMULATE_AGAIN (phi) = false;
4008 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
4010 tree stmt = bsi_stmt (si);
4012 if (!stmt_interesting_for_vrp (stmt))
4014 ssa_op_iter i;
4015 tree def;
4016 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
4017 set_value_range_to_varying (get_value_range (def));
4018 DONT_SIMULATE_AGAIN (stmt) = true;
4020 else
4022 DONT_SIMULATE_AGAIN (stmt) = false;
4029 /* Visit assignment STMT. If it produces an interesting range, record
4030 the SSA name in *OUTPUT_P. */
4032 static enum ssa_prop_result
4033 vrp_visit_assignment (tree stmt, tree *output_p)
4035 tree lhs, rhs, def;
4036 ssa_op_iter iter;
4038 lhs = TREE_OPERAND (stmt, 0);
4039 rhs = TREE_OPERAND (stmt, 1);
4041 /* We only keep track of ranges in integral and pointer types. */
4042 if (TREE_CODE (lhs) == SSA_NAME
4043 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4044 /* It is valid to have NULL MIN/MAX values on a type. See
4045 build_range_type. */
4046 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
4047 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
4048 || POINTER_TYPE_P (TREE_TYPE (lhs))))
4050 struct loop *l;
4051 value_range_t new_vr = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
4053 extract_range_from_expr (&new_vr, rhs);
4055 /* If STMT is inside a loop, we may be able to know something
4056 else about the range of LHS by examining scalar evolution
4057 information. */
4058 if (current_loops && (l = loop_containing_stmt (stmt)))
4059 adjust_range_with_scev (&new_vr, l, stmt, lhs);
4061 if (update_value_range (lhs, &new_vr))
4063 *output_p = lhs;
4065 if (dump_file && (dump_flags & TDF_DETAILS))
4067 fprintf (dump_file, "Found new range for ");
4068 print_generic_expr (dump_file, lhs, 0);
4069 fprintf (dump_file, ": ");
4070 dump_value_range (dump_file, &new_vr);
4071 fprintf (dump_file, "\n\n");
4074 if (new_vr.type == VR_VARYING)
4075 return SSA_PROP_VARYING;
4077 return SSA_PROP_INTERESTING;
4080 return SSA_PROP_NOT_INTERESTING;
4083 /* Every other statement produces no useful ranges. */
4084 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
4085 set_value_range_to_varying (get_value_range (def));
4087 return SSA_PROP_VARYING;
4091 /* Compare all the value ranges for names equivalent to VAR with VAL
4092 using comparison code COMP. Return the same value returned by
4093 compare_range_with_value, including the setting of
4094 *STRICT_OVERFLOW_P. */
4096 static tree
4097 compare_name_with_value (enum tree_code comp, tree var, tree val,
4098 bool *strict_overflow_p)
4100 bitmap_iterator bi;
4101 unsigned i;
4102 bitmap e;
4103 tree retval, t;
4104 int used_strict_overflow;
4106 t = retval = NULL_TREE;
4108 /* Get the set of equivalences for VAR. */
4109 e = get_value_range (var)->equiv;
4111 /* Add VAR to its own set of equivalences so that VAR's value range
4112 is processed by this loop (otherwise, we would have to replicate
4113 the body of the loop just to check VAR's value range). */
4114 bitmap_set_bit (e, SSA_NAME_VERSION (var));
4116 /* Start at -1. Set it to 0 if we do a comparison without relying
4117 on overflow, or 1 if all comparisons rely on overflow. */
4118 used_strict_overflow = -1;
4120 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
4122 bool sop;
4124 value_range_t equiv_vr = *(vr_value[i]);
4126 /* If name N_i does not have a valid range, use N_i as its own
4127 range. This allows us to compare against names that may
4128 have N_i in their ranges. */
4129 if (equiv_vr.type == VR_VARYING || equiv_vr.type == VR_UNDEFINED)
4131 equiv_vr.type = VR_RANGE;
4132 equiv_vr.min = ssa_name (i);
4133 equiv_vr.max = ssa_name (i);
4136 sop = false;
4137 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
4138 if (t)
4140 /* If we get different answers from different members
4141 of the equivalence set this check must be in a dead
4142 code region. Folding it to a trap representation
4143 would be correct here. For now just return don't-know. */
4144 if (retval != NULL
4145 && t != retval)
4147 retval = NULL_TREE;
4148 break;
4150 retval = t;
4152 if (!sop)
4153 used_strict_overflow = 0;
4154 else if (used_strict_overflow < 0)
4155 used_strict_overflow = 1;
4159 /* Remove VAR from its own equivalence set. */
4160 bitmap_clear_bit (e, SSA_NAME_VERSION (var));
4162 if (retval)
4164 if (used_strict_overflow > 0)
4165 *strict_overflow_p = true;
4166 return retval;
4169 /* We couldn't find a non-NULL value for the predicate. */
4170 return NULL_TREE;
4174 /* Given a comparison code COMP and names N1 and N2, compare all the
4175 ranges equivalent to N1 against all the ranges equivalent to N2
4176 to determine the value of N1 COMP N2. Return the same value
4177 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
4178 whether we relied on an overflow infinity in the comparison. */
4181 static tree
4182 compare_names (enum tree_code comp, tree n1, tree n2,
4183 bool *strict_overflow_p)
4185 tree t, retval;
4186 bitmap e1, e2;
4187 bitmap_iterator bi1, bi2;
4188 unsigned i1, i2;
4189 int used_strict_overflow;
4191 /* Compare the ranges of every name equivalent to N1 against the
4192 ranges of every name equivalent to N2. */
4193 e1 = get_value_range (n1)->equiv;
4194 e2 = get_value_range (n2)->equiv;
4196 /* Add N1 and N2 to their own set of equivalences to avoid
4197 duplicating the body of the loop just to check N1 and N2
4198 ranges. */
4199 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
4200 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
4202 /* If the equivalence sets have a common intersection, then the two
4203 names can be compared without checking their ranges. */
4204 if (bitmap_intersect_p (e1, e2))
4206 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4207 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4209 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
4210 ? boolean_true_node
4211 : boolean_false_node;
4214 /* Start at -1. Set it to 0 if we do a comparison without relying
4215 on overflow, or 1 if all comparisons rely on overflow. */
4216 used_strict_overflow = -1;
4218 /* Otherwise, compare all the equivalent ranges. First, add N1 and
4219 N2 to their own set of equivalences to avoid duplicating the body
4220 of the loop just to check N1 and N2 ranges. */
4221 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
4223 value_range_t vr1 = *(vr_value[i1]);
4225 /* If the range is VARYING or UNDEFINED, use the name itself. */
4226 if (vr1.type == VR_VARYING || vr1.type == VR_UNDEFINED)
4228 vr1.type = VR_RANGE;
4229 vr1.min = ssa_name (i1);
4230 vr1.max = ssa_name (i1);
4233 t = retval = NULL_TREE;
4234 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
4236 bool sop = false;
4238 value_range_t vr2 = *(vr_value[i2]);
4240 if (vr2.type == VR_VARYING || vr2.type == VR_UNDEFINED)
4242 vr2.type = VR_RANGE;
4243 vr2.min = ssa_name (i2);
4244 vr2.max = ssa_name (i2);
4247 t = compare_ranges (comp, &vr1, &vr2, &sop);
4248 if (t)
4250 /* If we get different answers from different members
4251 of the equivalence set this check must be in a dead
4252 code region. Folding it to a trap representation
4253 would be correct here. For now just return don't-know. */
4254 if (retval != NULL
4255 && t != retval)
4257 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4258 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4259 return NULL_TREE;
4261 retval = t;
4263 if (!sop)
4264 used_strict_overflow = 0;
4265 else if (used_strict_overflow < 0)
4266 used_strict_overflow = 1;
4270 if (retval)
4272 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4273 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4274 if (used_strict_overflow > 0)
4275 *strict_overflow_p = true;
4276 return retval;
4280 /* None of the equivalent ranges are useful in computing this
4281 comparison. */
4282 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4283 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4284 return NULL_TREE;
4288 /* Given a conditional predicate COND, try to determine if COND yields
4289 true or false based on the value ranges of its operands. Return
4290 BOOLEAN_TRUE_NODE if the conditional always evaluates to true,
4291 BOOLEAN_FALSE_NODE if the conditional always evaluates to false, and,
4292 NULL if the conditional cannot be evaluated at compile time.
4294 If USE_EQUIV_P is true, the ranges of all the names equivalent with
4295 the operands in COND are used when trying to compute its value.
4296 This is only used during final substitution. During propagation,
4297 we only check the range of each variable and not its equivalents.
4299 Set *STRICT_OVERFLOW_P to indicate whether we relied on an overflow
4300 infinity to produce the result. */
4302 static tree
4303 vrp_evaluate_conditional_warnv (tree cond, bool use_equiv_p,
4304 bool *strict_overflow_p)
4306 gcc_assert (TREE_CODE (cond) == SSA_NAME
4307 || TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison);
4309 if (TREE_CODE (cond) == SSA_NAME)
4311 value_range_t *vr;
4312 tree retval;
4314 if (use_equiv_p)
4315 retval = compare_name_with_value (NE_EXPR, cond, boolean_false_node,
4316 strict_overflow_p);
4317 else
4319 value_range_t *vr = get_value_range (cond);
4320 retval = compare_range_with_value (NE_EXPR, vr, boolean_false_node,
4321 strict_overflow_p);
4324 /* If COND has a known boolean range, return it. */
4325 if (retval)
4326 return retval;
4328 /* Otherwise, if COND has a symbolic range of exactly one value,
4329 return it. */
4330 vr = get_value_range (cond);
4331 if (vr->type == VR_RANGE && vr->min == vr->max)
4332 return vr->min;
4334 else
4336 tree op0 = TREE_OPERAND (cond, 0);
4337 tree op1 = TREE_OPERAND (cond, 1);
4339 /* We only deal with integral and pointer types. */
4340 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
4341 && !POINTER_TYPE_P (TREE_TYPE (op0)))
4342 return NULL_TREE;
4344 if (use_equiv_p)
4346 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
4347 return compare_names (TREE_CODE (cond), op0, op1,
4348 strict_overflow_p);
4349 else if (TREE_CODE (op0) == SSA_NAME)
4350 return compare_name_with_value (TREE_CODE (cond), op0, op1,
4351 strict_overflow_p);
4352 else if (TREE_CODE (op1) == SSA_NAME)
4353 return (compare_name_with_value
4354 (swap_tree_comparison (TREE_CODE (cond)), op1, op0,
4355 strict_overflow_p));
4357 else
4359 value_range_t *vr0, *vr1;
4361 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
4362 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
4364 if (vr0 && vr1)
4365 return compare_ranges (TREE_CODE (cond), vr0, vr1,
4366 strict_overflow_p);
4367 else if (vr0 && vr1 == NULL)
4368 return compare_range_with_value (TREE_CODE (cond), vr0, op1,
4369 strict_overflow_p);
4370 else if (vr0 == NULL && vr1)
4371 return (compare_range_with_value
4372 (swap_tree_comparison (TREE_CODE (cond)), vr1, op0,
4373 strict_overflow_p));
4377 /* Anything else cannot be computed statically. */
4378 return NULL_TREE;
4381 /* Given COND within STMT, try to simplify it based on value range
4382 information. Return NULL if the conditional can not be evaluated.
4383 The ranges of all the names equivalent with the operands in COND
4384 will be used when trying to compute the value. If the result is
4385 based on undefined signed overflow, issue a warning if
4386 appropriate. */
4388 tree
4389 vrp_evaluate_conditional (tree cond, tree stmt)
4391 bool sop;
4392 tree ret;
4394 sop = false;
4395 ret = vrp_evaluate_conditional_warnv (cond, true, &sop);
4397 if (ret && sop)
4399 enum warn_strict_overflow_code wc;
4400 const char* warnmsg;
4402 if (is_gimple_min_invariant (ret))
4404 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
4405 warnmsg = G_("assuming signed overflow does not occur when "
4406 "simplifying conditional to constant");
4408 else
4410 wc = WARN_STRICT_OVERFLOW_COMPARISON;
4411 warnmsg = G_("assuming signed overflow does not occur when "
4412 "simplifying conditional");
4415 if (issue_strict_overflow_warning (wc))
4417 location_t locus;
4419 if (!EXPR_HAS_LOCATION (stmt))
4420 locus = input_location;
4421 else
4422 locus = EXPR_LOCATION (stmt);
4423 warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
4427 return ret;
4431 /* Visit conditional statement STMT. If we can determine which edge
4432 will be taken out of STMT's basic block, record it in
4433 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
4434 SSA_PROP_VARYING. */
4436 static enum ssa_prop_result
4437 vrp_visit_cond_stmt (tree stmt, edge *taken_edge_p)
4439 tree cond, val;
4440 bool sop;
4442 *taken_edge_p = NULL;
4444 /* FIXME. Handle SWITCH_EXPRs. But first, the assert pass needs to
4445 add ASSERT_EXPRs for them. */
4446 if (TREE_CODE (stmt) == SWITCH_EXPR)
4447 return SSA_PROP_VARYING;
4449 cond = COND_EXPR_COND (stmt);
4451 if (dump_file && (dump_flags & TDF_DETAILS))
4453 tree use;
4454 ssa_op_iter i;
4456 fprintf (dump_file, "\nVisiting conditional with predicate: ");
4457 print_generic_expr (dump_file, cond, 0);
4458 fprintf (dump_file, "\nWith known ranges\n");
4460 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
4462 fprintf (dump_file, "\t");
4463 print_generic_expr (dump_file, use, 0);
4464 fprintf (dump_file, ": ");
4465 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
4468 fprintf (dump_file, "\n");
4471 /* Compute the value of the predicate COND by checking the known
4472 ranges of each of its operands.
4474 Note that we cannot evaluate all the equivalent ranges here
4475 because those ranges may not yet be final and with the current
4476 propagation strategy, we cannot determine when the value ranges
4477 of the names in the equivalence set have changed.
4479 For instance, given the following code fragment
4481 i_5 = PHI <8, i_13>
4483 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
4484 if (i_14 == 1)
4487 Assume that on the first visit to i_14, i_5 has the temporary
4488 range [8, 8] because the second argument to the PHI function is
4489 not yet executable. We derive the range ~[0, 0] for i_14 and the
4490 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
4491 the first time, since i_14 is equivalent to the range [8, 8], we
4492 determine that the predicate is always false.
4494 On the next round of propagation, i_13 is determined to be
4495 VARYING, which causes i_5 to drop down to VARYING. So, another
4496 visit to i_14 is scheduled. In this second visit, we compute the
4497 exact same range and equivalence set for i_14, namely ~[0, 0] and
4498 { i_5 }. But we did not have the previous range for i_5
4499 registered, so vrp_visit_assignment thinks that the range for
4500 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
4501 is not visited again, which stops propagation from visiting
4502 statements in the THEN clause of that if().
4504 To properly fix this we would need to keep the previous range
4505 value for the names in the equivalence set. This way we would've
4506 discovered that from one visit to the other i_5 changed from
4507 range [8, 8] to VR_VARYING.
4509 However, fixing this apparent limitation may not be worth the
4510 additional checking. Testing on several code bases (GCC, DLV,
4511 MICO, TRAMP3D and SPEC2000) showed that doing this results in
4512 4 more predicates folded in SPEC. */
4513 sop = false;
4514 val = vrp_evaluate_conditional_warnv (cond, false, &sop);
4515 if (val)
4517 if (!sop)
4518 *taken_edge_p = find_taken_edge (bb_for_stmt (stmt), val);
4519 else
4521 if (dump_file && (dump_flags & TDF_DETAILS))
4522 fprintf (dump_file,
4523 "\nIgnoring predicate evaluation because "
4524 "it assumes that signed overflow is undefined");
4525 val = NULL_TREE;
4529 if (dump_file && (dump_flags & TDF_DETAILS))
4531 fprintf (dump_file, "\nPredicate evaluates to: ");
4532 if (val == NULL_TREE)
4533 fprintf (dump_file, "DON'T KNOW\n");
4534 else
4535 print_generic_stmt (dump_file, val, 0);
4538 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
4542 /* Evaluate statement STMT. If the statement produces a useful range,
4543 return SSA_PROP_INTERESTING and record the SSA name with the
4544 interesting range into *OUTPUT_P.
4546 If STMT is a conditional branch and we can determine its truth
4547 value, the taken edge is recorded in *TAKEN_EDGE_P.
4549 If STMT produces a varying value, return SSA_PROP_VARYING. */
4551 static enum ssa_prop_result
4552 vrp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
4554 tree def;
4555 ssa_op_iter iter;
4556 stmt_ann_t ann;
4558 if (dump_file && (dump_flags & TDF_DETAILS))
4560 fprintf (dump_file, "\nVisiting statement:\n");
4561 print_generic_stmt (dump_file, stmt, dump_flags);
4562 fprintf (dump_file, "\n");
4565 ann = stmt_ann (stmt);
4566 if (TREE_CODE (stmt) == MODIFY_EXPR)
4568 tree rhs = TREE_OPERAND (stmt, 1);
4570 /* In general, assignments with virtual operands are not useful
4571 for deriving ranges, with the obvious exception of calls to
4572 builtin functions. */
4573 if ((TREE_CODE (rhs) == CALL_EXPR
4574 && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
4575 && DECL_P (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
4576 && DECL_IS_BUILTIN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
4577 || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
4578 return vrp_visit_assignment (stmt, output_p);
4580 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
4581 return vrp_visit_cond_stmt (stmt, taken_edge_p);
4583 /* All other statements produce nothing of interest for VRP, so mark
4584 their outputs varying and prevent further simulation. */
4585 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
4586 set_value_range_to_varying (get_value_range (def));
4588 return SSA_PROP_VARYING;
4592 /* Meet operation for value ranges. Given two value ranges VR0 and
4593 VR1, store in VR0 the result of meeting VR0 and VR1.
4595 The meeting rules are as follows:
4597 1- If VR0 and VR1 have an empty intersection, set VR0 to VR_VARYING.
4599 2- If VR0 and VR1 have a non-empty intersection, set VR0 to the
4600 union of VR0 and VR1. */
4602 static void
4603 vrp_meet (value_range_t *vr0, value_range_t *vr1)
4605 if (vr0->type == VR_UNDEFINED)
4607 copy_value_range (vr0, vr1);
4608 return;
4611 if (vr1->type == VR_UNDEFINED)
4613 /* Nothing to do. VR0 already has the resulting range. */
4614 return;
4617 if (vr0->type == VR_VARYING)
4619 /* Nothing to do. VR0 already has the resulting range. */
4620 return;
4623 if (vr1->type == VR_VARYING)
4625 set_value_range_to_varying (vr0);
4626 return;
4629 if (vr0->type == VR_RANGE && vr1->type == VR_RANGE)
4631 /* If VR0 and VR1 have a non-empty intersection, compute the
4632 union of both ranges. */
4633 if (value_ranges_intersect_p (vr0, vr1))
4635 int cmp;
4636 tree min, max;
4638 /* The lower limit of the new range is the minimum of the
4639 two ranges. If they cannot be compared, the result is
4640 VARYING. */
4641 cmp = compare_values (vr0->min, vr1->min);
4642 if (cmp == 0 || cmp == 1)
4643 min = vr1->min;
4644 else if (cmp == -1)
4645 min = vr0->min;
4646 else
4648 set_value_range_to_varying (vr0);
4649 return;
4652 /* Similarly, the upper limit of the new range is the
4653 maximum of the two ranges. If they cannot be compared,
4654 the result is VARYING. */
4655 cmp = compare_values (vr0->max, vr1->max);
4656 if (cmp == 0 || cmp == -1)
4657 max = vr1->max;
4658 else if (cmp == 1)
4659 max = vr0->max;
4660 else
4662 set_value_range_to_varying (vr0);
4663 return;
4666 /* Check for useless ranges. */
4667 if (INTEGRAL_TYPE_P (TREE_TYPE (min))
4668 && ((vrp_val_is_min (min) || is_overflow_infinity (min))
4669 && (vrp_val_is_max (max) || is_overflow_infinity (max))))
4671 set_value_range_to_varying (vr0);
4672 return;
4675 /* The resulting set of equivalences is the intersection of
4676 the two sets. */
4677 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4678 bitmap_and_into (vr0->equiv, vr1->equiv);
4679 else if (vr0->equiv && !vr1->equiv)
4680 bitmap_clear (vr0->equiv);
4682 set_value_range (vr0, vr0->type, min, max, vr0->equiv);
4684 else
4685 goto no_meet;
4687 else if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4689 /* Two anti-ranges meet only if they are both identical. */
4690 if (compare_values (vr0->min, vr1->min) == 0
4691 && compare_values (vr0->max, vr1->max) == 0
4692 && compare_values (vr0->min, vr0->max) == 0)
4694 /* The resulting set of equivalences is the intersection of
4695 the two sets. */
4696 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4697 bitmap_and_into (vr0->equiv, vr1->equiv);
4698 else if (vr0->equiv && !vr1->equiv)
4699 bitmap_clear (vr0->equiv);
4701 else
4702 goto no_meet;
4704 else if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4706 /* A numeric range [VAL1, VAL2] and an anti-range ~[VAL3, VAL4]
4707 meet only if the ranges have an empty intersection. The
4708 result of the meet operation is the anti-range. */
4709 if (!symbolic_range_p (vr0)
4710 && !symbolic_range_p (vr1)
4711 && !value_ranges_intersect_p (vr0, vr1))
4713 /* Copy most of VR1 into VR0. Don't copy VR1's equivalence
4714 set. We need to compute the intersection of the two
4715 equivalence sets. */
4716 if (vr1->type == VR_ANTI_RANGE)
4717 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr0->equiv);
4719 /* The resulting set of equivalences is the intersection of
4720 the two sets. */
4721 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4722 bitmap_and_into (vr0->equiv, vr1->equiv);
4723 else if (vr0->equiv && !vr1->equiv)
4724 bitmap_clear (vr0->equiv);
4726 else
4727 goto no_meet;
4729 else
4730 gcc_unreachable ();
4732 return;
4734 no_meet:
4735 /* The two range VR0 and VR1 do not meet. Before giving up and
4736 setting the result to VARYING, see if we can at least derive a
4737 useful anti-range. FIXME, all this nonsense about distinguishing
4738 anti-ranges from ranges is necessary because of the odd
4739 semantics of range_includes_zero_p and friends. */
4740 if (!symbolic_range_p (vr0)
4741 && ((vr0->type == VR_RANGE && !range_includes_zero_p (vr0))
4742 || (vr0->type == VR_ANTI_RANGE && range_includes_zero_p (vr0)))
4743 && !symbolic_range_p (vr1)
4744 && ((vr1->type == VR_RANGE && !range_includes_zero_p (vr1))
4745 || (vr1->type == VR_ANTI_RANGE && range_includes_zero_p (vr1))))
4747 set_value_range_to_nonnull (vr0, TREE_TYPE (vr0->min));
4749 /* Since this meet operation did not result from the meeting of
4750 two equivalent names, VR0 cannot have any equivalences. */
4751 if (vr0->equiv)
4752 bitmap_clear (vr0->equiv);
4754 else
4755 set_value_range_to_varying (vr0);
4759 /* Visit all arguments for PHI node PHI that flow through executable
4760 edges. If a valid value range can be derived from all the incoming
4761 value ranges, set a new range for the LHS of PHI. */
4763 static enum ssa_prop_result
4764 vrp_visit_phi_node (tree phi)
4766 int i;
4767 tree lhs = PHI_RESULT (phi);
4768 value_range_t *lhs_vr = get_value_range (lhs);
4769 value_range_t vr_result = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
4771 copy_value_range (&vr_result, lhs_vr);
4773 if (dump_file && (dump_flags & TDF_DETAILS))
4775 fprintf (dump_file, "\nVisiting PHI node: ");
4776 print_generic_expr (dump_file, phi, dump_flags);
4779 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
4781 edge e = PHI_ARG_EDGE (phi, i);
4783 if (dump_file && (dump_flags & TDF_DETAILS))
4785 fprintf (dump_file,
4786 "\n Argument #%d (%d -> %d %sexecutable)\n",
4787 i, e->src->index, e->dest->index,
4788 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
4791 if (e->flags & EDGE_EXECUTABLE)
4793 tree arg = PHI_ARG_DEF (phi, i);
4794 value_range_t vr_arg;
4796 if (TREE_CODE (arg) == SSA_NAME)
4797 vr_arg = *(get_value_range (arg));
4798 else
4800 if (is_overflow_infinity (arg))
4802 arg = copy_node (arg);
4803 TREE_OVERFLOW (arg) = 0;
4806 vr_arg.type = VR_RANGE;
4807 vr_arg.min = arg;
4808 vr_arg.max = arg;
4809 vr_arg.equiv = NULL;
4812 if (dump_file && (dump_flags & TDF_DETAILS))
4814 fprintf (dump_file, "\t");
4815 print_generic_expr (dump_file, arg, dump_flags);
4816 fprintf (dump_file, "\n\tValue: ");
4817 dump_value_range (dump_file, &vr_arg);
4818 fprintf (dump_file, "\n");
4821 vrp_meet (&vr_result, &vr_arg);
4823 if (vr_result.type == VR_VARYING)
4824 break;
4828 if (vr_result.type == VR_VARYING)
4829 goto varying;
4831 /* To prevent infinite iterations in the algorithm, derive ranges
4832 when the new value is slightly bigger or smaller than the
4833 previous one. */
4834 if (lhs_vr->type == VR_RANGE && vr_result.type == VR_RANGE)
4836 if (!POINTER_TYPE_P (TREE_TYPE (lhs)))
4838 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
4839 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
4841 /* If the new minimum is smaller or larger than the previous
4842 one, go all the way to -INF. In the first case, to avoid
4843 iterating millions of times to reach -INF, and in the
4844 other case to avoid infinite bouncing between different
4845 minimums. */
4846 if (cmp_min > 0 || cmp_min < 0)
4848 /* If we will end up with a (-INF, +INF) range, set it
4849 to VARYING. */
4850 if (vrp_val_is_max (vr_result.max))
4851 goto varying;
4853 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
4854 || !vrp_var_may_overflow (lhs, phi))
4855 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
4856 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
4857 vr_result.min =
4858 negative_overflow_infinity (TREE_TYPE (vr_result.min));
4859 else
4860 goto varying;
4863 /* Similarly, if the new maximum is smaller or larger than
4864 the previous one, go all the way to +INF. */
4865 if (cmp_max < 0 || cmp_max > 0)
4867 /* If we will end up with a (-INF, +INF) range, set it
4868 to VARYING. */
4869 if (vrp_val_is_min (vr_result.min))
4870 goto varying;
4872 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
4873 || !vrp_var_may_overflow (lhs, phi))
4874 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
4875 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
4876 vr_result.max =
4877 positive_overflow_infinity (TREE_TYPE (vr_result.max));
4878 else
4879 goto varying;
4884 /* If the new range is different than the previous value, keep
4885 iterating. */
4886 if (update_value_range (lhs, &vr_result))
4887 return SSA_PROP_INTERESTING;
4889 /* Nothing changed, don't add outgoing edges. */
4890 return SSA_PROP_NOT_INTERESTING;
4892 /* No match found. Set the LHS to VARYING. */
4893 varying:
4894 set_value_range_to_varying (lhs_vr);
4895 return SSA_PROP_VARYING;
4898 /* Simplify a division or modulo operator to a right shift or
4899 bitwise and if the first operand is unsigned or is greater
4900 than zero and the second operand is an exact power of two. */
4902 static void
4903 simplify_div_or_mod_using_ranges (tree stmt, tree rhs, enum tree_code rhs_code)
4905 tree val = NULL;
4906 tree op = TREE_OPERAND (rhs, 0);
4907 value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
4909 if (TYPE_UNSIGNED (TREE_TYPE (op)))
4911 val = integer_one_node;
4913 else
4915 bool sop = false;
4917 val = compare_range_with_value (GT_EXPR, vr, integer_zero_node, &sop);
4919 if (val
4920 && sop
4921 && integer_onep (val)
4922 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
4924 location_t locus;
4926 if (!EXPR_HAS_LOCATION (stmt))
4927 locus = input_location;
4928 else
4929 locus = EXPR_LOCATION (stmt);
4930 warning (OPT_Wstrict_overflow,
4931 ("%Hassuming signed overflow does not occur when "
4932 "simplifying / or %% to >> or &"),
4933 &locus);
4937 if (val && integer_onep (val))
4939 tree t;
4940 tree op0 = TREE_OPERAND (rhs, 0);
4941 tree op1 = TREE_OPERAND (rhs, 1);
4943 if (rhs_code == TRUNC_DIV_EXPR)
4945 t = build_int_cst (NULL_TREE, tree_log2 (op1));
4946 t = build2 (RSHIFT_EXPR, TREE_TYPE (op0), op0, t);
4948 else
4950 t = build_int_cst (TREE_TYPE (op1), 1);
4951 t = int_const_binop (MINUS_EXPR, op1, t, 0);
4952 t = fold_convert (TREE_TYPE (op0), t);
4953 t = build2 (BIT_AND_EXPR, TREE_TYPE (op0), op0, t);
4956 TREE_OPERAND (stmt, 1) = t;
4957 update_stmt (stmt);
4961 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
4962 ABS_EXPR. If the operand is <= 0, then simplify the
4963 ABS_EXPR into a NEGATE_EXPR. */
4965 static void
4966 simplify_abs_using_ranges (tree stmt, tree rhs)
4968 tree val = NULL;
4969 tree op = TREE_OPERAND (rhs, 0);
4970 tree type = TREE_TYPE (op);
4971 value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
4973 if (TYPE_UNSIGNED (type))
4975 val = integer_zero_node;
4977 else if (vr)
4979 bool sop = false;
4981 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
4982 if (!val)
4984 sop = false;
4985 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
4986 &sop);
4988 if (val)
4990 if (integer_zerop (val))
4991 val = integer_one_node;
4992 else if (integer_onep (val))
4993 val = integer_zero_node;
4997 if (val
4998 && (integer_onep (val) || integer_zerop (val)))
5000 tree t;
5002 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
5004 location_t locus;
5006 if (!EXPR_HAS_LOCATION (stmt))
5007 locus = input_location;
5008 else
5009 locus = EXPR_LOCATION (stmt);
5010 warning (OPT_Wstrict_overflow,
5011 ("%Hassuming signed overflow does not occur when "
5012 "simplifying abs (X) to X or -X"),
5013 &locus);
5016 if (integer_onep (val))
5017 t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
5018 else
5019 t = op;
5021 TREE_OPERAND (stmt, 1) = t;
5022 update_stmt (stmt);
5027 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
5028 a known value range VR.
5030 If there is one and only one value which will satisfy the
5031 conditional, then return that value. Else return NULL. */
5033 static tree
5034 test_for_singularity (enum tree_code cond_code, tree op0,
5035 tree op1, value_range_t *vr)
5037 tree min = NULL;
5038 tree max = NULL;
5040 /* Extract minimum/maximum values which satisfy the
5041 the conditional as it was written. */
5042 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
5044 /* This should not be negative infinity; there is no overflow
5045 here. */
5046 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
5048 max = op1;
5049 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
5051 tree one = build_int_cst (TREE_TYPE (op0), 1);
5052 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
5053 if (EXPR_P (max))
5054 TREE_NO_WARNING (max) = 1;
5057 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
5059 /* This should not be positive infinity; there is no overflow
5060 here. */
5061 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
5063 min = op1;
5064 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
5066 tree one = build_int_cst (TREE_TYPE (op0), 1);
5067 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
5068 if (EXPR_P (min))
5069 TREE_NO_WARNING (min) = 1;
5073 /* Now refine the minimum and maximum values using any
5074 value range information we have for op0. */
5075 if (min && max)
5077 if (compare_values (vr->min, min) == -1)
5078 min = min;
5079 else
5080 min = vr->min;
5081 if (compare_values (vr->max, max) == 1)
5082 max = max;
5083 else
5084 max = vr->max;
5086 /* If the new min/max values have converged to a single value,
5087 then there is only one value which can satisfy the condition,
5088 return that value. */
5089 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
5090 return min;
5092 return NULL;
5095 /* Simplify a conditional using a relational operator to an equality
5096 test if the range information indicates only one value can satisfy
5097 the original conditional. */
5099 static void
5100 simplify_cond_using_ranges (tree stmt)
5102 tree cond = COND_EXPR_COND (stmt);
5103 tree op0 = TREE_OPERAND (cond, 0);
5104 tree op1 = TREE_OPERAND (cond, 1);
5105 enum tree_code cond_code = TREE_CODE (cond);
5107 if (cond_code != NE_EXPR
5108 && cond_code != EQ_EXPR
5109 && TREE_CODE (op0) == SSA_NAME
5110 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
5111 && is_gimple_min_invariant (op1))
5113 value_range_t *vr = get_value_range (op0);
5115 /* If we have range information for OP0, then we might be
5116 able to simplify this conditional. */
5117 if (vr->type == VR_RANGE)
5119 tree new = test_for_singularity (cond_code, op0, op1, vr);
5121 if (new)
5123 if (dump_file)
5125 fprintf (dump_file, "Simplified relational ");
5126 print_generic_expr (dump_file, cond, 0);
5127 fprintf (dump_file, " into ");
5130 COND_EXPR_COND (stmt)
5131 = build2 (EQ_EXPR, boolean_type_node, op0, new);
5132 update_stmt (stmt);
5134 if (dump_file)
5136 print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
5137 fprintf (dump_file, "\n");
5139 return;
5143 /* Try again after inverting the condition. We only deal
5144 with integral types here, so no need to worry about
5145 issues with inverting FP comparisons. */
5146 cond_code = invert_tree_comparison (cond_code, false);
5147 new = test_for_singularity (cond_code, op0, op1, vr);
5149 if (new)
5151 if (dump_file)
5153 fprintf (dump_file, "Simplified relational ");
5154 print_generic_expr (dump_file, cond, 0);
5155 fprintf (dump_file, " into ");
5158 COND_EXPR_COND (stmt)
5159 = build2 (NE_EXPR, boolean_type_node, op0, new);
5160 update_stmt (stmt);
5162 if (dump_file)
5164 print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
5165 fprintf (dump_file, "\n");
5167 return;
5174 /* Simplify STMT using ranges if possible. */
5176 void
5177 simplify_stmt_using_ranges (tree stmt)
5179 if (TREE_CODE (stmt) == MODIFY_EXPR)
5181 tree rhs = TREE_OPERAND (stmt, 1);
5182 enum tree_code rhs_code = TREE_CODE (rhs);
5184 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
5185 and BIT_AND_EXPR respectively if the first operand is greater
5186 than zero and the second operand is an exact power of two. */
5187 if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
5188 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
5189 && integer_pow2p (TREE_OPERAND (rhs, 1)))
5190 simplify_div_or_mod_using_ranges (stmt, rhs, rhs_code);
5192 /* Transform ABS (X) into X or -X as appropriate. */
5193 if (rhs_code == ABS_EXPR
5194 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
5195 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
5196 simplify_abs_using_ranges (stmt, rhs);
5198 else if (TREE_CODE (stmt) == COND_EXPR
5199 && COMPARISON_CLASS_P (COND_EXPR_COND (stmt)))
5201 simplify_cond_using_ranges (stmt);
5205 /* Stack of dest,src equivalency pairs that need to be restored after
5206 each attempt to thread a block's incoming edge to an outgoing edge.
5208 A NULL entry is used to mark the end of pairs which need to be
5209 restored. */
5210 static VEC(tree,heap) *stack;
5212 /* A trivial wrapper so that we can present the generic jump threading
5213 code with a simple API for simplifying statements. STMT is the
5214 statement we want to simplify, WITHIN_STMT provides the location
5215 for any overflow warnings. */
5217 static tree
5218 simplify_stmt_for_jump_threading (tree stmt, tree within_stmt)
5220 /* We only use VRP information to simplify conditionals. This is
5221 overly conservative, but it's unclear if doing more would be
5222 worth the compile time cost. */
5223 if (TREE_CODE (stmt) != COND_EXPR)
5224 return NULL;
5226 return vrp_evaluate_conditional (COND_EXPR_COND (stmt), within_stmt);
5229 /* Blocks which have more than one predecessor and more than
5230 one successor present jump threading opportunities. ie,
5231 when the block is reached from a specific predecessor, we
5232 may be able to determine which of the outgoing edges will
5233 be traversed. When this optimization applies, we are able
5234 to avoid conditionals at runtime and we may expose secondary
5235 optimization opportunities.
5237 This routine is effectively a driver for the generic jump
5238 threading code. It basically just presents the generic code
5239 with edges that may be suitable for jump threading.
5241 Unlike DOM, we do not iterate VRP if jump threading was successful.
5242 While iterating may expose new opportunities for VRP, it is expected
5243 those opportunities would be very limited and the compile time cost
5244 to expose those opportunities would be significant.
5246 As jump threading opportunities are discovered, they are registered
5247 for later realization. */
5249 static void
5250 identify_jump_threads (void)
5252 basic_block bb;
5253 tree dummy;
5255 /* Ugh. When substituting values earlier in this pass we can
5256 wipe the dominance information. So rebuild the dominator
5257 information as we need it within the jump threading code. */
5258 calculate_dominance_info (CDI_DOMINATORS);
5260 /* We do not allow VRP information to be used for jump threading
5261 across a back edge in the CFG. Otherwise it becomes too
5262 difficult to avoid eliminating loop exit tests. Of course
5263 EDGE_DFS_BACK is not accurate at this time so we have to
5264 recompute it. */
5265 mark_dfs_back_edges ();
5267 /* Allocate our unwinder stack to unwind any temporary equivalences
5268 that might be recorded. */
5269 stack = VEC_alloc (tree, heap, 20);
5271 /* To avoid lots of silly node creation, we create a single
5272 conditional and just modify it in-place when attempting to
5273 thread jumps. */
5274 dummy = build2 (EQ_EXPR, boolean_type_node, NULL, NULL);
5275 dummy = build3 (COND_EXPR, void_type_node, dummy, NULL, NULL);
5277 /* Walk through all the blocks finding those which present a
5278 potential jump threading opportunity. We could set this up
5279 as a dominator walker and record data during the walk, but
5280 I doubt it's worth the effort for the classes of jump
5281 threading opportunities we are trying to identify at this
5282 point in compilation. */
5283 FOR_EACH_BB (bb)
5285 tree last, cond;
5287 /* If the generic jump threading code does not find this block
5288 interesting, then there is nothing to do. */
5289 if (! potentially_threadable_block (bb))
5290 continue;
5292 /* We only care about blocks ending in a COND_EXPR. While there
5293 may be some value in handling SWITCH_EXPR here, I doubt it's
5294 terribly important. */
5295 last = bsi_stmt (bsi_last (bb));
5296 if (TREE_CODE (last) != COND_EXPR)
5297 continue;
5299 /* We're basically looking for any kind of conditional with
5300 integral type arguments. */
5301 cond = COND_EXPR_COND (last);
5302 if ((TREE_CODE (cond) == SSA_NAME
5303 && INTEGRAL_TYPE_P (TREE_TYPE (cond)))
5304 || (COMPARISON_CLASS_P (cond)
5305 && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
5306 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 0)))
5307 && (TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME
5308 || is_gimple_min_invariant (TREE_OPERAND (cond, 1)))
5309 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 1)))))
5311 edge_iterator ei;
5312 edge e;
5314 /* We've got a block with multiple predecessors and multiple
5315 successors which also ends in a suitable conditional. For
5316 each predecessor, see if we can thread it to a specific
5317 successor. */
5318 FOR_EACH_EDGE (e, ei, bb->preds)
5320 /* Do not thread across back edges or abnormal edges
5321 in the CFG. */
5322 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
5323 continue;
5325 thread_across_edge (dummy, e, true,
5326 &stack,
5327 simplify_stmt_for_jump_threading);
5332 /* We do not actually update the CFG or SSA graphs at this point as
5333 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
5334 handle ASSERT_EXPRs gracefully. */
5337 /* We identified all the jump threading opportunities earlier, but could
5338 not transform the CFG at that time. This routine transforms the
5339 CFG and arranges for the dominator tree to be rebuilt if necessary.
5341 Note the SSA graph update will occur during the normal TODO
5342 processing by the pass manager. */
5343 static void
5344 finalize_jump_threads (void)
5346 bool cfg_altered = false;
5347 cfg_altered = thread_through_all_blocks ();
5349 /* If we threaded jumps, then we need to recompute the dominance
5350 information, to safely do that we must clean up the CFG first. */
5351 if (cfg_altered)
5353 free_dominance_info (CDI_DOMINATORS);
5354 cleanup_tree_cfg ();
5355 calculate_dominance_info (CDI_DOMINATORS);
5357 VEC_free (tree, heap, stack);
5361 /* Traverse all the blocks folding conditionals with known ranges. */
5363 static void
5364 vrp_finalize (void)
5366 size_t i;
5367 prop_value_t *single_val_range;
5368 bool do_value_subst_p;
5370 if (dump_file)
5372 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
5373 dump_all_value_ranges (dump_file);
5374 fprintf (dump_file, "\n");
5377 /* We may have ended with ranges that have exactly one value. Those
5378 values can be substituted as any other copy/const propagated
5379 value using substitute_and_fold. */
5380 single_val_range = XNEWVEC (prop_value_t, num_ssa_names);
5381 memset (single_val_range, 0, num_ssa_names * sizeof (*single_val_range));
5383 do_value_subst_p = false;
5384 for (i = 0; i < num_ssa_names; i++)
5385 if (vr_value[i]
5386 && vr_value[i]->type == VR_RANGE
5387 && vr_value[i]->min == vr_value[i]->max)
5389 single_val_range[i].value = vr_value[i]->min;
5390 do_value_subst_p = true;
5393 if (!do_value_subst_p)
5395 /* We found no single-valued ranges, don't waste time trying to
5396 do single value substitution in substitute_and_fold. */
5397 free (single_val_range);
5398 single_val_range = NULL;
5401 substitute_and_fold (single_val_range, true);
5403 /* We must identify jump threading opportunities before we release
5404 the datastructures built by VRP. */
5405 identify_jump_threads ();
5407 /* Free allocated memory. */
5408 for (i = 0; i < num_ssa_names; i++)
5409 if (vr_value[i])
5411 BITMAP_FREE (vr_value[i]->equiv);
5412 free (vr_value[i]);
5415 free (single_val_range);
5416 free (vr_value);
5418 /* So that we can distinguish between VRP data being available
5419 and not available. */
5420 vr_value = NULL;
5424 /* Main entry point to VRP (Value Range Propagation). This pass is
5425 loosely based on J. R. C. Patterson, ``Accurate Static Branch
5426 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
5427 Programming Language Design and Implementation, pp. 67-78, 1995.
5428 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
5430 This is essentially an SSA-CCP pass modified to deal with ranges
5431 instead of constants.
5433 While propagating ranges, we may find that two or more SSA name
5434 have equivalent, though distinct ranges. For instance,
5436 1 x_9 = p_3->a;
5437 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
5438 3 if (p_4 == q_2)
5439 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
5440 5 endif
5441 6 if (q_2)
5443 In the code above, pointer p_5 has range [q_2, q_2], but from the
5444 code we can also determine that p_5 cannot be NULL and, if q_2 had
5445 a non-varying range, p_5's range should also be compatible with it.
5447 These equivalences are created by two expressions: ASSERT_EXPR and
5448 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
5449 result of another assertion, then we can use the fact that p_5 and
5450 p_4 are equivalent when evaluating p_5's range.
5452 Together with value ranges, we also propagate these equivalences
5453 between names so that we can take advantage of information from
5454 multiple ranges when doing final replacement. Note that this
5455 equivalency relation is transitive but not symmetric.
5457 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
5458 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
5459 in contexts where that assertion does not hold (e.g., in line 6).
5461 TODO, the main difference between this pass and Patterson's is that
5462 we do not propagate edge probabilities. We only compute whether
5463 edges can be taken or not. That is, instead of having a spectrum
5464 of jump probabilities between 0 and 1, we only deal with 0, 1 and
5465 DON'T KNOW. In the future, it may be worthwhile to propagate
5466 probabilities to aid branch prediction. */
5468 static unsigned int
5469 execute_vrp (void)
5471 insert_range_assertions ();
5473 current_loops = loop_optimizer_init (LOOPS_NORMAL);
5474 if (current_loops)
5475 scev_initialize (current_loops);
5477 vrp_initialize ();
5478 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
5479 vrp_finalize ();
5481 if (current_loops)
5483 scev_finalize ();
5484 loop_optimizer_finalize (current_loops);
5485 current_loops = NULL;
5488 /* ASSERT_EXPRs must be removed before finalizing jump threads
5489 as finalizing jump threads calls the CFG cleanup code which
5490 does not properly handle ASSERT_EXPRs. */
5491 remove_range_assertions ();
5493 /* If we exposed any new variables, go ahead and put them into
5494 SSA form now, before we handle jump threading. This simplifies
5495 interactions between rewriting of _DECL nodes into SSA form
5496 and rewriting SSA_NAME nodes into SSA form after block
5497 duplication and CFG manipulation. */
5498 update_ssa (TODO_update_ssa);
5500 finalize_jump_threads ();
5501 return 0;
5504 static bool
5505 gate_vrp (void)
5507 return flag_tree_vrp != 0;
5510 struct tree_opt_pass pass_vrp =
5512 "vrp", /* name */
5513 gate_vrp, /* gate */
5514 execute_vrp, /* execute */
5515 NULL, /* sub */
5516 NULL, /* next */
5517 0, /* static_pass_number */
5518 TV_TREE_VRP, /* tv_id */
5519 PROP_ssa | PROP_alias, /* properties_required */
5520 0, /* properties_provided */
5521 PROP_smt_usage, /* properties_destroyed */
5522 0, /* todo_flags_start */
5523 TODO_cleanup_cfg
5524 | TODO_ggc_collect
5525 | TODO_verify_ssa
5526 | TODO_dump_func
5527 | TODO_update_ssa
5528 | TODO_update_smt_usage, /* todo_flags_finish */
5529 0 /* letter */