2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / tree-vrp.c
blobb3223a5e59bcdbe54cf18228173a5580ab538710
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "flags.h"
28 #include "tree.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
33 #include "timevar.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "cfgloop.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-ssa-propagate.h"
39 #include "tree-chrec.h"
41 /* Set of SSA names found during the dominator traversal of a
42 sub-graph in find_assert_locations. */
43 static sbitmap found_in_subgraph;
45 /* Local functions. */
46 static int compare_values (tree val1, tree val2);
47 static void vrp_meet (value_range_t *, value_range_t *);
49 /* Location information for ASSERT_EXPRs. Each instance of this
50 structure describes an ASSERT_EXPR for an SSA name. Since a single
51 SSA name may have more than one assertion associated with it, these
52 locations are kept in a linked list attached to the corresponding
53 SSA name. */
54 struct assert_locus_d
56 /* Basic block where the assertion would be inserted. */
57 basic_block bb;
59 /* Some assertions need to be inserted on an edge (e.g., assertions
60 generated by COND_EXPRs). In those cases, BB will be NULL. */
61 edge e;
63 /* Pointer to the statement that generated this assertion. */
64 block_stmt_iterator si;
66 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
67 enum tree_code comp_code;
69 /* Value being compared against. */
70 tree val;
72 /* Next node in the linked list. */
73 struct assert_locus_d *next;
76 typedef struct assert_locus_d *assert_locus_t;
78 /* If bit I is present, it means that SSA name N_i has a list of
79 assertions that should be inserted in the IL. */
80 static bitmap need_assert_for;
82 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
83 holds a list of ASSERT_LOCUS_T nodes that describe where
84 ASSERT_EXPRs for SSA name N_I should be inserted. */
85 static assert_locus_t *asserts_for;
87 /* Set of blocks visited in find_assert_locations. Used to avoid
88 visiting the same block more than once. */
89 static sbitmap blocks_visited;
91 /* Value range array. After propagation, VR_VALUE[I] holds the range
92 of values that SSA name N_I may take. */
93 static value_range_t **vr_value;
96 /* Return true if ARG is marked with the nonnull attribute in the
97 current function signature. */
99 static bool
100 nonnull_arg_p (tree arg)
102 tree t, attrs, fntype;
103 unsigned HOST_WIDE_INT arg_num;
105 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
107 /* The static chain decl is always non null. */
108 if (arg == cfun->static_chain_decl)
109 return true;
111 fntype = TREE_TYPE (current_function_decl);
112 attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype));
114 /* If "nonnull" wasn't specified, we know nothing about the argument. */
115 if (attrs == NULL_TREE)
116 return false;
118 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
119 if (TREE_VALUE (attrs) == NULL_TREE)
120 return true;
122 /* Get the position number for ARG in the function signature. */
123 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
125 t = TREE_CHAIN (t), arg_num++)
127 if (t == arg)
128 break;
131 gcc_assert (t == arg);
133 /* Now see if ARG_NUM is mentioned in the nonnull list. */
134 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
136 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
137 return true;
140 return false;
144 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
146 static void
147 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
148 tree max, bitmap equiv)
150 #if defined ENABLE_CHECKING
151 /* Check the validity of the range. */
152 if (t == VR_RANGE || t == VR_ANTI_RANGE)
154 int cmp;
156 gcc_assert (min && max);
158 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
159 gcc_assert (min != TYPE_MIN_VALUE (TREE_TYPE (min))
160 || max != TYPE_MAX_VALUE (TREE_TYPE (max)));
162 cmp = compare_values (min, max);
163 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
166 if (t == VR_UNDEFINED || t == VR_VARYING)
167 gcc_assert (min == NULL_TREE && max == NULL_TREE);
169 if (t == VR_UNDEFINED || t == VR_VARYING)
170 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
171 #endif
173 vr->type = t;
174 vr->min = min;
175 vr->max = max;
177 /* Since updating the equivalence set involves deep copying the
178 bitmaps, only do it if absolutely necessary. */
179 if (vr->equiv == NULL)
180 vr->equiv = BITMAP_ALLOC (NULL);
182 if (equiv != vr->equiv)
184 if (equiv && !bitmap_empty_p (equiv))
185 bitmap_copy (vr->equiv, equiv);
186 else
187 bitmap_clear (vr->equiv);
192 /* Copy value range FROM into value range TO. */
194 static inline void
195 copy_value_range (value_range_t *to, value_range_t *from)
197 set_value_range (to, from->type, from->min, from->max, from->equiv);
200 /* Set value range VR to a non-negative range of type TYPE. */
202 static inline void
203 set_value_range_to_nonnegative (value_range_t *vr, tree type)
205 tree zero = build_int_cst (type, 0);
206 set_value_range (vr, VR_RANGE, zero, TYPE_MAX_VALUE (type), vr->equiv);
209 /* Set value range VR to a non-NULL range of type TYPE. */
211 static inline void
212 set_value_range_to_nonnull (value_range_t *vr, tree type)
214 tree zero = build_int_cst (type, 0);
215 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
219 /* Set value range VR to a NULL range of type TYPE. */
221 static inline void
222 set_value_range_to_null (value_range_t *vr, tree type)
224 tree zero = build_int_cst (type, 0);
225 set_value_range (vr, VR_RANGE, zero, zero, vr->equiv);
229 /* Set value range VR to VR_VARYING. */
231 static inline void
232 set_value_range_to_varying (value_range_t *vr)
234 vr->type = VR_VARYING;
235 vr->min = vr->max = NULL_TREE;
236 if (vr->equiv)
237 bitmap_clear (vr->equiv);
241 /* Set value range VR to a range of a truthvalue of type TYPE. */
243 static inline void
244 set_value_range_to_truthvalue (value_range_t *vr, tree type)
246 if (TYPE_PRECISION (type) == 1)
247 set_value_range_to_varying (vr);
248 else
249 set_value_range (vr, VR_RANGE,
250 build_int_cst (type, 0), build_int_cst (type, 1),
251 vr->equiv);
255 /* Set value range VR to VR_UNDEFINED. */
257 static inline void
258 set_value_range_to_undefined (value_range_t *vr)
260 vr->type = VR_UNDEFINED;
261 vr->min = vr->max = NULL_TREE;
262 if (vr->equiv)
263 bitmap_clear (vr->equiv);
267 /* Return value range information for VAR.
269 If we have no values ranges recorded (ie, VRP is not running), then
270 return NULL. Otherwise create an empty range if none existed for VAR. */
272 static value_range_t *
273 get_value_range (tree var)
275 value_range_t *vr;
276 tree sym;
277 unsigned ver = SSA_NAME_VERSION (var);
279 /* If we have no recorded ranges, then return NULL. */
280 if (! vr_value)
281 return NULL;
283 vr = vr_value[ver];
284 if (vr)
285 return vr;
287 /* Create a default value range. */
288 vr_value[ver] = vr = XCNEW (value_range_t);
290 /* Allocate an equivalence set. */
291 vr->equiv = BITMAP_ALLOC (NULL);
293 /* If VAR is a default definition, the variable can take any value
294 in VAR's type. */
295 sym = SSA_NAME_VAR (var);
296 if (SSA_NAME_IS_DEFAULT_DEF (var))
298 /* Try to use the "nonnull" attribute to create ~[0, 0]
299 anti-ranges for pointers. Note that this is only valid with
300 default definitions of PARM_DECLs. */
301 if (TREE_CODE (sym) == PARM_DECL
302 && POINTER_TYPE_P (TREE_TYPE (sym))
303 && nonnull_arg_p (sym))
304 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
305 else
306 set_value_range_to_varying (vr);
309 return vr;
312 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
314 static inline bool
315 vrp_operand_equal_p (tree val1, tree val2)
317 return (val1 == val2
318 || (val1 && val2
319 && operand_equal_p (val1, val2, 0)));
322 /* Return true, if the bitmaps B1 and B2 are equal. */
324 static inline bool
325 vrp_bitmap_equal_p (bitmap b1, bitmap b2)
327 return (b1 == b2
328 || (b1 && b2
329 && bitmap_equal_p (b1, b2)));
332 /* Update the value range and equivalence set for variable VAR to
333 NEW_VR. Return true if NEW_VR is different from VAR's previous
334 value.
336 NOTE: This function assumes that NEW_VR is a temporary value range
337 object created for the sole purpose of updating VAR's range. The
338 storage used by the equivalence set from NEW_VR will be freed by
339 this function. Do not call update_value_range when NEW_VR
340 is the range object associated with another SSA name. */
342 static inline bool
343 update_value_range (tree var, value_range_t *new_vr)
345 value_range_t *old_vr;
346 bool is_new;
348 /* Update the value range, if necessary. */
349 old_vr = get_value_range (var);
350 is_new = old_vr->type != new_vr->type
351 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
352 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
353 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
355 if (is_new)
356 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
357 new_vr->equiv);
359 BITMAP_FREE (new_vr->equiv);
360 new_vr->equiv = NULL;
362 return is_new;
366 /* Add VAR and VAR's equivalence set to EQUIV. */
368 static void
369 add_equivalence (bitmap equiv, tree var)
371 unsigned ver = SSA_NAME_VERSION (var);
372 value_range_t *vr = vr_value[ver];
374 bitmap_set_bit (equiv, ver);
375 if (vr && vr->equiv)
376 bitmap_ior_into (equiv, vr->equiv);
380 /* Return true if VR is ~[0, 0]. */
382 static inline bool
383 range_is_nonnull (value_range_t *vr)
385 return vr->type == VR_ANTI_RANGE
386 && integer_zerop (vr->min)
387 && integer_zerop (vr->max);
391 /* Return true if VR is [0, 0]. */
393 static inline bool
394 range_is_null (value_range_t *vr)
396 return vr->type == VR_RANGE
397 && integer_zerop (vr->min)
398 && integer_zerop (vr->max);
402 /* Return true if value range VR involves at least one symbol. */
404 static inline bool
405 symbolic_range_p (value_range_t *vr)
407 return (!is_gimple_min_invariant (vr->min)
408 || !is_gimple_min_invariant (vr->max));
411 /* Like tree_expr_nonnegative_p, but this function uses value ranges
412 obtained so far. */
414 static bool
415 vrp_expr_computes_nonnegative (tree expr)
417 bool ovf;
419 /* FIXME: May need to record overflow information here. */
420 return tree_expr_nonnegative_warnv_p (expr, &ovf);
423 /* Like tree_expr_nonzero_p, but this function uses value ranges
424 obtained so far. */
426 static bool
427 vrp_expr_computes_nonzero (tree expr)
429 bool ovf;
431 /* FIXME: May need to record overflow information here. */
432 if (tree_expr_nonzero_warnv_p (expr, &ovf))
433 return true;
435 /* If we have an expression of the form &X->a, then the expression
436 is nonnull if X is nonnull. */
437 if (TREE_CODE (expr) == ADDR_EXPR)
439 tree base = get_base_address (TREE_OPERAND (expr, 0));
441 if (base != NULL_TREE
442 && TREE_CODE (base) == INDIRECT_REF
443 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
445 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
446 if (range_is_nonnull (vr))
447 return true;
451 return false;
454 /* Returns true if EXPR is a valid value (as expected by compare_values) --
455 a gimple invariant, or SSA_NAME +- CST. */
457 static bool
458 valid_value_p (tree expr)
460 if (TREE_CODE (expr) == SSA_NAME)
461 return true;
463 if (TREE_CODE (expr) == PLUS_EXPR
464 || TREE_CODE (expr) == MINUS_EXPR)
465 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
466 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
468 return is_gimple_min_invariant (expr);
471 /* Return
472 1 if VAL < VAL2
473 0 if !(VAL < VAL2)
474 -2 if those are incomparable. */
475 static inline int
476 operand_less_p (tree val, tree val2)
478 tree tcmp;
479 /* LT is folded faster than GE and others. Inline the common case. */
480 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
482 if (TYPE_UNSIGNED (TREE_TYPE (val)))
483 return INT_CST_LT_UNSIGNED (val, val2);
484 else
485 return INT_CST_LT (val, val2);
487 else
488 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
489 if (!tcmp)
490 return -2;
491 return !integer_zerop (tcmp);
494 /* Compare two values VAL1 and VAL2. Return
496 -2 if VAL1 and VAL2 cannot be compared at compile-time,
497 -1 if VAL1 < VAL2,
498 0 if VAL1 == VAL2,
499 +1 if VAL1 > VAL2, and
500 +2 if VAL1 != VAL2
502 This is similar to tree_int_cst_compare but supports pointer values
503 and values that cannot be compared at compile time. */
505 static int
506 compare_values (tree val1, tree val2)
508 if (val1 == val2)
509 return 0;
511 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
512 both integers. */
513 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
514 == POINTER_TYPE_P (TREE_TYPE (val2)));
516 if ((TREE_CODE (val1) == SSA_NAME
517 || TREE_CODE (val1) == PLUS_EXPR
518 || TREE_CODE (val1) == MINUS_EXPR)
519 && (TREE_CODE (val2) == SSA_NAME
520 || TREE_CODE (val2) == PLUS_EXPR
521 || TREE_CODE (val2) == MINUS_EXPR))
523 tree n1, c1, n2, c2;
524 enum tree_code code1, code2;
526 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
527 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
528 same name, return -2. */
529 if (TREE_CODE (val1) == SSA_NAME)
531 code1 = SSA_NAME;
532 n1 = val1;
533 c1 = NULL_TREE;
535 else
537 code1 = TREE_CODE (val1);
538 n1 = TREE_OPERAND (val1, 0);
539 c1 = TREE_OPERAND (val1, 1);
540 if (tree_int_cst_sgn (c1) == -1)
542 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
543 if (!c1)
544 return -2;
545 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
549 if (TREE_CODE (val2) == SSA_NAME)
551 code2 = SSA_NAME;
552 n2 = val2;
553 c2 = NULL_TREE;
555 else
557 code2 = TREE_CODE (val2);
558 n2 = TREE_OPERAND (val2, 0);
559 c2 = TREE_OPERAND (val2, 1);
560 if (tree_int_cst_sgn (c2) == -1)
562 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
563 if (!c2)
564 return -2;
565 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
569 /* Both values must use the same name. */
570 if (n1 != n2)
571 return -2;
573 if (code1 == SSA_NAME
574 && code2 == SSA_NAME)
575 /* NAME == NAME */
576 return 0;
578 /* If overflow is defined we cannot simplify more. */
579 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
580 return -2;
582 if (code1 == SSA_NAME)
584 if (code2 == PLUS_EXPR)
585 /* NAME < NAME + CST */
586 return -1;
587 else if (code2 == MINUS_EXPR)
588 /* NAME > NAME - CST */
589 return 1;
591 else if (code1 == PLUS_EXPR)
593 if (code2 == SSA_NAME)
594 /* NAME + CST > NAME */
595 return 1;
596 else if (code2 == PLUS_EXPR)
597 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
598 return compare_values (c1, c2);
599 else if (code2 == MINUS_EXPR)
600 /* NAME + CST1 > NAME - CST2 */
601 return 1;
603 else if (code1 == MINUS_EXPR)
605 if (code2 == SSA_NAME)
606 /* NAME - CST < NAME */
607 return -1;
608 else if (code2 == PLUS_EXPR)
609 /* NAME - CST1 < NAME + CST2 */
610 return -1;
611 else if (code2 == MINUS_EXPR)
612 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
613 C1 and C2 are swapped in the call to compare_values. */
614 return compare_values (c2, c1);
617 gcc_unreachable ();
620 /* We cannot compare non-constants. */
621 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
622 return -2;
624 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
626 /* We cannot compare overflowed values. */
627 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
628 return -2;
630 return tree_int_cst_compare (val1, val2);
632 else
634 tree t;
636 /* First see if VAL1 and VAL2 are not the same. */
637 if (val1 == val2 || operand_equal_p (val1, val2, 0))
638 return 0;
640 /* If VAL1 is a lower address than VAL2, return -1. */
641 if (operand_less_p (val1, val2) == 1)
642 return -1;
644 /* If VAL1 is a higher address than VAL2, return +1. */
645 if (operand_less_p (val2, val1) == 1)
646 return 1;
648 /* If VAL1 is different than VAL2, return +2.
649 For integer constants we either have already returned -1 or 1
650 or they are equivalent. We still might succeed in proving
651 something about non-trivial operands. */
652 if (TREE_CODE (val1) != INTEGER_CST
653 || TREE_CODE (val2) != INTEGER_CST)
655 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
656 if (t && tree_expr_nonzero_p (t))
657 return 2;
660 return -2;
665 /* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
666 0 if VAL is not inside VR,
667 -2 if we cannot tell either way.
669 FIXME, the current semantics of this functions are a bit quirky
670 when taken in the context of VRP. In here we do not care
671 about VR's type. If VR is the anti-range ~[3, 5] the call
672 value_inside_range (4, VR) will return 1.
674 This is counter-intuitive in a strict sense, but the callers
675 currently expect this. They are calling the function
676 merely to determine whether VR->MIN <= VAL <= VR->MAX. The
677 callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
678 themselves.
680 This also applies to value_ranges_intersect_p and
681 range_includes_zero_p. The semantics of VR_RANGE and
682 VR_ANTI_RANGE should be encoded here, but that also means
683 adapting the users of these functions to the new semantics.
685 Benchmark compile/20001226-1.c compilation time after changing this
686 function. */
688 static inline int
689 value_inside_range (tree val, value_range_t * vr)
691 int cmp1, cmp2;
693 cmp1 = operand_less_p (val, vr->min);
694 if (cmp1 == -2)
695 return -2;
696 if (cmp1 == 1)
697 return 0;
699 cmp2 = operand_less_p (vr->max, val);
700 if (cmp2 == -2)
701 return -2;
703 return !cmp2;
707 /* Return true if value ranges VR0 and VR1 have a non-empty
708 intersection.
710 Benchmark compile/20001226-1.c compilation time after changing this
711 function.
714 static inline bool
715 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
717 /* The value ranges do not intersect if the maximum of the first range is
718 less than the minimum of the second range or vice versa.
719 When those relations are unknown, we can't do any better. */
720 if (operand_less_p (vr0->max, vr1->min) != 0)
721 return false;
722 if (operand_less_p (vr1->max, vr0->min) != 0)
723 return false;
724 return true;
728 /* Return true if VR includes the value zero, false otherwise. FIXME,
729 currently this will return false for an anti-range like ~[-4, 3].
730 This will be wrong when the semantics of value_inside_range are
731 modified (currently the users of this function expect these
732 semantics). */
734 static inline bool
735 range_includes_zero_p (value_range_t *vr)
737 tree zero;
739 gcc_assert (vr->type != VR_UNDEFINED
740 && vr->type != VR_VARYING
741 && !symbolic_range_p (vr));
743 zero = build_int_cst (TREE_TYPE (vr->min), 0);
744 return (value_inside_range (zero, vr) == 1);
747 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
748 false otherwise or if no value range information is available. */
750 bool
751 ssa_name_nonnegative_p (tree t)
753 value_range_t *vr = get_value_range (t);
755 if (!vr)
756 return false;
758 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
759 which would return a useful value should be encoded as a VR_RANGE. */
760 if (vr->type == VR_RANGE)
762 int result = compare_values (vr->min, integer_zero_node);
764 return (result == 0 || result == 1);
766 return false;
769 /* Return true if T, an SSA_NAME, is known to be nonzero. Return
770 false otherwise or if no value range information is available. */
772 bool
773 ssa_name_nonzero_p (tree t)
775 value_range_t *vr = get_value_range (t);
777 if (!vr)
778 return false;
780 /* A VR_RANGE which does not include zero is a nonzero value. */
781 if (vr->type == VR_RANGE && !symbolic_range_p (vr))
782 return ! range_includes_zero_p (vr);
784 /* A VR_ANTI_RANGE which does include zero is a nonzero value. */
785 if (vr->type == VR_ANTI_RANGE && !symbolic_range_p (vr))
786 return range_includes_zero_p (vr);
788 return false;
792 /* Extract value range information from an ASSERT_EXPR EXPR and store
793 it in *VR_P. */
795 static void
796 extract_range_from_assert (value_range_t *vr_p, tree expr)
798 tree var, cond, limit, min, max, type;
799 value_range_t *var_vr, *limit_vr;
800 enum tree_code cond_code;
802 var = ASSERT_EXPR_VAR (expr);
803 cond = ASSERT_EXPR_COND (expr);
805 gcc_assert (COMPARISON_CLASS_P (cond));
807 /* Find VAR in the ASSERT_EXPR conditional. */
808 if (var == TREE_OPERAND (cond, 0))
810 /* If the predicate is of the form VAR COMP LIMIT, then we just
811 take LIMIT from the RHS and use the same comparison code. */
812 limit = TREE_OPERAND (cond, 1);
813 cond_code = TREE_CODE (cond);
815 else
817 /* If the predicate is of the form LIMIT COMP VAR, then we need
818 to flip around the comparison code to create the proper range
819 for VAR. */
820 limit = TREE_OPERAND (cond, 0);
821 cond_code = swap_tree_comparison (TREE_CODE (cond));
824 type = TREE_TYPE (limit);
825 gcc_assert (limit != var);
827 /* For pointer arithmetic, we only keep track of pointer equality
828 and inequality. */
829 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
831 set_value_range_to_varying (vr_p);
832 return;
835 /* If LIMIT is another SSA name and LIMIT has a range of its own,
836 try to use LIMIT's range to avoid creating symbolic ranges
837 unnecessarily. */
838 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
840 /* LIMIT's range is only interesting if it has any useful information. */
841 if (limit_vr
842 && (limit_vr->type == VR_UNDEFINED
843 || limit_vr->type == VR_VARYING
844 || symbolic_range_p (limit_vr)))
845 limit_vr = NULL;
847 /* Initially, the new range has the same set of equivalences of
848 VAR's range. This will be revised before returning the final
849 value. Since assertions may be chained via mutually exclusive
850 predicates, we will need to trim the set of equivalences before
851 we are done. */
852 gcc_assert (vr_p->equiv == NULL);
853 vr_p->equiv = BITMAP_ALLOC (NULL);
854 add_equivalence (vr_p->equiv, var);
856 /* Extract a new range based on the asserted comparison for VAR and
857 LIMIT's value range. Notice that if LIMIT has an anti-range, we
858 will only use it for equality comparisons (EQ_EXPR). For any
859 other kind of assertion, we cannot derive a range from LIMIT's
860 anti-range that can be used to describe the new range. For
861 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
862 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
863 no single range for x_2 that could describe LE_EXPR, so we might
864 as well build the range [b_4, +INF] for it. */
865 if (cond_code == EQ_EXPR)
867 enum value_range_type range_type;
869 if (limit_vr)
871 range_type = limit_vr->type;
872 min = limit_vr->min;
873 max = limit_vr->max;
875 else
877 range_type = VR_RANGE;
878 min = limit;
879 max = limit;
882 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
884 /* When asserting the equality VAR == LIMIT and LIMIT is another
885 SSA name, the new range will also inherit the equivalence set
886 from LIMIT. */
887 if (TREE_CODE (limit) == SSA_NAME)
888 add_equivalence (vr_p->equiv, limit);
890 else if (cond_code == NE_EXPR)
892 /* As described above, when LIMIT's range is an anti-range and
893 this assertion is an inequality (NE_EXPR), then we cannot
894 derive anything from the anti-range. For instance, if
895 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
896 not imply that VAR's range is [0, 0]. So, in the case of
897 anti-ranges, we just assert the inequality using LIMIT and
898 not its anti-range.
900 If LIMIT_VR is a range, we can only use it to build a new
901 anti-range if LIMIT_VR is a single-valued range. For
902 instance, if LIMIT_VR is [0, 1], the predicate
903 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
904 Rather, it means that for value 0 VAR should be ~[0, 0]
905 and for value 1, VAR should be ~[1, 1]. We cannot
906 represent these ranges.
908 The only situation in which we can build a valid
909 anti-range is when LIMIT_VR is a single-valued range
910 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
911 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
912 if (limit_vr
913 && limit_vr->type == VR_RANGE
914 && compare_values (limit_vr->min, limit_vr->max) == 0)
916 min = limit_vr->min;
917 max = limit_vr->max;
919 else
921 /* In any other case, we cannot use LIMIT's range to build a
922 valid anti-range. */
923 min = max = limit;
926 /* If MIN and MAX cover the whole range for their type, then
927 just use the original LIMIT. */
928 if (INTEGRAL_TYPE_P (type)
929 && min == TYPE_MIN_VALUE (type)
930 && max == TYPE_MAX_VALUE (type))
931 min = max = limit;
933 set_value_range (vr_p, VR_ANTI_RANGE, min, max, vr_p->equiv);
935 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
937 min = TYPE_MIN_VALUE (type);
939 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
940 max = limit;
941 else
943 /* If LIMIT_VR is of the form [N1, N2], we need to build the
944 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
945 LT_EXPR. */
946 max = limit_vr->max;
949 /* If the maximum value forces us to be out of bounds, simply punt.
950 It would be pointless to try and do anything more since this
951 all should be optimized away above us. */
952 if (cond_code == LT_EXPR && compare_values (max, min) == 0)
953 set_value_range_to_varying (vr_p);
954 else
956 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
957 if (cond_code == LT_EXPR)
959 tree one = build_int_cst (type, 1);
960 max = fold_build2 (MINUS_EXPR, type, max, one);
963 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
966 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
968 max = TYPE_MAX_VALUE (type);
970 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
971 min = limit;
972 else
974 /* If LIMIT_VR is of the form [N1, N2], we need to build the
975 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
976 GT_EXPR. */
977 min = limit_vr->min;
980 /* If the minimum value forces us to be out of bounds, simply punt.
981 It would be pointless to try and do anything more since this
982 all should be optimized away above us. */
983 if (cond_code == GT_EXPR && compare_values (min, max) == 0)
984 set_value_range_to_varying (vr_p);
985 else
987 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
988 if (cond_code == GT_EXPR)
990 tree one = build_int_cst (type, 1);
991 min = fold_build2 (PLUS_EXPR, type, min, one);
994 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
997 else
998 gcc_unreachable ();
1000 /* If VAR already had a known range, it may happen that the new
1001 range we have computed and VAR's range are not compatible. For
1002 instance,
1004 if (p_5 == NULL)
1005 p_6 = ASSERT_EXPR <p_5, p_5 == NULL>;
1006 x_7 = p_6->fld;
1007 p_8 = ASSERT_EXPR <p_6, p_6 != NULL>;
1009 While the above comes from a faulty program, it will cause an ICE
1010 later because p_8 and p_6 will have incompatible ranges and at
1011 the same time will be considered equivalent. A similar situation
1012 would arise from
1014 if (i_5 > 10)
1015 i_6 = ASSERT_EXPR <i_5, i_5 > 10>;
1016 if (i_5 < 5)
1017 i_7 = ASSERT_EXPR <i_6, i_6 < 5>;
1019 Again i_6 and i_7 will have incompatible ranges. It would be
1020 pointless to try and do anything with i_7's range because
1021 anything dominated by 'if (i_5 < 5)' will be optimized away.
1022 Note, due to the wa in which simulation proceeds, the statement
1023 i_7 = ASSERT_EXPR <...> we would never be visited because the
1024 conditional 'if (i_5 < 5)' always evaluates to false. However,
1025 this extra check does not hurt and may protect against future
1026 changes to VRP that may get into a situation similar to the
1027 NULL pointer dereference example.
1029 Note that these compatibility tests are only needed when dealing
1030 with ranges or a mix of range and anti-range. If VAR_VR and VR_P
1031 are both anti-ranges, they will always be compatible, because two
1032 anti-ranges will always have a non-empty intersection. */
1034 var_vr = get_value_range (var);
1036 /* We may need to make adjustments when VR_P and VAR_VR are numeric
1037 ranges or anti-ranges. */
1038 if (vr_p->type == VR_VARYING
1039 || vr_p->type == VR_UNDEFINED
1040 || var_vr->type == VR_VARYING
1041 || var_vr->type == VR_UNDEFINED
1042 || symbolic_range_p (vr_p)
1043 || symbolic_range_p (var_vr))
1044 return;
1046 if (var_vr->type == VR_RANGE && vr_p->type == VR_RANGE)
1048 /* If the two ranges have a non-empty intersection, we can
1049 refine the resulting range. Since the assert expression
1050 creates an equivalency and at the same time it asserts a
1051 predicate, we can take the intersection of the two ranges to
1052 get better precision. */
1053 if (value_ranges_intersect_p (var_vr, vr_p))
1055 /* Use the larger of the two minimums. */
1056 if (compare_values (vr_p->min, var_vr->min) == -1)
1057 min = var_vr->min;
1058 else
1059 min = vr_p->min;
1061 /* Use the smaller of the two maximums. */
1062 if (compare_values (vr_p->max, var_vr->max) == 1)
1063 max = var_vr->max;
1064 else
1065 max = vr_p->max;
1067 set_value_range (vr_p, vr_p->type, min, max, vr_p->equiv);
1069 else
1071 /* The two ranges do not intersect, set the new range to
1072 VARYING, because we will not be able to do anything
1073 meaningful with it. */
1074 set_value_range_to_varying (vr_p);
1077 else if ((var_vr->type == VR_RANGE && vr_p->type == VR_ANTI_RANGE)
1078 || (var_vr->type == VR_ANTI_RANGE && vr_p->type == VR_RANGE))
1080 /* A range and an anti-range will cancel each other only if
1081 their ends are the same. For instance, in the example above,
1082 p_8's range ~[0, 0] and p_6's range [0, 0] are incompatible,
1083 so VR_P should be set to VR_VARYING. */
1084 if (compare_values (var_vr->min, vr_p->min) == 0
1085 && compare_values (var_vr->max, vr_p->max) == 0)
1086 set_value_range_to_varying (vr_p);
1087 else
1089 tree min, max, anti_min, anti_max, real_min, real_max;
1090 int cmp;
1092 /* We want to compute the logical AND of the two ranges;
1093 there are three cases to consider.
1096 1. The VR_ANTI_RANGE range is completely within the
1097 VR_RANGE and the endpoints of the ranges are
1098 different. In that case the resulting range
1099 should be whichever range is more precise.
1100 Typically that will be the VR_RANGE.
1102 2. The VR_ANTI_RANGE is completely disjoint from
1103 the VR_RANGE. In this case the resulting range
1104 should be the VR_RANGE.
1106 3. There is some overlap between the VR_ANTI_RANGE
1107 and the VR_RANGE.
1109 3a. If the high limit of the VR_ANTI_RANGE resides
1110 within the VR_RANGE, then the result is a new
1111 VR_RANGE starting at the high limit of the
1112 the VR_ANTI_RANGE + 1 and extending to the
1113 high limit of the original VR_RANGE.
1115 3b. If the low limit of the VR_ANTI_RANGE resides
1116 within the VR_RANGE, then the result is a new
1117 VR_RANGE starting at the low limit of the original
1118 VR_RANGE and extending to the low limit of the
1119 VR_ANTI_RANGE - 1. */
1120 if (vr_p->type == VR_ANTI_RANGE)
1122 anti_min = vr_p->min;
1123 anti_max = vr_p->max;
1124 real_min = var_vr->min;
1125 real_max = var_vr->max;
1127 else
1129 anti_min = var_vr->min;
1130 anti_max = var_vr->max;
1131 real_min = vr_p->min;
1132 real_max = vr_p->max;
1136 /* Case 1, VR_ANTI_RANGE completely within VR_RANGE,
1137 not including any endpoints. */
1138 if (compare_values (anti_max, real_max) == -1
1139 && compare_values (anti_min, real_min) == 1)
1141 set_value_range (vr_p, VR_RANGE, real_min,
1142 real_max, vr_p->equiv);
1144 /* Case 2, VR_ANTI_RANGE completely disjoint from
1145 VR_RANGE. */
1146 else if (compare_values (anti_min, real_max) == 1
1147 || compare_values (anti_max, real_min) == -1)
1149 set_value_range (vr_p, VR_RANGE, real_min,
1150 real_max, vr_p->equiv);
1152 /* Case 3a, the anti-range extends into the low
1153 part of the real range. Thus creating a new
1154 low for the real range. */
1155 else if (((cmp = compare_values (anti_max, real_min)) == 1
1156 || cmp == 0)
1157 && compare_values (anti_max, real_max) == -1)
1159 min = fold_build2 (PLUS_EXPR, TREE_TYPE (var_vr->min),
1160 anti_max,
1161 build_int_cst (TREE_TYPE (var_vr->min), 1));
1162 max = real_max;
1163 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1165 /* Case 3b, the anti-range extends into the high
1166 part of the real range. Thus creating a new
1167 higher for the real range. */
1168 else if (compare_values (anti_min, real_min) == 1
1169 && ((cmp = compare_values (anti_min, real_max)) == -1
1170 || cmp == 0))
1172 max = fold_build2 (MINUS_EXPR, TREE_TYPE (var_vr->min),
1173 anti_min,
1174 build_int_cst (TREE_TYPE (var_vr->min), 1));
1175 min = real_min;
1176 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1183 /* Extract range information from SSA name VAR and store it in VR. If
1184 VAR has an interesting range, use it. Otherwise, create the
1185 range [VAR, VAR] and return it. This is useful in situations where
1186 we may have conditionals testing values of VARYING names. For
1187 instance,
1189 x_3 = y_5;
1190 if (x_3 > y_5)
1193 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1194 always false. */
1196 static void
1197 extract_range_from_ssa_name (value_range_t *vr, tree var)
1199 value_range_t *var_vr = get_value_range (var);
1201 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1202 copy_value_range (vr, var_vr);
1203 else
1204 set_value_range (vr, VR_RANGE, var, var, NULL);
1206 add_equivalence (vr->equiv, var);
1210 /* Wrapper around int_const_binop. If the operation overflows and we
1211 are not using wrapping arithmetic, then adjust the result to be
1212 -INF or +INF depending on CODE, VAL1 and VAL2. */
1214 static inline tree
1215 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1217 tree res;
1219 res = int_const_binop (code, val1, val2, 0);
1221 /* If we are not using wrapping arithmetic, operate symbolically
1222 on -INF and +INF. */
1223 if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1225 int checkz = compare_values (res, val1);
1226 bool overflow = false;
1228 /* Ensure that res = val1 [+*] val2 >= val1
1229 or that res = val1 - val2 <= val1. */
1230 if ((code == PLUS_EXPR
1231 && !(checkz == 1 || checkz == 0))
1232 || (code == MINUS_EXPR
1233 && !(checkz == 0 || checkz == -1)))
1235 overflow = true;
1237 /* Checking for multiplication overflow is done by dividing the
1238 output of the multiplication by the first input of the
1239 multiplication. If the result of that division operation is
1240 not equal to the second input of the multiplication, then the
1241 multiplication overflowed. */
1242 else if (code == MULT_EXPR && !integer_zerop (val1))
1244 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1245 res,
1246 val1, 0);
1247 int check = compare_values (tmp, val2);
1249 if (check != 0)
1250 overflow = true;
1253 if (overflow)
1255 res = copy_node (res);
1256 TREE_OVERFLOW (res) = 1;
1260 else if (TREE_OVERFLOW (res)
1261 && !TREE_OVERFLOW (val1)
1262 && !TREE_OVERFLOW (val2))
1264 /* If the operation overflowed but neither VAL1 nor VAL2 are
1265 overflown, return -INF or +INF depending on the operation
1266 and the combination of signs of the operands. */
1267 int sgn1 = tree_int_cst_sgn (val1);
1268 int sgn2 = tree_int_cst_sgn (val2);
1270 /* Notice that we only need to handle the restricted set of
1271 operations handled by extract_range_from_binary_expr.
1272 Among them, only multiplication, addition and subtraction
1273 can yield overflow without overflown operands because we
1274 are working with integral types only... except in the
1275 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1276 for division too. */
1278 /* For multiplication, the sign of the overflow is given
1279 by the comparison of the signs of the operands. */
1280 if ((code == MULT_EXPR && sgn1 == sgn2)
1281 /* For addition, the operands must be of the same sign
1282 to yield an overflow. Its sign is therefore that
1283 of one of the operands, for example the first. */
1284 || (code == PLUS_EXPR && sgn1 > 0)
1285 /* For subtraction, the operands must be of different
1286 signs to yield an overflow. Its sign is therefore
1287 that of the first operand or the opposite of that
1288 of the second operand. A first operand of 0 counts
1289 as positive here, for the corner case 0 - (-INF),
1290 which overflows, but must yield +INF. */
1291 || (code == MINUS_EXPR && sgn1 >= 0)
1292 /* For division, the only case is -INF / -1 = +INF. */
1293 || code == TRUNC_DIV_EXPR
1294 || code == FLOOR_DIV_EXPR
1295 || code == CEIL_DIV_EXPR
1296 || code == EXACT_DIV_EXPR
1297 || code == ROUND_DIV_EXPR)
1298 return TYPE_MAX_VALUE (TREE_TYPE (res));
1299 else
1300 return TYPE_MIN_VALUE (TREE_TYPE (res));
1303 return res;
1307 /* Extract range information from a binary expression EXPR based on
1308 the ranges of each of its operands and the expression code. */
1310 static void
1311 extract_range_from_binary_expr (value_range_t *vr, tree expr)
1313 enum tree_code code = TREE_CODE (expr);
1314 enum value_range_type type;
1315 tree op0, op1, min, max;
1316 int cmp;
1317 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1318 value_range_t vr1 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1320 /* Not all binary expressions can be applied to ranges in a
1321 meaningful way. Handle only arithmetic operations. */
1322 if (code != PLUS_EXPR
1323 && code != MINUS_EXPR
1324 && code != MULT_EXPR
1325 && code != TRUNC_DIV_EXPR
1326 && code != FLOOR_DIV_EXPR
1327 && code != CEIL_DIV_EXPR
1328 && code != EXACT_DIV_EXPR
1329 && code != ROUND_DIV_EXPR
1330 && code != MIN_EXPR
1331 && code != MAX_EXPR
1332 && code != BIT_AND_EXPR
1333 && code != TRUTH_ANDIF_EXPR
1334 && code != TRUTH_ORIF_EXPR
1335 && code != TRUTH_AND_EXPR
1336 && code != TRUTH_OR_EXPR)
1338 set_value_range_to_varying (vr);
1339 return;
1342 /* Get value ranges for each operand. For constant operands, create
1343 a new value range with the operand to simplify processing. */
1344 op0 = TREE_OPERAND (expr, 0);
1345 if (TREE_CODE (op0) == SSA_NAME)
1346 vr0 = *(get_value_range (op0));
1347 else if (is_gimple_min_invariant (op0))
1348 set_value_range (&vr0, VR_RANGE, op0, op0, NULL);
1349 else
1350 set_value_range_to_varying (&vr0);
1352 op1 = TREE_OPERAND (expr, 1);
1353 if (TREE_CODE (op1) == SSA_NAME)
1354 vr1 = *(get_value_range (op1));
1355 else if (is_gimple_min_invariant (op1))
1356 set_value_range (&vr1, VR_RANGE, op1, op1, NULL);
1357 else
1358 set_value_range_to_varying (&vr1);
1360 /* If either range is UNDEFINED, so is the result. */
1361 if (vr0.type == VR_UNDEFINED || vr1.type == VR_UNDEFINED)
1363 set_value_range_to_undefined (vr);
1364 return;
1367 /* The type of the resulting value range defaults to VR0.TYPE. */
1368 type = vr0.type;
1370 /* Refuse to operate on VARYING ranges, ranges of different kinds
1371 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
1372 because we may be able to derive a useful range even if one of
1373 the operands is VR_VARYING or symbolic range. TODO, we may be
1374 able to derive anti-ranges in some cases. */
1375 if (code != BIT_AND_EXPR
1376 && code != TRUTH_AND_EXPR
1377 && code != TRUTH_OR_EXPR
1378 && (vr0.type == VR_VARYING
1379 || vr1.type == VR_VARYING
1380 || vr0.type != vr1.type
1381 || symbolic_range_p (&vr0)
1382 || symbolic_range_p (&vr1)))
1384 set_value_range_to_varying (vr);
1385 return;
1388 /* Now evaluate the expression to determine the new range. */
1389 if (POINTER_TYPE_P (TREE_TYPE (expr))
1390 || POINTER_TYPE_P (TREE_TYPE (op0))
1391 || POINTER_TYPE_P (TREE_TYPE (op1)))
1393 /* For pointer types, we are really only interested in asserting
1394 whether the expression evaluates to non-NULL. FIXME, we used
1395 to gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR), but
1396 ivopts is generating expressions with pointer multiplication
1397 in them. */
1398 if (code == PLUS_EXPR)
1400 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
1401 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1402 else if (range_is_null (&vr0) && range_is_null (&vr1))
1403 set_value_range_to_null (vr, TREE_TYPE (expr));
1404 else
1405 set_value_range_to_varying (vr);
1407 else
1409 /* Subtracting from a pointer, may yield 0, so just drop the
1410 resulting range to varying. */
1411 set_value_range_to_varying (vr);
1414 return;
1417 /* For integer ranges, apply the operation to each end of the
1418 range and see what we end up with. */
1419 if (code == TRUTH_ANDIF_EXPR
1420 || code == TRUTH_ORIF_EXPR
1421 || code == TRUTH_AND_EXPR
1422 || code == TRUTH_OR_EXPR)
1424 /* If one of the operands is zero, we know that the whole
1425 expression evaluates zero. */
1426 if (code == TRUTH_AND_EXPR
1427 && ((vr0.type == VR_RANGE
1428 && integer_zerop (vr0.min)
1429 && integer_zerop (vr0.max))
1430 || (vr1.type == VR_RANGE
1431 && integer_zerop (vr1.min)
1432 && integer_zerop (vr1.max))))
1434 type = VR_RANGE;
1435 min = max = build_int_cst (TREE_TYPE (expr), 0);
1437 /* If one of the operands is one, we know that the whole
1438 expression evaluates one. */
1439 else if (code == TRUTH_OR_EXPR
1440 && ((vr0.type == VR_RANGE
1441 && integer_onep (vr0.min)
1442 && integer_onep (vr0.max))
1443 || (vr1.type == VR_RANGE
1444 && integer_onep (vr1.min)
1445 && integer_onep (vr1.max))))
1447 type = VR_RANGE;
1448 min = max = build_int_cst (TREE_TYPE (expr), 1);
1450 else if (vr0.type != VR_VARYING
1451 && vr1.type != VR_VARYING
1452 && vr0.type == vr1.type
1453 && !symbolic_range_p (&vr0)
1454 && !symbolic_range_p (&vr1))
1456 /* Boolean expressions cannot be folded with int_const_binop. */
1457 min = fold_binary (code, TREE_TYPE (expr), vr0.min, vr1.min);
1458 max = fold_binary (code, TREE_TYPE (expr), vr0.max, vr1.max);
1460 else
1462 /* The result of a TRUTH_*_EXPR is always true or false. */
1463 set_value_range_to_truthvalue (vr, TREE_TYPE (expr));
1464 return;
1467 else if (code == PLUS_EXPR
1468 || code == MIN_EXPR
1469 || code == MAX_EXPR)
1471 /* If we have a PLUS_EXPR with two VR_ANTI_RANGEs, drop to
1472 VR_VARYING. It would take more effort to compute a precise
1473 range for such a case. For example, if we have op0 == 1 and
1474 op1 == -1 with their ranges both being ~[0,0], we would have
1475 op0 + op1 == 0, so we cannot claim that the sum is in ~[0,0].
1476 Note that we are guaranteed to have vr0.type == vr1.type at
1477 this point. */
1478 if (code == PLUS_EXPR && vr0.type == VR_ANTI_RANGE)
1480 set_value_range_to_varying (vr);
1481 return;
1484 /* For operations that make the resulting range directly
1485 proportional to the original ranges, apply the operation to
1486 the same end of each range. */
1487 min = vrp_int_const_binop (code, vr0.min, vr1.min);
1488 max = vrp_int_const_binop (code, vr0.max, vr1.max);
1490 else if (code == MULT_EXPR
1491 || code == TRUNC_DIV_EXPR
1492 || code == FLOOR_DIV_EXPR
1493 || code == CEIL_DIV_EXPR
1494 || code == EXACT_DIV_EXPR
1495 || code == ROUND_DIV_EXPR)
1497 tree val[4];
1498 size_t i;
1500 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1501 drop to VR_VARYING. It would take more effort to compute a
1502 precise range for such a case. For example, if we have
1503 op0 == 65536 and op1 == 65536 with their ranges both being
1504 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1505 we cannot claim that the product is in ~[0,0]. Note that we
1506 are guaranteed to have vr0.type == vr1.type at this
1507 point. */
1508 if (code == MULT_EXPR
1509 && vr0.type == VR_ANTI_RANGE
1510 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
1512 set_value_range_to_varying (vr);
1513 return;
1516 /* Multiplications and divisions are a bit tricky to handle,
1517 depending on the mix of signs we have in the two ranges, we
1518 need to operate on different values to get the minimum and
1519 maximum values for the new range. One approach is to figure
1520 out all the variations of range combinations and do the
1521 operations.
1523 However, this involves several calls to compare_values and it
1524 is pretty convoluted. It's simpler to do the 4 operations
1525 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1526 MAX1) and then figure the smallest and largest values to form
1527 the new range. */
1529 /* Divisions by zero result in a VARYING value. */
1530 if (code != MULT_EXPR
1531 && (vr0.type == VR_ANTI_RANGE || range_includes_zero_p (&vr1)))
1533 set_value_range_to_varying (vr);
1534 return;
1537 /* Compute the 4 cross operations. */
1538 val[0] = vrp_int_const_binop (code, vr0.min, vr1.min);
1540 val[1] = (vr1.max != vr1.min)
1541 ? vrp_int_const_binop (code, vr0.min, vr1.max)
1542 : NULL_TREE;
1544 val[2] = (vr0.max != vr0.min)
1545 ? vrp_int_const_binop (code, vr0.max, vr1.min)
1546 : NULL_TREE;
1548 val[3] = (vr0.min != vr0.max && vr1.min != vr1.max)
1549 ? vrp_int_const_binop (code, vr0.max, vr1.max)
1550 : NULL_TREE;
1552 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
1553 of VAL[i]. */
1554 min = val[0];
1555 max = val[0];
1556 for (i = 1; i < 4; i++)
1558 if (!is_gimple_min_invariant (min) || TREE_OVERFLOW (min)
1559 || !is_gimple_min_invariant (max) || TREE_OVERFLOW (max))
1560 break;
1562 if (val[i])
1564 if (!is_gimple_min_invariant (val[i]) || TREE_OVERFLOW (val[i]))
1566 /* If we found an overflowed value, set MIN and MAX
1567 to it so that we set the resulting range to
1568 VARYING. */
1569 min = max = val[i];
1570 break;
1573 if (compare_values (val[i], min) == -1)
1574 min = val[i];
1576 if (compare_values (val[i], max) == 1)
1577 max = val[i];
1581 else if (code == MINUS_EXPR)
1583 /* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
1584 VR_VARYING. It would take more effort to compute a precise
1585 range for such a case. For example, if we have op0 == 1 and
1586 op1 == 1 with their ranges both being ~[0,0], we would have
1587 op0 - op1 == 0, so we cannot claim that the difference is in
1588 ~[0,0]. Note that we are guaranteed to have
1589 vr0.type == vr1.type at this point. */
1590 if (vr0.type == VR_ANTI_RANGE)
1592 set_value_range_to_varying (vr);
1593 return;
1596 /* For MINUS_EXPR, apply the operation to the opposite ends of
1597 each range. */
1598 min = vrp_int_const_binop (code, vr0.min, vr1.max);
1599 max = vrp_int_const_binop (code, vr0.max, vr1.min);
1601 else if (code == BIT_AND_EXPR)
1603 if (vr0.type == VR_RANGE
1604 && vr0.min == vr0.max
1605 && tree_expr_nonnegative_p (vr0.max)
1606 && TREE_CODE (vr0.max) == INTEGER_CST)
1608 min = build_int_cst (TREE_TYPE (expr), 0);
1609 max = vr0.max;
1611 else if (vr1.type == VR_RANGE
1612 && vr1.min == vr1.max
1613 && tree_expr_nonnegative_p (vr1.max)
1614 && TREE_CODE (vr1.max) == INTEGER_CST)
1616 type = VR_RANGE;
1617 min = build_int_cst (TREE_TYPE (expr), 0);
1618 max = vr1.max;
1620 else
1622 set_value_range_to_varying (vr);
1623 return;
1626 else
1627 gcc_unreachable ();
1629 /* If either MIN or MAX overflowed, then set the resulting range to
1630 VARYING. */
1631 if (!is_gimple_min_invariant (min) || TREE_OVERFLOW (min)
1632 || !is_gimple_min_invariant (max) || TREE_OVERFLOW (max))
1634 set_value_range_to_varying (vr);
1635 return;
1638 cmp = compare_values (min, max);
1639 if (cmp == -2 || cmp == 1)
1641 /* If the new range has its limits swapped around (MIN > MAX),
1642 then the operation caused one of them to wrap around, mark
1643 the new range VARYING. */
1644 set_value_range_to_varying (vr);
1646 else
1647 set_value_range (vr, type, min, max, NULL);
1651 /* Extract range information from a unary expression EXPR based on
1652 the range of its operand and the expression code. */
1654 static void
1655 extract_range_from_unary_expr (value_range_t *vr, tree expr)
1657 enum tree_code code = TREE_CODE (expr);
1658 tree min, max, op0;
1659 int cmp;
1660 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1662 /* Refuse to operate on certain unary expressions for which we
1663 cannot easily determine a resulting range. */
1664 if (code == FIX_TRUNC_EXPR
1665 || code == FLOAT_EXPR
1666 || code == BIT_NOT_EXPR
1667 || code == NON_LVALUE_EXPR
1668 || code == CONJ_EXPR)
1670 set_value_range_to_varying (vr);
1671 return;
1674 /* Get value ranges for the operand. For constant operands, create
1675 a new value range with the operand to simplify processing. */
1676 op0 = TREE_OPERAND (expr, 0);
1677 if (TREE_CODE (op0) == SSA_NAME)
1678 vr0 = *(get_value_range (op0));
1679 else if (is_gimple_min_invariant (op0))
1680 set_value_range (&vr0, VR_RANGE, op0, op0, NULL);
1681 else
1682 set_value_range_to_varying (&vr0);
1684 /* If VR0 is UNDEFINED, so is the result. */
1685 if (vr0.type == VR_UNDEFINED)
1687 set_value_range_to_undefined (vr);
1688 return;
1691 /* Refuse to operate on symbolic ranges, or if neither operand is
1692 a pointer or integral type. */
1693 if ((!INTEGRAL_TYPE_P (TREE_TYPE (op0))
1694 && !POINTER_TYPE_P (TREE_TYPE (op0)))
1695 || (vr0.type != VR_VARYING
1696 && symbolic_range_p (&vr0)))
1698 set_value_range_to_varying (vr);
1699 return;
1702 /* If the expression involves pointers, we are only interested in
1703 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
1704 if (POINTER_TYPE_P (TREE_TYPE (expr)) || POINTER_TYPE_P (TREE_TYPE (op0)))
1706 bool ovf;
1708 /* FIXME: May need to record overflow information here. */
1709 if (range_is_nonnull (&vr0) || tree_expr_nonzero_warnv_p (expr, &ovf))
1710 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
1711 else if (range_is_null (&vr0))
1712 set_value_range_to_null (vr, TREE_TYPE (expr));
1713 else
1714 set_value_range_to_varying (vr);
1716 return;
1719 /* Handle unary expressions on integer ranges. */
1720 if (code == NOP_EXPR || code == CONVERT_EXPR)
1722 tree inner_type = TREE_TYPE (op0);
1723 tree outer_type = TREE_TYPE (expr);
1725 /* If VR0 represents a simple range, then try to convert
1726 the min and max values for the range to the same type
1727 as OUTER_TYPE. If the results compare equal to VR0's
1728 min and max values and the new min is still less than
1729 or equal to the new max, then we can safely use the newly
1730 computed range for EXPR. This allows us to compute
1731 accurate ranges through many casts. */
1732 if (vr0.type == VR_RANGE
1733 || (vr0.type == VR_VARYING
1734 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)))
1736 tree new_min, new_max, orig_min, orig_max;
1738 /* Convert the input operand min/max to OUTER_TYPE. If
1739 the input has no range information, then use the min/max
1740 for the input's type. */
1741 if (vr0.type == VR_RANGE)
1743 orig_min = vr0.min;
1744 orig_max = vr0.max;
1746 else
1748 orig_min = TYPE_MIN_VALUE (inner_type);
1749 orig_max = TYPE_MAX_VALUE (inner_type);
1752 new_min = fold_convert (outer_type, orig_min);
1753 new_max = fold_convert (outer_type, orig_max);
1755 /* Verify the new min/max values are gimple values and
1756 that they compare equal to the original input's
1757 min/max values. */
1758 if (is_gimple_val (new_min)
1759 && is_gimple_val (new_max)
1760 && tree_int_cst_equal (new_min, orig_min)
1761 && tree_int_cst_equal (new_max, orig_max)
1762 && (cmp = compare_values (new_min, new_max)) <= 0
1763 && cmp >= -1)
1765 set_value_range (vr, VR_RANGE, new_min, new_max, vr->equiv);
1766 return;
1770 /* When converting types of different sizes, set the result to
1771 VARYING. Things like sign extensions and precision loss may
1772 change the range. For instance, if x_3 is of type 'long long
1773 int' and 'y_5 = (unsigned short) x_3', if x_3 is ~[0, 0], it
1774 is impossible to know at compile time whether y_5 will be
1775 ~[0, 0]. */
1776 if (TYPE_SIZE (inner_type) != TYPE_SIZE (outer_type)
1777 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
1779 set_value_range_to_varying (vr);
1780 return;
1784 /* Conversion of a VR_VARYING value to a wider type can result
1785 in a usable range. So wait until after we've handled conversions
1786 before dropping the result to VR_VARYING if we had a source
1787 operand that is VR_VARYING. */
1788 if (vr0.type == VR_VARYING)
1790 set_value_range_to_varying (vr);
1791 return;
1794 /* Apply the operation to each end of the range and see what we end
1795 up with. */
1796 if (code == NEGATE_EXPR
1797 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
1799 /* NEGATE_EXPR flips the range around. We need to treat
1800 TYPE_MIN_VALUE specially dependent on wrapping, range type
1801 and if it was used as minimum or maximum value:
1802 -~[MIN, MIN] == ~[MIN, MIN]
1803 -[MIN, 0] == [0, MAX] for -fno-wrapv
1804 -[MIN, 0] == [0, MIN] for -fwrapv (will be set to varying later) */
1805 min = vr0.max == TYPE_MIN_VALUE (TREE_TYPE (expr))
1806 ? TYPE_MIN_VALUE (TREE_TYPE (expr))
1807 : fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1809 max = (vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr))
1810 ? ((vr0.type == VR_ANTI_RANGE
1811 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
1812 ? TYPE_MIN_VALUE (TREE_TYPE (expr))
1813 : TYPE_MAX_VALUE (TREE_TYPE (expr)))
1814 : fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min));
1817 else if (code == NEGATE_EXPR
1818 && TYPE_UNSIGNED (TREE_TYPE (expr)))
1820 if (!range_includes_zero_p (&vr0))
1822 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
1823 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1825 else
1827 if (range_is_null (&vr0))
1828 set_value_range_to_null (vr, TREE_TYPE (expr));
1829 else
1830 set_value_range_to_varying (vr);
1831 return;
1834 else if (code == ABS_EXPR
1835 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
1837 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
1838 useful range. */
1839 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (expr))
1840 && ((vr0.type == VR_RANGE
1841 && vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr)))
1842 || (vr0.type == VR_ANTI_RANGE
1843 && vr0.min != TYPE_MIN_VALUE (TREE_TYPE (expr))
1844 && !range_includes_zero_p (&vr0))))
1846 set_value_range_to_varying (vr);
1847 return;
1850 /* ABS_EXPR may flip the range around, if the original range
1851 included negative values. */
1852 min = (vr0.min == TYPE_MIN_VALUE (TREE_TYPE (expr)))
1853 ? TYPE_MAX_VALUE (TREE_TYPE (expr))
1854 : fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
1856 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1858 cmp = compare_values (min, max);
1860 /* If a VR_ANTI_RANGEs contains zero, then we have
1861 ~[-INF, min(MIN, MAX)]. */
1862 if (vr0.type == VR_ANTI_RANGE)
1864 if (range_includes_zero_p (&vr0))
1866 tree type_min_value = TYPE_MIN_VALUE (TREE_TYPE (expr));
1868 /* Take the lower of the two values. */
1869 if (cmp != 1)
1870 max = min;
1872 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
1873 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
1874 flag_wrapv is set and the original anti-range doesn't include
1875 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
1876 min = ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
1877 && vr0.min != type_min_value)
1878 ? int_const_binop (PLUS_EXPR,
1879 type_min_value,
1880 integer_one_node, 0)
1881 : type_min_value);
1883 else
1885 /* All else has failed, so create the range [0, INF], even for
1886 flag_wrapv since TYPE_MIN_VALUE is in the original
1887 anti-range. */
1888 vr0.type = VR_RANGE;
1889 min = build_int_cst (TREE_TYPE (expr), 0);
1890 max = TYPE_MAX_VALUE (TREE_TYPE (expr));
1894 /* If the range contains zero then we know that the minimum value in the
1895 range will be zero. */
1896 else if (range_includes_zero_p (&vr0))
1898 if (cmp == 1)
1899 max = min;
1900 min = build_int_cst (TREE_TYPE (expr), 0);
1902 else
1904 /* If the range was reversed, swap MIN and MAX. */
1905 if (cmp == 1)
1907 tree t = min;
1908 min = max;
1909 max = t;
1913 else
1915 /* Otherwise, operate on each end of the range. */
1916 min = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.min);
1917 max = fold_unary_to_constant (code, TREE_TYPE (expr), vr0.max);
1920 cmp = compare_values (min, max);
1921 if (cmp == -2 || cmp == 1)
1923 /* If the new range has its limits swapped around (MIN > MAX),
1924 then the operation caused one of them to wrap around, mark
1925 the new range VARYING. */
1926 set_value_range_to_varying (vr);
1928 else
1929 set_value_range (vr, vr0.type, min, max, NULL);
1933 /* Extract range information from a conditional expression EXPR based on
1934 the ranges of each of its operands and the expression code. */
1936 static void
1937 extract_range_from_cond_expr (value_range_t *vr, tree expr)
1939 tree op0, op1;
1940 value_range_t vr0 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1941 value_range_t vr1 = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
1943 /* Get value ranges for each operand. For constant operands, create
1944 a new value range with the operand to simplify processing. */
1945 op0 = COND_EXPR_THEN (expr);
1946 if (TREE_CODE (op0) == SSA_NAME)
1947 vr0 = *(get_value_range (op0));
1948 else if (is_gimple_min_invariant (op0))
1949 set_value_range (&vr0, VR_RANGE, op0, op0, NULL);
1950 else
1951 set_value_range_to_varying (&vr0);
1953 op1 = COND_EXPR_ELSE (expr);
1954 if (TREE_CODE (op1) == SSA_NAME)
1955 vr1 = *(get_value_range (op1));
1956 else if (is_gimple_min_invariant (op1))
1957 set_value_range (&vr1, VR_RANGE, op1, op1, NULL);
1958 else
1959 set_value_range_to_varying (&vr1);
1961 /* The resulting value range is the union of the operand ranges */
1962 vrp_meet (&vr0, &vr1);
1963 copy_value_range (vr, &vr0);
1967 /* Extract range information from a comparison expression EXPR based
1968 on the range of its operand and the expression code. */
1970 static void
1971 extract_range_from_comparison (value_range_t *vr, tree expr)
1973 tree val = vrp_evaluate_conditional (expr, false);
1974 if (val)
1976 /* Since this expression was found on the RHS of an assignment,
1977 its type may be different from _Bool. Convert VAL to EXPR's
1978 type. */
1979 val = fold_convert (TREE_TYPE (expr), val);
1980 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
1982 else
1983 /* The result of a comparison is always true or false. */
1984 set_value_range_to_truthvalue (vr, TREE_TYPE (expr));
1988 /* Try to compute a useful range out of expression EXPR and store it
1989 in *VR. */
1991 static void
1992 extract_range_from_expr (value_range_t *vr, tree expr)
1994 enum tree_code code = TREE_CODE (expr);
1996 if (code == ASSERT_EXPR)
1997 extract_range_from_assert (vr, expr);
1998 else if (code == SSA_NAME)
1999 extract_range_from_ssa_name (vr, expr);
2000 else if (TREE_CODE_CLASS (code) == tcc_binary
2001 || code == TRUTH_ANDIF_EXPR
2002 || code == TRUTH_ORIF_EXPR
2003 || code == TRUTH_AND_EXPR
2004 || code == TRUTH_OR_EXPR
2005 || code == TRUTH_XOR_EXPR)
2006 extract_range_from_binary_expr (vr, expr);
2007 else if (TREE_CODE_CLASS (code) == tcc_unary)
2008 extract_range_from_unary_expr (vr, expr);
2009 else if (code == COND_EXPR)
2010 extract_range_from_cond_expr (vr, expr);
2011 else if (TREE_CODE_CLASS (code) == tcc_comparison)
2012 extract_range_from_comparison (vr, expr);
2013 else if (is_gimple_min_invariant (expr))
2014 set_value_range (vr, VR_RANGE, expr, expr, NULL);
2015 else
2016 set_value_range_to_varying (vr);
2018 /* If we got a varying range from the tests above, try a final
2019 time to derive a nonnegative or nonzero range. This time
2020 relying primarily on generic routines in fold in conjunction
2021 with range data. */
2022 if (vr->type == VR_VARYING)
2024 if (INTEGRAL_TYPE_P (TREE_TYPE (expr))
2025 && vrp_expr_computes_nonnegative (expr))
2026 set_value_range_to_nonnegative (vr, TREE_TYPE (expr));
2027 else if (vrp_expr_computes_nonzero (expr))
2028 set_value_range_to_nonnull (vr, TREE_TYPE (expr));
2032 /* Given a range VR, a LOOP and a variable VAR, determine whether it
2033 would be profitable to adjust VR using scalar evolution information
2034 for VAR. If so, update VR with the new limits. */
2036 static void
2037 adjust_range_with_scev (value_range_t *vr, struct loop *loop, tree stmt,
2038 tree var)
2040 tree init, step, chrec, tmin, tmax, min, max, type;
2041 enum ev_direction dir;
2043 /* TODO. Don't adjust anti-ranges. An anti-range may provide
2044 better opportunities than a regular range, but I'm not sure. */
2045 if (vr->type == VR_ANTI_RANGE)
2046 return;
2048 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
2049 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2050 return;
2052 init = initial_condition_in_loop_num (chrec, loop->num);
2053 step = evolution_part_in_loop_num (chrec, loop->num);
2055 /* If STEP is symbolic, we can't know whether INIT will be the
2056 minimum or maximum value in the range. Also, unless INIT is
2057 a simple expression, compare_values and possibly other functions
2058 in tree-vrp won't be able to handle it. */
2059 if (step == NULL_TREE
2060 || !is_gimple_min_invariant (step)
2061 || !valid_value_p (init))
2062 return;
2064 dir = scev_direction (chrec);
2065 if (/* Do not adjust ranges if we do not know whether the iv increases
2066 or decreases, ... */
2067 dir == EV_DIR_UNKNOWN
2068 /* ... or if it may wrap. */
2069 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
2070 true))
2071 return;
2073 type = TREE_TYPE (var);
2074 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
2075 tmin = lower_bound_in_type (type, type);
2076 else
2077 tmin = TYPE_MIN_VALUE (type);
2078 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
2079 tmax = upper_bound_in_type (type, type);
2080 else
2081 tmax = TYPE_MAX_VALUE (type);
2083 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2085 min = tmin;
2086 max = tmax;
2088 /* For VARYING or UNDEFINED ranges, just about anything we get
2089 from scalar evolutions should be better. */
2091 if (dir == EV_DIR_DECREASES)
2092 max = init;
2093 else
2094 min = init;
2096 /* If we would create an invalid range, then just assume we
2097 know absolutely nothing. This may be over-conservative,
2098 but it's clearly safe, and should happen only in unreachable
2099 parts of code, or for invalid programs. */
2100 if (compare_values (min, max) == 1)
2101 return;
2103 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2105 else if (vr->type == VR_RANGE)
2107 min = vr->min;
2108 max = vr->max;
2110 if (dir == EV_DIR_DECREASES)
2112 /* INIT is the maximum value. If INIT is lower than VR->MAX
2113 but no smaller than VR->MIN, set VR->MAX to INIT. */
2114 if (compare_values (init, max) == -1)
2116 max = init;
2118 /* If we just created an invalid range with the minimum
2119 greater than the maximum, we fail conservatively.
2120 This should happen only in unreachable
2121 parts of code, or for invalid programs. */
2122 if (compare_values (min, max) == 1)
2123 return;
2126 else
2128 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
2129 if (compare_values (init, min) == 1)
2131 min = init;
2133 /* Again, avoid creating invalid range by failing. */
2134 if (compare_values (min, max) == 1)
2135 return;
2139 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
2144 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
2146 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
2147 all the values in the ranges.
2149 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
2151 - Return NULL_TREE if it is not always possible to determine the
2152 value of the comparison. */
2155 static tree
2156 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1)
2158 /* VARYING or UNDEFINED ranges cannot be compared. */
2159 if (vr0->type == VR_VARYING
2160 || vr0->type == VR_UNDEFINED
2161 || vr1->type == VR_VARYING
2162 || vr1->type == VR_UNDEFINED)
2163 return NULL_TREE;
2165 /* Anti-ranges need to be handled separately. */
2166 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
2168 /* If both are anti-ranges, then we cannot compute any
2169 comparison. */
2170 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
2171 return NULL_TREE;
2173 /* These comparisons are never statically computable. */
2174 if (comp == GT_EXPR
2175 || comp == GE_EXPR
2176 || comp == LT_EXPR
2177 || comp == LE_EXPR)
2178 return NULL_TREE;
2180 /* Equality can be computed only between a range and an
2181 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
2182 if (vr0->type == VR_RANGE)
2184 /* To simplify processing, make VR0 the anti-range. */
2185 value_range_t *tmp = vr0;
2186 vr0 = vr1;
2187 vr1 = tmp;
2190 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
2192 if (compare_values (vr0->min, vr1->min) == 0
2193 && compare_values (vr0->max, vr1->max) == 0)
2194 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2196 return NULL_TREE;
2199 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
2200 operands around and change the comparison code. */
2201 if (comp == GT_EXPR || comp == GE_EXPR)
2203 value_range_t *tmp;
2204 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
2205 tmp = vr0;
2206 vr0 = vr1;
2207 vr1 = tmp;
2210 if (comp == EQ_EXPR)
2212 /* Equality may only be computed if both ranges represent
2213 exactly one value. */
2214 if (compare_values (vr0->min, vr0->max) == 0
2215 && compare_values (vr1->min, vr1->max) == 0)
2217 int cmp_min = compare_values (vr0->min, vr1->min);
2218 int cmp_max = compare_values (vr0->max, vr1->max);
2219 if (cmp_min == 0 && cmp_max == 0)
2220 return boolean_true_node;
2221 else if (cmp_min != -2 && cmp_max != -2)
2222 return boolean_false_node;
2224 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
2225 else if (compare_values (vr0->min, vr1->max) == 1
2226 || compare_values (vr1->min, vr0->max) == 1)
2227 return boolean_false_node;
2229 return NULL_TREE;
2231 else if (comp == NE_EXPR)
2233 int cmp1, cmp2;
2235 /* If VR0 is completely to the left or completely to the right
2236 of VR1, they are always different. Notice that we need to
2237 make sure that both comparisons yield similar results to
2238 avoid comparing values that cannot be compared at
2239 compile-time. */
2240 cmp1 = compare_values (vr0->max, vr1->min);
2241 cmp2 = compare_values (vr0->min, vr1->max);
2242 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
2243 return boolean_true_node;
2245 /* If VR0 and VR1 represent a single value and are identical,
2246 return false. */
2247 else if (compare_values (vr0->min, vr0->max) == 0
2248 && compare_values (vr1->min, vr1->max) == 0
2249 && compare_values (vr0->min, vr1->min) == 0
2250 && compare_values (vr0->max, vr1->max) == 0)
2251 return boolean_false_node;
2253 /* Otherwise, they may or may not be different. */
2254 else
2255 return NULL_TREE;
2257 else if (comp == LT_EXPR || comp == LE_EXPR)
2259 int tst;
2261 /* If VR0 is to the left of VR1, return true. */
2262 tst = compare_values (vr0->max, vr1->min);
2263 if ((comp == LT_EXPR && tst == -1)
2264 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2265 return boolean_true_node;
2267 /* If VR0 is to the right of VR1, return false. */
2268 tst = compare_values (vr0->min, vr1->max);
2269 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2270 || (comp == LE_EXPR && tst == 1))
2271 return boolean_false_node;
2273 /* Otherwise, we don't know. */
2274 return NULL_TREE;
2277 gcc_unreachable ();
2281 /* Given a value range VR, a value VAL and a comparison code COMP, return
2282 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
2283 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
2284 always returns false. Return NULL_TREE if it is not always
2285 possible to determine the value of the comparison. */
2287 static tree
2288 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val)
2290 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
2291 return NULL_TREE;
2293 /* Anti-ranges need to be handled separately. */
2294 if (vr->type == VR_ANTI_RANGE)
2296 /* For anti-ranges, the only predicates that we can compute at
2297 compile time are equality and inequality. */
2298 if (comp == GT_EXPR
2299 || comp == GE_EXPR
2300 || comp == LT_EXPR
2301 || comp == LE_EXPR)
2302 return NULL_TREE;
2304 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
2305 if (value_inside_range (val, vr) == 1)
2306 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
2308 return NULL_TREE;
2311 if (comp == EQ_EXPR)
2313 /* EQ_EXPR may only be computed if VR represents exactly
2314 one value. */
2315 if (compare_values (vr->min, vr->max) == 0)
2317 int cmp = compare_values (vr->min, val);
2318 if (cmp == 0)
2319 return boolean_true_node;
2320 else if (cmp == -1 || cmp == 1 || cmp == 2)
2321 return boolean_false_node;
2323 else if (compare_values (val, vr->min) == -1
2324 || compare_values (vr->max, val) == -1)
2325 return boolean_false_node;
2327 return NULL_TREE;
2329 else if (comp == NE_EXPR)
2331 /* If VAL is not inside VR, then they are always different. */
2332 if (compare_values (vr->max, val) == -1
2333 || compare_values (vr->min, val) == 1)
2334 return boolean_true_node;
2336 /* If VR represents exactly one value equal to VAL, then return
2337 false. */
2338 if (compare_values (vr->min, vr->max) == 0
2339 && compare_values (vr->min, val) == 0)
2340 return boolean_false_node;
2342 /* Otherwise, they may or may not be different. */
2343 return NULL_TREE;
2345 else if (comp == LT_EXPR || comp == LE_EXPR)
2347 int tst;
2349 /* If VR is to the left of VAL, return true. */
2350 tst = compare_values (vr->max, val);
2351 if ((comp == LT_EXPR && tst == -1)
2352 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
2353 return boolean_true_node;
2355 /* If VR is to the right of VAL, return false. */
2356 tst = compare_values (vr->min, val);
2357 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
2358 || (comp == LE_EXPR && tst == 1))
2359 return boolean_false_node;
2361 /* Otherwise, we don't know. */
2362 return NULL_TREE;
2364 else if (comp == GT_EXPR || comp == GE_EXPR)
2366 int tst;
2368 /* If VR is to the right of VAL, return true. */
2369 tst = compare_values (vr->min, val);
2370 if ((comp == GT_EXPR && tst == 1)
2371 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
2372 return boolean_true_node;
2374 /* If VR is to the left of VAL, return false. */
2375 tst = compare_values (vr->max, val);
2376 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
2377 || (comp == GE_EXPR && tst == -1))
2378 return boolean_false_node;
2380 /* Otherwise, we don't know. */
2381 return NULL_TREE;
2384 gcc_unreachable ();
2388 /* Debugging dumps. */
2390 void dump_value_range (FILE *, value_range_t *);
2391 void debug_value_range (value_range_t *);
2392 void dump_all_value_ranges (FILE *);
2393 void debug_all_value_ranges (void);
2394 void dump_vr_equiv (FILE *, bitmap);
2395 void debug_vr_equiv (bitmap);
2398 /* Dump value range VR to FILE. */
2400 void
2401 dump_value_range (FILE *file, value_range_t *vr)
2403 if (vr == NULL)
2404 fprintf (file, "[]");
2405 else if (vr->type == VR_UNDEFINED)
2406 fprintf (file, "UNDEFINED");
2407 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
2409 tree type = TREE_TYPE (vr->min);
2411 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
2413 if (INTEGRAL_TYPE_P (type)
2414 && !TYPE_UNSIGNED (type)
2415 && vr->min == TYPE_MIN_VALUE (type))
2416 fprintf (file, "-INF");
2417 else
2418 print_generic_expr (file, vr->min, 0);
2420 fprintf (file, ", ");
2422 if (INTEGRAL_TYPE_P (type)
2423 && vr->max == TYPE_MAX_VALUE (type))
2424 fprintf (file, "+INF");
2425 else
2426 print_generic_expr (file, vr->max, 0);
2428 fprintf (file, "]");
2430 if (vr->equiv)
2432 bitmap_iterator bi;
2433 unsigned i, c = 0;
2435 fprintf (file, " EQUIVALENCES: { ");
2437 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
2439 print_generic_expr (file, ssa_name (i), 0);
2440 fprintf (file, " ");
2441 c++;
2444 fprintf (file, "} (%u elements)", c);
2447 else if (vr->type == VR_VARYING)
2448 fprintf (file, "VARYING");
2449 else
2450 fprintf (file, "INVALID RANGE");
2454 /* Dump value range VR to stderr. */
2456 void
2457 debug_value_range (value_range_t *vr)
2459 dump_value_range (stderr, vr);
2460 fprintf (stderr, "\n");
2464 /* Dump value ranges of all SSA_NAMEs to FILE. */
2466 void
2467 dump_all_value_ranges (FILE *file)
2469 size_t i;
2471 for (i = 0; i < num_ssa_names; i++)
2473 if (vr_value[i])
2475 print_generic_expr (file, ssa_name (i), 0);
2476 fprintf (file, ": ");
2477 dump_value_range (file, vr_value[i]);
2478 fprintf (file, "\n");
2482 fprintf (file, "\n");
2486 /* Dump all value ranges to stderr. */
2488 void
2489 debug_all_value_ranges (void)
2491 dump_all_value_ranges (stderr);
2495 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2496 create a new SSA name N and return the assertion assignment
2497 'V = ASSERT_EXPR <V, V OP W>'. */
2499 static tree
2500 build_assert_expr_for (tree cond, tree v)
2502 tree n, assertion;
2504 gcc_assert (TREE_CODE (v) == SSA_NAME);
2505 n = duplicate_ssa_name (v, NULL_TREE);
2507 if (COMPARISON_CLASS_P (cond))
2509 tree a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
2510 assertion = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (v), n, a);
2512 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
2514 /* Given !V, build the assignment N = false. */
2515 tree op0 = TREE_OPERAND (cond, 0);
2516 gcc_assert (op0 == v);
2517 assertion = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (v), n,
2518 boolean_false_node);
2520 else if (TREE_CODE (cond) == SSA_NAME)
2522 /* Given V, build the assignment N = true. */
2523 gcc_assert (v == cond);
2524 assertion = build2 (GIMPLE_MODIFY_STMT,
2525 TREE_TYPE (v), n, boolean_true_node);
2527 else
2528 gcc_unreachable ();
2530 SSA_NAME_DEF_STMT (n) = assertion;
2532 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2533 operand of the ASSERT_EXPR. Register the new name and the old one
2534 in the replacement table so that we can fix the SSA web after
2535 adding all the ASSERT_EXPRs. */
2536 register_new_name_mapping (n, v);
2538 return assertion;
2542 /* Return false if EXPR is a predicate expression involving floating
2543 point values. */
2545 static inline bool
2546 fp_predicate (tree expr)
2548 return (COMPARISON_CLASS_P (expr)
2549 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0))));
2553 /* If the range of values taken by OP can be inferred after STMT executes,
2554 return the comparison code (COMP_CODE_P) and value (VAL_P) that
2555 describes the inferred range. Return true if a range could be
2556 inferred. */
2558 static bool
2559 infer_value_range (tree stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
2561 *val_p = NULL_TREE;
2562 *comp_code_p = ERROR_MARK;
2564 /* Do not attempt to infer anything in names that flow through
2565 abnormal edges. */
2566 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
2567 return false;
2569 /* Similarly, don't infer anything from statements that may throw
2570 exceptions. */
2571 if (tree_could_throw_p (stmt))
2572 return false;
2574 /* If STMT is the last statement of a basic block with no
2575 successors, there is no point inferring anything about any of its
2576 operands. We would not be able to find a proper insertion point
2577 for the assertion, anyway. */
2578 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (bb_for_stmt (stmt)->succs) == 0)
2579 return false;
2581 /* We can only assume that a pointer dereference will yield
2582 non-NULL if -fdelete-null-pointer-checks is enabled. */
2583 if (flag_delete_null_pointer_checks && POINTER_TYPE_P (TREE_TYPE (op)))
2585 bool is_store;
2586 unsigned num_uses, num_derefs;
2588 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs, &is_store);
2589 if (num_derefs > 0)
2591 *val_p = build_int_cst (TREE_TYPE (op), 0);
2592 *comp_code_p = NE_EXPR;
2593 return true;
2597 return false;
2601 void dump_asserts_for (FILE *, tree);
2602 void debug_asserts_for (tree);
2603 void dump_all_asserts (FILE *);
2604 void debug_all_asserts (void);
2606 /* Dump all the registered assertions for NAME to FILE. */
2608 void
2609 dump_asserts_for (FILE *file, tree name)
2611 assert_locus_t loc;
2613 fprintf (file, "Assertions to be inserted for ");
2614 print_generic_expr (file, name, 0);
2615 fprintf (file, "\n");
2617 loc = asserts_for[SSA_NAME_VERSION (name)];
2618 while (loc)
2620 fprintf (file, "\t");
2621 print_generic_expr (file, bsi_stmt (loc->si), 0);
2622 fprintf (file, "\n\tBB #%d", loc->bb->index);
2623 if (loc->e)
2625 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
2626 loc->e->dest->index);
2627 dump_edge_info (file, loc->e, 0);
2629 fprintf (file, "\n\tPREDICATE: ");
2630 print_generic_expr (file, name, 0);
2631 fprintf (file, " %s ", tree_code_name[(int)loc->comp_code]);
2632 print_generic_expr (file, loc->val, 0);
2633 fprintf (file, "\n\n");
2634 loc = loc->next;
2637 fprintf (file, "\n");
2641 /* Dump all the registered assertions for NAME to stderr. */
2643 void
2644 debug_asserts_for (tree name)
2646 dump_asserts_for (stderr, name);
2650 /* Dump all the registered assertions for all the names to FILE. */
2652 void
2653 dump_all_asserts (FILE *file)
2655 unsigned i;
2656 bitmap_iterator bi;
2658 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
2659 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
2660 dump_asserts_for (file, ssa_name (i));
2661 fprintf (file, "\n");
2665 /* Dump all the registered assertions for all the names to stderr. */
2667 void
2668 debug_all_asserts (void)
2670 dump_all_asserts (stderr);
2674 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2675 'NAME COMP_CODE VAL' at a location that dominates block BB or
2676 E->DEST, then register this location as a possible insertion point
2677 for ASSERT_EXPR <NAME, NAME COMP_CODE VAL>.
2679 BB, E and SI provide the exact insertion point for the new
2680 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2681 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2682 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2683 must not be NULL. */
2685 static void
2686 register_new_assert_for (tree name,
2687 enum tree_code comp_code,
2688 tree val,
2689 basic_block bb,
2690 edge e,
2691 block_stmt_iterator si)
2693 assert_locus_t n, loc, last_loc;
2694 bool found;
2695 basic_block dest_bb;
2697 #if defined ENABLE_CHECKING
2698 gcc_assert (bb == NULL || e == NULL);
2700 if (e == NULL)
2701 gcc_assert (TREE_CODE (bsi_stmt (si)) != COND_EXPR
2702 && TREE_CODE (bsi_stmt (si)) != SWITCH_EXPR);
2703 #endif
2705 /* The new assertion A will be inserted at BB or E. We need to
2706 determine if the new location is dominated by a previously
2707 registered location for A. If we are doing an edge insertion,
2708 assume that A will be inserted at E->DEST. Note that this is not
2709 necessarily true.
2711 If E is a critical edge, it will be split. But even if E is
2712 split, the new block will dominate the same set of blocks that
2713 E->DEST dominates.
2715 The reverse, however, is not true, blocks dominated by E->DEST
2716 will not be dominated by the new block created to split E. So,
2717 if the insertion location is on a critical edge, we will not use
2718 the new location to move another assertion previously registered
2719 at a block dominated by E->DEST. */
2720 dest_bb = (bb) ? bb : e->dest;
2722 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2723 VAL at a block dominating DEST_BB, then we don't need to insert a new
2724 one. Similarly, if the same assertion already exists at a block
2725 dominated by DEST_BB and the new location is not on a critical
2726 edge, then update the existing location for the assertion (i.e.,
2727 move the assertion up in the dominance tree).
2729 Note, this is implemented as a simple linked list because there
2730 should not be more than a handful of assertions registered per
2731 name. If this becomes a performance problem, a table hashed by
2732 COMP_CODE and VAL could be implemented. */
2733 loc = asserts_for[SSA_NAME_VERSION (name)];
2734 last_loc = loc;
2735 found = false;
2736 while (loc)
2738 if (loc->comp_code == comp_code
2739 && (loc->val == val
2740 || operand_equal_p (loc->val, val, 0)))
2742 /* If the assertion NAME COMP_CODE VAL has already been
2743 registered at a basic block that dominates DEST_BB, then
2744 we don't need to insert the same assertion again. Note
2745 that we don't check strict dominance here to avoid
2746 replicating the same assertion inside the same basic
2747 block more than once (e.g., when a pointer is
2748 dereferenced several times inside a block).
2750 An exception to this rule are edge insertions. If the
2751 new assertion is to be inserted on edge E, then it will
2752 dominate all the other insertions that we may want to
2753 insert in DEST_BB. So, if we are doing an edge
2754 insertion, don't do this dominance check. */
2755 if (e == NULL
2756 && dominated_by_p (CDI_DOMINATORS, dest_bb, loc->bb))
2757 return;
2759 /* Otherwise, if E is not a critical edge and DEST_BB
2760 dominates the existing location for the assertion, move
2761 the assertion up in the dominance tree by updating its
2762 location information. */
2763 if ((e == NULL || !EDGE_CRITICAL_P (e))
2764 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
2766 loc->bb = dest_bb;
2767 loc->e = e;
2768 loc->si = si;
2769 return;
2773 /* Update the last node of the list and move to the next one. */
2774 last_loc = loc;
2775 loc = loc->next;
2778 /* If we didn't find an assertion already registered for
2779 NAME COMP_CODE VAL, add a new one at the end of the list of
2780 assertions associated with NAME. */
2781 n = XNEW (struct assert_locus_d);
2782 n->bb = dest_bb;
2783 n->e = e;
2784 n->si = si;
2785 n->comp_code = comp_code;
2786 n->val = val;
2787 n->next = NULL;
2789 if (last_loc)
2790 last_loc->next = n;
2791 else
2792 asserts_for[SSA_NAME_VERSION (name)] = n;
2794 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
2797 /* COND is a predicate which uses NAME. Extract a suitable test code
2798 and value and store them into *CODE_P and *VAL_P so the predicate
2799 is normalized to NAME *CODE_P *VAL_P.
2801 If no extraction was possible, return FALSE, otherwise return TRUE.
2803 If INVERT is true, then we invert the result stored into *CODE_P. */
2805 static bool
2806 extract_code_and_val_from_cond (tree name, tree cond, bool invert,
2807 enum tree_code *code_p, tree *val_p)
2809 enum tree_code comp_code;
2810 tree val;
2812 /* Predicates may be a single SSA name or NAME OP VAL. */
2813 if (cond == name)
2815 /* If the predicate is a name, it must be NAME, in which
2816 case we create the predicate NAME == true or
2817 NAME == false accordingly. */
2818 comp_code = EQ_EXPR;
2819 val = invert ? boolean_false_node : boolean_true_node;
2821 else
2823 /* Otherwise, we have a comparison of the form NAME COMP VAL
2824 or VAL COMP NAME. */
2825 if (name == TREE_OPERAND (cond, 1))
2827 /* If the predicate is of the form VAL COMP NAME, flip
2828 COMP around because we need to register NAME as the
2829 first operand in the predicate. */
2830 comp_code = swap_tree_comparison (TREE_CODE (cond));
2831 val = TREE_OPERAND (cond, 0);
2833 else
2835 /* The comparison is of the form NAME COMP VAL, so the
2836 comparison code remains unchanged. */
2837 comp_code = TREE_CODE (cond);
2838 val = TREE_OPERAND (cond, 1);
2841 /* Invert the comparison code as necessary. */
2842 if (invert)
2843 comp_code = invert_tree_comparison (comp_code, 0);
2845 /* VRP does not handle float types. */
2846 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
2847 return false;
2849 /* Do not register always-false predicates.
2850 FIXME: this works around a limitation in fold() when dealing with
2851 enumerations. Given 'enum { N1, N2 } x;', fold will not
2852 fold 'if (x > N2)' to 'if (0)'. */
2853 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
2854 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2856 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
2857 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
2859 if (comp_code == GT_EXPR
2860 && (!max
2861 || compare_values (val, max) == 0))
2862 return false;
2864 if (comp_code == LT_EXPR
2865 && (!min
2866 || compare_values (val, min) == 0))
2867 return false;
2870 *code_p = comp_code;
2871 *val_p = val;
2872 return true;
2875 /* OP is an operand of a truth value expression which is known to have
2876 a particular value. Register any asserts for OP and for any
2877 operands in OP's defining statement.
2879 If CODE is EQ_EXPR, then we want to register OP is zero (false),
2880 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
2882 static bool
2883 register_edge_assert_for_1 (tree op, enum tree_code code,
2884 edge e, block_stmt_iterator bsi)
2886 bool retval = false;
2887 tree op_def, rhs, val;
2889 /* We only care about SSA_NAMEs. */
2890 if (TREE_CODE (op) != SSA_NAME)
2891 return false;
2893 /* We know that OP will have a zero or nonzero value. If OP is used
2894 more than once go ahead and register an assert for OP.
2896 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
2897 it will always be set for OP (because OP is used in a COND_EXPR in
2898 the subgraph). */
2899 if (!has_single_use (op))
2901 val = build_int_cst (TREE_TYPE (op), 0);
2902 register_new_assert_for (op, code, val, NULL, e, bsi);
2903 retval = true;
2906 /* Now look at how OP is set. If it's set from a comparison,
2907 a truth operation or some bit operations, then we may be able
2908 to register information about the operands of that assignment. */
2909 op_def = SSA_NAME_DEF_STMT (op);
2910 if (TREE_CODE (op_def) != GIMPLE_MODIFY_STMT)
2911 return retval;
2913 rhs = GIMPLE_STMT_OPERAND (op_def, 1);
2915 if (COMPARISON_CLASS_P (rhs))
2917 bool invert = (code == EQ_EXPR ? true : false);
2918 tree op0 = TREE_OPERAND (rhs, 0);
2919 tree op1 = TREE_OPERAND (rhs, 1);
2921 /* Conditionally register an assert for each SSA_NAME in the
2922 comparison. */
2923 if (TREE_CODE (op0) == SSA_NAME
2924 && !has_single_use (op0)
2925 && extract_code_and_val_from_cond (op0, rhs,
2926 invert, &code, &val))
2928 register_new_assert_for (op0, code, val, NULL, e, bsi);
2929 retval = true;
2932 /* Similarly for the second operand of the comparison. */
2933 if (TREE_CODE (op1) == SSA_NAME
2934 && !has_single_use (op1)
2935 && extract_code_and_val_from_cond (op1, rhs,
2936 invert, &code, &val))
2938 register_new_assert_for (op1, code, val, NULL, e, bsi);
2939 retval = true;
2942 else if ((code == NE_EXPR
2943 && (TREE_CODE (rhs) == TRUTH_AND_EXPR
2944 || TREE_CODE (rhs) == BIT_AND_EXPR))
2945 || (code == EQ_EXPR
2946 && (TREE_CODE (rhs) == TRUTH_OR_EXPR
2947 || TREE_CODE (rhs) == BIT_IOR_EXPR)))
2949 /* Recurse on each operand. */
2950 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 0),
2951 code, e, bsi);
2952 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 1),
2953 code, e, bsi);
2955 else if (TREE_CODE (rhs) == TRUTH_NOT_EXPR)
2957 /* Recurse, flipping CODE. */
2958 code = invert_tree_comparison (code, false);
2959 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 0),
2960 code, e, bsi);
2962 else if (TREE_CODE (rhs) == SSA_NAME)
2964 /* Recurse through the copy. */
2965 retval |= register_edge_assert_for_1 (rhs, code, e, bsi);
2967 else if (TREE_CODE (rhs) == NOP_EXPR
2968 || TREE_CODE (rhs) == CONVERT_EXPR
2969 || TREE_CODE (rhs) == VIEW_CONVERT_EXPR
2970 || TREE_CODE (rhs) == NON_LVALUE_EXPR)
2972 /* Recurse through the type conversion. */
2973 retval |= register_edge_assert_for_1 (TREE_OPERAND (rhs, 0),
2974 code, e, bsi);
2977 return retval;
2980 /* Try to register an edge assertion for SSA name NAME on edge E for
2981 the condition COND contributing to the conditional jump pointed to by SI.
2982 Return true if an assertion for NAME could be registered. */
2984 static bool
2985 register_edge_assert_for (tree name, edge e, block_stmt_iterator si, tree cond)
2987 tree val;
2988 enum tree_code comp_code;
2989 bool retval = false;
2990 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
2992 /* Do not attempt to infer anything in names that flow through
2993 abnormal edges. */
2994 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
2995 return false;
2997 if (!extract_code_and_val_from_cond (name, cond, is_else_edge,
2998 &comp_code, &val))
2999 return false;
3001 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
3002 reachable from E. */
3003 if (TEST_BIT (found_in_subgraph, SSA_NAME_VERSION (name)))
3005 register_new_assert_for (name, comp_code, val, NULL, e, si);
3006 retval = true;
3009 /* If COND is effectively an equality test of an SSA_NAME against
3010 the value zero or one, then we may be able to assert values
3011 for SSA_NAMEs which flow into COND. */
3013 /* In the case of NAME == 1 or NAME != 0, for TRUTH_AND_EXPR defining
3014 statement of NAME we can assert both operands of the TRUTH_AND_EXPR
3015 have nonzero value. */
3016 if (((comp_code == EQ_EXPR && integer_onep (val))
3017 || (comp_code == NE_EXPR && integer_zerop (val))))
3019 tree def_stmt = SSA_NAME_DEF_STMT (name);
3021 if (TREE_CODE (def_stmt) == GIMPLE_MODIFY_STMT
3022 && (TREE_CODE (GIMPLE_STMT_OPERAND (def_stmt, 1)) == TRUTH_AND_EXPR
3023 || TREE_CODE (GIMPLE_STMT_OPERAND (def_stmt, 1)) == BIT_AND_EXPR))
3025 tree op0 = TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1), 0);
3026 tree op1 = TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1), 1);
3027 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
3028 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
3032 /* In the case of NAME == 0 or NAME != 1, for TRUTH_OR_EXPR defining
3033 statement of NAME we can assert both operands of the TRUTH_OR_EXPR
3034 have zero value. */
3035 if (((comp_code == EQ_EXPR && integer_zerop (val))
3036 || (comp_code == NE_EXPR && integer_onep (val))))
3038 tree def_stmt = SSA_NAME_DEF_STMT (name);
3040 if (TREE_CODE (def_stmt) == GIMPLE_MODIFY_STMT
3041 && (TREE_CODE (GIMPLE_STMT_OPERAND (def_stmt, 1)) == TRUTH_OR_EXPR
3042 || TREE_CODE (GIMPLE_STMT_OPERAND (def_stmt, 1)) == BIT_IOR_EXPR))
3044 tree op0 = TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1), 0);
3045 tree op1 = TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1), 1);
3046 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
3047 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
3051 return retval;
3055 static bool find_assert_locations (basic_block bb);
3057 /* Determine whether the outgoing edges of BB should receive an
3058 ASSERT_EXPR for each of the operands of BB's LAST statement.
3059 The last statement of BB must be a COND_EXPR or a SWITCH_EXPR.
3061 If any of the sub-graphs rooted at BB have an interesting use of
3062 the predicate operands, an assert location node is added to the
3063 list of assertions for the corresponding operands. */
3065 static bool
3066 find_conditional_asserts (basic_block bb, tree last)
3068 bool need_assert;
3069 block_stmt_iterator bsi;
3070 tree op;
3071 edge_iterator ei;
3072 edge e;
3073 ssa_op_iter iter;
3075 need_assert = false;
3076 bsi = bsi_for_stmt (last);
3078 /* Look for uses of the operands in each of the sub-graphs
3079 rooted at BB. We need to check each of the outgoing edges
3080 separately, so that we know what kind of ASSERT_EXPR to
3081 insert. */
3082 FOR_EACH_EDGE (e, ei, bb->succs)
3084 if (e->dest == bb)
3085 continue;
3087 /* Remove the COND_EXPR operands from the FOUND_IN_SUBGRAPH bitmap.
3088 Otherwise, when we finish traversing each of the sub-graphs, we
3089 won't know whether the variables were found in the sub-graphs or
3090 if they had been found in a block upstream from BB.
3092 This is actually a bad idea is some cases, particularly jump
3093 threading. Consider a CFG like the following:
3103 Assume that one or more operands in the conditional at the
3104 end of block 0 are used in a conditional in block 2, but not
3105 anywhere in block 1. In this case we will not insert any
3106 assert statements in block 1, which may cause us to miss
3107 opportunities to optimize, particularly for jump threading. */
3108 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3109 RESET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3111 /* Traverse the strictly dominated sub-graph rooted at E->DEST
3112 to determine if any of the operands in the conditional
3113 predicate are used. */
3114 if (e->dest != bb)
3115 need_assert |= find_assert_locations (e->dest);
3117 /* Register the necessary assertions for each operand in the
3118 conditional predicate. */
3119 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3120 need_assert |= register_edge_assert_for (op, e, bsi,
3121 COND_EXPR_COND (last));
3124 /* Finally, indicate that we have found the operands in the
3125 conditional. */
3126 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3127 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3129 return need_assert;
3133 /* Traverse all the statements in block BB looking for statements that
3134 may generate useful assertions for the SSA names in their operand.
3135 If a statement produces a useful assertion A for name N_i, then the
3136 list of assertions already generated for N_i is scanned to
3137 determine if A is actually needed.
3139 If N_i already had the assertion A at a location dominating the
3140 current location, then nothing needs to be done. Otherwise, the
3141 new location for A is recorded instead.
3143 1- For every statement S in BB, all the variables used by S are
3144 added to bitmap FOUND_IN_SUBGRAPH.
3146 2- If statement S uses an operand N in a way that exposes a known
3147 value range for N, then if N was not already generated by an
3148 ASSERT_EXPR, create a new assert location for N. For instance,
3149 if N is a pointer and the statement dereferences it, we can
3150 assume that N is not NULL.
3152 3- COND_EXPRs are a special case of #2. We can derive range
3153 information from the predicate but need to insert different
3154 ASSERT_EXPRs for each of the sub-graphs rooted at the
3155 conditional block. If the last statement of BB is a conditional
3156 expression of the form 'X op Y', then
3158 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3160 b) If the conditional is the only entry point to the sub-graph
3161 corresponding to the THEN_CLAUSE, recurse into it. On
3162 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3163 an ASSERT_EXPR is added for the corresponding variable.
3165 c) Repeat step (b) on the ELSE_CLAUSE.
3167 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3169 For instance,
3171 if (a == 9)
3172 b = a;
3173 else
3174 b = c + 1;
3176 In this case, an assertion on the THEN clause is useful to
3177 determine that 'a' is always 9 on that edge. However, an assertion
3178 on the ELSE clause would be unnecessary.
3180 4- If BB does not end in a conditional expression, then we recurse
3181 into BB's dominator children.
3183 At the end of the recursive traversal, every SSA name will have a
3184 list of locations where ASSERT_EXPRs should be added. When a new
3185 location for name N is found, it is registered by calling
3186 register_new_assert_for. That function keeps track of all the
3187 registered assertions to prevent adding unnecessary assertions.
3188 For instance, if a pointer P_4 is dereferenced more than once in a
3189 dominator tree, only the location dominating all the dereference of
3190 P_4 will receive an ASSERT_EXPR.
3192 If this function returns true, then it means that there are names
3193 for which we need to generate ASSERT_EXPRs. Those assertions are
3194 inserted by process_assert_insertions.
3196 TODO. Handle SWITCH_EXPR. */
3198 static bool
3199 find_assert_locations (basic_block bb)
3201 block_stmt_iterator si;
3202 tree last, phi;
3203 bool need_assert;
3204 basic_block son;
3206 if (TEST_BIT (blocks_visited, bb->index))
3207 return false;
3209 SET_BIT (blocks_visited, bb->index);
3211 need_assert = false;
3213 /* Traverse all PHI nodes in BB marking used operands. */
3214 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3216 use_operand_p arg_p;
3217 ssa_op_iter i;
3219 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
3221 tree arg = USE_FROM_PTR (arg_p);
3222 if (TREE_CODE (arg) == SSA_NAME)
3224 gcc_assert (is_gimple_reg (PHI_RESULT (phi)));
3225 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (arg));
3230 /* Traverse all the statements in BB marking used names and looking
3231 for statements that may infer assertions for their used operands. */
3232 last = NULL_TREE;
3233 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3235 tree stmt, op;
3236 ssa_op_iter i;
3238 stmt = bsi_stmt (si);
3240 /* See if we can derive an assertion for any of STMT's operands. */
3241 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
3243 tree value;
3244 enum tree_code comp_code;
3246 /* Mark OP in bitmap FOUND_IN_SUBGRAPH. If STMT is inside
3247 the sub-graph of a conditional block, when we return from
3248 this recursive walk, our parent will use the
3249 FOUND_IN_SUBGRAPH bitset to determine if one of the
3250 operands it was looking for was present in the sub-graph. */
3251 SET_BIT (found_in_subgraph, SSA_NAME_VERSION (op));
3253 /* If OP is used in such a way that we can infer a value
3254 range for it, and we don't find a previous assertion for
3255 it, create a new assertion location node for OP. */
3256 if (infer_value_range (stmt, op, &comp_code, &value))
3258 /* If we are able to infer a nonzero value range for OP,
3259 then walk backwards through the use-def chain to see if OP
3260 was set via a typecast.
3262 If so, then we can also infer a nonzero value range
3263 for the operand of the NOP_EXPR. */
3264 if (comp_code == NE_EXPR && integer_zerop (value))
3266 tree t = op;
3267 tree def_stmt = SSA_NAME_DEF_STMT (t);
3269 while (TREE_CODE (def_stmt) == GIMPLE_MODIFY_STMT
3270 && TREE_CODE
3271 (GIMPLE_STMT_OPERAND (def_stmt, 1)) == NOP_EXPR
3272 && TREE_CODE
3273 (TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1),
3274 0)) == SSA_NAME
3275 && POINTER_TYPE_P
3276 (TREE_TYPE (TREE_OPERAND
3277 (GIMPLE_STMT_OPERAND (def_stmt,
3278 1), 0))))
3280 t = TREE_OPERAND (GIMPLE_STMT_OPERAND (def_stmt, 1), 0);
3281 def_stmt = SSA_NAME_DEF_STMT (t);
3283 /* Note we want to register the assert for the
3284 operand of the NOP_EXPR after SI, not after the
3285 conversion. */
3286 if (! has_single_use (t))
3288 register_new_assert_for (t, comp_code, value,
3289 bb, NULL, si);
3290 need_assert = true;
3295 /* If OP is used only once, namely in this STMT, don't
3296 bother creating an ASSERT_EXPR for it. Such an
3297 ASSERT_EXPR would do nothing but increase compile time. */
3298 if (!has_single_use (op))
3300 register_new_assert_for (op, comp_code, value, bb, NULL, si);
3301 need_assert = true;
3306 /* Remember the last statement of the block. */
3307 last = stmt;
3310 /* If BB's last statement is a conditional expression
3311 involving integer operands, recurse into each of the sub-graphs
3312 rooted at BB to determine if we need to add ASSERT_EXPRs. */
3313 if (last
3314 && TREE_CODE (last) == COND_EXPR
3315 && !fp_predicate (COND_EXPR_COND (last))
3316 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
3317 need_assert |= find_conditional_asserts (bb, last);
3319 /* Recurse into the dominator children of BB. */
3320 for (son = first_dom_son (CDI_DOMINATORS, bb);
3321 son;
3322 son = next_dom_son (CDI_DOMINATORS, son))
3323 need_assert |= find_assert_locations (son);
3325 return need_assert;
3329 /* Create an ASSERT_EXPR for NAME and insert it in the location
3330 indicated by LOC. Return true if we made any edge insertions. */
3332 static bool
3333 process_assert_insertions_for (tree name, assert_locus_t loc)
3335 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3336 tree stmt, cond, assert_expr;
3337 edge_iterator ei;
3338 edge e;
3340 cond = build2 (loc->comp_code, boolean_type_node, name, loc->val);
3341 assert_expr = build_assert_expr_for (cond, name);
3343 if (loc->e)
3345 /* We have been asked to insert the assertion on an edge. This
3346 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3347 #if defined ENABLE_CHECKING
3348 gcc_assert (TREE_CODE (bsi_stmt (loc->si)) == COND_EXPR
3349 || TREE_CODE (bsi_stmt (loc->si)) == SWITCH_EXPR);
3350 #endif
3352 bsi_insert_on_edge (loc->e, assert_expr);
3353 return true;
3356 /* Otherwise, we can insert right after LOC->SI iff the
3357 statement must not be the last statement in the block. */
3358 stmt = bsi_stmt (loc->si);
3359 if (!stmt_ends_bb_p (stmt))
3361 bsi_insert_after (&loc->si, assert_expr, BSI_SAME_STMT);
3362 return false;
3365 /* If STMT must be the last statement in BB, we can only insert new
3366 assertions on the non-abnormal edge out of BB. Note that since
3367 STMT is not control flow, there may only be one non-abnormal edge
3368 out of BB. */
3369 FOR_EACH_EDGE (e, ei, loc->bb->succs)
3370 if (!(e->flags & EDGE_ABNORMAL))
3372 bsi_insert_on_edge (e, assert_expr);
3373 return true;
3376 gcc_unreachable ();
3380 /* Process all the insertions registered for every name N_i registered
3381 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3382 found in ASSERTS_FOR[i]. */
3384 static void
3385 process_assert_insertions (void)
3387 unsigned i;
3388 bitmap_iterator bi;
3389 bool update_edges_p = false;
3390 int num_asserts = 0;
3392 if (dump_file && (dump_flags & TDF_DETAILS))
3393 dump_all_asserts (dump_file);
3395 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3397 assert_locus_t loc = asserts_for[i];
3398 gcc_assert (loc);
3400 while (loc)
3402 assert_locus_t next = loc->next;
3403 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
3404 free (loc);
3405 loc = next;
3406 num_asserts++;
3410 if (update_edges_p)
3411 bsi_commit_edge_inserts ();
3413 if (dump_file && (dump_flags & TDF_STATS))
3414 fprintf (dump_file, "\nNumber of ASSERT_EXPR expressions inserted: %d\n\n",
3415 num_asserts);
3419 /* Traverse the flowgraph looking for conditional jumps to insert range
3420 expressions. These range expressions are meant to provide information
3421 to optimizations that need to reason in terms of value ranges. They
3422 will not be expanded into RTL. For instance, given:
3424 x = ...
3425 y = ...
3426 if (x < y)
3427 y = x - 2;
3428 else
3429 x = y + 3;
3431 this pass will transform the code into:
3433 x = ...
3434 y = ...
3435 if (x < y)
3437 x = ASSERT_EXPR <x, x < y>
3438 y = x - 2
3440 else
3442 y = ASSERT_EXPR <y, x <= y>
3443 x = y + 3
3446 The idea is that once copy and constant propagation have run, other
3447 optimizations will be able to determine what ranges of values can 'x'
3448 take in different paths of the code, simply by checking the reaching
3449 definition of 'x'. */
3451 static void
3452 insert_range_assertions (void)
3454 edge e;
3455 edge_iterator ei;
3456 bool update_ssa_p;
3458 found_in_subgraph = sbitmap_alloc (num_ssa_names);
3459 sbitmap_zero (found_in_subgraph);
3461 blocks_visited = sbitmap_alloc (last_basic_block);
3462 sbitmap_zero (blocks_visited);
3464 need_assert_for = BITMAP_ALLOC (NULL);
3465 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
3467 calculate_dominance_info (CDI_DOMINATORS);
3469 update_ssa_p = false;
3470 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3471 if (find_assert_locations (e->dest))
3472 update_ssa_p = true;
3474 if (update_ssa_p)
3476 process_assert_insertions ();
3477 update_ssa (TODO_update_ssa_no_phi);
3480 if (dump_file && (dump_flags & TDF_DETAILS))
3482 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
3483 dump_function_to_file (current_function_decl, dump_file, dump_flags);
3486 sbitmap_free (found_in_subgraph);
3487 free (asserts_for);
3488 BITMAP_FREE (need_assert_for);
3491 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
3492 and "struct" hacks. If VRP can determine that the
3493 array subscript is a constant, check if it is outside valid
3494 range. If the array subscript is a RANGE, warn if it is
3495 non-overlapping with valid range.
3496 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
3498 static void
3499 check_array_ref (tree ref, location_t* locus, bool ignore_off_by_one)
3501 value_range_t* vr = NULL;
3502 tree low_sub, up_sub;
3503 tree low_bound, up_bound = array_ref_up_bound (ref);
3505 low_sub = up_sub = TREE_OPERAND (ref, 1);
3507 if (!up_bound || !locus || TREE_NO_WARNING (ref)
3508 || TREE_CODE (up_bound) != INTEGER_CST
3509 /* Can not check flexible arrays. */
3510 || (TYPE_SIZE (TREE_TYPE (ref)) == NULL_TREE
3511 && TYPE_DOMAIN (TREE_TYPE (ref)) != NULL_TREE
3512 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (ref))) == NULL_TREE)
3513 /* Accesses after the end of arrays of size 0 (gcc
3514 extension) and 1 are likely intentional ("struct
3515 hack"). */
3516 || compare_tree_int (up_bound, 1) <= 0)
3517 return;
3519 low_bound = array_ref_low_bound (ref);
3521 if (TREE_CODE (low_sub) == SSA_NAME)
3523 vr = get_value_range (low_sub);
3524 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
3526 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
3527 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
3531 if (vr && vr->type == VR_ANTI_RANGE)
3533 if (TREE_CODE (up_sub) == INTEGER_CST
3534 && tree_int_cst_lt (up_bound, up_sub)
3535 && TREE_CODE (low_sub) == INTEGER_CST
3536 && tree_int_cst_lt (low_sub, low_bound))
3538 warning (OPT_Warray_bounds,
3539 "%Harray subscript is outside array bounds", locus);
3540 TREE_NO_WARNING (ref) = 1;
3543 else if (TREE_CODE (up_sub) == INTEGER_CST
3544 && tree_int_cst_lt (up_bound, up_sub)
3545 && !tree_int_cst_equal (up_bound, up_sub)
3546 && (!ignore_off_by_one
3547 || !tree_int_cst_equal (int_const_binop (PLUS_EXPR,
3548 up_bound,
3549 integer_one_node,
3551 up_sub)))
3553 warning (OPT_Warray_bounds, "%Harray subscript is above array bounds",
3554 locus);
3555 TREE_NO_WARNING (ref) = 1;
3557 else if (TREE_CODE (low_sub) == INTEGER_CST
3558 && tree_int_cst_lt (low_sub, low_bound))
3560 warning (OPT_Warray_bounds, "%Harray subscript is below array bounds",
3561 locus);
3562 TREE_NO_WARNING (ref) = 1;
3566 /* walk_tree() callback that checks if *TP is
3567 an ARRAY_REF inside an ADDR_EXPR (in which an array
3568 subscript one outside the valid range is allowed). Call
3569 check_array_ref for each ARRAY_REF found. The location is
3570 passed in DATA. */
3572 static tree
3573 check_array_bounds (tree *tp, int *walk_subtree, void *data)
3575 tree t = *tp;
3576 tree stmt = (tree)data;
3577 location_t *location = EXPR_LOCUS (stmt);
3579 *walk_subtree = TRUE;
3581 if (TREE_CODE (t) == ARRAY_REF)
3582 check_array_ref (t, location, false /*ignore_off_by_one*/);
3583 else if (TREE_CODE (t) == ADDR_EXPR)
3585 use_operand_p op;
3586 tree use_stmt;
3587 t = TREE_OPERAND (t, 0);
3589 /* Don't warn on statements like
3591 ssa_name = 500 + &array[-200]
3595 ssa_name = &array[-200]
3596 other_name = ssa_name + 300;
3598 which are sometimes
3599 produced by other optimizing passes. */
3601 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3602 && BINARY_CLASS_P (GIMPLE_STMT_OPERAND (stmt, 1)))
3603 *walk_subtree = FALSE;
3605 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3606 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
3607 && single_imm_use (GIMPLE_STMT_OPERAND (stmt, 0), &op, &use_stmt)
3608 && TREE_CODE (use_stmt) == GIMPLE_MODIFY_STMT
3609 && BINARY_CLASS_P (GIMPLE_STMT_OPERAND (use_stmt, 1)))
3610 *walk_subtree = FALSE;
3612 while (*walk_subtree && handled_component_p (t))
3614 if (TREE_CODE (t) == ARRAY_REF)
3615 check_array_ref (t, location, true /*ignore_off_by_one*/);
3616 t = TREE_OPERAND (t, 0);
3618 *walk_subtree = FALSE;
3621 return NULL_TREE;
3624 /* Walk over all statements of all reachable BBs and call check_array_bounds
3625 on them. */
3627 static void
3628 check_all_array_refs (void)
3630 basic_block bb;
3631 block_stmt_iterator si;
3633 FOR_EACH_BB (bb)
3635 /* Skip bb's that are clearly unreachable. */
3636 if (single_pred_p (bb))
3638 basic_block pred_bb = EDGE_PRED (bb, 0)->src;
3639 tree ls = NULL_TREE;
3641 if (!bsi_end_p (bsi_last (pred_bb)))
3642 ls = bsi_stmt (bsi_last (pred_bb));
3644 if (ls && TREE_CODE (ls) == COND_EXPR
3645 && ((COND_EXPR_COND (ls) == boolean_false_node
3646 && (EDGE_PRED (bb, 0)->flags & EDGE_TRUE_VALUE))
3647 || (COND_EXPR_COND (ls) == boolean_true_node
3648 && (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE))))
3649 continue;
3651 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3652 walk_tree (bsi_stmt_ptr (si), check_array_bounds,
3653 bsi_stmt (si), NULL);
3657 /* Convert range assertion expressions into the implied copies and
3658 copy propagate away the copies. Doing the trivial copy propagation
3659 here avoids the need to run the full copy propagation pass after
3660 VRP.
3662 FIXME, this will eventually lead to copy propagation removing the
3663 names that had useful range information attached to them. For
3664 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
3665 then N_i will have the range [3, +INF].
3667 However, by converting the assertion into the implied copy
3668 operation N_i = N_j, we will then copy-propagate N_j into the uses
3669 of N_i and lose the range information. We may want to hold on to
3670 ASSERT_EXPRs a little while longer as the ranges could be used in
3671 things like jump threading.
3673 The problem with keeping ASSERT_EXPRs around is that passes after
3674 VRP need to handle them appropriately.
3676 Another approach would be to make the range information a first
3677 class property of the SSA_NAME so that it can be queried from
3678 any pass. This is made somewhat more complex by the need for
3679 multiple ranges to be associated with one SSA_NAME. */
3681 static void
3682 remove_range_assertions (void)
3684 basic_block bb;
3685 block_stmt_iterator si;
3687 /* Note that the BSI iterator bump happens at the bottom of the
3688 loop and no bump is necessary if we're removing the statement
3689 referenced by the current BSI. */
3690 FOR_EACH_BB (bb)
3691 for (si = bsi_start (bb); !bsi_end_p (si);)
3693 tree stmt = bsi_stmt (si);
3694 tree use_stmt;
3696 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3697 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == ASSERT_EXPR)
3699 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1), var;
3700 tree cond = fold (ASSERT_EXPR_COND (rhs));
3701 use_operand_p use_p;
3702 imm_use_iterator iter;
3704 gcc_assert (cond != boolean_false_node);
3706 /* Propagate the RHS into every use of the LHS. */
3707 var = ASSERT_EXPR_VAR (rhs);
3708 FOR_EACH_IMM_USE_STMT (use_stmt, iter,
3709 GIMPLE_STMT_OPERAND (stmt, 0))
3710 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3712 SET_USE (use_p, var);
3713 gcc_assert (TREE_CODE (var) == SSA_NAME);
3716 /* And finally, remove the copy, it is not needed. */
3717 bsi_remove (&si, true);
3718 release_defs (stmt);
3720 else
3721 bsi_next (&si);
3724 sbitmap_free (blocks_visited);
3728 /* Return true if STMT is interesting for VRP. */
3730 static bool
3731 stmt_interesting_for_vrp (tree stmt)
3733 if (TREE_CODE (stmt) == PHI_NODE
3734 && is_gimple_reg (PHI_RESULT (stmt))
3735 && (INTEGRAL_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))
3736 || POINTER_TYPE_P (TREE_TYPE (PHI_RESULT (stmt)))))
3737 return true;
3738 else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
3740 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3741 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3743 /* In general, assignments with virtual operands are not useful
3744 for deriving ranges, with the obvious exception of calls to
3745 builtin functions. */
3746 if (TREE_CODE (lhs) == SSA_NAME
3747 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
3748 || POINTER_TYPE_P (TREE_TYPE (lhs)))
3749 && ((TREE_CODE (rhs) == CALL_EXPR
3750 && TREE_CODE (CALL_EXPR_FN (rhs)) == ADDR_EXPR
3751 && DECL_P (TREE_OPERAND (CALL_EXPR_FN (rhs), 0))
3752 && DECL_IS_BUILTIN (TREE_OPERAND (CALL_EXPR_FN (rhs), 0)))
3753 || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS)))
3754 return true;
3756 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
3757 return true;
3759 return false;
3763 /* Initialize local data structures for VRP. */
3765 static void
3766 vrp_initialize (void)
3768 basic_block bb;
3770 vr_value = XCNEWVEC (value_range_t *, num_ssa_names);
3772 FOR_EACH_BB (bb)
3774 block_stmt_iterator si;
3775 tree phi;
3777 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3779 if (!stmt_interesting_for_vrp (phi))
3781 tree lhs = PHI_RESULT (phi);
3782 set_value_range_to_varying (get_value_range (lhs));
3783 DONT_SIMULATE_AGAIN (phi) = true;
3785 else
3786 DONT_SIMULATE_AGAIN (phi) = false;
3789 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
3791 tree stmt = bsi_stmt (si);
3793 if (!stmt_interesting_for_vrp (stmt))
3795 ssa_op_iter i;
3796 tree def;
3797 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
3798 set_value_range_to_varying (get_value_range (def));
3799 DONT_SIMULATE_AGAIN (stmt) = true;
3801 else
3803 DONT_SIMULATE_AGAIN (stmt) = false;
3810 /* Visit assignment STMT. If it produces an interesting range, record
3811 the SSA name in *OUTPUT_P. */
3813 static enum ssa_prop_result
3814 vrp_visit_assignment (tree stmt, tree *output_p)
3816 tree lhs, rhs, def;
3817 ssa_op_iter iter;
3819 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3820 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3822 /* We only keep track of ranges in integral and pointer types. */
3823 if (TREE_CODE (lhs) == SSA_NAME
3824 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
3825 /* It is valid to have NULL MIN/MAX values on a type. See
3826 build_range_type. */
3827 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
3828 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
3829 || POINTER_TYPE_P (TREE_TYPE (lhs))))
3831 struct loop *l;
3832 value_range_t new_vr = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
3834 extract_range_from_expr (&new_vr, rhs);
3836 /* If STMT is inside a loop, we may be able to know something
3837 else about the range of LHS by examining scalar evolution
3838 information. */
3839 if (current_loops && (l = loop_containing_stmt (stmt)))
3840 adjust_range_with_scev (&new_vr, l, stmt, lhs);
3842 if (update_value_range (lhs, &new_vr))
3844 *output_p = lhs;
3846 if (dump_file && (dump_flags & TDF_DETAILS))
3848 fprintf (dump_file, "Found new range for ");
3849 print_generic_expr (dump_file, lhs, 0);
3850 fprintf (dump_file, ": ");
3851 dump_value_range (dump_file, &new_vr);
3852 fprintf (dump_file, "\n\n");
3855 if (new_vr.type == VR_VARYING)
3856 return SSA_PROP_VARYING;
3858 return SSA_PROP_INTERESTING;
3861 return SSA_PROP_NOT_INTERESTING;
3864 /* Every other statement produces no useful ranges. */
3865 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
3866 set_value_range_to_varying (get_value_range (def));
3868 return SSA_PROP_VARYING;
3872 /* Compare all the value ranges for names equivalent to VAR with VAL
3873 using comparison code COMP. Return the same value returned by
3874 compare_range_with_value. */
3876 static tree
3877 compare_name_with_value (enum tree_code comp, tree var, tree val)
3879 bitmap_iterator bi;
3880 unsigned i;
3881 bitmap e;
3882 tree retval, t;
3884 t = retval = NULL_TREE;
3886 /* Get the set of equivalences for VAR. */
3887 e = get_value_range (var)->equiv;
3889 /* Add VAR to its own set of equivalences so that VAR's value range
3890 is processed by this loop (otherwise, we would have to replicate
3891 the body of the loop just to check VAR's value range). */
3892 bitmap_set_bit (e, SSA_NAME_VERSION (var));
3894 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
3896 value_range_t equiv_vr = *(vr_value[i]);
3898 /* If name N_i does not have a valid range, use N_i as its own
3899 range. This allows us to compare against names that may
3900 have N_i in their ranges. */
3901 if (equiv_vr.type == VR_VARYING || equiv_vr.type == VR_UNDEFINED)
3903 equiv_vr.type = VR_RANGE;
3904 equiv_vr.min = ssa_name (i);
3905 equiv_vr.max = ssa_name (i);
3908 t = compare_range_with_value (comp, &equiv_vr, val);
3909 if (t)
3911 /* If we get different answers from different members
3912 of the equivalence set this check must be in a dead
3913 code region. Folding it to a trap representation
3914 would be correct here. For now just return don't-know. */
3915 if (retval != NULL
3916 && t != retval)
3918 retval = NULL_TREE;
3919 break;
3921 retval = t;
3925 /* Remove VAR from its own equivalence set. */
3926 bitmap_clear_bit (e, SSA_NAME_VERSION (var));
3928 if (retval)
3929 return retval;
3931 /* We couldn't find a non-NULL value for the predicate. */
3932 return NULL_TREE;
3936 /* Given a comparison code COMP and names N1 and N2, compare all the
3937 ranges equivalent to N1 against all the ranges equivalent to N2
3938 to determine the value of N1 COMP N2. Return the same value
3939 returned by compare_ranges. */
3941 static tree
3942 compare_names (enum tree_code comp, tree n1, tree n2)
3944 tree t, retval;
3945 bitmap e1, e2;
3946 bitmap_iterator bi1, bi2;
3947 unsigned i1, i2;
3949 /* Compare the ranges of every name equivalent to N1 against the
3950 ranges of every name equivalent to N2. */
3951 e1 = get_value_range (n1)->equiv;
3952 e2 = get_value_range (n2)->equiv;
3954 /* Add N1 and N2 to their own set of equivalences to avoid
3955 duplicating the body of the loop just to check N1 and N2
3956 ranges. */
3957 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
3958 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
3960 /* If the equivalence sets have a common intersection, then the two
3961 names can be compared without checking their ranges. */
3962 if (bitmap_intersect_p (e1, e2))
3964 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
3965 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
3967 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
3968 ? boolean_true_node
3969 : boolean_false_node;
3972 /* Otherwise, compare all the equivalent ranges. First, add N1 and
3973 N2 to their own set of equivalences to avoid duplicating the body
3974 of the loop just to check N1 and N2 ranges. */
3975 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
3977 value_range_t vr1 = *(vr_value[i1]);
3979 /* If the range is VARYING or UNDEFINED, use the name itself. */
3980 if (vr1.type == VR_VARYING || vr1.type == VR_UNDEFINED)
3982 vr1.type = VR_RANGE;
3983 vr1.min = ssa_name (i1);
3984 vr1.max = ssa_name (i1);
3987 t = retval = NULL_TREE;
3988 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
3990 value_range_t vr2 = *(vr_value[i2]);
3992 if (vr2.type == VR_VARYING || vr2.type == VR_UNDEFINED)
3994 vr2.type = VR_RANGE;
3995 vr2.min = ssa_name (i2);
3996 vr2.max = ssa_name (i2);
3999 t = compare_ranges (comp, &vr1, &vr2);
4000 if (t)
4002 /* If we get different answers from different members
4003 of the equivalence set this check must be in a dead
4004 code region. Folding it to a trap representation
4005 would be correct here. For now just return don't-know. */
4006 if (retval != NULL
4007 && t != retval)
4009 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4010 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4011 return NULL_TREE;
4013 retval = t;
4017 if (retval)
4019 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4020 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4021 return retval;
4025 /* None of the equivalent ranges are useful in computing this
4026 comparison. */
4027 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
4028 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
4029 return NULL_TREE;
4033 /* Given a conditional predicate COND, try to determine if COND yields
4034 true or false based on the value ranges of its operands. Return
4035 BOOLEAN_TRUE_NODE if the conditional always evaluates to true,
4036 BOOLEAN_FALSE_NODE if the conditional always evaluates to false, and,
4037 NULL if the conditional cannot be evaluated at compile time.
4039 If USE_EQUIV_P is true, the ranges of all the names equivalent with
4040 the operands in COND are used when trying to compute its value.
4041 This is only used during final substitution. During propagation,
4042 we only check the range of each variable and not its equivalents. */
4044 tree
4045 vrp_evaluate_conditional (tree cond, bool use_equiv_p)
4047 gcc_assert (TREE_CODE (cond) == SSA_NAME
4048 || TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison);
4050 if (TREE_CODE (cond) == SSA_NAME)
4052 value_range_t *vr;
4053 tree retval;
4055 if (use_equiv_p)
4056 retval = compare_name_with_value (NE_EXPR, cond, boolean_false_node);
4057 else
4059 value_range_t *vr = get_value_range (cond);
4060 retval = compare_range_with_value (NE_EXPR, vr, boolean_false_node);
4063 /* If COND has a known boolean range, return it. */
4064 if (retval)
4065 return retval;
4067 /* Otherwise, if COND has a symbolic range of exactly one value,
4068 return it. */
4069 vr = get_value_range (cond);
4070 if (vr->type == VR_RANGE && vr->min == vr->max)
4071 return vr->min;
4073 else
4075 tree op0 = TREE_OPERAND (cond, 0);
4076 tree op1 = TREE_OPERAND (cond, 1);
4078 /* We only deal with integral and pointer types. */
4079 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
4080 && !POINTER_TYPE_P (TREE_TYPE (op0)))
4081 return NULL_TREE;
4083 if (use_equiv_p)
4085 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
4086 return compare_names (TREE_CODE (cond), op0, op1);
4087 else if (TREE_CODE (op0) == SSA_NAME)
4088 return compare_name_with_value (TREE_CODE (cond), op0, op1);
4089 else if (TREE_CODE (op1) == SSA_NAME)
4090 return compare_name_with_value (
4091 swap_tree_comparison (TREE_CODE (cond)), op1, op0);
4093 else
4095 value_range_t *vr0, *vr1;
4097 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
4098 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
4100 if (vr0 && vr1)
4101 return compare_ranges (TREE_CODE (cond), vr0, vr1);
4102 else if (vr0 && vr1 == NULL)
4103 return compare_range_with_value (TREE_CODE (cond), vr0, op1);
4104 else if (vr0 == NULL && vr1)
4105 return compare_range_with_value (
4106 swap_tree_comparison (TREE_CODE (cond)), vr1, op0);
4110 /* Anything else cannot be computed statically. */
4111 return NULL_TREE;
4115 /* Visit conditional statement STMT. If we can determine which edge
4116 will be taken out of STMT's basic block, record it in
4117 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
4118 SSA_PROP_VARYING. */
4120 static enum ssa_prop_result
4121 vrp_visit_cond_stmt (tree stmt, edge *taken_edge_p)
4123 tree cond, val;
4125 *taken_edge_p = NULL;
4127 /* FIXME. Handle SWITCH_EXPRs. But first, the assert pass needs to
4128 add ASSERT_EXPRs for them. */
4129 if (TREE_CODE (stmt) == SWITCH_EXPR)
4130 return SSA_PROP_VARYING;
4132 cond = COND_EXPR_COND (stmt);
4134 if (dump_file && (dump_flags & TDF_DETAILS))
4136 tree use;
4137 ssa_op_iter i;
4139 fprintf (dump_file, "\nVisiting conditional with predicate: ");
4140 print_generic_expr (dump_file, cond, 0);
4141 fprintf (dump_file, "\nWith known ranges\n");
4143 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
4145 fprintf (dump_file, "\t");
4146 print_generic_expr (dump_file, use, 0);
4147 fprintf (dump_file, ": ");
4148 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
4151 fprintf (dump_file, "\n");
4154 /* Compute the value of the predicate COND by checking the known
4155 ranges of each of its operands.
4157 Note that we cannot evaluate all the equivalent ranges here
4158 because those ranges may not yet be final and with the current
4159 propagation strategy, we cannot determine when the value ranges
4160 of the names in the equivalence set have changed.
4162 For instance, given the following code fragment
4164 i_5 = PHI <8, i_13>
4166 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
4167 if (i_14 == 1)
4170 Assume that on the first visit to i_14, i_5 has the temporary
4171 range [8, 8] because the second argument to the PHI function is
4172 not yet executable. We derive the range ~[0, 0] for i_14 and the
4173 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
4174 the first time, since i_14 is equivalent to the range [8, 8], we
4175 determine that the predicate is always false.
4177 On the next round of propagation, i_13 is determined to be
4178 VARYING, which causes i_5 to drop down to VARYING. So, another
4179 visit to i_14 is scheduled. In this second visit, we compute the
4180 exact same range and equivalence set for i_14, namely ~[0, 0] and
4181 { i_5 }. But we did not have the previous range for i_5
4182 registered, so vrp_visit_assignment thinks that the range for
4183 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
4184 is not visited again, which stops propagation from visiting
4185 statements in the THEN clause of that if().
4187 To properly fix this we would need to keep the previous range
4188 value for the names in the equivalence set. This way we would've
4189 discovered that from one visit to the other i_5 changed from
4190 range [8, 8] to VR_VARYING.
4192 However, fixing this apparent limitation may not be worth the
4193 additional checking. Testing on several code bases (GCC, DLV,
4194 MICO, TRAMP3D and SPEC2000) showed that doing this results in
4195 4 more predicates folded in SPEC. */
4196 val = vrp_evaluate_conditional (cond, false);
4197 if (val)
4198 *taken_edge_p = find_taken_edge (bb_for_stmt (stmt), val);
4200 if (dump_file && (dump_flags & TDF_DETAILS))
4202 fprintf (dump_file, "\nPredicate evaluates to: ");
4203 if (val == NULL_TREE)
4204 fprintf (dump_file, "DON'T KNOW\n");
4205 else
4206 print_generic_stmt (dump_file, val, 0);
4209 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
4213 /* Evaluate statement STMT. If the statement produces a useful range,
4214 return SSA_PROP_INTERESTING and record the SSA name with the
4215 interesting range into *OUTPUT_P.
4217 If STMT is a conditional branch and we can determine its truth
4218 value, the taken edge is recorded in *TAKEN_EDGE_P.
4220 If STMT produces a varying value, return SSA_PROP_VARYING. */
4222 static enum ssa_prop_result
4223 vrp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
4225 tree def;
4226 ssa_op_iter iter;
4227 stmt_ann_t ann;
4229 if (dump_file && (dump_flags & TDF_DETAILS))
4231 fprintf (dump_file, "\nVisiting statement:\n");
4232 print_generic_stmt (dump_file, stmt, dump_flags);
4233 fprintf (dump_file, "\n");
4236 ann = stmt_ann (stmt);
4237 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
4239 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
4241 /* In general, assignments with virtual operands are not useful
4242 for deriving ranges, with the obvious exception of calls to
4243 builtin functions. */
4244 if ((TREE_CODE (rhs) == CALL_EXPR
4245 && TREE_CODE (CALL_EXPR_FN (rhs)) == ADDR_EXPR
4246 && DECL_P (TREE_OPERAND (CALL_EXPR_FN (rhs), 0))
4247 && DECL_IS_BUILTIN (TREE_OPERAND (CALL_EXPR_FN (rhs), 0)))
4248 || ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
4249 return vrp_visit_assignment (stmt, output_p);
4251 else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
4252 return vrp_visit_cond_stmt (stmt, taken_edge_p);
4254 /* All other statements produce nothing of interest for VRP, so mark
4255 their outputs varying and prevent further simulation. */
4256 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
4257 set_value_range_to_varying (get_value_range (def));
4259 return SSA_PROP_VARYING;
4263 /* Meet operation for value ranges. Given two value ranges VR0 and
4264 VR1, store in VR0 a range that contains both VR0 and VR1. This
4265 may not be the smallest possible such range. */
4267 static void
4268 vrp_meet (value_range_t *vr0, value_range_t *vr1)
4270 if (vr0->type == VR_UNDEFINED)
4272 copy_value_range (vr0, vr1);
4273 return;
4276 if (vr1->type == VR_UNDEFINED)
4278 /* Nothing to do. VR0 already has the resulting range. */
4279 return;
4282 if (vr0->type == VR_VARYING)
4284 /* Nothing to do. VR0 already has the resulting range. */
4285 return;
4288 if (vr1->type == VR_VARYING)
4290 set_value_range_to_varying (vr0);
4291 return;
4294 if (vr0->type == VR_RANGE && vr1->type == VR_RANGE)
4296 int cmp;
4297 tree min, max;
4299 /* Compute the convex hull of the ranges. The lower limit of
4300 the new range is the minimum of the two ranges. If they
4301 cannot be compared, then give up. */
4302 cmp = compare_values (vr0->min, vr1->min);
4303 if (cmp == 0 || cmp == 1)
4304 min = vr1->min;
4305 else if (cmp == -1)
4306 min = vr0->min;
4307 else
4308 goto give_up;
4310 /* Similarly, the upper limit of the new range is the maximum
4311 of the two ranges. If they cannot be compared, then
4312 give up. */
4313 cmp = compare_values (vr0->max, vr1->max);
4314 if (cmp == 0 || cmp == -1)
4315 max = vr1->max;
4316 else if (cmp == 1)
4317 max = vr0->max;
4318 else
4319 goto give_up;
4321 /* The resulting set of equivalences is the intersection of
4322 the two sets. */
4323 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4324 bitmap_and_into (vr0->equiv, vr1->equiv);
4325 else if (vr0->equiv && !vr1->equiv)
4326 bitmap_clear (vr0->equiv);
4328 set_value_range (vr0, vr0->type, min, max, vr0->equiv);
4330 else if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4332 /* Two anti-ranges meet only if their complements intersect.
4333 Only handle the case of identical ranges. */
4334 if (compare_values (vr0->min, vr1->min) == 0
4335 && compare_values (vr0->max, vr1->max) == 0
4336 && compare_values (vr0->min, vr0->max) == 0)
4338 /* The resulting set of equivalences is the intersection of
4339 the two sets. */
4340 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4341 bitmap_and_into (vr0->equiv, vr1->equiv);
4342 else if (vr0->equiv && !vr1->equiv)
4343 bitmap_clear (vr0->equiv);
4345 else
4346 goto give_up;
4348 else if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4350 /* For a numeric range [VAL1, VAL2] and an anti-range ~[VAL3, VAL4],
4351 only handle the case where the ranges have an empty intersection.
4352 The result of the meet operation is the anti-range. */
4353 if (!symbolic_range_p (vr0)
4354 && !symbolic_range_p (vr1)
4355 && !value_ranges_intersect_p (vr0, vr1))
4357 /* Copy most of VR1 into VR0. Don't copy VR1's equivalence
4358 set. We need to compute the intersection of the two
4359 equivalence sets. */
4360 if (vr1->type == VR_ANTI_RANGE)
4361 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr0->equiv);
4363 /* The resulting set of equivalences is the intersection of
4364 the two sets. */
4365 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
4366 bitmap_and_into (vr0->equiv, vr1->equiv);
4367 else if (vr0->equiv && !vr1->equiv)
4368 bitmap_clear (vr0->equiv);
4370 else
4371 goto give_up;
4373 else
4374 gcc_unreachable ();
4376 return;
4378 give_up:
4379 /* Failed to find an efficient meet. Before giving up and setting
4380 the result to VARYING, see if we can at least derive a useful
4381 anti-range. FIXME, all this nonsense about distinguishing
4382 anti-ranges from ranges is necessary because of the odd
4383 semantics of range_includes_zero_p and friends. */
4384 if (!symbolic_range_p (vr0)
4385 && ((vr0->type == VR_RANGE && !range_includes_zero_p (vr0))
4386 || (vr0->type == VR_ANTI_RANGE && range_includes_zero_p (vr0)))
4387 && !symbolic_range_p (vr1)
4388 && ((vr1->type == VR_RANGE && !range_includes_zero_p (vr1))
4389 || (vr1->type == VR_ANTI_RANGE && range_includes_zero_p (vr1))))
4391 set_value_range_to_nonnull (vr0, TREE_TYPE (vr0->min));
4393 /* Since this meet operation did not result from the meeting of
4394 two equivalent names, VR0 cannot have any equivalences. */
4395 if (vr0->equiv)
4396 bitmap_clear (vr0->equiv);
4398 else
4399 set_value_range_to_varying (vr0);
4403 /* Visit all arguments for PHI node PHI that flow through executable
4404 edges. If a valid value range can be derived from all the incoming
4405 value ranges, set a new range for the LHS of PHI. */
4407 static enum ssa_prop_result
4408 vrp_visit_phi_node (tree phi)
4410 int i;
4411 tree lhs = PHI_RESULT (phi);
4412 value_range_t *lhs_vr = get_value_range (lhs);
4413 value_range_t vr_result = { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL };
4414 bool all_const = true;
4416 copy_value_range (&vr_result, lhs_vr);
4418 if (dump_file && (dump_flags & TDF_DETAILS))
4420 fprintf (dump_file, "\nVisiting PHI node: ");
4421 print_generic_expr (dump_file, phi, dump_flags);
4424 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
4426 edge e = PHI_ARG_EDGE (phi, i);
4428 if (dump_file && (dump_flags & TDF_DETAILS))
4430 fprintf (dump_file,
4431 "\n Argument #%d (%d -> %d %sexecutable)\n",
4432 i, e->src->index, e->dest->index,
4433 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
4436 if (e->flags & EDGE_EXECUTABLE)
4438 tree arg = PHI_ARG_DEF (phi, i);
4439 value_range_t vr_arg;
4441 if (TREE_CODE (arg) == SSA_NAME)
4443 vr_arg = *(get_value_range (arg));
4444 all_const = false;
4446 else
4448 vr_arg.type = VR_RANGE;
4449 vr_arg.min = arg;
4450 vr_arg.max = arg;
4451 vr_arg.equiv = NULL;
4454 if (dump_file && (dump_flags & TDF_DETAILS))
4456 fprintf (dump_file, "\t");
4457 print_generic_expr (dump_file, arg, dump_flags);
4458 fprintf (dump_file, "\n\tValue: ");
4459 dump_value_range (dump_file, &vr_arg);
4460 fprintf (dump_file, "\n");
4463 vrp_meet (&vr_result, &vr_arg);
4465 if (vr_result.type == VR_VARYING)
4466 break;
4470 if (vr_result.type == VR_VARYING)
4471 goto varying;
4473 /* To prevent infinite iterations in the algorithm, derive ranges
4474 when the new value is slightly bigger or smaller than the
4475 previous one. */
4476 if (lhs_vr->type == VR_RANGE && vr_result.type == VR_RANGE
4477 && !all_const)
4479 if (!POINTER_TYPE_P (TREE_TYPE (lhs)))
4481 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
4482 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
4484 /* If the new minimum is smaller or larger than the previous
4485 one, go all the way to -INF. In the first case, to avoid
4486 iterating millions of times to reach -INF, and in the
4487 other case to avoid infinite bouncing between different
4488 minimums. */
4489 if (cmp_min > 0 || cmp_min < 0)
4490 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
4492 /* Similarly, if the new maximum is smaller or larger than
4493 the previous one, go all the way to +INF. */
4494 if (cmp_max < 0 || cmp_max > 0)
4495 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
4497 /* If we ended up with a (-INF, +INF) range, set it to
4498 VARYING. */
4499 if (vr_result.min == TYPE_MIN_VALUE (TREE_TYPE (vr_result.min))
4500 && vr_result.max == TYPE_MAX_VALUE (TREE_TYPE (vr_result.max)))
4501 goto varying;
4505 /* If the new range is different than the previous value, keep
4506 iterating. */
4507 if (update_value_range (lhs, &vr_result))
4508 return SSA_PROP_INTERESTING;
4510 /* Nothing changed, don't add outgoing edges. */
4511 return SSA_PROP_NOT_INTERESTING;
4513 /* No match found. Set the LHS to VARYING. */
4514 varying:
4515 set_value_range_to_varying (lhs_vr);
4516 return SSA_PROP_VARYING;
4519 /* Simplify a division or modulo operator to a right shift or
4520 bitwise and if the first operand is unsigned or is greater
4521 than zero and the second operand is an exact power of two. */
4523 static void
4524 simplify_div_or_mod_using_ranges (tree stmt, tree rhs, enum tree_code rhs_code)
4526 tree val = NULL;
4527 tree op = TREE_OPERAND (rhs, 0);
4528 value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
4530 if (TYPE_UNSIGNED (TREE_TYPE (op)))
4532 val = integer_one_node;
4534 else
4536 val = compare_range_with_value (GT_EXPR, vr, integer_zero_node);
4539 if (val && integer_onep (val))
4541 tree t;
4542 tree op0 = TREE_OPERAND (rhs, 0);
4543 tree op1 = TREE_OPERAND (rhs, 1);
4545 if (rhs_code == TRUNC_DIV_EXPR)
4547 t = build_int_cst (NULL_TREE, tree_log2 (op1));
4548 t = build2 (RSHIFT_EXPR, TREE_TYPE (op0), op0, t);
4550 else
4552 t = build_int_cst (TREE_TYPE (op1), 1);
4553 t = int_const_binop (MINUS_EXPR, op1, t, 0);
4554 t = fold_convert (TREE_TYPE (op0), t);
4555 t = build2 (BIT_AND_EXPR, TREE_TYPE (op0), op0, t);
4558 GIMPLE_STMT_OPERAND (stmt, 1) = t;
4559 update_stmt (stmt);
4563 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
4564 ABS_EXPR. If the operand is <= 0, then simplify the
4565 ABS_EXPR into a NEGATE_EXPR. */
4567 static void
4568 simplify_abs_using_ranges (tree stmt, tree rhs)
4570 tree val = NULL;
4571 tree op = TREE_OPERAND (rhs, 0);
4572 tree type = TREE_TYPE (op);
4573 value_range_t *vr = get_value_range (TREE_OPERAND (rhs, 0));
4575 if (TYPE_UNSIGNED (type))
4577 val = integer_zero_node;
4579 else if (vr)
4581 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node);
4582 if (!val)
4584 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node);
4586 if (val)
4588 if (integer_zerop (val))
4589 val = integer_one_node;
4590 else if (integer_onep (val))
4591 val = integer_zero_node;
4595 if (val
4596 && (integer_onep (val) || integer_zerop (val)))
4598 tree t;
4600 if (integer_onep (val))
4601 t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
4602 else
4603 t = op;
4605 GIMPLE_STMT_OPERAND (stmt, 1) = t;
4606 update_stmt (stmt);
4611 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
4612 a known value range VR.
4614 If there is one and only one value which will satisfy the
4615 conditional, then return that value. Else return NULL. */
4617 static tree
4618 test_for_singularity (enum tree_code cond_code, tree op0,
4619 tree op1, value_range_t *vr)
4621 tree min = NULL;
4622 tree max = NULL;
4624 /* Extract minimum/maximum values which satisfy the
4625 the conditional as it was written. */
4626 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
4628 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
4630 max = op1;
4631 if (cond_code == LT_EXPR)
4633 tree one = build_int_cst (TREE_TYPE (op0), 1);
4634 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
4637 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
4639 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
4641 min = op1;
4642 if (cond_code == GT_EXPR)
4644 tree one = build_int_cst (TREE_TYPE (op0), 1);
4645 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
4649 /* Now refine the minimum and maximum values using any
4650 value range information we have for op0. */
4651 if (min && max)
4653 if (compare_values (vr->min, min) == -1)
4654 min = min;
4655 else
4656 min = vr->min;
4657 if (compare_values (vr->max, max) == 1)
4658 max = max;
4659 else
4660 max = vr->max;
4662 /* If the new min/max values have converged to a single value,
4663 then there is only one value which can satisfy the condition,
4664 return that value. */
4665 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
4666 return min;
4668 return NULL;
4671 /* Simplify a conditional using a relational operator to an equality
4672 test if the range information indicates only one value can satisfy
4673 the original conditional. */
4675 static void
4676 simplify_cond_using_ranges (tree stmt)
4678 tree cond = COND_EXPR_COND (stmt);
4679 tree op0 = TREE_OPERAND (cond, 0);
4680 tree op1 = TREE_OPERAND (cond, 1);
4681 enum tree_code cond_code = TREE_CODE (cond);
4683 if (cond_code != NE_EXPR
4684 && cond_code != EQ_EXPR
4685 && TREE_CODE (op0) == SSA_NAME
4686 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
4687 && is_gimple_min_invariant (op1))
4689 value_range_t *vr = get_value_range (op0);
4691 /* If we have range information for OP0, then we might be
4692 able to simplify this conditional. */
4693 if (vr->type == VR_RANGE)
4695 tree new = test_for_singularity (cond_code, op0, op1, vr);
4697 if (new)
4699 if (dump_file)
4701 fprintf (dump_file, "Simplified relational ");
4702 print_generic_expr (dump_file, cond, 0);
4703 fprintf (dump_file, " into ");
4706 COND_EXPR_COND (stmt)
4707 = build2 (EQ_EXPR, boolean_type_node, op0, new);
4708 update_stmt (stmt);
4710 if (dump_file)
4712 print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
4713 fprintf (dump_file, "\n");
4715 return;
4719 /* Try again after inverting the condition. We only deal
4720 with integral types here, so no need to worry about
4721 issues with inverting FP comparisons. */
4722 cond_code = invert_tree_comparison (cond_code, false);
4723 new = test_for_singularity (cond_code, op0, op1, vr);
4725 if (new)
4727 if (dump_file)
4729 fprintf (dump_file, "Simplified relational ");
4730 print_generic_expr (dump_file, cond, 0);
4731 fprintf (dump_file, " into ");
4734 COND_EXPR_COND (stmt)
4735 = build2 (NE_EXPR, boolean_type_node, op0, new);
4736 update_stmt (stmt);
4738 if (dump_file)
4740 print_generic_expr (dump_file, COND_EXPR_COND (stmt), 0);
4741 fprintf (dump_file, "\n");
4743 return;
4750 /* Simplify STMT using ranges if possible. */
4752 void
4753 simplify_stmt_using_ranges (tree stmt)
4755 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
4757 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
4758 enum tree_code rhs_code = TREE_CODE (rhs);
4760 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
4761 and BIT_AND_EXPR respectively if the first operand is greater
4762 than zero and the second operand is an exact power of two. */
4763 if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
4764 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
4765 && integer_pow2p (TREE_OPERAND (rhs, 1)))
4766 simplify_div_or_mod_using_ranges (stmt, rhs, rhs_code);
4768 /* Transform ABS (X) into X or -X as appropriate. */
4769 if (rhs_code == ABS_EXPR
4770 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
4771 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
4772 simplify_abs_using_ranges (stmt, rhs);
4774 else if (TREE_CODE (stmt) == COND_EXPR
4775 && COMPARISON_CLASS_P (COND_EXPR_COND (stmt)))
4777 simplify_cond_using_ranges (stmt);
4781 /* Stack of dest,src equivalency pairs that need to be restored after
4782 each attempt to thread a block's incoming edge to an outgoing edge.
4784 A NULL entry is used to mark the end of pairs which need to be
4785 restored. */
4786 static VEC(tree,heap) *stack;
4788 /* A trivial wrapper so that we can present the generic jump
4789 threading code with a simple API for simplifying statements. */
4790 static tree
4791 simplify_stmt_for_jump_threading (tree stmt)
4793 /* We only use VRP information to simplify conditionals. This is
4794 overly conservative, but it's unclear if doing more would be
4795 worth the compile time cost. */
4796 if (TREE_CODE (stmt) != COND_EXPR)
4797 return NULL;
4799 return vrp_evaluate_conditional (COND_EXPR_COND (stmt), true);
4802 /* Blocks which have more than one predecessor and more than
4803 one successor present jump threading opportunities. ie,
4804 when the block is reached from a specific predecessor, we
4805 may be able to determine which of the outgoing edges will
4806 be traversed. When this optimization applies, we are able
4807 to avoid conditionals at runtime and we may expose secondary
4808 optimization opportunities.
4810 This routine is effectively a driver for the generic jump
4811 threading code. It basically just presents the generic code
4812 with edges that may be suitable for jump threading.
4814 Unlike DOM, we do not iterate VRP if jump threading was successful.
4815 While iterating may expose new opportunities for VRP, it is expected
4816 those opportunities would be very limited and the compile time cost
4817 to expose those opportunities would be significant.
4819 As jump threading opportunities are discovered, they are registered
4820 for later realization. */
4822 static void
4823 identify_jump_threads (void)
4825 basic_block bb;
4826 tree dummy;
4828 /* Ugh. When substituting values earlier in this pass we can
4829 wipe the dominance information. So rebuild the dominator
4830 information as we need it within the jump threading code. */
4831 calculate_dominance_info (CDI_DOMINATORS);
4833 /* We do not allow VRP information to be used for jump threading
4834 across a back edge in the CFG. Otherwise it becomes too
4835 difficult to avoid eliminating loop exit tests. Of course
4836 EDGE_DFS_BACK is not accurate at this time so we have to
4837 recompute it. */
4838 mark_dfs_back_edges ();
4840 /* Allocate our unwinder stack to unwind any temporary equivalences
4841 that might be recorded. */
4842 stack = VEC_alloc (tree, heap, 20);
4844 /* To avoid lots of silly node creation, we create a single
4845 conditional and just modify it in-place when attempting to
4846 thread jumps. */
4847 dummy = build2 (EQ_EXPR, boolean_type_node, NULL, NULL);
4848 dummy = build3 (COND_EXPR, void_type_node, dummy, NULL, NULL);
4850 /* Walk through all the blocks finding those which present a
4851 potential jump threading opportunity. We could set this up
4852 as a dominator walker and record data during the walk, but
4853 I doubt it's worth the effort for the classes of jump
4854 threading opportunities we are trying to identify at this
4855 point in compilation. */
4856 FOR_EACH_BB (bb)
4858 tree last, cond;
4860 /* If the generic jump threading code does not find this block
4861 interesting, then there is nothing to do. */
4862 if (! potentially_threadable_block (bb))
4863 continue;
4865 /* We only care about blocks ending in a COND_EXPR. While there
4866 may be some value in handling SWITCH_EXPR here, I doubt it's
4867 terribly important. */
4868 last = bsi_stmt (bsi_last (bb));
4869 if (TREE_CODE (last) != COND_EXPR)
4870 continue;
4872 /* We're basically looking for any kind of conditional with
4873 integral type arguments. */
4874 cond = COND_EXPR_COND (last);
4875 if ((TREE_CODE (cond) == SSA_NAME
4876 && INTEGRAL_TYPE_P (TREE_TYPE (cond)))
4877 || (COMPARISON_CLASS_P (cond)
4878 && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
4879 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 0)))
4880 && (TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME
4881 || is_gimple_min_invariant (TREE_OPERAND (cond, 1)))
4882 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 1)))))
4884 edge_iterator ei;
4885 edge e;
4887 /* We've got a block with multiple predecessors and multiple
4888 successors which also ends in a suitable conditional. For
4889 each predecessor, see if we can thread it to a specific
4890 successor. */
4891 FOR_EACH_EDGE (e, ei, bb->preds)
4893 /* Do not thread across back edges or abnormal edges
4894 in the CFG. */
4895 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
4896 continue;
4898 thread_across_edge (dummy, e, true,
4899 &stack,
4900 simplify_stmt_for_jump_threading);
4905 /* We do not actually update the CFG or SSA graphs at this point as
4906 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
4907 handle ASSERT_EXPRs gracefully. */
4910 /* We identified all the jump threading opportunities earlier, but could
4911 not transform the CFG at that time. This routine transforms the
4912 CFG and arranges for the dominator tree to be rebuilt if necessary.
4914 Note the SSA graph update will occur during the normal TODO
4915 processing by the pass manager. */
4916 static void
4917 finalize_jump_threads (void)
4919 bool cfg_altered = false;
4920 cfg_altered = thread_through_all_blocks ();
4922 /* If we threaded jumps, then we need to recompute the dominance
4923 information. */
4924 if (cfg_altered)
4925 free_dominance_info (CDI_DOMINATORS);
4926 VEC_free (tree, heap, stack);
4930 /* Traverse all the blocks folding conditionals with known ranges. */
4932 static void
4933 vrp_finalize (void)
4935 size_t i;
4936 prop_value_t *single_val_range;
4937 bool do_value_subst_p;
4939 if (dump_file)
4941 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
4942 dump_all_value_ranges (dump_file);
4943 fprintf (dump_file, "\n");
4946 /* We may have ended with ranges that have exactly one value. Those
4947 values can be substituted as any other copy/const propagated
4948 value using substitute_and_fold. */
4949 single_val_range = XCNEWVEC (prop_value_t, num_ssa_names);
4951 do_value_subst_p = false;
4952 for (i = 0; i < num_ssa_names; i++)
4953 if (vr_value[i]
4954 && vr_value[i]->type == VR_RANGE
4955 && vr_value[i]->min == vr_value[i]->max)
4957 single_val_range[i].value = vr_value[i]->min;
4958 do_value_subst_p = true;
4961 if (!do_value_subst_p)
4963 /* We found no single-valued ranges, don't waste time trying to
4964 do single value substitution in substitute_and_fold. */
4965 free (single_val_range);
4966 single_val_range = NULL;
4969 substitute_and_fold (single_val_range, true);
4971 if (warn_array_bounds)
4972 check_all_array_refs ();
4974 /* We must identify jump threading opportunities before we release
4975 the datastructures built by VRP. */
4976 identify_jump_threads ();
4978 /* Free allocated memory. */
4979 for (i = 0; i < num_ssa_names; i++)
4980 if (vr_value[i])
4982 BITMAP_FREE (vr_value[i]->equiv);
4983 free (vr_value[i]);
4986 free (single_val_range);
4987 free (vr_value);
4989 /* So that we can distinguish between VRP data being available
4990 and not available. */
4991 vr_value = NULL;
4995 /* Main entry point to VRP (Value Range Propagation). This pass is
4996 loosely based on J. R. C. Patterson, ``Accurate Static Branch
4997 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
4998 Programming Language Design and Implementation, pp. 67-78, 1995.
4999 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
5001 This is essentially an SSA-CCP pass modified to deal with ranges
5002 instead of constants.
5004 While propagating ranges, we may find that two or more SSA name
5005 have equivalent, though distinct ranges. For instance,
5007 1 x_9 = p_3->a;
5008 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
5009 3 if (p_4 == q_2)
5010 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
5011 5 endif
5012 6 if (q_2)
5014 In the code above, pointer p_5 has range [q_2, q_2], but from the
5015 code we can also determine that p_5 cannot be NULL and, if q_2 had
5016 a non-varying range, p_5's range should also be compatible with it.
5018 These equivalences are created by two expressions: ASSERT_EXPR and
5019 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
5020 result of another assertion, then we can use the fact that p_5 and
5021 p_4 are equivalent when evaluating p_5's range.
5023 Together with value ranges, we also propagate these equivalences
5024 between names so that we can take advantage of information from
5025 multiple ranges when doing final replacement. Note that this
5026 equivalency relation is transitive but not symmetric.
5028 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
5029 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
5030 in contexts where that assertion does not hold (e.g., in line 6).
5032 TODO, the main difference between this pass and Patterson's is that
5033 we do not propagate edge probabilities. We only compute whether
5034 edges can be taken or not. That is, instead of having a spectrum
5035 of jump probabilities between 0 and 1, we only deal with 0, 1 and
5036 DON'T KNOW. In the future, it may be worthwhile to propagate
5037 probabilities to aid branch prediction. */
5039 static unsigned int
5040 execute_vrp (void)
5042 insert_range_assertions ();
5044 loop_optimizer_init (LOOPS_NORMAL);
5045 if (current_loops)
5046 scev_initialize ();
5048 vrp_initialize ();
5049 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
5050 vrp_finalize ();
5052 if (current_loops)
5054 scev_finalize ();
5055 loop_optimizer_finalize ();
5058 /* ASSERT_EXPRs must be removed before finalizing jump threads
5059 as finalizing jump threads calls the CFG cleanup code which
5060 does not properly handle ASSERT_EXPRs. */
5061 remove_range_assertions ();
5063 /* If we exposed any new variables, go ahead and put them into
5064 SSA form now, before we handle jump threading. This simplifies
5065 interactions between rewriting of _DECL nodes into SSA form
5066 and rewriting SSA_NAME nodes into SSA form after block
5067 duplication and CFG manipulation. */
5068 update_ssa (TODO_update_ssa);
5070 finalize_jump_threads ();
5071 return 0;
5074 static bool
5075 gate_vrp (void)
5077 return flag_tree_vrp != 0;
5080 struct tree_opt_pass pass_vrp =
5082 "vrp", /* name */
5083 gate_vrp, /* gate */
5084 execute_vrp, /* execute */
5085 NULL, /* sub */
5086 NULL, /* next */
5087 0, /* static_pass_number */
5088 TV_TREE_VRP, /* tv_id */
5089 PROP_ssa | PROP_alias, /* properties_required */
5090 0, /* properties_provided */
5091 0, /* properties_destroyed */
5092 0, /* todo_flags_start */
5093 TODO_cleanup_cfg
5094 | TODO_ggc_collect
5095 | TODO_verify_ssa
5096 | TODO_dump_func
5097 | TODO_update_ssa
5098 | TODO_update_smt_usage, /* todo_flags_finish */
5099 0 /* letter */