* tree-vrp.c (value_inside_range, range_includes_zero_p): Add
[official-gcc.git] / gcc / tree-vrp.c
blobde7a931cb1562b71372b3203e4fed6539a195998
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005 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 "cfgloop.h"
36 #include "tree-scalar-evolution.h"
37 #include "tree-ssa-propagate.h"
38 #include "tree-chrec.h"
40 /* Set of SSA names found during the dominator traversal of a
41 sub-graph in find_assert_locations. */
42 static sbitmap found_in_subgraph;
44 /* Loop structure of the program. Used to analyze scalar evolutions
45 inside adjust_range_with_scev. */
46 static struct loops *cfg_loops;
48 /* Local functions. */
49 static int compare_values (tree val1, tree val2);
51 /* Location information for ASSERT_EXPRs. Each instance of this
52 structure describes an ASSERT_EXPR for an SSA name. Since a single
53 SSA name may have more than one assertion associated with it, these
54 locations are kept in a linked list attached to the corresponding
55 SSA name. */
56 struct assert_locus_d
58 /* Basic block where the assertion would be inserted. */
59 basic_block bb;
61 /* Some assertions need to be inserted on an edge (e.g., assertions
62 generated by COND_EXPRs). In those cases, BB will be NULL. */
63 edge e;
65 /* Pointer to the statement that generated this assertion. */
66 block_stmt_iterator si;
68 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
69 enum tree_code comp_code;
71 /* Value being compared against. */
72 tree val;
74 /* Next node in the linked list. */
75 struct assert_locus_d *next;
78 typedef struct assert_locus_d *assert_locus_t;
80 /* If bit I is present, it means that SSA name N_i has a list of
81 assertions that should be inserted in the IL. */
82 static bitmap need_assert_for;
84 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
85 holds a list of ASSERT_LOCUS_T nodes that describe where
86 ASSERT_EXPRs for SSA name N_I should be inserted. */
87 static assert_locus_t *asserts_for;
89 /* Set of blocks visited in find_assert_locations. Used to avoid
90 visiting the same block more than once. */
91 static sbitmap blocks_visited;
93 /* Value range array. After propagation, VR_VALUE[I] holds the range
94 of values that SSA name N_I may take. */
95 static value_range_t **vr_value;
98 /* Return true if ARG is marked with the nonnull attribute in the
99 current function signature. */
101 static bool
102 nonnull_arg_p (tree arg)
104 tree t, attrs, fntype;
105 unsigned HOST_WIDE_INT arg_num;
107 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
109 fntype = TREE_TYPE (current_function_decl);
110 attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype));
112 /* If "nonnull" wasn't specified, we know nothing about the argument. */
113 if (attrs == NULL_TREE)
114 return false;
116 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
117 if (TREE_VALUE (attrs) == NULL_TREE)
118 return true;
120 /* Get the position number for ARG in the function signature. */
121 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
123 t = TREE_CHAIN (t), arg_num++)
125 if (t == arg)
126 break;
129 gcc_assert (t == arg);
131 /* Now see if ARG_NUM is mentioned in the nonnull list. */
132 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
134 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
135 return true;
138 return false;
142 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
144 static void
145 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
146 tree max, bitmap equiv)
148 #if defined ENABLE_CHECKING
149 /* Check the validity of the range. */
150 if (t == VR_RANGE || t == VR_ANTI_RANGE)
152 int cmp;
154 gcc_assert (min && max);
156 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
157 gcc_assert (min != TYPE_MIN_VALUE (TREE_TYPE (min))
158 || max != TYPE_MAX_VALUE (TREE_TYPE (max)));
160 cmp = compare_values (min, max);
161 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
164 if (t == VR_UNDEFINED || t == VR_VARYING)
165 gcc_assert (min == NULL_TREE && max == NULL_TREE);
167 if (t == VR_UNDEFINED || t == VR_VARYING)
168 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
169 #endif
171 vr->type = t;
172 vr->min = min;
173 vr->max = max;
175 /* Since updating the equivalence set involves deep copying the
176 bitmaps, only do it if absolutely necessary. */
177 if (vr->equiv == NULL)
178 vr->equiv = BITMAP_ALLOC (NULL);
180 if (equiv != vr->equiv)
182 if (equiv && !bitmap_empty_p (equiv))
183 bitmap_copy (vr->equiv, equiv);
184 else
185 bitmap_clear (vr->equiv);
190 /* Copy value range FROM into value range TO. */
192 static inline void
193 copy_value_range (value_range_t *to, value_range_t *from)
195 set_value_range (to, from->type, from->min, from->max, from->equiv);
199 /* Set value range VR to a non-NULL range of type TYPE. */
201 static inline void
202 set_value_range_to_nonnull (value_range_t *vr, tree type)
204 tree zero = build_int_cst (type, 0);
205 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
209 /* Set value range VR to a NULL range of type TYPE. */
211 static inline void
212 set_value_range_to_null (value_range_t *vr, tree type)
214 tree zero = build_int_cst (type, 0);
215 set_value_range (vr, VR_RANGE, zero, zero, vr->equiv);
219 /* Set value range VR to VR_VARYING. */
221 static inline void
222 set_value_range_to_varying (value_range_t *vr)
224 vr->type = VR_VARYING;
225 vr->min = vr->max = NULL_TREE;
226 if (vr->equiv)
227 bitmap_clear (vr->equiv);
231 /* Set value range VR to VR_UNDEFINED. */
233 static inline void
234 set_value_range_to_undefined (value_range_t *vr)
236 vr->type = VR_UNDEFINED;
237 vr->min = vr->max = NULL_TREE;
238 if (vr->equiv)
239 bitmap_clear (vr->equiv);
243 /* Return value range information for VAR. Create an empty range
244 if none existed. */
246 static value_range_t *
247 get_value_range (tree var)
249 value_range_t *vr;
250 tree sym;
251 unsigned ver = SSA_NAME_VERSION (var);
253 vr = vr_value[ver];
254 if (vr)
255 return vr;
257 /* Create a default value range. */
258 vr_value[ver] = vr = xmalloc (sizeof (*vr));
259 memset (vr, 0, sizeof (*vr));
261 /* Allocate an equivalence set. */
262 vr->equiv = BITMAP_ALLOC (NULL);
264 /* If VAR is a default definition, the variable can take any value
265 in VAR's type. */
266 sym = SSA_NAME_VAR (var);
267 if (var == default_def (sym))
269 /* Try to use the "nonnull" attribute to create ~[0, 0]
270 anti-ranges for pointers. Note that this is only valid with
271 default definitions of PARM_DECLs. */
272 if (TREE_CODE (sym) == PARM_DECL
273 && POINTER_TYPE_P (TREE_TYPE (sym))
274 && nonnull_arg_p (sym))
275 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
276 else
277 set_value_range_to_varying (vr);
280 return vr;
284 /* Update the value range and equivalence set for variable VAR to
285 NEW_VR. Return true if NEW_VR is different from VAR's previous
286 value.
288 NOTE: This function assumes that NEW_VR is a temporary value range
289 object created for the sole purpose of updating VAR's range. The
290 storage used by the equivalence set from NEW_VR will be freed by
291 this function. Do not call update_value_range when NEW_VR
292 is the range object associated with another SSA name. */
294 static inline bool
295 update_value_range (tree var, value_range_t *new_vr)
297 value_range_t *old_vr;
298 bool is_new;
300 /* Update the value range, if necessary. */
301 old_vr = get_value_range (var);
302 is_new = old_vr->type != new_vr->type
303 || old_vr->min != new_vr->min
304 || old_vr->max != new_vr->max
305 || (old_vr->equiv == NULL && new_vr->equiv)
306 || (old_vr->equiv && new_vr->equiv == NULL)
307 || (!bitmap_equal_p (old_vr->equiv, new_vr->equiv));
309 if (is_new)
310 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
311 new_vr->equiv);
313 BITMAP_FREE (new_vr->equiv);
314 new_vr->equiv = NULL;
316 return is_new;
320 /* Add VAR and VAR's equivalence set to EQUIV. */
322 static void
323 add_equivalence (bitmap equiv, tree var)
325 unsigned ver = SSA_NAME_VERSION (var);
326 value_range_t *vr = vr_value[ver];
328 bitmap_set_bit (equiv, ver);
329 if (vr && vr->equiv)
330 bitmap_ior_into (equiv, vr->equiv);
334 /* Return true if VR is ~[0, 0]. */
336 static inline bool
337 range_is_nonnull (value_range_t *vr)
339 return vr->type == VR_ANTI_RANGE
340 && integer_zerop (vr->min)
341 && integer_zerop (vr->max);
345 /* Return true if VR is [0, 0]. */
347 static inline bool
348 range_is_null (value_range_t *vr)
350 return vr->type == VR_RANGE
351 && integer_zerop (vr->min)
352 && integer_zerop (vr->max);
356 /* Return true if value range VR involves at least one symbol. */
358 static inline bool
359 symbolic_range_p (value_range_t *vr)
361 return (!is_gimple_min_invariant (vr->min)
362 || !is_gimple_min_invariant (vr->max));
366 /* Like tree_expr_nonzero_p, but this function uses value ranges
367 obtained so far. */
369 static bool
370 vrp_expr_computes_nonzero (tree expr)
372 if (tree_expr_nonzero_p (expr))
373 return true;
375 /* If we have an expression of the form &X->a, then the expression
376 is nonnull if X is nonnull. */
377 if (TREE_CODE (expr) == ADDR_EXPR)
379 tree base = get_base_address (TREE_OPERAND (expr, 0));
381 if (base != NULL_TREE
382 && TREE_CODE (base) == INDIRECT_REF
383 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
385 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
386 if (range_is_nonnull (vr))
387 return true;
391 return false;
395 /* Compare two values VAL1 and VAL2. Return
397 -2 if VAL1 and VAL2 cannot be compared at compile-time,
398 -1 if VAL1 < VAL2,
399 0 if VAL1 == VAL2,
400 +1 if VAL1 > VAL2, and
401 +2 if VAL1 != VAL2
403 This is similar to tree_int_cst_compare but supports pointer values
404 and values that cannot be compared at compile time. */
406 static int
407 compare_values (tree val1, tree val2)
409 if (val1 == val2)
410 return 0;
412 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
413 both integers. */
414 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
415 == POINTER_TYPE_P (TREE_TYPE (val2)));
417 /* Do some limited symbolic comparisons. */
418 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
420 /* We can determine some comparisons against +INF and -INF even
421 if the other value is an expression. */
422 if (val1 == TYPE_MAX_VALUE (TREE_TYPE (val1))
423 && TREE_CODE (val2) == MINUS_EXPR)
425 /* +INF > NAME - CST. */
426 return 1;
428 else if (val1 == TYPE_MIN_VALUE (TREE_TYPE (val1))
429 && TREE_CODE (val2) == PLUS_EXPR)
431 /* -INF < NAME + CST. */
432 return -1;
434 else if (TREE_CODE (val1) == MINUS_EXPR
435 && val2 == TYPE_MAX_VALUE (TREE_TYPE (val2)))
437 /* NAME - CST < +INF. */
438 return -1;
440 else if (TREE_CODE (val1) == PLUS_EXPR
441 && val2 == TYPE_MIN_VALUE (TREE_TYPE (val2)))
443 /* NAME + CST > -INF. */
444 return 1;
448 if ((TREE_CODE (val1) == SSA_NAME
449 || TREE_CODE (val1) == PLUS_EXPR
450 || TREE_CODE (val1) == MINUS_EXPR)
451 && (TREE_CODE (val2) == SSA_NAME
452 || TREE_CODE (val2) == PLUS_EXPR
453 || TREE_CODE (val2) == MINUS_EXPR))
455 tree n1, c1, n2, c2;
457 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
458 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
459 same name, return -2. */
460 if (TREE_CODE (val1) == SSA_NAME)
462 n1 = val1;
463 c1 = NULL_TREE;
465 else
467 n1 = TREE_OPERAND (val1, 0);
468 c1 = TREE_OPERAND (val1, 1);
471 if (TREE_CODE (val2) == SSA_NAME)
473 n2 = val2;
474 c2 = NULL_TREE;
476 else
478 n2 = TREE_OPERAND (val2, 0);
479 c2 = TREE_OPERAND (val2, 1);
482 /* Both values must use the same name. */
483 if (n1 != n2)
484 return -2;
486 if (TREE_CODE (val1) == SSA_NAME)
488 if (TREE_CODE (val2) == SSA_NAME)
489 /* NAME == NAME */
490 return 0;
491 else if (TREE_CODE (val2) == PLUS_EXPR)
492 /* NAME < NAME + CST */
493 return -1;
494 else if (TREE_CODE (val2) == MINUS_EXPR)
495 /* NAME > NAME - CST */
496 return 1;
498 else if (TREE_CODE (val1) == PLUS_EXPR)
500 if (TREE_CODE (val2) == SSA_NAME)
501 /* NAME + CST > NAME */
502 return 1;
503 else if (TREE_CODE (val2) == PLUS_EXPR)
504 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
505 return compare_values (c1, c2);
506 else if (TREE_CODE (val2) == MINUS_EXPR)
507 /* NAME + CST1 > NAME - CST2 */
508 return 1;
510 else if (TREE_CODE (val1) == MINUS_EXPR)
512 if (TREE_CODE (val2) == SSA_NAME)
513 /* NAME - CST < NAME */
514 return -1;
515 else if (TREE_CODE (val2) == PLUS_EXPR)
516 /* NAME - CST1 < NAME + CST2 */
517 return -1;
518 else if (TREE_CODE (val2) == MINUS_EXPR)
519 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
520 C1 and C2 are swapped in the call to compare_values. */
521 return compare_values (c2, c1);
524 gcc_unreachable ();
527 /* We cannot compare non-constants. */
528 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
529 return -2;
531 /* We cannot compare overflowed values. */
532 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
533 return -2;
535 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
536 return tree_int_cst_compare (val1, val2);
537 else
539 tree t;
541 /* First see if VAL1 and VAL2 are not the same. */
542 if (val1 == val2 || operand_equal_p (val1, val2, 0))
543 return 0;
545 /* If VAL1 is a lower address than VAL2, return -1. */
546 t = fold_binary (LT_EXPR, boolean_type_node, val1, val2);
547 if (t == boolean_true_node)
548 return -1;
550 /* If VAL1 is a higher address than VAL2, return +1. */
551 t = fold_binary (GT_EXPR, boolean_type_node, val1, val2);
552 if (t == boolean_true_node)
553 return 1;
555 /* If VAL1 is different than VAL2, return +2. */
556 t = fold_binary (NE_EXPR, boolean_type_node, val1, val2);
557 if (t == boolean_true_node)
558 return 2;
560 return -2;
565 /* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
566 0 if VAL is not inside VR,
567 -2 if we cannot tell either way.
569 FIXME, the current semantics of this functions are a bit quirky
570 when taken in the context of VRP. In here we do not care
571 about VR's type. If VR is the anti-range ~[3, 5] the call
572 value_inside_range (4, VR) will return 1.
574 This is counter-intuitive in a strict sense, but the callers
575 currently expect this. They are calling the function
576 merely to determine whether VR->MIN <= VAL <= VR->MAX. The
577 callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
578 themselves.
580 This also applies to value_ranges_intersect_p and
581 range_includes_zero_p. The semantics of VR_RANGE and
582 VR_ANTI_RANGE should be encoded here, but that also means
583 adapting the users of these functions to the new semantics. */
585 static inline int
586 value_inside_range (tree val, value_range_t *vr)
588 int cmp1, cmp2;
590 cmp1 = compare_values (val, vr->min);
591 if (cmp1 == -2 || cmp1 == 2)
592 return -2;
594 cmp2 = compare_values (val, vr->max);
595 if (cmp2 == -2 || cmp2 == 2)
596 return -2;
598 return (cmp1 == 0 || cmp1 == 1) && (cmp2 == -1 || cmp2 == 0);
602 /* Return true if value ranges VR0 and VR1 have a non-empty
603 intersection. */
605 static inline bool
606 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
608 return (value_inside_range (vr1->min, vr0) == 1
609 || value_inside_range (vr1->max, vr0) == 1
610 || value_inside_range (vr0->min, vr1) == 1
611 || value_inside_range (vr0->max, vr1) == 1);
615 /* Return true if VR includes the value zero, false otherwise. FIXME,
616 currently this will return false for an anti-range like ~[-4, 3].
617 This will be wrong when the semantics of value_inside_range are
618 modified (currently the users of this function expect these
619 semantics). */
621 static inline bool
622 range_includes_zero_p (value_range_t *vr)
624 tree zero;
626 gcc_assert (vr->type != VR_UNDEFINED
627 && vr->type != VR_VARYING
628 && !symbolic_range_p (vr));
630 zero = build_int_cst (TREE_TYPE (vr->min), 0);
631 return (value_inside_range (zero, vr) == 1);
635 /* Extract value range information from an ASSERT_EXPR EXPR and store
636 it in *VR_P. */
638 static void
639 extract_range_from_assert (value_range_t *vr_p, tree expr)
641 tree var, cond, limit, min, max, type;
642 value_range_t *var_vr, *limit_vr;
643 enum tree_code cond_code;
645 var = ASSERT_EXPR_VAR (expr);
646 cond = ASSERT_EXPR_COND (expr);
648 gcc_assert (COMPARISON_CLASS_P (cond));
650 /* Find VAR in the ASSERT_EXPR conditional. */
651 if (var == TREE_OPERAND (cond, 0))
653 /* If the predicate is of the form VAR COMP LIMIT, then we just
654 take LIMIT from the RHS and use the same comparison code. */
655 limit = TREE_OPERAND (cond, 1);
656 cond_code = TREE_CODE (cond);
658 else
660 /* If the predicate is of the form LIMIT COMP VAR, then we need
661 to flip around the comparison code to create the proper range
662 for VAR. */
663 limit = TREE_OPERAND (cond, 0);
664 cond_code = swap_tree_comparison (TREE_CODE (cond));
667 type = TREE_TYPE (limit);
668 gcc_assert (limit != var);
670 /* For pointer arithmetic, we only keep track of pointer equality
671 and inequality. */
672 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
674 set_value_range_to_varying (vr_p);
675 return;
678 /* If LIMIT is another SSA name and LIMIT has a range of its own,
679 try to use LIMIT's range to avoid creating symbolic ranges
680 unnecessarily. */
681 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
683 /* LIMIT's range is only interesting if it has any useful information. */
684 if (limit_vr
685 && (limit_vr->type == VR_UNDEFINED
686 || limit_vr->type == VR_VARYING
687 || symbolic_range_p (limit_vr)))
688 limit_vr = NULL;
690 /* Special handling for integral types with super-types. Some FEs
691 construct integral types derived from other types and restrict
692 the range of values these new types may take.
694 It may happen that LIMIT is actually smaller than TYPE's minimum
695 value. For instance, the Ada FE is generating code like this
696 during bootstrap:
698 D.1480_32 = nam_30 - 300000361;
699 if (D.1480_32 <= 1) goto <L112>; else goto <L52>;
700 <L112>:;
701 D.1480_94 = ASSERT_EXPR <D.1480_32, D.1480_32 <= 1>;
703 All the names are of type types__name_id___XDLU_300000000__399999999
704 which has min == 300000000 and max == 399999999. This means that
705 the ASSERT_EXPR would try to create the range [3000000, 1] which
706 is invalid.
708 The fact that the type specifies MIN and MAX values does not
709 automatically mean that every variable of that type will always
710 be within that range, so the predicate may well be true at run
711 time. If we had symbolic -INF and +INF values, we could
712 represent this range, but we currently represent -INF and +INF
713 using the type's min and max values.
715 So, the only sensible thing we can do for now is set the
716 resulting range to VR_VARYING. TODO, would having symbolic -INF
717 and +INF values be worth the trouble? */
718 if (TREE_CODE (limit) != SSA_NAME
719 && INTEGRAL_TYPE_P (type)
720 && TREE_TYPE (type))
722 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
724 tree type_min = TYPE_MIN_VALUE (type);
725 int cmp = compare_values (limit, type_min);
727 /* For < or <= comparisons, if LIMIT is smaller than
728 TYPE_MIN, set the range to VR_VARYING. */
729 if (cmp == -1 || cmp == 0)
731 set_value_range_to_varying (vr_p);
732 return;
735 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
737 tree type_max = TYPE_MIN_VALUE (type);
738 int cmp = compare_values (limit, type_max);
740 /* For > or >= comparisons, if LIMIT is bigger than
741 TYPE_MAX, set the range to VR_VARYING. */
742 if (cmp == 1 || cmp == 0)
744 set_value_range_to_varying (vr_p);
745 return;
750 /* The new range has the same set of equivalences of VAR's range. */
751 gcc_assert (vr_p->equiv == NULL);
752 vr_p->equiv = BITMAP_ALLOC (NULL);
753 add_equivalence (vr_p->equiv, var);
755 /* Extract a new range based on the asserted comparison for VAR and
756 LIMIT's value range. Notice that if LIMIT has an anti-range, we
757 will only use it for equality comparisons (EQ_EXPR). For any
758 other kind of assertion, we cannot derive a range from LIMIT's
759 anti-range that can be used to describe the new range. For
760 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
761 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
762 no single range for x_2 that could describe LE_EXPR, so we might
763 as well build the range [b_4, +INF] for it. */
764 if (cond_code == EQ_EXPR)
766 enum value_range_type range_type;
768 if (limit_vr)
770 range_type = limit_vr->type;
771 min = limit_vr->min;
772 max = limit_vr->max;
774 else
776 range_type = VR_RANGE;
777 min = limit;
778 max = limit;
781 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
783 /* When asserting the equality VAR == LIMIT and LIMIT is another
784 SSA name, the new range will also inherit the equivalence set
785 from LIMIT. */
786 if (TREE_CODE (limit) == SSA_NAME)
787 add_equivalence (vr_p->equiv, limit);
789 else if (cond_code == NE_EXPR)
791 /* As described above, when LIMIT's range is an anti-range and
792 this assertion is an inequality (NE_EXPR), then we cannot
793 derive anything from the anti-range. For instance, if
794 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
795 not imply that VAR's range is [0, 0]. So, in the case of
796 anti-ranges, we just assert the inequality using LIMIT and
797 not its anti-range.
799 If LIMIT_VR is a range, we can only use it to build a new
800 anti-range if LIMIT_VR is a single-valued range. For
801 instance, if LIMIT_VR is [0, 1], the predicate
802 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
803 Rather, it means that for value 0 VAR should be ~[0, 0]
804 and for value 1, VAR should be ~[1, 1]. We cannot
805 represent these ranges.
807 The only situation in which we can build a valid
808 anti-range is when LIMIT_VR is a single-valued range
809 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
810 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
811 if (limit_vr
812 && limit_vr->type == VR_RANGE
813 && compare_values (limit_vr->min, limit_vr->max) == 0)
815 min = limit_vr->min;
816 max = limit_vr->max;
818 else
820 /* In any other case, we cannot use LIMIT's range to build a
821 valid anti-range. */
822 min = max = limit;
825 /* If MIN and MAX cover the whole range for their type, then
826 just use the original LIMIT. */
827 if (INTEGRAL_TYPE_P (type)
828 && min == TYPE_MIN_VALUE (type)
829 && max == TYPE_MAX_VALUE (type))
830 min = max = limit;
832 set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
834 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
836 min = TYPE_MIN_VALUE (type);
838 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
839 max = limit;
840 else
842 /* If LIMIT_VR is of the form [N1, N2], we need to build the
843 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
844 LT_EXPR. */
845 max = limit_vr->max;
848 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
849 if (cond_code == LT_EXPR)
851 tree one = build_int_cst (type, 1);
852 max = fold_build2 (MINUS_EXPR, type, max, one);
855 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
857 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
859 max = TYPE_MAX_VALUE (type);
861 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
862 min = limit;
863 else
865 /* If LIMIT_VR is of the form [N1, N2], we need to build the
866 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
867 GT_EXPR. */
868 min = limit_vr->min;
871 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
872 if (cond_code == GT_EXPR)
874 tree one = build_int_cst (type, 1);
875 min = fold_build2 (PLUS_EXPR, type, min, one);
878 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
880 else
881 gcc_unreachable ();
883 /* If VAR already had a known range and the two ranges have a
884 non-empty intersection, we can refine the resulting range.
885 Since the assert expression creates an equivalency and at the
886 same time it asserts a predicate, we can take the intersection of
887 the two ranges to get better precision. */
888 var_vr = get_value_range (var);
889 if (var_vr->type == VR_RANGE
890 && vr_p->type == VR_RANGE
891 && value_ranges_intersect_p (var_vr, vr_p))
893 /* Use the larger of the two minimums. */
894 if (compare_values (vr_p->min, var_vr->min) == -1)
895 min = var_vr->min;
896 else
897 min = vr_p->min;
899 /* Use the smaller of the two maximums. */
900 if (compare_values (vr_p->max, var_vr->max) == 1)
901 max = var_vr->max;
902 else
903 max = vr_p->max;
905 set_value_range (vr_p, vr_p->type, min, max, vr_p->equiv);
910 /* Extract range information from SSA name VAR and store it in VR. If
911 VAR has an interesting range, use it. Otherwise, create the
912 range [VAR, VAR] and return it. This is useful in situations where
913 we may have conditionals testing values of VARYING names. For
914 instance,
916 x_3 = y_5;
917 if (x_3 > y_5)
920 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
921 always false. */
923 static void
924 extract_range_from_ssa_name (value_range_t *vr, tree var)
926 value_range_t *var_vr = get_value_range (var);
928 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
929 copy_value_range (vr, var_vr);
930 else
931 set_value_range (vr, VR_RANGE, var, var, NULL);
933 add_equivalence (vr->equiv, var);
937 /* Wrapper around int_const_binop. If the operation overflows and we
938 are not using wrapping arithmetic, then adjust the result to be
939 -INF or +INF depending on CODE, VAL1 and VAL2. */
941 static inline tree
942 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
944 tree res;
946 if (flag_wrapv)
947 return int_const_binop (code, val1, val2, 0);
949 /* If we are not using wrapping arithmetic, operate symbolically
950 on -INF and +INF. */
951 res = int_const_binop (code, val1, val2, 0);
953 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
955 int checkz = compare_values (res, val1);
957 /* Ensure that res = val1 + val2 >= val1
958 or that res = val1 - val2 <= val1. */
959 if ((code == PLUS_EXPR && !(checkz == 1 || checkz == 0))
960 || (code == MINUS_EXPR && !(checkz == 0 || checkz == -1)))
962 res = copy_node (res);
963 TREE_OVERFLOW (res) = 1;
966 /* If the operation overflowed but neither VAL1 nor VAL2 are
967 overflown, return -INF or +INF depending on the operation
968 and the combination of signs of the operands. */
969 else if (TREE_OVERFLOW (res)
970 && !TREE_OVERFLOW (val1)
971 && !TREE_OVERFLOW (val2))
973 int sgn1 = tree_int_cst_sgn (val1);
974 int sgn2 = tree_int_cst_sgn (val2);
976 /* Notice that we only need to handle the restricted set of
977 operations handled by extract_range_from_binary_expr.
978 Among them, only multiplication, addition and subtraction
979 can yield overflow without overflown operands because we
980 are working with integral types only... except in the
981 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
982 for division too. */
984 /* For multiplication, the sign of the overflow is given
985 by the comparison of the signs of the operands. */
986 if ((code == MULT_EXPR && sgn1 == sgn2)
987 /* For addition, the operands must be of the same sign
988 to yield an overflow. Its sign is therefore that
989 of one of the operands, for example the first. */
990 || (code == PLUS_EXPR && sgn1 > 0)
991 /* For subtraction, the operands must be of different
992 signs to yield an overflow. Its sign is therefore
993 that of the first operand or the opposite of that
994 of the second operand. A first operand of 0 counts
995 as positive here, for the corner case 0 - (-INF),
996 which overflows, but must yield +INF. */
997 || (code == MINUS_EXPR && sgn1 >= 0)
998 /* For division, the only case is -INF / -1 = +INF. */
999 || code == TRUNC_DIV_EXPR
1000 || code == FLOOR_DIV_EXPR
1001 || code == CEIL_DIV_EXPR
1002 || code == EXACT_DIV_EXPR
1003 || code == ROUND_DIV_EXPR)
1004 return TYPE_MAX_VALUE (TREE_TYPE (res));
1005 else
1006 return TYPE_MIN_VALUE (TREE_TYPE (res));
1009 return res;
1013 /* Extract range information from a binary expression EXPR based on
1014 the ranges of each of its operands and the expression code. */
1016 static void
1017 extract_range_from_binary_expr (value_range_t *vr, tree expr)
1019 enum tree_code code = TREE_CODE (expr);
1020 tree op0, op1, min, max;
1021 int cmp;
1022 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1023 value_range_t vr1 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1025 /* Not all binary expressions can be applied to ranges in a
1026 meaningful way. Handle only arithmetic operations. */
1027 if (code != PLUS_EXPR
1028 && code != MINUS_EXPR
1029 && code != MULT_EXPR
1030 && code != TRUNC_DIV_EXPR
1031 && code != FLOOR_DIV_EXPR
1032 && code != CEIL_DIV_EXPR
1033 && code != EXACT_DIV_EXPR
1034 && code != ROUND_DIV_EXPR
1035 && code != MIN_EXPR
1036 && code != MAX_EXPR
1037 && code != TRUTH_ANDIF_EXPR
1038 && code != TRUTH_ORIF_EXPR
1039 && code != TRUTH_AND_EXPR
1040 && code != TRUTH_OR_EXPR
1041 && code != TRUTH_XOR_EXPR)
1043 set_value_range_to_varying (vr);
1044 return;
1047 /* Get value ranges for each operand. For constant operands, create
1048 a new value range with the operand to simplify processing. */
1049 op0 = TREE_OPERAND (expr, 0);
1050 if (TREE_CODE (op0) == SSA_NAME)
1051 vr0 = *(get_value_range (op0));
1052 else if (is_gimple_min_invariant (op0))
1053 set_value_range (&vr0, VR_RANGE, op0, op0, NULL);
1054 else
1055 set_value_range_to_varying (&vr0);
1057 op1 = TREE_OPERAND (expr, 1);
1058 if (TREE_CODE (op1) == SSA_NAME)
1059 vr1 = *(get_value_range (op1));
1060 else if (is_gimple_min_invariant (op1))
1061 set_value_range (&vr1, VR_RANGE, op1, op1, NULL);
1062 else
1063 set_value_range_to_varying (&vr1);
1065 /* If either range is UNDEFINED, so is the result. */
1066 if (vr0.type == VR_UNDEFINED || vr1.type == VR_UNDEFINED)
1068 set_value_range_to_undefined (vr);
1069 return;
1072 /* Refuse to operate on VARYING ranges, ranges of different kinds
1073 and symbolic ranges. TODO, we may be able to derive anti-ranges
1074 in some cases. */
1075 if (vr0.type == VR_VARYING
1076 || vr1.type == VR_VARYING
1077 || vr0.type != vr1.type
1078 || symbolic_range_p (&vr0)
1079 || symbolic_range_p (&vr1))
1081 set_value_range_to_varying (vr);
1082 return;
1085 /* Now evaluate the expression to determine the new range. */
1086 if (POINTER_TYPE_P (TREE_TYPE (expr))
1087 || POINTER_TYPE_P (TREE_TYPE (op0))
1088 || POINTER_TYPE_P (TREE_TYPE (op1)))
1090 /* For pointer types, we are really only interested in asserting
1091 whether the expression evaluates to non-NULL. FIXME, we used
1092 to gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR), but
1093 ivopts is generating expressions with pointer multiplication
1094 in them. */
1095 if (code == PLUS_EXPR)
1097 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
1098 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1099 else if (range_is_null (&vr0) && range_is_null (&vr1))
1100 set_value_range_to_null (vr, TREE_TYPE (expr));
1101 else
1102 set_value_range_to_varying (vr);
1104 else
1106 /* Subtracting from a pointer, may yield 0, so just drop the
1107 resulting range to varying. */
1108 set_value_range_to_varying (vr);
1111 return;
1114 /* For integer ranges, apply the operation to each end of the
1115 range and see what we end up with. */
1116 if (code == TRUTH_ANDIF_EXPR
1117 || code == TRUTH_ORIF_EXPR
1118 || code == TRUTH_AND_EXPR
1119 || code == TRUTH_OR_EXPR
1120 || code == TRUTH_XOR_EXPR)
1122 /* Boolean expressions cannot be folded with int_const_binop. */
1123 min = fold_binary (code, TREE_TYPE (expr), vr0.min, vr1.min);
1124 max = fold_binary (code, TREE_TYPE (expr), vr0.max, vr1.max);
1126 else if (code == PLUS_EXPR
1127 || code == MIN_EXPR
1128 || code == MAX_EXPR)
1130 /* If we have a PLUS_EXPR with two VR_ANTI_RANGEs, drop to
1131 VR_VARYING. It would take more effort to compute a precise
1132 range for such a case. For example, if we have op0 == 1 and
1133 op1 == -1 with their ranges both being ~[0,0], we would have
1134 op0 + op1 == 0, so we cannot claim that the sum is in ~[0,0].
1135 Note that we are guaranteed to have vr0.type == vr1.type at
1136 this point. */
1137 if (code == PLUS_EXPR && vr0.type == VR_ANTI_RANGE)
1139 set_value_range_to_varying (vr);
1140 return;
1143 /* For operations that make the resulting range directly
1144 proportional to the original ranges, apply the operation to
1145 the same end of each range. */
1146 min = vrp_int_const_binop (code, vr0.min, vr1.min);
1147 max = vrp_int_const_binop (code, vr0.max, vr1.max);
1149 else if (code == MULT_EXPR
1150 || code == TRUNC_DIV_EXPR
1151 || code == FLOOR_DIV_EXPR
1152 || code == CEIL_DIV_EXPR
1153 || code == EXACT_DIV_EXPR
1154 || code == ROUND_DIV_EXPR)
1156 tree val[4];
1157 size_t i;
1159 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1160 drop to VR_VARYING. It would take more effort to compute a
1161 precise range for such a case. For example, if we have
1162 op0 == 65536 and op1 == 65536 with their ranges both being
1163 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1164 we cannot claim that the product is in ~[0,0]. Note that we
1165 are guaranteed to have vr0.type == vr1.type at this
1166 point. */
1167 if (code == MULT_EXPR
1168 && vr0.type == VR_ANTI_RANGE
1169 && (flag_wrapv || TYPE_UNSIGNED (TREE_TYPE (op0))))
1171 set_value_range_to_varying (vr);
1172 return;
1175 /* Multiplications and divisions are a bit tricky to handle,
1176 depending on the mix of signs we have in the two ranges, we
1177 need to operate on different values to get the minimum and
1178 maximum values for the new range. One approach is to figure
1179 out all the variations of range combinations and do the
1180 operations.
1182 However, this involves several calls to compare_values and it
1183 is pretty convoluted. It's simpler to do the 4 operations
1184 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1185 MAX1) and then figure the smallest and largest values to form
1186 the new range. */
1188 /* Divisions by zero result in a VARYING value. */
1189 if (code != MULT_EXPR
1190 && (vr0.type == VR_ANTI_RANGE || range_includes_zero_p (&vr1)))
1192 set_value_range_to_varying (vr);
1193 return;
1196 /* Compute the 4 cross operations. */
1197 val[0] = vrp_int_const_binop (code, vr0.min, vr1.min);
1199 val[1] = (vr1.max != vr1.min)
1200 ? vrp_int_const_binop (code, vr0.min, vr1.max)
1201 : NULL_TREE;
1203 val[2] = (vr0.max != vr0.min)
1204 ? vrp_int_const_binop (code, vr0.max, vr1.min)
1205 : NULL_TREE;
1207 val[3] = (vr0.min != vr0.max && vr1.min != vr1.max)
1208 ? vrp_int_const_binop (code, vr0.max, vr1.max)
1209 : NULL_TREE;
1211 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
1212 of VAL[i]. */
1213 min = val[0];
1214 max = val[0];
1215 for (i = 1; i < 4; i++)
1217 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
1218 break;
1220 if (val[i])
1222 if (TREE_OVERFLOW (val[i]))
1224 /* If we found an overflowed value, set MIN and MAX
1225 to it so that we set the resulting range to
1226 VARYING. */
1227 min = max = val[i];
1228 break;
1231 if (compare_values (val[i], min) == -1)
1232 min = val[i];
1234 if (compare_values (val[i], max) == 1)
1235 max = val[i];
1239 else if (code == MINUS_EXPR)
1241 /* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
1242 VR_VARYING. It would take more effort to compute a precise
1243 range for such a case. For example, if we have op0 == 1 and
1244 op1 == 1 with their ranges both being ~[0,0], we would have
1245 op0 - op1 == 0, so we cannot claim that the difference is in
1246 ~[0,0]. Note that we are guaranteed to have
1247 vr0.type == vr1.type at this point. */
1248 if (vr0.type == VR_ANTI_RANGE)
1250 set_value_range_to_varying (vr);
1251 return;
1254 /* For MINUS_EXPR, apply the operation to the opposite ends of
1255 each range. */
1256 min = vrp_int_const_binop (code, vr0.min, vr1.max);
1257 max = vrp_int_const_binop (code, vr0.max, vr1.min);
1259 else
1260 gcc_unreachable ();
1262 /* If either MIN or MAX overflowed, then set the resulting range to
1263 VARYING. */
1264 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
1266 set_value_range_to_varying (vr);
1267 return;
1270 cmp = compare_values (min, max);
1271 if (cmp == -2 || cmp == 1)
1273 /* If the new range has its limits swapped around (MIN > MAX),
1274 then the operation caused one of them to wrap around, mark
1275 the new range VARYING. */
1276 set_value_range_to_varying (vr);
1278 else
1279 set_value_range (vr, vr0.type, min, max, NULL);
1283 /* Extract range information from a unary expression EXPR based on
1284 the range of its operand and the expression code. */
1286 static void
1287 extract_range_from_unary_expr (value_range_t *vr, tree expr)
1289 enum tree_code code = TREE_CODE (expr);
1290 tree min, max, op0;
1291 int cmp;
1292 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1294 /* Refuse to operate on certain unary expressions for which we
1295 cannot easily determine a resulting range. */
1296 if (code == FIX_TRUNC_EXPR
1297 || code == FIX_CEIL_EXPR
1298 || code == FIX_FLOOR_EXPR
1299 || code == FIX_ROUND_EXPR
1300 || code == FLOAT_EXPR
1301 || code == BIT_NOT_EXPR
1302 || code == NON_LVALUE_EXPR
1303 || code == CONJ_EXPR)
1305 set_value_range_to_varying (vr);
1306 return;
1309 /* Get value ranges for the operand. For constant operands, create
1310 a new value range with the operand to simplify processing. */
1311 op0 = TREE_OPERAND (expr, 0);
1312 if (TREE_CODE (op0) == SSA_NAME)
1313 vr0 = *(get_value_range (op0));
1314 else if (is_gimple_min_invariant (op0))
1315 set_value_range (&vr0, VR_RANGE, op0, op0, NULL);
1316 else
1317 set_value_range_to_varying (&vr0);
1319 /* If VR0 is UNDEFINED, so is the result. */
1320 if (vr0.type == VR_UNDEFINED)
1322 set_value_range_to_undefined (vr);
1323 return;
1326 /* Refuse to operate on varying and symbolic ranges. Also, if the
1327 operand is neither a pointer nor an integral type, set the
1328 resulting range to VARYING. TODO, in some cases we may be able
1329 to derive anti-ranges (like nonzero values). */
1330 if (vr0.type == VR_VARYING
1331 || (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
1332 && !POINTER_TYPE_P (TREE_TYPE (op0)))
1333 || symbolic_range_p (&vr0))
1335 set_value_range_to_varying (vr);
1336 return;
1339 /* If the expression involves pointers, we are only interested in
1340 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
1341 if (POINTER_TYPE_P (TREE_TYPE (expr)) || POINTER_TYPE_P (TREE_TYPE (op0)))
1343 if (range_is_nonnull (&vr0) || tree_expr_nonzero_p (expr))
1344 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1345 else if (range_is_null (&vr0))
1346 set_value_range_to_null (vr, TREE_TYPE (expr));
1347 else
1348 set_value_range_to_varying (vr);
1350 return;
1353 /* Handle unary expressions on integer ranges. */
1354 if (code == NOP_EXPR || code == CONVERT_EXPR)
1356 tree inner_type = TREE_TYPE (op0);
1357 tree outer_type = TREE_TYPE (expr);
1359 /* If VR0 represents a simple range, then try to convert
1360 the min and max values for the range to the same type
1361 as OUTER_TYPE. If the results compare equal to VR0's
1362 min and max values and the new min is still less than
1363 or equal to the new max, then we can safely use the newly
1364 computed range for EXPR. This allows us to compute
1365 accurate ranges through many casts. */
1366 if (vr0.type == VR_RANGE)
1368 tree new_min, new_max;
1370 /* Convert VR0's min/max to OUTER_TYPE. */
1371 new_min = fold_convert (outer_type, vr0.min);
1372 new_max = fold_convert (outer_type, vr0.max);
1374 /* Verify the new min/max values are gimple values and
1375 that they compare equal to VR0's min/max values. */
1376 if (is_gimple_val (new_min)
1377 && is_gimple_val (new_max)
1378 && tree_int_cst_equal (new_min, vr0.min)
1379 && tree_int_cst_equal (new_max, vr0.max)
1380 && compare_values (new_min, new_max) <= 0
1381 && compare_values (new_min, new_max) >= -1)
1383 set_value_range (vr, VR_RANGE, new_min, new_max, vr->equiv);
1384 return;
1388 /* When converting types of different sizes, set the result to
1389 VARYING. Things like sign extensions and precision loss may
1390 change the range. For instance, if x_3 is of type 'long long
1391 int' and 'y_5 = (unsigned short) x_3', if x_3 is ~[0, 0], it
1392 is impossible to know at compile time whether y_5 will be
1393 ~[0, 0]. */
1394 if (TYPE_SIZE (inner_type) != TYPE_SIZE (outer_type)
1395 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
1397 set_value_range_to_varying (vr);
1398 return;
1402 /* Apply the operation to each end of the range and see what we end
1403 up with. */
1404 if (code == NEGATE_EXPR
1405 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
1407 /* NEGATE_EXPR flips the range around. */
1408 min = (vr0.max == TYPE_MAX_VALUE (TREE_TYPE (expr)) && !flag_wrapv)
1409 ? TYPE_MIN_VALUE (TREE_TYPE (expr))
1410 : fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1412 max = (vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr)) && !flag_wrapv)
1413 ? TYPE_MAX_VALUE (TREE_TYPE (expr))
1414 : fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
1416 else if (code == ABS_EXPR
1417 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
1419 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
1420 useful range. */
1421 if (flag_wrapv
1422 && ((vr0.type == VR_RANGE
1423 && vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr)))
1424 || (vr0.type == VR_ANTI_RANGE
1425 && vr0.min != TYPE_MIN_VALUE (TREE_TYPE (expr))
1426 && !range_includes_zero_p (&vr0))))
1428 set_value_range_to_varying (vr);
1429 return;
1432 /* ABS_EXPR may flip the range around, if the original range
1433 included negative values. */
1434 min = (vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr)))
1435 ? TYPE_MAX_VALUE (TREE_TYPE (expr))
1436 : fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
1438 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1440 cmp = compare_values (min, max);
1442 /* If a VR_ANTI_RANGEs contains zero, then we have
1443 ~[-INF, min(MIN, MAX)]. */
1444 if (vr0.type == VR_ANTI_RANGE)
1446 if (range_includes_zero_p (&vr0))
1448 tree type_min_value = TYPE_MIN_VALUE (TREE_TYPE (expr));
1450 /* Take the lower of the two values. */
1451 if (cmp != 1)
1452 max = min;
1454 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
1455 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
1456 flag_wrapv is set and the original anti-range doesn't include
1457 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
1458 min = (flag_wrapv && vr0.min != type_min_value
1459 ? int_const_binop (PLUS_EXPR,
1460 type_min_value,
1461 integer_one_node, 0)
1462 : type_min_value);
1464 else
1466 /* All else has failed, so create the range [0, INF], even for
1467 flag_wrapv since TYPE_MIN_VALUE is in the original
1468 anti-range. */
1469 vr0.type = VR_RANGE;
1470 min = build_int_cst (TREE_TYPE (expr), 0);
1471 max = TYPE_MAX_VALUE (TREE_TYPE (expr));
1475 /* If the range contains zero then we know that the minimum value in the
1476 range will be zero. */
1477 else if (range_includes_zero_p (&vr0))
1479 if (cmp == 1)
1480 max = min;
1481 min = build_int_cst (TREE_TYPE (expr), 0);
1483 else
1485 /* If the range was reversed, swap MIN and MAX. */
1486 if (cmp == 1)
1488 tree t = min;
1489 min = max;
1490 max = t;
1494 else
1496 /* Otherwise, operate on each end of the range. */
1497 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
1498 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1501 cmp = compare_values (min, max);
1502 if (cmp == -2 || cmp == 1)
1504 /* If the new range has its limits swapped around (MIN > MAX),
1505 then the operation caused one of them to wrap around, mark
1506 the new range VARYING. */
1507 set_value_range_to_varying (vr);
1509 else
1510 set_value_range (vr, vr0.type, min, max, NULL);
1514 /* Extract range information from a comparison expression EXPR based
1515 on the range of its operand and the expression code. */
1517 static void
1518 extract_range_from_comparison (value_range_t *vr, tree expr)
1520 tree val = vrp_evaluate_conditional (expr, false);
1521 if (val)
1523 /* Since this expression was found on the RHS of an assignment,
1524 its type may be different from _Bool. Convert VAL to EXPR's
1525 type. */
1526 val = fold_convert (TREE_TYPE (expr), val);
1527 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
1529 else
1530 set_value_range_to_varying (vr);
1534 /* Try to compute a useful range out of expression EXPR and store it
1535 in *VR. */
1537 static void
1538 extract_range_from_expr (value_range_t *vr, tree expr)
1540 enum tree_code code = TREE_CODE (expr);
1542 if (code == ASSERT_EXPR)
1543 extract_range_from_assert (vr, expr);
1544 else if (code == SSA_NAME)
1545 extract_range_from_ssa_name (vr, expr);
1546 else if (TREE_CODE_CLASS (code) == tcc_binary
1547 || code == TRUTH_ANDIF_EXPR
1548 || code == TRUTH_ORIF_EXPR
1549 || code == TRUTH_AND_EXPR
1550 || code == TRUTH_OR_EXPR
1551 || code == TRUTH_XOR_EXPR)
1552 extract_range_from_binary_expr (vr, expr);
1553 else if (TREE_CODE_CLASS (code) == tcc_unary)
1554 extract_range_from_unary_expr (vr, expr);
1555 else if (TREE_CODE_CLASS (code) == tcc_comparison)
1556 extract_range_from_comparison (vr, expr);
1557 else if (is_gimple_min_invariant (expr))
1558 set_value_range (vr, VR_RANGE, expr, expr, NULL);
1559 else if (vrp_expr_computes_nonzero (expr))
1560 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1561 else
1562 set_value_range_to_varying (vr);
1565 /* Given a range VR, a LOOP and a variable VAR, determine whether it
1566 would be profitable to adjust VR using scalar evolution information
1567 for VAR. If so, update VR with the new limits. */
1569 static void
1570 adjust_range_with_scev (value_range_t *vr, struct loop *loop, tree stmt,
1571 tree var)
1573 tree init, step, chrec;
1574 bool init_is_max, unknown_max;
1576 /* TODO. Don't adjust anti-ranges. An anti-range may provide
1577 better opportunities than a regular range, but I'm not sure. */
1578 if (vr->type == VR_ANTI_RANGE)
1579 return;
1581 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
1582 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
1583 return;
1585 init = initial_condition_in_loop_num (chrec, loop->num);
1586 step = evolution_part_in_loop_num (chrec, loop->num);
1588 /* If STEP is symbolic, we can't know whether INIT will be the
1589 minimum or maximum value in the range. */
1590 if (step == NULL_TREE
1591 || !is_gimple_min_invariant (step))
1592 return;
1594 /* Do not adjust ranges when chrec may wrap. */
1595 if (scev_probably_wraps_p (chrec_type (chrec), init, step, stmt,
1596 cfg_loops->parray[CHREC_VARIABLE (chrec)],
1597 &init_is_max, &unknown_max)
1598 || unknown_max)
1599 return;
1601 if (!POINTER_TYPE_P (TREE_TYPE (init))
1602 && (vr->type == VR_VARYING || vr->type == VR_UNDEFINED))
1604 /* For VARYING or UNDEFINED ranges, just about anything we get
1605 from scalar evolutions should be better. */
1606 if (init_is_max)
1607 set_value_range (vr, VR_RANGE, TYPE_MIN_VALUE (TREE_TYPE (init)),
1608 init, vr->equiv);
1609 else
1610 set_value_range (vr, VR_RANGE, init, TYPE_MAX_VALUE (TREE_TYPE (init)),
1611 vr->equiv);
1613 else if (vr->type == VR_RANGE)
1615 tree min = vr->min;
1616 tree max = vr->max;
1618 if (init_is_max)
1620 /* INIT is the maximum value. If INIT is lower than VR->MAX
1621 but no smaller than VR->MIN, set VR->MAX to INIT. */
1622 if (compare_values (init, max) == -1)
1624 max = init;
1626 /* If we just created an invalid range with the minimum
1627 greater than the maximum, take the minimum all the
1628 way to -INF. */
1629 if (compare_values (min, max) == 1)
1630 min = TYPE_MIN_VALUE (TREE_TYPE (min));
1633 else
1635 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
1636 if (compare_values (init, min) == 1)
1638 min = init;
1640 /* If we just created an invalid range with the minimum
1641 greater than the maximum, take the maximum all the
1642 way to +INF. */
1643 if (compare_values (min, max) == 1)
1644 max = TYPE_MAX_VALUE (TREE_TYPE (max));
1648 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
1653 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
1655 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
1656 all the values in the ranges.
1658 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
1660 - Return NULL_TREE if it is not always possible to determine the
1661 value of the comparison. */
1664 static tree
1665 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1)
1667 /* VARYING or UNDEFINED ranges cannot be compared. */
1668 if (vr0->type == VR_VARYING
1669 || vr0->type == VR_UNDEFINED
1670 || vr1->type == VR_VARYING
1671 || vr1->type == VR_UNDEFINED)
1672 return NULL_TREE;
1674 /* Anti-ranges need to be handled separately. */
1675 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
1677 /* If both are anti-ranges, then we cannot compute any
1678 comparison. */
1679 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
1680 return NULL_TREE;
1682 /* These comparisons are never statically computable. */
1683 if (comp == GT_EXPR
1684 || comp == GE_EXPR
1685 || comp == LT_EXPR
1686 || comp == LE_EXPR)
1687 return NULL_TREE;
1689 /* Equality can be computed only between a range and an
1690 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
1691 if (vr0->type == VR_RANGE)
1693 /* To simplify processing, make VR0 the anti-range. */
1694 value_range_t *tmp = vr0;
1695 vr0 = vr1;
1696 vr1 = tmp;
1699 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
1701 if (compare_values (vr0->min, vr1->min) == 0
1702 && compare_values (vr0->max, vr1->max) == 0)
1703 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
1705 return NULL_TREE;
1708 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
1709 operands around and change the comparison code. */
1710 if (comp == GT_EXPR || comp == GE_EXPR)
1712 value_range_t *tmp;
1713 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
1714 tmp = vr0;
1715 vr0 = vr1;
1716 vr1 = tmp;
1719 if (comp == EQ_EXPR)
1721 /* Equality may only be computed if both ranges represent
1722 exactly one value. */
1723 if (compare_values (vr0->min, vr0->max) == 0
1724 && compare_values (vr1->min, vr1->max) == 0)
1726 int cmp_min = compare_values (vr0->min, vr1->min);
1727 int cmp_max = compare_values (vr0->max, vr1->max);
1728 if (cmp_min == 0 && cmp_max == 0)
1729 return boolean_true_node;
1730 else if (cmp_min != -2 && cmp_max != -2)
1731 return boolean_false_node;
1734 return NULL_TREE;
1736 else if (comp == NE_EXPR)
1738 int cmp1, cmp2;
1740 /* If VR0 is completely to the left or completely to the right
1741 of VR1, they are always different. Notice that we need to
1742 make sure that both comparisons yield similar results to
1743 avoid comparing values that cannot be compared at
1744 compile-time. */
1745 cmp1 = compare_values (vr0->max, vr1->min);
1746 cmp2 = compare_values (vr0->min, vr1->max);
1747 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
1748 return boolean_true_node;
1750 /* If VR0 and VR1 represent a single value and are identical,
1751 return false. */
1752 else if (compare_values (vr0->min, vr0->max) == 0
1753 && compare_values (vr1->min, vr1->max) == 0
1754 && compare_values (vr0->min, vr1->min) == 0
1755 && compare_values (vr0->max, vr1->max) == 0)
1756 return boolean_false_node;
1758 /* Otherwise, they may or may not be different. */
1759 else
1760 return NULL_TREE;
1762 else if (comp == LT_EXPR || comp == LE_EXPR)
1764 int tst;
1766 /* If VR0 is to the left of VR1, return true. */
1767 tst = compare_values (vr0->max, vr1->min);
1768 if ((comp == LT_EXPR && tst == -1)
1769 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
1770 return boolean_true_node;
1772 /* If VR0 is to the right of VR1, return false. */
1773 tst = compare_values (vr0->min, vr1->max);
1774 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
1775 || (comp == LE_EXPR && tst == 1))
1776 return boolean_false_node;
1778 /* Otherwise, we don't know. */
1779 return NULL_TREE;
1782 gcc_unreachable ();
1786 /* Given a value range VR, a value VAL and a comparison code COMP, return
1787 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
1788 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
1789 always returns false. Return NULL_TREE if it is not always
1790 possible to determine the value of the comparison. */
1792 static tree
1793 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val)
1795 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
1796 return NULL_TREE;
1798 /* Anti-ranges need to be handled separately. */
1799 if (vr->type == VR_ANTI_RANGE)
1801 /* For anti-ranges, the only predicates that we can compute at
1802 compile time are equality and inequality. */
1803 if (comp == GT_EXPR
1804 || comp == GE_EXPR
1805 || comp == LT_EXPR
1806 || comp == LE_EXPR)
1807 return NULL_TREE;
1809 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
1810 if (value_inside_range (val, vr) == 1)
1811 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
1813 return NULL_TREE;
1816 if (comp == EQ_EXPR)
1818 /* EQ_EXPR may only be computed if VR represents exactly
1819 one value. */
1820 if (compare_values (vr->min, vr->max) == 0)
1822 int cmp = compare_values (vr->min, val);
1823 if (cmp == 0)
1824 return boolean_true_node;
1825 else if (cmp == -1 || cmp == 1 || cmp == 2)
1826 return boolean_false_node;
1828 else if (compare_values (val, vr->min) == -1
1829 || compare_values (vr->max, val) == -1)
1830 return boolean_false_node;
1832 return NULL_TREE;
1834 else if (comp == NE_EXPR)
1836 /* If VAL is not inside VR, then they are always different. */
1837 if (compare_values (vr->max, val) == -1
1838 || compare_values (vr->min, val) == 1)
1839 return boolean_true_node;
1841 /* If VR represents exactly one value equal to VAL, then return
1842 false. */
1843 if (compare_values (vr->min, vr->max) == 0
1844 && compare_values (vr->min, val) == 0)
1845 return boolean_false_node;
1847 /* Otherwise, they may or may not be different. */
1848 return NULL_TREE;
1850 else if (comp == LT_EXPR || comp == LE_EXPR)
1852 int tst;
1854 /* If VR is to the left of VAL, return true. */
1855 tst = compare_values (vr->max, val);
1856 if ((comp == LT_EXPR && tst == -1)
1857 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
1858 return boolean_true_node;
1860 /* If VR is to the right of VAL, return false. */
1861 tst = compare_values (vr->min, val);
1862 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
1863 || (comp == LE_EXPR && tst == 1))
1864 return boolean_false_node;
1866 /* Otherwise, we don't know. */
1867 return NULL_TREE;
1869 else if (comp == GT_EXPR || comp == GE_EXPR)
1871 int tst;
1873 /* If VR is to the right of VAL, return true. */
1874 tst = compare_values (vr->min, val);
1875 if ((comp == GT_EXPR && tst == 1)
1876 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
1877 return boolean_true_node;
1879 /* If VR is to the left of VAL, return false. */
1880 tst = compare_values (vr->max, val);
1881 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
1882 || (comp == GE_EXPR && tst == -1))
1883 return boolean_false_node;
1885 /* Otherwise, we don't know. */
1886 return NULL_TREE;
1889 gcc_unreachable ();
1893 /* Debugging dumps. */
1895 void dump_value_range (FILE *, value_range_t *);
1896 void debug_value_range (value_range_t *);
1897 void dump_all_value_ranges (FILE *);
1898 void debug_all_value_ranges (void);
1899 void dump_vr_equiv (FILE *, bitmap);
1900 void debug_vr_equiv (bitmap);
1903 /* Dump value range VR to FILE. */
1905 void
1906 dump_value_range (FILE *file, value_range_t *vr)
1908 if (vr == NULL)
1909 fprintf (file, "[]");
1910 else if (vr->type == VR_UNDEFINED)
1911 fprintf (file, "UNDEFINED");
1912 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
1914 tree type = TREE_TYPE (vr->min);
1916 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
1918 if (INTEGRAL_TYPE_P (type)
1919 && !TYPE_UNSIGNED (type)
1920 && vr->min == TYPE_MIN_VALUE (type))
1921 fprintf (file, "-INF");
1922 else
1923 print_generic_expr (file, vr->min, 0);
1925 fprintf (file, ", ");
1927 if (INTEGRAL_TYPE_P (type)
1928 && vr->max == TYPE_MAX_VALUE (type))
1929 fprintf (file, "+INF");
1930 else
1931 print_generic_expr (file, vr->max, 0);
1933 fprintf (file, "]");
1935 if (vr->equiv)
1937 bitmap_iterator bi;
1938 unsigned i, c = 0;
1940 fprintf (file, " EQUIVALENCES: { ");
1942 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
1944 print_generic_expr (file, ssa_name (i), 0);
1945 fprintf (file, " ");
1946 c++;
1949 fprintf (file, "} (%u elements)", c);
1952 else if (vr->type == VR_VARYING)
1953 fprintf (file, "VARYING");
1954 else
1955 fprintf (file, "INVALID RANGE");
1959 /* Dump value range VR to stderr. */
1961 void
1962 debug_value_range (value_range_t *vr)
1964 dump_value_range (stderr, vr);
1968 /* Dump value ranges of all SSA_NAMEs to FILE. */
1970 void
1971 dump_all_value_ranges (FILE *file)
1973 size_t i;
1975 for (i = 0; i < num_ssa_names; i++)
1977 if (vr_value[i])
1979 print_generic_expr (file, ssa_name (i), 0);
1980 fprintf (file, ": ");
1981 dump_value_range (file, vr_value[i]);
1982 fprintf (file, "\n");
1986 fprintf (file, "\n");
1990 /* Dump all value ranges to stderr. */
1992 void
1993 debug_all_value_ranges (void)
1995 dump_all_value_ranges (stderr);
1999 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2000 create a new SSA name N and return the assertion assignment
2001 'V = ASSERT_EXPR <V, V OP W>'. */
2003 static tree
2004 build_assert_expr_for (tree cond, tree v)
2006 tree n, assertion;
2008 gcc_assert (TREE_CODE (v) == SSA_NAME);
2009 n = duplicate_ssa_name (v, NULL_TREE);
2011 if (COMPARISON_CLASS_P (cond))
2013 tree a = build (ASSERT_EXPR, TREE_TYPE (v), v, cond);
2014 assertion = build (MODIFY_EXPR, TREE_TYPE (v), n, a);
2016 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
2018 /* Given !V, build the assignment N = false. */
2019 tree op0 = TREE_OPERAND (cond, 0);
2020 gcc_assert (op0 == v);
2021 assertion = build (MODIFY_EXPR, TREE_TYPE (v), n, boolean_false_node);
2023 else if (TREE_CODE (cond) == SSA_NAME)
2025 /* Given V, build the assignment N = true. */
2026 gcc_assert (v == cond);
2027 assertion = build (MODIFY_EXPR, TREE_TYPE (v), n, boolean_true_node);
2029 else
2030 gcc_unreachable ();
2032 SSA_NAME_DEF_STMT (n) = assertion;
2034 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2035 operand of the ASSERT_EXPR. Register the new name and the old one
2036 in the replacement table so that we can fix the SSA web after
2037 adding all the ASSERT_EXPRs. */
2038 register_new_name_mapping (n, v);
2040 return assertion;
2044 /* Return false if EXPR is a predicate expression involving floating
2045 point values. */
2047 static inline bool
2048 fp_predicate (tree expr)
2050 return (COMPARISON_CLASS_P (expr)
2051 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0))));
2055 /* If the range of values taken by OP can be inferred after STMT executes,
2056 return the comparison code (COMP_CODE_P) and value (VAL_P) that
2057 describes the inferred range. Return true if a range could be
2058 inferred. */
2060 static bool
2061 infer_value_range (tree stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
2063 *val_p = NULL_TREE;
2064 *comp_code_p = ERROR_MARK;
2066 /* Do not attempt to infer anything in names that flow through
2067 abnormal edges. */
2068 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
2069 return false;
2071 /* Similarly, don't infer anything from statements that may throw
2072 exceptions. */
2073 if (tree_could_throw_p (stmt))
2074 return false;
2076 if (POINTER_TYPE_P (TREE_TYPE (op)))
2078 bool is_store;
2079 unsigned num_uses, num_derefs;
2081 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs, &is_store);
2082 if (num_derefs > 0 && flag_delete_null_pointer_checks)
2084 /* We can only assume that a pointer dereference will yield
2085 non-NULL if -fdelete-null-pointer-checks is enabled. */
2086 *val_p = build_int_cst (TREE_TYPE (op), 0);
2087 *comp_code_p = NE_EXPR;
2088 return true;
2092 return false;
2096 void dump_asserts_for (FILE *, tree);
2097 void debug_asserts_for (tree);
2098 void dump_all_asserts (FILE *);
2099 void debug_all_asserts (void);
2101 /* Dump all the registered assertions for NAME to FILE. */
2103 void
2104 dump_asserts_for (FILE *file, tree name)
2106 assert_locus_t loc;
2108 fprintf (file, "Assertions to be inserted for ");
2109 print_generic_expr (file, name, 0);
2110 fprintf (file, "\n");
2112 loc = asserts_for[SSA_NAME_VERSION (name)];
2113 while (loc)
2115 fprintf (file, "\t");
2116 print_generic_expr (file, bsi_stmt (loc->si), 0);
2117 fprintf (file, "\n\tBB #%d", loc->bb->index);
2118 if (loc->e)
2120 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
2121 loc->e->dest->index);
2122 dump_edge_info (file, loc->e, 0);
2124 fprintf (file, "\n\tPREDICATE: ");
2125 print_generic_expr (file, name, 0);
2126 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
2127 print_generic_expr (file, loc->val, 0);
2128 fprintf (file, "\n\n");
2129 loc = loc->next;
2132 fprintf (file, "\n");
2136 /* Dump all the registered assertions for NAME to stderr. */
2138 void
2139 debug_asserts_for (tree name)
2141 dump_asserts_for (stderr, name);
2145 /* Dump all the registered assertions for all the names to FILE. */
2147 void
2148 dump_all_asserts (FILE *file)
2150 unsigned i;
2151 bitmap_iterator bi;
2153 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
2154 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
2155 dump_asserts_for (file, ssa_name (i));
2156 fprintf (file, "\n");
2160 /* Dump all the registered assertions for all the names to stderr. */
2162 void
2163 debug_all_asserts (void)
2165 dump_all_asserts (stderr);
2169 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2170 'NAME COMP_CODE VAL' at a location that dominates block BB or
2171 E->DEST, then register this location as a possible insertion point
2172 for ASSERT_EXPR <NAME, NAME COMP_CODE VAL>.
2174 BB, E and SI provide the exact insertion point for the new
2175 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2176 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2177 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2178 must not be NULL. */
2180 static void
2181 register_new_assert_for (tree name,
2182 enum tree_code comp_code,
2183 tree val,
2184 basic_block bb,
2185 edge e,
2186 block_stmt_iterator si)
2188 assert_locus_t n, loc, last_loc;
2189 bool found;
2190 basic_block dest_bb;
2192 #if defined ENABLE_CHECKING
2193 gcc_assert (bb == NULL || e == NULL);
2195 if (e == NULL)
2196 gcc_assert (TREE_CODE (bsi_stmt (si)) != COND_EXPR
2197 && TREE_CODE (bsi_stmt (si)) != SWITCH_EXPR);
2198 #endif
2200 /* The new assertion A will be inserted at BB or E. We need to
2201 determine if the new location is dominated by a previously
2202 registered location for A. If we are doing an edge insertion,
2203 assume that A will be inserted at E->DEST. Note that this is not
2204 necessarily true.
2206 If E is a critical edge, it will be split. But even if E is
2207 split, the new block will dominate the same set of blocks that
2208 E->DEST dominates.
2210 The reverse, however, is not true, blocks dominated by E->DEST
2211 will not be dominated by the new block created to split E. So,
2212 if the insertion location is on a critical edge, we will not use
2213 the new location to move another assertion previously registered
2214 at a block dominated by E->DEST. */
2215 dest_bb = (bb) ? bb : e->dest;
2217 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2218 VAL at a block dominating DEST_BB, then we don't need to insert a new
2219 one. Similarly, if the same assertion already exists at a block
2220 dominated by DEST_BB and the new location is not on a critical
2221 edge, then update the existing location for the assertion (i.e.,
2222 move the assertion up in the dominance tree).
2224 Note, this is implemented as a simple linked list because there
2225 should not be more than a handful of assertions registered per
2226 name. If this becomes a performance problem, a table hashed by
2227 COMP_CODE and VAL could be implemented. */
2228 loc = asserts_for[SSA_NAME_VERSION (name)];
2229 last_loc = loc;
2230 found = false;
2231 while (loc)
2233 if (loc->comp_code == comp_code
2234 && (loc->val == val
2235 || operand_equal_p (loc->val, val, 0)))
2237 /* If the assertion NAME COMP_CODE VAL has already been
2238 registered at a basic block that dominates DEST_BB, then
2239 we don't need to insert the same assertion again. Note
2240 that we don't check strict dominance here to avoid
2241 replicating the same assertion inside the same basic
2242 block more than once (e.g., when a pointer is
2243 dereferenced several times inside a block).
2245 An exception to this rule are edge insertions. If the
2246 new assertion is to be inserted on edge E, then it will
2247 dominate all the other insertions that we may want to
2248 insert in DEST_BB. So, if we are doing an edge
2249 insertion, don't do this dominance check. */
2250 if (e == NULL
2251 && dominated_by_p (CDI_DOMINATORS, dest_bb, loc->bb))
2252 return;
2254 /* Otherwise, if E is not a critical edge and DEST_BB
2255 dominates the existing location for the assertion, move
2256 the assertion up in the dominance tree by updating its
2257 location information. */
2258 if ((e == NULL || !EDGE_CRITICAL_P (e))
2259 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
2261 loc->bb = dest_bb;
2262 loc->e = e;
2263 loc->si = si;
2264 return;
2268 /* Update the last node of the list and move to the next one. */
2269 last_loc = loc;
2270 loc = loc->next;
2273 /* If we didn't find an assertion already registered for
2274 NAME COMP_CODE VAL, add a new one at the end of the list of
2275 assertions associated with NAME. */
2276 n = xmalloc (sizeof (*n));
2277 n->bb = dest_bb;
2278 n->e = e;
2279 n->si = si;
2280 n->comp_code = comp_code;
2281 n->val = val;
2282 n->next = NULL;
2284 if (last_loc)
2285 last_loc->next = n;
2286 else
2287 asserts_for[SSA_NAME_VERSION (name)] = n;
2289 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
2293 /* Try to register an edge assertion for SSA name NAME on edge E for
2294 the conditional jump pointed to by SI. Return true if an assertion
2295 for NAME could be registered. */
2297 static bool
2298 register_edge_assert_for (tree name, edge e, block_stmt_iterator si)
2300 tree val, stmt;
2301 enum tree_code comp_code;
2303 stmt = bsi_stmt (si);
2305 /* Do not attempt to infer anything in names that flow through
2306 abnormal edges. */
2307 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
2308 return false;
2310 /* If NAME was not found in the sub-graph reachable from E, then
2311 there's nothing to do. */
2312 if (!TEST_BIT (found_in_subgraph, SSA_NAME_VERSION (name)))
2313 return false;
2315 /* We found a use of NAME in the sub-graph rooted at E->DEST.
2316 Register an assertion for NAME according to the value that NAME
2317 takes on edge E. */
2318 if (TREE_CODE (stmt) == COND_EXPR)
2320 /* If BB ends in a COND_EXPR then NAME then we should insert
2321 the original predicate on EDGE_TRUE_VALUE and the
2322 opposite predicate on EDGE_FALSE_VALUE. */
2323 tree cond = COND_EXPR_COND (stmt);
2324 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
2326 /* Predicates may be a single SSA name or NAME OP VAL. */
2327 if (cond == name)
2329 /* If the predicate is a name, it must be NAME, in which
2330 case we create the predicate NAME == true or
2331 NAME == false accordingly. */
2332 comp_code = EQ_EXPR;
2333 val = (is_else_edge) ? boolean_false_node : boolean_true_node;
2335 else
2337 /* Otherwise, we have a comparison of the form NAME COMP VAL
2338 or VAL COMP NAME. */
2339 if (name == TREE_OPERAND (cond, 1))
2341 /* If the predicate is of the form VAL COMP NAME, flip
2342 COMP around because we need to register NAME as the
2343 first operand in the predicate. */
2344 comp_code = swap_tree_comparison (TREE_CODE (cond));
2345 val = TREE_OPERAND (cond, 0);
2347 else
2349 /* The comparison is of the form NAME COMP VAL, so the
2350 comparison code remains unchanged. */
2351 comp_code = TREE_CODE (cond);
2352 val = TREE_OPERAND (cond, 1);
2355 /* If we are inserting the assertion on the ELSE edge, we
2356 need to invert the sign comparison. */
2357 if (is_else_edge)
2358 comp_code = invert_tree_comparison (comp_code, 0);
2361 else
2363 /* FIXME. Handle SWITCH_EXPR. */
2364 gcc_unreachable ();
2367 register_new_assert_for (name, comp_code, val, NULL, e, si);
2368 return true;
2372 static bool find_assert_locations (basic_block bb);
2374 /* Determine whether the outgoing edges of BB should receive an
2375 ASSERT_EXPR for each of the operands of BB's last statement. The
2376 last statement of BB must be a COND_EXPR or a SWITCH_EXPR.
2378 If any of the sub-graphs rooted at BB have an interesting use of
2379 the predicate operands, an assert location node is added to the
2380 list of assertions for the corresponding operands. */
2382 static bool
2383 find_conditional_asserts (basic_block bb)
2385 bool need_assert;
2386 block_stmt_iterator last_si;
2387 tree op, last;
2388 edge_iterator ei;
2389 edge e;
2390 ssa_op_iter iter;
2392 need_assert = false;
2393 last_si = bsi_last (bb);
2394 last = bsi_stmt (last_si);
2396 /* Look for uses of the operands in each of the sub-graphs
2397 rooted at BB. We need to check each of the outgoing edges
2398 separately, so that we know what kind of ASSERT_EXPR to
2399 insert. */
2400 FOR_EACH_EDGE (e, ei, bb->succs)
2402 if (e->dest == bb)
2403 continue;
2405 /* Remove the COND_EXPR operands from the FOUND_IN_SUBGRAPH bitmap.
2406 Otherwise, when we finish traversing each of the sub-graphs, we
2407 won't know whether the variables were found in the sub-graphs or
2408 if they had been found in a block upstream from BB. */
2409 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
2410 RESET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
2412 /* Traverse the strictly dominated sub-graph rooted at E->DEST
2413 to determine if any of the operands in the conditional
2414 predicate are used. */
2415 if (e->dest != bb)
2416 need_assert |= find_assert_locations (e->dest);
2418 /* Register the necessary assertions for each operand in the
2419 conditional predicate. */
2420 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
2421 need_assert |= register_edge_assert_for (op, e, last_si);
2424 /* Finally, indicate that we have found the operands in the
2425 conditional. */
2426 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
2427 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
2429 return need_assert;
2433 /* Traverse all the statements in block BB looking for statements that
2434 may generate useful assertions for the SSA names in their operand.
2435 If a statement produces a useful assertion A for name N_i, then the
2436 list of assertions already generated for N_i is scanned to
2437 determine if A is actually needed.
2439 If N_i already had the assertion A at a location dominating the
2440 current location, then nothing needs to be done. Otherwise, the
2441 new location for A is recorded instead.
2443 1- For every statement S in BB, all the variables used by S are
2444 added to bitmap FOUND_IN_SUBGRAPH.
2446 2- If statement S uses an operand N in a way that exposes a known
2447 value range for N, then if N was not already generated by an
2448 ASSERT_EXPR, create a new assert location for N. For instance,
2449 if N is a pointer and the statement dereferences it, we can
2450 assume that N is not NULL.
2452 3- COND_EXPRs are a special case of #2. We can derive range
2453 information from the predicate but need to insert different
2454 ASSERT_EXPRs for each of the sub-graphs rooted at the
2455 conditional block. If the last statement of BB is a conditional
2456 expression of the form 'X op Y', then
2458 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
2460 b) If the conditional is the only entry point to the sub-graph
2461 corresponding to the THEN_CLAUSE, recurse into it. On
2462 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
2463 an ASSERT_EXPR is added for the corresponding variable.
2465 c) Repeat step (b) on the ELSE_CLAUSE.
2467 d) Mark X and Y in FOUND_IN_SUBGRAPH.
2469 For instance,
2471 if (a == 9)
2472 b = a;
2473 else
2474 b = c + 1;
2476 In this case, an assertion on the THEN clause is useful to
2477 determine that 'a' is always 9 on that edge. However, an assertion
2478 on the ELSE clause would be unnecessary.
2480 4- If BB does not end in a conditional expression, then we recurse
2481 into BB's dominator children.
2483 At the end of the recursive traversal, every SSA name will have a
2484 list of locations where ASSERT_EXPRs should be added. When a new
2485 location for name N is found, it is registered by calling
2486 register_new_assert_for. That function keeps track of all the
2487 registered assertions to prevent adding unnecessary assertions.
2488 For instance, if a pointer P_4 is dereferenced more than once in a
2489 dominator tree, only the location dominating all the dereference of
2490 P_4 will receive an ASSERT_EXPR.
2492 If this function returns true, then it means that there are names
2493 for which we need to generate ASSERT_EXPRs. Those assertions are
2494 inserted by process_assert_insertions.
2496 TODO. Handle SWITCH_EXPR. */
2498 static bool
2499 find_assert_locations (basic_block bb)
2501 block_stmt_iterator si;
2502 tree last, phi;
2503 bool need_assert;
2504 basic_block son;
2506 if (TEST_BIT (blocks_visited, bb->index))
2507 return false;
2509 SET_BIT (blocks_visited, bb->index);
2511 need_assert = false;
2513 /* Traverse all PHI nodes in BB marking used operands. */
2514 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2516 use_operand_p arg_p;
2517 ssa_op_iter i;
2519 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
2521 tree arg = USE_FROM_PTR (arg_p);
2522 if (TREE_CODE (arg) == SSA_NAME)
2524 gcc_assert (is_gimple_reg (PHI_RESULT (phi)));
2525 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (arg));
2530 /* Traverse all the statements in BB marking used names and looking
2531 for statements that may infer assertions for their used operands. */
2532 last = NULL_TREE;
2533 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2535 tree stmt, op;
2536 ssa_op_iter i;
2538 stmt = bsi_stmt (si);
2540 /* See if we can derive an assertion for any of STMT's operands. */
2541 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
2543 tree value;
2544 enum tree_code comp_code;
2546 /* Mark OP in bitmap FOUND_IN_SUBGRAPH. If STMT is inside
2547 the sub-graph of a conditional block, when we return from
2548 this recursive walk, our parent will use the
2549 FOUND_IN_SUBGRAPH bitset to determine if one of the
2550 operands it was looking for was present in the sub-graph. */
2551 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
2553 /* If OP is used only once, namely in this STMT, don't
2554 bother creating an ASSERT_EXPR for it. Such an
2555 ASSERT_EXPR would do nothing but increase compile time.
2556 Experiments show that with this simple check, we can save
2557 more than 20% of ASSERT_EXPRs. */
2558 if (has_single_use (op))
2559 continue;
2561 /* If OP is used in such a way that we can infer a value
2562 range for it, and we don't find a previous assertion for
2563 it, create a new assertion location node for OP. */
2564 if (infer_value_range (stmt, op, &comp_code, &value))
2566 register_new_assert_for (op, comp_code, value, bb, NULL, si);
2567 need_assert = true;
2571 /* Remember the last statement of the block. */
2572 last = stmt;
2575 /* If BB's last statement is a conditional expression
2576 involving integer operands, recurse into each of the sub-graphs
2577 rooted at BB to determine if we need to add ASSERT_EXPRs. */
2578 if (last
2579 && TREE_CODE (last) == COND_EXPR
2580 && !fp_predicate (COND_EXPR_COND (last))
2581 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
2582 need_assert |= find_conditional_asserts (bb);
2584 /* Recurse into the dominator children of BB. */
2585 for (son = first_dom_son (CDI_DOMINATORS, bb);
2586 son;
2587 son = next_dom_son (CDI_DOMINATORS, son))
2588 need_assert |= find_assert_locations (son);
2590 return need_assert;
2594 /* Create an ASSERT_EXPR for NAME and insert it in the location
2595 indicated by LOC. Return true if we made any edge insertions. */
2597 static bool
2598 process_assert_insertions_for (tree name, assert_locus_t loc)
2600 /* Build the comparison expression NAME_i COMP_CODE VAL. */
2601 tree stmt, cond, assert_expr;
2602 edge_iterator ei;
2603 edge e;
2605 cond = build (loc->comp_code, boolean_type_node, name, loc->val);
2606 assert_expr = build_assert_expr_for (cond, name);
2608 if (loc->e)
2610 /* We have been asked to insert the assertion on an edge. This
2611 is used only by COND_EXPR and SWITCH_EXPR assertions. */
2612 #if defined ENABLE_CHECKING
2613 gcc_assert (TREE_CODE (bsi_stmt (loc->si)) == COND_EXPR
2614 || TREE_CODE (bsi_stmt (loc->si)) == SWITCH_EXPR);
2615 #endif
2617 bsi_insert_on_edge (loc->e, assert_expr);
2618 return true;
2621 /* Otherwise, we can insert right after LOC->SI iff the
2622 statement must not be the last statement in the block. */
2623 stmt = bsi_stmt (loc->si);
2624 if (!stmt_ends_bb_p (stmt))
2626 bsi_insert_after (&loc->si, assert_expr, BSI_SAME_STMT);
2627 return false;
2630 /* If STMT must be the last statement in BB, we can only insert new
2631 assertions on the non-abnormal edge out of BB. Note that since
2632 STMT is not control flow, there may only be one non-abnormal edge
2633 out of BB. */
2634 FOR_EACH_EDGE (e, ei, loc->bb->succs)
2635 if (!(e->flags & EDGE_ABNORMAL))
2637 bsi_insert_on_edge (e, assert_expr);
2638 return true;
2641 gcc_unreachable ();
2645 /* Process all the insertions registered for every name N_i registered
2646 in NEED_ASSERT_FOR. The list of assertions to be inserted are
2647 found in ASSERTS_FOR[i]. */
2649 static void
2650 process_assert_insertions (void)
2652 unsigned i;
2653 bitmap_iterator bi;
2654 bool update_edges_p = false;
2655 int num_asserts = 0;
2657 if (dump_file && (dump_flags & TDF_DETAILS))
2658 dump_all_asserts (dump_file);
2660 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
2662 assert_locus_t loc = asserts_for[i];
2663 gcc_assert (loc);
2665 while (loc)
2667 assert_locus_t next = loc->next;
2668 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
2669 free (loc);
2670 loc = next;
2671 num_asserts++;
2675 if (update_edges_p)
2676 bsi_commit_edge_inserts ();
2678 if (dump_file && (dump_flags & TDF_STATS))
2679 fprintf (dump_file, "\nNumber of ASSERT_EXPR expressions inserted: %d\n\n",
2680 num_asserts);
2684 /* Traverse the flowgraph looking for conditional jumps to insert range
2685 expressions. These range expressions are meant to provide information
2686 to optimizations that need to reason in terms of value ranges. They
2687 will not be expanded into RTL. For instance, given:
2689 x = ...
2690 y = ...
2691 if (x < y)
2692 y = x - 2;
2693 else
2694 x = y + 3;
2696 this pass will transform the code into:
2698 x = ...
2699 y = ...
2700 if (x < y)
2702 x = ASSERT_EXPR <x, x < y>
2703 y = x - 2
2705 else
2707 y = ASSERT_EXPR <y, x <= y>
2708 x = y + 3
2711 The idea is that once copy and constant propagation have run, other
2712 optimizations will be able to determine what ranges of values can 'x'
2713 take in different paths of the code, simply by checking the reaching
2714 definition of 'x'. */
2716 static void
2717 insert_range_assertions (void)
2719 edge e;
2720 edge_iterator ei;
2721 bool update_ssa_p;
2723 found_in_subgraph = sbitmap_alloc (num_ssa_names);
2724 sbitmap_zero (found_in_subgraph);
2726 blocks_visited = sbitmap_alloc (last_basic_block);
2727 sbitmap_zero (blocks_visited);
2729 need_assert_for = BITMAP_ALLOC (NULL);
2730 asserts_for = xmalloc (num_ssa_names * sizeof (assert_locus_t));
2731 memset (asserts_for, 0, num_ssa_names * sizeof (assert_locus_t));
2733 calculate_dominance_info (CDI_DOMINATORS);
2735 update_ssa_p = false;
2736 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2737 if (find_assert_locations (e->dest))
2738 update_ssa_p = true;
2740 if (update_ssa_p)
2742 process_assert_insertions ();
2743 update_ssa (TODO_update_ssa_no_phi);
2746 if (dump_file && (dump_flags & TDF_DETAILS))
2748 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
2749 dump_function_to_file (current_function_decl, dump_file, dump_flags);
2752 sbitmap_free (found_in_subgraph);
2753 free (asserts_for);
2754 BITMAP_FREE (need_assert_for);
2758 /* Convert range assertion expressions into the implied copies and
2759 copy propagate away the copies. Doing the trivial copy propagation
2760 here avoids the need to run the full copy propagation pass after
2761 VRP.
2763 FIXME, this will eventually lead to copy propagation removing the
2764 names that had useful range information attached to them. For
2765 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
2766 then N_i will have the range [3, +INF].
2768 However, by converting the assertion into the implied copy
2769 operation N_i = N_j, we will then copy-propagate N_j into the uses
2770 of N_i and lose the range information. We may want to hold on to
2771 ASSERT_EXPRs a little while longer as the ranges could be used in
2772 things like jump threading.
2774 The problem with keeping ASSERT_EXPRs around is that passes after
2775 VRP need to handle them appropriately.
2777 Another approach would be to make the range information a first
2778 class property of the SSA_NAME so that it can be queried from
2779 any pass. This is made somewhat more complex by the need for
2780 multiple ranges to be associated with one SSA_NAME. */
2782 static void
2783 remove_range_assertions (void)
2785 basic_block bb;
2786 block_stmt_iterator si;
2788 /* Note that the BSI iterator bump happens at the bottom of the
2789 loop and no bump is necessary if we're removing the statement
2790 referenced by the current BSI. */
2791 FOR_EACH_BB (bb)
2792 for (si = bsi_start (bb); !bsi_end_p (si);)
2794 tree stmt = bsi_stmt (si);
2796 if (TREE_CODE (stmt) == MODIFY_EXPR
2797 && TREE_CODE (TREE_OPERAND (stmt, 1)) == ASSERT_EXPR)
2799 tree rhs = TREE_OPERAND (stmt, 1);
2800 tree cond = fold (ASSERT_EXPR_COND (rhs));
2801 use_operand_p use_p;
2802 imm_use_iterator iter;
2804 gcc_assert (cond != boolean_false_node);
2805 TREE_OPERAND (stmt, 1) = ASSERT_EXPR_VAR (rhs);
2806 update_stmt (stmt);
2808 /* The statement is now a copy. Propagate the RHS into
2809 every use of the LHS. */
2810 FOR_EACH_IMM_USE_SAFE (use_p, iter, TREE_OPERAND (stmt, 0))
2812 SET_USE (use_p, ASSERT_EXPR_VAR (rhs));
2813 update_stmt (USE_STMT (use_p));
2816 /* And finally, remove the copy, it is not needed. */
2817 bsi_remove (&si);
2819 else
2820 bsi_next (&si);
2823 sbitmap_free (blocks_visited);
2827 /* Return true if STMT is interesting for VRP. */
2829 static bool
2830 stmt_interesting_for_vrp (tree stmt)
2832 if (TREE_CODE (stmt) == PHI_NODE
2833 && is_gimple_reg (PHI_RESULT (stmt))
2834 && (INTEGRAL_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))
2835 || POINTER_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))))
2836 return true;
2837 else if (TREE_CODE (stmt) == MODIFY_EXPR)
2839 tree lhs = TREE_OPERAND (stmt, 0);
2841 if (TREE_CODE (lhs) == SSA_NAME
2842 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
2843 || POINTER_TYPE_P (TREE_TYPE (lhs)))
2844 && ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
2845 return true;
2847 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
2848 return true;
2850 return false;
2854 /* Initialize local data structures for VRP. */
2856 static void
2857 vrp_initialize (void)
2859 basic_block bb;
2861 vr_value = xmalloc (num_ssa_names * sizeof (value_range_t *));
2862 memset (vr_value, 0, num_ssa_names * sizeof (value_range_t *));
2864 FOR_EACH_BB (bb)
2866 block_stmt_iterator si;
2867 tree phi;
2869 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2871 if (!stmt_interesting_for_vrp (phi))
2873 tree lhs = PHI_RESULT (phi);
2874 set_value_range_to_varying (get_value_range (lhs));
2875 DONT_SIMULATE_AGAIN (phi) = true;
2877 else
2878 DONT_SIMULATE_AGAIN (phi) = false;
2881 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2883 tree stmt = bsi_stmt (si);
2885 if (!stmt_interesting_for_vrp (stmt))
2887 ssa_op_iter i;
2888 tree def;
2889 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
2890 set_value_range_to_varying (get_value_range (def));
2891 DONT_SIMULATE_AGAIN (stmt) = true;
2893 else
2895 DONT_SIMULATE_AGAIN (stmt) = false;
2902 /* Visit assignment STMT. If it produces an interesting range, record
2903 the SSA name in *OUTPUT_P. */
2905 static enum ssa_prop_result
2906 vrp_visit_assignment (tree stmt, tree *output_p)
2908 tree lhs, rhs, def;
2909 ssa_op_iter iter;
2911 lhs = TREE_OPERAND (stmt, 0);
2912 rhs = TREE_OPERAND (stmt, 1);
2914 /* We only keep track of ranges in integral and pointer types. */
2915 if (TREE_CODE (lhs) == SSA_NAME
2916 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
2917 || POINTER_TYPE_P (TREE_TYPE (lhs))))
2919 struct loop *l;
2920 value_range_t new_vr = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
2922 extract_range_from_expr (&new_vr, rhs);
2924 /* If STMT is inside a loop, we may be able to know something
2925 else about the range of LHS by examining scalar evolution
2926 information. */
2927 if (cfg_loops && (l = loop_containing_stmt (stmt)))
2928 adjust_range_with_scev (&new_vr, l, stmt, lhs);
2930 if (update_value_range (lhs, &new_vr))
2932 *output_p = lhs;
2934 if (dump_file && (dump_flags & TDF_DETAILS))
2936 fprintf (dump_file, "Found new range for ");
2937 print_generic_expr (dump_file, lhs, 0);
2938 fprintf (dump_file, ": ");
2939 dump_value_range (dump_file, &new_vr);
2940 fprintf (dump_file, "\n\n");
2943 if (new_vr.type == VR_VARYING)
2944 return SSA_PROP_VARYING;
2946 return SSA_PROP_INTERESTING;
2949 return SSA_PROP_NOT_INTERESTING;
2952 /* Every other statement produces no useful ranges. */
2953 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
2954 set_value_range_to_varying (get_value_range (def));
2956 return SSA_PROP_VARYING;
2960 /* Compare all the value ranges for names equivalent to VAR with VAL
2961 using comparison code COMP. Return the same value returned by
2962 compare_range_with_value. */
2964 static tree
2965 compare_name_with_value (enum tree_code comp, tree var, tree val)
2967 bitmap_iterator bi;
2968 unsigned i;
2969 bitmap e;
2970 tree retval, t;
2972 t = retval = NULL_TREE;
2974 /* Get the set of equivalences for VAR. */
2975 e = get_value_range (var)->equiv;
2977 /* Add VAR to its own set of equivalences so that VAR's value range
2978 is processed by this loop (otherwise, we would have to replicate
2979 the body of the loop just to check VAR's value range). */
2980 bitmap_set_bit (e, SSA_NAME_VERSION (var));
2982 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
2984 value_range_t equiv_vr = *(vr_value[i]);
2986 /* If name N_i does not have a valid range, use N_i as its own
2987 range. This allows us to compare against names that may
2988 have N_i in their ranges. */
2989 if (equiv_vr.type == VR_VARYING || equiv_vr.type == VR_UNDEFINED)
2991 equiv_vr.type = VR_RANGE;
2992 equiv_vr.min = ssa_name (i);
2993 equiv_vr.max = ssa_name (i);
2996 t = compare_range_with_value (comp, &equiv_vr, val);
2997 if (t)
2999 /* All the ranges should compare the same against VAL. */
3000 gcc_assert (retval == NULL || t == retval);
3001 retval = t;
3005 /* Remove VAR from its own equivalence set. */
3006 bitmap_clear_bit (e, SSA_NAME_VERSION (var));
3008 if (retval)
3009 return retval;
3011 /* We couldn't find a non-NULL value for the predicate. */
3012 return NULL_TREE;
3016 /* Given a comparison code COMP and names N1 and N2, compare all the
3017 ranges equivalent to N1 against all the ranges equivalent to N2
3018 to determine the value of N1 COMP N2. Return the same value
3019 returned by compare_ranges. */
3021 static tree
3022 compare_names (enum tree_code comp, tree n1, tree n2)
3024 tree t, retval;
3025 bitmap e1, e2;
3026 bitmap_iterator bi1, bi2;
3027 unsigned i1, i2;
3029 /* Compare the ranges of every name equivalent to N1 against the
3030 ranges of every name equivalent to N2. */
3031 e1 = get_value_range (n1)->equiv;
3032 e2 = get_value_range (n2)->equiv;
3034 /* Add N1 and N2 to their own set of equivalences to avoid
3035 duplicating the body of the loop just to check N1 and N2
3036 ranges. */
3037 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
3038 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
3040 /* If the equivalence sets have a common intersection, then the two
3041 names can be compared without checking their ranges. */
3042 if (bitmap_intersect_p (e1, e2))
3044 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
3045 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
3047 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
3048 ? boolean_true_node
3049 : boolean_false_node;
3052 /* Otherwise, compare all the equivalent ranges. First, add N1 and
3053 N2 to their own set of equivalences to avoid duplicating the body
3054 of the loop just to check N1 and N2 ranges. */
3055 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
3057 value_range_t vr1 = *(vr_value[i1]);
3059 /* If the range is VARYING or UNDEFINED, use the name itself. */
3060 if (vr1.type == VR_VARYING || vr1.type == VR_UNDEFINED)
3062 vr1.type = VR_RANGE;
3063 vr1.min = ssa_name (i1);
3064 vr1.max = ssa_name (i1);
3067 t = retval = NULL_TREE;
3068 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
3070 value_range_t vr2 = *(vr_value[i2]);
3072 if (vr2.type == VR_VARYING || vr2.type == VR_UNDEFINED)
3074 vr2.type = VR_RANGE;
3075 vr2.min = ssa_name (i2);
3076 vr2.max = ssa_name (i2);
3079 t = compare_ranges (comp, &vr1, &vr2);
3080 if (t)
3082 /* All the ranges in the equivalent sets should compare
3083 the same. */
3084 gcc_assert (retval == NULL || t == retval);
3085 retval = t;
3089 if (retval)
3091 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
3092 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
3093 return retval;
3097 /* None of the equivalent ranges are useful in computing this
3098 comparison. */
3099 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
3100 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
3101 return NULL_TREE;
3105 /* Given a conditional predicate COND, try to determine if COND yields
3106 true or false based on the value ranges of its operands. Return
3107 BOOLEAN_TRUE_NODE if the conditional always evaluates to true,
3108 BOOLEAN_FALSE_NODE if the conditional always evaluates to false, and,
3109 NULL if the conditional cannot be evaluated at compile time.
3111 If USE_EQUIV_P is true, the ranges of all the names equivalent with
3112 the operands in COND are used when trying to compute its value.
3113 This is only used during final substitution. During propagation,
3114 we only check the range of each variable and not its equivalents. */
3116 tree
3117 vrp_evaluate_conditional (tree cond, bool use_equiv_p)
3119 gcc_assert (TREE_CODE (cond) == SSA_NAME
3120 || TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison);
3122 if (TREE_CODE (cond) == SSA_NAME)
3124 value_range_t *vr;
3125 tree retval;
3127 if (use_equiv_p)
3128 retval = compare_name_with_value (NE_EXPR, cond, boolean_false_node);
3129 else
3131 value_range_t *vr = get_value_range (cond);
3132 retval = compare_range_with_value (NE_EXPR, vr, boolean_false_node);
3135 /* If COND has a known boolean range, return it. */
3136 if (retval)
3137 return retval;
3139 /* Otherwise, if COND has a symbolic range of exactly one value,
3140 return it. */
3141 vr = get_value_range (cond);
3142 if (vr->type == VR_RANGE && vr->min == vr->max)
3143 return vr->min;
3145 else
3147 tree op0 = TREE_OPERAND (cond, 0);
3148 tree op1 = TREE_OPERAND (cond, 1);
3150 /* We only deal with integral and pointer types. */
3151 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
3152 && !POINTER_TYPE_P (TREE_TYPE (op0)))
3153 return NULL_TREE;
3155 if (use_equiv_p)
3157 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
3158 return compare_names (TREE_CODE (cond), op0, op1);
3159 else if (TREE_CODE (op0) == SSA_NAME)
3160 return compare_name_with_value (TREE_CODE (cond), op0, op1);
3161 else if (TREE_CODE (op1) == SSA_NAME)
3162 return compare_name_with_value (
3163 swap_tree_comparison (TREE_CODE (cond)), op1, op0);
3165 else
3167 value_range_t *vr0, *vr1;
3169 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
3170 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
3172 if (vr0 && vr1)
3173 return compare_ranges (TREE_CODE (cond), vr0, vr1);
3174 else if (vr0 && vr1 == NULL)
3175 return compare_range_with_value (TREE_CODE (cond), vr0, op1);
3176 else if (vr0 == NULL && vr1)
3177 return compare_range_with_value (
3178 swap_tree_comparison (TREE_CODE (cond)), vr1, op0);
3182 /* Anything else cannot be computed statically. */
3183 return NULL_TREE;
3187 /* Visit conditional statement STMT. If we can determine which edge
3188 will be taken out of STMT's basic block, record it in
3189 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
3190 SSA_PROP_VARYING. */
3192 static enum ssa_prop_result
3193 vrp_visit_cond_stmt (tree stmt, edge *taken_edge_p)
3195 tree cond, val;
3197 *taken_edge_p = NULL;
3199 /* FIXME. Handle SWITCH_EXPRs. But first, the assert pass needs to
3200 add ASSERT_EXPRs for them. */
3201 if (TREE_CODE (stmt) == SWITCH_EXPR)
3202 return SSA_PROP_VARYING;
3204 cond = COND_EXPR_COND (stmt);
3206 if (dump_file && (dump_flags & TDF_DETAILS))
3208 tree use;
3209 ssa_op_iter i;
3211 fprintf (dump_file, "\nVisiting conditional with predicate: ");
3212 print_generic_expr (dump_file, cond, 0);
3213 fprintf (dump_file, "\nWith known ranges\n");
3215 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
3217 fprintf (dump_file, "\t");
3218 print_generic_expr (dump_file, use, 0);
3219 fprintf (dump_file, ": ");
3220 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
3223 fprintf (dump_file, "\n");
3226 /* Compute the value of the predicate COND by checking the known
3227 ranges of each of its operands.
3229 Note that we cannot evaluate all the equivalent ranges here
3230 because those ranges may not yet be final and with the current
3231 propagation strategy, we cannot determine when the value ranges
3232 of the names in the equivalence set have changed.
3234 For instance, given the following code fragment
3236 i_5 = PHI <8, i_13>
3238 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
3239 if (i_14 == 1)
3242 Assume that on the first visit to i_14, i_5 has the temporary
3243 range [8, 8] because the second argument to the PHI function is
3244 not yet executable. We derive the range ~[0, 0] for i_14 and the
3245 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
3246 the first time, since i_14 is equivalent to the range [8, 8], we
3247 determine that the predicate is always false.
3249 On the next round of propagation, i_13 is determined to be
3250 VARYING, which causes i_5 to drop down to VARYING. So, another
3251 visit to i_14 is scheduled. In this second visit, we compute the
3252 exact same range and equivalence set for i_14, namely ~[0, 0] and
3253 { i_5 }. But we did not have the previous range for i_5
3254 registered, so vrp_visit_assignment thinks that the range for
3255 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
3256 is not visited again, which stops propagation from visiting
3257 statements in the THEN clause of that if().
3259 To properly fix this we would need to keep the previous range
3260 value for the names in the equivalence set. This way we would've
3261 discovered that from one visit to the other i_5 changed from
3262 range [8, 8] to VR_VARYING.
3264 However, fixing this apparent limitation may not be worth the
3265 additional checking. Testing on several code bases (GCC, DLV,
3266 MICO, TRAMP3D and SPEC2000) showed that doing this results in
3267 4 more predicates folded in SPEC. */
3268 val = vrp_evaluate_conditional (cond, false);
3269 if (val)
3270 *taken_edge_p = find_taken_edge (bb_for_stmt (stmt), val);
3272 if (dump_file && (dump_flags & TDF_DETAILS))
3274 fprintf (dump_file, "\nPredicate evaluates to: ");
3275 if (val == NULL_TREE)
3276 fprintf (dump_file, "DON'T KNOW\n");
3277 else
3278 print_generic_stmt (dump_file, val, 0);
3281 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
3285 /* Evaluate statement STMT. If the statement produces a useful range,
3286 return SSA_PROP_INTERESTING and record the SSA name with the
3287 interesting range into *OUTPUT_P.
3289 If STMT is a conditional branch and we can determine its truth
3290 value, the taken edge is recorded in *TAKEN_EDGE_P.
3292 If STMT produces a varying value, return SSA_PROP_VARYING. */
3294 static enum ssa_prop_result
3295 vrp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
3297 tree def;
3298 ssa_op_iter iter;
3299 stmt_ann_t ann;
3301 if (dump_file && (dump_flags & TDF_DETAILS))
3303 fprintf (dump_file, "\nVisiting statement:\n");
3304 print_generic_stmt (dump_file, stmt, dump_flags);
3305 fprintf (dump_file, "\n");
3308 ann = stmt_ann (stmt);
3309 if (TREE_CODE (stmt) == MODIFY_EXPR
3310 && ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
3311 return vrp_visit_assignment (stmt, output_p);
3312 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
3313 return vrp_visit_cond_stmt (stmt, taken_edge_p);
3315 /* All other statements produce nothing of interest for VRP, so mark
3316 their outputs varying and prevent further simulation. */
3317 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
3318 set_value_range_to_varying (get_value_range (def));
3320 return SSA_PROP_VARYING;
3324 /* Meet operation for value ranges. Given two value ranges VR0 and
3325 VR1, store in VR0 the result of meeting VR0 and VR1.
3327 The meeting rules are as follows:
3329 1- If VR0 and VR1 have an empty intersection, set VR0 to VR_VARYING.
3331 2- If VR0 and VR1 have a non-empty intersection, set VR0 to the
3332 union of VR0 and VR1. */
3334 static void
3335 vrp_meet (value_range_t *vr0, value_range_t *vr1)
3337 if (vr0->type == VR_UNDEFINED)
3339 copy_value_range (vr0, vr1);
3340 return;
3343 if (vr1->type == VR_UNDEFINED)
3345 /* Nothing to do. VR0 already has the resulting range. */
3346 return;
3349 if (vr0->type == VR_VARYING)
3351 /* Nothing to do. VR0 already has the resulting range. */
3352 return;
3355 if (vr1->type == VR_VARYING)
3357 set_value_range_to_varying (vr0);
3358 return;
3361 if (vr0->type == VR_RANGE && vr1->type == VR_RANGE)
3363 /* If VR0 and VR1 have a non-empty intersection, compute the
3364 union of both ranges. */
3365 if (value_ranges_intersect_p (vr0, vr1))
3367 int cmp;
3368 tree min, max;
3370 /* The lower limit of the new range is the minimum of the
3371 two ranges. If they cannot be compared, the result is
3372 VARYING. */
3373 cmp = compare_values (vr0->min, vr1->min);
3374 if (cmp == 0 || cmp == 1)
3375 min = vr1->min;
3376 else if (cmp == -1)
3377 min = vr0->min;
3378 else
3380 set_value_range_to_varying (vr0);
3381 return;
3384 /* Similarly, the upper limit of the new range is the
3385 maximum of the two ranges. If they cannot be compared,
3386 the result is VARYING. */
3387 cmp = compare_values (vr0->max, vr1->max);
3388 if (cmp == 0 || cmp == -1)
3389 max = vr1->max;
3390 else if (cmp == 1)
3391 max = vr0->max;
3392 else
3394 set_value_range_to_varying (vr0);
3395 return;
3398 /* The resulting set of equivalences is the intersection of
3399 the two sets. */
3400 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
3401 bitmap_and_into (vr0->equiv, vr1->equiv);
3402 else if (vr0->equiv && !vr1->equiv)
3403 bitmap_clear (vr0->equiv);
3405 set_value_range (vr0, vr0->type, min, max, vr0->equiv);
3407 else
3408 goto no_meet;
3410 else if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
3412 /* Two anti-ranges meet only if they are both identical. */
3413 if (compare_values (vr0->min, vr1->min) == 0
3414 && compare_values (vr0->max, vr1->max) == 0
3415 && compare_values (vr0->min, vr0->max) == 0)
3417 /* The resulting set of equivalences is the intersection of
3418 the two sets. */
3419 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
3420 bitmap_and_into (vr0->equiv, vr1->equiv);
3421 else if (vr0->equiv && !vr1->equiv)
3422 bitmap_clear (vr0->equiv);
3424 else
3425 goto no_meet;
3427 else if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
3429 /* A numeric range [VAL1, VAL2] and an anti-range ~[VAL3, VAL4]
3430 meet only if the ranges have an empty intersection. The
3431 result of the meet operation is the anti-range. */
3432 if (!symbolic_range_p (vr0)
3433 && !symbolic_range_p (vr1)
3434 && !value_ranges_intersect_p (vr0, vr1))
3436 if (vr1->type == VR_ANTI_RANGE)
3437 copy_value_range (vr0, vr1);
3439 /* The resulting set of equivalences is the intersection of
3440 the two sets. */
3441 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
3442 bitmap_and_into (vr0->equiv, vr1->equiv);
3443 else if (vr0->equiv && !vr1->equiv)
3444 bitmap_clear (vr0->equiv);
3446 else
3447 goto no_meet;
3449 else
3450 gcc_unreachable ();
3452 return;
3454 no_meet:
3455 /* The two range VR0 and VR1 do not meet. Before giving up and
3456 setting the result to VARYING, see if we can at least derive a
3457 useful anti-range. */
3458 if (!symbolic_range_p (vr0)
3459 && !range_includes_zero_p (vr0)
3460 && !symbolic_range_p (vr1)
3461 && !range_includes_zero_p (vr1))
3463 set_value_range_to_nonnull (vr0, TREE_TYPE (vr0->min));
3465 /* Since this meet operation did not result from the meeting of
3466 two equivalent names, VR0 cannot have any equivalences. */
3467 if (vr0->equiv)
3468 bitmap_clear (vr0->equiv);
3470 else
3471 set_value_range_to_varying (vr0);
3475 /* Visit all arguments for PHI node PHI that flow through executable
3476 edges. If a valid value range can be derived from all the incoming
3477 value ranges, set a new range for the LHS of PHI. */
3479 static enum ssa_prop_result
3480 vrp_visit_phi_node (tree phi)
3482 int i;
3483 tree lhs = PHI_RESULT (phi);
3484 value_range_t *lhs_vr = get_value_range (lhs);
3485 value_range_t vr_result = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
3487 copy_value_range (&vr_result, lhs_vr);
3489 if (dump_file && (dump_flags & TDF_DETAILS))
3491 fprintf (dump_file, "\nVisiting PHI node: ");
3492 print_generic_expr (dump_file, phi, dump_flags);
3495 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
3497 edge e = PHI_ARG_EDGE (phi, i);
3499 if (dump_file && (dump_flags & TDF_DETAILS))
3501 fprintf (dump_file,
3502 "\n Argument #%d (%d -> %d %sexecutable)\n",
3503 i, e->src->index, e->dest->index,
3504 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
3507 if (e->flags & EDGE_EXECUTABLE)
3509 tree arg = PHI_ARG_DEF (phi, i);
3510 value_range_t vr_arg;
3512 if (TREE_CODE (arg) == SSA_NAME)
3513 vr_arg = *(get_value_range (arg));
3514 else
3516 vr_arg.type = VR_RANGE;
3517 vr_arg.min = arg;
3518 vr_arg.max = arg;
3519 vr_arg.equiv = NULL;
3522 if (dump_file && (dump_flags & TDF_DETAILS))
3524 fprintf (dump_file, "\t");
3525 print_generic_expr (dump_file, arg, dump_flags);
3526 fprintf (dump_file, "\n\tValue: ");
3527 dump_value_range (dump_file, &vr_arg);
3528 fprintf (dump_file, "\n");
3531 vrp_meet (&vr_result, &vr_arg);
3533 if (vr_result.type == VR_VARYING)
3534 break;
3538 if (vr_result.type == VR_VARYING)
3539 goto varying;
3541 /* To prevent infinite iterations in the algorithm, derive ranges
3542 when the new value is slightly bigger or smaller than the
3543 previous one. */
3544 if (lhs_vr->type == VR_RANGE)
3546 if (!POINTER_TYPE_P (TREE_TYPE (lhs)))
3548 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
3549 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
3551 /* If the new minimum is smaller or larger than the previous
3552 one, go all the way to -INF. In the first case, to avoid
3553 iterating millions of times to reach -INF, and in the
3554 other case to avoid infinite bouncing between different
3555 minimums. */
3556 if (cmp_min > 0 || cmp_min < 0)
3557 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
3559 /* Similarly, if the new maximum is smaller or larger than
3560 the previous one, go all the way to +INF. */
3561 if (cmp_max < 0 || cmp_max > 0)
3562 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
3564 /* If we ended up with a (-INF, +INF) range, set it to
3565 VARYING. */
3566 if (vr_result.min == TYPE_MIN_VALUE (TREE_TYPE (vr_result.min))
3567 && vr_result.max == TYPE_MAX_VALUE (TREE_TYPE (vr_result.max)))
3568 goto varying;
3572 /* If the new range is different than the previous value, keep
3573 iterating. */
3574 if (update_value_range (lhs, &vr_result))
3575 return SSA_PROP_INTERESTING;
3577 /* Nothing changed, don't add outgoing edges. */
3578 return SSA_PROP_NOT_INTERESTING;
3580 /* No match found. Set the LHS to VARYING. */
3581 varying:
3582 set_value_range_to_varying (lhs_vr);
3583 return SSA_PROP_VARYING;
3586 /* Simplify a division or modulo operator to a right shift or
3587 bitwise and if the first operand is unsigned or is greater
3588 than zero and the second operand is an exact power of two. */
3590 static void
3591 simplify_div_or_mod_using_ranges (tree stmt, tree rhs, enum tree_code rhs_code)
3593 tree val = NULL;
3594 tree op = TREE_OPERAND (rhs, 0);
3595 value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
3597 if (TYPE_UNSIGNED (TREE_TYPE (op)))
3599 val = integer_one_node;
3601 else
3603 val = compare_range_with_value (GT_EXPR, vr, integer_zero_node);
3606 if (val && integer_onep (val))
3608 tree t;
3609 tree op0 = TREE_OPERAND (rhs, 0);
3610 tree op1 = TREE_OPERAND (rhs, 1);
3612 if (rhs_code == TRUNC_DIV_EXPR)
3614 t = build_int_cst (NULL_TREE, tree_log2 (op1));
3615 t = build2 (RSHIFT_EXPR, TREE_TYPE (op0), op0, t);
3617 else
3619 t = build_int_cst (TREE_TYPE (op1), 1);
3620 t = int_const_binop (MINUS_EXPR, op1, t, 0);
3621 t = fold_convert (TREE_TYPE (op0), t);
3622 t = build2 (BIT_AND_EXPR, TREE_TYPE (op0), op0, t);
3625 TREE_OPERAND (stmt, 1) = t;
3626 update_stmt (stmt);
3630 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
3631 ABS_EXPR. If the operand is <= 0, then simplify the
3632 ABS_EXPR into a NEGATE_EXPR. */
3634 static void
3635 simplify_abs_using_ranges (tree stmt, tree rhs)
3637 tree val = NULL;
3638 tree op = TREE_OPERAND (rhs, 0);
3639 tree type = TREE_TYPE (op);
3640 value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
3642 if (TYPE_UNSIGNED (type))
3644 val = integer_zero_node;
3646 else if (vr)
3648 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node);
3649 if (!val)
3651 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node);
3653 if (val)
3655 if (integer_zerop (val))
3656 val = integer_one_node;
3657 else if (integer_onep (val))
3658 val = integer_zero_node;
3662 if (val
3663 && (integer_onep (val) || integer_zerop (val)))
3665 tree t;
3667 if (integer_onep (val))
3668 t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
3669 else
3670 t = op;
3672 TREE_OPERAND (stmt, 1) = t;
3673 update_stmt (stmt);
3678 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
3679 a known value range VR.
3681 If there is one and only one value which will satisfy the
3682 conditional, then return that value. Else return NULL. */
3684 static tree
3685 test_for_singularity (enum tree_code cond_code, tree op0,
3686 tree op1, value_range_t *vr)
3688 tree min = NULL;
3689 tree max = NULL;
3691 /* Extract minimum/maximum values which satisfy the
3692 the conditional as it was written. */
3693 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
3695 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
3697 max = op1;
3698 if (cond_code == LT_EXPR)
3700 tree one = build_int_cst (TREE_TYPE (op0), 1);
3701 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
3704 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
3706 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
3708 min = op1;
3709 if (cond_code == GT_EXPR)
3711 tree one = build_int_cst (TREE_TYPE (op0), 1);
3712 max = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), max, one);
3716 /* Now refine the minimum and maximum values using any
3717 value range information we have for op0. */
3718 if (min && max)
3720 if (compare_values (vr->min, min) == -1)
3721 min = min;
3722 else
3723 min = vr->min;
3724 if (compare_values (vr->max, max) == 1)
3725 max = max;
3726 else
3727 max = vr->max;
3729 /* If the new min/max values have converged to a
3730 single value, then there is only one value which
3731 can satisfy the condition, return that value. */
3732 if (min == max && is_gimple_min_invariant (min))
3733 return min;
3735 return NULL;
3738 /* Simplify a conditional using a relational operator to an equality
3739 test if the range information indicates only one value can satisfy
3740 the original conditional. */
3742 static void
3743 simplify_cond_using_ranges (tree stmt)
3745 tree cond = COND_EXPR_COND (stmt);
3746 tree op0 = TREE_OPERAND (cond, 0);
3747 tree op1 = TREE_OPERAND (cond, 1);
3748 enum tree_code cond_code = TREE_CODE (cond);
3750 if (cond_code != NE_EXPR
3751 && cond_code != EQ_EXPR
3752 && TREE_CODE (op0) == SSA_NAME
3753 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
3754 && is_gimple_min_invariant (op1))
3756 value_range_t *vr = get_value_range (op0);
3758 /* If we have range information for OP0, then we might be
3759 able to simplify this conditional. */
3760 if (vr->type == VR_RANGE)
3762 tree new = test_for_singularity (cond_code, op0, op1, vr);
3764 if (new)
3766 if (dump_file)
3768 fprintf (dump_file, "Simplified relational ");
3769 print_generic_expr (dump_file, cond, 0);
3770 fprintf (dump_file, " into ");
3773 COND_EXPR_COND (stmt)
3774 = build (EQ_EXPR, boolean_type_node, op0, new);
3775 update_stmt (stmt);
3777 if (dump_file)
3779 print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
3780 fprintf (dump_file, "\n");
3782 return;
3786 /* Try again after inverting the condition. We only deal
3787 with integral types here, so no need to worry about
3788 issues with inverting FP comparisons. */
3789 cond_code = invert_tree_comparison (cond_code, false);
3790 new = test_for_singularity (cond_code, op0, op1, vr);
3792 if (new)
3794 if (dump_file)
3796 fprintf (dump_file, "Simplified relational ");
3797 print_generic_expr (dump_file, cond, 0);
3798 fprintf (dump_file, " into ");
3801 COND_EXPR_COND (stmt)
3802 = build (NE_EXPR, boolean_type_node, op0, new);
3803 update_stmt (stmt);
3805 if (dump_file)
3807 print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
3808 fprintf (dump_file, "\n");
3810 return;
3817 /* Simplify STMT using ranges if possible. */
3819 void
3820 simplify_stmt_using_ranges (tree stmt)
3822 if (TREE_CODE (stmt) == MODIFY_EXPR)
3824 tree rhs = TREE_OPERAND (stmt, 1);
3825 enum tree_code rhs_code = TREE_CODE (rhs);
3827 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
3828 and BIT_AND_EXPR respectively if the first operand is greater
3829 than zero and the second operand is an exact power of two. */
3830 if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
3831 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
3832 && integer_pow2p (TREE_OPERAND (rhs, 1)))
3833 simplify_div_or_mod_using_ranges (stmt, rhs, rhs_code);
3835 /* Transform ABS (X) into X or -X as appropriate. */
3836 if (rhs_code == ABS_EXPR
3837 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
3838 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
3839 simplify_abs_using_ranges (stmt, rhs);
3841 else if (TREE_CODE (stmt) == COND_EXPR
3842 && COMPARISON_CLASS_P (COND_EXPR_COND (stmt)))
3844 simplify_cond_using_ranges (stmt);
3850 /* Traverse all the blocks folding conditionals with known ranges. */
3852 static void
3853 vrp_finalize (void)
3855 size_t i;
3856 prop_value_t *single_val_range;
3857 bool do_value_subst_p;
3859 if (dump_file)
3861 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
3862 dump_all_value_ranges (dump_file);
3863 fprintf (dump_file, "\n");
3866 /* We may have ended with ranges that have exactly one value. Those
3867 values can be substituted as any other copy/const propagated
3868 value using substitute_and_fold. */
3869 single_val_range = xmalloc (num_ssa_names * sizeof (*single_val_range));
3870 memset (single_val_range, 0, num_ssa_names * sizeof (*single_val_range));
3872 do_value_subst_p = false;
3873 for (i = 0; i < num_ssa_names; i++)
3874 if (vr_value[i]
3875 && vr_value[i]->type == VR_RANGE
3876 && vr_value[i]->min == vr_value[i]->max)
3878 single_val_range[i].value = vr_value[i]->min;
3879 do_value_subst_p = true;
3882 if (!do_value_subst_p)
3884 /* We found no single-valued ranges, don't waste time trying to
3885 do single value substitution in substitute_and_fold. */
3886 free (single_val_range);
3887 single_val_range = NULL;
3890 substitute_and_fold (single_val_range, true);
3892 /* Free allocated memory. */
3893 for (i = 0; i < num_ssa_names; i++)
3894 if (vr_value[i])
3896 BITMAP_FREE (vr_value[i]->equiv);
3897 free (vr_value[i]);
3900 free (single_val_range);
3901 free (vr_value);
3905 /* Main entry point to VRP (Value Range Propagation). This pass is
3906 loosely based on J. R. C. Patterson, ``Accurate Static Branch
3907 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
3908 Programming Language Design and Implementation, pp. 67-78, 1995.
3909 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
3911 This is essentially an SSA-CCP pass modified to deal with ranges
3912 instead of constants.
3914 While propagating ranges, we may find that two or more SSA name
3915 have equivalent, though distinct ranges. For instance,
3917 1 x_9 = p_3->a;
3918 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
3919 3 if (p_4 == q_2)
3920 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
3921 5 endif
3922 6 if (q_2)
3924 In the code above, pointer p_5 has range [q_2, q_2], but from the
3925 code we can also determine that p_5 cannot be NULL and, if q_2 had
3926 a non-varying range, p_5's range should also be compatible with it.
3928 These equivalences are created by two expressions: ASSERT_EXPR and
3929 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
3930 result of another assertion, then we can use the fact that p_5 and
3931 p_4 are equivalent when evaluating p_5's range.
3933 Together with value ranges, we also propagate these equivalences
3934 between names so that we can take advantage of information from
3935 multiple ranges when doing final replacement. Note that this
3936 equivalency relation is transitive but not symmetric.
3938 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
3939 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
3940 in contexts where that assertion does not hold (e.g., in line 6).
3942 TODO, the main difference between this pass and Patterson's is that
3943 we do not propagate edge probabilities. We only compute whether
3944 edges can be taken or not. That is, instead of having a spectrum
3945 of jump probabilities between 0 and 1, we only deal with 0, 1 and
3946 DON'T KNOW. In the future, it may be worthwhile to propagate
3947 probabilities to aid branch prediction. */
3949 static void
3950 execute_vrp (void)
3952 insert_range_assertions ();
3954 cfg_loops = loop_optimizer_init (NULL);
3955 if (cfg_loops)
3956 scev_initialize (cfg_loops);
3958 vrp_initialize ();
3959 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
3960 vrp_finalize ();
3962 if (cfg_loops)
3964 scev_finalize ();
3965 loop_optimizer_finalize (cfg_loops, NULL);
3966 current_loops = NULL;
3969 remove_range_assertions ();
3972 static bool
3973 gate_vrp (void)
3975 return flag_tree_vrp != 0;
3978 struct tree_opt_pass pass_vrp =
3980 "vrp", /* name */
3981 gate_vrp, /* gate */
3982 execute_vrp, /* execute */
3983 NULL, /* sub */
3984 NULL, /* next */
3985 0, /* static_pass_number */
3986 TV_TREE_VRP, /* tv_id */
3987 PROP_ssa | PROP_alias, /* properties_required */
3988 0, /* properties_provided */
3989 0, /* properties_destroyed */
3990 0, /* todo_flags_start */
3991 TODO_cleanup_cfg
3992 | TODO_ggc_collect
3993 | TODO_verify_ssa
3994 | TODO_dump_func
3995 | TODO_update_ssa, /* todo_flags_finish */
3996 0 /* letter */