Mark ChangeLog
[official-gcc.git] / gcc / tree-vrp.c
blobeb9b34852a8e465af141969d271bd1bada1db6c8
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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "flags.h"
27 #include "tree.h"
28 #include "basic-block.h"
29 #include "tree-flow.h"
30 #include "tree-pass.h"
31 #include "tree-dump.h"
32 #include "timevar.h"
33 #include "diagnostic.h"
34 #include "toplev.h"
35 #include "intl.h"
36 #include "cfgloop.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-ssa-propagate.h"
39 #include "tree-chrec.h"
41 /* Set of SSA names found during the dominator traversal of a
42 sub-graph in find_assert_locations. */
43 static sbitmap found_in_subgraph;
45 /* Local functions. */
46 static int compare_values (tree val1, tree val2);
47 static int compare_values_warnv (tree val1, tree val2, bool *);
48 static tree vrp_evaluate_conditional_warnv (tree, bool, bool *);
50 /* Location information for ASSERT_EXPRs. Each instance of this
51 structure describes an ASSERT_EXPR for an SSA name. Since a single
52 SSA name may have more than one assertion associated with it, these
53 locations are kept in a linked list attached to the corresponding
54 SSA name. */
55 struct assert_locus_d
57 /* Basic block where the assertion would be inserted. */
58 basic_block bb;
60 /* Some assertions need to be inserted on an edge (e.g., assertions
61 generated by COND_EXPRs). In those cases, BB will be NULL. */
62 edge e;
64 /* Pointer to the statement that generated this assertion. */
65 block_stmt_iterator si;
67 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
68 enum tree_code comp_code;
70 /* Value being compared against. */
71 tree val;
73 /* Next node in the linked list. */
74 struct assert_locus_d *next;
77 typedef struct assert_locus_d *assert_locus_t;
79 /* If bit I is present, it means that SSA name N_i has a list of
80 assertions that should be inserted in the IL. */
81 static bitmap need_assert_for;
83 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
84 holds a list of ASSERT_LOCUS_T nodes that describe where
85 ASSERT_EXPRs for SSA name N_I should be inserted. */
86 static assert_locus_t *asserts_for;
88 /* Set of blocks visited in find_assert_locations. Used to avoid
89 visiting the same block more than once. */
90 static sbitmap blocks_visited;
92 /* Value range array. After propagation, VR_VALUE[I] holds the range
93 of values that SSA name N_I may take. */
94 static value_range_t **vr_value;
97 /* Return whether TYPE should use an overflow infinity distinct from
98 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
99 represent a signed overflow during VRP computations. An infinity
100 is distinct from a half-range, which will go from some number to
101 TYPE_{MIN,MAX}_VALUE. */
103 static inline bool
104 needs_overflow_infinity (tree type)
106 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
109 /* Return whether TYPE can support our overflow infinity
110 representation: we use the TREE_OVERFLOW flag, which only exists
111 for constants. If TYPE doesn't support this, we don't optimize
112 cases which would require signed overflow--we drop them to
113 VARYING. */
115 static inline bool
116 supports_overflow_infinity (tree type)
118 #ifdef ENABLE_CHECKING
119 gcc_assert (needs_overflow_infinity (type));
120 #endif
121 return (TYPE_MIN_VALUE (type) != NULL_TREE
122 && CONSTANT_CLASS_P (TYPE_MIN_VALUE (type))
123 && TYPE_MAX_VALUE (type) != NULL_TREE
124 && CONSTANT_CLASS_P (TYPE_MAX_VALUE (type)));
127 /* VAL is the maximum or minimum value of a type. Return a
128 corresponding overflow infinity. */
130 static inline tree
131 make_overflow_infinity (tree val)
133 #ifdef ENABLE_CHECKING
134 gcc_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
135 #endif
136 val = copy_node (val);
137 TREE_OVERFLOW (val) = 1;
138 return val;
141 /* Return a negative overflow infinity for TYPE. */
143 static inline tree
144 negative_overflow_infinity (tree type)
146 #ifdef ENABLE_CHECKING
147 gcc_assert (supports_overflow_infinity (type));
148 #endif
149 return make_overflow_infinity (TYPE_MIN_VALUE (type));
152 /* Return a positive overflow infinity for TYPE. */
154 static inline tree
155 positive_overflow_infinity (tree type)
157 #ifdef ENABLE_CHECKING
158 gcc_assert (supports_overflow_infinity (type));
159 #endif
160 return make_overflow_infinity (TYPE_MAX_VALUE (type));
163 /* Return whether VAL is a negative overflow infinity. */
165 static inline bool
166 is_negative_overflow_infinity (tree val)
168 return (needs_overflow_infinity (TREE_TYPE (val))
169 && CONSTANT_CLASS_P (val)
170 && TREE_OVERFLOW (val)
171 && operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0));
174 /* Return whether VAL is a positive overflow infinity. */
176 static inline bool
177 is_positive_overflow_infinity (tree val)
179 return (needs_overflow_infinity (TREE_TYPE (val))
180 && CONSTANT_CLASS_P (val)
181 && TREE_OVERFLOW (val)
182 && operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0));
185 /* Return whether VAL is a positive or negative overflow infinity. */
187 static inline bool
188 is_overflow_infinity (tree val)
190 return (needs_overflow_infinity (TREE_TYPE (val))
191 && CONSTANT_CLASS_P (val)
192 && TREE_OVERFLOW (val)
193 && (operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0)
194 || operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0)));
197 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
198 the same value with TREE_OVERFLOW clear. This can be used to avoid
199 confusing a regular value with an overflow value. */
201 static inline tree
202 avoid_overflow_infinity (tree val)
204 if (!is_overflow_infinity (val))
205 return val;
207 if (operand_equal_p (val, TYPE_MAX_VALUE (TREE_TYPE (val)), 0))
208 return TYPE_MAX_VALUE (TREE_TYPE (val));
209 else
211 #ifdef ENABLE_CHECKING
212 gcc_assert (operand_equal_p (val, TYPE_MIN_VALUE (TREE_TYPE (val)), 0));
213 #endif
214 return TYPE_MIN_VALUE (TREE_TYPE (val));
219 /* Return whether VAL is equal to the maximum value of its type. This
220 will be true for a positive overflow infinity. We can't do a
221 simple equality comparison with TYPE_MAX_VALUE because C typedefs
222 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
223 to the integer constant with the same value in the type. */
225 static inline bool
226 vrp_val_is_max (tree val)
228 tree type_max = TYPE_MAX_VALUE (TREE_TYPE (val));
230 return (val == type_max
231 || (type_max != NULL_TREE
232 && operand_equal_p (val, type_max, 0)));
235 /* Return whether VAL is equal to the minimum value of its type. This
236 will be true for a negative overflow infinity. */
238 static inline bool
239 vrp_val_is_min (tree val)
241 tree type_min = TYPE_MIN_VALUE (TREE_TYPE (val));
243 return (val == type_min
244 || (type_min != NULL_TREE
245 && operand_equal_p (val, type_min, 0)));
249 /* Return true if ARG is marked with the nonnull attribute in the
250 current function signature. */
252 static bool
253 nonnull_arg_p (tree arg)
255 tree t, attrs, fntype;
256 unsigned HOST_WIDE_INT arg_num;
258 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
260 /* The static chain decl is always non null. */
261 if (arg == cfun->static_chain_decl)
262 return true;
264 fntype = TREE_TYPE (current_function_decl);
265 attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype));
267 /* If "nonnull" wasn't specified, we know nothing about the argument. */
268 if (attrs == NULL_TREE)
269 return false;
271 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
272 if (TREE_VALUE (attrs) == NULL_TREE)
273 return true;
275 /* Get the position number for ARG in the function signature. */
276 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
278 t = TREE_CHAIN (t), arg_num++)
280 if (t == arg)
281 break;
284 gcc_assert (t == arg);
286 /* Now see if ARG_NUM is mentioned in the nonnull list. */
287 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
289 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
290 return true;
293 return false;
297 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
299 static void
300 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
301 tree max, bitmap equiv)
303 #if defined ENABLE_CHECKING
304 /* Check the validity of the range. */
305 if (t == VR_RANGE || t == VR_ANTI_RANGE)
307 int cmp;
309 gcc_assert (min && max);
311 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
312 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
314 cmp = compare_values (min, max);
315 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
317 if (needs_overflow_infinity (TREE_TYPE (min)))
318 gcc_assert (!is_overflow_infinity (min)
319 || !is_overflow_infinity (max));
322 if (t == VR_UNDEFINED || t == VR_VARYING)
323 gcc_assert (min == NULL_TREE && max == NULL_TREE);
325 if (t == VR_UNDEFINED || t == VR_VARYING)
326 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
327 #endif
329 vr->type = t;
330 vr->min = min;
331 vr->max = max;
333 /* Since updating the equivalence set involves deep copying the
334 bitmaps, only do it if absolutely necessary. */
335 if (vr->equiv == NULL)
336 vr->equiv = BITMAP_ALLOC (NULL);
338 if (equiv != vr->equiv)
340 if (equiv && !bitmap_empty_p (equiv))
341 bitmap_copy (vr->equiv, equiv);
342 else
343 bitmap_clear (vr->equiv);
348 /* Copy value range FROM into value range TO. */
350 static inline void
351 copy_value_range (value_range_t *to, value_range_t *from)
353 set_value_range (to, from->type, from->min, from->max, from->equiv);
357 /* Set value range VR to VR_VARYING. */
359 static inline void
360 set_value_range_to_varying (value_range_t *vr)
362 vr->type = VR_VARYING;
363 vr->min = vr->max = NULL_TREE;
364 if (vr->equiv)
365 bitmap_clear (vr->equiv);
368 /* Set value range VR to a single value. This function is only called
369 with values we get from statements, and exists to clear the
370 TREE_OVERFLOW flag so that we don't think we have an overflow
371 infinity when we shouldn't. */
373 static inline void
374 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
376 gcc_assert (is_gimple_min_invariant (val));
377 val = avoid_overflow_infinity (val);
378 set_value_range (vr, VR_RANGE, val, val, equiv);
381 /* Set value range VR to a non-negative range of type TYPE.
382 OVERFLOW_INFINITY indicates whether to use a overflow infinity
383 rather than TYPE_MAX_VALUE; this should be true if we determine
384 that the range is nonnegative based on the assumption that signed
385 overflow does not occur. */
387 static inline void
388 set_value_range_to_nonnegative (value_range_t *vr, tree type,
389 bool overflow_infinity)
391 tree zero;
393 if (overflow_infinity && !supports_overflow_infinity (type))
395 set_value_range_to_varying (vr);
396 return;
399 zero = build_int_cst (type, 0);
400 set_value_range (vr, VR_RANGE, zero,
401 (overflow_infinity
402 ? positive_overflow_infinity (type)
403 : TYPE_MAX_VALUE (type)),
404 vr->equiv);
407 /* Set value range VR to a non-NULL range of type TYPE. */
409 static inline void
410 set_value_range_to_nonnull (value_range_t *vr, tree type)
412 tree zero = build_int_cst (type, 0);
413 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
417 /* Set value range VR to a NULL range of type TYPE. */
419 static inline void
420 set_value_range_to_null (value_range_t *vr, tree type)
422 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
426 /* Set value range VR to VR_UNDEFINED. */
428 static inline void
429 set_value_range_to_undefined (value_range_t *vr)
431 vr->type = VR_UNDEFINED;
432 vr->min = vr->max = NULL_TREE;
433 if (vr->equiv)
434 bitmap_clear (vr->equiv);
438 /* Return value range information for VAR.
440 If we have no values ranges recorded (ie, VRP is not running), then
441 return NULL. Otherwise create an empty range if none existed for VAR. */
443 static value_range_t *
444 get_value_range (tree var)
446 value_range_t *vr;
447 tree sym;
448 unsigned ver = SSA_NAME_VERSION (var);
450 /* If we have no recorded ranges, then return NULL. */
451 if (! vr_value)
452 return NULL;
454 vr = vr_value[ver];
455 if (vr)
456 return vr;
458 /* Create a default value range. */
459 vr_value[ver] = vr = XNEW (value_range_t);
460 memset (vr, 0, sizeof (*vr));
462 /* Allocate an equivalence set. */
463 vr->equiv = BITMAP_ALLOC (NULL);
465 /* If VAR is a default definition, the variable can take any value
466 in VAR's type. */
467 sym = SSA_NAME_VAR (var);
468 if (var == default_def (sym))
470 /* Try to use the "nonnull" attribute to create ~[0, 0]
471 anti-ranges for pointers. Note that this is only valid with
472 default definitions of PARM_DECLs. */
473 if (TREE_CODE (sym) == PARM_DECL
474 && POINTER_TYPE_P (TREE_TYPE (sym))
475 && nonnull_arg_p (sym))
476 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
477 else
478 set_value_range_to_varying (vr);
481 return vr;
484 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
486 static inline bool
487 vrp_operand_equal_p (tree val1, tree val2)
489 if (val1 == val2)
490 return true;
491 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
492 return false;
493 if (is_overflow_infinity (val1))
494 return is_overflow_infinity (val2);
495 return true;
498 /* Return true, if the bitmaps B1 and B2 are equal. */
500 static inline bool
501 vrp_bitmap_equal_p (bitmap b1, bitmap b2)
503 return (b1 == b2
504 || (b1 && b2
505 && bitmap_equal_p (b1, b2)));
508 /* Update the value range and equivalence set for variable VAR to
509 NEW_VR. Return true if NEW_VR is different from VAR's previous
510 value.
512 NOTE: This function assumes that NEW_VR is a temporary value range
513 object created for the sole purpose of updating VAR's range. The
514 storage used by the equivalence set from NEW_VR will be freed by
515 this function. Do not call update_value_range when NEW_VR
516 is the range object associated with another SSA name. */
518 static inline bool
519 update_value_range (tree var, value_range_t *new_vr)
521 value_range_t *old_vr;
522 bool is_new;
524 /* Update the value range, if necessary. */
525 old_vr = get_value_range (var);
526 is_new = old_vr->type != new_vr->type
527 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
528 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
529 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
531 if (is_new)
532 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
533 new_vr->equiv);
535 BITMAP_FREE (new_vr->equiv);
536 new_vr->equiv = NULL;
538 return is_new;
542 /* Add VAR and VAR's equivalence set to EQUIV. */
544 static void
545 add_equivalence (bitmap equiv, tree var)
547 unsigned ver = SSA_NAME_VERSION (var);
548 value_range_t *vr = vr_value[ver];
550 bitmap_set_bit (equiv, ver);
551 if (vr && vr->equiv)
552 bitmap_ior_into (equiv, vr->equiv);
556 /* Return true if VR is ~[0, 0]. */
558 static inline bool
559 range_is_nonnull (value_range_t *vr)
561 return vr->type == VR_ANTI_RANGE
562 && integer_zerop (vr->min)
563 && integer_zerop (vr->max);
567 /* Return true if VR is [0, 0]. */
569 static inline bool
570 range_is_null (value_range_t *vr)
572 return vr->type == VR_RANGE
573 && integer_zerop (vr->min)
574 && integer_zerop (vr->max);
578 /* Return true if value range VR involves at least one symbol. */
580 static inline bool
581 symbolic_range_p (value_range_t *vr)
583 return (!is_gimple_min_invariant (vr->min)
584 || !is_gimple_min_invariant (vr->max));
587 /* Return true if value range VR uses a overflow infinity. */
589 static inline bool
590 overflow_infinity_range_p (value_range_t *vr)
592 return (vr->type == VR_RANGE
593 && (is_overflow_infinity (vr->min)
594 || is_overflow_infinity (vr->max)));
597 /* Return false if we can not make a valid comparison based on VR;
598 this will be the case if it uses an overflow infinity and overflow
599 is not undefined (i.e., -fno-strict-overflow is in effect).
600 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
601 uses an overflow infinity. */
603 static bool
604 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
606 gcc_assert (vr->type == VR_RANGE);
607 if (is_overflow_infinity (vr->min))
609 *strict_overflow_p = true;
610 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
611 return false;
613 if (is_overflow_infinity (vr->max))
615 *strict_overflow_p = true;
616 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
617 return false;
619 return true;
623 /* Like tree_expr_nonnegative_warnv_p, but this function uses value
624 ranges obtained so far. */
626 static bool
627 vrp_expr_computes_nonnegative (tree expr, bool *strict_overflow_p)
629 return tree_expr_nonnegative_warnv_p (expr, strict_overflow_p);
632 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
633 obtained so far. */
635 static bool
636 vrp_expr_computes_nonzero (tree expr, bool *strict_overflow_p)
638 if (tree_expr_nonzero_warnv_p (expr, strict_overflow_p))
639 return true;
641 /* If we have an expression of the form &X->a, then the expression
642 is nonnull if X is nonnull. */
643 if (TREE_CODE (expr) == ADDR_EXPR)
645 tree base = get_base_address (TREE_OPERAND (expr, 0));
647 if (base != NULL_TREE
648 && TREE_CODE (base) == INDIRECT_REF
649 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
651 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
652 if (range_is_nonnull (vr))
653 return true;
657 return false;
660 /* Returns true if EXPR is a valid value (as expected by compare_values) --
661 a gimple invariant, or SSA_NAME +- CST. */
663 static bool
664 valid_value_p (tree expr)
666 if (TREE_CODE (expr) == SSA_NAME)
667 return true;
669 if (TREE_CODE (expr) == PLUS_EXPR
670 || TREE_CODE (expr) == MINUS_EXPR)
671 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
672 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
674 return is_gimple_min_invariant (expr);
677 /* Compare two values VAL1 and VAL2. Return
679 -2 if VAL1 and VAL2 cannot be compared at compile-time,
680 -1 if VAL1 < VAL2,
681 0 if VAL1 == VAL2,
682 +1 if VAL1 > VAL2, and
683 +2 if VAL1 != VAL2
685 This is similar to tree_int_cst_compare but supports pointer values
686 and values that cannot be compared at compile time.
688 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
689 true if the return value is only valid if we assume that signed
690 overflow is undefined. */
692 static int
693 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
695 if (val1 == val2)
696 return 0;
698 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
699 both integers. */
700 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
701 == POINTER_TYPE_P (TREE_TYPE (val2)));
703 if ((TREE_CODE (val1) == SSA_NAME
704 || TREE_CODE (val1) == PLUS_EXPR
705 || TREE_CODE (val1) == MINUS_EXPR)
706 && (TREE_CODE (val2) == SSA_NAME
707 || TREE_CODE (val2) == PLUS_EXPR
708 || TREE_CODE (val2) == MINUS_EXPR))
710 tree n1, c1, n2, c2;
711 enum tree_code code1, code2;
713 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
714 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
715 same name, return -2. */
716 if (TREE_CODE (val1) == SSA_NAME)
718 code1 = SSA_NAME;
719 n1 = val1;
720 c1 = NULL_TREE;
722 else
724 code1 = TREE_CODE (val1);
725 n1 = TREE_OPERAND (val1, 0);
726 c1 = TREE_OPERAND (val1, 1);
727 if (tree_int_cst_sgn (c1) == -1)
729 if (is_negative_overflow_infinity (c1))
730 return -2;
731 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
732 if (!c1)
733 return -2;
734 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
738 if (TREE_CODE (val2) == SSA_NAME)
740 code2 = SSA_NAME;
741 n2 = val2;
742 c2 = NULL_TREE;
744 else
746 code2 = TREE_CODE (val2);
747 n2 = TREE_OPERAND (val2, 0);
748 c2 = TREE_OPERAND (val2, 1);
749 if (tree_int_cst_sgn (c2) == -1)
751 if (is_negative_overflow_infinity (c2))
752 return -2;
753 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
754 if (!c2)
755 return -2;
756 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
760 /* Both values must use the same name. */
761 if (n1 != n2)
762 return -2;
764 if (code1 == SSA_NAME
765 && code2 == SSA_NAME)
766 /* NAME == NAME */
767 return 0;
769 /* If overflow is defined we cannot simplify more. */
770 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
771 return -2;
773 if (strict_overflow_p != NULL
774 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
775 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
776 *strict_overflow_p = true;
778 if (code1 == SSA_NAME)
780 if (code2 == PLUS_EXPR)
781 /* NAME < NAME + CST */
782 return -1;
783 else if (code2 == MINUS_EXPR)
784 /* NAME > NAME - CST */
785 return 1;
787 else if (code1 == PLUS_EXPR)
789 if (code2 == SSA_NAME)
790 /* NAME + CST > NAME */
791 return 1;
792 else if (code2 == PLUS_EXPR)
793 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
794 return compare_values_warnv (c1, c2, strict_overflow_p);
795 else if (code2 == MINUS_EXPR)
796 /* NAME + CST1 > NAME - CST2 */
797 return 1;
799 else if (code1 == MINUS_EXPR)
801 if (code2 == SSA_NAME)
802 /* NAME - CST < NAME */
803 return -1;
804 else if (code2 == PLUS_EXPR)
805 /* NAME - CST1 < NAME + CST2 */
806 return -1;
807 else if (code2 == MINUS_EXPR)
808 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
809 C1 and C2 are swapped in the call to compare_values. */
810 return compare_values_warnv (c2, c1, strict_overflow_p);
813 gcc_unreachable ();
816 /* We cannot compare non-constants. */
817 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
818 return -2;
820 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
822 /* We cannot compare overflowed values, except for overflow
823 infinities. */
824 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
826 if (strict_overflow_p != NULL)
827 *strict_overflow_p = true;
828 if (is_negative_overflow_infinity (val1))
829 return is_negative_overflow_infinity (val2) ? 0 : -1;
830 else if (is_negative_overflow_infinity (val2))
831 return 1;
832 else if (is_positive_overflow_infinity (val1))
833 return is_positive_overflow_infinity (val2) ? 0 : 1;
834 else if (is_positive_overflow_infinity (val2))
835 return -1;
836 return -2;
839 return tree_int_cst_compare (val1, val2);
841 else
843 tree t;
845 /* First see if VAL1 and VAL2 are not the same. */
846 if (val1 == val2 || operand_equal_p (val1, val2, 0))
847 return 0;
849 /* If VAL1 is a lower address than VAL2, return -1. */
850 t = fold_binary (LT_EXPR, boolean_type_node, val1, val2);
851 if (t == boolean_true_node)
852 return -1;
854 /* If VAL1 is a higher address than VAL2, return +1. */
855 t = fold_binary (GT_EXPR, boolean_type_node, val1, val2);
856 if (t == boolean_true_node)
857 return 1;
859 /* If VAL1 is different than VAL2, return +2. */
860 t = fold_binary (NE_EXPR, boolean_type_node, val1, val2);
861 if (t == boolean_true_node)
862 return 2;
864 return -2;
868 /* Compare values like compare_values_warnv, but treat comparisons of
869 nonconstants which rely on undefined overflow as incomparable. */
871 static int
872 compare_values (tree val1, tree val2)
874 bool sop;
875 int ret;
877 sop = false;
878 ret = compare_values_warnv (val1, val2, &sop);
879 if (sop
880 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
881 ret = -2;
882 return ret;
886 /* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
887 0 if VAL is not inside VR,
888 -2 if we cannot tell either way.
890 FIXME, the current semantics of this functions are a bit quirky
891 when taken in the context of VRP. In here we do not care
892 about VR's type. If VR is the anti-range ~[3, 5] the call
893 value_inside_range (4, VR) will return 1.
895 This is counter-intuitive in a strict sense, but the callers
896 currently expect this. They are calling the function
897 merely to determine whether VR->MIN <= VAL <= VR->MAX. The
898 callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
899 themselves.
901 This also applies to value_ranges_intersect_p and
902 range_includes_zero_p. The semantics of VR_RANGE and
903 VR_ANTI_RANGE should be encoded here, but that also means
904 adapting the users of these functions to the new semantics. */
906 static inline int
907 value_inside_range (tree val, value_range_t *vr)
909 tree cmp1, cmp2;
911 fold_defer_overflow_warnings ();
913 cmp1 = fold_binary_to_constant (GE_EXPR, boolean_type_node, val, vr->min);
914 if (!cmp1)
916 fold_undefer_and_ignore_overflow_warnings ();
917 return -2;
920 cmp2 = fold_binary_to_constant (LE_EXPR, boolean_type_node, val, vr->max);
922 fold_undefer_and_ignore_overflow_warnings ();
924 if (!cmp2)
925 return -2;
927 return cmp1 == boolean_true_node && cmp2 == boolean_true_node;
931 /* Return true if value ranges VR0 and VR1 have a non-empty
932 intersection. */
934 static inline bool
935 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
937 return (value_inside_range (vr1->min, vr0) == 1
938 || value_inside_range (vr1->max, vr0) == 1
939 || value_inside_range (vr0->min, vr1) == 1
940 || value_inside_range (vr0->max, vr1) == 1);
944 /* Return true if VR includes the value zero, false otherwise. FIXME,
945 currently this will return false for an anti-range like ~[-4, 3].
946 This will be wrong when the semantics of value_inside_range are
947 modified (currently the users of this function expect these
948 semantics). */
950 static inline bool
951 range_includes_zero_p (value_range_t *vr)
953 tree zero;
955 gcc_assert (vr->type != VR_UNDEFINED
956 && vr->type != VR_VARYING
957 && !symbolic_range_p (vr));
959 zero = build_int_cst (TREE_TYPE (vr->min), 0);
960 return (value_inside_range (zero, vr) == 1);
963 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
964 false otherwise or if no value range information is available. */
966 bool
967 ssa_name_nonnegative_p (tree t)
969 value_range_t *vr = get_value_range (t);
971 if (!vr)
972 return false;
974 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
975 which would return a useful value should be encoded as a VR_RANGE. */
976 if (vr->type == VR_RANGE)
978 int result = compare_values (vr->min, integer_zero_node);
980 return (result == 0 || result == 1);
982 return false;
985 /* Return true if T, an SSA_NAME, is known to be nonzero. Return
986 false otherwise or if no value range information is available. */
988 bool
989 ssa_name_nonzero_p (tree t)
991 value_range_t *vr = get_value_range (t);
993 if (!vr)
994 return false;
996 /* A VR_RANGE which does not include zero is a nonzero value. */
997 if (vr->type == VR_RANGE && !symbolic_range_p (vr))
998 return ! range_includes_zero_p (vr);
1000 /* A VR_ANTI_RANGE which does include zero is a nonzero value. */
1001 if (vr->type == VR_ANTI_RANGE && !symbolic_range_p (vr))
1002 return range_includes_zero_p (vr);
1004 return false;
1008 /* Extract value range information from an ASSERT_EXPR EXPR and store
1009 it in *VR_P. */
1011 static void
1012 extract_range_from_assert (value_range_t *vr_p, tree expr)
1014 tree var, cond, limit, min, max, type;
1015 value_range_t *var_vr, *limit_vr;
1016 enum tree_code cond_code;
1018 var = ASSERT_EXPR_VAR (expr);
1019 cond = ASSERT_EXPR_COND (expr);
1021 gcc_assert (COMPARISON_CLASS_P (cond));
1023 /* Find VAR in the ASSERT_EXPR conditional. */
1024 if (var == TREE_OPERAND (cond, 0))
1026 /* If the predicate is of the form VAR COMP LIMIT, then we just
1027 take LIMIT from the RHS and use the same comparison code. */
1028 limit = TREE_OPERAND (cond, 1);
1029 cond_code = TREE_CODE (cond);
1031 else
1033 /* If the predicate is of the form LIMIT COMP VAR, then we need
1034 to flip around the comparison code to create the proper range
1035 for VAR. */
1036 limit = TREE_OPERAND (cond, 0);
1037 cond_code = swap_tree_comparison (TREE_CODE (cond));
1040 limit = avoid_overflow_infinity (limit);
1042 type = TREE_TYPE (limit);
1043 gcc_assert (limit != var);
1045 /* For pointer arithmetic, we only keep track of pointer equality
1046 and inequality. */
1047 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1049 set_value_range_to_varying (vr_p);
1050 return;
1053 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1054 try to use LIMIT's range to avoid creating symbolic ranges
1055 unnecessarily. */
1056 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1058 /* LIMIT's range is only interesting if it has any useful information. */
1059 if (limit_vr
1060 && (limit_vr->type == VR_UNDEFINED
1061 || limit_vr->type == VR_VARYING
1062 || symbolic_range_p (limit_vr)))
1063 limit_vr = NULL;
1065 /* Initially, the new range has the same set of equivalences of
1066 VAR's range. This will be revised before returning the final
1067 value. Since assertions may be chained via mutually exclusive
1068 predicates, we will need to trim the set of equivalences before
1069 we are done. */
1070 gcc_assert (vr_p->equiv == NULL);
1071 vr_p->equiv = BITMAP_ALLOC (NULL);
1072 add_equivalence (vr_p->equiv, var);
1074 /* Extract a new range based on the asserted comparison for VAR and
1075 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1076 will only use it for equality comparisons (EQ_EXPR). For any
1077 other kind of assertion, we cannot derive a range from LIMIT's
1078 anti-range that can be used to describe the new range. For
1079 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1080 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1081 no single range for x_2 that could describe LE_EXPR, so we might
1082 as well build the range [b_4, +INF] for it. */
1083 if (cond_code == EQ_EXPR)
1085 enum value_range_type range_type;
1087 if (limit_vr)
1089 range_type = limit_vr->type;
1090 min = limit_vr->min;
1091 max = limit_vr->max;
1093 else
1095 range_type = VR_RANGE;
1096 min = limit;
1097 max = limit;
1100 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1102 /* When asserting the equality VAR == LIMIT and LIMIT is another
1103 SSA name, the new range will also inherit the equivalence set
1104 from LIMIT. */
1105 if (TREE_CODE (limit) == SSA_NAME)
1106 add_equivalence (vr_p->equiv, limit);
1108 else if (cond_code == NE_EXPR)
1110 /* As described above, when LIMIT's range is an anti-range and
1111 this assertion is an inequality (NE_EXPR), then we cannot
1112 derive anything from the anti-range. For instance, if
1113 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1114 not imply that VAR's range is [0, 0]. So, in the case of
1115 anti-ranges, we just assert the inequality using LIMIT and
1116 not its anti-range.
1118 If LIMIT_VR is a range, we can only use it to build a new
1119 anti-range if LIMIT_VR is a single-valued range. For
1120 instance, if LIMIT_VR is [0, 1], the predicate
1121 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1122 Rather, it means that for value 0 VAR should be ~[0, 0]
1123 and for value 1, VAR should be ~[1, 1]. We cannot
1124 represent these ranges.
1126 The only situation in which we can build a valid
1127 anti-range is when LIMIT_VR is a single-valued range
1128 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1129 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1130 if (limit_vr
1131 && limit_vr->type == VR_RANGE
1132 && compare_values (limit_vr->min, limit_vr->max) == 0)
1134 min = limit_vr->min;
1135 max = limit_vr->max;
1137 else
1139 /* In any other case, we cannot use LIMIT's range to build a
1140 valid anti-range. */
1141 min = max = limit;
1144 /* If MIN and MAX cover the whole range for their type, then
1145 just use the original LIMIT. */
1146 if (INTEGRAL_TYPE_P (type)
1147 && vrp_val_is_min (min)
1148 && vrp_val_is_max (max))
1149 min = max = limit;
1151 set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
1153 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1155 min = TYPE_MIN_VALUE (type);
1157 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1158 max = limit;
1159 else
1161 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1162 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1163 LT_EXPR. */
1164 max = limit_vr->max;
1167 /* If the maximum value forces us to be out of bounds, simply punt.
1168 It would be pointless to try and do anything more since this
1169 all should be optimized away above us. */
1170 if ((cond_code == LT_EXPR
1171 && compare_values (max, min) == 0)
1172 || is_overflow_infinity (max))
1173 set_value_range_to_varying (vr_p);
1174 else
1176 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1177 if (cond_code == LT_EXPR)
1179 tree one = build_int_cst (type, 1);
1180 max = fold_build2 (MINUS_EXPR, type, max, one);
1181 if (EXPR_P (max))
1182 TREE_NO_WARNING (max) = 1;
1185 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1188 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1190 max = TYPE_MAX_VALUE (type);
1192 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1193 min = limit;
1194 else
1196 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1197 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1198 GT_EXPR. */
1199 min = limit_vr->min;
1202 /* If the minimum value forces us to be out of bounds, simply punt.
1203 It would be pointless to try and do anything more since this
1204 all should be optimized away above us. */
1205 if ((cond_code == GT_EXPR
1206 && compare_values (min, max) == 0)
1207 || is_overflow_infinity (min))
1208 set_value_range_to_varying (vr_p);
1209 else
1211 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1212 if (cond_code == GT_EXPR)
1214 tree one = build_int_cst (type, 1);
1215 min = fold_build2 (PLUS_EXPR, type, min, one);
1216 if (EXPR_P (min))
1217 TREE_NO_WARNING (min) = 1;
1220 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1223 else
1224 gcc_unreachable ();
1226 /* If VAR already had a known range, it may happen that the new
1227 range we have computed and VAR's range are not compatible. For
1228 instance,
1230 if (p_5 == NULL)
1231 p_6 = ASSERT_EXPR <p_5, p_5 == NULL>;
1232 x_7 = p_6->fld;
1233 p_8 = ASSERT_EXPR <p_6, p_6 != NULL>;
1235 While the above comes from a faulty program, it will cause an ICE
1236 later because p_8 and p_6 will have incompatible ranges and at
1237 the same time will be considered equivalent. A similar situation
1238 would arise from
1240 if (i_5 > 10)
1241 i_6 = ASSERT_EXPR <i_5, i_5 > 10>;
1242 if (i_5 < 5)
1243 i_7 = ASSERT_EXPR <i_6, i_6 < 5>;
1245 Again i_6 and i_7 will have incompatible ranges. It would be
1246 pointless to try and do anything with i_7's range because
1247 anything dominated by 'if (i_5 < 5)' will be optimized away.
1248 Note, due to the wa in which simulation proceeds, the statement
1249 i_7 = ASSERT_EXPR <...> we would never be visited because the
1250 conditional 'if (i_5 < 5)' always evaluates to false. However,
1251 this extra check does not hurt and may protect against future
1252 changes to VRP that may get into a situation similar to the
1253 NULL pointer dereference example.
1255 Note that these compatibility tests are only needed when dealing
1256 with ranges or a mix of range and anti-range. If VAR_VR and VR_P
1257 are both anti-ranges, they will always be compatible, because two
1258 anti-ranges will always have a non-empty intersection. */
1260 var_vr = get_value_range (var);
1262 /* We may need to make adjustments when VR_P and VAR_VR are numeric
1263 ranges or anti-ranges. */
1264 if (vr_p->type == VR_VARYING
1265 || vr_p->type == VR_UNDEFINED
1266 || var_vr->type == VR_VARYING
1267 || var_vr->type == VR_UNDEFINED
1268 || symbolic_range_p (vr_p)
1269 || symbolic_range_p (var_vr))
1270 return;
1272 if (var_vr->type == VR_RANGE && vr_p->type == VR_RANGE)
1274 /* If the two ranges have a non-empty intersection, we can
1275 refine the resulting range. Since the assert expression
1276 creates an equivalency and at the same time it asserts a
1277 predicate, we can take the intersection of the two ranges to
1278 get better precision. */
1279 if (value_ranges_intersect_p (var_vr, vr_p))
1281 /* Use the larger of the two minimums. */
1282 if (compare_values (vr_p->min, var_vr->min) == -1)
1283 min = var_vr->min;
1284 else
1285 min = vr_p->min;
1287 /* Use the smaller of the two maximums. */
1288 if (compare_values (vr_p->max, var_vr->max) == 1)
1289 max = var_vr->max;
1290 else
1291 max = vr_p->max;
1293 set_value_range (vr_p, vr_p->type, min, max, vr_p->equiv);
1295 else
1297 /* The two ranges do not intersect, set the new range to
1298 VARYING, because we will not be able to do anything
1299 meaningful with it. */
1300 set_value_range_to_varying (vr_p);
1303 else if ((var_vr->type == VR_RANGE && vr_p->type == VR_ANTI_RANGE)
1304 || (var_vr->type == VR_ANTI_RANGE && vr_p->type == VR_RANGE))
1306 /* A range and an anti-range will cancel each other only if
1307 their ends are the same. For instance, in the example above,
1308 p_8's range ~[0, 0] and p_6's range [0, 0] are incompatible,
1309 so VR_P should be set to VR_VARYING. */
1310 if (compare_values (var_vr->min, vr_p->min) == 0
1311 && compare_values (var_vr->max, vr_p->max) == 0)
1312 set_value_range_to_varying (vr_p);
1313 else
1315 tree min, max, anti_min, anti_max, real_min, real_max;
1317 /* We want to compute the logical AND of the two ranges;
1318 there are three cases to consider.
1321 1. The VR_ANTI_RANGE range is completely within the
1322 VR_RANGE and the endpoints of the ranges are
1323 different. In that case the resulting range
1324 should be whichever range is more precise.
1325 Typically that will be the VR_RANGE.
1327 2. The VR_ANTI_RANGE is completely disjoint from
1328 the VR_RANGE. In this case the resulting range
1329 should be the VR_RANGE.
1331 3. There is some overlap between the VR_ANTI_RANGE
1332 and the VR_RANGE.
1334 3a. If the high limit of the VR_ANTI_RANGE resides
1335 within the VR_RANGE, then the result is a new
1336 VR_RANGE starting at the high limit of the
1337 the VR_ANTI_RANGE + 1 and extending to the
1338 high limit of the original VR_RANGE.
1340 3b. If the low limit of the VR_ANTI_RANGE resides
1341 within the VR_RANGE, then the result is a new
1342 VR_RANGE starting at the low limit of the original
1343 VR_RANGE and extending to the low limit of the
1344 VR_ANTI_RANGE - 1. */
1345 if (vr_p->type == VR_ANTI_RANGE)
1347 anti_min = vr_p->min;
1348 anti_max = vr_p->max;
1349 real_min = var_vr->min;
1350 real_max = var_vr->max;
1352 else
1354 anti_min = var_vr->min;
1355 anti_max = var_vr->max;
1356 real_min = vr_p->min;
1357 real_max = vr_p->max;
1361 /* Case 1, VR_ANTI_RANGE completely within VR_RANGE,
1362 not including any endpoints. */
1363 if (compare_values (anti_max, real_max) == -1
1364 && compare_values (anti_min, real_min) == 1)
1366 set_value_range (vr_p, VR_RANGE, real_min,
1367 real_max, vr_p->equiv);
1369 /* Case 2, VR_ANTI_RANGE completely disjoint from
1370 VR_RANGE. */
1371 else if (compare_values (anti_min, real_max) == 1
1372 || compare_values (anti_max, real_min) == -1)
1374 set_value_range (vr_p, VR_RANGE, real_min,
1375 real_max, vr_p->equiv);
1377 /* Case 3a, the anti-range extends into the low
1378 part of the real range. Thus creating a new
1379 low for the real range. */
1380 else if ((compare_values (anti_max, real_min) == 1
1381 || compare_values (anti_max, real_min) == 0)
1382 && compare_values (anti_max, real_max) == -1)
1384 gcc_assert (!is_positive_overflow_infinity (anti_max));
1385 if (needs_overflow_infinity (TREE_TYPE (anti_max))
1386 && vrp_val_is_max (anti_max))
1388 if (!supports_overflow_infinity (TREE_TYPE (var_vr->min)))
1390 set_value_range_to_varying (vr_p);
1391 return;
1393 min = positive_overflow_infinity (TREE_TYPE (var_vr->min));
1395 else
1396 min = fold_build2 (PLUS_EXPR, TREE_TYPE (var_vr->min),
1397 anti_max,
1398 build_int_cst (TREE_TYPE (var_vr->min), 1));
1399 max = real_max;
1400 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1402 /* Case 3b, the anti-range extends into the high
1403 part of the real range. Thus creating a new
1404 higher for the real range. */
1405 else if (compare_values (anti_min, real_min) == 1
1406 && (compare_values (anti_min, real_max) == -1
1407 || compare_values (anti_min, real_max) == 0))
1409 gcc_assert (!is_negative_overflow_infinity (anti_min));
1410 if (needs_overflow_infinity (TREE_TYPE (anti_min))
1411 && vrp_val_is_min (anti_min))
1413 if (!supports_overflow_infinity (TREE_TYPE (var_vr->min)))
1415 set_value_range_to_varying (vr_p);
1416 return;
1418 max = negative_overflow_infinity (TREE_TYPE (var_vr->min));
1420 else
1421 max = fold_build2 (MINUS_EXPR, TREE_TYPE (var_vr->min),
1422 anti_min,
1423 build_int_cst (TREE_TYPE (var_vr->min), 1));
1424 min = real_min;
1425 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1432 /* Extract range information from SSA name VAR and store it in VR. If
1433 VAR has an interesting range, use it. Otherwise, create the
1434 range [VAR, VAR] and return it. This is useful in situations where
1435 we may have conditionals testing values of VARYING names. For
1436 instance,
1438 x_3 = y_5;
1439 if (x_3 > y_5)
1442 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1443 always false. */
1445 static void
1446 extract_range_from_ssa_name (value_range_t *vr, tree var)
1448 value_range_t *var_vr = get_value_range (var);
1450 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1451 copy_value_range (vr, var_vr);
1452 else
1453 set_value_range (vr, VR_RANGE, var, var, NULL);
1455 add_equivalence (vr->equiv, var);
1459 /* Wrapper around int_const_binop. If the operation overflows and we
1460 are not using wrapping arithmetic, then adjust the result to be
1461 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1462 NULL_TREE if we need to use an overflow infinity representation but
1463 the type does not support it. */
1465 static tree
1466 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1468 tree res;
1470 res = int_const_binop (code, val1, val2, 0);
1472 /* If we are not using wrapping arithmetic, operate symbolically
1473 on -INF and +INF. */
1474 if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1476 int checkz = compare_values (res, val1);
1477 bool overflow = false;
1479 /* Ensure that res = val1 [+*] val2 >= val1
1480 or that res = val1 - val2 <= val1. */
1481 if ((code == PLUS_EXPR
1482 && !(checkz == 1 || checkz == 0))
1483 || (code == MINUS_EXPR
1484 && !(checkz == 0 || checkz == -1)))
1486 overflow = true;
1488 /* Checking for multiplication overflow is done by dividing the
1489 output of the multiplication by the first input of the
1490 multiplication. If the result of that division operation is
1491 not equal to the second input of the multiplication, then the
1492 multiplication overflowed. */
1493 else if (code == MULT_EXPR && !integer_zerop (val1))
1495 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1496 res,
1497 val1, 0);
1498 int check = compare_values (tmp, val2);
1500 if (check != 0)
1501 overflow = true;
1504 if (overflow)
1506 res = copy_node (res);
1507 TREE_OVERFLOW (res) = 1;
1511 else if ((TREE_OVERFLOW (res)
1512 && !TREE_OVERFLOW (val1)
1513 && !TREE_OVERFLOW (val2))
1514 || is_overflow_infinity (val1)
1515 || is_overflow_infinity (val2))
1517 /* If the operation overflowed but neither VAL1 nor VAL2 are
1518 overflown, return -INF or +INF depending on the operation
1519 and the combination of signs of the operands. */
1520 int sgn1 = tree_int_cst_sgn (val1);
1521 int sgn2 = tree_int_cst_sgn (val2);
1523 if (needs_overflow_infinity (TREE_TYPE (res))
1524 && !supports_overflow_infinity (TREE_TYPE (res)))
1525 return NULL_TREE;
1527 /* We have to punt on adding infinities of different signs,
1528 since we can't tell what the sign of the result should be.
1529 Likewise for subtracting infinities of the same sign. */
1530 if (((code == PLUS_EXPR && sgn1 != sgn2)
1531 || (code == MINUS_EXPR && sgn1 == sgn2))
1532 && is_overflow_infinity (val1)
1533 && is_overflow_infinity (val2))
1534 return NULL_TREE;
1536 /* Don't try to handle division or shifting of infinities. */
1537 if ((code == TRUNC_DIV_EXPR
1538 || code == FLOOR_DIV_EXPR
1539 || code == CEIL_DIV_EXPR
1540 || code == EXACT_DIV_EXPR
1541 || code == ROUND_DIV_EXPR
1542 || code == RSHIFT_EXPR)
1543 && (is_overflow_infinity (val1)
1544 || is_overflow_infinity (val2)))
1545 return NULL_TREE;
1547 /* Notice that we only need to handle the restricted set of
1548 operations handled by extract_range_from_binary_expr.
1549 Among them, only multiplication, addition and subtraction
1550 can yield overflow without overflown operands because we
1551 are working with integral types only... except in the
1552 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1553 for division too. */
1555 /* For multiplication, the sign of the overflow is given
1556 by the comparison of the signs of the operands. */
1557 if ((code == MULT_EXPR && sgn1 == sgn2)
1558 /* For addition, the operands must be of the same sign
1559 to yield an overflow. Its sign is therefore that
1560 of one of the operands, for example the first. For
1561 infinite operands X + -INF is negative, not positive. */
1562 || (code == PLUS_EXPR
1563 && (sgn1 >= 0
1564 ? !is_negative_overflow_infinity (val2)
1565 : is_positive_overflow_infinity (val2)))
1566 /* For subtraction, non-infinite operands must be of
1567 different signs to yield an overflow. Its sign is
1568 therefore that of the first operand or the opposite of
1569 that of the second operand. A first operand of 0 counts
1570 as positive here, for the corner case 0 - (-INF), which
1571 overflows, but must yield +INF. For infinite operands 0
1572 - INF is negative, not positive. */
1573 || (code == MINUS_EXPR
1574 && (sgn1 >= 0
1575 ? !is_positive_overflow_infinity (val2)
1576 : is_negative_overflow_infinity (val2)))
1577 /* For division, the only case is -INF / -1 = +INF. */
1578 || code == TRUNC_DIV_EXPR
1579 || code == FLOOR_DIV_EXPR
1580 || code == CEIL_DIV_EXPR
1581 || code == EXACT_DIV_EXPR
1582 || code == ROUND_DIV_EXPR)
1583 return (needs_overflow_infinity (TREE_TYPE (res))
1584 ? positive_overflow_infinity (TREE_TYPE (res))
1585 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1586 else
1587 return (needs_overflow_infinity (TREE_TYPE (res))
1588 ? negative_overflow_infinity (TREE_TYPE (res))
1589 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1592 return res;
1596 /* Extract range information from a binary expression EXPR based on
1597 the ranges of each of its operands and the expression code. */
1599 static void
1600 extract_range_from_binary_expr (value_range_t *vr, tree expr)
1602 enum tree_code code = TREE_CODE (expr);
1603 enum value_range_type type;
1604 tree op0, op1, min, max;
1605 int cmp;
1606 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1607 value_range_t vr1 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1609 /* Not all binary expressions can be applied to ranges in a
1610 meaningful way. Handle only arithmetic operations. */
1611 if (code != PLUS_EXPR
1612 && code != MINUS_EXPR
1613 && code != MULT_EXPR
1614 && code != TRUNC_DIV_EXPR
1615 && code != FLOOR_DIV_EXPR
1616 && code != CEIL_DIV_EXPR
1617 && code != EXACT_DIV_EXPR
1618 && code != ROUND_DIV_EXPR
1619 && code != MIN_EXPR
1620 && code != MAX_EXPR
1621 && code != BIT_AND_EXPR
1622 && code != TRUTH_ANDIF_EXPR
1623 && code != TRUTH_ORIF_EXPR
1624 && code != TRUTH_AND_EXPR
1625 && code != TRUTH_OR_EXPR)
1627 set_value_range_to_varying (vr);
1628 return;
1631 /* Get value ranges for each operand. For constant operands, create
1632 a new value range with the operand to simplify processing. */
1633 op0 = TREE_OPERAND (expr, 0);
1634 if (TREE_CODE (op0) == SSA_NAME)
1635 vr0 = *(get_value_range (op0));
1636 else if (is_gimple_min_invariant (op0))
1637 set_value_range_to_value (&vr0, op0, NULL);
1638 else
1639 set_value_range_to_varying (&vr0);
1641 op1 = TREE_OPERAND (expr, 1);
1642 if (TREE_CODE (op1) == SSA_NAME)
1643 vr1 = *(get_value_range (op1));
1644 else if (is_gimple_min_invariant (op1))
1645 set_value_range_to_value (&vr1, op1, NULL);
1646 else
1647 set_value_range_to_varying (&vr1);
1649 /* If either range is UNDEFINED, so is the result. */
1650 if (vr0.type == VR_UNDEFINED || vr1.type == VR_UNDEFINED)
1652 set_value_range_to_undefined (vr);
1653 return;
1656 /* The type of the resulting value range defaults to VR0.TYPE. */
1657 type = vr0.type;
1659 /* Refuse to operate on VARYING ranges, ranges of different kinds
1660 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
1661 because we may be able to derive a useful range even if one of
1662 the operands is VR_VARYING or symbolic range. TODO, we may be
1663 able to derive anti-ranges in some cases. */
1664 if (code != BIT_AND_EXPR
1665 && code != TRUTH_AND_EXPR
1666 && code != TRUTH_OR_EXPR
1667 && (vr0.type == VR_VARYING
1668 || vr1.type == VR_VARYING
1669 || vr0.type != vr1.type
1670 || symbolic_range_p (&vr0)
1671 || symbolic_range_p (&vr1)))
1673 set_value_range_to_varying (vr);
1674 return;
1677 /* Now evaluate the expression to determine the new range. */
1678 if (POINTER_TYPE_P (TREE_TYPE (expr))
1679 || POINTER_TYPE_P (TREE_TYPE (op0))
1680 || POINTER_TYPE_P (TREE_TYPE (op1)))
1682 /* For pointer types, we are really only interested in asserting
1683 whether the expression evaluates to non-NULL. FIXME, we used
1684 to gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR), but
1685 ivopts is generating expressions with pointer multiplication
1686 in them. */
1687 if (code == PLUS_EXPR)
1689 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
1690 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1691 else if (range_is_null (&vr0) && range_is_null (&vr1))
1692 set_value_range_to_null (vr, TREE_TYPE (expr));
1693 else
1694 set_value_range_to_varying (vr);
1696 else
1698 /* Subtracting from a pointer, may yield 0, so just drop the
1699 resulting range to varying. */
1700 set_value_range_to_varying (vr);
1703 return;
1706 /* For integer ranges, apply the operation to each end of the
1707 range and see what we end up with. */
1708 if (code == TRUTH_ANDIF_EXPR
1709 || code == TRUTH_ORIF_EXPR
1710 || code == TRUTH_AND_EXPR
1711 || code == TRUTH_OR_EXPR)
1713 /* If one of the operands is zero, we know that the whole
1714 expression evaluates zero. */
1715 if (code == TRUTH_AND_EXPR
1716 && ((vr0.type == VR_RANGE
1717 && integer_zerop (vr0.min)
1718 && integer_zerop (vr0.max))
1719 || (vr1.type == VR_RANGE
1720 && integer_zerop (vr1.min)
1721 && integer_zerop (vr1.max))))
1723 type = VR_RANGE;
1724 min = max = build_int_cst (TREE_TYPE (expr), 0);
1726 /* If one of the operands is one, we know that the whole
1727 expression evaluates one. */
1728 else if (code == TRUTH_OR_EXPR
1729 && ((vr0.type == VR_RANGE
1730 && integer_onep (vr0.min)
1731 && integer_onep (vr0.max))
1732 || (vr1.type == VR_RANGE
1733 && integer_onep (vr1.min)
1734 && integer_onep (vr1.max))))
1736 type = VR_RANGE;
1737 min = max = build_int_cst (TREE_TYPE (expr), 1);
1739 else if (vr0.type != VR_VARYING
1740 && vr1.type != VR_VARYING
1741 && vr0.type == vr1.type
1742 && !symbolic_range_p (&vr0)
1743 && !overflow_infinity_range_p (&vr0)
1744 && !symbolic_range_p (&vr1)
1745 && !overflow_infinity_range_p (&vr1))
1747 /* Boolean expressions cannot be folded with int_const_binop. */
1748 min = fold_binary (code, TREE_TYPE (expr), vr0.min, vr1.min);
1749 max = fold_binary (code, TREE_TYPE (expr), vr0.max, vr1.max);
1751 else
1753 set_value_range_to_varying (vr);
1754 return;
1757 else if (code == PLUS_EXPR
1758 || code == MIN_EXPR
1759 || code == MAX_EXPR)
1761 /* If we have a PLUS_EXPR with two VR_ANTI_RANGEs, drop to
1762 VR_VARYING. It would take more effort to compute a precise
1763 range for such a case. For example, if we have op0 == 1 and
1764 op1 == -1 with their ranges both being ~[0,0], we would have
1765 op0 + op1 == 0, so we cannot claim that the sum is in ~[0,0].
1766 Note that we are guaranteed to have vr0.type == vr1.type at
1767 this point. */
1768 if (code == PLUS_EXPR && vr0.type == VR_ANTI_RANGE)
1770 set_value_range_to_varying (vr);
1771 return;
1774 /* For operations that make the resulting range directly
1775 proportional to the original ranges, apply the operation to
1776 the same end of each range. */
1777 min = vrp_int_const_binop (code, vr0.min, vr1.min);
1778 max = vrp_int_const_binop (code, vr0.max, vr1.max);
1780 else if (code == MULT_EXPR
1781 || code == TRUNC_DIV_EXPR
1782 || code == FLOOR_DIV_EXPR
1783 || code == CEIL_DIV_EXPR
1784 || code == EXACT_DIV_EXPR
1785 || code == ROUND_DIV_EXPR)
1787 tree val[4];
1788 size_t i;
1789 bool sop;
1791 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1792 drop to VR_VARYING. It would take more effort to compute a
1793 precise range for such a case. For example, if we have
1794 op0 == 65536 and op1 == 65536 with their ranges both being
1795 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1796 we cannot claim that the product is in ~[0,0]. Note that we
1797 are guaranteed to have vr0.type == vr1.type at this
1798 point. */
1799 if (code == MULT_EXPR
1800 && vr0.type == VR_ANTI_RANGE
1801 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
1803 set_value_range_to_varying (vr);
1804 return;
1807 /* Multiplications and divisions are a bit tricky to handle,
1808 depending on the mix of signs we have in the two ranges, we
1809 need to operate on different values to get the minimum and
1810 maximum values for the new range. One approach is to figure
1811 out all the variations of range combinations and do the
1812 operations.
1814 However, this involves several calls to compare_values and it
1815 is pretty convoluted. It's simpler to do the 4 operations
1816 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1817 MAX1) and then figure the smallest and largest values to form
1818 the new range. */
1820 /* Divisions by zero result in a VARYING value. */
1821 if (code != MULT_EXPR
1822 && (vr0.type == VR_ANTI_RANGE || range_includes_zero_p (&vr1)))
1824 set_value_range_to_varying (vr);
1825 return;
1828 /* Compute the 4 cross operations. */
1829 sop = false;
1830 val[0] = vrp_int_const_binop (code, vr0.min, vr1.min);
1831 if (val[0] == NULL_TREE)
1832 sop = true;
1834 if (vr1.max == vr1.min)
1835 val[1] = NULL_TREE;
1836 else
1838 val[1] = vrp_int_const_binop (code, vr0.min, vr1.max);
1839 if (val[1] == NULL_TREE)
1840 sop = true;
1843 if (vr0.max == vr0.min)
1844 val[2] = NULL_TREE;
1845 else
1847 val[2] = vrp_int_const_binop (code, vr0.max, vr1.min);
1848 if (val[2] == NULL_TREE)
1849 sop = true;
1852 if (vr0.min == vr0.max || vr1.min == vr1.max)
1853 val[3] = NULL_TREE;
1854 else
1856 val[3] = vrp_int_const_binop (code, vr0.max, vr1.max);
1857 if (val[3] == NULL_TREE)
1858 sop = true;
1861 if (sop)
1863 set_value_range_to_varying (vr);
1864 return;
1867 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
1868 of VAL[i]. */
1869 min = val[0];
1870 max = val[0];
1871 for (i = 1; i < 4; i++)
1873 if (!is_gimple_min_invariant (min)
1874 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
1875 || !is_gimple_min_invariant (max)
1876 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
1877 break;
1879 if (val[i])
1881 if (!is_gimple_min_invariant (val[i])
1882 || (TREE_OVERFLOW (val[i])
1883 && !is_overflow_infinity (val[i])))
1885 /* If we found an overflowed value, set MIN and MAX
1886 to it so that we set the resulting range to
1887 VARYING. */
1888 min = max = val[i];
1889 break;
1892 if (compare_values (val[i], min) == -1)
1893 min = val[i];
1895 if (compare_values (val[i], max) == 1)
1896 max = val[i];
1900 else if (code == MINUS_EXPR)
1902 /* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
1903 VR_VARYING. It would take more effort to compute a precise
1904 range for such a case. For example, if we have op0 == 1 and
1905 op1 == 1 with their ranges both being ~[0,0], we would have
1906 op0 - op1 == 0, so we cannot claim that the difference is in
1907 ~[0,0]. Note that we are guaranteed to have
1908 vr0.type == vr1.type at this point. */
1909 if (vr0.type == VR_ANTI_RANGE)
1911 set_value_range_to_varying (vr);
1912 return;
1915 /* For MINUS_EXPR, apply the operation to the opposite ends of
1916 each range. */
1917 min = vrp_int_const_binop (code, vr0.min, vr1.max);
1918 max = vrp_int_const_binop (code, vr0.max, vr1.min);
1920 else if (code == BIT_AND_EXPR)
1922 if (vr0.type == VR_RANGE
1923 && vr0.min == vr0.max
1924 && TREE_CODE (vr0.max) == INTEGER_CST
1925 && !TREE_OVERFLOW (vr0.max)
1926 && tree_int_cst_sgn (vr0.max) >= 0)
1928 min = build_int_cst (TREE_TYPE (expr), 0);
1929 max = vr0.max;
1931 else if (vr1.type == VR_RANGE
1932 && vr1.min == vr1.max
1933 && TREE_CODE (vr1.max) == INTEGER_CST
1934 && !TREE_OVERFLOW (vr1.max)
1935 && tree_int_cst_sgn (vr1.max) >= 0)
1937 type = VR_RANGE;
1938 min = build_int_cst (TREE_TYPE (expr), 0);
1939 max = vr1.max;
1941 else
1943 set_value_range_to_varying (vr);
1944 return;
1947 else
1948 gcc_unreachable ();
1950 /* If either MIN or MAX overflowed, then set the resulting range to
1951 VARYING. But we do accept an overflow infinity
1952 representation. */
1953 if (min == NULL_TREE
1954 || !is_gimple_min_invariant (min)
1955 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
1956 || max == NULL_TREE
1957 || !is_gimple_min_invariant (max)
1958 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
1960 set_value_range_to_varying (vr);
1961 return;
1964 /* We punt if:
1965 1) [-INF, +INF]
1966 2) [-INF, +-INF(OVF)]
1967 3) [+-INF(OVF), +INF]
1968 4) [+-INF(OVF), +-INF(OVF)]
1969 We learn nothing when we have INF and INF(OVF) on both sides.
1970 Note that we do accept [-INF, -INF] and [+INF, +INF] without
1971 overflow. */
1972 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
1973 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
1975 set_value_range_to_varying (vr);
1976 return;
1979 cmp = compare_values (min, max);
1980 if (cmp == -2 || cmp == 1)
1982 /* If the new range has its limits swapped around (MIN > MAX),
1983 then the operation caused one of them to wrap around, mark
1984 the new range VARYING. */
1985 set_value_range_to_varying (vr);
1987 else
1988 set_value_range (vr, type, min, max, NULL);
1992 /* Extract range information from a unary expression EXPR based on
1993 the range of its operand and the expression code. */
1995 static void
1996 extract_range_from_unary_expr (value_range_t *vr, tree expr)
1998 enum tree_code code = TREE_CODE (expr);
1999 tree min, max, op0;
2000 int cmp;
2001 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
2003 /* Refuse to operate on certain unary expressions for which we
2004 cannot easily determine a resulting range. */
2005 if (code == FIX_TRUNC_EXPR
2006 || code == FIX_CEIL_EXPR
2007 || code == FIX_FLOOR_EXPR
2008 || code == FIX_ROUND_EXPR
2009 || code == FLOAT_EXPR
2010 || code == BIT_NOT_EXPR
2011 || code == NON_LVALUE_EXPR
2012 || code == CONJ_EXPR)
2014 set_value_range_to_varying (vr);
2015 return;
2018 /* Get value ranges for the operand. For constant operands, create
2019 a new value range with the operand to simplify processing. */
2020 op0 = TREE_OPERAND (expr, 0);
2021 if (TREE_CODE (op0) == SSA_NAME)
2022 vr0 = *(get_value_range (op0));
2023 else if (is_gimple_min_invariant (op0))
2024 set_value_range_to_value (&vr0, op0, NULL);
2025 else
2026 set_value_range_to_varying (&vr0);
2028 /* If VR0 is UNDEFINED, so is the result. */
2029 if (vr0.type == VR_UNDEFINED)
2031 set_value_range_to_undefined (vr);
2032 return;
2035 /* Refuse to operate on symbolic ranges, or if neither operand is
2036 a pointer or integral type. */
2037 if ((!INTEGRAL_TYPE_P (TREE_TYPE (op0))
2038 && !POINTER_TYPE_P (TREE_TYPE (op0)))
2039 || (vr0.type != VR_VARYING
2040 && symbolic_range_p (&vr0)))
2042 set_value_range_to_varying (vr);
2043 return;
2046 /* If the expression involves pointers, we are only interested in
2047 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
2048 if (POINTER_TYPE_P (TREE_TYPE (expr)) || POINTER_TYPE_P (TREE_TYPE (op0)))
2050 bool sop;
2052 sop = false;
2053 if (range_is_nonnull (&vr0)
2054 || (tree_expr_nonzero_warnv_p (expr, &sop)
2055 && !sop))
2056 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
2057 else if (range_is_null (&vr0))
2058 set_value_range_to_null (vr, TREE_TYPE (expr));
2059 else
2060 set_value_range_to_varying (vr);
2062 return;
2065 /* Handle unary expressions on integer ranges. */
2066 if (code == NOP_EXPR || code == CONVERT_EXPR)
2068 tree inner_type = TREE_TYPE (op0);
2069 tree outer_type = TREE_TYPE (expr);
2071 /* If VR0 represents a simple range, then try to convert
2072 the min and max values for the range to the same type
2073 as OUTER_TYPE. If the results compare equal to VR0's
2074 min and max values and the new min is still less than
2075 or equal to the new max, then we can safely use the newly
2076 computed range for EXPR. This allows us to compute
2077 accurate ranges through many casts. */
2078 if ((vr0.type == VR_RANGE
2079 && !overflow_infinity_range_p (&vr0))
2080 || (vr0.type == VR_VARYING
2081 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)))
2083 tree new_min, new_max, orig_min, orig_max;
2085 /* Convert the input operand min/max to OUTER_TYPE. If
2086 the input has no range information, then use the min/max
2087 for the input's type. */
2088 if (vr0.type == VR_RANGE)
2090 orig_min = vr0.min;
2091 orig_max = vr0.max;
2093 else
2095 orig_min = TYPE_MIN_VALUE (inner_type);
2096 orig_max = TYPE_MAX_VALUE (inner_type);
2099 new_min = fold_convert (outer_type, orig_min);
2100 new_max = fold_convert (outer_type, orig_max);
2102 /* Verify the new min/max values are gimple values and
2103 that they compare equal to the original input's
2104 min/max values. */
2105 if (is_gimple_val (new_min)
2106 && is_gimple_val (new_max)
2107 && tree_int_cst_equal (new_min, orig_min)
2108 && tree_int_cst_equal (new_max, orig_max)
2109 && (!is_overflow_infinity (new_min)
2110 || !is_overflow_infinity (new_max))
2111 && compare_values (new_min, new_max) <= 0
2112 && compare_values (new_min, new_max) >= -1)
2114 set_value_range (vr, VR_RANGE, new_min, new_max, vr->equiv);
2115 return;
2119 /* When converting types of different sizes, set the result to
2120 VARYING. Things like sign extensions and precision loss may
2121 change the range. For instance, if x_3 is of type 'long long
2122 int' and 'y_5 = (unsigned short) x_3', if x_3 is ~[0, 0], it
2123 is impossible to know at compile time whether y_5 will be
2124 ~[0, 0]. */
2125 if (TYPE_SIZE (inner_type) != TYPE_SIZE (outer_type)
2126 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
2128 set_value_range_to_varying (vr);
2129 return;
2133 /* Conversion of a VR_VARYING value to a wider type can result
2134 in a usable range. So wait until after we've handled conversions
2135 before dropping the result to VR_VARYING if we had a source
2136 operand that is VR_VARYING. */
2137 if (vr0.type == VR_VARYING)
2139 set_value_range_to_varying (vr);
2140 return;
2143 /* Apply the operation to each end of the range and see what we end
2144 up with. */
2145 if (code == NEGATE_EXPR
2146 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
2148 /* NEGATE_EXPR flips the range around. We need to treat
2149 TYPE_MIN_VALUE specially. */
2150 if (is_positive_overflow_infinity (vr0.max))
2151 min = negative_overflow_infinity (TREE_TYPE (expr));
2152 else if (is_negative_overflow_infinity (vr0.max))
2153 min = positive_overflow_infinity (TREE_TYPE (expr));
2154 else if (!vrp_val_is_min (vr0.max))
2155 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2156 else if (needs_overflow_infinity (TREE_TYPE (expr)))
2158 if (supports_overflow_infinity (TREE_TYPE (expr))
2159 && !is_overflow_infinity (vr0.min)
2160 && !vrp_val_is_min (vr0.min))
2161 min = positive_overflow_infinity (TREE_TYPE (expr));
2162 else
2164 set_value_range_to_varying (vr);
2165 return;
2168 else
2169 min = TYPE_MIN_VALUE (TREE_TYPE (expr));
2171 if (is_positive_overflow_infinity (vr0.min))
2172 max = negative_overflow_infinity (TREE_TYPE (expr));
2173 else if (is_negative_overflow_infinity (vr0.min))
2174 max = positive_overflow_infinity (TREE_TYPE (expr));
2175 else if (!vrp_val_is_min (vr0.min))
2176 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2177 else if (needs_overflow_infinity (TREE_TYPE (expr)))
2179 if (supports_overflow_infinity (TREE_TYPE (expr)))
2180 max = positive_overflow_infinity (TREE_TYPE (expr));
2181 else
2183 set_value_range_to_varying (vr);
2184 return;
2187 else
2188 max = TYPE_MIN_VALUE (TREE_TYPE (expr));
2190 else if (code == NEGATE_EXPR
2191 && TYPE_UNSIGNED (TREE_TYPE (expr)))
2193 if (!range_includes_zero_p (&vr0))
2195 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2196 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2198 else
2200 if (range_is_null (&vr0))
2201 set_value_range_to_null (vr, TREE_TYPE (expr));
2202 else
2203 set_value_range_to_varying (vr);
2204 return;
2207 else if (code == ABS_EXPR
2208 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
2210 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
2211 useful range. */
2212 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (expr))
2213 && ((vr0.type == VR_RANGE
2214 && vrp_val_is_min (vr0.min))
2215 || (vr0.type == VR_ANTI_RANGE
2216 && !vrp_val_is_min (vr0.min)
2217 && !range_includes_zero_p (&vr0))))
2219 set_value_range_to_varying (vr);
2220 return;
2223 /* ABS_EXPR may flip the range around, if the original range
2224 included negative values. */
2225 if (is_overflow_infinity (vr0.min))
2226 min = positive_overflow_infinity (TREE_TYPE (expr));
2227 else if (!vrp_val_is_min (vr0.min))
2228 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2229 else if (!needs_overflow_infinity (TREE_TYPE (expr)))
2230 min = TYPE_MAX_VALUE (TREE_TYPE (expr));
2231 else if (supports_overflow_infinity (TREE_TYPE (expr)))
2232 min = positive_overflow_infinity (TREE_TYPE (expr));
2233 else
2235 set_value_range_to_varying (vr);
2236 return;
2239 if (is_overflow_infinity (vr0.max))
2240 max = positive_overflow_infinity (TREE_TYPE (expr));
2241 else if (!vrp_val_is_min (vr0.max))
2242 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2243 else if (!needs_overflow_infinity (TREE_TYPE (expr)))
2244 max = TYPE_MAX_VALUE (TREE_TYPE (expr));
2245 else if (supports_overflow_infinity (TREE_TYPE (expr)))
2246 max = positive_overflow_infinity (TREE_TYPE (expr));
2247 else
2249 set_value_range_to_varying (vr);
2250 return;
2253 cmp = compare_values (min, max);
2255 /* If a VR_ANTI_RANGEs contains zero, then we have
2256 ~[-INF, min(MIN, MAX)]. */
2257 if (vr0.type == VR_ANTI_RANGE)
2259 if (range_includes_zero_p (&vr0))
2261 /* Take the lower of the two values. */
2262 if (cmp != 1)
2263 max = min;
2265 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
2266 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
2267 flag_wrapv is set and the original anti-range doesn't include
2268 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
2269 if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
2271 tree type_min_value = TYPE_MIN_VALUE (TREE_TYPE (expr));
2273 min = (vr0.min != type_min_value
2274 ? int_const_binop (PLUS_EXPR, type_min_value,
2275 integer_one_node, 0)
2276 : type_min_value);
2278 else
2280 if (overflow_infinity_range_p (&vr0))
2281 min = negative_overflow_infinity (TREE_TYPE (expr));
2282 else
2283 min = TYPE_MIN_VALUE (TREE_TYPE (expr));
2286 else
2288 /* All else has failed, so create the range [0, INF], even for
2289 flag_wrapv since TYPE_MIN_VALUE is in the original
2290 anti-range. */
2291 vr0.type = VR_RANGE;
2292 min = build_int_cst (TREE_TYPE (expr), 0);
2293 if (needs_overflow_infinity (TREE_TYPE (expr)))
2295 if (supports_overflow_infinity (TREE_TYPE (expr)))
2296 max = positive_overflow_infinity (TREE_TYPE (expr));
2297 else
2299 set_value_range_to_varying (vr);
2300 return;
2303 else
2304 max = TYPE_MAX_VALUE (TREE_TYPE (expr));
2308 /* If the range contains zero then we know that the minimum value in the
2309 range will be zero. */
2310 else if (range_includes_zero_p (&vr0))
2312 if (cmp == 1)
2313 max = min;
2314 min = build_int_cst (TREE_TYPE (expr), 0);
2316 else
2318 /* If the range was reversed, swap MIN and MAX. */
2319 if (cmp == 1)
2321 tree t = min;
2322 min = max;
2323 max = t;
2327 else
2329 /* Otherwise, operate on each end of the range. */
2330 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2331 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2333 if (needs_overflow_infinity (TREE_TYPE (expr)))
2335 gcc_assert (code != NEGATE_EXPR && code != ABS_EXPR);
2337 /* If both sides have overflowed, we don't know
2338 anything. */
2339 if ((is_overflow_infinity (vr0.min)
2340 || TREE_OVERFLOW (min))
2341 && (is_overflow_infinity (vr0.max)
2342 || TREE_OVERFLOW (max)))
2344 set_value_range_to_varying (vr);
2345 return;
2348 if (is_overflow_infinity (vr0.min))
2349 min = vr0.min;
2350 else if (TREE_OVERFLOW (min))
2352 if (supports_overflow_infinity (TREE_TYPE (expr)))
2353 min = (tree_int_cst_sgn (min) >= 0
2354 ? positive_overflow_infinity (TREE_TYPE (min))
2355 : negative_overflow_infinity (TREE_TYPE (min)));
2356 else
2358 set_value_range_to_varying (vr);
2359 return;
2363 if (is_overflow_infinity (vr0.max))
2364 max = vr0.max;
2365 else if (TREE_OVERFLOW (max))
2367 if (supports_overflow_infinity (TREE_TYPE (expr)))
2368 max = (tree_int_cst_sgn (max) >= 0
2369 ? positive_overflow_infinity (TREE_TYPE (max))
2370 : negative_overflow_infinity (TREE_TYPE (max)));
2371 else
2373 set_value_range_to_varying (vr);
2374 return;
2380 cmp = compare_values (min, max);
2381 if (cmp == -2 || cmp == 1)
2383 /* If the new range has its limits swapped around (MIN > MAX),
2384 then the operation caused one of them to wrap around, mark
2385 the new range VARYING. */
2386 set_value_range_to_varying (vr);
2388 else
2389 set_value_range (vr, vr0.type, min, max, NULL);
2393 /* Extract range information from a comparison expression EXPR based
2394 on the range of its operand and the expression code. */
2396 static void
2397 extract_range_from_comparison (value_range_t *vr, tree expr)
2399 bool sop = false;
2400 tree val = vrp_evaluate_conditional_warnv (expr, false, &sop);
2402 /* A disadvantage of using a special infinity as an overflow
2403 representation is that we lose the ability to record overflow
2404 when we don't have an infinity. So we have to ignore a result
2405 which relies on overflow. */
2407 if (val && !is_overflow_infinity (val) && !sop)
2409 /* Since this expression was found on the RHS of an assignment,
2410 its type may be different from _Bool. Convert VAL to EXPR's
2411 type. */
2412 val = fold_convert (TREE_TYPE (expr), val);
2413 if (is_gimple_min_invariant (val))
2414 set_value_range_to_value (vr, val, vr->equiv);
2415 else
2416 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
2418 else
2419 set_value_range_to_varying (vr);
2423 /* Try to compute a useful range out of expression EXPR and store it
2424 in *VR. */
2426 static void
2427 extract_range_from_expr (value_range_t *vr, tree expr)
2429 enum tree_code code = TREE_CODE (expr);
2431 if (code == ASSERT_EXPR)
2432 extract_range_from_assert (vr, expr);
2433 else if (code == SSA_NAME)
2434 extract_range_from_ssa_name (vr, expr);
2435 else if (TREE_CODE_CLASS (code) == tcc_binary
2436 || code == TRUTH_ANDIF_EXPR
2437 || code == TRUTH_ORIF_EXPR
2438 || code == TRUTH_AND_EXPR
2439 || code == TRUTH_OR_EXPR
2440 || code == TRUTH_XOR_EXPR)
2441 extract_range_from_binary_expr (vr, expr);
2442 else if (TREE_CODE_CLASS (code) == tcc_unary)
2443 extract_range_from_unary_expr (vr, expr);
2444 else if (TREE_CODE_CLASS (code) == tcc_comparison)
2445 extract_range_from_comparison (vr, expr);
2446 else if (is_gimple_min_invariant (expr))
2447 set_value_range_to_value (vr, expr, NULL);
2448 else
2449 set_value_range_to_varying (vr);
2451 /* If we got a varying range from the tests above, try a final
2452 time to derive a nonnegative or nonzero range. This time
2453 relying primarily on generic routines in fold in conjunction
2454 with range data. */
2455 if (vr->type == VR_VARYING)
2457 bool sop = false;
2459 if (INTEGRAL_TYPE_P (TREE_TYPE (expr))
2460 && vrp_expr_computes_nonnegative (expr, &sop))
2461 set_value_range_to_nonnegative (vr, TREE_TYPE (expr),
2462 sop || is_overflow_infinity (expr));
2463 else if (vrp_expr_computes_nonzero (expr, &sop)
2464 && !sop)
2465 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
2469 /* Given a range VR, a LOOP and a variable VAR, determine whether it
2470 would be profitable to adjust VR using scalar evolution information
2471 for VAR. If so, update VR with the new limits. */
2473 static void
2474 adjust_range_with_scev (value_range_t *vr, struct loop *loop, tree stmt,
2475 tree var)
2477 tree init, step, chrec, tmin, tmax, min, max, type;
2478 enum ev_direction dir;
2480 /* TODO. Don't adjust anti-ranges. An anti-range may provide
2481 better opportunities than a regular range, but I'm not sure. */
2482 if (vr->type == VR_ANTI_RANGE)
2483 return;
2485 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
2486 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2487 return;
2489 /* Don't adjust ranges from pointer CHRECs. */
2490 if (POINTER_TYPE_P (TREE_TYPE (chrec)))
2491 return;
2493 init = initial_condition_in_loop_num (chrec, loop->num);
2494 step = evolution_part_in_loop_num (chrec, loop->num);
2496 /* If STEP is symbolic, we can't know whether INIT will be the
2497 minimum or maximum value in the range. Also, unless INIT is
2498 a simple expression, compare_values and possibly other functions
2499 in tree-vrp won't be able to handle it. */
2500 if (step == NULL_TREE
2501 || !is_gimple_min_invariant (step)
2502 || !valid_value_p (init))
2503 return;
2505 dir = scev_direction (chrec);
2506 if (/* Do not adjust ranges if we do not know whether the iv increases
2507 or decreases, ... */
2508 dir == EV_DIR_UNKNOWN
2509 /* ... or if it may wrap. */
2510 || scev_probably_wraps_p (init, step, stmt,
2511 current_loops->parray[CHREC_VARIABLE (chrec)],
2512 true))
2513 return;
2515 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
2516 negative_overflow_infinity and positive_overflow_infinity,
2517 because we have concluded that the loop probably does not
2518 wrap. */
2520 type = TREE_TYPE (var);
2521 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
2522 tmin = lower_bound_in_type (type, type);
2523 else
2524 tmin = TYPE_MIN_VALUE (type);
2525 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
2526 tmax = upper_bound_in_type (type, type);
2527 else
2528 tmax = TYPE_MAX_VALUE (type);
2530 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2532 min = tmin;
2533 max = tmax;
2535 /* For VARYING or UNDEFINED ranges, just about anything we get
2536 from scalar evolutions should be better. */
2538 if (dir == EV_DIR_DECREASES)
2539 max = init;
2540 else
2541 min = init;
2543 /* If we would create an invalid range, then just assume we
2544 know absolutely nothing. This may be over-conservative,
2545 but it's clearly safe, and should happen only in unreachable
2546 parts of code, or for invalid programs. */
2547 if (compare_values (min, max) == 1)
2548 return;
2550 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2552 else if (vr->type == VR_RANGE)
2554 min = vr->min;
2555 max = vr->max;
2557 if (dir == EV_DIR_DECREASES)
2559 /* INIT is the maximum value. If INIT is lower than VR->MAX
2560 but no smaller than VR->MIN, set VR->MAX to INIT. */
2561 if (compare_values (init, max) == -1)
2563 max = init;
2565 /* If we just created an invalid range with the minimum
2566 greater than the maximum, we fail conservatively.
2567 This should happen only in unreachable
2568 parts of code, or for invalid programs. */
2569 if (compare_values (min, max) == 1)
2570 return;
2573 /* According to the loop information, the variable does not
2574 overflow. If we think it does, probably because of an
2575 overflow due to arithmetic on a different INF value,
2576 reset now. */
2577 if (is_negative_overflow_infinity (min))
2578 min = tmin;
2580 else
2582 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
2583 if (compare_values (init, min) == 1)
2585 min = init;
2587 /* Again, avoid creating invalid range by failing. */
2588 if (compare_values (min, max) == 1)
2589 return;
2592 if (is_positive_overflow_infinity (max))
2593 max = tmax;
2596 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2600 /* Return true if VAR may overflow at STMT. This checks any available
2601 loop information to see if we can determine that VAR does not
2602 overflow. */
2604 static bool
2605 vrp_var_may_overflow (tree var, tree stmt)
2607 struct loop *l;
2608 tree chrec, init, step;
2610 if (current_loops == NULL)
2611 return true;
2613 l = loop_containing_stmt (stmt);
2614 if (l == NULL)
2615 return true;
2617 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
2618 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2619 return true;
2621 init = initial_condition_in_loop_num (chrec, l->num);
2622 step = evolution_part_in_loop_num (chrec, l->num);
2624 if (step == NULL_TREE
2625 || !is_gimple_min_invariant (step)
2626 || !valid_value_p (init))
2627 return true;
2629 /* If we get here, we know something useful about VAR based on the
2630 loop information. If it wraps, it may overflow. */
2632 if (scev_probably_wraps_p (init, step, stmt,
2633 current_loops->parray[CHREC_VARIABLE (chrec)],
2634 true))
2635 return true;
2637 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2639 print_generic_expr (dump_file, var, 0);
2640 fprintf (dump_file, ": loop information indicates does not overflow\n");
2643 return false;
2647 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
2649 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
2650 all the values in the ranges.
2652 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
2654 - Return NULL_TREE if it is not always possible to determine the
2655 value of the comparison.
2657 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
2658 overflow infinity was used in the test. */
2661 static tree
2662 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
2663 bool *strict_overflow_p)
2665 /* VARYING or UNDEFINED ranges cannot be compared. */
2666 if (vr0->type == VR_VARYING
2667 || vr0->type == VR_UNDEFINED
2668 || vr1->type == VR_VARYING
2669 || vr1->type == VR_UNDEFINED)
2670 return NULL_TREE;
2672 /* Anti-ranges need to be handled separately. */
2673 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
2675 /* If both are anti-ranges, then we cannot compute any
2676 comparison. */
2677 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
2678 return NULL_TREE;
2680 /* These comparisons are never statically computable. */
2681 if (comp == GT_EXPR
2682 || comp == GE_EXPR
2683 || comp == LT_EXPR
2684 || comp == LE_EXPR)
2685 return NULL_TREE;
2687 /* Equality can be computed only between a range and an
2688 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
2689 if (vr0->type == VR_RANGE)
2691 /* To simplify processing, make VR0 the anti-range. */
2692 value_range_t *tmp = vr0;
2693 vr0 = vr1;
2694 vr1 = tmp;
2697 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
2699 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
2700 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
2701 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2703 return NULL_TREE;
2706 if (!usable_range_p (vr0, strict_overflow_p)
2707 || !usable_range_p (vr1, strict_overflow_p))
2708 return NULL_TREE;
2710 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
2711 operands around and change the comparison code. */
2712 if (comp == GT_EXPR || comp == GE_EXPR)
2714 value_range_t *tmp;
2715 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
2716 tmp = vr0;
2717 vr0 = vr1;
2718 vr1 = tmp;
2721 if (comp == EQ_EXPR)
2723 /* Equality may only be computed if both ranges represent
2724 exactly one value. */
2725 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
2726 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
2728 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
2729 strict_overflow_p);
2730 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
2731 strict_overflow_p);
2732 if (cmp_min == 0 && cmp_max == 0)
2733 return boolean_true_node;
2734 else if (cmp_min != -2 && cmp_max != -2)
2735 return boolean_false_node;
2737 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
2738 else if (compare_values_warnv (vr0->min, vr1->max,
2739 strict_overflow_p) == 1
2740 || compare_values_warnv (vr1->min, vr0->max,
2741 strict_overflow_p) == 1)
2742 return boolean_false_node;
2744 return NULL_TREE;
2746 else if (comp == NE_EXPR)
2748 int cmp1, cmp2;
2750 /* If VR0 is completely to the left or completely to the right
2751 of VR1, they are always different. Notice that we need to
2752 make sure that both comparisons yield similar results to
2753 avoid comparing values that cannot be compared at
2754 compile-time. */
2755 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
2756 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
2757 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
2758 return boolean_true_node;
2760 /* If VR0 and VR1 represent a single value and are identical,
2761 return false. */
2762 else if (compare_values_warnv (vr0->min, vr0->max,
2763 strict_overflow_p) == 0
2764 && compare_values_warnv (vr1->min, vr1->max,
2765 strict_overflow_p) == 0
2766 && compare_values_warnv (vr0->min, vr1->min,
2767 strict_overflow_p) == 0
2768 && compare_values_warnv (vr0->max, vr1->max,
2769 strict_overflow_p) == 0)
2770 return boolean_false_node;
2772 /* Otherwise, they may or may not be different. */
2773 else
2774 return NULL_TREE;
2776 else if (comp == LT_EXPR || comp == LE_EXPR)
2778 int tst;
2780 /* If VR0 is to the left of VR1, return true. */
2781 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
2782 if ((comp == LT_EXPR && tst == -1)
2783 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2785 if (overflow_infinity_range_p (vr0)
2786 || overflow_infinity_range_p (vr1))
2787 *strict_overflow_p = true;
2788 return boolean_true_node;
2791 /* If VR0 is to the right of VR1, return false. */
2792 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
2793 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2794 || (comp == LE_EXPR && tst == 1))
2796 if (overflow_infinity_range_p (vr0)
2797 || overflow_infinity_range_p (vr1))
2798 *strict_overflow_p = true;
2799 return boolean_false_node;
2802 /* Otherwise, we don't know. */
2803 return NULL_TREE;
2806 gcc_unreachable ();
2810 /* Given a value range VR, a value VAL and a comparison code COMP, return
2811 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
2812 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
2813 always returns false. Return NULL_TREE if it is not always
2814 possible to determine the value of the comparison. Also set
2815 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
2816 infinity was used in the test. */
2818 static tree
2819 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
2820 bool *strict_overflow_p)
2822 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2823 return NULL_TREE;
2825 /* Anti-ranges need to be handled separately. */
2826 if (vr->type == VR_ANTI_RANGE)
2828 /* For anti-ranges, the only predicates that we can compute at
2829 compile time are equality and inequality. */
2830 if (comp == GT_EXPR
2831 || comp == GE_EXPR
2832 || comp == LT_EXPR
2833 || comp == LE_EXPR)
2834 return NULL_TREE;
2836 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
2837 if (value_inside_range (val, vr) == 1)
2838 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2840 return NULL_TREE;
2843 if (!usable_range_p (vr, strict_overflow_p))
2844 return NULL_TREE;
2846 if (comp == EQ_EXPR)
2848 /* EQ_EXPR may only be computed if VR represents exactly
2849 one value. */
2850 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
2852 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
2853 if (cmp == 0)
2854 return boolean_true_node;
2855 else if (cmp == -1 || cmp == 1 || cmp == 2)
2856 return boolean_false_node;
2858 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
2859 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
2860 return boolean_false_node;
2862 return NULL_TREE;
2864 else if (comp == NE_EXPR)
2866 /* If VAL is not inside VR, then they are always different. */
2867 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
2868 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
2869 return boolean_true_node;
2871 /* If VR represents exactly one value equal to VAL, then return
2872 false. */
2873 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
2874 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
2875 return boolean_false_node;
2877 /* Otherwise, they may or may not be different. */
2878 return NULL_TREE;
2880 else if (comp == LT_EXPR || comp == LE_EXPR)
2882 int tst;
2884 /* If VR is to the left of VAL, return true. */
2885 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
2886 if ((comp == LT_EXPR && tst == -1)
2887 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2889 if (overflow_infinity_range_p (vr))
2890 *strict_overflow_p = true;
2891 return boolean_true_node;
2894 /* If VR is to the right of VAL, return false. */
2895 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
2896 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2897 || (comp == LE_EXPR && tst == 1))
2899 if (overflow_infinity_range_p (vr))
2900 *strict_overflow_p = true;
2901 return boolean_false_node;
2904 /* Otherwise, we don't know. */
2905 return NULL_TREE;
2907 else if (comp == GT_EXPR || comp == GE_EXPR)
2909 int tst;
2911 /* If VR is to the right of VAL, return true. */
2912 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
2913 if ((comp == GT_EXPR && tst == 1)
2914 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
2916 if (overflow_infinity_range_p (vr))
2917 *strict_overflow_p = true;
2918 return boolean_true_node;
2921 /* If VR is to the left of VAL, return false. */
2922 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
2923 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
2924 || (comp == GE_EXPR && tst == -1))
2926 if (overflow_infinity_range_p (vr))
2927 *strict_overflow_p = true;
2928 return boolean_false_node;
2931 /* Otherwise, we don't know. */
2932 return NULL_TREE;
2935 gcc_unreachable ();
2939 /* Debugging dumps. */
2941 void dump_value_range (FILE *, value_range_t *);
2942 void debug_value_range (value_range_t *);
2943 void dump_all_value_ranges (FILE *);
2944 void debug_all_value_ranges (void);
2945 void dump_vr_equiv (FILE *, bitmap);
2946 void debug_vr_equiv (bitmap);
2949 /* Dump value range VR to FILE. */
2951 void
2952 dump_value_range (FILE *file, value_range_t *vr)
2954 if (vr == NULL)
2955 fprintf (file, "[]");
2956 else if (vr->type == VR_UNDEFINED)
2957 fprintf (file, "UNDEFINED");
2958 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
2960 tree type = TREE_TYPE (vr->min);
2962 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
2964 if (is_negative_overflow_infinity (vr->min))
2965 fprintf (file, "-INF(OVF)");
2966 else if (INTEGRAL_TYPE_P (type)
2967 && !TYPE_UNSIGNED (type)
2968 && vrp_val_is_min (vr->min))
2969 fprintf (file, "-INF");
2970 else
2971 print_generic_expr (file, vr->min, 0);
2973 fprintf (file, ", ");
2975 if (is_positive_overflow_infinity (vr->max))
2976 fprintf (file, "+INF(OVF)");
2977 else if (INTEGRAL_TYPE_P (type)
2978 && vrp_val_is_max (vr->max))
2979 fprintf (file, "+INF");
2980 else
2981 print_generic_expr (file, vr->max, 0);
2983 fprintf (file, "]");
2985 if (vr->equiv)
2987 bitmap_iterator bi;
2988 unsigned i, c = 0;
2990 fprintf (file, " EQUIVALENCES: { ");
2992 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
2994 print_generic_expr (file, ssa_name (i), 0);
2995 fprintf (file, " ");
2996 c++;
2999 fprintf (file, "} (%u elements)", c);
3002 else if (vr->type == VR_VARYING)
3003 fprintf (file, "VARYING");
3004 else
3005 fprintf (file, "INVALID RANGE");
3009 /* Dump value range VR to stderr. */
3011 void
3012 debug_value_range (value_range_t *vr)
3014 dump_value_range (stderr, vr);
3015 fprintf (stderr, "\n");
3019 /* Dump value ranges of all SSA_NAMEs to FILE. */
3021 void
3022 dump_all_value_ranges (FILE *file)
3024 size_t i;
3026 for (i = 0; i < num_ssa_names; i++)
3028 if (vr_value[i])
3030 print_generic_expr (file, ssa_name (i), 0);
3031 fprintf (file, ": ");
3032 dump_value_range (file, vr_value[i]);
3033 fprintf (file, "\n");
3037 fprintf (file, "\n");
3041 /* Dump all value ranges to stderr. */
3043 void
3044 debug_all_value_ranges (void)
3046 dump_all_value_ranges (stderr);
3050 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
3051 create a new SSA name N and return the assertion assignment
3052 'V = ASSERT_EXPR <V, V OP W>'. */
3054 static tree
3055 build_assert_expr_for (tree cond, tree v)
3057 tree n, assertion;
3059 gcc_assert (TREE_CODE (v) == SSA_NAME);
3060 n = duplicate_ssa_name (v, NULL_TREE);
3062 if (COMPARISON_CLASS_P (cond))
3064 tree a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
3065 assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, a);
3067 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
3069 /* Given !V, build the assignment N = false. */
3070 tree op0 = TREE_OPERAND (cond, 0);
3071 gcc_assert (op0 == v);
3072 assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, boolean_false_node);
3074 else if (TREE_CODE (cond) == SSA_NAME)
3076 /* Given V, build the assignment N = true. */
3077 gcc_assert (v == cond);
3078 assertion = build2 (MODIFY_EXPR, TREE_TYPE (v), n, boolean_true_node);
3080 else
3081 gcc_unreachable ();
3083 SSA_NAME_DEF_STMT (n) = assertion;
3085 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
3086 operand of the ASSERT_EXPR. Register the new name and the old one
3087 in the replacement table so that we can fix the SSA web after
3088 adding all the ASSERT_EXPRs. */
3089 register_new_name_mapping (n, v);
3091 return assertion;
3095 /* Return false if EXPR is a predicate expression involving floating
3096 point values. */
3098 static inline bool
3099 fp_predicate (tree expr)
3101 return (COMPARISON_CLASS_P (expr)
3102 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0))));
3106 /* If the range of values taken by OP can be inferred after STMT executes,
3107 return the comparison code (COMP_CODE_P) and value (VAL_P) that
3108 describes the inferred range. Return true if a range could be
3109 inferred. */
3111 static bool
3112 infer_value_range (tree stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
3114 *val_p = NULL_TREE;
3115 *comp_code_p = ERROR_MARK;
3117 /* Do not attempt to infer anything in names that flow through
3118 abnormal edges. */
3119 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
3120 return false;
3122 /* Similarly, don't infer anything from statements that may throw
3123 exceptions. */
3124 if (tree_could_throw_p (stmt))
3125 return false;
3127 /* If STMT is the last statement of a basic block with no
3128 successors, there is no point inferring anything about any of its
3129 operands. We would not be able to find a proper insertion point
3130 for the assertion, anyway. */
3131 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (bb_for_stmt (stmt)->succs) == 0)
3132 return false;
3134 /* We can only assume that a pointer dereference will yield
3135 non-NULL if -fdelete-null-pointer-checks is enabled. */
3136 if (flag_delete_null_pointer_checks && POINTER_TYPE_P (TREE_TYPE (op)))
3138 bool is_store;
3139 unsigned num_uses, num_derefs;
3141 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs, &is_store);
3142 if (num_derefs > 0)
3144 *val_p = build_int_cst (TREE_TYPE (op), 0);
3145 *comp_code_p = NE_EXPR;
3146 return true;
3150 return false;
3154 void dump_asserts_for (FILE *, tree);
3155 void debug_asserts_for (tree);
3156 void dump_all_asserts (FILE *);
3157 void debug_all_asserts (void);
3159 /* Dump all the registered assertions for NAME to FILE. */
3161 void
3162 dump_asserts_for (FILE *file, tree name)
3164 assert_locus_t loc;
3166 fprintf (file, "Assertions to be inserted for ");
3167 print_generic_expr (file, name, 0);
3168 fprintf (file, "\n");
3170 loc = asserts_for[SSA_NAME_VERSION (name)];
3171 while (loc)
3173 fprintf (file, "\t");
3174 print_generic_expr (file, bsi_stmt (loc->si), 0);
3175 fprintf (file, "\n\tBB #%d", loc->bb->index);
3176 if (loc->e)
3178 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
3179 loc->e->dest->index);
3180 dump_edge_info (file, loc->e, 0);
3182 fprintf (file, "\n\tPREDICATE: ");
3183 print_generic_expr (file, name, 0);
3184 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
3185 print_generic_expr (file, loc->val, 0);
3186 fprintf (file, "\n\n");
3187 loc = loc->next;
3190 fprintf (file, "\n");
3194 /* Dump all the registered assertions for NAME to stderr. */
3196 void
3197 debug_asserts_for (tree name)
3199 dump_asserts_for (stderr, name);
3203 /* Dump all the registered assertions for all the names to FILE. */
3205 void
3206 dump_all_asserts (FILE *file)
3208 unsigned i;
3209 bitmap_iterator bi;
3211 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
3212 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3213 dump_asserts_for (file, ssa_name (i));
3214 fprintf (file, "\n");
3218 /* Dump all the registered assertions for all the names to stderr. */
3220 void
3221 debug_all_asserts (void)
3223 dump_all_asserts (stderr);
3227 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
3228 'NAME COMP_CODE VAL' at a location that dominates block BB or
3229 E->DEST, then register this location as a possible insertion point
3230 for ASSERT_EXPR <NAME, NAME COMP_CODE VAL>.
3232 BB, E and SI provide the exact insertion point for the new
3233 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
3234 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
3235 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
3236 must not be NULL. */
3238 static void
3239 register_new_assert_for (tree name,
3240 enum tree_code comp_code,
3241 tree val,
3242 basic_block bb,
3243 edge e,
3244 block_stmt_iterator si)
3246 assert_locus_t n, loc, last_loc;
3247 bool found;
3248 basic_block dest_bb;
3250 #if defined ENABLE_CHECKING
3251 gcc_assert (bb == NULL || e == NULL);
3253 if (e == NULL)
3254 gcc_assert (TREE_CODE (bsi_stmt (si)) != COND_EXPR
3255 && TREE_CODE (bsi_stmt (si)) != SWITCH_EXPR);
3256 #endif
3258 /* The new assertion A will be inserted at BB or E. We need to
3259 determine if the new location is dominated by a previously
3260 registered location for A. If we are doing an edge insertion,
3261 assume that A will be inserted at E->DEST. Note that this is not
3262 necessarily true.
3264 If E is a critical edge, it will be split. But even if E is
3265 split, the new block will dominate the same set of blocks that
3266 E->DEST dominates.
3268 The reverse, however, is not true, blocks dominated by E->DEST
3269 will not be dominated by the new block created to split E. So,
3270 if the insertion location is on a critical edge, we will not use
3271 the new location to move another assertion previously registered
3272 at a block dominated by E->DEST. */
3273 dest_bb = (bb) ? bb : e->dest;
3275 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
3276 VAL at a block dominating DEST_BB, then we don't need to insert a new
3277 one. Similarly, if the same assertion already exists at a block
3278 dominated by DEST_BB and the new location is not on a critical
3279 edge, then update the existing location for the assertion (i.e.,
3280 move the assertion up in the dominance tree).
3282 Note, this is implemented as a simple linked list because there
3283 should not be more than a handful of assertions registered per
3284 name. If this becomes a performance problem, a table hashed by
3285 COMP_CODE and VAL could be implemented. */
3286 loc = asserts_for[SSA_NAME_VERSION (name)];
3287 last_loc = loc;
3288 found = false;
3289 while (loc)
3291 if (loc->comp_code == comp_code
3292 && (loc->val == val
3293 || operand_equal_p (loc->val, val, 0)))
3295 /* If the assertion NAME COMP_CODE VAL has already been
3296 registered at a basic block that dominates DEST_BB, then
3297 we don't need to insert the same assertion again. Note
3298 that we don't check strict dominance here to avoid
3299 replicating the same assertion inside the same basic
3300 block more than once (e.g., when a pointer is
3301 dereferenced several times inside a block).
3303 An exception to this rule are edge insertions. If the
3304 new assertion is to be inserted on edge E, then it will
3305 dominate all the other insertions that we may want to
3306 insert in DEST_BB. So, if we are doing an edge
3307 insertion, don't do this dominance check. */
3308 if (e == NULL
3309 && dominated_by_p (CDI_DOMINATORS, dest_bb, loc->bb))
3310 return;
3312 /* Otherwise, if E is not a critical edge and DEST_BB
3313 dominates the existing location for the assertion, move
3314 the assertion up in the dominance tree by updating its
3315 location information. */
3316 if ((e == NULL || !EDGE_CRITICAL_P (e))
3317 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
3319 loc->bb = dest_bb;
3320 loc->e = e;
3321 loc->si = si;
3322 return;
3326 /* Update the last node of the list and move to the next one. */
3327 last_loc = loc;
3328 loc = loc->next;
3331 /* If we didn't find an assertion already registered for
3332 NAME COMP_CODE VAL, add a new one at the end of the list of
3333 assertions associated with NAME. */
3334 n = XNEW (struct assert_locus_d);
3335 n->bb = dest_bb;
3336 n->e = e;
3337 n->si = si;
3338 n->comp_code = comp_code;
3339 n->val = val;
3340 n->next = NULL;
3342 if (last_loc)
3343 last_loc->next = n;
3344 else
3345 asserts_for[SSA_NAME_VERSION (name)] = n;
3347 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
3351 /* Try to register an edge assertion for SSA name NAME on edge E for
3352 the conditional jump pointed to by SI. Return true if an assertion
3353 for NAME could be registered. */
3355 static bool
3356 register_edge_assert_for (tree name, edge e, block_stmt_iterator si)
3358 tree val, stmt;
3359 enum tree_code comp_code;
3361 stmt = bsi_stmt (si);
3363 /* Do not attempt to infer anything in names that flow through
3364 abnormal edges. */
3365 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
3366 return false;
3368 /* If NAME was not found in the sub-graph reachable from E, then
3369 there's nothing to do. */
3370 if (!TEST_BIT (found_in_subgraph, SSA_NAME_VERSION (name)))
3371 return false;
3373 /* We found a use of NAME in the sub-graph rooted at E->DEST.
3374 Register an assertion for NAME according to the value that NAME
3375 takes on edge E. */
3376 if (TREE_CODE (stmt) == COND_EXPR)
3378 /* If BB ends in a COND_EXPR then NAME then we should insert
3379 the original predicate on EDGE_TRUE_VALUE and the
3380 opposite predicate on EDGE_FALSE_VALUE. */
3381 tree cond = COND_EXPR_COND (stmt);
3382 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
3384 /* Predicates may be a single SSA name or NAME OP VAL. */
3385 if (cond == name)
3387 /* If the predicate is a name, it must be NAME, in which
3388 case we create the predicate NAME == true or
3389 NAME == false accordingly. */
3390 comp_code = EQ_EXPR;
3391 val = (is_else_edge) ? boolean_false_node : boolean_true_node;
3393 else
3395 /* Otherwise, we have a comparison of the form NAME COMP VAL
3396 or VAL COMP NAME. */
3397 if (name == TREE_OPERAND (cond, 1))
3399 /* If the predicate is of the form VAL COMP NAME, flip
3400 COMP around because we need to register NAME as the
3401 first operand in the predicate. */
3402 comp_code = swap_tree_comparison (TREE_CODE (cond));
3403 val = TREE_OPERAND (cond, 0);
3405 else
3407 /* The comparison is of the form NAME COMP VAL, so the
3408 comparison code remains unchanged. */
3409 comp_code = TREE_CODE (cond);
3410 val = TREE_OPERAND (cond, 1);
3413 /* If we are inserting the assertion on the ELSE edge, we
3414 need to invert the sign comparison. */
3415 if (is_else_edge)
3416 comp_code = invert_tree_comparison (comp_code, 0);
3418 /* Do not register always-false predicates. FIXME, this
3419 works around a limitation in fold() when dealing with
3420 enumerations. Given 'enum { N1, N2 } x;', fold will not
3421 fold 'if (x > N2)' to 'if (0)'. */
3422 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
3423 && (INTEGRAL_TYPE_P (TREE_TYPE (val))
3424 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (val))))
3426 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
3427 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
3429 if (comp_code == GT_EXPR && compare_values (val, max) == 0)
3430 return false;
3432 if (comp_code == LT_EXPR && compare_values (val, min) == 0)
3433 return false;
3437 else
3439 /* FIXME. Handle SWITCH_EXPR. */
3440 gcc_unreachable ();
3443 register_new_assert_for (name, comp_code, val, NULL, e, si);
3444 return true;
3448 static bool find_assert_locations (basic_block bb);
3450 /* Determine whether the outgoing edges of BB should receive an
3451 ASSERT_EXPR for each of the operands of BB's last statement. The
3452 last statement of BB must be a COND_EXPR or a SWITCH_EXPR.
3454 If any of the sub-graphs rooted at BB have an interesting use of
3455 the predicate operands, an assert location node is added to the
3456 list of assertions for the corresponding operands. */
3458 static bool
3459 find_conditional_asserts (basic_block bb)
3461 bool need_assert;
3462 block_stmt_iterator last_si;
3463 tree op, last;
3464 edge_iterator ei;
3465 edge e;
3466 ssa_op_iter iter;
3468 need_assert = false;
3469 last_si = bsi_last (bb);
3470 last = bsi_stmt (last_si);
3472 /* Look for uses of the operands in each of the sub-graphs
3473 rooted at BB. We need to check each of the outgoing edges
3474 separately, so that we know what kind of ASSERT_EXPR to
3475 insert. */
3476 FOR_EACH_EDGE (e, ei, bb->succs)
3478 if (e->dest == bb)
3479 continue;
3481 /* Remove the COND_EXPR operands from the FOUND_IN_SUBGRAPH bitmap.
3482 Otherwise, when we finish traversing each of the sub-graphs, we
3483 won't know whether the variables were found in the sub-graphs or
3484 if they had been found in a block upstream from BB.
3486 This is actually a bad idea is some cases, particularly jump
3487 threading. Consider a CFG like the following:
3497 Assume that one or more operands in the conditional at the
3498 end of block 0 are used in a conditional in block 2, but not
3499 anywhere in block 1. In this case we will not insert any
3500 assert statements in block 1, which may cause us to miss
3501 opportunities to optimize, particularly for jump threading. */
3502 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3503 RESET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3505 /* Traverse the strictly dominated sub-graph rooted at E->DEST
3506 to determine if any of the operands in the conditional
3507 predicate are used. */
3508 if (e->dest != bb)
3509 need_assert |= find_assert_locations (e->dest);
3511 /* Register the necessary assertions for each operand in the
3512 conditional predicate. */
3513 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3514 need_assert |= register_edge_assert_for (op, e, last_si);
3517 /* Finally, indicate that we have found the operands in the
3518 conditional. */
3519 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3520 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3522 return need_assert;
3526 /* Traverse all the statements in block BB looking for statements that
3527 may generate useful assertions for the SSA names in their operand.
3528 If a statement produces a useful assertion A for name N_i, then the
3529 list of assertions already generated for N_i is scanned to
3530 determine if A is actually needed.
3532 If N_i already had the assertion A at a location dominating the
3533 current location, then nothing needs to be done. Otherwise, the
3534 new location for A is recorded instead.
3536 1- For every statement S in BB, all the variables used by S are
3537 added to bitmap FOUND_IN_SUBGRAPH.
3539 2- If statement S uses an operand N in a way that exposes a known
3540 value range for N, then if N was not already generated by an
3541 ASSERT_EXPR, create a new assert location for N. For instance,
3542 if N is a pointer and the statement dereferences it, we can
3543 assume that N is not NULL.
3545 3- COND_EXPRs are a special case of #2. We can derive range
3546 information from the predicate but need to insert different
3547 ASSERT_EXPRs for each of the sub-graphs rooted at the
3548 conditional block. If the last statement of BB is a conditional
3549 expression of the form 'X op Y', then
3551 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3553 b) If the conditional is the only entry point to the sub-graph
3554 corresponding to the THEN_CLAUSE, recurse into it. On
3555 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3556 an ASSERT_EXPR is added for the corresponding variable.
3558 c) Repeat step (b) on the ELSE_CLAUSE.
3560 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3562 For instance,
3564 if (a == 9)
3565 b = a;
3566 else
3567 b = c + 1;
3569 In this case, an assertion on the THEN clause is useful to
3570 determine that 'a' is always 9 on that edge. However, an assertion
3571 on the ELSE clause would be unnecessary.
3573 4- If BB does not end in a conditional expression, then we recurse
3574 into BB's dominator children.
3576 At the end of the recursive traversal, every SSA name will have a
3577 list of locations where ASSERT_EXPRs should be added. When a new
3578 location for name N is found, it is registered by calling
3579 register_new_assert_for. That function keeps track of all the
3580 registered assertions to prevent adding unnecessary assertions.
3581 For instance, if a pointer P_4 is dereferenced more than once in a
3582 dominator tree, only the location dominating all the dereference of
3583 P_4 will receive an ASSERT_EXPR.
3585 If this function returns true, then it means that there are names
3586 for which we need to generate ASSERT_EXPRs. Those assertions are
3587 inserted by process_assert_insertions.
3589 TODO. Handle SWITCH_EXPR. */
3591 static bool
3592 find_assert_locations (basic_block bb)
3594 block_stmt_iterator si;
3595 tree last, phi;
3596 bool need_assert;
3597 basic_block son;
3599 if (TEST_BIT (blocks_visited, bb->index))
3600 return false;
3602 SET_BIT (blocks_visited, bb->index);
3604 need_assert = false;
3606 /* Traverse all PHI nodes in BB marking used operands. */
3607 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3609 use_operand_p arg_p;
3610 ssa_op_iter i;
3612 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
3614 tree arg = USE_FROM_PTR (arg_p);
3615 if (TREE_CODE (arg) == SSA_NAME)
3617 gcc_assert (is_gimple_reg (PHI_RESULT (phi)));
3618 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (arg));
3623 /* Traverse all the statements in BB marking used names and looking
3624 for statements that may infer assertions for their used operands. */
3625 last = NULL_TREE;
3626 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3628 tree stmt, op;
3629 ssa_op_iter i;
3631 stmt = bsi_stmt (si);
3633 /* See if we can derive an assertion for any of STMT's operands. */
3634 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
3636 tree value;
3637 enum tree_code comp_code;
3639 /* Mark OP in bitmap FOUND_IN_SUBGRAPH. If STMT is inside
3640 the sub-graph of a conditional block, when we return from
3641 this recursive walk, our parent will use the
3642 FOUND_IN_SUBGRAPH bitset to determine if one of the
3643 operands it was looking for was present in the sub-graph. */
3644 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3646 /* If OP is used in such a way that we can infer a value
3647 range for it, and we don't find a previous assertion for
3648 it, create a new assertion location node for OP. */
3649 if (infer_value_range (stmt, op, &comp_code, &value))
3651 /* If we are able to infer a nonzero value range for OP,
3652 then walk backwards through the use-def chain to see if OP
3653 was set via a typecast.
3655 If so, then we can also infer a nonzero value range
3656 for the operand of the NOP_EXPR. */
3657 if (comp_code == NE_EXPR && integer_zerop (value))
3659 tree t = op;
3660 tree def_stmt = SSA_NAME_DEF_STMT (t);
3662 while (TREE_CODE (def_stmt) == MODIFY_EXPR
3663 && TREE_CODE (TREE_OPERAND (def_stmt, 1)) == NOP_EXPR
3664 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0)) == SSA_NAME
3665 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0))))
3667 t = TREE_OPERAND (TREE_OPERAND (def_stmt, 1), 0);
3668 def_stmt = SSA_NAME_DEF_STMT (t);
3670 /* Note we want to register the assert for the
3671 operand of the NOP_EXPR after SI, not after the
3672 conversion. */
3673 if (! has_single_use (t))
3675 register_new_assert_for (t, comp_code, value,
3676 bb, NULL, si);
3677 need_assert = true;
3682 /* If OP is used only once, namely in this STMT, don't
3683 bother creating an ASSERT_EXPR for it. Such an
3684 ASSERT_EXPR would do nothing but increase compile time. */
3685 if (!has_single_use (op))
3687 register_new_assert_for (op, comp_code, value, bb, NULL, si);
3688 need_assert = true;
3693 /* Remember the last statement of the block. */
3694 last = stmt;
3697 /* If BB's last statement is a conditional expression
3698 involving integer operands, recurse into each of the sub-graphs
3699 rooted at BB to determine if we need to add ASSERT_EXPRs. */
3700 if (last
3701 && TREE_CODE (last) == COND_EXPR
3702 && !fp_predicate (COND_EXPR_COND (last))
3703 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
3704 need_assert |= find_conditional_asserts (bb);
3706 /* Recurse into the dominator children of BB. */
3707 for (son = first_dom_son (CDI_DOMINATORS, bb);
3708 son;
3709 son = next_dom_son (CDI_DOMINATORS, son))
3710 need_assert |= find_assert_locations (son);
3712 return need_assert;
3716 /* Create an ASSERT_EXPR for NAME and insert it in the location
3717 indicated by LOC. Return true if we made any edge insertions. */
3719 static bool
3720 process_assert_insertions_for (tree name, assert_locus_t loc)
3722 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3723 tree stmt, cond, assert_expr;
3724 edge_iterator ei;
3725 edge e;
3727 cond = build2 (loc->comp_code, boolean_type_node, name, loc->val);
3728 assert_expr = build_assert_expr_for (cond, name);
3730 if (loc->e)
3732 /* We have been asked to insert the assertion on an edge. This
3733 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3734 #if defined ENABLE_CHECKING
3735 gcc_assert (TREE_CODE (bsi_stmt (loc->si)) == COND_EXPR
3736 || TREE_CODE (bsi_stmt (loc->si)) == SWITCH_EXPR);
3737 #endif
3739 bsi_insert_on_edge (loc->e, assert_expr);
3740 return true;
3743 /* Otherwise, we can insert right after LOC->SI iff the
3744 statement must not be the last statement in the block. */
3745 stmt = bsi_stmt (loc->si);
3746 if (!stmt_ends_bb_p (stmt))
3748 bsi_insert_after (&loc->si, assert_expr, BSI_SAME_STMT);
3749 return false;
3752 /* If STMT must be the last statement in BB, we can only insert new
3753 assertions on the non-abnormal edge out of BB. Note that since
3754 STMT is not control flow, there may only be one non-abnormal edge
3755 out of BB. */
3756 FOR_EACH_EDGE (e, ei, loc->bb->succs)
3757 if (!(e->flags & EDGE_ABNORMAL))
3759 bsi_insert_on_edge (e, assert_expr);
3760 return true;
3763 gcc_unreachable ();
3767 /* Process all the insertions registered for every name N_i registered
3768 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3769 found in ASSERTS_FOR[i]. */
3771 static void
3772 process_assert_insertions (void)
3774 unsigned i;
3775 bitmap_iterator bi;
3776 bool update_edges_p = false;
3777 int num_asserts = 0;
3779 if (dump_file && (dump_flags & TDF_DETAILS))
3780 dump_all_asserts (dump_file);
3782 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3784 assert_locus_t loc = asserts_for[i];
3785 gcc_assert (loc);
3787 while (loc)
3789 assert_locus_t next = loc->next;
3790 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
3791 free (loc);
3792 loc = next;
3793 num_asserts++;
3797 if (update_edges_p)
3798 bsi_commit_edge_inserts ();
3800 if (dump_file && (dump_flags & TDF_STATS))
3801 fprintf (dump_file, "\nNumber of ASSERT_EXPR expressions inserted: %d\n\n",
3802 num_asserts);
3806 /* Traverse the flowgraph looking for conditional jumps to insert range
3807 expressions. These range expressions are meant to provide information
3808 to optimizations that need to reason in terms of value ranges. They
3809 will not be expanded into RTL. For instance, given:
3811 x = ...
3812 y = ...
3813 if (x < y)
3814 y = x - 2;
3815 else
3816 x = y + 3;
3818 this pass will transform the code into:
3820 x = ...
3821 y = ...
3822 if (x < y)
3824 x = ASSERT_EXPR <x, x < y>
3825 y = x - 2
3827 else
3829 y = ASSERT_EXPR <y, x <= y>
3830 x = y + 3
3833 The idea is that once copy and constant propagation have run, other
3834 optimizations will be able to determine what ranges of values can 'x'
3835 take in different paths of the code, simply by checking the reaching
3836 definition of 'x'. */
3838 static void
3839 insert_range_assertions (void)
3841 edge e;
3842 edge_iterator ei;
3843 bool update_ssa_p;
3845 found_in_subgraph = sbitmap_alloc (num_ssa_names);
3846 sbitmap_zero (found_in_subgraph);
3848 blocks_visited = sbitmap_alloc (last_basic_block);
3849 sbitmap_zero (blocks_visited);
3851 need_assert_for = BITMAP_ALLOC (NULL);
3852 asserts_for = XNEWVEC (assert_locus_t, num_ssa_names);
3853 memset (asserts_for, 0, num_ssa_names * sizeof (assert_locus_t));
3855 calculate_dominance_info (CDI_DOMINATORS);
3857 update_ssa_p = false;
3858 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3859 if (find_assert_locations (e->dest))
3860 update_ssa_p = true;
3862 if (update_ssa_p)
3864 process_assert_insertions ();
3865 update_ssa (TODO_update_ssa_no_phi);
3868 if (dump_file && (dump_flags & TDF_DETAILS))
3870 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
3871 dump_function_to_file (current_function_decl, dump_file, dump_flags);
3874 sbitmap_free (found_in_subgraph);
3875 free (asserts_for);
3876 BITMAP_FREE (need_assert_for);
3880 /* Convert range assertion expressions into the implied copies and
3881 copy propagate away the copies. Doing the trivial copy propagation
3882 here avoids the need to run the full copy propagation pass after
3883 VRP.
3885 FIXME, this will eventually lead to copy propagation removing the
3886 names that had useful range information attached to them. For
3887 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
3888 then N_i will have the range [3, +INF].
3890 However, by converting the assertion into the implied copy
3891 operation N_i = N_j, we will then copy-propagate N_j into the uses
3892 of N_i and lose the range information. We may want to hold on to
3893 ASSERT_EXPRs a little while longer as the ranges could be used in
3894 things like jump threading.
3896 The problem with keeping ASSERT_EXPRs around is that passes after
3897 VRP need to handle them appropriately.
3899 Another approach would be to make the range information a first
3900 class property of the SSA_NAME so that it can be queried from
3901 any pass. This is made somewhat more complex by the need for
3902 multiple ranges to be associated with one SSA_NAME. */
3904 static void
3905 remove_range_assertions (void)
3907 basic_block bb;
3908 block_stmt_iterator si;
3910 /* Note that the BSI iterator bump happens at the bottom of the
3911 loop and no bump is necessary if we're removing the statement
3912 referenced by the current BSI. */
3913 FOR_EACH_BB (bb)
3914 for (si = bsi_start (bb); !bsi_end_p (si);)
3916 tree stmt = bsi_stmt (si);
3917 tree use_stmt;
3919 if (TREE_CODE (stmt) == MODIFY_EXPR
3920 && TREE_CODE (TREE_OPERAND (stmt, 1)) == ASSERT_EXPR)
3922 tree rhs = TREE_OPERAND (stmt, 1), var;
3923 tree cond = fold (ASSERT_EXPR_COND (rhs));
3924 use_operand_p use_p;
3925 imm_use_iterator iter;
3927 gcc_assert (cond != boolean_false_node);
3929 /* Propagate the RHS into every use of the LHS. */
3930 var = ASSERT_EXPR_VAR (rhs);
3931 FOR_EACH_IMM_USE_STMT (use_stmt, iter, TREE_OPERAND (stmt, 0))
3932 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3934 SET_USE (use_p, var);
3935 gcc_assert (TREE_CODE (var) == SSA_NAME);
3938 /* And finally, remove the copy, it is not needed. */
3939 bsi_remove (&si, true);
3941 else
3942 bsi_next (&si);
3945 sbitmap_free (blocks_visited);
3949 /* Return true if STMT is interesting for VRP. */
3951 static bool
3952 stmt_interesting_for_vrp (tree stmt)
3954 if (TREE_CODE (stmt) == PHI_NODE
3955 && is_gimple_reg (PHI_RESULT (stmt))
3956 && (INTEGRAL_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))
3957 || POINTER_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))))
3958 return true;
3959 else if (TREE_CODE (stmt) == MODIFY_EXPR)
3961 tree lhs = TREE_OPERAND (stmt, 0);
3962 tree rhs = TREE_OPERAND (stmt, 1);
3964 /* In general, assignments with virtual operands are not useful
3965 for deriving ranges, with the obvious exception of calls to
3966 builtin functions. */
3967 if (TREE_CODE (lhs) == SSA_NAME
3968 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
3969 || POINTER_TYPE_P (TREE_TYPE (lhs)))
3970 && ((TREE_CODE (rhs) == CALL_EXPR
3971 && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
3972 && DECL_P (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
3973 && DECL_IS_BUILTIN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
3974 || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS)))
3975 return true;
3977 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
3978 return true;
3980 return false;
3984 /* Initialize local data structures for VRP. */
3986 static void
3987 vrp_initialize (void)
3989 basic_block bb;
3991 vr_value = XNEWVEC (value_range_t *, num_ssa_names);
3992 memset (vr_value, 0, num_ssa_names * sizeof (value_range_t *));
3994 FOR_EACH_BB (bb)
3996 block_stmt_iterator si;
3997 tree phi;
3999 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4001 if (!stmt_interesting_for_vrp (phi))
4003 tree lhs = PHI_RESULT (phi);
4004 set_value_range_to_varying (get_value_range (lhs));
4005 DONT_SIMULATE_AGAIN (phi) = true;
4007 else
4008 DONT_SIMULATE_AGAIN (phi) = false;
4011 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
4013 tree stmt = bsi_stmt (si);
4015 if (!stmt_interesting_for_vrp (stmt))
4017 ssa_op_iter i;
4018 tree def;
4019 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
4020 set_value_range_to_varying (get_value_range (def));
4021 DONT_SIMULATE_AGAIN (stmt) = true;
4023 else
4025 DONT_SIMULATE_AGAIN (stmt) = false;
4032 /* Visit assignment STMT. If it produces an interesting range, record
4033 the SSA name in *OUTPUT_P. */
4035 static enum ssa_prop_result
4036 vrp_visit_assignment (tree stmt, tree *output_p)
4038 tree lhs, rhs, def;
4039 ssa_op_iter iter;
4041 lhs = TREE_OPERAND (stmt, 0);
4042 rhs = TREE_OPERAND (stmt, 1);
4044 /* We only keep track of ranges in integral and pointer types. */
4045 if (TREE_CODE (lhs) == SSA_NAME
4046 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4047 /* It is valid to have NULL MIN/MAX values on a type. See
4048 build_range_type. */
4049 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
4050 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
4051 || POINTER_TYPE_P (TREE_TYPE (lhs))))
4053 struct loop *l;
4054 value_range_t new_vr = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
4056 extract_range_from_expr (&new_vr, rhs);
4058 /* If STMT is inside a loop, we may be able to know something
4059 else about the range of LHS by examining scalar evolution
4060 information. */
4061 if (current_loops && (l = loop_containing_stmt (stmt)))
4062 adjust_range_with_scev (&new_vr, l, stmt, lhs);
4064 if (update_value_range (lhs, &new_vr))
4066 *output_p = lhs;
4068 if (dump_file && (dump_flags & TDF_DETAILS))
4070 fprintf (dump_file, "Found new range for ");
4071 print_generic_expr (dump_file, lhs, 0);
4072 fprintf (dump_file, ": ");
4073 dump_value_range (dump_file, &new_vr);
4074 fprintf (dump_file, "\n\n");
4077 if (new_vr.type == VR_VARYING)
4078 return SSA_PROP_VARYING;
4080 return SSA_PROP_INTERESTING;
4083 return SSA_PROP_NOT_INTERESTING;
4086 /* Every other statement produces no useful ranges. */
4087 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
4088 set_value_range_to_varying (get_value_range (def));
4090 return SSA_PROP_VARYING;
4094 /* Compare all the value ranges for names equivalent to VAR with VAL
4095 using comparison code COMP. Return the same value returned by
4096 compare_range_with_value, including the setting of
4097 *STRICT_OVERFLOW_P. */
4099 static tree
4100 compare_name_with_value (enum tree_code comp, tree var, tree val,
4101 bool *strict_overflow_p)
4103 bitmap_iterator bi;
4104 unsigned i;
4105 bitmap e;
4106 tree retval, t;
4107 int used_strict_overflow;
4109 t = retval = NULL_TREE;
4111 /* Get the set of equivalences for VAR. */
4112 e = get_value_range (var)->equiv;
4114 /* Add VAR to its own set of equivalences so that VAR's value range
4115 is processed by this loop (otherwise, we would have to replicate
4116 the body of the loop just to check VAR's value range). */
4117 bitmap_set_bit (e, SSA_NAME_VERSION (var));
4119 /* Start at -1. Set it to 0 if we do a comparison without relying
4120 on overflow, or 1 if all comparisons rely on overflow. */
4121 used_strict_overflow = -1;
4123 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
4125 bool sop;
4127 value_range_t equiv_vr = *(vr_value[i]);
4129 /* If name N_i does not have a valid range, use N_i as its own
4130 range. This allows us to compare against names that may
4131 have N_i in their ranges. */
4132 if (equiv_vr.type == VR_VARYING || equiv_vr.type == VR_UNDEFINED)
4134 equiv_vr.type = VR_RANGE;
4135 equiv_vr.min = ssa_name (i);
4136 equiv_vr.max = ssa_name (i);
4139 sop = false;
4140 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
4141 if (t)
4143 /* If we get different answers from different members
4144 of the equivalence set this check must be in a dead
4145 code region. Folding it to a trap representation
4146 would be correct here. For now just return don't-know. */
4147 if (retval != NULL
4148 && t != retval)
4150 retval = NULL_TREE;
4151 break;
4153 retval = t;
4155 if (!sop)
4156 used_strict_overflow = 0;
4157 else if (used_strict_overflow < 0)
4158 used_strict_overflow = 1;
4162 /* Remove VAR from its own equivalence set. */
4163 bitmap_clear_bit (e, SSA_NAME_VERSION (var));
4165 if (retval)
4167 if (used_strict_overflow > 0)
4168 *strict_overflow_p = true;
4169 return retval;
4172 /* We couldn't find a non-NULL value for the predicate. */
4173 return NULL_TREE;
4177 /* Given a comparison code COMP and names N1 and N2, compare all the
4178 ranges equivalent to N1 against all the ranges equivalent to N2
4179 to determine the value of N1 COMP N2. Return the same value
4180 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
4181 whether we relied on an overflow infinity in the comparison. */
4184 static tree
4185 compare_names (enum tree_code comp, tree n1, tree n2,
4186 bool *strict_overflow_p)
4188 tree t, retval;
4189 bitmap e1, e2;
4190 bitmap_iterator bi1, bi2;
4191 unsigned i1, i2;
4192 int used_strict_overflow;
4194 /* Compare the ranges of every name equivalent to N1 against the
4195 ranges of every name equivalent to N2. */
4196 e1 = get_value_range (n1)->equiv;
4197 e2 = get_value_range (n2)->equiv;
4199 /* Add N1 and N2 to their own set of equivalences to avoid
4200 duplicating the body of the loop just to check N1 and N2
4201 ranges. */
4202 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
4203 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
4205 /* If the equivalence sets have a common intersection, then the two
4206 names can be compared without checking their ranges. */
4207 if (bitmap_intersect_p (e1, e2))
4209 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4210 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4212 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
4213 ? boolean_true_node
4214 : boolean_false_node;
4217 /* Start at -1. Set it to 0 if we do a comparison without relying
4218 on overflow, or 1 if all comparisons rely on overflow. */
4219 used_strict_overflow = -1;
4221 /* Otherwise, compare all the equivalent ranges. First, add N1 and
4222 N2 to their own set of equivalences to avoid duplicating the body
4223 of the loop just to check N1 and N2 ranges. */
4224 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
4226 value_range_t vr1 = *(vr_value[i1]);
4228 /* If the range is VARYING or UNDEFINED, use the name itself. */
4229 if (vr1.type == VR_VARYING || vr1.type == VR_UNDEFINED)
4231 vr1.type = VR_RANGE;
4232 vr1.min = ssa_name (i1);
4233 vr1.max = ssa_name (i1);
4236 t = retval = NULL_TREE;
4237 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
4239 bool sop = false;
4241 value_range_t vr2 = *(vr_value[i2]);
4243 if (vr2.type == VR_VARYING || vr2.type == VR_UNDEFINED)
4245 vr2.type = VR_RANGE;
4246 vr2.min = ssa_name (i2);
4247 vr2.max = ssa_name (i2);
4250 t = compare_ranges (comp, &vr1, &vr2, &sop);
4251 if (t)
4253 /* If we get different answers from different members
4254 of the equivalence set this check must be in a dead
4255 code region. Folding it to a trap representation
4256 would be correct here. For now just return don't-know. */
4257 if (retval != NULL
4258 && t != retval)
4260 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4261 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4262 return NULL_TREE;
4264 retval = t;
4266 if (!sop)
4267 used_strict_overflow = 0;
4268 else if (used_strict_overflow < 0)
4269 used_strict_overflow = 1;
4273 if (retval)
4275 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4276 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4277 if (used_strict_overflow > 0)
4278 *strict_overflow_p = true;
4279 return retval;
4283 /* None of the equivalent ranges are useful in computing this
4284 comparison. */
4285 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4286 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4287 return NULL_TREE;
4291 /* Given a conditional predicate COND, try to determine if COND yields
4292 true or false based on the value ranges of its operands. Return
4293 BOOLEAN_TRUE_NODE if the conditional always evaluates to true,
4294 BOOLEAN_FALSE_NODE if the conditional always evaluates to false, and,
4295 NULL if the conditional cannot be evaluated at compile time.
4297 If USE_EQUIV_P is true, the ranges of all the names equivalent with
4298 the operands in COND are used when trying to compute its value.
4299 This is only used during final substitution. During propagation,
4300 we only check the range of each variable and not its equivalents.
4302 Set *STRICT_OVERFLOW_P to indicate whether we relied on an overflow
4303 infinity to produce the result. */
4305 static tree
4306 vrp_evaluate_conditional_warnv (tree cond, bool use_equiv_p,
4307 bool *strict_overflow_p)
4309 gcc_assert (TREE_CODE (cond) == SSA_NAME
4310 || TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison);
4312 if (TREE_CODE (cond) == SSA_NAME)
4314 value_range_t *vr;
4315 tree retval;
4317 if (use_equiv_p)
4318 retval = compare_name_with_value (NE_EXPR, cond, boolean_false_node,
4319 strict_overflow_p);
4320 else
4322 value_range_t *vr = get_value_range (cond);
4323 retval = compare_range_with_value (NE_EXPR, vr, boolean_false_node,
4324 strict_overflow_p);
4327 /* If COND has a known boolean range, return it. */
4328 if (retval)
4329 return retval;
4331 /* Otherwise, if COND has a symbolic range of exactly one value,
4332 return it. */
4333 vr = get_value_range (cond);
4334 if (vr->type == VR_RANGE && vr->min == vr->max)
4335 return vr->min;
4337 else
4339 tree op0 = TREE_OPERAND (cond, 0);
4340 tree op1 = TREE_OPERAND (cond, 1);
4342 /* We only deal with integral and pointer types. */
4343 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
4344 && !POINTER_TYPE_P (TREE_TYPE (op0)))
4345 return NULL_TREE;
4347 if (use_equiv_p)
4349 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
4350 return compare_names (TREE_CODE (cond), op0, op1,
4351 strict_overflow_p);
4352 else if (TREE_CODE (op0) == SSA_NAME)
4353 return compare_name_with_value (TREE_CODE (cond), op0, op1,
4354 strict_overflow_p);
4355 else if (TREE_CODE (op1) == SSA_NAME)
4356 return (compare_name_with_value
4357 (swap_tree_comparison (TREE_CODE (cond)), op1, op0,
4358 strict_overflow_p));
4360 else
4362 value_range_t *vr0, *vr1;
4364 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
4365 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
4367 if (vr0 && vr1)
4368 return compare_ranges (TREE_CODE (cond), vr0, vr1,
4369 strict_overflow_p);
4370 else if (vr0 && vr1 == NULL)
4371 return compare_range_with_value (TREE_CODE (cond), vr0, op1,
4372 strict_overflow_p);
4373 else if (vr0 == NULL && vr1)
4374 return (compare_range_with_value
4375 (swap_tree_comparison (TREE_CODE (cond)), vr1, op0,
4376 strict_overflow_p));
4380 /* Anything else cannot be computed statically. */
4381 return NULL_TREE;
4384 /* Given COND within STMT, try to simplify it based on value range
4385 information. Return NULL if the conditional can not be evaluated.
4386 The ranges of all the names equivalent with the operands in COND
4387 will be used when trying to compute the value. If the result is
4388 based on undefined signed overflow, issue a warning if
4389 appropriate. */
4391 tree
4392 vrp_evaluate_conditional (tree cond, tree stmt)
4394 bool sop;
4395 tree ret;
4397 sop = false;
4398 ret = vrp_evaluate_conditional_warnv (cond, true, &sop);
4400 if (ret && sop)
4402 enum warn_strict_overflow_code wc;
4403 const char* warnmsg;
4405 if (is_gimple_min_invariant (ret))
4407 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
4408 warnmsg = G_("assuming signed overflow does not occur when "
4409 "simplifying conditional to constant");
4411 else
4413 wc = WARN_STRICT_OVERFLOW_COMPARISON;
4414 warnmsg = G_("assuming signed overflow does not occur when "
4415 "simplifying conditional");
4418 if (issue_strict_overflow_warning (wc))
4420 location_t locus;
4422 if (!EXPR_HAS_LOCATION (stmt))
4423 locus = input_location;
4424 else
4425 locus = EXPR_LOCATION (stmt);
4426 warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
4430 return ret;
4434 /* Visit conditional statement STMT. If we can determine which edge
4435 will be taken out of STMT's basic block, record it in
4436 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
4437 SSA_PROP_VARYING. */
4439 static enum ssa_prop_result
4440 vrp_visit_cond_stmt (tree stmt, edge *taken_edge_p)
4442 tree cond, val;
4443 bool sop;
4445 *taken_edge_p = NULL;
4447 /* FIXME. Handle SWITCH_EXPRs. But first, the assert pass needs to
4448 add ASSERT_EXPRs for them. */
4449 if (TREE_CODE (stmt) == SWITCH_EXPR)
4450 return SSA_PROP_VARYING;
4452 cond = COND_EXPR_COND (stmt);
4454 if (dump_file && (dump_flags & TDF_DETAILS))
4456 tree use;
4457 ssa_op_iter i;
4459 fprintf (dump_file, "\nVisiting conditional with predicate: ");
4460 print_generic_expr (dump_file, cond, 0);
4461 fprintf (dump_file, "\nWith known ranges\n");
4463 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
4465 fprintf (dump_file, "\t");
4466 print_generic_expr (dump_file, use, 0);
4467 fprintf (dump_file, ": ");
4468 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
4471 fprintf (dump_file, "\n");
4474 /* Compute the value of the predicate COND by checking the known
4475 ranges of each of its operands.
4477 Note that we cannot evaluate all the equivalent ranges here
4478 because those ranges may not yet be final and with the current
4479 propagation strategy, we cannot determine when the value ranges
4480 of the names in the equivalence set have changed.
4482 For instance, given the following code fragment
4484 i_5 = PHI <8, i_13>
4486 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
4487 if (i_14 == 1)
4490 Assume that on the first visit to i_14, i_5 has the temporary
4491 range [8, 8] because the second argument to the PHI function is
4492 not yet executable. We derive the range ~[0, 0] for i_14 and the
4493 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
4494 the first time, since i_14 is equivalent to the range [8, 8], we
4495 determine that the predicate is always false.
4497 On the next round of propagation, i_13 is determined to be
4498 VARYING, which causes i_5 to drop down to VARYING. So, another
4499 visit to i_14 is scheduled. In this second visit, we compute the
4500 exact same range and equivalence set for i_14, namely ~[0, 0] and
4501 { i_5 }. But we did not have the previous range for i_5
4502 registered, so vrp_visit_assignment thinks that the range for
4503 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
4504 is not visited again, which stops propagation from visiting
4505 statements in the THEN clause of that if().
4507 To properly fix this we would need to keep the previous range
4508 value for the names in the equivalence set. This way we would've
4509 discovered that from one visit to the other i_5 changed from
4510 range [8, 8] to VR_VARYING.
4512 However, fixing this apparent limitation may not be worth the
4513 additional checking. Testing on several code bases (GCC, DLV,
4514 MICO, TRAMP3D and SPEC2000) showed that doing this results in
4515 4 more predicates folded in SPEC. */
4516 sop = false;
4517 val = vrp_evaluate_conditional_warnv (cond, false, &sop);
4518 if (val)
4520 if (!sop)
4521 *taken_edge_p = find_taken_edge (bb_for_stmt (stmt), val);
4522 else
4524 if (dump_file && (dump_flags & TDF_DETAILS))
4525 fprintf (dump_file,
4526 "\nIgnoring predicate evaluation because "
4527 "it assumes that signed overflow is undefined");
4528 val = NULL_TREE;
4532 if (dump_file && (dump_flags & TDF_DETAILS))
4534 fprintf (dump_file, "\nPredicate evaluates to: ");
4535 if (val == NULL_TREE)
4536 fprintf (dump_file, "DON'T KNOW\n");
4537 else
4538 print_generic_stmt (dump_file, val, 0);
4541 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
4545 /* Evaluate statement STMT. If the statement produces a useful range,
4546 return SSA_PROP_INTERESTING and record the SSA name with the
4547 interesting range into *OUTPUT_P.
4549 If STMT is a conditional branch and we can determine its truth
4550 value, the taken edge is recorded in *TAKEN_EDGE_P.
4552 If STMT produces a varying value, return SSA_PROP_VARYING. */
4554 static enum ssa_prop_result
4555 vrp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
4557 tree def;
4558 ssa_op_iter iter;
4559 stmt_ann_t ann;
4561 if (dump_file && (dump_flags & TDF_DETAILS))
4563 fprintf (dump_file, "\nVisiting statement:\n");
4564 print_generic_stmt (dump_file, stmt, dump_flags);
4565 fprintf (dump_file, "\n");
4568 ann = stmt_ann (stmt);
4569 if (TREE_CODE (stmt) == MODIFY_EXPR)
4571 tree rhs = TREE_OPERAND (stmt, 1);
4573 /* In general, assignments with virtual operands are not useful
4574 for deriving ranges, with the obvious exception of calls to
4575 builtin functions. */
4576 if ((TREE_CODE (rhs) == CALL_EXPR
4577 && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
4578 && DECL_P (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
4579 && DECL_IS_BUILTIN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
4580 || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
4581 return vrp_visit_assignment (stmt, output_p);
4583 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
4584 return vrp_visit_cond_stmt (stmt, taken_edge_p);
4586 /* All other statements produce nothing of interest for VRP, so mark
4587 their outputs varying and prevent further simulation. */
4588 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
4589 set_value_range_to_varying (get_value_range (def));
4591 return SSA_PROP_VARYING;
4595 /* Meet operation for value ranges. Given two value ranges VR0 and
4596 VR1, store in VR0 the result of meeting VR0 and VR1.
4598 The meeting rules are as follows:
4600 1- If VR0 and VR1 have an empty intersection, set VR0 to VR_VARYING.
4602 2- If VR0 and VR1 have a non-empty intersection, set VR0 to the
4603 union of VR0 and VR1. */
4605 static void
4606 vrp_meet (value_range_t *vr0, value_range_t *vr1)
4608 if (vr0->type == VR_UNDEFINED)
4610 copy_value_range (vr0, vr1);
4611 return;
4614 if (vr1->type == VR_UNDEFINED)
4616 /* Nothing to do. VR0 already has the resulting range. */
4617 return;
4620 if (vr0->type == VR_VARYING)
4622 /* Nothing to do. VR0 already has the resulting range. */
4623 return;
4626 if (vr1->type == VR_VARYING)
4628 set_value_range_to_varying (vr0);
4629 return;
4632 if (vr0->type == VR_RANGE && vr1->type == VR_RANGE)
4634 /* If VR0 and VR1 have a non-empty intersection, compute the
4635 union of both ranges. */
4636 if (value_ranges_intersect_p (vr0, vr1))
4638 int cmp;
4639 tree min, max;
4641 /* The lower limit of the new range is the minimum of the
4642 two ranges. If they cannot be compared, the result is
4643 VARYING. */
4644 cmp = compare_values (vr0->min, vr1->min);
4645 if (cmp == 0 || cmp == 1)
4646 min = vr1->min;
4647 else if (cmp == -1)
4648 min = vr0->min;
4649 else
4651 set_value_range_to_varying (vr0);
4652 return;
4655 /* Similarly, the upper limit of the new range is the
4656 maximum of the two ranges. If they cannot be compared,
4657 the result is VARYING. */
4658 cmp = compare_values (vr0->max, vr1->max);
4659 if (cmp == 0 || cmp == -1)
4660 max = vr1->max;
4661 else if (cmp == 1)
4662 max = vr0->max;
4663 else
4665 set_value_range_to_varying (vr0);
4666 return;
4669 /* Check for useless ranges. */
4670 if (INTEGRAL_TYPE_P (TREE_TYPE (min))
4671 && ((vrp_val_is_min (min) || is_overflow_infinity (min))
4672 && (vrp_val_is_max (max) || is_overflow_infinity (max))))
4674 set_value_range_to_varying (vr0);
4675 return;
4678 /* The resulting set of equivalences is the intersection of
4679 the two sets. */
4680 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4681 bitmap_and_into (vr0->equiv, vr1->equiv);
4682 else if (vr0->equiv && !vr1->equiv)
4683 bitmap_clear (vr0->equiv);
4685 set_value_range (vr0, vr0->type, min, max, vr0->equiv);
4687 else
4688 goto no_meet;
4690 else if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4692 /* Two anti-ranges meet only if they are both identical. */
4693 if (compare_values (vr0->min, vr1->min) == 0
4694 && compare_values (vr0->max, vr1->max) == 0
4695 && compare_values (vr0->min, vr0->max) == 0)
4697 /* The resulting set of equivalences is the intersection of
4698 the two sets. */
4699 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4700 bitmap_and_into (vr0->equiv, vr1->equiv);
4701 else if (vr0->equiv && !vr1->equiv)
4702 bitmap_clear (vr0->equiv);
4704 else
4705 goto no_meet;
4707 else if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4709 /* A numeric range [VAL1, VAL2] and an anti-range ~[VAL3, VAL4]
4710 meet only if the ranges have an empty intersection. The
4711 result of the meet operation is the anti-range. */
4712 if (!symbolic_range_p (vr0)
4713 && !symbolic_range_p (vr1)
4714 && !value_ranges_intersect_p (vr0, vr1))
4716 /* Copy most of VR1 into VR0. Don't copy VR1's equivalence
4717 set. We need to compute the intersection of the two
4718 equivalence sets. */
4719 if (vr1->type == VR_ANTI_RANGE)
4720 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr0->equiv);
4722 /* The resulting set of equivalences is the intersection of
4723 the two sets. */
4724 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4725 bitmap_and_into (vr0->equiv, vr1->equiv);
4726 else if (vr0->equiv && !vr1->equiv)
4727 bitmap_clear (vr0->equiv);
4729 else
4730 goto no_meet;
4732 else
4733 gcc_unreachable ();
4735 return;
4737 no_meet:
4738 /* The two range VR0 and VR1 do not meet. Before giving up and
4739 setting the result to VARYING, see if we can at least derive a
4740 useful anti-range. FIXME, all this nonsense about distinguishing
4741 anti-ranges from ranges is necessary because of the odd
4742 semantics of range_includes_zero_p and friends. */
4743 if (!symbolic_range_p (vr0)
4744 && ((vr0->type == VR_RANGE && !range_includes_zero_p (vr0))
4745 || (vr0->type == VR_ANTI_RANGE && range_includes_zero_p (vr0)))
4746 && !symbolic_range_p (vr1)
4747 && ((vr1->type == VR_RANGE && !range_includes_zero_p (vr1))
4748 || (vr1->type == VR_ANTI_RANGE && range_includes_zero_p (vr1))))
4750 set_value_range_to_nonnull (vr0, TREE_TYPE (vr0->min));
4752 /* Since this meet operation did not result from the meeting of
4753 two equivalent names, VR0 cannot have any equivalences. */
4754 if (vr0->equiv)
4755 bitmap_clear (vr0->equiv);
4757 else
4758 set_value_range_to_varying (vr0);
4762 /* Visit all arguments for PHI node PHI that flow through executable
4763 edges. If a valid value range can be derived from all the incoming
4764 value ranges, set a new range for the LHS of PHI. */
4766 static enum ssa_prop_result
4767 vrp_visit_phi_node (tree phi)
4769 int i;
4770 tree lhs = PHI_RESULT (phi);
4771 value_range_t *lhs_vr = get_value_range (lhs);
4772 value_range_t vr_result = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
4774 copy_value_range (&vr_result, lhs_vr);
4776 if (dump_file && (dump_flags & TDF_DETAILS))
4778 fprintf (dump_file, "\nVisiting PHI node: ");
4779 print_generic_expr (dump_file, phi, dump_flags);
4782 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
4784 edge e = PHI_ARG_EDGE (phi, i);
4786 if (dump_file && (dump_flags & TDF_DETAILS))
4788 fprintf (dump_file,
4789 "\n Argument #%d (%d -> %d %sexecutable)\n",
4790 i, e->src->index, e->dest->index,
4791 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
4794 if (e->flags & EDGE_EXECUTABLE)
4796 tree arg = PHI_ARG_DEF (phi, i);
4797 value_range_t vr_arg;
4799 if (TREE_CODE (arg) == SSA_NAME)
4800 vr_arg = *(get_value_range (arg));
4801 else
4803 if (is_overflow_infinity (arg))
4805 arg = copy_node (arg);
4806 TREE_OVERFLOW (arg) = 0;
4809 vr_arg.type = VR_RANGE;
4810 vr_arg.min = arg;
4811 vr_arg.max = arg;
4812 vr_arg.equiv = NULL;
4815 if (dump_file && (dump_flags & TDF_DETAILS))
4817 fprintf (dump_file, "\t");
4818 print_generic_expr (dump_file, arg, dump_flags);
4819 fprintf (dump_file, "\n\tValue: ");
4820 dump_value_range (dump_file, &vr_arg);
4821 fprintf (dump_file, "\n");
4824 vrp_meet (&vr_result, &vr_arg);
4826 if (vr_result.type == VR_VARYING)
4827 break;
4831 if (vr_result.type == VR_VARYING)
4832 goto varying;
4834 /* To prevent infinite iterations in the algorithm, derive ranges
4835 when the new value is slightly bigger or smaller than the
4836 previous one. */
4837 if (lhs_vr->type == VR_RANGE && vr_result.type == VR_RANGE)
4839 if (!POINTER_TYPE_P (TREE_TYPE (lhs)))
4841 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
4842 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
4844 /* If the new minimum is smaller or larger than the previous
4845 one, go all the way to -INF. In the first case, to avoid
4846 iterating millions of times to reach -INF, and in the
4847 other case to avoid infinite bouncing between different
4848 minimums. */
4849 if (cmp_min > 0 || cmp_min < 0)
4851 /* If we will end up with a (-INF, +INF) range, set it
4852 to VARYING. */
4853 if (vrp_val_is_max (vr_result.max))
4854 goto varying;
4856 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
4857 || !vrp_var_may_overflow (lhs, phi))
4858 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
4859 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
4860 vr_result.min =
4861 negative_overflow_infinity (TREE_TYPE (vr_result.min));
4862 else
4863 goto varying;
4866 /* Similarly, if the new maximum is smaller or larger than
4867 the previous one, go all the way to +INF. */
4868 if (cmp_max < 0 || cmp_max > 0)
4870 /* If we will end up with a (-INF, +INF) range, set it
4871 to VARYING. */
4872 if (vrp_val_is_min (vr_result.min))
4873 goto varying;
4875 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
4876 || !vrp_var_may_overflow (lhs, phi))
4877 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
4878 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
4879 vr_result.max =
4880 positive_overflow_infinity (TREE_TYPE (vr_result.max));
4881 else
4882 goto varying;
4887 /* If the new range is different than the previous value, keep
4888 iterating. */
4889 if (update_value_range (lhs, &vr_result))
4890 return SSA_PROP_INTERESTING;
4892 /* Nothing changed, don't add outgoing edges. */
4893 return SSA_PROP_NOT_INTERESTING;
4895 /* No match found. Set the LHS to VARYING. */
4896 varying:
4897 set_value_range_to_varying (lhs_vr);
4898 return SSA_PROP_VARYING;
4901 /* Simplify a division or modulo operator to a right shift or
4902 bitwise and if the first operand is unsigned or is greater
4903 than zero and the second operand is an exact power of two. */
4905 static void
4906 simplify_div_or_mod_using_ranges (tree stmt, tree rhs, enum tree_code rhs_code)
4908 tree val = NULL;
4909 tree op = TREE_OPERAND (rhs, 0);
4910 value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
4912 if (TYPE_UNSIGNED (TREE_TYPE (op)))
4914 val = integer_one_node;
4916 else
4918 bool sop = false;
4920 val = compare_range_with_value (GT_EXPR, vr, integer_zero_node, &sop);
4922 if (val
4923 && sop
4924 && integer_onep (val)
4925 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
4927 location_t locus;
4929 if (!EXPR_HAS_LOCATION (stmt))
4930 locus = input_location;
4931 else
4932 locus = EXPR_LOCATION (stmt);
4933 warning (OPT_Wstrict_overflow,
4934 ("%Hassuming signed overflow does not occur when "
4935 "simplifying / or %% to >> or &"),
4936 &locus);
4940 if (val && integer_onep (val))
4942 tree t;
4943 tree op0 = TREE_OPERAND (rhs, 0);
4944 tree op1 = TREE_OPERAND (rhs, 1);
4946 if (rhs_code == TRUNC_DIV_EXPR)
4948 t = build_int_cst (NULL_TREE, tree_log2 (op1));
4949 t = build2 (RSHIFT_EXPR, TREE_TYPE (op0), op0, t);
4951 else
4953 t = build_int_cst (TREE_TYPE (op1), 1);
4954 t = int_const_binop (MINUS_EXPR, op1, t, 0);
4955 t = fold_convert (TREE_TYPE (op0), t);
4956 t = build2 (BIT_AND_EXPR, TREE_TYPE (op0), op0, t);
4959 TREE_OPERAND (stmt, 1) = t;
4960 update_stmt (stmt);
4964 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
4965 ABS_EXPR. If the operand is <= 0, then simplify the
4966 ABS_EXPR into a NEGATE_EXPR. */
4968 static void
4969 simplify_abs_using_ranges (tree stmt, tree rhs)
4971 tree val = NULL;
4972 tree op = TREE_OPERAND (rhs, 0);
4973 tree type = TREE_TYPE (op);
4974 value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
4976 if (TYPE_UNSIGNED (type))
4978 val = integer_zero_node;
4980 else if (vr)
4982 bool sop = false;
4984 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
4985 if (!val)
4987 sop = false;
4988 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
4989 &sop);
4991 if (val)
4993 if (integer_zerop (val))
4994 val = integer_one_node;
4995 else if (integer_onep (val))
4996 val = integer_zero_node;
5000 if (val
5001 && (integer_onep (val) || integer_zerop (val)))
5003 tree t;
5005 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
5007 location_t locus;
5009 if (!EXPR_HAS_LOCATION (stmt))
5010 locus = input_location;
5011 else
5012 locus = EXPR_LOCATION (stmt);
5013 warning (OPT_Wstrict_overflow,
5014 ("%Hassuming signed overflow does not occur when "
5015 "simplifying abs (X) to X or -X"),
5016 &locus);
5019 if (integer_onep (val))
5020 t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
5021 else
5022 t = op;
5024 TREE_OPERAND (stmt, 1) = t;
5025 update_stmt (stmt);
5030 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
5031 a known value range VR.
5033 If there is one and only one value which will satisfy the
5034 conditional, then return that value. Else return NULL. */
5036 static tree
5037 test_for_singularity (enum tree_code cond_code, tree op0,
5038 tree op1, value_range_t *vr)
5040 tree min = NULL;
5041 tree max = NULL;
5043 /* Extract minimum/maximum values which satisfy the
5044 the conditional as it was written. */
5045 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
5047 /* This should not be negative infinity; there is no overflow
5048 here. */
5049 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
5051 max = op1;
5052 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
5054 tree one = build_int_cst (TREE_TYPE (op0), 1);
5055 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
5056 if (EXPR_P (max))
5057 TREE_NO_WARNING (max) = 1;
5060 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
5062 /* This should not be positive infinity; there is no overflow
5063 here. */
5064 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
5066 min = op1;
5067 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
5069 tree one = build_int_cst (TREE_TYPE (op0), 1);
5070 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
5071 if (EXPR_P (min))
5072 TREE_NO_WARNING (min) = 1;
5076 /* Now refine the minimum and maximum values using any
5077 value range information we have for op0. */
5078 if (min && max)
5080 if (compare_values (vr->min, min) == -1)
5081 min = min;
5082 else
5083 min = vr->min;
5084 if (compare_values (vr->max, max) == 1)
5085 max = max;
5086 else
5087 max = vr->max;
5089 /* If the new min/max values have converged to a single value,
5090 then there is only one value which can satisfy the condition,
5091 return that value. */
5092 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
5093 return min;
5095 return NULL;
5098 /* Simplify a conditional using a relational operator to an equality
5099 test if the range information indicates only one value can satisfy
5100 the original conditional. */
5102 static void
5103 simplify_cond_using_ranges (tree stmt)
5105 tree cond = COND_EXPR_COND (stmt);
5106 tree op0 = TREE_OPERAND (cond, 0);
5107 tree op1 = TREE_OPERAND (cond, 1);
5108 enum tree_code cond_code = TREE_CODE (cond);
5110 if (cond_code != NE_EXPR
5111 && cond_code != EQ_EXPR
5112 && TREE_CODE (op0) == SSA_NAME
5113 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
5114 && is_gimple_min_invariant (op1))
5116 value_range_t *vr = get_value_range (op0);
5118 /* If we have range information for OP0, then we might be
5119 able to simplify this conditional. */
5120 if (vr->type == VR_RANGE)
5122 tree new = test_for_singularity (cond_code, op0, op1, vr);
5124 if (new)
5126 if (dump_file)
5128 fprintf (dump_file, "Simplified relational ");
5129 print_generic_expr (dump_file, cond, 0);
5130 fprintf (dump_file, " into ");
5133 COND_EXPR_COND (stmt)
5134 = build2 (EQ_EXPR, boolean_type_node, op0, new);
5135 update_stmt (stmt);
5137 if (dump_file)
5139 print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
5140 fprintf (dump_file, "\n");
5142 return;
5146 /* Try again after inverting the condition. We only deal
5147 with integral types here, so no need to worry about
5148 issues with inverting FP comparisons. */
5149 cond_code = invert_tree_comparison (cond_code, false);
5150 new = test_for_singularity (cond_code, op0, op1, vr);
5152 if (new)
5154 if (dump_file)
5156 fprintf (dump_file, "Simplified relational ");
5157 print_generic_expr (dump_file, cond, 0);
5158 fprintf (dump_file, " into ");
5161 COND_EXPR_COND (stmt)
5162 = build2 (NE_EXPR, boolean_type_node, op0, new);
5163 update_stmt (stmt);
5165 if (dump_file)
5167 print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
5168 fprintf (dump_file, "\n");
5170 return;
5177 /* Simplify STMT using ranges if possible. */
5179 void
5180 simplify_stmt_using_ranges (tree stmt)
5182 if (TREE_CODE (stmt) == MODIFY_EXPR)
5184 tree rhs = TREE_OPERAND (stmt, 1);
5185 enum tree_code rhs_code = TREE_CODE (rhs);
5187 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
5188 and BIT_AND_EXPR respectively if the first operand is greater
5189 than zero and the second operand is an exact power of two. */
5190 if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
5191 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
5192 && integer_pow2p (TREE_OPERAND (rhs, 1)))
5193 simplify_div_or_mod_using_ranges (stmt, rhs, rhs_code);
5195 /* Transform ABS (X) into X or -X as appropriate. */
5196 if (rhs_code == ABS_EXPR
5197 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
5198 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
5199 simplify_abs_using_ranges (stmt, rhs);
5201 else if (TREE_CODE (stmt) == COND_EXPR
5202 && COMPARISON_CLASS_P (COND_EXPR_COND (stmt)))
5204 simplify_cond_using_ranges (stmt);
5208 /* Stack of dest,src equivalency pairs that need to be restored after
5209 each attempt to thread a block's incoming edge to an outgoing edge.
5211 A NULL entry is used to mark the end of pairs which need to be
5212 restored. */
5213 static VEC(tree,heap) *stack;
5215 /* A trivial wrapper so that we can present the generic jump threading
5216 code with a simple API for simplifying statements. STMT is the
5217 statement we want to simplify, WITHIN_STMT provides the location
5218 for any overflow warnings. */
5220 static tree
5221 simplify_stmt_for_jump_threading (tree stmt, tree within_stmt)
5223 /* We only use VRP information to simplify conditionals. This is
5224 overly conservative, but it's unclear if doing more would be
5225 worth the compile time cost. */
5226 if (TREE_CODE (stmt) != COND_EXPR)
5227 return NULL;
5229 return vrp_evaluate_conditional (COND_EXPR_COND (stmt), within_stmt);
5232 /* Blocks which have more than one predecessor and more than
5233 one successor present jump threading opportunities. ie,
5234 when the block is reached from a specific predecessor, we
5235 may be able to determine which of the outgoing edges will
5236 be traversed. When this optimization applies, we are able
5237 to avoid conditionals at runtime and we may expose secondary
5238 optimization opportunities.
5240 This routine is effectively a driver for the generic jump
5241 threading code. It basically just presents the generic code
5242 with edges that may be suitable for jump threading.
5244 Unlike DOM, we do not iterate VRP if jump threading was successful.
5245 While iterating may expose new opportunities for VRP, it is expected
5246 those opportunities would be very limited and the compile time cost
5247 to expose those opportunities would be significant.
5249 As jump threading opportunities are discovered, they are registered
5250 for later realization. */
5252 static void
5253 identify_jump_threads (void)
5255 basic_block bb;
5256 tree dummy;
5258 /* Ugh. When substituting values earlier in this pass we can
5259 wipe the dominance information. So rebuild the dominator
5260 information as we need it within the jump threading code. */
5261 calculate_dominance_info (CDI_DOMINATORS);
5263 /* We do not allow VRP information to be used for jump threading
5264 across a back edge in the CFG. Otherwise it becomes too
5265 difficult to avoid eliminating loop exit tests. Of course
5266 EDGE_DFS_BACK is not accurate at this time so we have to
5267 recompute it. */
5268 mark_dfs_back_edges ();
5270 /* Allocate our unwinder stack to unwind any temporary equivalences
5271 that might be recorded. */
5272 stack = VEC_alloc (tree, heap, 20);
5274 /* To avoid lots of silly node creation, we create a single
5275 conditional and just modify it in-place when attempting to
5276 thread jumps. */
5277 dummy = build2 (EQ_EXPR, boolean_type_node, NULL, NULL);
5278 dummy = build3 (COND_EXPR, void_type_node, dummy, NULL, NULL);
5280 /* Walk through all the blocks finding those which present a
5281 potential jump threading opportunity. We could set this up
5282 as a dominator walker and record data during the walk, but
5283 I doubt it's worth the effort for the classes of jump
5284 threading opportunities we are trying to identify at this
5285 point in compilation. */
5286 FOR_EACH_BB (bb)
5288 tree last, cond;
5290 /* If the generic jump threading code does not find this block
5291 interesting, then there is nothing to do. */
5292 if (! potentially_threadable_block (bb))
5293 continue;
5295 /* We only care about blocks ending in a COND_EXPR. While there
5296 may be some value in handling SWITCH_EXPR here, I doubt it's
5297 terribly important. */
5298 last = bsi_stmt (bsi_last (bb));
5299 if (TREE_CODE (last) != COND_EXPR)
5300 continue;
5302 /* We're basically looking for any kind of conditional with
5303 integral type arguments. */
5304 cond = COND_EXPR_COND (last);
5305 if ((TREE_CODE (cond) == SSA_NAME
5306 && INTEGRAL_TYPE_P (TREE_TYPE (cond)))
5307 || (COMPARISON_CLASS_P (cond)
5308 && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
5309 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 0)))
5310 && (TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME
5311 || is_gimple_min_invariant (TREE_OPERAND (cond, 1)))
5312 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 1)))))
5314 edge_iterator ei;
5315 edge e;
5317 /* We've got a block with multiple predecessors and multiple
5318 successors which also ends in a suitable conditional. For
5319 each predecessor, see if we can thread it to a specific
5320 successor. */
5321 FOR_EACH_EDGE (e, ei, bb->preds)
5323 /* Do not thread across back edges or abnormal edges
5324 in the CFG. */
5325 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
5326 continue;
5328 thread_across_edge (dummy, e, true,
5329 &stack,
5330 simplify_stmt_for_jump_threading);
5335 /* We do not actually update the CFG or SSA graphs at this point as
5336 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
5337 handle ASSERT_EXPRs gracefully. */
5340 /* We identified all the jump threading opportunities earlier, but could
5341 not transform the CFG at that time. This routine transforms the
5342 CFG and arranges for the dominator tree to be rebuilt if necessary.
5344 Note the SSA graph update will occur during the normal TODO
5345 processing by the pass manager. */
5346 static void
5347 finalize_jump_threads (void)
5349 bool cfg_altered = false;
5350 cfg_altered = thread_through_all_blocks ();
5352 /* If we threaded jumps, then we need to recompute the dominance
5353 information, to safely do that we must clean up the CFG first. */
5354 if (cfg_altered)
5356 free_dominance_info (CDI_DOMINATORS);
5357 cleanup_tree_cfg ();
5358 calculate_dominance_info (CDI_DOMINATORS);
5360 VEC_free (tree, heap, stack);
5364 /* Traverse all the blocks folding conditionals with known ranges. */
5366 static void
5367 vrp_finalize (void)
5369 size_t i;
5370 prop_value_t *single_val_range;
5371 bool do_value_subst_p;
5373 if (dump_file)
5375 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
5376 dump_all_value_ranges (dump_file);
5377 fprintf (dump_file, "\n");
5380 /* We may have ended with ranges that have exactly one value. Those
5381 values can be substituted as any other copy/const propagated
5382 value using substitute_and_fold. */
5383 single_val_range = XNEWVEC (prop_value_t, num_ssa_names);
5384 memset (single_val_range, 0, num_ssa_names * sizeof (*single_val_range));
5386 do_value_subst_p = false;
5387 for (i = 0; i < num_ssa_names; i++)
5388 if (vr_value[i]
5389 && vr_value[i]->type == VR_RANGE
5390 && vr_value[i]->min == vr_value[i]->max)
5392 single_val_range[i].value = vr_value[i]->min;
5393 do_value_subst_p = true;
5396 if (!do_value_subst_p)
5398 /* We found no single-valued ranges, don't waste time trying to
5399 do single value substitution in substitute_and_fold. */
5400 free (single_val_range);
5401 single_val_range = NULL;
5404 substitute_and_fold (single_val_range, true);
5406 /* We must identify jump threading opportunities before we release
5407 the datastructures built by VRP. */
5408 identify_jump_threads ();
5410 /* Free allocated memory. */
5411 for (i = 0; i < num_ssa_names; i++)
5412 if (vr_value[i])
5414 BITMAP_FREE (vr_value[i]->equiv);
5415 free (vr_value[i]);
5418 free (single_val_range);
5419 free (vr_value);
5421 /* So that we can distinguish between VRP data being available
5422 and not available. */
5423 vr_value = NULL;
5427 /* Main entry point to VRP (Value Range Propagation). This pass is
5428 loosely based on J. R. C. Patterson, ``Accurate Static Branch
5429 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
5430 Programming Language Design and Implementation, pp. 67-78, 1995.
5431 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
5433 This is essentially an SSA-CCP pass modified to deal with ranges
5434 instead of constants.
5436 While propagating ranges, we may find that two or more SSA name
5437 have equivalent, though distinct ranges. For instance,
5439 1 x_9 = p_3->a;
5440 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
5441 3 if (p_4 == q_2)
5442 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
5443 5 endif
5444 6 if (q_2)
5446 In the code above, pointer p_5 has range [q_2, q_2], but from the
5447 code we can also determine that p_5 cannot be NULL and, if q_2 had
5448 a non-varying range, p_5's range should also be compatible with it.
5450 These equivalences are created by two expressions: ASSERT_EXPR and
5451 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
5452 result of another assertion, then we can use the fact that p_5 and
5453 p_4 are equivalent when evaluating p_5's range.
5455 Together with value ranges, we also propagate these equivalences
5456 between names so that we can take advantage of information from
5457 multiple ranges when doing final replacement. Note that this
5458 equivalency relation is transitive but not symmetric.
5460 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
5461 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
5462 in contexts where that assertion does not hold (e.g., in line 6).
5464 TODO, the main difference between this pass and Patterson's is that
5465 we do not propagate edge probabilities. We only compute whether
5466 edges can be taken or not. That is, instead of having a spectrum
5467 of jump probabilities between 0 and 1, we only deal with 0, 1 and
5468 DON'T KNOW. In the future, it may be worthwhile to propagate
5469 probabilities to aid branch prediction. */
5471 static unsigned int
5472 execute_vrp (void)
5474 insert_range_assertions ();
5476 current_loops = loop_optimizer_init (LOOPS_NORMAL);
5477 if (current_loops)
5478 scev_initialize (current_loops);
5480 vrp_initialize ();
5481 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
5482 vrp_finalize ();
5484 if (current_loops)
5486 scev_finalize ();
5487 loop_optimizer_finalize (current_loops);
5488 current_loops = NULL;
5491 /* ASSERT_EXPRs must be removed before finalizing jump threads
5492 as finalizing jump threads calls the CFG cleanup code which
5493 does not properly handle ASSERT_EXPRs. */
5494 remove_range_assertions ();
5496 /* If we exposed any new variables, go ahead and put them into
5497 SSA form now, before we handle jump threading. This simplifies
5498 interactions between rewriting of _DECL nodes into SSA form
5499 and rewriting SSA_NAME nodes into SSA form after block
5500 duplication and CFG manipulation. */
5501 update_ssa (TODO_update_ssa);
5503 finalize_jump_threads ();
5504 return 0;
5507 static bool
5508 gate_vrp (void)
5510 return flag_tree_vrp != 0;
5513 struct tree_opt_pass pass_vrp =
5515 "vrp", /* name */
5516 gate_vrp, /* gate */
5517 execute_vrp, /* execute */
5518 NULL, /* sub */
5519 NULL, /* next */
5520 0, /* static_pass_number */
5521 TV_TREE_VRP, /* tv_id */
5522 PROP_ssa | PROP_alias, /* properties_required */
5523 0, /* properties_provided */
5524 PROP_smt_usage, /* properties_destroyed */
5525 0, /* todo_flags_start */
5526 TODO_cleanup_cfg
5527 | TODO_ggc_collect
5528 | TODO_verify_ssa
5529 | TODO_dump_func
5530 | TODO_update_ssa
5531 | TODO_update_smt_usage, /* todo_flags_finish */
5532 0 /* letter */