2007-03-05 Matthias Klose <doko@debian.org>
[official-gcc.git] / gcc / tree-vrp.c
blobfd5ba643db09ed8fe3ab7cc2d08191c5e0fc010f
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "flags.h"
28 #include "tree.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
33 #include "timevar.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "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 void vrp_meet (value_range_t *, value_range_t *);
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)));
198 /* Return true if ARG is marked with the nonnull attribute in the
199 current function signature. */
201 static bool
202 nonnull_arg_p (tree arg)
204 tree t, attrs, fntype;
205 unsigned HOST_WIDE_INT arg_num;
207 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
209 /* The static chain decl is always non null. */
210 if (arg == cfun->static_chain_decl)
211 return true;
213 fntype = TREE_TYPE (current_function_decl);
214 attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype));
216 /* If "nonnull" wasn't specified, we know nothing about the argument. */
217 if (attrs == NULL_TREE)
218 return false;
220 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
221 if (TREE_VALUE (attrs) == NULL_TREE)
222 return true;
224 /* Get the position number for ARG in the function signature. */
225 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
227 t = TREE_CHAIN (t), arg_num++)
229 if (t == arg)
230 break;
233 gcc_assert (t == arg);
235 /* Now see if ARG_NUM is mentioned in the nonnull list. */
236 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
238 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
239 return true;
242 return false;
246 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
248 static void
249 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
250 tree max, bitmap equiv)
252 #if defined ENABLE_CHECKING
253 /* Check the validity of the range. */
254 if (t == VR_RANGE || t == VR_ANTI_RANGE)
256 int cmp;
258 gcc_assert (min && max);
260 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
261 gcc_assert ((min != TYPE_MIN_VALUE (TREE_TYPE (min))
262 && !is_negative_overflow_infinity (min))
263 || (max != TYPE_MAX_VALUE (TREE_TYPE (max))
264 && !is_positive_overflow_infinity (max)));
266 cmp = compare_values (min, max);
267 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
270 if (t == VR_UNDEFINED || t == VR_VARYING)
271 gcc_assert (min == NULL_TREE && max == NULL_TREE);
273 if (t == VR_UNDEFINED || t == VR_VARYING)
274 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
275 #endif
277 vr->type = t;
278 vr->min = min;
279 vr->max = max;
281 /* Since updating the equivalence set involves deep copying the
282 bitmaps, only do it if absolutely necessary. */
283 if (vr->equiv == NULL)
284 vr->equiv = BITMAP_ALLOC (NULL);
286 if (equiv != vr->equiv)
288 if (equiv && !bitmap_empty_p (equiv))
289 bitmap_copy (vr->equiv, equiv);
290 else
291 bitmap_clear (vr->equiv);
296 /* Copy value range FROM into value range TO. */
298 static inline void
299 copy_value_range (value_range_t *to, value_range_t *from)
301 set_value_range (to, from->type, from->min, from->max, from->equiv);
305 /* Set value range VR to VR_VARYING. */
307 static inline void
308 set_value_range_to_varying (value_range_t *vr)
310 vr->type = VR_VARYING;
311 vr->min = vr->max = NULL_TREE;
312 if (vr->equiv)
313 bitmap_clear (vr->equiv);
316 /* Set value range VR to a non-negative range of type TYPE.
317 OVERFLOW_INFINITY indicates whether to use a overflow infinity
318 rather than TYPE_MAX_VALUE; this should be true if we determine
319 that the range is nonnegative based on the assumption that signed
320 overflow does not occur. */
322 static inline void
323 set_value_range_to_nonnegative (value_range_t *vr, tree type,
324 bool overflow_infinity)
326 tree zero;
328 if (overflow_infinity && !supports_overflow_infinity (type))
330 set_value_range_to_varying (vr);
331 return;
334 zero = build_int_cst (type, 0);
335 set_value_range (vr, VR_RANGE, zero,
336 (overflow_infinity
337 ? positive_overflow_infinity (type)
338 : TYPE_MAX_VALUE (type)),
339 vr->equiv);
342 /* Set value range VR to a non-NULL range of type TYPE. */
344 static inline void
345 set_value_range_to_nonnull (value_range_t *vr, tree type)
347 tree zero = build_int_cst (type, 0);
348 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
352 /* Set value range VR to a NULL range of type TYPE. */
354 static inline void
355 set_value_range_to_null (value_range_t *vr, tree type)
357 tree zero = build_int_cst (type, 0);
358 set_value_range (vr, VR_RANGE, zero, zero, vr->equiv);
362 /* Set value range VR to a range of a truthvalue of type TYPE. */
364 static inline void
365 set_value_range_to_truthvalue (value_range_t *vr, tree type)
367 if (TYPE_PRECISION (type) == 1)
368 set_value_range_to_varying (vr);
369 else
370 set_value_range (vr, VR_RANGE,
371 build_int_cst (type, 0), build_int_cst (type, 1),
372 vr->equiv);
376 /* Set value range VR to VR_UNDEFINED. */
378 static inline void
379 set_value_range_to_undefined (value_range_t *vr)
381 vr->type = VR_UNDEFINED;
382 vr->min = vr->max = NULL_TREE;
383 if (vr->equiv)
384 bitmap_clear (vr->equiv);
388 /* Return value range information for VAR.
390 If we have no values ranges recorded (ie, VRP is not running), then
391 return NULL. Otherwise create an empty range if none existed for VAR. */
393 static value_range_t *
394 get_value_range (tree var)
396 value_range_t *vr;
397 tree sym;
398 unsigned ver = SSA_NAME_VERSION (var);
400 /* If we have no recorded ranges, then return NULL. */
401 if (! vr_value)
402 return NULL;
404 vr = vr_value[ver];
405 if (vr)
406 return vr;
408 /* Create a default value range. */
409 vr_value[ver] = vr = XCNEW (value_range_t);
411 /* Allocate an equivalence set. */
412 vr->equiv = BITMAP_ALLOC (NULL);
414 /* If VAR is a default definition, the variable can take any value
415 in VAR's type. */
416 sym = SSA_NAME_VAR (var);
417 if (SSA_NAME_IS_DEFAULT_DEF (var))
419 /* Try to use the "nonnull" attribute to create ~[0, 0]
420 anti-ranges for pointers. Note that this is only valid with
421 default definitions of PARM_DECLs. */
422 if (TREE_CODE (sym) == PARM_DECL
423 && POINTER_TYPE_P (TREE_TYPE (sym))
424 && nonnull_arg_p (sym))
425 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
426 else
427 set_value_range_to_varying (vr);
430 return vr;
433 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
435 static inline bool
436 vrp_operand_equal_p (tree val1, tree val2)
438 if (val1 == val2)
439 return true;
440 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
441 return false;
442 if (is_overflow_infinity (val1))
443 return is_overflow_infinity (val2);
444 return true;
447 /* Return true, if the bitmaps B1 and B2 are equal. */
449 static inline bool
450 vrp_bitmap_equal_p (bitmap b1, bitmap b2)
452 return (b1 == b2
453 || (b1 && b2
454 && bitmap_equal_p (b1, b2)));
457 /* Update the value range and equivalence set for variable VAR to
458 NEW_VR. Return true if NEW_VR is different from VAR's previous
459 value.
461 NOTE: This function assumes that NEW_VR is a temporary value range
462 object created for the sole purpose of updating VAR's range. The
463 storage used by the equivalence set from NEW_VR will be freed by
464 this function. Do not call update_value_range when NEW_VR
465 is the range object associated with another SSA name. */
467 static inline bool
468 update_value_range (tree var, value_range_t *new_vr)
470 value_range_t *old_vr;
471 bool is_new;
473 /* Update the value range, if necessary. */
474 old_vr = get_value_range (var);
475 is_new = old_vr->type != new_vr->type
476 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
477 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
478 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
480 if (is_new)
481 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
482 new_vr->equiv);
484 BITMAP_FREE (new_vr->equiv);
485 new_vr->equiv = NULL;
487 return is_new;
491 /* Add VAR and VAR's equivalence set to EQUIV. */
493 static void
494 add_equivalence (bitmap equiv, tree var)
496 unsigned ver = SSA_NAME_VERSION (var);
497 value_range_t *vr = vr_value[ver];
499 bitmap_set_bit (equiv, ver);
500 if (vr && vr->equiv)
501 bitmap_ior_into (equiv, vr->equiv);
505 /* Return true if VR is ~[0, 0]. */
507 static inline bool
508 range_is_nonnull (value_range_t *vr)
510 return vr->type == VR_ANTI_RANGE
511 && integer_zerop (vr->min)
512 && integer_zerop (vr->max);
516 /* Return true if VR is [0, 0]. */
518 static inline bool
519 range_is_null (value_range_t *vr)
521 return vr->type == VR_RANGE
522 && integer_zerop (vr->min)
523 && integer_zerop (vr->max);
527 /* Return true if value range VR involves at least one symbol. */
529 static inline bool
530 symbolic_range_p (value_range_t *vr)
532 return (!is_gimple_min_invariant (vr->min)
533 || !is_gimple_min_invariant (vr->max));
536 /* Return true if value range VR uses a overflow infinity. */
538 static inline bool
539 overflow_infinity_range_p (value_range_t *vr)
541 return (vr->type == VR_RANGE
542 && (is_overflow_infinity (vr->min)
543 || is_overflow_infinity (vr->max)));
546 /* Like tree_expr_nonnegative_warnv_p, but this function uses value
547 ranges obtained so far. */
549 static bool
550 vrp_expr_computes_nonnegative (tree expr, bool *strict_overflow_p)
552 return tree_expr_nonnegative_warnv_p (expr, strict_overflow_p);
555 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
556 obtained so far. */
558 static bool
559 vrp_expr_computes_nonzero (tree expr, bool *strict_overflow_p)
561 if (tree_expr_nonzero_warnv_p (expr, strict_overflow_p))
562 return true;
564 /* If we have an expression of the form &X->a, then the expression
565 is nonnull if X is nonnull. */
566 if (TREE_CODE (expr) == ADDR_EXPR)
568 tree base = get_base_address (TREE_OPERAND (expr, 0));
570 if (base != NULL_TREE
571 && TREE_CODE (base) == INDIRECT_REF
572 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
574 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
575 if (range_is_nonnull (vr))
576 return true;
580 return false;
583 /* Returns true if EXPR is a valid value (as expected by compare_values) --
584 a gimple invariant, or SSA_NAME +- CST. */
586 static bool
587 valid_value_p (tree expr)
589 if (TREE_CODE (expr) == SSA_NAME)
590 return true;
592 if (TREE_CODE (expr) == PLUS_EXPR
593 || TREE_CODE (expr) == MINUS_EXPR)
594 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
595 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
597 return is_gimple_min_invariant (expr);
600 /* Return
601 1 if VAL < VAL2
602 0 if !(VAL < VAL2)
603 -2 if those are incomparable. */
604 static inline int
605 operand_less_p (tree val, tree val2)
607 /* LT is folded faster than GE and others. Inline the common case. */
608 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
610 if (TYPE_UNSIGNED (TREE_TYPE (val)))
611 return INT_CST_LT_UNSIGNED (val, val2);
612 else
614 if (INT_CST_LT (val, val2))
615 return 1;
618 else
620 tree tcmp;
622 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
623 if (!tcmp)
624 return -2;
626 if (!integer_zerop (tcmp))
627 return 1;
630 /* val >= val2, not considering overflow infinity. */
631 if (is_negative_overflow_infinity (val))
632 return is_negative_overflow_infinity (val2) ? 0 : 1;
633 else if (is_positive_overflow_infinity (val2))
634 return is_positive_overflow_infinity (val) ? 0 : 1;
636 return 0;
639 /* Compare two values VAL1 and VAL2. Return
641 -2 if VAL1 and VAL2 cannot be compared at compile-time,
642 -1 if VAL1 < VAL2,
643 0 if VAL1 == VAL2,
644 +1 if VAL1 > VAL2, and
645 +2 if VAL1 != VAL2
647 This is similar to tree_int_cst_compare but supports pointer values
648 and values that cannot be compared at compile time.
650 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
651 true if the return value is only valid if we assume that signed
652 overflow is undefined. */
654 static int
655 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
657 if (val1 == val2)
658 return 0;
660 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
661 both integers. */
662 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
663 == POINTER_TYPE_P (TREE_TYPE (val2)));
665 if ((TREE_CODE (val1) == SSA_NAME
666 || TREE_CODE (val1) == PLUS_EXPR
667 || TREE_CODE (val1) == MINUS_EXPR)
668 && (TREE_CODE (val2) == SSA_NAME
669 || TREE_CODE (val2) == PLUS_EXPR
670 || TREE_CODE (val2) == MINUS_EXPR))
672 tree n1, c1, n2, c2;
673 enum tree_code code1, code2;
675 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
676 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
677 same name, return -2. */
678 if (TREE_CODE (val1) == SSA_NAME)
680 code1 = SSA_NAME;
681 n1 = val1;
682 c1 = NULL_TREE;
684 else
686 code1 = TREE_CODE (val1);
687 n1 = TREE_OPERAND (val1, 0);
688 c1 = TREE_OPERAND (val1, 1);
689 if (tree_int_cst_sgn (c1) == -1)
691 if (is_negative_overflow_infinity (c1))
692 return -2;
693 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
694 if (!c1)
695 return -2;
696 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
700 if (TREE_CODE (val2) == SSA_NAME)
702 code2 = SSA_NAME;
703 n2 = val2;
704 c2 = NULL_TREE;
706 else
708 code2 = TREE_CODE (val2);
709 n2 = TREE_OPERAND (val2, 0);
710 c2 = TREE_OPERAND (val2, 1);
711 if (tree_int_cst_sgn (c2) == -1)
713 if (is_negative_overflow_infinity (c2))
714 return -2;
715 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
716 if (!c2)
717 return -2;
718 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
722 /* Both values must use the same name. */
723 if (n1 != n2)
724 return -2;
726 if (code1 == SSA_NAME
727 && code2 == SSA_NAME)
728 /* NAME == NAME */
729 return 0;
731 /* If overflow is defined we cannot simplify more. */
732 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
733 return -2;
735 if (strict_overflow_p != NULL)
736 *strict_overflow_p = true;
738 if (code1 == SSA_NAME)
740 if (code2 == PLUS_EXPR)
741 /* NAME < NAME + CST */
742 return -1;
743 else if (code2 == MINUS_EXPR)
744 /* NAME > NAME - CST */
745 return 1;
747 else if (code1 == PLUS_EXPR)
749 if (code2 == SSA_NAME)
750 /* NAME + CST > NAME */
751 return 1;
752 else if (code2 == PLUS_EXPR)
753 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
754 return compare_values_warnv (c1, c2, strict_overflow_p);
755 else if (code2 == MINUS_EXPR)
756 /* NAME + CST1 > NAME - CST2 */
757 return 1;
759 else if (code1 == MINUS_EXPR)
761 if (code2 == SSA_NAME)
762 /* NAME - CST < NAME */
763 return -1;
764 else if (code2 == PLUS_EXPR)
765 /* NAME - CST1 < NAME + CST2 */
766 return -1;
767 else if (code2 == MINUS_EXPR)
768 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
769 C1 and C2 are swapped in the call to compare_values. */
770 return compare_values_warnv (c2, c1, strict_overflow_p);
773 gcc_unreachable ();
776 /* We cannot compare non-constants. */
777 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
778 return -2;
780 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
782 /* We cannot compare overflowed values, except for overflow
783 infinities. */
784 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
786 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
787 return -2;
789 if (is_negative_overflow_infinity (val1))
790 return is_negative_overflow_infinity (val2) ? 0 : -1;
791 else if (is_negative_overflow_infinity (val2))
792 return 1;
793 else if (is_positive_overflow_infinity (val1))
794 return is_positive_overflow_infinity (val2) ? 0 : 1;
795 else if (is_positive_overflow_infinity (val2))
796 return -1;
797 return -2;
800 return tree_int_cst_compare (val1, val2);
802 else
804 tree t;
806 /* First see if VAL1 and VAL2 are not the same. */
807 if (val1 == val2 || operand_equal_p (val1, val2, 0))
808 return 0;
810 /* If VAL1 is a lower address than VAL2, return -1. */
811 if (operand_less_p (val1, val2) == 1)
812 return -1;
814 /* If VAL1 is a higher address than VAL2, return +1. */
815 if (operand_less_p (val2, val1) == 1)
816 return 1;
818 /* If VAL1 is different than VAL2, return +2.
819 For integer constants we either have already returned -1 or 1
820 or they are equivalent. We still might succeed in proving
821 something about non-trivial operands. */
822 if (TREE_CODE (val1) != INTEGER_CST
823 || TREE_CODE (val2) != INTEGER_CST)
825 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
826 if (t && tree_expr_nonzero_p (t))
827 return 2;
830 return -2;
834 /* Compare values like compare_values_warnv, but treat comparisons
835 which rely on undefined overflow as incomparable. */
837 static int
838 compare_values (tree val1, tree val2)
840 bool sop;
841 int ret;
843 sop = false;
844 ret = compare_values_warnv (val1, val2, &sop);
845 if (sop)
846 ret = -2;
847 return ret;
851 /* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
852 0 if VAL is not inside VR,
853 -2 if we cannot tell either way.
855 FIXME, the current semantics of this functions are a bit quirky
856 when taken in the context of VRP. In here we do not care
857 about VR's type. If VR is the anti-range ~[3, 5] the call
858 value_inside_range (4, VR) will return 1.
860 This is counter-intuitive in a strict sense, but the callers
861 currently expect this. They are calling the function
862 merely to determine whether VR->MIN <= VAL <= VR->MAX. The
863 callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
864 themselves.
866 This also applies to value_ranges_intersect_p and
867 range_includes_zero_p. The semantics of VR_RANGE and
868 VR_ANTI_RANGE should be encoded here, but that also means
869 adapting the users of these functions to the new semantics.
871 Benchmark compile/20001226-1.c compilation time after changing this
872 function. */
874 static inline int
875 value_inside_range (tree val, value_range_t * vr)
877 int cmp1, cmp2;
879 cmp1 = operand_less_p (val, vr->min);
880 if (cmp1 == -2)
881 return -2;
882 if (cmp1 == 1)
883 return 0;
885 cmp2 = operand_less_p (vr->max, val);
886 if (cmp2 == -2)
887 return -2;
889 return !cmp2;
893 /* Return true if value ranges VR0 and VR1 have a non-empty
894 intersection.
896 Benchmark compile/20001226-1.c compilation time after changing this
897 function.
900 static inline bool
901 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
903 /* The value ranges do not intersect if the maximum of the first range is
904 less than the minimum of the second range or vice versa.
905 When those relations are unknown, we can't do any better. */
906 if (operand_less_p (vr0->max, vr1->min) != 0)
907 return false;
908 if (operand_less_p (vr1->max, vr0->min) != 0)
909 return false;
910 return true;
914 /* Return true if VR includes the value zero, false otherwise. FIXME,
915 currently this will return false for an anti-range like ~[-4, 3].
916 This will be wrong when the semantics of value_inside_range are
917 modified (currently the users of this function expect these
918 semantics). */
920 static inline bool
921 range_includes_zero_p (value_range_t *vr)
923 tree zero;
925 gcc_assert (vr->type != VR_UNDEFINED
926 && vr->type != VR_VARYING
927 && !symbolic_range_p (vr));
929 zero = build_int_cst (TREE_TYPE (vr->min), 0);
930 return (value_inside_range (zero, vr) == 1);
933 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
934 false otherwise or if no value range information is available. */
936 bool
937 ssa_name_nonnegative_p (tree t)
939 value_range_t *vr = get_value_range (t);
941 if (!vr)
942 return false;
944 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
945 which would return a useful value should be encoded as a VR_RANGE. */
946 if (vr->type == VR_RANGE)
948 int result = compare_values (vr->min, integer_zero_node);
950 return (result == 0 || result == 1);
952 return false;
955 /* Return true if T, an SSA_NAME, is known to be nonzero. Return
956 false otherwise or if no value range information is available. */
958 bool
959 ssa_name_nonzero_p (tree t)
961 value_range_t *vr = get_value_range (t);
963 if (!vr)
964 return false;
966 /* A VR_RANGE which does not include zero is a nonzero value. */
967 if (vr->type == VR_RANGE && !symbolic_range_p (vr))
968 return ! range_includes_zero_p (vr);
970 /* A VR_ANTI_RANGE which does include zero is a nonzero value. */
971 if (vr->type == VR_ANTI_RANGE && !symbolic_range_p (vr))
972 return range_includes_zero_p (vr);
974 return false;
978 /* Extract value range information from an ASSERT_EXPR EXPR and store
979 it in *VR_P. */
981 static void
982 extract_range_from_assert (value_range_t *vr_p, tree expr)
984 tree var, cond, limit, min, max, type;
985 value_range_t *var_vr, *limit_vr;
986 enum tree_code cond_code;
988 var = ASSERT_EXPR_VAR (expr);
989 cond = ASSERT_EXPR_COND (expr);
991 gcc_assert (COMPARISON_CLASS_P (cond));
993 /* Find VAR in the ASSERT_EXPR conditional. */
994 if (var == TREE_OPERAND (cond, 0))
996 /* If the predicate is of the form VAR COMP LIMIT, then we just
997 take LIMIT from the RHS and use the same comparison code. */
998 limit = TREE_OPERAND (cond, 1);
999 cond_code = TREE_CODE (cond);
1001 else
1003 /* If the predicate is of the form LIMIT COMP VAR, then we need
1004 to flip around the comparison code to create the proper range
1005 for VAR. */
1006 limit = TREE_OPERAND (cond, 0);
1007 cond_code = swap_tree_comparison (TREE_CODE (cond));
1010 type = TREE_TYPE (limit);
1011 gcc_assert (limit != var);
1013 /* For pointer arithmetic, we only keep track of pointer equality
1014 and inequality. */
1015 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1017 set_value_range_to_varying (vr_p);
1018 return;
1021 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1022 try to use LIMIT's range to avoid creating symbolic ranges
1023 unnecessarily. */
1024 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1026 /* LIMIT's range is only interesting if it has any useful information. */
1027 if (limit_vr
1028 && (limit_vr->type == VR_UNDEFINED
1029 || limit_vr->type == VR_VARYING
1030 || symbolic_range_p (limit_vr)))
1031 limit_vr = NULL;
1033 /* Initially, the new range has the same set of equivalences of
1034 VAR's range. This will be revised before returning the final
1035 value. Since assertions may be chained via mutually exclusive
1036 predicates, we will need to trim the set of equivalences before
1037 we are done. */
1038 gcc_assert (vr_p->equiv == NULL);
1039 vr_p->equiv = BITMAP_ALLOC (NULL);
1040 add_equivalence (vr_p->equiv, var);
1042 /* Extract a new range based on the asserted comparison for VAR and
1043 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1044 will only use it for equality comparisons (EQ_EXPR). For any
1045 other kind of assertion, we cannot derive a range from LIMIT's
1046 anti-range that can be used to describe the new range. For
1047 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1048 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1049 no single range for x_2 that could describe LE_EXPR, so we might
1050 as well build the range [b_4, +INF] for it. */
1051 if (cond_code == EQ_EXPR)
1053 enum value_range_type range_type;
1055 if (limit_vr)
1057 range_type = limit_vr->type;
1058 min = limit_vr->min;
1059 max = limit_vr->max;
1061 else
1063 range_type = VR_RANGE;
1064 min = limit;
1065 max = limit;
1068 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1070 /* When asserting the equality VAR == LIMIT and LIMIT is another
1071 SSA name, the new range will also inherit the equivalence set
1072 from LIMIT. */
1073 if (TREE_CODE (limit) == SSA_NAME)
1074 add_equivalence (vr_p->equiv, limit);
1076 else if (cond_code == NE_EXPR)
1078 /* As described above, when LIMIT's range is an anti-range and
1079 this assertion is an inequality (NE_EXPR), then we cannot
1080 derive anything from the anti-range. For instance, if
1081 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1082 not imply that VAR's range is [0, 0]. So, in the case of
1083 anti-ranges, we just assert the inequality using LIMIT and
1084 not its anti-range.
1086 If LIMIT_VR is a range, we can only use it to build a new
1087 anti-range if LIMIT_VR is a single-valued range. For
1088 instance, if LIMIT_VR is [0, 1], the predicate
1089 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1090 Rather, it means that for value 0 VAR should be ~[0, 0]
1091 and for value 1, VAR should be ~[1, 1]. We cannot
1092 represent these ranges.
1094 The only situation in which we can build a valid
1095 anti-range is when LIMIT_VR is a single-valued range
1096 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1097 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1098 if (limit_vr
1099 && limit_vr->type == VR_RANGE
1100 && compare_values (limit_vr->min, limit_vr->max) == 0)
1102 min = limit_vr->min;
1103 max = limit_vr->max;
1105 else
1107 /* In any other case, we cannot use LIMIT's range to build a
1108 valid anti-range. */
1109 min = max = limit;
1112 /* If MIN and MAX cover the whole range for their type, then
1113 just use the original LIMIT. */
1114 if (INTEGRAL_TYPE_P (type)
1115 && (min == TYPE_MIN_VALUE (type)
1116 || is_negative_overflow_infinity (min))
1117 && (max == TYPE_MAX_VALUE (type)
1118 || is_positive_overflow_infinity (max)))
1119 min = max = limit;
1121 set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
1123 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1125 min = TYPE_MIN_VALUE (type);
1127 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1128 max = limit;
1129 else
1131 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1132 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1133 LT_EXPR. */
1134 max = limit_vr->max;
1137 /* If the maximum value forces us to be out of bounds, simply punt.
1138 It would be pointless to try and do anything more since this
1139 all should be optimized away above us. */
1140 if ((cond_code == LT_EXPR
1141 && compare_values (max, min) == 0)
1142 || is_overflow_infinity (max))
1143 set_value_range_to_varying (vr_p);
1144 else
1146 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1147 if (cond_code == LT_EXPR)
1149 tree one = build_int_cst (type, 1);
1150 max = fold_build2 (MINUS_EXPR, type, max, one);
1153 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1156 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1158 max = TYPE_MAX_VALUE (type);
1160 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1161 min = limit;
1162 else
1164 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1165 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1166 GT_EXPR. */
1167 min = limit_vr->min;
1170 /* If the minimum value forces us to be out of bounds, simply punt.
1171 It would be pointless to try and do anything more since this
1172 all should be optimized away above us. */
1173 if ((cond_code == GT_EXPR
1174 && compare_values (min, max) == 0)
1175 || is_overflow_infinity (min))
1176 set_value_range_to_varying (vr_p);
1177 else
1179 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1180 if (cond_code == GT_EXPR)
1182 tree one = build_int_cst (type, 1);
1183 min = fold_build2 (PLUS_EXPR, type, min, one);
1186 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1189 else
1190 gcc_unreachable ();
1192 /* If VAR already had a known range, it may happen that the new
1193 range we have computed and VAR's range are not compatible. For
1194 instance,
1196 if (p_5 == NULL)
1197 p_6 = ASSERT_EXPR <p_5, p_5 == NULL>;
1198 x_7 = p_6->fld;
1199 p_8 = ASSERT_EXPR <p_6, p_6 != NULL>;
1201 While the above comes from a faulty program, it will cause an ICE
1202 later because p_8 and p_6 will have incompatible ranges and at
1203 the same time will be considered equivalent. A similar situation
1204 would arise from
1206 if (i_5 > 10)
1207 i_6 = ASSERT_EXPR <i_5, i_5 > 10>;
1208 if (i_5 < 5)
1209 i_7 = ASSERT_EXPR <i_6, i_6 < 5>;
1211 Again i_6 and i_7 will have incompatible ranges. It would be
1212 pointless to try and do anything with i_7's range because
1213 anything dominated by 'if (i_5 < 5)' will be optimized away.
1214 Note, due to the wa in which simulation proceeds, the statement
1215 i_7 = ASSERT_EXPR <...> we would never be visited because the
1216 conditional 'if (i_5 < 5)' always evaluates to false. However,
1217 this extra check does not hurt and may protect against future
1218 changes to VRP that may get into a situation similar to the
1219 NULL pointer dereference example.
1221 Note that these compatibility tests are only needed when dealing
1222 with ranges or a mix of range and anti-range. If VAR_VR and VR_P
1223 are both anti-ranges, they will always be compatible, because two
1224 anti-ranges will always have a non-empty intersection. */
1226 var_vr = get_value_range (var);
1228 /* We may need to make adjustments when VR_P and VAR_VR are numeric
1229 ranges or anti-ranges. */
1230 if (vr_p->type == VR_VARYING
1231 || vr_p->type == VR_UNDEFINED
1232 || var_vr->type == VR_VARYING
1233 || var_vr->type == VR_UNDEFINED
1234 || symbolic_range_p (vr_p)
1235 || symbolic_range_p (var_vr))
1236 return;
1238 if (var_vr->type == VR_RANGE && vr_p->type == VR_RANGE)
1240 /* If the two ranges have a non-empty intersection, we can
1241 refine the resulting range. Since the assert expression
1242 creates an equivalency and at the same time it asserts a
1243 predicate, we can take the intersection of the two ranges to
1244 get better precision. */
1245 if (value_ranges_intersect_p (var_vr, vr_p))
1247 /* Use the larger of the two minimums. */
1248 if (compare_values (vr_p->min, var_vr->min) == -1)
1249 min = var_vr->min;
1250 else
1251 min = vr_p->min;
1253 /* Use the smaller of the two maximums. */
1254 if (compare_values (vr_p->max, var_vr->max) == 1)
1255 max = var_vr->max;
1256 else
1257 max = vr_p->max;
1259 set_value_range (vr_p, vr_p->type, min, max, vr_p->equiv);
1261 else
1263 /* The two ranges do not intersect, set the new range to
1264 VARYING, because we will not be able to do anything
1265 meaningful with it. */
1266 set_value_range_to_varying (vr_p);
1269 else if ((var_vr->type == VR_RANGE && vr_p->type == VR_ANTI_RANGE)
1270 || (var_vr->type == VR_ANTI_RANGE && vr_p->type == VR_RANGE))
1272 /* A range and an anti-range will cancel each other only if
1273 their ends are the same. For instance, in the example above,
1274 p_8's range ~[0, 0] and p_6's range [0, 0] are incompatible,
1275 so VR_P should be set to VR_VARYING. */
1276 if (compare_values (var_vr->min, vr_p->min) == 0
1277 && compare_values (var_vr->max, vr_p->max) == 0)
1278 set_value_range_to_varying (vr_p);
1279 else
1281 tree min, max, anti_min, anti_max, real_min, real_max;
1282 int cmp;
1284 /* We want to compute the logical AND of the two ranges;
1285 there are three cases to consider.
1288 1. The VR_ANTI_RANGE range is completely within the
1289 VR_RANGE and the endpoints of the ranges are
1290 different. In that case the resulting range
1291 should be whichever range is more precise.
1292 Typically that will be the VR_RANGE.
1294 2. The VR_ANTI_RANGE is completely disjoint from
1295 the VR_RANGE. In this case the resulting range
1296 should be the VR_RANGE.
1298 3. There is some overlap between the VR_ANTI_RANGE
1299 and the VR_RANGE.
1301 3a. If the high limit of the VR_ANTI_RANGE resides
1302 within the VR_RANGE, then the result is a new
1303 VR_RANGE starting at the high limit of the
1304 the VR_ANTI_RANGE + 1 and extending to the
1305 high limit of the original VR_RANGE.
1307 3b. If the low limit of the VR_ANTI_RANGE resides
1308 within the VR_RANGE, then the result is a new
1309 VR_RANGE starting at the low limit of the original
1310 VR_RANGE and extending to the low limit of the
1311 VR_ANTI_RANGE - 1. */
1312 if (vr_p->type == VR_ANTI_RANGE)
1314 anti_min = vr_p->min;
1315 anti_max = vr_p->max;
1316 real_min = var_vr->min;
1317 real_max = var_vr->max;
1319 else
1321 anti_min = var_vr->min;
1322 anti_max = var_vr->max;
1323 real_min = vr_p->min;
1324 real_max = vr_p->max;
1328 /* Case 1, VR_ANTI_RANGE completely within VR_RANGE,
1329 not including any endpoints. */
1330 if (compare_values (anti_max, real_max) == -1
1331 && compare_values (anti_min, real_min) == 1)
1333 set_value_range (vr_p, VR_RANGE, real_min,
1334 real_max, vr_p->equiv);
1336 /* Case 2, VR_ANTI_RANGE completely disjoint from
1337 VR_RANGE. */
1338 else if (compare_values (anti_min, real_max) == 1
1339 || compare_values (anti_max, real_min) == -1)
1341 set_value_range (vr_p, VR_RANGE, real_min,
1342 real_max, vr_p->equiv);
1344 /* Case 3a, the anti-range extends into the low
1345 part of the real range. Thus creating a new
1346 low for the real range. */
1347 else if (((cmp = compare_values (anti_max, real_min)) == 1
1348 || cmp == 0)
1349 && compare_values (anti_max, real_max) == -1)
1351 gcc_assert (!is_positive_overflow_infinity (anti_max));
1352 if (needs_overflow_infinity (TREE_TYPE (anti_max))
1353 && anti_max == TYPE_MAX_VALUE (TREE_TYPE (anti_max)))
1355 if (!supports_overflow_infinity (TREE_TYPE (var_vr->min)))
1357 set_value_range_to_varying (vr_p);
1358 return;
1360 min = positive_overflow_infinity (TREE_TYPE (var_vr->min));
1362 else
1363 min = fold_build2 (PLUS_EXPR, TREE_TYPE (var_vr->min),
1364 anti_max,
1365 build_int_cst (TREE_TYPE (var_vr->min), 1));
1366 max = real_max;
1367 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1369 /* Case 3b, the anti-range extends into the high
1370 part of the real range. Thus creating a new
1371 higher for the real range. */
1372 else if (compare_values (anti_min, real_min) == 1
1373 && ((cmp = compare_values (anti_min, real_max)) == -1
1374 || cmp == 0))
1376 gcc_assert (!is_negative_overflow_infinity (anti_min));
1377 if (needs_overflow_infinity (TREE_TYPE (anti_min))
1378 && anti_min == TYPE_MIN_VALUE (TREE_TYPE (anti_min)))
1380 if (!supports_overflow_infinity (TREE_TYPE (var_vr->min)))
1382 set_value_range_to_varying (vr_p);
1383 return;
1385 max = negative_overflow_infinity (TREE_TYPE (var_vr->min));
1387 else
1388 max = fold_build2 (MINUS_EXPR, TREE_TYPE (var_vr->min),
1389 anti_min,
1390 build_int_cst (TREE_TYPE (var_vr->min), 1));
1391 min = real_min;
1392 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1399 /* Extract range information from SSA name VAR and store it in VR. If
1400 VAR has an interesting range, use it. Otherwise, create the
1401 range [VAR, VAR] and return it. This is useful in situations where
1402 we may have conditionals testing values of VARYING names. For
1403 instance,
1405 x_3 = y_5;
1406 if (x_3 > y_5)
1409 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1410 always false. */
1412 static void
1413 extract_range_from_ssa_name (value_range_t *vr, tree var)
1415 value_range_t *var_vr = get_value_range (var);
1417 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1418 copy_value_range (vr, var_vr);
1419 else
1420 set_value_range (vr, VR_RANGE, var, var, NULL);
1422 add_equivalence (vr->equiv, var);
1426 /* Wrapper around int_const_binop. If the operation overflows and we
1427 are not using wrapping arithmetic, then adjust the result to be
1428 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1429 NULL_TREE if we need to use an overflow infinity representation but
1430 the type does not support it. */
1432 static tree
1433 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1435 tree res;
1437 res = int_const_binop (code, val1, val2, 0);
1439 /* If we are not using wrapping arithmetic, operate symbolically
1440 on -INF and +INF. */
1441 if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1443 int checkz = compare_values (res, val1);
1444 bool overflow = false;
1446 /* Ensure that res = val1 [+*] val2 >= val1
1447 or that res = val1 - val2 <= val1. */
1448 if ((code == PLUS_EXPR
1449 && !(checkz == 1 || checkz == 0))
1450 || (code == MINUS_EXPR
1451 && !(checkz == 0 || checkz == -1)))
1453 overflow = true;
1455 /* Checking for multiplication overflow is done by dividing the
1456 output of the multiplication by the first input of the
1457 multiplication. If the result of that division operation is
1458 not equal to the second input of the multiplication, then the
1459 multiplication overflowed. */
1460 else if (code == MULT_EXPR && !integer_zerop (val1))
1462 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1463 res,
1464 val1, 0);
1465 int check = compare_values (tmp, val2);
1467 if (check != 0)
1468 overflow = true;
1471 if (overflow)
1473 res = copy_node (res);
1474 TREE_OVERFLOW (res) = 1;
1478 else if ((TREE_OVERFLOW (res)
1479 && !TREE_OVERFLOW (val1)
1480 && !TREE_OVERFLOW (val2))
1481 || is_overflow_infinity (val1)
1482 || is_overflow_infinity (val2))
1484 /* If the operation overflowed but neither VAL1 nor VAL2 are
1485 overflown, return -INF or +INF depending on the operation
1486 and the combination of signs of the operands. */
1487 int sgn1 = tree_int_cst_sgn (val1);
1488 int sgn2 = tree_int_cst_sgn (val2);
1490 if (needs_overflow_infinity (TREE_TYPE (res))
1491 && !supports_overflow_infinity (TREE_TYPE (res)))
1492 return NULL_TREE;
1494 /* We have to punt on subtracting infinities of the same sign,
1495 since we can't tell what the sign of the result should
1496 be. */
1497 if (code == MINUS_EXPR
1498 && sgn1 == sgn2
1499 && is_overflow_infinity (val1)
1500 && is_overflow_infinity (val2))
1501 return NULL_TREE;
1503 /* Notice that we only need to handle the restricted set of
1504 operations handled by extract_range_from_binary_expr.
1505 Among them, only multiplication, addition and subtraction
1506 can yield overflow without overflown operands because we
1507 are working with integral types only... except in the
1508 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1509 for division too. */
1511 /* For multiplication, the sign of the overflow is given
1512 by the comparison of the signs of the operands. */
1513 if ((code == MULT_EXPR && sgn1 == sgn2)
1514 /* For addition, the operands must be of the same sign
1515 to yield an overflow. Its sign is therefore that
1516 of one of the operands, for example the first. */
1517 || (code == PLUS_EXPR && sgn1 > 0)
1518 /* For subtraction, non-infinite operands must be of
1519 different signs to yield an overflow. Its sign is
1520 therefore that of the first operand or the opposite of
1521 that of the second operand. A first operand of 0 counts
1522 as positive here, for the corner case 0 - (-INF), which
1523 overflows, but must yield +INF. For infinite operands 0
1524 - INF is negative, not positive. */
1525 || (code == MINUS_EXPR
1526 && (sgn1 >= 0
1527 ? !is_positive_overflow_infinity (val2)
1528 : is_negative_overflow_infinity (val2)))
1529 /* For division, the only case is -INF / -1 = +INF. */
1530 || code == TRUNC_DIV_EXPR
1531 || code == FLOOR_DIV_EXPR
1532 || code == CEIL_DIV_EXPR
1533 || code == EXACT_DIV_EXPR
1534 || code == ROUND_DIV_EXPR)
1535 return (needs_overflow_infinity (TREE_TYPE (res))
1536 ? positive_overflow_infinity (TREE_TYPE (res))
1537 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1538 else
1539 return (needs_overflow_infinity (TREE_TYPE (res))
1540 ? negative_overflow_infinity (TREE_TYPE (res))
1541 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1544 return res;
1548 /* Extract range information from a binary expression EXPR based on
1549 the ranges of each of its operands and the expression code. */
1551 static void
1552 extract_range_from_binary_expr (value_range_t *vr, tree expr)
1554 enum tree_code code = TREE_CODE (expr);
1555 enum value_range_type type;
1556 tree op0, op1, min, max;
1557 int cmp;
1558 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1559 value_range_t vr1 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1561 /* Not all binary expressions can be applied to ranges in a
1562 meaningful way. Handle only arithmetic operations. */
1563 if (code != PLUS_EXPR
1564 && code != MINUS_EXPR
1565 && code != MULT_EXPR
1566 && code != TRUNC_DIV_EXPR
1567 && code != FLOOR_DIV_EXPR
1568 && code != CEIL_DIV_EXPR
1569 && code != EXACT_DIV_EXPR
1570 && code != ROUND_DIV_EXPR
1571 && code != MIN_EXPR
1572 && code != MAX_EXPR
1573 && code != BIT_AND_EXPR
1574 && code != TRUTH_ANDIF_EXPR
1575 && code != TRUTH_ORIF_EXPR
1576 && code != TRUTH_AND_EXPR
1577 && code != TRUTH_OR_EXPR)
1579 set_value_range_to_varying (vr);
1580 return;
1583 /* Get value ranges for each operand. For constant operands, create
1584 a new value range with the operand to simplify processing. */
1585 op0 = TREE_OPERAND (expr, 0);
1586 if (TREE_CODE (op0) == SSA_NAME)
1587 vr0 = *(get_value_range (op0));
1588 else if (is_gimple_min_invariant (op0))
1589 set_value_range (&vr0, VR_RANGE, op0, op0, NULL);
1590 else
1591 set_value_range_to_varying (&vr0);
1593 op1 = TREE_OPERAND (expr, 1);
1594 if (TREE_CODE (op1) == SSA_NAME)
1595 vr1 = *(get_value_range (op1));
1596 else if (is_gimple_min_invariant (op1))
1597 set_value_range (&vr1, VR_RANGE, op1, op1, NULL);
1598 else
1599 set_value_range_to_varying (&vr1);
1601 /* If either range is UNDEFINED, so is the result. */
1602 if (vr0.type == VR_UNDEFINED || vr1.type == VR_UNDEFINED)
1604 set_value_range_to_undefined (vr);
1605 return;
1608 /* The type of the resulting value range defaults to VR0.TYPE. */
1609 type = vr0.type;
1611 /* Refuse to operate on VARYING ranges, ranges of different kinds
1612 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
1613 because we may be able to derive a useful range even if one of
1614 the operands is VR_VARYING or symbolic range. TODO, we may be
1615 able to derive anti-ranges in some cases. */
1616 if (code != BIT_AND_EXPR
1617 && code != TRUTH_AND_EXPR
1618 && code != TRUTH_OR_EXPR
1619 && (vr0.type == VR_VARYING
1620 || vr1.type == VR_VARYING
1621 || vr0.type != vr1.type
1622 || symbolic_range_p (&vr0)
1623 || symbolic_range_p (&vr1)))
1625 set_value_range_to_varying (vr);
1626 return;
1629 /* Now evaluate the expression to determine the new range. */
1630 if (POINTER_TYPE_P (TREE_TYPE (expr))
1631 || POINTER_TYPE_P (TREE_TYPE (op0))
1632 || POINTER_TYPE_P (TREE_TYPE (op1)))
1634 /* For pointer types, we are really only interested in asserting
1635 whether the expression evaluates to non-NULL. FIXME, we used
1636 to gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR), but
1637 ivopts is generating expressions with pointer multiplication
1638 in them. */
1639 if (code == PLUS_EXPR)
1641 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
1642 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1643 else if (range_is_null (&vr0) && range_is_null (&vr1))
1644 set_value_range_to_null (vr, TREE_TYPE (expr));
1645 else
1646 set_value_range_to_varying (vr);
1648 else
1650 /* Subtracting from a pointer, may yield 0, so just drop the
1651 resulting range to varying. */
1652 set_value_range_to_varying (vr);
1655 return;
1658 /* For integer ranges, apply the operation to each end of the
1659 range and see what we end up with. */
1660 if (code == TRUTH_ANDIF_EXPR
1661 || code == TRUTH_ORIF_EXPR
1662 || code == TRUTH_AND_EXPR
1663 || code == TRUTH_OR_EXPR)
1665 /* If one of the operands is zero, we know that the whole
1666 expression evaluates zero. */
1667 if (code == TRUTH_AND_EXPR
1668 && ((vr0.type == VR_RANGE
1669 && integer_zerop (vr0.min)
1670 && integer_zerop (vr0.max))
1671 || (vr1.type == VR_RANGE
1672 && integer_zerop (vr1.min)
1673 && integer_zerop (vr1.max))))
1675 type = VR_RANGE;
1676 min = max = build_int_cst (TREE_TYPE (expr), 0);
1678 /* If one of the operands is one, we know that the whole
1679 expression evaluates one. */
1680 else if (code == TRUTH_OR_EXPR
1681 && ((vr0.type == VR_RANGE
1682 && integer_onep (vr0.min)
1683 && integer_onep (vr0.max))
1684 || (vr1.type == VR_RANGE
1685 && integer_onep (vr1.min)
1686 && integer_onep (vr1.max))))
1688 type = VR_RANGE;
1689 min = max = build_int_cst (TREE_TYPE (expr), 1);
1691 else if (vr0.type != VR_VARYING
1692 && vr1.type != VR_VARYING
1693 && vr0.type == vr1.type
1694 && !symbolic_range_p (&vr0)
1695 && !overflow_infinity_range_p (&vr0)
1696 && !symbolic_range_p (&vr1)
1697 && !overflow_infinity_range_p (&vr1))
1699 /* Boolean expressions cannot be folded with int_const_binop. */
1700 min = fold_binary (code, TREE_TYPE (expr), vr0.min, vr1.min);
1701 max = fold_binary (code, TREE_TYPE (expr), vr0.max, vr1.max);
1703 else
1705 /* The result of a TRUTH_*_EXPR is always true or false. */
1706 set_value_range_to_truthvalue (vr, TREE_TYPE (expr));
1707 return;
1710 else if (code == PLUS_EXPR
1711 || code == MIN_EXPR
1712 || code == MAX_EXPR)
1714 /* If we have a PLUS_EXPR with two VR_ANTI_RANGEs, drop to
1715 VR_VARYING. It would take more effort to compute a precise
1716 range for such a case. For example, if we have op0 == 1 and
1717 op1 == -1 with their ranges both being ~[0,0], we would have
1718 op0 + op1 == 0, so we cannot claim that the sum is in ~[0,0].
1719 Note that we are guaranteed to have vr0.type == vr1.type at
1720 this point. */
1721 if (code == PLUS_EXPR && vr0.type == VR_ANTI_RANGE)
1723 set_value_range_to_varying (vr);
1724 return;
1727 /* For operations that make the resulting range directly
1728 proportional to the original ranges, apply the operation to
1729 the same end of each range. */
1730 min = vrp_int_const_binop (code, vr0.min, vr1.min);
1731 max = vrp_int_const_binop (code, vr0.max, vr1.max);
1733 else if (code == MULT_EXPR
1734 || code == TRUNC_DIV_EXPR
1735 || code == FLOOR_DIV_EXPR
1736 || code == CEIL_DIV_EXPR
1737 || code == EXACT_DIV_EXPR
1738 || code == ROUND_DIV_EXPR)
1740 tree val[4];
1741 size_t i;
1742 bool sop;
1744 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1745 drop to VR_VARYING. It would take more effort to compute a
1746 precise range for such a case. For example, if we have
1747 op0 == 65536 and op1 == 65536 with their ranges both being
1748 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1749 we cannot claim that the product is in ~[0,0]. Note that we
1750 are guaranteed to have vr0.type == vr1.type at this
1751 point. */
1752 if (code == MULT_EXPR
1753 && vr0.type == VR_ANTI_RANGE
1754 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
1756 set_value_range_to_varying (vr);
1757 return;
1760 /* Multiplications and divisions are a bit tricky to handle,
1761 depending on the mix of signs we have in the two ranges, we
1762 need to operate on different values to get the minimum and
1763 maximum values for the new range. One approach is to figure
1764 out all the variations of range combinations and do the
1765 operations.
1767 However, this involves several calls to compare_values and it
1768 is pretty convoluted. It's simpler to do the 4 operations
1769 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1770 MAX1) and then figure the smallest and largest values to form
1771 the new range. */
1773 /* Divisions by zero result in a VARYING value. */
1774 if (code != MULT_EXPR
1775 && (vr0.type == VR_ANTI_RANGE || range_includes_zero_p (&vr1)))
1777 set_value_range_to_varying (vr);
1778 return;
1781 /* Compute the 4 cross operations. */
1782 sop = false;
1783 val[0] = vrp_int_const_binop (code, vr0.min, vr1.min);
1784 if (val[0] == NULL_TREE)
1785 sop = true;
1787 if (vr1.max == vr1.min)
1788 val[1] = NULL_TREE;
1789 else
1791 val[1] = vrp_int_const_binop (code, vr0.min, vr1.max);
1792 if (val[1] == NULL_TREE)
1793 sop = true;
1796 if (vr0.max == vr0.min)
1797 val[2] = NULL_TREE;
1798 else
1800 val[2] = vrp_int_const_binop (code, vr0.max, vr1.min);
1801 if (val[2] == NULL_TREE)
1802 sop = true;
1805 if (vr0.min == vr0.max || vr1.min == vr1.max)
1806 val[3] = NULL_TREE;
1807 else
1809 val[3] = vrp_int_const_binop (code, vr0.max, vr1.max);
1810 if (val[3] == NULL_TREE)
1811 sop = true;
1814 if (sop)
1816 set_value_range_to_varying (vr);
1817 return;
1820 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
1821 of VAL[i]. */
1822 min = val[0];
1823 max = val[0];
1824 for (i = 1; i < 4; i++)
1826 if (!is_gimple_min_invariant (min)
1827 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
1828 || !is_gimple_min_invariant (max)
1829 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
1830 break;
1832 if (val[i])
1834 if (!is_gimple_min_invariant (val[i])
1835 || (TREE_OVERFLOW (val[i])
1836 && !is_overflow_infinity (val[i])))
1838 /* If we found an overflowed value, set MIN and MAX
1839 to it so that we set the resulting range to
1840 VARYING. */
1841 min = max = val[i];
1842 break;
1845 if (compare_values (val[i], min) == -1)
1846 min = val[i];
1848 if (compare_values (val[i], max) == 1)
1849 max = val[i];
1853 else if (code == MINUS_EXPR)
1855 /* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
1856 VR_VARYING. It would take more effort to compute a precise
1857 range for such a case. For example, if we have op0 == 1 and
1858 op1 == 1 with their ranges both being ~[0,0], we would have
1859 op0 - op1 == 0, so we cannot claim that the difference is in
1860 ~[0,0]. Note that we are guaranteed to have
1861 vr0.type == vr1.type at this point. */
1862 if (vr0.type == VR_ANTI_RANGE)
1864 set_value_range_to_varying (vr);
1865 return;
1868 /* For MINUS_EXPR, apply the operation to the opposite ends of
1869 each range. */
1870 min = vrp_int_const_binop (code, vr0.min, vr1.max);
1871 max = vrp_int_const_binop (code, vr0.max, vr1.min);
1873 else if (code == BIT_AND_EXPR)
1875 if (vr0.type == VR_RANGE
1876 && vr0.min == vr0.max
1877 && TREE_CODE (vr0.max) == INTEGER_CST
1878 && !TREE_OVERFLOW (vr0.max)
1879 && tree_int_cst_sgn (vr0.max) >= 0)
1881 min = build_int_cst (TREE_TYPE (expr), 0);
1882 max = vr0.max;
1884 else if (vr1.type == VR_RANGE
1885 && vr1.min == vr1.max
1886 && TREE_CODE (vr1.max) == INTEGER_CST
1887 && !TREE_OVERFLOW (vr1.max)
1888 && tree_int_cst_sgn (vr1.max) >= 0)
1890 type = VR_RANGE;
1891 min = build_int_cst (TREE_TYPE (expr), 0);
1892 max = vr1.max;
1894 else
1896 set_value_range_to_varying (vr);
1897 return;
1900 else
1901 gcc_unreachable ();
1903 /* If either MIN or MAX overflowed, then set the resulting range to
1904 VARYING. But we do accept an overflow infinity
1905 representation. */
1906 if (min == NULL_TREE
1907 || !is_gimple_min_invariant (min)
1908 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
1909 || max == NULL_TREE
1910 || !is_gimple_min_invariant (max)
1911 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
1913 set_value_range_to_varying (vr);
1914 return;
1917 if ((min == TYPE_MIN_VALUE (TREE_TYPE (min))
1918 || is_negative_overflow_infinity (min))
1919 && (max == TYPE_MAX_VALUE (TREE_TYPE (max))
1920 || is_positive_overflow_infinity (max)))
1922 set_value_range_to_varying (vr);
1923 return;
1926 cmp = compare_values (min, max);
1927 if (cmp == -2 || cmp == 1)
1929 /* If the new range has its limits swapped around (MIN > MAX),
1930 then the operation caused one of them to wrap around, mark
1931 the new range VARYING. */
1932 set_value_range_to_varying (vr);
1934 else
1935 set_value_range (vr, type, min, max, NULL);
1939 /* Extract range information from a unary expression EXPR based on
1940 the range of its operand and the expression code. */
1942 static void
1943 extract_range_from_unary_expr (value_range_t *vr, tree expr)
1945 enum tree_code code = TREE_CODE (expr);
1946 tree min, max, op0;
1947 int cmp;
1948 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1950 /* Refuse to operate on certain unary expressions for which we
1951 cannot easily determine a resulting range. */
1952 if (code == FIX_TRUNC_EXPR
1953 || code == FLOAT_EXPR
1954 || code == BIT_NOT_EXPR
1955 || code == NON_LVALUE_EXPR
1956 || code == CONJ_EXPR)
1958 set_value_range_to_varying (vr);
1959 return;
1962 /* Get value ranges for the operand. For constant operands, create
1963 a new value range with the operand to simplify processing. */
1964 op0 = TREE_OPERAND (expr, 0);
1965 if (TREE_CODE (op0) == SSA_NAME)
1966 vr0 = *(get_value_range (op0));
1967 else if (is_gimple_min_invariant (op0))
1968 set_value_range (&vr0, VR_RANGE, op0, op0, NULL);
1969 else
1970 set_value_range_to_varying (&vr0);
1972 /* If VR0 is UNDEFINED, so is the result. */
1973 if (vr0.type == VR_UNDEFINED)
1975 set_value_range_to_undefined (vr);
1976 return;
1979 /* Refuse to operate on symbolic ranges, or if neither operand is
1980 a pointer or integral type. */
1981 if ((!INTEGRAL_TYPE_P (TREE_TYPE (op0))
1982 && !POINTER_TYPE_P (TREE_TYPE (op0)))
1983 || (vr0.type != VR_VARYING
1984 && symbolic_range_p (&vr0)))
1986 set_value_range_to_varying (vr);
1987 return;
1990 /* If the expression involves pointers, we are only interested in
1991 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
1992 if (POINTER_TYPE_P (TREE_TYPE (expr)) || POINTER_TYPE_P (TREE_TYPE (op0)))
1994 bool sop;
1996 sop = false;
1997 if (range_is_nonnull (&vr0)
1998 || (tree_expr_nonzero_warnv_p (expr, &sop)
1999 && !sop))
2000 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
2001 else if (range_is_null (&vr0))
2002 set_value_range_to_null (vr, TREE_TYPE (expr));
2003 else
2004 set_value_range_to_varying (vr);
2006 return;
2009 /* Handle unary expressions on integer ranges. */
2010 if (code == NOP_EXPR || code == CONVERT_EXPR)
2012 tree inner_type = TREE_TYPE (op0);
2013 tree outer_type = TREE_TYPE (expr);
2015 /* If VR0 represents a simple range, then try to convert
2016 the min and max values for the range to the same type
2017 as OUTER_TYPE. If the results compare equal to VR0's
2018 min and max values and the new min is still less than
2019 or equal to the new max, then we can safely use the newly
2020 computed range for EXPR. This allows us to compute
2021 accurate ranges through many casts. */
2022 if ((vr0.type == VR_RANGE
2023 && !overflow_infinity_range_p (&vr0))
2024 || (vr0.type == VR_VARYING
2025 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)))
2027 tree new_min, new_max, orig_min, orig_max;
2029 /* Convert the input operand min/max to OUTER_TYPE. If
2030 the input has no range information, then use the min/max
2031 for the input's type. */
2032 if (vr0.type == VR_RANGE)
2034 orig_min = vr0.min;
2035 orig_max = vr0.max;
2037 else
2039 orig_min = TYPE_MIN_VALUE (inner_type);
2040 orig_max = TYPE_MAX_VALUE (inner_type);
2043 new_min = fold_convert (outer_type, orig_min);
2044 new_max = fold_convert (outer_type, orig_max);
2046 /* Verify the new min/max values are gimple values and
2047 that they compare equal to the original input's
2048 min/max values. */
2049 if (is_gimple_val (new_min)
2050 && is_gimple_val (new_max)
2051 && tree_int_cst_equal (new_min, orig_min)
2052 && tree_int_cst_equal (new_max, orig_max)
2053 && (cmp = compare_values (new_min, new_max)) <= 0
2054 && cmp >= -1)
2056 set_value_range (vr, VR_RANGE, new_min, new_max, vr->equiv);
2057 return;
2061 /* When converting types of different sizes, set the result to
2062 VARYING. Things like sign extensions and precision loss may
2063 change the range. For instance, if x_3 is of type 'long long
2064 int' and 'y_5 = (unsigned short) x_3', if x_3 is ~[0, 0], it
2065 is impossible to know at compile time whether y_5 will be
2066 ~[0, 0]. */
2067 if (TYPE_SIZE (inner_type) != TYPE_SIZE (outer_type)
2068 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
2070 set_value_range_to_varying (vr);
2071 return;
2075 /* Conversion of a VR_VARYING value to a wider type can result
2076 in a usable range. So wait until after we've handled conversions
2077 before dropping the result to VR_VARYING if we had a source
2078 operand that is VR_VARYING. */
2079 if (vr0.type == VR_VARYING)
2081 set_value_range_to_varying (vr);
2082 return;
2085 /* Apply the operation to each end of the range and see what we end
2086 up with. */
2087 if (code == NEGATE_EXPR
2088 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
2090 /* NEGATE_EXPR flips the range around. We need to treat
2091 TYPE_MIN_VALUE specially. */
2092 if (is_positive_overflow_infinity (vr0.max))
2093 min = negative_overflow_infinity (TREE_TYPE (expr));
2094 else if (is_negative_overflow_infinity (vr0.max))
2095 min = positive_overflow_infinity (TREE_TYPE (expr));
2096 else if (vr0.max != TYPE_MIN_VALUE (TREE_TYPE (expr)))
2097 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2098 else if (needs_overflow_infinity (TREE_TYPE (expr)))
2100 if (supports_overflow_infinity (TREE_TYPE (expr)))
2101 min = positive_overflow_infinity (TREE_TYPE (expr));
2102 else
2104 set_value_range_to_varying (vr);
2105 return;
2108 else
2109 min = TYPE_MIN_VALUE (TREE_TYPE (expr));
2111 if (is_positive_overflow_infinity (vr0.min))
2112 max = negative_overflow_infinity (TREE_TYPE (expr));
2113 else if (is_negative_overflow_infinity (vr0.min))
2114 max = positive_overflow_infinity (TREE_TYPE (expr));
2115 else if (vr0.min != TYPE_MIN_VALUE (TREE_TYPE (expr)))
2116 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2117 else if (needs_overflow_infinity (TREE_TYPE (expr)))
2119 if (supports_overflow_infinity (TREE_TYPE (expr)))
2120 max = positive_overflow_infinity (TREE_TYPE (expr));
2121 else
2123 set_value_range_to_varying (vr);
2124 return;
2127 else
2128 max = TYPE_MIN_VALUE (TREE_TYPE (expr));
2130 else if (code == NEGATE_EXPR
2131 && TYPE_UNSIGNED (TREE_TYPE (expr)))
2133 if (!range_includes_zero_p (&vr0))
2135 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2136 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2138 else
2140 if (range_is_null (&vr0))
2141 set_value_range_to_null (vr, TREE_TYPE (expr));
2142 else
2143 set_value_range_to_varying (vr);
2144 return;
2147 else if (code == ABS_EXPR
2148 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
2150 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
2151 useful range. */
2152 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (expr))
2153 && ((vr0.type == VR_RANGE
2154 && vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr)))
2155 || (vr0.type == VR_ANTI_RANGE
2156 && vr0.min != TYPE_MIN_VALUE (TREE_TYPE (expr))
2157 && !range_includes_zero_p (&vr0))))
2159 set_value_range_to_varying (vr);
2160 return;
2163 /* ABS_EXPR may flip the range around, if the original range
2164 included negative values. */
2165 if (is_overflow_infinity (vr0.min))
2166 min = positive_overflow_infinity (TREE_TYPE (expr));
2167 else if (vr0.min != TYPE_MIN_VALUE (TREE_TYPE (expr)))
2168 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2169 else if (!needs_overflow_infinity (TREE_TYPE (expr)))
2170 min = TYPE_MAX_VALUE (TREE_TYPE (expr));
2171 else if (supports_overflow_infinity (TREE_TYPE (expr)))
2172 min = positive_overflow_infinity (TREE_TYPE (expr));
2173 else
2175 set_value_range_to_varying (vr);
2176 return;
2179 if (is_overflow_infinity (vr0.max))
2180 max = positive_overflow_infinity (TREE_TYPE (expr));
2181 else if (vr0.max != TYPE_MIN_VALUE (TREE_TYPE (expr)))
2182 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2183 else if (!needs_overflow_infinity (TREE_TYPE (expr)))
2184 max = TYPE_MAX_VALUE (TREE_TYPE (expr));
2185 else if (supports_overflow_infinity (TREE_TYPE (expr)))
2186 max = positive_overflow_infinity (TREE_TYPE (expr));
2187 else
2189 set_value_range_to_varying (vr);
2190 return;
2193 cmp = compare_values (min, max);
2195 /* If a VR_ANTI_RANGEs contains zero, then we have
2196 ~[-INF, min(MIN, MAX)]. */
2197 if (vr0.type == VR_ANTI_RANGE)
2199 if (range_includes_zero_p (&vr0))
2201 /* Take the lower of the two values. */
2202 if (cmp != 1)
2203 max = min;
2205 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
2206 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
2207 flag_wrapv is set and the original anti-range doesn't include
2208 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
2209 if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
2211 tree type_min_value = TYPE_MIN_VALUE (TREE_TYPE (expr));
2213 min = (vr0.min != type_min_value
2214 ? int_const_binop (PLUS_EXPR, type_min_value,
2215 integer_one_node, 0)
2216 : type_min_value);
2218 else
2220 if (overflow_infinity_range_p (&vr0))
2221 min = negative_overflow_infinity (TREE_TYPE (expr));
2222 else
2223 min = TYPE_MIN_VALUE (TREE_TYPE (expr));
2226 else
2228 /* All else has failed, so create the range [0, INF], even for
2229 flag_wrapv since TYPE_MIN_VALUE is in the original
2230 anti-range. */
2231 vr0.type = VR_RANGE;
2232 min = build_int_cst (TREE_TYPE (expr), 0);
2233 if (needs_overflow_infinity (TREE_TYPE (expr)))
2235 if (supports_overflow_infinity (TREE_TYPE (expr)))
2236 max = positive_overflow_infinity (TREE_TYPE (expr));
2237 else
2239 set_value_range_to_varying (vr);
2240 return;
2243 else
2244 max = TYPE_MAX_VALUE (TREE_TYPE (expr));
2248 /* If the range contains zero then we know that the minimum value in the
2249 range will be zero. */
2250 else if (range_includes_zero_p (&vr0))
2252 if (cmp == 1)
2253 max = min;
2254 min = build_int_cst (TREE_TYPE (expr), 0);
2256 else
2258 /* If the range was reversed, swap MIN and MAX. */
2259 if (cmp == 1)
2261 tree t = min;
2262 min = max;
2263 max = t;
2267 else
2269 /* Otherwise, operate on each end of the range. */
2270 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
2271 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
2273 if (needs_overflow_infinity (TREE_TYPE (expr)))
2275 gcc_assert (code != NEGATE_EXPR && code != ABS_EXPR);
2276 if (is_overflow_infinity (vr0.min))
2277 min = vr0.min;
2278 else if (TREE_OVERFLOW (min))
2280 if (supports_overflow_infinity (TREE_TYPE (expr)))
2281 min = (tree_int_cst_sgn (min) >= 0
2282 ? positive_overflow_infinity (TREE_TYPE (min))
2283 : negative_overflow_infinity (TREE_TYPE (min)));
2284 else
2286 set_value_range_to_varying (vr);
2287 return;
2291 if (is_overflow_infinity (vr0.max))
2292 max = vr0.max;
2293 else if (TREE_OVERFLOW (max))
2295 if (supports_overflow_infinity (TREE_TYPE (expr)))
2296 max = (tree_int_cst_sgn (max) >= 0
2297 ? positive_overflow_infinity (TREE_TYPE (max))
2298 : negative_overflow_infinity (TREE_TYPE (max)));
2299 else
2301 set_value_range_to_varying (vr);
2302 return;
2308 cmp = compare_values (min, max);
2309 if (cmp == -2 || cmp == 1)
2311 /* If the new range has its limits swapped around (MIN > MAX),
2312 then the operation caused one of them to wrap around, mark
2313 the new range VARYING. */
2314 set_value_range_to_varying (vr);
2316 else
2317 set_value_range (vr, vr0.type, min, max, NULL);
2321 /* Extract range information from a conditional expression EXPR based on
2322 the ranges of each of its operands and the expression code. */
2324 static void
2325 extract_range_from_cond_expr (value_range_t *vr, tree expr)
2327 tree op0, op1;
2328 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
2329 value_range_t vr1 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
2331 /* Get value ranges for each operand. For constant operands, create
2332 a new value range with the operand to simplify processing. */
2333 op0 = COND_EXPR_THEN (expr);
2334 if (TREE_CODE (op0) == SSA_NAME)
2335 vr0 = *(get_value_range (op0));
2336 else if (is_gimple_min_invariant (op0))
2337 set_value_range (&vr0, VR_RANGE, op0, op0, NULL);
2338 else
2339 set_value_range_to_varying (&vr0);
2341 op1 = COND_EXPR_ELSE (expr);
2342 if (TREE_CODE (op1) == SSA_NAME)
2343 vr1 = *(get_value_range (op1));
2344 else if (is_gimple_min_invariant (op1))
2345 set_value_range (&vr1, VR_RANGE, op1, op1, NULL);
2346 else
2347 set_value_range_to_varying (&vr1);
2349 /* The resulting value range is the union of the operand ranges */
2350 vrp_meet (&vr0, &vr1);
2351 copy_value_range (vr, &vr0);
2355 /* Extract range information from a comparison expression EXPR based
2356 on the range of its operand and the expression code. */
2358 static void
2359 extract_range_from_comparison (value_range_t *vr, tree expr)
2361 bool sop = false;
2362 tree val = vrp_evaluate_conditional (expr, false, &sop);
2364 /* A disadvantage of using a special infinity as an overflow
2365 representation is that we lose the ability to record overflow
2366 when we don't have an infinity. So we have to ignore a result
2367 which relies on overflow. */
2369 if (val && !is_overflow_infinity (val) && !sop)
2371 /* Since this expression was found on the RHS of an assignment,
2372 its type may be different from _Bool. Convert VAL to EXPR's
2373 type. */
2374 val = fold_convert (TREE_TYPE (expr), val);
2375 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
2377 else
2378 /* The result of a comparison is always true or false. */
2379 set_value_range_to_truthvalue (vr, TREE_TYPE (expr));
2383 /* Try to compute a useful range out of expression EXPR and store it
2384 in *VR. */
2386 static void
2387 extract_range_from_expr (value_range_t *vr, tree expr)
2389 enum tree_code code = TREE_CODE (expr);
2391 if (code == ASSERT_EXPR)
2392 extract_range_from_assert (vr, expr);
2393 else if (code == SSA_NAME)
2394 extract_range_from_ssa_name (vr, expr);
2395 else if (TREE_CODE_CLASS (code) == tcc_binary
2396 || code == TRUTH_ANDIF_EXPR
2397 || code == TRUTH_ORIF_EXPR
2398 || code == TRUTH_AND_EXPR
2399 || code == TRUTH_OR_EXPR
2400 || code == TRUTH_XOR_EXPR)
2401 extract_range_from_binary_expr (vr, expr);
2402 else if (TREE_CODE_CLASS (code) == tcc_unary)
2403 extract_range_from_unary_expr (vr, expr);
2404 else if (code == COND_EXPR)
2405 extract_range_from_cond_expr (vr, expr);
2406 else if (TREE_CODE_CLASS (code) == tcc_comparison)
2407 extract_range_from_comparison (vr, expr);
2408 else if (is_gimple_min_invariant (expr))
2409 set_value_range (vr, VR_RANGE, expr, expr, NULL);
2410 else
2411 set_value_range_to_varying (vr);
2413 /* If we got a varying range from the tests above, try a final
2414 time to derive a nonnegative or nonzero range. This time
2415 relying primarily on generic routines in fold in conjunction
2416 with range data. */
2417 if (vr->type == VR_VARYING)
2419 bool sop = false;
2421 if (INTEGRAL_TYPE_P (TREE_TYPE (expr))
2422 && vrp_expr_computes_nonnegative (expr, &sop))
2423 set_value_range_to_nonnegative (vr, TREE_TYPE (expr),
2424 sop || is_overflow_infinity (expr));
2425 else if (vrp_expr_computes_nonzero (expr, &sop)
2426 && !sop)
2427 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
2431 /* Given a range VR, a LOOP and a variable VAR, determine whether it
2432 would be profitable to adjust VR using scalar evolution information
2433 for VAR. If so, update VR with the new limits. */
2435 static void
2436 adjust_range_with_scev (value_range_t *vr, struct loop *loop, tree stmt,
2437 tree var)
2439 tree init, step, chrec, tmin, tmax, min, max, type;
2440 enum ev_direction dir;
2442 /* TODO. Don't adjust anti-ranges. An anti-range may provide
2443 better opportunities than a regular range, but I'm not sure. */
2444 if (vr->type == VR_ANTI_RANGE)
2445 return;
2447 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
2448 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2449 return;
2451 init = initial_condition_in_loop_num (chrec, loop->num);
2452 step = evolution_part_in_loop_num (chrec, loop->num);
2454 /* If STEP is symbolic, we can't know whether INIT will be the
2455 minimum or maximum value in the range. Also, unless INIT is
2456 a simple expression, compare_values and possibly other functions
2457 in tree-vrp won't be able to handle it. */
2458 if (step == NULL_TREE
2459 || !is_gimple_min_invariant (step)
2460 || !valid_value_p (init))
2461 return;
2463 dir = scev_direction (chrec);
2464 if (/* Do not adjust ranges if we do not know whether the iv increases
2465 or decreases, ... */
2466 dir == EV_DIR_UNKNOWN
2467 /* ... or if it may wrap. */
2468 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
2469 true))
2470 return;
2472 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
2473 negative_overflow_infinity and positive_overflow_infinity,
2474 because we have concluded that the loop probably does not
2475 wrap. */
2477 type = TREE_TYPE (var);
2478 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
2479 tmin = lower_bound_in_type (type, type);
2480 else
2481 tmin = TYPE_MIN_VALUE (type);
2482 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
2483 tmax = upper_bound_in_type (type, type);
2484 else
2485 tmax = TYPE_MAX_VALUE (type);
2487 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2489 min = tmin;
2490 max = tmax;
2492 /* For VARYING or UNDEFINED ranges, just about anything we get
2493 from scalar evolutions should be better. */
2495 if (dir == EV_DIR_DECREASES)
2496 max = init;
2497 else
2498 min = init;
2500 /* If we would create an invalid range, then just assume we
2501 know absolutely nothing. This may be over-conservative,
2502 but it's clearly safe, and should happen only in unreachable
2503 parts of code, or for invalid programs. */
2504 if (compare_values (min, max) == 1)
2505 return;
2507 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2509 else if (vr->type == VR_RANGE)
2511 min = vr->min;
2512 max = vr->max;
2514 if (dir == EV_DIR_DECREASES)
2516 /* INIT is the maximum value. If INIT is lower than VR->MAX
2517 but no smaller than VR->MIN, set VR->MAX to INIT. */
2518 if (compare_values (init, max) == -1)
2520 max = init;
2522 /* If we just created an invalid range with the minimum
2523 greater than the maximum, we fail conservatively.
2524 This should happen only in unreachable
2525 parts of code, or for invalid programs. */
2526 if (compare_values (min, max) == 1)
2527 return;
2530 else
2532 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
2533 if (compare_values (init, min) == 1)
2535 min = init;
2537 /* Again, avoid creating invalid range by failing. */
2538 if (compare_values (min, max) == 1)
2539 return;
2543 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2548 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
2550 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
2551 all the values in the ranges.
2553 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
2555 - Return NULL_TREE if it is not always possible to determine the
2556 value of the comparison.
2558 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
2559 overflow infinity was used in the test. */
2562 static tree
2563 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
2564 bool *strict_overflow_p)
2566 /* VARYING or UNDEFINED ranges cannot be compared. */
2567 if (vr0->type == VR_VARYING
2568 || vr0->type == VR_UNDEFINED
2569 || vr1->type == VR_VARYING
2570 || vr1->type == VR_UNDEFINED)
2571 return NULL_TREE;
2573 /* Anti-ranges need to be handled separately. */
2574 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
2576 /* If both are anti-ranges, then we cannot compute any
2577 comparison. */
2578 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
2579 return NULL_TREE;
2581 /* These comparisons are never statically computable. */
2582 if (comp == GT_EXPR
2583 || comp == GE_EXPR
2584 || comp == LT_EXPR
2585 || comp == LE_EXPR)
2586 return NULL_TREE;
2588 /* Equality can be computed only between a range and an
2589 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
2590 if (vr0->type == VR_RANGE)
2592 /* To simplify processing, make VR0 the anti-range. */
2593 value_range_t *tmp = vr0;
2594 vr0 = vr1;
2595 vr1 = tmp;
2598 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
2600 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
2601 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
2602 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2604 return NULL_TREE;
2607 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
2608 operands around and change the comparison code. */
2609 if (comp == GT_EXPR || comp == GE_EXPR)
2611 value_range_t *tmp;
2612 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
2613 tmp = vr0;
2614 vr0 = vr1;
2615 vr1 = tmp;
2618 if (comp == EQ_EXPR)
2620 /* Equality may only be computed if both ranges represent
2621 exactly one value. */
2622 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
2623 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
2625 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
2626 strict_overflow_p);
2627 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
2628 strict_overflow_p);
2629 if (cmp_min == 0 && cmp_max == 0)
2630 return boolean_true_node;
2631 else if (cmp_min != -2 && cmp_max != -2)
2632 return boolean_false_node;
2634 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
2635 else if (compare_values_warnv (vr0->min, vr1->max,
2636 strict_overflow_p) == 1
2637 || compare_values_warnv (vr1->min, vr0->max,
2638 strict_overflow_p) == 1)
2639 return boolean_false_node;
2641 return NULL_TREE;
2643 else if (comp == NE_EXPR)
2645 int cmp1, cmp2;
2647 /* If VR0 is completely to the left or completely to the right
2648 of VR1, they are always different. Notice that we need to
2649 make sure that both comparisons yield similar results to
2650 avoid comparing values that cannot be compared at
2651 compile-time. */
2652 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
2653 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
2654 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
2655 return boolean_true_node;
2657 /* If VR0 and VR1 represent a single value and are identical,
2658 return false. */
2659 else if (compare_values_warnv (vr0->min, vr0->max,
2660 strict_overflow_p) == 0
2661 && compare_values_warnv (vr1->min, vr1->max,
2662 strict_overflow_p) == 0
2663 && compare_values_warnv (vr0->min, vr1->min,
2664 strict_overflow_p) == 0
2665 && compare_values_warnv (vr0->max, vr1->max,
2666 strict_overflow_p) == 0)
2667 return boolean_false_node;
2669 /* Otherwise, they may or may not be different. */
2670 else
2671 return NULL_TREE;
2673 else if (comp == LT_EXPR || comp == LE_EXPR)
2675 int tst;
2677 /* If VR0 is to the left of VR1, return true. */
2678 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
2679 if ((comp == LT_EXPR && tst == -1)
2680 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2682 if (overflow_infinity_range_p (vr0)
2683 || overflow_infinity_range_p (vr1))
2684 *strict_overflow_p = true;
2685 return boolean_true_node;
2688 /* If VR0 is to the right of VR1, return false. */
2689 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
2690 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2691 || (comp == LE_EXPR && tst == 1))
2693 if (overflow_infinity_range_p (vr0)
2694 || overflow_infinity_range_p (vr1))
2695 *strict_overflow_p = true;
2696 return boolean_false_node;
2699 /* Otherwise, we don't know. */
2700 return NULL_TREE;
2703 gcc_unreachable ();
2707 /* Given a value range VR, a value VAL and a comparison code COMP, return
2708 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
2709 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
2710 always returns false. Return NULL_TREE if it is not always
2711 possible to determine the value of the comparison. Also set
2712 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
2713 infinity was used in the test. */
2715 static tree
2716 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
2717 bool *strict_overflow_p)
2719 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2720 return NULL_TREE;
2722 /* Anti-ranges need to be handled separately. */
2723 if (vr->type == VR_ANTI_RANGE)
2725 /* For anti-ranges, the only predicates that we can compute at
2726 compile time are equality and inequality. */
2727 if (comp == GT_EXPR
2728 || comp == GE_EXPR
2729 || comp == LT_EXPR
2730 || comp == LE_EXPR)
2731 return NULL_TREE;
2733 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
2734 if (value_inside_range (val, vr) == 1)
2735 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2737 return NULL_TREE;
2740 if (comp == EQ_EXPR)
2742 /* EQ_EXPR may only be computed if VR represents exactly
2743 one value. */
2744 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
2746 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
2747 if (cmp == 0)
2748 return boolean_true_node;
2749 else if (cmp == -1 || cmp == 1 || cmp == 2)
2750 return boolean_false_node;
2752 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
2753 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
2754 return boolean_false_node;
2756 return NULL_TREE;
2758 else if (comp == NE_EXPR)
2760 /* If VAL is not inside VR, then they are always different. */
2761 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
2762 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
2763 return boolean_true_node;
2765 /* If VR represents exactly one value equal to VAL, then return
2766 false. */
2767 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
2768 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
2769 return boolean_false_node;
2771 /* Otherwise, they may or may not be different. */
2772 return NULL_TREE;
2774 else if (comp == LT_EXPR || comp == LE_EXPR)
2776 int tst;
2778 /* If VR is to the left of VAL, return true. */
2779 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
2780 if ((comp == LT_EXPR && tst == -1)
2781 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2783 if (overflow_infinity_range_p (vr))
2784 *strict_overflow_p = true;
2785 return boolean_true_node;
2788 /* If VR is to the right of VAL, return false. */
2789 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
2790 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2791 || (comp == LE_EXPR && tst == 1))
2793 if (overflow_infinity_range_p (vr))
2794 *strict_overflow_p = true;
2795 return boolean_false_node;
2798 /* Otherwise, we don't know. */
2799 return NULL_TREE;
2801 else if (comp == GT_EXPR || comp == GE_EXPR)
2803 int tst;
2805 /* If VR is to the right of VAL, return true. */
2806 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
2807 if ((comp == GT_EXPR && tst == 1)
2808 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
2810 if (overflow_infinity_range_p (vr))
2811 *strict_overflow_p = true;
2812 return boolean_true_node;
2815 /* If VR is to the left of VAL, return false. */
2816 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
2817 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
2818 || (comp == GE_EXPR && tst == -1))
2820 if (overflow_infinity_range_p (vr))
2821 *strict_overflow_p = true;
2822 return boolean_false_node;
2825 /* Otherwise, we don't know. */
2826 return NULL_TREE;
2829 gcc_unreachable ();
2833 /* Debugging dumps. */
2835 void dump_value_range (FILE *, value_range_t *);
2836 void debug_value_range (value_range_t *);
2837 void dump_all_value_ranges (FILE *);
2838 void debug_all_value_ranges (void);
2839 void dump_vr_equiv (FILE *, bitmap);
2840 void debug_vr_equiv (bitmap);
2843 /* Dump value range VR to FILE. */
2845 void
2846 dump_value_range (FILE *file, value_range_t *vr)
2848 if (vr == NULL)
2849 fprintf (file, "[]");
2850 else if (vr->type == VR_UNDEFINED)
2851 fprintf (file, "UNDEFINED");
2852 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
2854 tree type = TREE_TYPE (vr->min);
2856 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
2858 if (INTEGRAL_TYPE_P (type)
2859 && !TYPE_UNSIGNED (type)
2860 && vr->min == TYPE_MIN_VALUE (type))
2861 fprintf (file, "-INF");
2862 else if (needs_overflow_infinity (type)
2863 && is_negative_overflow_infinity (vr->min))
2864 fprintf (file, "-INF(OVF)");
2865 else
2866 print_generic_expr (file, vr->min, 0);
2868 fprintf (file, ", ");
2870 if (INTEGRAL_TYPE_P (type)
2871 && vr->max == TYPE_MAX_VALUE (type))
2872 fprintf (file, "+INF");
2873 else if (needs_overflow_infinity (type)
2874 && is_positive_overflow_infinity (vr->max))
2875 fprintf (file, "+INF(OVF)");
2876 else
2877 print_generic_expr (file, vr->max, 0);
2879 fprintf (file, "]");
2881 if (vr->equiv)
2883 bitmap_iterator bi;
2884 unsigned i, c = 0;
2886 fprintf (file, " EQUIVALENCES: { ");
2888 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
2890 print_generic_expr (file, ssa_name (i), 0);
2891 fprintf (file, " ");
2892 c++;
2895 fprintf (file, "} (%u elements)", c);
2898 else if (vr->type == VR_VARYING)
2899 fprintf (file, "VARYING");
2900 else
2901 fprintf (file, "INVALID RANGE");
2905 /* Dump value range VR to stderr. */
2907 void
2908 debug_value_range (value_range_t *vr)
2910 dump_value_range (stderr, vr);
2911 fprintf (stderr, "\n");
2915 /* Dump value ranges of all SSA_NAMEs to FILE. */
2917 void
2918 dump_all_value_ranges (FILE *file)
2920 size_t i;
2922 for (i = 0; i < num_ssa_names; i++)
2924 if (vr_value[i])
2926 print_generic_expr (file, ssa_name (i), 0);
2927 fprintf (file, ": ");
2928 dump_value_range (file, vr_value[i]);
2929 fprintf (file, "\n");
2933 fprintf (file, "\n");
2937 /* Dump all value ranges to stderr. */
2939 void
2940 debug_all_value_ranges (void)
2942 dump_all_value_ranges (stderr);
2946 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2947 create a new SSA name N and return the assertion assignment
2948 'V = ASSERT_EXPR <V, V OP W>'. */
2950 static tree
2951 build_assert_expr_for (tree cond, tree v)
2953 tree n, assertion;
2955 gcc_assert (TREE_CODE (v) == SSA_NAME);
2956 n = duplicate_ssa_name (v, NULL_TREE);
2958 if (COMPARISON_CLASS_P (cond))
2960 tree a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
2961 assertion = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (v), n, a);
2963 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
2965 /* Given !V, build the assignment N = false. */
2966 tree op0 = TREE_OPERAND (cond, 0);
2967 gcc_assert (op0 == v);
2968 assertion = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (v), n,
2969 boolean_false_node);
2971 else if (TREE_CODE (cond) == SSA_NAME)
2973 /* Given V, build the assignment N = true. */
2974 gcc_assert (v == cond);
2975 assertion = build2 (GIMPLE_MODIFY_STMT,
2976 TREE_TYPE (v), n, boolean_true_node);
2978 else
2979 gcc_unreachable ();
2981 SSA_NAME_DEF_STMT (n) = assertion;
2983 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2984 operand of the ASSERT_EXPR. Register the new name and the old one
2985 in the replacement table so that we can fix the SSA web after
2986 adding all the ASSERT_EXPRs. */
2987 register_new_name_mapping (n, v);
2989 return assertion;
2993 /* Return false if EXPR is a predicate expression involving floating
2994 point values. */
2996 static inline bool
2997 fp_predicate (tree expr)
2999 return (COMPARISON_CLASS_P (expr)
3000 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0))));
3004 /* If the range of values taken by OP can be inferred after STMT executes,
3005 return the comparison code (COMP_CODE_P) and value (VAL_P) that
3006 describes the inferred range. Return true if a range could be
3007 inferred. */
3009 static bool
3010 infer_value_range (tree stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
3012 *val_p = NULL_TREE;
3013 *comp_code_p = ERROR_MARK;
3015 /* Do not attempt to infer anything in names that flow through
3016 abnormal edges. */
3017 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
3018 return false;
3020 /* Similarly, don't infer anything from statements that may throw
3021 exceptions. */
3022 if (tree_could_throw_p (stmt))
3023 return false;
3025 /* If STMT is the last statement of a basic block with no
3026 successors, there is no point inferring anything about any of its
3027 operands. We would not be able to find a proper insertion point
3028 for the assertion, anyway. */
3029 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (bb_for_stmt (stmt)->succs) == 0)
3030 return false;
3032 /* We can only assume that a pointer dereference will yield
3033 non-NULL if -fdelete-null-pointer-checks is enabled. */
3034 if (flag_delete_null_pointer_checks && POINTER_TYPE_P (TREE_TYPE (op)))
3036 bool is_store;
3037 unsigned num_uses, num_derefs;
3039 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs, &is_store);
3040 if (num_derefs > 0)
3042 *val_p = build_int_cst (TREE_TYPE (op), 0);
3043 *comp_code_p = NE_EXPR;
3044 return true;
3048 return false;
3052 void dump_asserts_for (FILE *, tree);
3053 void debug_asserts_for (tree);
3054 void dump_all_asserts (FILE *);
3055 void debug_all_asserts (void);
3057 /* Dump all the registered assertions for NAME to FILE. */
3059 void
3060 dump_asserts_for (FILE *file, tree name)
3062 assert_locus_t loc;
3064 fprintf (file, "Assertions to be inserted for ");
3065 print_generic_expr (file, name, 0);
3066 fprintf (file, "\n");
3068 loc = asserts_for[SSA_NAME_VERSION (name)];
3069 while (loc)
3071 fprintf (file, "\t");
3072 print_generic_expr (file, bsi_stmt (loc->si), 0);
3073 fprintf (file, "\n\tBB #%d", loc->bb->index);
3074 if (loc->e)
3076 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
3077 loc->e->dest->index);
3078 dump_edge_info (file, loc->e, 0);
3080 fprintf (file, "\n\tPREDICATE: ");
3081 print_generic_expr (file, name, 0);
3082 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
3083 print_generic_expr (file, loc->val, 0);
3084 fprintf (file, "\n\n");
3085 loc = loc->next;
3088 fprintf (file, "\n");
3092 /* Dump all the registered assertions for NAME to stderr. */
3094 void
3095 debug_asserts_for (tree name)
3097 dump_asserts_for (stderr, name);
3101 /* Dump all the registered assertions for all the names to FILE. */
3103 void
3104 dump_all_asserts (FILE *file)
3106 unsigned i;
3107 bitmap_iterator bi;
3109 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
3110 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3111 dump_asserts_for (file, ssa_name (i));
3112 fprintf (file, "\n");
3116 /* Dump all the registered assertions for all the names to stderr. */
3118 void
3119 debug_all_asserts (void)
3121 dump_all_asserts (stderr);
3125 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
3126 'NAME COMP_CODE VAL' at a location that dominates block BB or
3127 E->DEST, then register this location as a possible insertion point
3128 for ASSERT_EXPR <NAME, NAME COMP_CODE VAL>.
3130 BB, E and SI provide the exact insertion point for the new
3131 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
3132 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
3133 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
3134 must not be NULL. */
3136 static void
3137 register_new_assert_for (tree name,
3138 enum tree_code comp_code,
3139 tree val,
3140 basic_block bb,
3141 edge e,
3142 block_stmt_iterator si)
3144 assert_locus_t n, loc, last_loc;
3145 bool found;
3146 basic_block dest_bb;
3148 #if defined ENABLE_CHECKING
3149 gcc_assert (bb == NULL || e == NULL);
3151 if (e == NULL)
3152 gcc_assert (TREE_CODE (bsi_stmt (si)) != COND_EXPR
3153 && TREE_CODE (bsi_stmt (si)) != SWITCH_EXPR);
3154 #endif
3156 /* The new assertion A will be inserted at BB or E. We need to
3157 determine if the new location is dominated by a previously
3158 registered location for A. If we are doing an edge insertion,
3159 assume that A will be inserted at E->DEST. Note that this is not
3160 necessarily true.
3162 If E is a critical edge, it will be split. But even if E is
3163 split, the new block will dominate the same set of blocks that
3164 E->DEST dominates.
3166 The reverse, however, is not true, blocks dominated by E->DEST
3167 will not be dominated by the new block created to split E. So,
3168 if the insertion location is on a critical edge, we will not use
3169 the new location to move another assertion previously registered
3170 at a block dominated by E->DEST. */
3171 dest_bb = (bb) ? bb : e->dest;
3173 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
3174 VAL at a block dominating DEST_BB, then we don't need to insert a new
3175 one. Similarly, if the same assertion already exists at a block
3176 dominated by DEST_BB and the new location is not on a critical
3177 edge, then update the existing location for the assertion (i.e.,
3178 move the assertion up in the dominance tree).
3180 Note, this is implemented as a simple linked list because there
3181 should not be more than a handful of assertions registered per
3182 name. If this becomes a performance problem, a table hashed by
3183 COMP_CODE and VAL could be implemented. */
3184 loc = asserts_for[SSA_NAME_VERSION (name)];
3185 last_loc = loc;
3186 found = false;
3187 while (loc)
3189 if (loc->comp_code == comp_code
3190 && (loc->val == val
3191 || operand_equal_p (loc->val, val, 0)))
3193 /* If the assertion NAME COMP_CODE VAL has already been
3194 registered at a basic block that dominates DEST_BB, then
3195 we don't need to insert the same assertion again. Note
3196 that we don't check strict dominance here to avoid
3197 replicating the same assertion inside the same basic
3198 block more than once (e.g., when a pointer is
3199 dereferenced several times inside a block).
3201 An exception to this rule are edge insertions. If the
3202 new assertion is to be inserted on edge E, then it will
3203 dominate all the other insertions that we may want to
3204 insert in DEST_BB. So, if we are doing an edge
3205 insertion, don't do this dominance check. */
3206 if (e == NULL
3207 && dominated_by_p (CDI_DOMINATORS, dest_bb, loc->bb))
3208 return;
3210 /* Otherwise, if E is not a critical edge and DEST_BB
3211 dominates the existing location for the assertion, move
3212 the assertion up in the dominance tree by updating its
3213 location information. */
3214 if ((e == NULL || !EDGE_CRITICAL_P (e))
3215 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
3217 loc->bb = dest_bb;
3218 loc->e = e;
3219 loc->si = si;
3220 return;
3224 /* Update the last node of the list and move to the next one. */
3225 last_loc = loc;
3226 loc = loc->next;
3229 /* If we didn't find an assertion already registered for
3230 NAME COMP_CODE VAL, add a new one at the end of the list of
3231 assertions associated with NAME. */
3232 n = XNEW (struct assert_locus_d);
3233 n->bb = dest_bb;
3234 n->e = e;
3235 n->si = si;
3236 n->comp_code = comp_code;
3237 n->val = val;
3238 n->next = NULL;
3240 if (last_loc)
3241 last_loc->next = n;
3242 else
3243 asserts_for[SSA_NAME_VERSION (name)] = n;
3245 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
3248 /* COND is a predicate which uses NAME. Extract a suitable test code
3249 and value and store them into *CODE_P and *VAL_P so the predicate
3250 is normalized to NAME *CODE_P *VAL_P.
3252 If no extraction was possible, return FALSE, otherwise return TRUE.
3254 If INVERT is true, then we invert the result stored into *CODE_P. */
3256 static bool
3257 extract_code_and_val_from_cond (tree name, tree cond, bool invert,
3258 enum tree_code *code_p, tree *val_p)
3260 enum tree_code comp_code;
3261 tree val;
3263 /* Predicates may be a single SSA name or NAME OP VAL. */
3264 if (cond == name)
3266 /* If the predicate is a name, it must be NAME, in which
3267 case we create the predicate NAME == true or
3268 NAME == false accordingly. */
3269 comp_code = EQ_EXPR;
3270 val = invert ? boolean_false_node : boolean_true_node;
3272 else
3274 /* Otherwise, we have a comparison of the form NAME COMP VAL
3275 or VAL COMP NAME. */
3276 if (name == TREE_OPERAND (cond, 1))
3278 /* If the predicate is of the form VAL COMP NAME, flip
3279 COMP around because we need to register NAME as the
3280 first operand in the predicate. */
3281 comp_code = swap_tree_comparison (TREE_CODE (cond));
3282 val = TREE_OPERAND (cond, 0);
3284 else
3286 /* The comparison is of the form NAME COMP VAL, so the
3287 comparison code remains unchanged. */
3288 comp_code = TREE_CODE (cond);
3289 val = TREE_OPERAND (cond, 1);
3292 /* Invert the comparison code as necessary. */
3293 if (invert)
3294 comp_code = invert_tree_comparison (comp_code, 0);
3296 /* VRP does not handle float types. */
3297 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
3298 return false;
3300 /* Do not register always-false predicates.
3301 FIXME: this works around a limitation in fold() when dealing with
3302 enumerations. Given 'enum { N1, N2 } x;', fold will not
3303 fold 'if (x > N2)' to 'if (0)'. */
3304 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
3305 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
3307 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
3308 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
3310 if (comp_code == GT_EXPR
3311 && (!max
3312 || compare_values (val, max) == 0))
3313 return false;
3315 if (comp_code == LT_EXPR
3316 && (!min
3317 || compare_values (val, min) == 0))
3318 return false;
3321 *code_p = comp_code;
3322 *val_p = val;
3323 return true;
3326 /* OP is an operand of a truth value expression which is known to have
3327 a particular value. Register any asserts for OP and for any
3328 operands in OP's defining statement.
3330 If CODE is EQ_EXPR, then we want to register OP is zero (false),
3331 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
3333 static bool
3334 register_edge_assert_for_1 (tree op, enum tree_code code,
3335 edge e, block_stmt_iterator bsi)
3337 bool retval = false;
3338 tree op_def, rhs, val;
3340 /* We only care about SSA_NAMEs. */
3341 if (TREE_CODE (op) != SSA_NAME)
3342 return false;
3344 /* We know that OP will have a zero or nonzero value. If OP is used
3345 more than once go ahead and register an assert for OP.
3347 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
3348 it will always be set for OP (because OP is used in a COND_EXPR in
3349 the subgraph). */
3350 if (!has_single_use (op))
3352 val = build_int_cst (TREE_TYPE (op), 0);
3353 register_new_assert_for (op, code, val, NULL, e, bsi);
3354 retval = true;
3357 /* Now look at how OP is set. If it's set from a comparison,
3358 a truth operation or some bit operations, then we may be able
3359 to register information about the operands of that assignment. */
3360 op_def = SSA_NAME_DEF_STMT (op);
3361 if (TREE_CODE (op_def) != GIMPLE_MODIFY_STMT)
3362 return retval;
3364 rhs = GIMPLE_STMT_OPERAND (op_def, 1);
3366 if (COMPARISON_CLASS_P (rhs))
3368 bool invert = (code == EQ_EXPR ? true : false);
3369 tree op0 = TREE_OPERAND (rhs, 0);
3370 tree op1 = TREE_OPERAND (rhs, 1);
3372 /* Conditionally register an assert for each SSA_NAME in the
3373 comparison. */
3374 if (TREE_CODE (op0) == SSA_NAME
3375 && !has_single_use (op0)
3376 && extract_code_and_val_from_cond (op0, rhs,
3377 invert, &code, &val))
3379 register_new_assert_for (op0, code, val, NULL, e, bsi);
3380 retval = true;
3383 /* Similarly for the second operand of the comparison. */
3384 if (TREE_CODE (op1) == SSA_NAME
3385 && !has_single_use (op1)
3386 && extract_code_and_val_from_cond (op1, rhs,
3387 invert, &code, &val))
3389 register_new_assert_for (op1, code, val, NULL, e, bsi);
3390 retval = true;
3393 else if ((code == NE_EXPR
3394 && (TREE_CODE (rhs) == TRUTH_AND_EXPR
3395 || TREE_CODE (rhs) == BIT_AND_EXPR))
3396 || (code == EQ_EXPR
3397 && (TREE_CODE (rhs) == TRUTH_OR_EXPR
3398 || TREE_CODE (rhs) == BIT_IOR_EXPR)))
3400 /* Recurse on each operand. */
3401 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 0),
3402 code, e, bsi);
3403 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 1),
3404 code, e, bsi);
3406 else if (TREE_CODE (rhs) == TRUTH_NOT_EXPR)
3408 /* Recurse, flipping CODE. */
3409 code = invert_tree_comparison (code, false);
3410 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 0),
3411 code, e, bsi);
3413 else if (TREE_CODE (rhs) == SSA_NAME)
3415 /* Recurse through the copy. */
3416 retval |= register_edge_assert_for_1 (rhs, code, e, bsi);
3418 else if (TREE_CODE (rhs) == NOP_EXPR
3419 || TREE_CODE (rhs) == CONVERT_EXPR
3420 || TREE_CODE (rhs) == VIEW_CONVERT_EXPR
3421 || TREE_CODE (rhs) == NON_LVALUE_EXPR)
3423 /* Recurse through the type conversion. */
3424 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 0),
3425 code, e, bsi);
3428 return retval;
3431 /* Try to register an edge assertion for SSA name NAME on edge E for
3432 the condition COND contributing to the conditional jump pointed to by SI.
3433 Return true if an assertion for NAME could be registered. */
3435 static bool
3436 register_edge_assert_for (tree name, edge e, block_stmt_iterator si, tree cond)
3438 tree val;
3439 enum tree_code comp_code;
3440 bool retval = false;
3441 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
3443 /* Do not attempt to infer anything in names that flow through
3444 abnormal edges. */
3445 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
3446 return false;
3448 if (!extract_code_and_val_from_cond (name, cond, is_else_edge,
3449 &comp_code, &val))
3450 return false;
3452 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
3453 reachable from E. */
3454 if (TEST_BIT (found_in_subgraph, SSA_NAME_VERSION (name)))
3456 register_new_assert_for (name, comp_code, val, NULL, e, si);
3457 retval = true;
3460 /* If COND is effectively an equality test of an SSA_NAME against
3461 the value zero or one, then we may be able to assert values
3462 for SSA_NAMEs which flow into COND. */
3464 /* In the case of NAME == 1 or NAME != 0, for TRUTH_AND_EXPR defining
3465 statement of NAME we can assert both operands of the TRUTH_AND_EXPR
3466 have nonzero value. */
3467 if (((comp_code == EQ_EXPR && integer_onep (val))
3468 || (comp_code == NE_EXPR && integer_zerop (val))))
3470 tree def_stmt = SSA_NAME_DEF_STMT (name);
3472 if (TREE_CODE (def_stmt) == GIMPLE_MODIFY_STMT
3473 && (TREE_CODE (GIMPLE_STMT_OPERAND (def_stmt, 1)) == TRUTH_AND_EXPR
3474 || TREE_CODE (GIMPLE_STMT_OPERAND (def_stmt, 1)) == BIT_AND_EXPR))
3476 tree op0 = TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1), 0);
3477 tree op1 = TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1), 1);
3478 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
3479 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
3483 /* In the case of NAME == 0 or NAME != 1, for TRUTH_OR_EXPR defining
3484 statement of NAME we can assert both operands of the TRUTH_OR_EXPR
3485 have zero value. */
3486 if (((comp_code == EQ_EXPR && integer_zerop (val))
3487 || (comp_code == NE_EXPR && integer_onep (val))))
3489 tree def_stmt = SSA_NAME_DEF_STMT (name);
3491 if (TREE_CODE (def_stmt) == GIMPLE_MODIFY_STMT
3492 && (TREE_CODE (GIMPLE_STMT_OPERAND (def_stmt, 1)) == TRUTH_OR_EXPR
3493 || TREE_CODE (GIMPLE_STMT_OPERAND (def_stmt, 1)) == BIT_IOR_EXPR))
3495 tree op0 = TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1), 0);
3496 tree op1 = TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1), 1);
3497 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
3498 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
3502 return retval;
3506 static bool find_assert_locations (basic_block bb);
3508 /* Determine whether the outgoing edges of BB should receive an
3509 ASSERT_EXPR for each of the operands of BB's LAST statement.
3510 The last statement of BB must be a COND_EXPR or a SWITCH_EXPR.
3512 If any of the sub-graphs rooted at BB have an interesting use of
3513 the predicate operands, an assert location node is added to the
3514 list of assertions for the corresponding operands. */
3516 static bool
3517 find_conditional_asserts (basic_block bb, tree last)
3519 bool need_assert;
3520 block_stmt_iterator bsi;
3521 tree op;
3522 edge_iterator ei;
3523 edge e;
3524 ssa_op_iter iter;
3526 need_assert = false;
3527 bsi = bsi_for_stmt (last);
3529 /* Look for uses of the operands in each of the sub-graphs
3530 rooted at BB. We need to check each of the outgoing edges
3531 separately, so that we know what kind of ASSERT_EXPR to
3532 insert. */
3533 FOR_EACH_EDGE (e, ei, bb->succs)
3535 if (e->dest == bb)
3536 continue;
3538 /* Remove the COND_EXPR operands from the FOUND_IN_SUBGRAPH bitmap.
3539 Otherwise, when we finish traversing each of the sub-graphs, we
3540 won't know whether the variables were found in the sub-graphs or
3541 if they had been found in a block upstream from BB.
3543 This is actually a bad idea is some cases, particularly jump
3544 threading. Consider a CFG like the following:
3554 Assume that one or more operands in the conditional at the
3555 end of block 0 are used in a conditional in block 2, but not
3556 anywhere in block 1. In this case we will not insert any
3557 assert statements in block 1, which may cause us to miss
3558 opportunities to optimize, particularly for jump threading. */
3559 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3560 RESET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3562 /* Traverse the strictly dominated sub-graph rooted at E->DEST
3563 to determine if any of the operands in the conditional
3564 predicate are used. */
3565 if (e->dest != bb)
3566 need_assert |= find_assert_locations (e->dest);
3568 /* Register the necessary assertions for each operand in the
3569 conditional predicate. */
3570 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3571 need_assert |= register_edge_assert_for (op, e, bsi,
3572 COND_EXPR_COND (last));
3575 /* Finally, indicate that we have found the operands in the
3576 conditional. */
3577 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3578 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3580 return need_assert;
3584 /* Traverse all the statements in block BB looking for statements that
3585 may generate useful assertions for the SSA names in their operand.
3586 If a statement produces a useful assertion A for name N_i, then the
3587 list of assertions already generated for N_i is scanned to
3588 determine if A is actually needed.
3590 If N_i already had the assertion A at a location dominating the
3591 current location, then nothing needs to be done. Otherwise, the
3592 new location for A is recorded instead.
3594 1- For every statement S in BB, all the variables used by S are
3595 added to bitmap FOUND_IN_SUBGRAPH.
3597 2- If statement S uses an operand N in a way that exposes a known
3598 value range for N, then if N was not already generated by an
3599 ASSERT_EXPR, create a new assert location for N. For instance,
3600 if N is a pointer and the statement dereferences it, we can
3601 assume that N is not NULL.
3603 3- COND_EXPRs are a special case of #2. We can derive range
3604 information from the predicate but need to insert different
3605 ASSERT_EXPRs for each of the sub-graphs rooted at the
3606 conditional block. If the last statement of BB is a conditional
3607 expression of the form 'X op Y', then
3609 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3611 b) If the conditional is the only entry point to the sub-graph
3612 corresponding to the THEN_CLAUSE, recurse into it. On
3613 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3614 an ASSERT_EXPR is added for the corresponding variable.
3616 c) Repeat step (b) on the ELSE_CLAUSE.
3618 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3620 For instance,
3622 if (a == 9)
3623 b = a;
3624 else
3625 b = c + 1;
3627 In this case, an assertion on the THEN clause is useful to
3628 determine that 'a' is always 9 on that edge. However, an assertion
3629 on the ELSE clause would be unnecessary.
3631 4- If BB does not end in a conditional expression, then we recurse
3632 into BB's dominator children.
3634 At the end of the recursive traversal, every SSA name will have a
3635 list of locations where ASSERT_EXPRs should be added. When a new
3636 location for name N is found, it is registered by calling
3637 register_new_assert_for. That function keeps track of all the
3638 registered assertions to prevent adding unnecessary assertions.
3639 For instance, if a pointer P_4 is dereferenced more than once in a
3640 dominator tree, only the location dominating all the dereference of
3641 P_4 will receive an ASSERT_EXPR.
3643 If this function returns true, then it means that there are names
3644 for which we need to generate ASSERT_EXPRs. Those assertions are
3645 inserted by process_assert_insertions.
3647 TODO. Handle SWITCH_EXPR. */
3649 static bool
3650 find_assert_locations (basic_block bb)
3652 block_stmt_iterator si;
3653 tree last, phi;
3654 bool need_assert;
3655 basic_block son;
3657 if (TEST_BIT (blocks_visited, bb->index))
3658 return false;
3660 SET_BIT (blocks_visited, bb->index);
3662 need_assert = false;
3664 /* Traverse all PHI nodes in BB marking used operands. */
3665 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3667 use_operand_p arg_p;
3668 ssa_op_iter i;
3670 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
3672 tree arg = USE_FROM_PTR (arg_p);
3673 if (TREE_CODE (arg) == SSA_NAME)
3675 gcc_assert (is_gimple_reg (PHI_RESULT (phi)));
3676 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (arg));
3681 /* Traverse all the statements in BB marking used names and looking
3682 for statements that may infer assertions for their used operands. */
3683 last = NULL_TREE;
3684 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3686 tree stmt, op;
3687 ssa_op_iter i;
3689 stmt = bsi_stmt (si);
3691 /* See if we can derive an assertion for any of STMT's operands. */
3692 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
3694 tree value;
3695 enum tree_code comp_code;
3697 /* Mark OP in bitmap FOUND_IN_SUBGRAPH. If STMT is inside
3698 the sub-graph of a conditional block, when we return from
3699 this recursive walk, our parent will use the
3700 FOUND_IN_SUBGRAPH bitset to determine if one of the
3701 operands it was looking for was present in the sub-graph. */
3702 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3704 /* If OP is used in such a way that we can infer a value
3705 range for it, and we don't find a previous assertion for
3706 it, create a new assertion location node for OP. */
3707 if (infer_value_range (stmt, op, &comp_code, &value))
3709 /* If we are able to infer a nonzero value range for OP,
3710 then walk backwards through the use-def chain to see if OP
3711 was set via a typecast.
3713 If so, then we can also infer a nonzero value range
3714 for the operand of the NOP_EXPR. */
3715 if (comp_code == NE_EXPR && integer_zerop (value))
3717 tree t = op;
3718 tree def_stmt = SSA_NAME_DEF_STMT (t);
3720 while (TREE_CODE (def_stmt) == GIMPLE_MODIFY_STMT
3721 && TREE_CODE
3722 (GIMPLE_STMT_OPERAND (def_stmt, 1)) == NOP_EXPR
3723 && TREE_CODE
3724 (TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1),
3725 0)) == SSA_NAME
3726 && POINTER_TYPE_P
3727 (TREE_TYPE (TREE_OPERAND
3728 (GIMPLE_STMT_OPERAND (def_stmt,
3729 1), 0))))
3731 t = TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1), 0);
3732 def_stmt = SSA_NAME_DEF_STMT (t);
3734 /* Note we want to register the assert for the
3735 operand of the NOP_EXPR after SI, not after the
3736 conversion. */
3737 if (! has_single_use (t))
3739 register_new_assert_for (t, comp_code, value,
3740 bb, NULL, si);
3741 need_assert = true;
3746 /* If OP is used only once, namely in this STMT, don't
3747 bother creating an ASSERT_EXPR for it. Such an
3748 ASSERT_EXPR would do nothing but increase compile time. */
3749 if (!has_single_use (op))
3751 register_new_assert_for (op, comp_code, value, bb, NULL, si);
3752 need_assert = true;
3757 /* Remember the last statement of the block. */
3758 last = stmt;
3761 /* If BB's last statement is a conditional expression
3762 involving integer operands, recurse into each of the sub-graphs
3763 rooted at BB to determine if we need to add ASSERT_EXPRs. */
3764 if (last
3765 && TREE_CODE (last) == COND_EXPR
3766 && !fp_predicate (COND_EXPR_COND (last))
3767 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
3768 need_assert |= find_conditional_asserts (bb, last);
3770 /* Recurse into the dominator children of BB. */
3771 for (son = first_dom_son (CDI_DOMINATORS, bb);
3772 son;
3773 son = next_dom_son (CDI_DOMINATORS, son))
3774 need_assert |= find_assert_locations (son);
3776 return need_assert;
3780 /* Create an ASSERT_EXPR for NAME and insert it in the location
3781 indicated by LOC. Return true if we made any edge insertions. */
3783 static bool
3784 process_assert_insertions_for (tree name, assert_locus_t loc)
3786 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3787 tree stmt, cond, assert_expr;
3788 edge_iterator ei;
3789 edge e;
3791 cond = build2 (loc->comp_code, boolean_type_node, name, loc->val);
3792 assert_expr = build_assert_expr_for (cond, name);
3794 if (loc->e)
3796 /* We have been asked to insert the assertion on an edge. This
3797 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3798 #if defined ENABLE_CHECKING
3799 gcc_assert (TREE_CODE (bsi_stmt (loc->si)) == COND_EXPR
3800 || TREE_CODE (bsi_stmt (loc->si)) == SWITCH_EXPR);
3801 #endif
3803 bsi_insert_on_edge (loc->e, assert_expr);
3804 return true;
3807 /* Otherwise, we can insert right after LOC->SI iff the
3808 statement must not be the last statement in the block. */
3809 stmt = bsi_stmt (loc->si);
3810 if (!stmt_ends_bb_p (stmt))
3812 bsi_insert_after (&loc->si, assert_expr, BSI_SAME_STMT);
3813 return false;
3816 /* If STMT must be the last statement in BB, we can only insert new
3817 assertions on the non-abnormal edge out of BB. Note that since
3818 STMT is not control flow, there may only be one non-abnormal edge
3819 out of BB. */
3820 FOR_EACH_EDGE (e, ei, loc->bb->succs)
3821 if (!(e->flags & EDGE_ABNORMAL))
3823 bsi_insert_on_edge (e, assert_expr);
3824 return true;
3827 gcc_unreachable ();
3831 /* Process all the insertions registered for every name N_i registered
3832 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3833 found in ASSERTS_FOR[i]. */
3835 static void
3836 process_assert_insertions (void)
3838 unsigned i;
3839 bitmap_iterator bi;
3840 bool update_edges_p = false;
3841 int num_asserts = 0;
3843 if (dump_file && (dump_flags & TDF_DETAILS))
3844 dump_all_asserts (dump_file);
3846 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3848 assert_locus_t loc = asserts_for[i];
3849 gcc_assert (loc);
3851 while (loc)
3853 assert_locus_t next = loc->next;
3854 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
3855 free (loc);
3856 loc = next;
3857 num_asserts++;
3861 if (update_edges_p)
3862 bsi_commit_edge_inserts ();
3864 if (dump_file && (dump_flags & TDF_STATS))
3865 fprintf (dump_file, "\nNumber of ASSERT_EXPR expressions inserted: %d\n\n",
3866 num_asserts);
3870 /* Traverse the flowgraph looking for conditional jumps to insert range
3871 expressions. These range expressions are meant to provide information
3872 to optimizations that need to reason in terms of value ranges. They
3873 will not be expanded into RTL. For instance, given:
3875 x = ...
3876 y = ...
3877 if (x < y)
3878 y = x - 2;
3879 else
3880 x = y + 3;
3882 this pass will transform the code into:
3884 x = ...
3885 y = ...
3886 if (x < y)
3888 x = ASSERT_EXPR <x, x < y>
3889 y = x - 2
3891 else
3893 y = ASSERT_EXPR <y, x <= y>
3894 x = y + 3
3897 The idea is that once copy and constant propagation have run, other
3898 optimizations will be able to determine what ranges of values can 'x'
3899 take in different paths of the code, simply by checking the reaching
3900 definition of 'x'. */
3902 static void
3903 insert_range_assertions (void)
3905 edge e;
3906 edge_iterator ei;
3907 bool update_ssa_p;
3909 found_in_subgraph = sbitmap_alloc (num_ssa_names);
3910 sbitmap_zero (found_in_subgraph);
3912 blocks_visited = sbitmap_alloc (last_basic_block);
3913 sbitmap_zero (blocks_visited);
3915 need_assert_for = BITMAP_ALLOC (NULL);
3916 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
3918 calculate_dominance_info (CDI_DOMINATORS);
3920 update_ssa_p = false;
3921 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3922 if (find_assert_locations (e->dest))
3923 update_ssa_p = true;
3925 if (update_ssa_p)
3927 process_assert_insertions ();
3928 update_ssa (TODO_update_ssa_no_phi);
3931 if (dump_file && (dump_flags & TDF_DETAILS))
3933 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
3934 dump_function_to_file (current_function_decl, dump_file, dump_flags);
3937 sbitmap_free (found_in_subgraph);
3938 free (asserts_for);
3939 BITMAP_FREE (need_assert_for);
3942 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
3943 and "struct" hacks. If VRP can determine that the
3944 array subscript is a constant, check if it is outside valid
3945 range. If the array subscript is a RANGE, warn if it is
3946 non-overlapping with valid range.
3947 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
3949 static void
3950 check_array_ref (tree ref, location_t* locus, bool ignore_off_by_one)
3952 value_range_t* vr = NULL;
3953 tree low_sub, up_sub;
3954 tree low_bound, up_bound = array_ref_up_bound (ref);
3956 low_sub = up_sub = TREE_OPERAND (ref, 1);
3958 if (!up_bound || !locus || TREE_NO_WARNING (ref)
3959 || TREE_CODE (up_bound) != INTEGER_CST
3960 /* Can not check flexible arrays. */
3961 || (TYPE_SIZE (TREE_TYPE (ref)) == NULL_TREE
3962 && TYPE_DOMAIN (TREE_TYPE (ref)) != NULL_TREE
3963 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (ref))) == NULL_TREE)
3964 /* Accesses after the end of arrays of size 0 (gcc
3965 extension) and 1 are likely intentional ("struct
3966 hack"). */
3967 || compare_tree_int (up_bound, 1) <= 0)
3968 return;
3970 low_bound = array_ref_low_bound (ref);
3972 if (TREE_CODE (low_sub) == SSA_NAME)
3974 vr = get_value_range (low_sub);
3975 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
3977 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
3978 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
3982 if (vr && vr->type == VR_ANTI_RANGE)
3984 if (TREE_CODE (up_sub) == INTEGER_CST
3985 && tree_int_cst_lt (up_bound, up_sub)
3986 && TREE_CODE (low_sub) == INTEGER_CST
3987 && tree_int_cst_lt (low_sub, low_bound))
3989 warning (OPT_Warray_bounds,
3990 "%Harray subscript is outside array bounds", locus);
3991 TREE_NO_WARNING (ref) = 1;
3994 else if (TREE_CODE (up_sub) == INTEGER_CST
3995 && tree_int_cst_lt (up_bound, up_sub)
3996 && !tree_int_cst_equal (up_bound, up_sub)
3997 && (!ignore_off_by_one
3998 || !tree_int_cst_equal (int_const_binop (PLUS_EXPR,
3999 up_bound,
4000 integer_one_node,
4002 up_sub)))
4004 warning (OPT_Warray_bounds, "%Harray subscript is above array bounds",
4005 locus);
4006 TREE_NO_WARNING (ref) = 1;
4008 else if (TREE_CODE (low_sub) == INTEGER_CST
4009 && tree_int_cst_lt (low_sub, low_bound))
4011 warning (OPT_Warray_bounds, "%Harray subscript is below array bounds",
4012 locus);
4013 TREE_NO_WARNING (ref) = 1;
4017 /* walk_tree() callback that checks if *TP is
4018 an ARRAY_REF inside an ADDR_EXPR (in which an array
4019 subscript one outside the valid range is allowed). Call
4020 check_array_ref for each ARRAY_REF found. The location is
4021 passed in DATA. */
4023 static tree
4024 check_array_bounds (tree *tp, int *walk_subtree, void *data)
4026 tree t = *tp;
4027 tree stmt = (tree)data;
4028 location_t *location = EXPR_LOCUS (stmt);
4030 *walk_subtree = TRUE;
4032 if (TREE_CODE (t) == ARRAY_REF)
4033 check_array_ref (t, location, false /*ignore_off_by_one*/);
4034 else if (TREE_CODE (t) == ADDR_EXPR)
4036 use_operand_p op;
4037 tree use_stmt;
4038 t = TREE_OPERAND (t, 0);
4040 /* Don't warn on statements like
4042 ssa_name = 500 + &array[-200]
4046 ssa_name = &array[-200]
4047 other_name = ssa_name + 300;
4049 which are sometimes
4050 produced by other optimizing passes. */
4052 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
4053 && BINARY_CLASS_P (GIMPLE_STMT_OPERAND (stmt, 1)))
4054 *walk_subtree = FALSE;
4056 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
4057 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
4058 && single_imm_use (GIMPLE_STMT_OPERAND (stmt, 0), &op, &use_stmt)
4059 && TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
4060 && BINARY_CLASS_P (GIMPLE_STMT_OPERAND (use_stmt, 1)))
4061 *walk_subtree = FALSE;
4063 while (*walk_subtree && handled_component_p (t))
4065 if (TREE_CODE (t) == ARRAY_REF)
4066 check_array_ref (t, location, true /*ignore_off_by_one*/);
4067 t = TREE_OPERAND (t, 0);
4069 *walk_subtree = FALSE;
4072 return NULL_TREE;
4075 /* Walk over all statements of all reachable BBs and call check_array_bounds
4076 on them. */
4078 static void
4079 check_all_array_refs (void)
4081 basic_block bb;
4082 block_stmt_iterator si;
4084 FOR_EACH_BB (bb)
4086 /* Skip bb's that are clearly unreachable. */
4087 if (single_pred_p (bb))
4089 basic_block pred_bb = EDGE_PRED (bb, 0)->src;
4090 tree ls = NULL_TREE;
4092 if (!bsi_end_p (bsi_last (pred_bb)))
4093 ls = bsi_stmt (bsi_last (pred_bb));
4095 if (ls && TREE_CODE (ls) == COND_EXPR
4096 && ((COND_EXPR_COND (ls) == boolean_false_node
4097 && (EDGE_PRED (bb, 0)->flags & EDGE_TRUE_VALUE))
4098 || (COND_EXPR_COND (ls) == boolean_true_node
4099 && (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE))))
4100 continue;
4102 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
4103 walk_tree (bsi_stmt_ptr (si), check_array_bounds,
4104 bsi_stmt (si), NULL);
4108 /* Convert range assertion expressions into the implied copies and
4109 copy propagate away the copies. Doing the trivial copy propagation
4110 here avoids the need to run the full copy propagation pass after
4111 VRP.
4113 FIXME, this will eventually lead to copy propagation removing the
4114 names that had useful range information attached to them. For
4115 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
4116 then N_i will have the range [3, +INF].
4118 However, by converting the assertion into the implied copy
4119 operation N_i = N_j, we will then copy-propagate N_j into the uses
4120 of N_i and lose the range information. We may want to hold on to
4121 ASSERT_EXPRs a little while longer as the ranges could be used in
4122 things like jump threading.
4124 The problem with keeping ASSERT_EXPRs around is that passes after
4125 VRP need to handle them appropriately.
4127 Another approach would be to make the range information a first
4128 class property of the SSA_NAME so that it can be queried from
4129 any pass. This is made somewhat more complex by the need for
4130 multiple ranges to be associated with one SSA_NAME. */
4132 static void
4133 remove_range_assertions (void)
4135 basic_block bb;
4136 block_stmt_iterator si;
4138 /* Note that the BSI iterator bump happens at the bottom of the
4139 loop and no bump is necessary if we're removing the statement
4140 referenced by the current BSI. */
4141 FOR_EACH_BB (bb)
4142 for (si = bsi_start (bb); !bsi_end_p (si);)
4144 tree stmt = bsi_stmt (si);
4145 tree use_stmt;
4147 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
4148 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == ASSERT_EXPR)
4150 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1), var;
4151 tree cond = fold (ASSERT_EXPR_COND (rhs));
4152 use_operand_p use_p;
4153 imm_use_iterator iter;
4155 gcc_assert (cond != boolean_false_node);
4157 /* Propagate the RHS into every use of the LHS. */
4158 var = ASSERT_EXPR_VAR (rhs);
4159 FOR_EACH_IMM_USE_STMT (use_stmt, iter,
4160 GIMPLE_STMT_OPERAND (stmt, 0))
4161 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
4163 SET_USE (use_p, var);
4164 gcc_assert (TREE_CODE (var) == SSA_NAME);
4167 /* And finally, remove the copy, it is not needed. */
4168 bsi_remove (&si, true);
4169 release_defs (stmt);
4171 else
4172 bsi_next (&si);
4175 sbitmap_free (blocks_visited);
4179 /* Return true if STMT is interesting for VRP. */
4181 static bool
4182 stmt_interesting_for_vrp (tree stmt)
4184 if (TREE_CODE (stmt) == PHI_NODE
4185 && is_gimple_reg (PHI_RESULT (stmt))
4186 && (INTEGRAL_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))
4187 || POINTER_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))))
4188 return true;
4189 else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
4191 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
4192 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
4194 /* In general, assignments with virtual operands are not useful
4195 for deriving ranges, with the obvious exception of calls to
4196 builtin functions. */
4197 if (TREE_CODE (lhs) == SSA_NAME
4198 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4199 || POINTER_TYPE_P (TREE_TYPE (lhs)))
4200 && ((TREE_CODE (rhs) == CALL_EXPR
4201 && TREE_CODE (CALL_EXPR_FN (rhs)) == ADDR_EXPR
4202 && DECL_P (TREE_OPERAND (CALL_EXPR_FN (rhs), 0))
4203 && DECL_IS_BUILTIN (TREE_OPERAND (CALL_EXPR_FN (rhs), 0)))
4204 || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS)))
4205 return true;
4207 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
4208 return true;
4210 return false;
4214 /* Initialize local data structures for VRP. */
4216 static void
4217 vrp_initialize (void)
4219 basic_block bb;
4221 vr_value = XCNEWVEC (value_range_t *, num_ssa_names);
4223 FOR_EACH_BB (bb)
4225 block_stmt_iterator si;
4226 tree phi;
4228 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4230 if (!stmt_interesting_for_vrp (phi))
4232 tree lhs = PHI_RESULT (phi);
4233 set_value_range_to_varying (get_value_range (lhs));
4234 DONT_SIMULATE_AGAIN (phi) = true;
4236 else
4237 DONT_SIMULATE_AGAIN (phi) = false;
4240 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
4242 tree stmt = bsi_stmt (si);
4244 if (!stmt_interesting_for_vrp (stmt))
4246 ssa_op_iter i;
4247 tree def;
4248 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
4249 set_value_range_to_varying (get_value_range (def));
4250 DONT_SIMULATE_AGAIN (stmt) = true;
4252 else
4254 DONT_SIMULATE_AGAIN (stmt) = false;
4261 /* Visit assignment STMT. If it produces an interesting range, record
4262 the SSA name in *OUTPUT_P. */
4264 static enum ssa_prop_result
4265 vrp_visit_assignment (tree stmt, tree *output_p)
4267 tree lhs, rhs, def;
4268 ssa_op_iter iter;
4270 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
4271 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
4273 /* We only keep track of ranges in integral and pointer types. */
4274 if (TREE_CODE (lhs) == SSA_NAME
4275 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4276 /* It is valid to have NULL MIN/MAX values on a type. See
4277 build_range_type. */
4278 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
4279 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
4280 || POINTER_TYPE_P (TREE_TYPE (lhs))))
4282 struct loop *l;
4283 value_range_t new_vr = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
4285 extract_range_from_expr (&new_vr, rhs);
4287 /* If STMT is inside a loop, we may be able to know something
4288 else about the range of LHS by examining scalar evolution
4289 information. */
4290 if (current_loops && (l = loop_containing_stmt (stmt)))
4291 adjust_range_with_scev (&new_vr, l, stmt, lhs);
4293 if (update_value_range (lhs, &new_vr))
4295 *output_p = lhs;
4297 if (dump_file && (dump_flags & TDF_DETAILS))
4299 fprintf (dump_file, "Found new range for ");
4300 print_generic_expr (dump_file, lhs, 0);
4301 fprintf (dump_file, ": ");
4302 dump_value_range (dump_file, &new_vr);
4303 fprintf (dump_file, "\n\n");
4306 if (new_vr.type == VR_VARYING)
4307 return SSA_PROP_VARYING;
4309 return SSA_PROP_INTERESTING;
4312 return SSA_PROP_NOT_INTERESTING;
4315 /* Every other statement produces no useful ranges. */
4316 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
4317 set_value_range_to_varying (get_value_range (def));
4319 return SSA_PROP_VARYING;
4323 /* Compare all the value ranges for names equivalent to VAR with VAL
4324 using comparison code COMP. Return the same value returned by
4325 compare_range_with_value, including the setting of
4326 *STRICT_OVERFLOW_P. */
4328 static tree
4329 compare_name_with_value (enum tree_code comp, tree var, tree val,
4330 bool *strict_overflow_p)
4332 bitmap_iterator bi;
4333 unsigned i;
4334 bitmap e;
4335 tree retval, t;
4336 int used_strict_overflow;
4338 t = retval = NULL_TREE;
4340 /* Get the set of equivalences for VAR. */
4341 e = get_value_range (var)->equiv;
4343 /* Add VAR to its own set of equivalences so that VAR's value range
4344 is processed by this loop (otherwise, we would have to replicate
4345 the body of the loop just to check VAR's value range). */
4346 bitmap_set_bit (e, SSA_NAME_VERSION (var));
4348 /* Start at -1. Set it to 0 if we do a comparison without relying
4349 on overflow, or 1 if all comparisons rely on overflow. */
4350 used_strict_overflow = -1;
4352 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
4354 bool sop;
4356 value_range_t equiv_vr = *(vr_value[i]);
4358 /* If name N_i does not have a valid range, use N_i as its own
4359 range. This allows us to compare against names that may
4360 have N_i in their ranges. */
4361 if (equiv_vr.type == VR_VARYING || equiv_vr.type == VR_UNDEFINED)
4363 equiv_vr.type = VR_RANGE;
4364 equiv_vr.min = ssa_name (i);
4365 equiv_vr.max = ssa_name (i);
4368 sop = false;
4369 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
4370 if (t)
4372 /* If we get different answers from different members
4373 of the equivalence set this check must be in a dead
4374 code region. Folding it to a trap representation
4375 would be correct here. For now just return don't-know. */
4376 if (retval != NULL
4377 && t != retval)
4379 retval = NULL_TREE;
4380 break;
4382 retval = t;
4384 if (!sop)
4385 used_strict_overflow = 0;
4386 else if (used_strict_overflow < 0)
4387 used_strict_overflow = 1;
4391 /* Remove VAR from its own equivalence set. */
4392 bitmap_clear_bit (e, SSA_NAME_VERSION (var));
4394 if (retval)
4396 if (used_strict_overflow > 0)
4397 *strict_overflow_p = true;
4398 return retval;
4401 /* We couldn't find a non-NULL value for the predicate. */
4402 return NULL_TREE;
4406 /* Given a comparison code COMP and names N1 and N2, compare all the
4407 ranges equivalent to N1 against all the ranges equivalent to N2
4408 to determine the value of N1 COMP N2. Return the same value
4409 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
4410 whether we relied on an overflow infinity in the comparison. */
4413 static tree
4414 compare_names (enum tree_code comp, tree n1, tree n2,
4415 bool *strict_overflow_p)
4417 tree t, retval;
4418 bitmap e1, e2;
4419 bitmap_iterator bi1, bi2;
4420 unsigned i1, i2;
4421 int used_strict_overflow;
4423 /* Compare the ranges of every name equivalent to N1 against the
4424 ranges of every name equivalent to N2. */
4425 e1 = get_value_range (n1)->equiv;
4426 e2 = get_value_range (n2)->equiv;
4428 /* Add N1 and N2 to their own set of equivalences to avoid
4429 duplicating the body of the loop just to check N1 and N2
4430 ranges. */
4431 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
4432 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
4434 /* If the equivalence sets have a common intersection, then the two
4435 names can be compared without checking their ranges. */
4436 if (bitmap_intersect_p (e1, e2))
4438 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4439 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4441 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
4442 ? boolean_true_node
4443 : boolean_false_node;
4446 /* Start at -1. Set it to 0 if we do a comparison without relying
4447 on overflow, or 1 if all comparisons rely on overflow. */
4448 used_strict_overflow = -1;
4450 /* Otherwise, compare all the equivalent ranges. First, add N1 and
4451 N2 to their own set of equivalences to avoid duplicating the body
4452 of the loop just to check N1 and N2 ranges. */
4453 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
4455 value_range_t vr1 = *(vr_value[i1]);
4457 /* If the range is VARYING or UNDEFINED, use the name itself. */
4458 if (vr1.type == VR_VARYING || vr1.type == VR_UNDEFINED)
4460 vr1.type = VR_RANGE;
4461 vr1.min = ssa_name (i1);
4462 vr1.max = ssa_name (i1);
4465 t = retval = NULL_TREE;
4466 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
4468 bool sop;
4470 value_range_t vr2 = *(vr_value[i2]);
4472 if (vr2.type == VR_VARYING || vr2.type == VR_UNDEFINED)
4474 vr2.type = VR_RANGE;
4475 vr2.min = ssa_name (i2);
4476 vr2.max = ssa_name (i2);
4479 t = compare_ranges (comp, &vr1, &vr2, &sop);
4480 if (t)
4482 /* If we get different answers from different members
4483 of the equivalence set this check must be in a dead
4484 code region. Folding it to a trap representation
4485 would be correct here. For now just return don't-know. */
4486 if (retval != NULL
4487 && t != retval)
4489 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4490 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4491 return NULL_TREE;
4493 retval = t;
4495 if (!sop)
4496 used_strict_overflow = 0;
4497 else if (used_strict_overflow < 0)
4498 used_strict_overflow = 1;
4502 if (retval)
4504 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4505 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4506 if (used_strict_overflow > 0)
4507 *strict_overflow_p = true;
4508 return retval;
4512 /* None of the equivalent ranges are useful in computing this
4513 comparison. */
4514 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4515 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4516 return NULL_TREE;
4520 /* Given a conditional predicate COND, try to determine if COND yields
4521 true or false based on the value ranges of its operands. Return
4522 BOOLEAN_TRUE_NODE if the conditional always evaluates to true,
4523 BOOLEAN_FALSE_NODE if the conditional always evaluates to false, and,
4524 NULL if the conditional cannot be evaluated at compile time.
4526 If USE_EQUIV_P is true, the ranges of all the names equivalent with
4527 the operands in COND are used when trying to compute its value.
4528 This is only used during final substitution. During propagation,
4529 we only check the range of each variable and not its equivalents.
4531 Set *STRICT_OVERFLOW_P to indicate whether we relied on an overflow
4532 infinity to produce the result. */
4534 tree
4535 vrp_evaluate_conditional (tree cond, bool use_equiv_p, bool *strict_overflow_p)
4537 gcc_assert (TREE_CODE (cond) == SSA_NAME
4538 || TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison);
4540 if (TREE_CODE (cond) == SSA_NAME)
4542 value_range_t *vr;
4543 tree retval;
4545 if (use_equiv_p)
4546 retval = compare_name_with_value (NE_EXPR, cond, boolean_false_node,
4547 strict_overflow_p);
4548 else
4550 value_range_t *vr = get_value_range (cond);
4551 retval = compare_range_with_value (NE_EXPR, vr, boolean_false_node,
4552 strict_overflow_p);
4555 /* If COND has a known boolean range, return it. */
4556 if (retval)
4557 return retval;
4559 /* Otherwise, if COND has a symbolic range of exactly one value,
4560 return it. */
4561 vr = get_value_range (cond);
4562 if (vr->type == VR_RANGE && vr->min == vr->max)
4563 return vr->min;
4565 else
4567 tree op0 = TREE_OPERAND (cond, 0);
4568 tree op1 = TREE_OPERAND (cond, 1);
4570 /* We only deal with integral and pointer types. */
4571 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
4572 && !POINTER_TYPE_P (TREE_TYPE (op0)))
4573 return NULL_TREE;
4575 if (use_equiv_p)
4577 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
4578 return compare_names (TREE_CODE (cond), op0, op1,
4579 strict_overflow_p);
4580 else if (TREE_CODE (op0) == SSA_NAME)
4581 return compare_name_with_value (TREE_CODE (cond), op0, op1,
4582 strict_overflow_p);
4583 else if (TREE_CODE (op1) == SSA_NAME)
4584 return (compare_name_with_value
4585 (swap_tree_comparison (TREE_CODE (cond)), op1, op0,
4586 strict_overflow_p));
4588 else
4590 value_range_t *vr0, *vr1;
4592 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
4593 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
4595 if (vr0 && vr1)
4596 return compare_ranges (TREE_CODE (cond), vr0, vr1,
4597 strict_overflow_p);
4598 else if (vr0 && vr1 == NULL)
4599 return compare_range_with_value (TREE_CODE (cond), vr0, op1,
4600 strict_overflow_p);
4601 else if (vr0 == NULL && vr1)
4602 return (compare_range_with_value
4603 (swap_tree_comparison (TREE_CODE (cond)), vr1, op0,
4604 strict_overflow_p));
4608 /* Anything else cannot be computed statically. */
4609 return NULL_TREE;
4613 /* Visit conditional statement STMT. If we can determine which edge
4614 will be taken out of STMT's basic block, record it in
4615 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
4616 SSA_PROP_VARYING. */
4618 static enum ssa_prop_result
4619 vrp_visit_cond_stmt (tree stmt, edge *taken_edge_p)
4621 tree cond, val;
4622 bool sop;
4624 *taken_edge_p = NULL;
4626 /* FIXME. Handle SWITCH_EXPRs. But first, the assert pass needs to
4627 add ASSERT_EXPRs for them. */
4628 if (TREE_CODE (stmt) == SWITCH_EXPR)
4629 return SSA_PROP_VARYING;
4631 cond = COND_EXPR_COND (stmt);
4633 if (dump_file && (dump_flags & TDF_DETAILS))
4635 tree use;
4636 ssa_op_iter i;
4638 fprintf (dump_file, "\nVisiting conditional with predicate: ");
4639 print_generic_expr (dump_file, cond, 0);
4640 fprintf (dump_file, "\nWith known ranges\n");
4642 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
4644 fprintf (dump_file, "\t");
4645 print_generic_expr (dump_file, use, 0);
4646 fprintf (dump_file, ": ");
4647 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
4650 fprintf (dump_file, "\n");
4653 /* Compute the value of the predicate COND by checking the known
4654 ranges of each of its operands.
4656 Note that we cannot evaluate all the equivalent ranges here
4657 because those ranges may not yet be final and with the current
4658 propagation strategy, we cannot determine when the value ranges
4659 of the names in the equivalence set have changed.
4661 For instance, given the following code fragment
4663 i_5 = PHI <8, i_13>
4665 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
4666 if (i_14 == 1)
4669 Assume that on the first visit to i_14, i_5 has the temporary
4670 range [8, 8] because the second argument to the PHI function is
4671 not yet executable. We derive the range ~[0, 0] for i_14 and the
4672 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
4673 the first time, since i_14 is equivalent to the range [8, 8], we
4674 determine that the predicate is always false.
4676 On the next round of propagation, i_13 is determined to be
4677 VARYING, which causes i_5 to drop down to VARYING. So, another
4678 visit to i_14 is scheduled. In this second visit, we compute the
4679 exact same range and equivalence set for i_14, namely ~[0, 0] and
4680 { i_5 }. But we did not have the previous range for i_5
4681 registered, so vrp_visit_assignment thinks that the range for
4682 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
4683 is not visited again, which stops propagation from visiting
4684 statements in the THEN clause of that if().
4686 To properly fix this we would need to keep the previous range
4687 value for the names in the equivalence set. This way we would've
4688 discovered that from one visit to the other i_5 changed from
4689 range [8, 8] to VR_VARYING.
4691 However, fixing this apparent limitation may not be worth the
4692 additional checking. Testing on several code bases (GCC, DLV,
4693 MICO, TRAMP3D and SPEC2000) showed that doing this results in
4694 4 more predicates folded in SPEC. */
4695 sop = false;
4696 val = vrp_evaluate_conditional (cond, false, &sop);
4697 if (val)
4699 if (!sop)
4700 *taken_edge_p = find_taken_edge (bb_for_stmt (stmt), val);
4701 else
4703 if (dump_file && (dump_flags & TDF_DETAILS))
4704 fprintf (dump_file,
4705 "\nIgnoring predicate evaluation because "
4706 "it assumes that signed overflow is undefined");
4707 val = NULL_TREE;
4711 if (dump_file && (dump_flags & TDF_DETAILS))
4713 fprintf (dump_file, "\nPredicate evaluates to: ");
4714 if (val == NULL_TREE)
4715 fprintf (dump_file, "DON'T KNOW\n");
4716 else
4717 print_generic_stmt (dump_file, val, 0);
4720 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
4724 /* Evaluate statement STMT. If the statement produces a useful range,
4725 return SSA_PROP_INTERESTING and record the SSA name with the
4726 interesting range into *OUTPUT_P.
4728 If STMT is a conditional branch and we can determine its truth
4729 value, the taken edge is recorded in *TAKEN_EDGE_P.
4731 If STMT produces a varying value, return SSA_PROP_VARYING. */
4733 static enum ssa_prop_result
4734 vrp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
4736 tree def;
4737 ssa_op_iter iter;
4738 stmt_ann_t ann;
4740 if (dump_file && (dump_flags & TDF_DETAILS))
4742 fprintf (dump_file, "\nVisiting statement:\n");
4743 print_generic_stmt (dump_file, stmt, dump_flags);
4744 fprintf (dump_file, "\n");
4747 ann = stmt_ann (stmt);
4748 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
4750 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
4752 /* In general, assignments with virtual operands are not useful
4753 for deriving ranges, with the obvious exception of calls to
4754 builtin functions. */
4755 if ((TREE_CODE (rhs) == CALL_EXPR
4756 && TREE_CODE (CALL_EXPR_FN (rhs)) == ADDR_EXPR
4757 && DECL_P (TREE_OPERAND (CALL_EXPR_FN (rhs), 0))
4758 && DECL_IS_BUILTIN (TREE_OPERAND (CALL_EXPR_FN (rhs), 0)))
4759 || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
4760 return vrp_visit_assignment (stmt, output_p);
4762 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
4763 return vrp_visit_cond_stmt (stmt, taken_edge_p);
4765 /* All other statements produce nothing of interest for VRP, so mark
4766 their outputs varying and prevent further simulation. */
4767 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
4768 set_value_range_to_varying (get_value_range (def));
4770 return SSA_PROP_VARYING;
4774 /* Meet operation for value ranges. Given two value ranges VR0 and
4775 VR1, store in VR0 a range that contains both VR0 and VR1. This
4776 may not be the smallest possible such range. */
4778 static void
4779 vrp_meet (value_range_t *vr0, value_range_t *vr1)
4781 if (vr0->type == VR_UNDEFINED)
4783 copy_value_range (vr0, vr1);
4784 return;
4787 if (vr1->type == VR_UNDEFINED)
4789 /* Nothing to do. VR0 already has the resulting range. */
4790 return;
4793 if (vr0->type == VR_VARYING)
4795 /* Nothing to do. VR0 already has the resulting range. */
4796 return;
4799 if (vr1->type == VR_VARYING)
4801 set_value_range_to_varying (vr0);
4802 return;
4805 if (vr0->type == VR_RANGE && vr1->type == VR_RANGE)
4807 int cmp;
4808 tree min, max;
4810 /* Compute the convex hull of the ranges. The lower limit of
4811 the new range is the minimum of the two ranges. If they
4812 cannot be compared, then give up. */
4813 cmp = compare_values (vr0->min, vr1->min);
4814 if (cmp == 0 || cmp == 1)
4815 min = vr1->min;
4816 else if (cmp == -1)
4817 min = vr0->min;
4818 else
4819 goto give_up;
4821 /* Similarly, the upper limit of the new range is the maximum
4822 of the two ranges. If they cannot be compared, then
4823 give up. */
4824 cmp = compare_values (vr0->max, vr1->max);
4825 if (cmp == 0 || cmp == -1)
4826 max = vr1->max;
4827 else if (cmp == 1)
4828 max = vr0->max;
4829 else
4830 goto give_up;
4832 /* The resulting set of equivalences is the intersection of
4833 the two sets. */
4834 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4835 bitmap_and_into (vr0->equiv, vr1->equiv);
4836 else if (vr0->equiv && !vr1->equiv)
4837 bitmap_clear (vr0->equiv);
4839 set_value_range (vr0, vr0->type, min, max, vr0->equiv);
4841 else if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4843 /* Two anti-ranges meet only if their complements intersect.
4844 Only handle the case of identical ranges. */
4845 if (compare_values (vr0->min, vr1->min) == 0
4846 && compare_values (vr0->max, vr1->max) == 0
4847 && compare_values (vr0->min, vr0->max) == 0)
4849 /* The resulting set of equivalences is the intersection of
4850 the two sets. */
4851 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4852 bitmap_and_into (vr0->equiv, vr1->equiv);
4853 else if (vr0->equiv && !vr1->equiv)
4854 bitmap_clear (vr0->equiv);
4856 else
4857 goto give_up;
4859 else if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4861 /* For a numeric range [VAL1, VAL2] and an anti-range ~[VAL3, VAL4],
4862 only handle the case where the ranges have an empty intersection.
4863 The result of the meet operation is the anti-range. */
4864 if (!symbolic_range_p (vr0)
4865 && !symbolic_range_p (vr1)
4866 && !value_ranges_intersect_p (vr0, vr1))
4868 /* Copy most of VR1 into VR0. Don't copy VR1's equivalence
4869 set. We need to compute the intersection of the two
4870 equivalence sets. */
4871 if (vr1->type == VR_ANTI_RANGE)
4872 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr0->equiv);
4874 /* The resulting set of equivalences is the intersection of
4875 the two sets. */
4876 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4877 bitmap_and_into (vr0->equiv, vr1->equiv);
4878 else if (vr0->equiv && !vr1->equiv)
4879 bitmap_clear (vr0->equiv);
4881 else
4882 goto give_up;
4884 else
4885 gcc_unreachable ();
4887 return;
4889 give_up:
4890 /* Failed to find an efficient meet. Before giving up and setting
4891 the result to VARYING, see if we can at least derive a useful
4892 anti-range. FIXME, all this nonsense about distinguishing
4893 anti-ranges from ranges is necessary because of the odd
4894 semantics of range_includes_zero_p and friends. */
4895 if (!symbolic_range_p (vr0)
4896 && ((vr0->type == VR_RANGE && !range_includes_zero_p (vr0))
4897 || (vr0->type == VR_ANTI_RANGE && range_includes_zero_p (vr0)))
4898 && !symbolic_range_p (vr1)
4899 && ((vr1->type == VR_RANGE && !range_includes_zero_p (vr1))
4900 || (vr1->type == VR_ANTI_RANGE && range_includes_zero_p (vr1))))
4902 set_value_range_to_nonnull (vr0, TREE_TYPE (vr0->min));
4904 /* Since this meet operation did not result from the meeting of
4905 two equivalent names, VR0 cannot have any equivalences. */
4906 if (vr0->equiv)
4907 bitmap_clear (vr0->equiv);
4909 else
4910 set_value_range_to_varying (vr0);
4914 /* Visit all arguments for PHI node PHI that flow through executable
4915 edges. If a valid value range can be derived from all the incoming
4916 value ranges, set a new range for the LHS of PHI. */
4918 static enum ssa_prop_result
4919 vrp_visit_phi_node (tree phi)
4921 int i;
4922 tree lhs = PHI_RESULT (phi);
4923 value_range_t *lhs_vr = get_value_range (lhs);
4924 value_range_t vr_result = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
4925 bool all_const = true;
4927 copy_value_range (&vr_result, lhs_vr);
4929 if (dump_file && (dump_flags & TDF_DETAILS))
4931 fprintf (dump_file, "\nVisiting PHI node: ");
4932 print_generic_expr (dump_file, phi, dump_flags);
4935 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
4937 edge e = PHI_ARG_EDGE (phi, i);
4939 if (dump_file && (dump_flags & TDF_DETAILS))
4941 fprintf (dump_file,
4942 "\n Argument #%d (%d -> %d %sexecutable)\n",
4943 i, e->src->index, e->dest->index,
4944 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
4947 if (e->flags & EDGE_EXECUTABLE)
4949 tree arg = PHI_ARG_DEF (phi, i);
4950 value_range_t vr_arg;
4952 if (TREE_CODE (arg) == SSA_NAME)
4954 vr_arg = *(get_value_range (arg));
4955 all_const = false;
4957 else
4959 vr_arg.type = VR_RANGE;
4960 vr_arg.min = arg;
4961 vr_arg.max = arg;
4962 vr_arg.equiv = NULL;
4965 if (dump_file && (dump_flags & TDF_DETAILS))
4967 fprintf (dump_file, "\t");
4968 print_generic_expr (dump_file, arg, dump_flags);
4969 fprintf (dump_file, "\n\tValue: ");
4970 dump_value_range (dump_file, &vr_arg);
4971 fprintf (dump_file, "\n");
4974 vrp_meet (&vr_result, &vr_arg);
4976 if (vr_result.type == VR_VARYING)
4977 break;
4981 if (vr_result.type == VR_VARYING)
4982 goto varying;
4984 /* To prevent infinite iterations in the algorithm, derive ranges
4985 when the new value is slightly bigger or smaller than the
4986 previous one. */
4987 if (lhs_vr->type == VR_RANGE && vr_result.type == VR_RANGE
4988 && !all_const)
4990 if (!POINTER_TYPE_P (TREE_TYPE (lhs)))
4992 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
4993 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
4995 /* If the new minimum is smaller or larger than the previous
4996 one, go all the way to -INF. In the first case, to avoid
4997 iterating millions of times to reach -INF, and in the
4998 other case to avoid infinite bouncing between different
4999 minimums. */
5000 if (cmp_min > 0 || cmp_min < 0)
5002 /* If we will end up with a (-INF, +INF) range, set it
5003 to VARYING. */
5004 if (is_positive_overflow_infinity (vr_result.max)
5005 || (vr_result.max
5006 == TYPE_MAX_VALUE (TREE_TYPE (vr_result.max))))
5007 goto varying;
5009 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min)))
5010 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
5011 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
5012 vr_result.min =
5013 negative_overflow_infinity (TREE_TYPE (vr_result.min));
5014 else
5015 goto varying;
5018 /* Similarly, if the new maximum is smaller or larger than
5019 the previous one, go all the way to +INF. */
5020 if (cmp_max < 0 || cmp_max > 0)
5022 /* If we will end up with a (-INF, +INF) range, set it
5023 to VARYING. */
5024 if (is_negative_overflow_infinity (vr_result.min)
5025 || (vr_result.min
5026 == TYPE_MIN_VALUE (TREE_TYPE (vr_result.min))))
5027 goto varying;
5029 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max)))
5030 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
5031 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
5032 vr_result.max =
5033 positive_overflow_infinity (TREE_TYPE (vr_result.max));
5034 else
5035 goto varying;
5040 /* If the new range is different than the previous value, keep
5041 iterating. */
5042 if (update_value_range (lhs, &vr_result))
5043 return SSA_PROP_INTERESTING;
5045 /* Nothing changed, don't add outgoing edges. */
5046 return SSA_PROP_NOT_INTERESTING;
5048 /* No match found. Set the LHS to VARYING. */
5049 varying:
5050 set_value_range_to_varying (lhs_vr);
5051 return SSA_PROP_VARYING;
5054 /* Simplify a division or modulo operator to a right shift or
5055 bitwise and if the first operand is unsigned or is greater
5056 than zero and the second operand is an exact power of two. */
5058 static void
5059 simplify_div_or_mod_using_ranges (tree stmt, tree rhs, enum tree_code rhs_code)
5061 tree val = NULL;
5062 tree op = TREE_OPERAND (rhs, 0);
5063 value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
5065 if (TYPE_UNSIGNED (TREE_TYPE (op)))
5067 val = integer_one_node;
5069 else
5071 bool sop = false;
5073 val = compare_range_with_value (GT_EXPR, vr, integer_zero_node, &sop);
5076 if (val && integer_onep (val))
5078 tree t;
5079 tree op0 = TREE_OPERAND (rhs, 0);
5080 tree op1 = TREE_OPERAND (rhs, 1);
5082 if (rhs_code == TRUNC_DIV_EXPR)
5084 t = build_int_cst (NULL_TREE, tree_log2 (op1));
5085 t = build2 (RSHIFT_EXPR, TREE_TYPE (op0), op0, t);
5087 else
5089 t = build_int_cst (TREE_TYPE (op1), 1);
5090 t = int_const_binop (MINUS_EXPR, op1, t, 0);
5091 t = fold_convert (TREE_TYPE (op0), t);
5092 t = build2 (BIT_AND_EXPR, TREE_TYPE (op0), op0, t);
5095 GIMPLE_STMT_OPERAND (stmt, 1) = t;
5096 update_stmt (stmt);
5100 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
5101 ABS_EXPR. If the operand is <= 0, then simplify the
5102 ABS_EXPR into a NEGATE_EXPR. */
5104 static void
5105 simplify_abs_using_ranges (tree stmt, tree rhs)
5107 tree val = NULL;
5108 tree op = TREE_OPERAND (rhs, 0);
5109 tree type = TREE_TYPE (op);
5110 value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
5112 if (TYPE_UNSIGNED (type))
5114 val = integer_zero_node;
5116 else if (vr)
5118 bool sop = false;
5120 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
5121 if (!val)
5123 sop = false;
5124 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
5125 &sop);
5127 if (val)
5129 if (integer_zerop (val))
5130 val = integer_one_node;
5131 else if (integer_onep (val))
5132 val = integer_zero_node;
5136 if (val
5137 && (integer_onep (val) || integer_zerop (val)))
5139 tree t;
5141 if (integer_onep (val))
5142 t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
5143 else
5144 t = op;
5146 GIMPLE_STMT_OPERAND (stmt, 1) = t;
5147 update_stmt (stmt);
5152 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
5153 a known value range VR.
5155 If there is one and only one value which will satisfy the
5156 conditional, then return that value. Else return NULL. */
5158 static tree
5159 test_for_singularity (enum tree_code cond_code, tree op0,
5160 tree op1, value_range_t *vr)
5162 tree min = NULL;
5163 tree max = NULL;
5165 /* Extract minimum/maximum values which satisfy the
5166 the conditional as it was written. */
5167 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
5169 /* This should not be negative infinity; there is no overflow
5170 here. */
5171 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
5173 max = op1;
5174 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
5176 tree one = build_int_cst (TREE_TYPE (op0), 1);
5177 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
5180 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
5182 /* This should not be positive infinity; there is no overflow
5183 here. */
5184 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
5186 min = op1;
5187 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
5189 tree one = build_int_cst (TREE_TYPE (op0), 1);
5190 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
5194 /* Now refine the minimum and maximum values using any
5195 value range information we have for op0. */
5196 if (min && max)
5198 if (compare_values (vr->min, min) == -1)
5199 min = min;
5200 else
5201 min = vr->min;
5202 if (compare_values (vr->max, max) == 1)
5203 max = max;
5204 else
5205 max = vr->max;
5207 /* If the new min/max values have converged to a single value,
5208 then there is only one value which can satisfy the condition,
5209 return that value. */
5210 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
5211 return min;
5213 return NULL;
5216 /* Simplify a conditional using a relational operator to an equality
5217 test if the range information indicates only one value can satisfy
5218 the original conditional. */
5220 static void
5221 simplify_cond_using_ranges (tree stmt)
5223 tree cond = COND_EXPR_COND (stmt);
5224 tree op0 = TREE_OPERAND (cond, 0);
5225 tree op1 = TREE_OPERAND (cond, 1);
5226 enum tree_code cond_code = TREE_CODE (cond);
5228 if (cond_code != NE_EXPR
5229 && cond_code != EQ_EXPR
5230 && TREE_CODE (op0) == SSA_NAME
5231 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
5232 && is_gimple_min_invariant (op1))
5234 value_range_t *vr = get_value_range (op0);
5236 /* If we have range information for OP0, then we might be
5237 able to simplify this conditional. */
5238 if (vr->type == VR_RANGE)
5240 tree new = test_for_singularity (cond_code, op0, op1, vr);
5242 if (new)
5244 if (dump_file)
5246 fprintf (dump_file, "Simplified relational ");
5247 print_generic_expr (dump_file, cond, 0);
5248 fprintf (dump_file, " into ");
5251 COND_EXPR_COND (stmt)
5252 = build2 (EQ_EXPR, boolean_type_node, op0, new);
5253 update_stmt (stmt);
5255 if (dump_file)
5257 print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
5258 fprintf (dump_file, "\n");
5260 return;
5264 /* Try again after inverting the condition. We only deal
5265 with integral types here, so no need to worry about
5266 issues with inverting FP comparisons. */
5267 cond_code = invert_tree_comparison (cond_code, false);
5268 new = test_for_singularity (cond_code, op0, op1, vr);
5270 if (new)
5272 if (dump_file)
5274 fprintf (dump_file, "Simplified relational ");
5275 print_generic_expr (dump_file, cond, 0);
5276 fprintf (dump_file, " into ");
5279 COND_EXPR_COND (stmt)
5280 = build2 (NE_EXPR, boolean_type_node, op0, new);
5281 update_stmt (stmt);
5283 if (dump_file)
5285 print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
5286 fprintf (dump_file, "\n");
5288 return;
5295 /* Simplify STMT using ranges if possible. */
5297 void
5298 simplify_stmt_using_ranges (tree stmt)
5300 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
5302 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
5303 enum tree_code rhs_code = TREE_CODE (rhs);
5305 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
5306 and BIT_AND_EXPR respectively if the first operand is greater
5307 than zero and the second operand is an exact power of two. */
5308 if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
5309 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
5310 && integer_pow2p (TREE_OPERAND (rhs, 1)))
5311 simplify_div_or_mod_using_ranges (stmt, rhs, rhs_code);
5313 /* Transform ABS (X) into X or -X as appropriate. */
5314 if (rhs_code == ABS_EXPR
5315 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
5316 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
5317 simplify_abs_using_ranges (stmt, rhs);
5319 else if (TREE_CODE (stmt) == COND_EXPR
5320 && COMPARISON_CLASS_P (COND_EXPR_COND (stmt)))
5322 simplify_cond_using_ranges (stmt);
5326 /* Stack of dest,src equivalency pairs that need to be restored after
5327 each attempt to thread a block's incoming edge to an outgoing edge.
5329 A NULL entry is used to mark the end of pairs which need to be
5330 restored. */
5331 static VEC(tree,heap) *stack;
5333 /* A trivial wrapper so that we can present the generic jump
5334 threading code with a simple API for simplifying statements. */
5335 static tree
5336 simplify_stmt_for_jump_threading (tree stmt)
5338 bool sop;
5340 /* We only use VRP information to simplify conditionals. This is
5341 overly conservative, but it's unclear if doing more would be
5342 worth the compile time cost. */
5343 if (TREE_CODE (stmt) != COND_EXPR)
5344 return NULL;
5346 sop = false;
5347 return vrp_evaluate_conditional (COND_EXPR_COND (stmt), true, &sop);
5350 /* Blocks which have more than one predecessor and more than
5351 one successor present jump threading opportunities. ie,
5352 when the block is reached from a specific predecessor, we
5353 may be able to determine which of the outgoing edges will
5354 be traversed. When this optimization applies, we are able
5355 to avoid conditionals at runtime and we may expose secondary
5356 optimization opportunities.
5358 This routine is effectively a driver for the generic jump
5359 threading code. It basically just presents the generic code
5360 with edges that may be suitable for jump threading.
5362 Unlike DOM, we do not iterate VRP if jump threading was successful.
5363 While iterating may expose new opportunities for VRP, it is expected
5364 those opportunities would be very limited and the compile time cost
5365 to expose those opportunities would be significant.
5367 As jump threading opportunities are discovered, they are registered
5368 for later realization. */
5370 static void
5371 identify_jump_threads (void)
5373 basic_block bb;
5374 tree dummy;
5376 /* Ugh. When substituting values earlier in this pass we can
5377 wipe the dominance information. So rebuild the dominator
5378 information as we need it within the jump threading code. */
5379 calculate_dominance_info (CDI_DOMINATORS);
5381 /* We do not allow VRP information to be used for jump threading
5382 across a back edge in the CFG. Otherwise it becomes too
5383 difficult to avoid eliminating loop exit tests. Of course
5384 EDGE_DFS_BACK is not accurate at this time so we have to
5385 recompute it. */
5386 mark_dfs_back_edges ();
5388 /* Allocate our unwinder stack to unwind any temporary equivalences
5389 that might be recorded. */
5390 stack = VEC_alloc (tree, heap, 20);
5392 /* To avoid lots of silly node creation, we create a single
5393 conditional and just modify it in-place when attempting to
5394 thread jumps. */
5395 dummy = build2 (EQ_EXPR, boolean_type_node, NULL, NULL);
5396 dummy = build3 (COND_EXPR, void_type_node, dummy, NULL, NULL);
5398 /* Walk through all the blocks finding those which present a
5399 potential jump threading opportunity. We could set this up
5400 as a dominator walker and record data during the walk, but
5401 I doubt it's worth the effort for the classes of jump
5402 threading opportunities we are trying to identify at this
5403 point in compilation. */
5404 FOR_EACH_BB (bb)
5406 tree last, cond;
5408 /* If the generic jump threading code does not find this block
5409 interesting, then there is nothing to do. */
5410 if (! potentially_threadable_block (bb))
5411 continue;
5413 /* We only care about blocks ending in a COND_EXPR. While there
5414 may be some value in handling SWITCH_EXPR here, I doubt it's
5415 terribly important. */
5416 last = bsi_stmt (bsi_last (bb));
5417 if (TREE_CODE (last) != COND_EXPR)
5418 continue;
5420 /* We're basically looking for any kind of conditional with
5421 integral type arguments. */
5422 cond = COND_EXPR_COND (last);
5423 if ((TREE_CODE (cond) == SSA_NAME
5424 && INTEGRAL_TYPE_P (TREE_TYPE (cond)))
5425 || (COMPARISON_CLASS_P (cond)
5426 && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
5427 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 0)))
5428 && (TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME
5429 || is_gimple_min_invariant (TREE_OPERAND (cond, 1)))
5430 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 1)))))
5432 edge_iterator ei;
5433 edge e;
5435 /* We've got a block with multiple predecessors and multiple
5436 successors which also ends in a suitable conditional. For
5437 each predecessor, see if we can thread it to a specific
5438 successor. */
5439 FOR_EACH_EDGE (e, ei, bb->preds)
5441 /* Do not thread across back edges or abnormal edges
5442 in the CFG. */
5443 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
5444 continue;
5446 thread_across_edge (dummy, e, true,
5447 &stack,
5448 simplify_stmt_for_jump_threading);
5453 /* We do not actually update the CFG or SSA graphs at this point as
5454 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
5455 handle ASSERT_EXPRs gracefully. */
5458 /* We identified all the jump threading opportunities earlier, but could
5459 not transform the CFG at that time. This routine transforms the
5460 CFG and arranges for the dominator tree to be rebuilt if necessary.
5462 Note the SSA graph update will occur during the normal TODO
5463 processing by the pass manager. */
5464 static void
5465 finalize_jump_threads (void)
5467 bool cfg_altered = false;
5468 cfg_altered = thread_through_all_blocks ();
5470 /* If we threaded jumps, then we need to recompute the dominance
5471 information. */
5472 if (cfg_altered)
5473 free_dominance_info (CDI_DOMINATORS);
5474 VEC_free (tree, heap, stack);
5478 /* Traverse all the blocks folding conditionals with known ranges. */
5480 static void
5481 vrp_finalize (void)
5483 size_t i;
5484 prop_value_t *single_val_range;
5485 bool do_value_subst_p;
5487 if (dump_file)
5489 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
5490 dump_all_value_ranges (dump_file);
5491 fprintf (dump_file, "\n");
5494 /* We may have ended with ranges that have exactly one value. Those
5495 values can be substituted as any other copy/const propagated
5496 value using substitute_and_fold. */
5497 single_val_range = XCNEWVEC (prop_value_t, num_ssa_names);
5499 do_value_subst_p = false;
5500 for (i = 0; i < num_ssa_names; i++)
5501 if (vr_value[i]
5502 && vr_value[i]->type == VR_RANGE
5503 && vr_value[i]->min == vr_value[i]->max)
5505 single_val_range[i].value = vr_value[i]->min;
5506 do_value_subst_p = true;
5509 if (!do_value_subst_p)
5511 /* We found no single-valued ranges, don't waste time trying to
5512 do single value substitution in substitute_and_fold. */
5513 free (single_val_range);
5514 single_val_range = NULL;
5517 substitute_and_fold (single_val_range, true);
5519 if (warn_array_bounds)
5520 check_all_array_refs ();
5522 /* We must identify jump threading opportunities before we release
5523 the datastructures built by VRP. */
5524 identify_jump_threads ();
5526 /* Free allocated memory. */
5527 for (i = 0; i < num_ssa_names; i++)
5528 if (vr_value[i])
5530 BITMAP_FREE (vr_value[i]->equiv);
5531 free (vr_value[i]);
5534 free (single_val_range);
5535 free (vr_value);
5537 /* So that we can distinguish between VRP data being available
5538 and not available. */
5539 vr_value = NULL;
5543 /* Main entry point to VRP (Value Range Propagation). This pass is
5544 loosely based on J. R. C. Patterson, ``Accurate Static Branch
5545 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
5546 Programming Language Design and Implementation, pp. 67-78, 1995.
5547 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
5549 This is essentially an SSA-CCP pass modified to deal with ranges
5550 instead of constants.
5552 While propagating ranges, we may find that two or more SSA name
5553 have equivalent, though distinct ranges. For instance,
5555 1 x_9 = p_3->a;
5556 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
5557 3 if (p_4 == q_2)
5558 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
5559 5 endif
5560 6 if (q_2)
5562 In the code above, pointer p_5 has range [q_2, q_2], but from the
5563 code we can also determine that p_5 cannot be NULL and, if q_2 had
5564 a non-varying range, p_5's range should also be compatible with it.
5566 These equivalences are created by two expressions: ASSERT_EXPR and
5567 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
5568 result of another assertion, then we can use the fact that p_5 and
5569 p_4 are equivalent when evaluating p_5's range.
5571 Together with value ranges, we also propagate these equivalences
5572 between names so that we can take advantage of information from
5573 multiple ranges when doing final replacement. Note that this
5574 equivalency relation is transitive but not symmetric.
5576 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
5577 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
5578 in contexts where that assertion does not hold (e.g., in line 6).
5580 TODO, the main difference between this pass and Patterson's is that
5581 we do not propagate edge probabilities. We only compute whether
5582 edges can be taken or not. That is, instead of having a spectrum
5583 of jump probabilities between 0 and 1, we only deal with 0, 1 and
5584 DON'T KNOW. In the future, it may be worthwhile to propagate
5585 probabilities to aid branch prediction. */
5587 static unsigned int
5588 execute_vrp (void)
5590 insert_range_assertions ();
5592 loop_optimizer_init (LOOPS_NORMAL);
5593 if (current_loops)
5594 scev_initialize ();
5596 vrp_initialize ();
5597 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
5598 vrp_finalize ();
5600 if (current_loops)
5602 scev_finalize ();
5603 loop_optimizer_finalize ();
5606 /* ASSERT_EXPRs must be removed before finalizing jump threads
5607 as finalizing jump threads calls the CFG cleanup code which
5608 does not properly handle ASSERT_EXPRs. */
5609 remove_range_assertions ();
5611 /* If we exposed any new variables, go ahead and put them into
5612 SSA form now, before we handle jump threading. This simplifies
5613 interactions between rewriting of _DECL nodes into SSA form
5614 and rewriting SSA_NAME nodes into SSA form after block
5615 duplication and CFG manipulation. */
5616 update_ssa (TODO_update_ssa);
5618 finalize_jump_threads ();
5619 return 0;
5622 static bool
5623 gate_vrp (void)
5625 return flag_tree_vrp != 0;
5628 struct tree_opt_pass pass_vrp =
5630 "vrp", /* name */
5631 gate_vrp, /* gate */
5632 execute_vrp, /* execute */
5633 NULL, /* sub */
5634 NULL, /* next */
5635 0, /* static_pass_number */
5636 TV_TREE_VRP, /* tv_id */
5637 PROP_ssa | PROP_alias, /* properties_required */
5638 0, /* properties_provided */
5639 0, /* properties_destroyed */
5640 0, /* todo_flags_start */
5641 TODO_cleanup_cfg
5642 | TODO_ggc_collect
5643 | TODO_verify_ssa
5644 | TODO_dump_func
5645 | TODO_update_ssa
5646 | TODO_update_smt_usage, /* todo_flags_finish */
5647 0 /* letter */