2013-11-19 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-vrp.c
blob4a24c668d57f25ce3767038b70c001040d255b76
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "flags.h"
27 #include "tree.h"
28 #include "stor-layout.h"
29 #include "calls.h"
30 #include "basic-block.h"
31 #include "gimple.h"
32 #include "gimple-iterator.h"
33 #include "gimple-walk.h"
34 #include "gimple-ssa.h"
35 #include "tree-cfg.h"
36 #include "tree-phinodes.h"
37 #include "ssa-iterators.h"
38 #include "stringpool.h"
39 #include "tree-ssanames.h"
40 #include "tree-ssa-loop-manip.h"
41 #include "tree-ssa-loop-niter.h"
42 #include "tree-ssa-loop.h"
43 #include "tree-into-ssa.h"
44 #include "tree-ssa.h"
45 #include "tree-pass.h"
46 #include "tree-dump.h"
47 #include "gimple-pretty-print.h"
48 #include "diagnostic-core.h"
49 #include "intl.h"
50 #include "cfgloop.h"
51 #include "tree-scalar-evolution.h"
52 #include "tree-ssa-propagate.h"
53 #include "tree-chrec.h"
54 #include "tree-ssa-threadupdate.h"
55 #include "expr.h"
56 #include "optabs.h"
57 #include "tree-ssa-threadedge.h"
61 /* Range of values that can be associated with an SSA_NAME after VRP
62 has executed. */
63 struct value_range_d
65 /* Lattice value represented by this range. */
66 enum value_range_type type;
68 /* Minimum and maximum values represented by this range. These
69 values should be interpreted as follows:
71 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
72 be NULL.
74 - If TYPE == VR_RANGE then MIN holds the minimum value and
75 MAX holds the maximum value of the range [MIN, MAX].
77 - If TYPE == ANTI_RANGE the variable is known to NOT
78 take any values in the range [MIN, MAX]. */
79 tree min;
80 tree max;
82 /* Set of SSA names whose value ranges are equivalent to this one.
83 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
84 bitmap equiv;
87 typedef struct value_range_d value_range_t;
89 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
91 /* Set of SSA names found live during the RPO traversal of the function
92 for still active basic-blocks. */
93 static sbitmap *live;
95 /* Return true if the SSA name NAME is live on the edge E. */
97 static bool
98 live_on_edge (edge e, tree name)
100 return (live[e->dest->index]
101 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
104 /* Local functions. */
105 static int compare_values (tree val1, tree val2);
106 static int compare_values_warnv (tree val1, tree val2, bool *);
107 static void vrp_meet (value_range_t *, value_range_t *);
108 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
109 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
110 tree, tree, bool, bool *,
111 bool *);
113 /* Location information for ASSERT_EXPRs. Each instance of this
114 structure describes an ASSERT_EXPR for an SSA name. Since a single
115 SSA name may have more than one assertion associated with it, these
116 locations are kept in a linked list attached to the corresponding
117 SSA name. */
118 struct assert_locus_d
120 /* Basic block where the assertion would be inserted. */
121 basic_block bb;
123 /* Some assertions need to be inserted on an edge (e.g., assertions
124 generated by COND_EXPRs). In those cases, BB will be NULL. */
125 edge e;
127 /* Pointer to the statement that generated this assertion. */
128 gimple_stmt_iterator si;
130 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
131 enum tree_code comp_code;
133 /* Value being compared against. */
134 tree val;
136 /* Expression to compare. */
137 tree expr;
139 /* Next node in the linked list. */
140 struct assert_locus_d *next;
143 typedef struct assert_locus_d *assert_locus_t;
145 /* If bit I is present, it means that SSA name N_i has a list of
146 assertions that should be inserted in the IL. */
147 static bitmap need_assert_for;
149 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
150 holds a list of ASSERT_LOCUS_T nodes that describe where
151 ASSERT_EXPRs for SSA name N_I should be inserted. */
152 static assert_locus_t *asserts_for;
154 /* Value range array. After propagation, VR_VALUE[I] holds the range
155 of values that SSA name N_I may take. */
156 static unsigned num_vr_values;
157 static value_range_t **vr_value;
158 static bool values_propagated;
160 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
161 number of executable edges we saw the last time we visited the
162 node. */
163 static int *vr_phi_edge_counts;
165 typedef struct {
166 gimple stmt;
167 tree vec;
168 } switch_update;
170 static vec<edge> to_remove_edges;
171 static vec<switch_update> to_update_switch_stmts;
174 /* Return the maximum value for TYPE. */
176 static inline tree
177 vrp_val_max (const_tree type)
179 if (!INTEGRAL_TYPE_P (type))
180 return NULL_TREE;
182 return TYPE_MAX_VALUE (type);
185 /* Return the minimum value for TYPE. */
187 static inline tree
188 vrp_val_min (const_tree type)
190 if (!INTEGRAL_TYPE_P (type))
191 return NULL_TREE;
193 return TYPE_MIN_VALUE (type);
196 /* Return whether VAL is equal to the maximum value of its type. This
197 will be true for a positive overflow infinity. We can't do a
198 simple equality comparison with TYPE_MAX_VALUE because C typedefs
199 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
200 to the integer constant with the same value in the type. */
202 static inline bool
203 vrp_val_is_max (const_tree val)
205 tree type_max = vrp_val_max (TREE_TYPE (val));
206 return (val == type_max
207 || (type_max != NULL_TREE
208 && operand_equal_p (val, type_max, 0)));
211 /* Return whether VAL is equal to the minimum value of its type. This
212 will be true for a negative overflow infinity. */
214 static inline bool
215 vrp_val_is_min (const_tree val)
217 tree type_min = vrp_val_min (TREE_TYPE (val));
218 return (val == type_min
219 || (type_min != NULL_TREE
220 && operand_equal_p (val, type_min, 0)));
224 /* Return whether TYPE should use an overflow infinity distinct from
225 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
226 represent a signed overflow during VRP computations. An infinity
227 is distinct from a half-range, which will go from some number to
228 TYPE_{MIN,MAX}_VALUE. */
230 static inline bool
231 needs_overflow_infinity (const_tree type)
233 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
236 /* Return whether TYPE can support our overflow infinity
237 representation: we use the TREE_OVERFLOW flag, which only exists
238 for constants. If TYPE doesn't support this, we don't optimize
239 cases which would require signed overflow--we drop them to
240 VARYING. */
242 static inline bool
243 supports_overflow_infinity (const_tree type)
245 tree min = vrp_val_min (type), max = vrp_val_max (type);
246 #ifdef ENABLE_CHECKING
247 gcc_assert (needs_overflow_infinity (type));
248 #endif
249 return (min != NULL_TREE
250 && CONSTANT_CLASS_P (min)
251 && max != NULL_TREE
252 && CONSTANT_CLASS_P (max));
255 /* VAL is the maximum or minimum value of a type. Return a
256 corresponding overflow infinity. */
258 static inline tree
259 make_overflow_infinity (tree val)
261 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
262 val = copy_node (val);
263 TREE_OVERFLOW (val) = 1;
264 return val;
267 /* Return a negative overflow infinity for TYPE. */
269 static inline tree
270 negative_overflow_infinity (tree type)
272 gcc_checking_assert (supports_overflow_infinity (type));
273 return make_overflow_infinity (vrp_val_min (type));
276 /* Return a positive overflow infinity for TYPE. */
278 static inline tree
279 positive_overflow_infinity (tree type)
281 gcc_checking_assert (supports_overflow_infinity (type));
282 return make_overflow_infinity (vrp_val_max (type));
285 /* Return whether VAL is a negative overflow infinity. */
287 static inline bool
288 is_negative_overflow_infinity (const_tree val)
290 return (needs_overflow_infinity (TREE_TYPE (val))
291 && CONSTANT_CLASS_P (val)
292 && TREE_OVERFLOW (val)
293 && vrp_val_is_min (val));
296 /* Return whether VAL is a positive overflow infinity. */
298 static inline bool
299 is_positive_overflow_infinity (const_tree val)
301 return (needs_overflow_infinity (TREE_TYPE (val))
302 && CONSTANT_CLASS_P (val)
303 && TREE_OVERFLOW (val)
304 && vrp_val_is_max (val));
307 /* Return whether VAL is a positive or negative overflow infinity. */
309 static inline bool
310 is_overflow_infinity (const_tree val)
312 return (needs_overflow_infinity (TREE_TYPE (val))
313 && CONSTANT_CLASS_P (val)
314 && TREE_OVERFLOW (val)
315 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
318 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
320 static inline bool
321 stmt_overflow_infinity (gimple stmt)
323 if (is_gimple_assign (stmt)
324 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
325 GIMPLE_SINGLE_RHS)
326 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
327 return false;
330 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
331 the same value with TREE_OVERFLOW clear. This can be used to avoid
332 confusing a regular value with an overflow value. */
334 static inline tree
335 avoid_overflow_infinity (tree val)
337 if (!is_overflow_infinity (val))
338 return val;
340 if (vrp_val_is_max (val))
341 return vrp_val_max (TREE_TYPE (val));
342 else
344 gcc_checking_assert (vrp_val_is_min (val));
345 return vrp_val_min (TREE_TYPE (val));
350 /* Return true if ARG is marked with the nonnull attribute in the
351 current function signature. */
353 static bool
354 nonnull_arg_p (const_tree arg)
356 tree t, attrs, fntype;
357 unsigned HOST_WIDE_INT arg_num;
359 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
361 /* The static chain decl is always non null. */
362 if (arg == cfun->static_chain_decl)
363 return true;
365 fntype = TREE_TYPE (current_function_decl);
366 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
368 attrs = lookup_attribute ("nonnull", attrs);
370 /* If "nonnull" wasn't specified, we know nothing about the argument. */
371 if (attrs == NULL_TREE)
372 return false;
374 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
375 if (TREE_VALUE (attrs) == NULL_TREE)
376 return true;
378 /* Get the position number for ARG in the function signature. */
379 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
381 t = DECL_CHAIN (t), arg_num++)
383 if (t == arg)
384 break;
387 gcc_assert (t == arg);
389 /* Now see if ARG_NUM is mentioned in the nonnull list. */
390 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
392 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
393 return true;
397 return false;
401 /* Set value range VR to VR_UNDEFINED. */
403 static inline void
404 set_value_range_to_undefined (value_range_t *vr)
406 vr->type = VR_UNDEFINED;
407 vr->min = vr->max = NULL_TREE;
408 if (vr->equiv)
409 bitmap_clear (vr->equiv);
413 /* Set value range VR to VR_VARYING. */
415 static inline void
416 set_value_range_to_varying (value_range_t *vr)
418 vr->type = VR_VARYING;
419 vr->min = vr->max = NULL_TREE;
420 if (vr->equiv)
421 bitmap_clear (vr->equiv);
425 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
427 static void
428 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
429 tree max, bitmap equiv)
431 #if defined ENABLE_CHECKING
432 /* Check the validity of the range. */
433 if (t == VR_RANGE || t == VR_ANTI_RANGE)
435 int cmp;
437 gcc_assert (min && max);
439 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
440 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
442 cmp = compare_values (min, max);
443 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
445 if (needs_overflow_infinity (TREE_TYPE (min)))
446 gcc_assert (!is_overflow_infinity (min)
447 || !is_overflow_infinity (max));
450 if (t == VR_UNDEFINED || t == VR_VARYING)
451 gcc_assert (min == NULL_TREE && max == NULL_TREE);
453 if (t == VR_UNDEFINED || t == VR_VARYING)
454 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
455 #endif
457 vr->type = t;
458 vr->min = min;
459 vr->max = max;
461 /* Since updating the equivalence set involves deep copying the
462 bitmaps, only do it if absolutely necessary. */
463 if (vr->equiv == NULL
464 && equiv != NULL)
465 vr->equiv = BITMAP_ALLOC (NULL);
467 if (equiv != vr->equiv)
469 if (equiv && !bitmap_empty_p (equiv))
470 bitmap_copy (vr->equiv, equiv);
471 else
472 bitmap_clear (vr->equiv);
477 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
478 This means adjusting T, MIN and MAX representing the case of a
479 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
480 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
481 In corner cases where MAX+1 or MIN-1 wraps this will fall back
482 to varying.
483 This routine exists to ease canonicalization in the case where we
484 extract ranges from var + CST op limit. */
486 static void
487 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
488 tree min, tree max, bitmap equiv)
490 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
491 if (t == VR_UNDEFINED)
493 set_value_range_to_undefined (vr);
494 return;
496 else if (t == VR_VARYING)
498 set_value_range_to_varying (vr);
499 return;
502 /* Nothing to canonicalize for symbolic ranges. */
503 if (TREE_CODE (min) != INTEGER_CST
504 || TREE_CODE (max) != INTEGER_CST)
506 set_value_range (vr, t, min, max, equiv);
507 return;
510 /* Wrong order for min and max, to swap them and the VR type we need
511 to adjust them. */
512 if (tree_int_cst_lt (max, min))
514 tree one, tmp;
516 /* For one bit precision if max < min, then the swapped
517 range covers all values, so for VR_RANGE it is varying and
518 for VR_ANTI_RANGE empty range, so drop to varying as well. */
519 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
521 set_value_range_to_varying (vr);
522 return;
525 one = build_int_cst (TREE_TYPE (min), 1);
526 tmp = int_const_binop (PLUS_EXPR, max, one);
527 max = int_const_binop (MINUS_EXPR, min, one);
528 min = tmp;
530 /* There's one corner case, if we had [C+1, C] before we now have
531 that again. But this represents an empty value range, so drop
532 to varying in this case. */
533 if (tree_int_cst_lt (max, min))
535 set_value_range_to_varying (vr);
536 return;
539 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
542 /* Anti-ranges that can be represented as ranges should be so. */
543 if (t == VR_ANTI_RANGE)
545 bool is_min = vrp_val_is_min (min);
546 bool is_max = vrp_val_is_max (max);
548 if (is_min && is_max)
550 /* We cannot deal with empty ranges, drop to varying.
551 ??? This could be VR_UNDEFINED instead. */
552 set_value_range_to_varying (vr);
553 return;
555 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
556 && (is_min || is_max))
558 /* Non-empty boolean ranges can always be represented
559 as a singleton range. */
560 if (is_min)
561 min = max = vrp_val_max (TREE_TYPE (min));
562 else
563 min = max = vrp_val_min (TREE_TYPE (min));
564 t = VR_RANGE;
566 else if (is_min
567 /* As a special exception preserve non-null ranges. */
568 && !(TYPE_UNSIGNED (TREE_TYPE (min))
569 && integer_zerop (max)))
571 tree one = build_int_cst (TREE_TYPE (max), 1);
572 min = int_const_binop (PLUS_EXPR, max, one);
573 max = vrp_val_max (TREE_TYPE (max));
574 t = VR_RANGE;
576 else if (is_max)
578 tree one = build_int_cst (TREE_TYPE (min), 1);
579 max = int_const_binop (MINUS_EXPR, min, one);
580 min = vrp_val_min (TREE_TYPE (min));
581 t = VR_RANGE;
585 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
586 if (needs_overflow_infinity (TREE_TYPE (min))
587 && is_overflow_infinity (min)
588 && is_overflow_infinity (max))
590 set_value_range_to_varying (vr);
591 return;
594 set_value_range (vr, t, min, max, equiv);
597 /* Copy value range FROM into value range TO. */
599 static inline void
600 copy_value_range (value_range_t *to, value_range_t *from)
602 set_value_range (to, from->type, from->min, from->max, from->equiv);
605 /* Set value range VR to a single value. This function is only called
606 with values we get from statements, and exists to clear the
607 TREE_OVERFLOW flag so that we don't think we have an overflow
608 infinity when we shouldn't. */
610 static inline void
611 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
613 gcc_assert (is_gimple_min_invariant (val));
614 val = avoid_overflow_infinity (val);
615 set_value_range (vr, VR_RANGE, val, val, equiv);
618 /* Set value range VR to a non-negative range of type TYPE.
619 OVERFLOW_INFINITY indicates whether to use an overflow infinity
620 rather than TYPE_MAX_VALUE; this should be true if we determine
621 that the range is nonnegative based on the assumption that signed
622 overflow does not occur. */
624 static inline void
625 set_value_range_to_nonnegative (value_range_t *vr, tree type,
626 bool overflow_infinity)
628 tree zero;
630 if (overflow_infinity && !supports_overflow_infinity (type))
632 set_value_range_to_varying (vr);
633 return;
636 zero = build_int_cst (type, 0);
637 set_value_range (vr, VR_RANGE, zero,
638 (overflow_infinity
639 ? positive_overflow_infinity (type)
640 : TYPE_MAX_VALUE (type)),
641 vr->equiv);
644 /* Set value range VR to a non-NULL range of type TYPE. */
646 static inline void
647 set_value_range_to_nonnull (value_range_t *vr, tree type)
649 tree zero = build_int_cst (type, 0);
650 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
654 /* Set value range VR to a NULL range of type TYPE. */
656 static inline void
657 set_value_range_to_null (value_range_t *vr, tree type)
659 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
663 /* Set value range VR to a range of a truthvalue of type TYPE. */
665 static inline void
666 set_value_range_to_truthvalue (value_range_t *vr, tree type)
668 if (TYPE_PRECISION (type) == 1)
669 set_value_range_to_varying (vr);
670 else
671 set_value_range (vr, VR_RANGE,
672 build_int_cst (type, 0), build_int_cst (type, 1),
673 vr->equiv);
677 /* If abs (min) < abs (max), set VR to [-max, max], if
678 abs (min) >= abs (max), set VR to [-min, min]. */
680 static void
681 abs_extent_range (value_range_t *vr, tree min, tree max)
683 int cmp;
685 gcc_assert (TREE_CODE (min) == INTEGER_CST);
686 gcc_assert (TREE_CODE (max) == INTEGER_CST);
687 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
688 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
689 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
690 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
691 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
693 set_value_range_to_varying (vr);
694 return;
696 cmp = compare_values (min, max);
697 if (cmp == -1)
698 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
699 else if (cmp == 0 || cmp == 1)
701 max = min;
702 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
704 else
706 set_value_range_to_varying (vr);
707 return;
709 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
713 /* Return value range information for VAR.
715 If we have no values ranges recorded (ie, VRP is not running), then
716 return NULL. Otherwise create an empty range if none existed for VAR. */
718 static value_range_t *
719 get_value_range (const_tree var)
721 static const struct value_range_d vr_const_varying
722 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
723 value_range_t *vr;
724 tree sym;
725 unsigned ver = SSA_NAME_VERSION (var);
727 /* If we have no recorded ranges, then return NULL. */
728 if (! vr_value)
729 return NULL;
731 /* If we query the range for a new SSA name return an unmodifiable VARYING.
732 We should get here at most from the substitute-and-fold stage which
733 will never try to change values. */
734 if (ver >= num_vr_values)
735 return CONST_CAST (value_range_t *, &vr_const_varying);
737 vr = vr_value[ver];
738 if (vr)
739 return vr;
741 /* After propagation finished do not allocate new value-ranges. */
742 if (values_propagated)
743 return CONST_CAST (value_range_t *, &vr_const_varying);
745 /* Create a default value range. */
746 vr_value[ver] = vr = XCNEW (value_range_t);
748 /* Defer allocating the equivalence set. */
749 vr->equiv = NULL;
751 /* If VAR is a default definition of a parameter, the variable can
752 take any value in VAR's type. */
753 if (SSA_NAME_IS_DEFAULT_DEF (var))
755 sym = SSA_NAME_VAR (var);
756 if (TREE_CODE (sym) == PARM_DECL)
758 /* Try to use the "nonnull" attribute to create ~[0, 0]
759 anti-ranges for pointers. Note that this is only valid with
760 default definitions of PARM_DECLs. */
761 if (POINTER_TYPE_P (TREE_TYPE (sym))
762 && nonnull_arg_p (sym))
763 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
764 else
765 set_value_range_to_varying (vr);
767 else if (TREE_CODE (sym) == RESULT_DECL
768 && DECL_BY_REFERENCE (sym))
769 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
772 return vr;
775 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
777 static inline bool
778 vrp_operand_equal_p (const_tree val1, const_tree val2)
780 if (val1 == val2)
781 return true;
782 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
783 return false;
784 if (is_overflow_infinity (val1))
785 return is_overflow_infinity (val2);
786 return true;
789 /* Return true, if the bitmaps B1 and B2 are equal. */
791 static inline bool
792 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
794 return (b1 == b2
795 || ((!b1 || bitmap_empty_p (b1))
796 && (!b2 || bitmap_empty_p (b2)))
797 || (b1 && b2
798 && bitmap_equal_p (b1, b2)));
801 /* Update the value range and equivalence set for variable VAR to
802 NEW_VR. Return true if NEW_VR is different from VAR's previous
803 value.
805 NOTE: This function assumes that NEW_VR is a temporary value range
806 object created for the sole purpose of updating VAR's range. The
807 storage used by the equivalence set from NEW_VR will be freed by
808 this function. Do not call update_value_range when NEW_VR
809 is the range object associated with another SSA name. */
811 static inline bool
812 update_value_range (const_tree var, value_range_t *new_vr)
814 value_range_t *old_vr;
815 bool is_new;
817 /* Update the value range, if necessary. */
818 old_vr = get_value_range (var);
819 is_new = old_vr->type != new_vr->type
820 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
821 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
822 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
824 if (is_new)
826 /* Do not allow transitions up the lattice. The following
827 is slightly more awkward than just new_vr->type < old_vr->type
828 because VR_RANGE and VR_ANTI_RANGE need to be considered
829 the same. We may not have is_new when transitioning to
830 UNDEFINED or from VARYING. */
831 if (new_vr->type == VR_UNDEFINED
832 || old_vr->type == VR_VARYING)
833 set_value_range_to_varying (old_vr);
834 else
835 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
836 new_vr->equiv);
839 BITMAP_FREE (new_vr->equiv);
841 return is_new;
845 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
846 point where equivalence processing can be turned on/off. */
848 static void
849 add_equivalence (bitmap *equiv, const_tree var)
851 unsigned ver = SSA_NAME_VERSION (var);
852 value_range_t *vr = vr_value[ver];
854 if (*equiv == NULL)
855 *equiv = BITMAP_ALLOC (NULL);
856 bitmap_set_bit (*equiv, ver);
857 if (vr && vr->equiv)
858 bitmap_ior_into (*equiv, vr->equiv);
862 /* Return true if VR is ~[0, 0]. */
864 static inline bool
865 range_is_nonnull (value_range_t *vr)
867 return vr->type == VR_ANTI_RANGE
868 && integer_zerop (vr->min)
869 && integer_zerop (vr->max);
873 /* Return true if VR is [0, 0]. */
875 static inline bool
876 range_is_null (value_range_t *vr)
878 return vr->type == VR_RANGE
879 && integer_zerop (vr->min)
880 && integer_zerop (vr->max);
883 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
884 a singleton. */
886 static inline bool
887 range_int_cst_p (value_range_t *vr)
889 return (vr->type == VR_RANGE
890 && TREE_CODE (vr->max) == INTEGER_CST
891 && TREE_CODE (vr->min) == INTEGER_CST);
894 /* Return true if VR is a INTEGER_CST singleton. */
896 static inline bool
897 range_int_cst_singleton_p (value_range_t *vr)
899 return (range_int_cst_p (vr)
900 && !is_overflow_infinity (vr->min)
901 && !is_overflow_infinity (vr->max)
902 && tree_int_cst_equal (vr->min, vr->max));
905 /* Return true if value range VR involves at least one symbol. */
907 static inline bool
908 symbolic_range_p (value_range_t *vr)
910 return (!is_gimple_min_invariant (vr->min)
911 || !is_gimple_min_invariant (vr->max));
914 /* Return true if value range VR uses an overflow infinity. */
916 static inline bool
917 overflow_infinity_range_p (value_range_t *vr)
919 return (vr->type == VR_RANGE
920 && (is_overflow_infinity (vr->min)
921 || is_overflow_infinity (vr->max)));
924 /* Return false if we can not make a valid comparison based on VR;
925 this will be the case if it uses an overflow infinity and overflow
926 is not undefined (i.e., -fno-strict-overflow is in effect).
927 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
928 uses an overflow infinity. */
930 static bool
931 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
933 gcc_assert (vr->type == VR_RANGE);
934 if (is_overflow_infinity (vr->min))
936 *strict_overflow_p = true;
937 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
938 return false;
940 if (is_overflow_infinity (vr->max))
942 *strict_overflow_p = true;
943 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
944 return false;
946 return true;
950 /* Return true if the result of assignment STMT is know to be non-negative.
951 If the return value is based on the assumption that signed overflow is
952 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
953 *STRICT_OVERFLOW_P.*/
955 static bool
956 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
958 enum tree_code code = gimple_assign_rhs_code (stmt);
959 switch (get_gimple_rhs_class (code))
961 case GIMPLE_UNARY_RHS:
962 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
963 gimple_expr_type (stmt),
964 gimple_assign_rhs1 (stmt),
965 strict_overflow_p);
966 case GIMPLE_BINARY_RHS:
967 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
968 gimple_expr_type (stmt),
969 gimple_assign_rhs1 (stmt),
970 gimple_assign_rhs2 (stmt),
971 strict_overflow_p);
972 case GIMPLE_TERNARY_RHS:
973 return false;
974 case GIMPLE_SINGLE_RHS:
975 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
976 strict_overflow_p);
977 case GIMPLE_INVALID_RHS:
978 gcc_unreachable ();
979 default:
980 gcc_unreachable ();
984 /* Return true if return value of call STMT is know to be non-negative.
985 If the return value is based on the assumption that signed overflow is
986 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
987 *STRICT_OVERFLOW_P.*/
989 static bool
990 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
992 tree arg0 = gimple_call_num_args (stmt) > 0 ?
993 gimple_call_arg (stmt, 0) : NULL_TREE;
994 tree arg1 = gimple_call_num_args (stmt) > 1 ?
995 gimple_call_arg (stmt, 1) : NULL_TREE;
997 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
998 gimple_call_fndecl (stmt),
999 arg0,
1000 arg1,
1001 strict_overflow_p);
1004 /* Return true if STMT is know to to compute a non-negative value.
1005 If the return value is based on the assumption that signed overflow is
1006 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1007 *STRICT_OVERFLOW_P.*/
1009 static bool
1010 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1012 switch (gimple_code (stmt))
1014 case GIMPLE_ASSIGN:
1015 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1016 case GIMPLE_CALL:
1017 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1018 default:
1019 gcc_unreachable ();
1023 /* Return true if the result of assignment STMT is know to be non-zero.
1024 If the return value is based on the assumption that signed overflow is
1025 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1026 *STRICT_OVERFLOW_P.*/
1028 static bool
1029 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1031 enum tree_code code = gimple_assign_rhs_code (stmt);
1032 switch (get_gimple_rhs_class (code))
1034 case GIMPLE_UNARY_RHS:
1035 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1036 gimple_expr_type (stmt),
1037 gimple_assign_rhs1 (stmt),
1038 strict_overflow_p);
1039 case GIMPLE_BINARY_RHS:
1040 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1041 gimple_expr_type (stmt),
1042 gimple_assign_rhs1 (stmt),
1043 gimple_assign_rhs2 (stmt),
1044 strict_overflow_p);
1045 case GIMPLE_TERNARY_RHS:
1046 return false;
1047 case GIMPLE_SINGLE_RHS:
1048 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1049 strict_overflow_p);
1050 case GIMPLE_INVALID_RHS:
1051 gcc_unreachable ();
1052 default:
1053 gcc_unreachable ();
1057 /* Return true if STMT is known to compute a non-zero value.
1058 If the return value is based on the assumption that signed overflow is
1059 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1060 *STRICT_OVERFLOW_P.*/
1062 static bool
1063 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1065 switch (gimple_code (stmt))
1067 case GIMPLE_ASSIGN:
1068 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1069 case GIMPLE_CALL:
1071 tree fndecl = gimple_call_fndecl (stmt);
1072 if (!fndecl) return false;
1073 if (flag_delete_null_pointer_checks && !flag_check_new
1074 && DECL_IS_OPERATOR_NEW (fndecl)
1075 && !TREE_NOTHROW (fndecl))
1076 return true;
1077 if (flag_delete_null_pointer_checks &&
1078 lookup_attribute ("returns_nonnull",
1079 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1080 return true;
1081 return gimple_alloca_call_p (stmt);
1083 default:
1084 gcc_unreachable ();
1088 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1089 obtained so far. */
1091 static bool
1092 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1094 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1095 return true;
1097 /* If we have an expression of the form &X->a, then the expression
1098 is nonnull if X is nonnull. */
1099 if (is_gimple_assign (stmt)
1100 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1102 tree expr = gimple_assign_rhs1 (stmt);
1103 tree base = get_base_address (TREE_OPERAND (expr, 0));
1105 if (base != NULL_TREE
1106 && TREE_CODE (base) == MEM_REF
1107 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1109 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1110 if (range_is_nonnull (vr))
1111 return true;
1115 return false;
1118 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1119 a gimple invariant, or SSA_NAME +- CST. */
1121 static bool
1122 valid_value_p (tree expr)
1124 if (TREE_CODE (expr) == SSA_NAME)
1125 return true;
1127 if (TREE_CODE (expr) == PLUS_EXPR
1128 || TREE_CODE (expr) == MINUS_EXPR)
1129 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1130 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1132 return is_gimple_min_invariant (expr);
1135 /* Return
1136 1 if VAL < VAL2
1137 0 if !(VAL < VAL2)
1138 -2 if those are incomparable. */
1139 static inline int
1140 operand_less_p (tree val, tree val2)
1142 /* LT is folded faster than GE and others. Inline the common case. */
1143 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1145 if (TYPE_UNSIGNED (TREE_TYPE (val)))
1146 return INT_CST_LT_UNSIGNED (val, val2);
1147 else
1149 if (INT_CST_LT (val, val2))
1150 return 1;
1153 else
1155 tree tcmp;
1157 fold_defer_overflow_warnings ();
1159 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1161 fold_undefer_and_ignore_overflow_warnings ();
1163 if (!tcmp
1164 || TREE_CODE (tcmp) != INTEGER_CST)
1165 return -2;
1167 if (!integer_zerop (tcmp))
1168 return 1;
1171 /* val >= val2, not considering overflow infinity. */
1172 if (is_negative_overflow_infinity (val))
1173 return is_negative_overflow_infinity (val2) ? 0 : 1;
1174 else if (is_positive_overflow_infinity (val2))
1175 return is_positive_overflow_infinity (val) ? 0 : 1;
1177 return 0;
1180 /* Compare two values VAL1 and VAL2. Return
1182 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1183 -1 if VAL1 < VAL2,
1184 0 if VAL1 == VAL2,
1185 +1 if VAL1 > VAL2, and
1186 +2 if VAL1 != VAL2
1188 This is similar to tree_int_cst_compare but supports pointer values
1189 and values that cannot be compared at compile time.
1191 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1192 true if the return value is only valid if we assume that signed
1193 overflow is undefined. */
1195 static int
1196 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1198 if (val1 == val2)
1199 return 0;
1201 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1202 both integers. */
1203 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1204 == POINTER_TYPE_P (TREE_TYPE (val2)));
1205 /* Convert the two values into the same type. This is needed because
1206 sizetype causes sign extension even for unsigned types. */
1207 val2 = fold_convert (TREE_TYPE (val1), val2);
1208 STRIP_USELESS_TYPE_CONVERSION (val2);
1210 if ((TREE_CODE (val1) == SSA_NAME
1211 || TREE_CODE (val1) == PLUS_EXPR
1212 || TREE_CODE (val1) == MINUS_EXPR)
1213 && (TREE_CODE (val2) == SSA_NAME
1214 || TREE_CODE (val2) == PLUS_EXPR
1215 || TREE_CODE (val2) == MINUS_EXPR))
1217 tree n1, c1, n2, c2;
1218 enum tree_code code1, code2;
1220 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1221 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1222 same name, return -2. */
1223 if (TREE_CODE (val1) == SSA_NAME)
1225 code1 = SSA_NAME;
1226 n1 = val1;
1227 c1 = NULL_TREE;
1229 else
1231 code1 = TREE_CODE (val1);
1232 n1 = TREE_OPERAND (val1, 0);
1233 c1 = TREE_OPERAND (val1, 1);
1234 if (tree_int_cst_sgn (c1) == -1)
1236 if (is_negative_overflow_infinity (c1))
1237 return -2;
1238 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1239 if (!c1)
1240 return -2;
1241 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1245 if (TREE_CODE (val2) == SSA_NAME)
1247 code2 = SSA_NAME;
1248 n2 = val2;
1249 c2 = NULL_TREE;
1251 else
1253 code2 = TREE_CODE (val2);
1254 n2 = TREE_OPERAND (val2, 0);
1255 c2 = TREE_OPERAND (val2, 1);
1256 if (tree_int_cst_sgn (c2) == -1)
1258 if (is_negative_overflow_infinity (c2))
1259 return -2;
1260 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1261 if (!c2)
1262 return -2;
1263 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1267 /* Both values must use the same name. */
1268 if (n1 != n2)
1269 return -2;
1271 if (code1 == SSA_NAME
1272 && code2 == SSA_NAME)
1273 /* NAME == NAME */
1274 return 0;
1276 /* If overflow is defined we cannot simplify more. */
1277 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1278 return -2;
1280 if (strict_overflow_p != NULL
1281 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1282 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1283 *strict_overflow_p = true;
1285 if (code1 == SSA_NAME)
1287 if (code2 == PLUS_EXPR)
1288 /* NAME < NAME + CST */
1289 return -1;
1290 else if (code2 == MINUS_EXPR)
1291 /* NAME > NAME - CST */
1292 return 1;
1294 else if (code1 == PLUS_EXPR)
1296 if (code2 == SSA_NAME)
1297 /* NAME + CST > NAME */
1298 return 1;
1299 else if (code2 == PLUS_EXPR)
1300 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1301 return compare_values_warnv (c1, c2, strict_overflow_p);
1302 else if (code2 == MINUS_EXPR)
1303 /* NAME + CST1 > NAME - CST2 */
1304 return 1;
1306 else if (code1 == MINUS_EXPR)
1308 if (code2 == SSA_NAME)
1309 /* NAME - CST < NAME */
1310 return -1;
1311 else if (code2 == PLUS_EXPR)
1312 /* NAME - CST1 < NAME + CST2 */
1313 return -1;
1314 else if (code2 == MINUS_EXPR)
1315 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1316 C1 and C2 are swapped in the call to compare_values. */
1317 return compare_values_warnv (c2, c1, strict_overflow_p);
1320 gcc_unreachable ();
1323 /* We cannot compare non-constants. */
1324 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1325 return -2;
1327 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1329 /* We cannot compare overflowed values, except for overflow
1330 infinities. */
1331 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1333 if (strict_overflow_p != NULL)
1334 *strict_overflow_p = true;
1335 if (is_negative_overflow_infinity (val1))
1336 return is_negative_overflow_infinity (val2) ? 0 : -1;
1337 else if (is_negative_overflow_infinity (val2))
1338 return 1;
1339 else if (is_positive_overflow_infinity (val1))
1340 return is_positive_overflow_infinity (val2) ? 0 : 1;
1341 else if (is_positive_overflow_infinity (val2))
1342 return -1;
1343 return -2;
1346 return tree_int_cst_compare (val1, val2);
1348 else
1350 tree t;
1352 /* First see if VAL1 and VAL2 are not the same. */
1353 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1354 return 0;
1356 /* If VAL1 is a lower address than VAL2, return -1. */
1357 if (operand_less_p (val1, val2) == 1)
1358 return -1;
1360 /* If VAL1 is a higher address than VAL2, return +1. */
1361 if (operand_less_p (val2, val1) == 1)
1362 return 1;
1364 /* If VAL1 is different than VAL2, return +2.
1365 For integer constants we either have already returned -1 or 1
1366 or they are equivalent. We still might succeed in proving
1367 something about non-trivial operands. */
1368 if (TREE_CODE (val1) != INTEGER_CST
1369 || TREE_CODE (val2) != INTEGER_CST)
1371 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1372 if (t && integer_onep (t))
1373 return 2;
1376 return -2;
1380 /* Compare values like compare_values_warnv, but treat comparisons of
1381 nonconstants which rely on undefined overflow as incomparable. */
1383 static int
1384 compare_values (tree val1, tree val2)
1386 bool sop;
1387 int ret;
1389 sop = false;
1390 ret = compare_values_warnv (val1, val2, &sop);
1391 if (sop
1392 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1393 ret = -2;
1394 return ret;
1398 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1399 0 if VAL is not inside [MIN, MAX],
1400 -2 if we cannot tell either way.
1402 Benchmark compile/20001226-1.c compilation time after changing this
1403 function. */
1405 static inline int
1406 value_inside_range (tree val, tree min, tree max)
1408 int cmp1, cmp2;
1410 cmp1 = operand_less_p (val, min);
1411 if (cmp1 == -2)
1412 return -2;
1413 if (cmp1 == 1)
1414 return 0;
1416 cmp2 = operand_less_p (max, val);
1417 if (cmp2 == -2)
1418 return -2;
1420 return !cmp2;
1424 /* Return true if value ranges VR0 and VR1 have a non-empty
1425 intersection.
1427 Benchmark compile/20001226-1.c compilation time after changing this
1428 function.
1431 static inline bool
1432 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1434 /* The value ranges do not intersect if the maximum of the first range is
1435 less than the minimum of the second range or vice versa.
1436 When those relations are unknown, we can't do any better. */
1437 if (operand_less_p (vr0->max, vr1->min) != 0)
1438 return false;
1439 if (operand_less_p (vr1->max, vr0->min) != 0)
1440 return false;
1441 return true;
1445 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1446 include the value zero, -2 if we cannot tell. */
1448 static inline int
1449 range_includes_zero_p (tree min, tree max)
1451 tree zero = build_int_cst (TREE_TYPE (min), 0);
1452 return value_inside_range (zero, min, max);
1455 /* Return true if *VR is know to only contain nonnegative values. */
1457 static inline bool
1458 value_range_nonnegative_p (value_range_t *vr)
1460 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1461 which would return a useful value should be encoded as a
1462 VR_RANGE. */
1463 if (vr->type == VR_RANGE)
1465 int result = compare_values (vr->min, integer_zero_node);
1466 return (result == 0 || result == 1);
1469 return false;
1472 /* If *VR has a value rante that is a single constant value return that,
1473 otherwise return NULL_TREE. */
1475 static tree
1476 value_range_constant_singleton (value_range_t *vr)
1478 if (vr->type == VR_RANGE
1479 && operand_equal_p (vr->min, vr->max, 0)
1480 && is_gimple_min_invariant (vr->min))
1481 return vr->min;
1483 return NULL_TREE;
1486 /* If OP has a value range with a single constant value return that,
1487 otherwise return NULL_TREE. This returns OP itself if OP is a
1488 constant. */
1490 static tree
1491 op_with_constant_singleton_value_range (tree op)
1493 if (is_gimple_min_invariant (op))
1494 return op;
1496 if (TREE_CODE (op) != SSA_NAME)
1497 return NULL_TREE;
1499 return value_range_constant_singleton (get_value_range (op));
1502 /* Return true if op is in a boolean [0, 1] value-range. */
1504 static bool
1505 op_with_boolean_value_range_p (tree op)
1507 value_range_t *vr;
1509 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1510 return true;
1512 if (integer_zerop (op)
1513 || integer_onep (op))
1514 return true;
1516 if (TREE_CODE (op) != SSA_NAME)
1517 return false;
1519 vr = get_value_range (op);
1520 return (vr->type == VR_RANGE
1521 && integer_zerop (vr->min)
1522 && integer_onep (vr->max));
1525 /* Extract value range information from an ASSERT_EXPR EXPR and store
1526 it in *VR_P. */
1528 static void
1529 extract_range_from_assert (value_range_t *vr_p, tree expr)
1531 tree var, cond, limit, min, max, type;
1532 value_range_t *limit_vr;
1533 enum tree_code cond_code;
1535 var = ASSERT_EXPR_VAR (expr);
1536 cond = ASSERT_EXPR_COND (expr);
1538 gcc_assert (COMPARISON_CLASS_P (cond));
1540 /* Find VAR in the ASSERT_EXPR conditional. */
1541 if (var == TREE_OPERAND (cond, 0)
1542 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1543 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1545 /* If the predicate is of the form VAR COMP LIMIT, then we just
1546 take LIMIT from the RHS and use the same comparison code. */
1547 cond_code = TREE_CODE (cond);
1548 limit = TREE_OPERAND (cond, 1);
1549 cond = TREE_OPERAND (cond, 0);
1551 else
1553 /* If the predicate is of the form LIMIT COMP VAR, then we need
1554 to flip around the comparison code to create the proper range
1555 for VAR. */
1556 cond_code = swap_tree_comparison (TREE_CODE (cond));
1557 limit = TREE_OPERAND (cond, 0);
1558 cond = TREE_OPERAND (cond, 1);
1561 limit = avoid_overflow_infinity (limit);
1563 type = TREE_TYPE (var);
1564 gcc_assert (limit != var);
1566 /* For pointer arithmetic, we only keep track of pointer equality
1567 and inequality. */
1568 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1570 set_value_range_to_varying (vr_p);
1571 return;
1574 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1575 try to use LIMIT's range to avoid creating symbolic ranges
1576 unnecessarily. */
1577 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1579 /* LIMIT's range is only interesting if it has any useful information. */
1580 if (limit_vr
1581 && (limit_vr->type == VR_UNDEFINED
1582 || limit_vr->type == VR_VARYING
1583 || symbolic_range_p (limit_vr)))
1584 limit_vr = NULL;
1586 /* Initially, the new range has the same set of equivalences of
1587 VAR's range. This will be revised before returning the final
1588 value. Since assertions may be chained via mutually exclusive
1589 predicates, we will need to trim the set of equivalences before
1590 we are done. */
1591 gcc_assert (vr_p->equiv == NULL);
1592 add_equivalence (&vr_p->equiv, var);
1594 /* Extract a new range based on the asserted comparison for VAR and
1595 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1596 will only use it for equality comparisons (EQ_EXPR). For any
1597 other kind of assertion, we cannot derive a range from LIMIT's
1598 anti-range that can be used to describe the new range. For
1599 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1600 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1601 no single range for x_2 that could describe LE_EXPR, so we might
1602 as well build the range [b_4, +INF] for it.
1603 One special case we handle is extracting a range from a
1604 range test encoded as (unsigned)var + CST <= limit. */
1605 if (TREE_CODE (cond) == NOP_EXPR
1606 || TREE_CODE (cond) == PLUS_EXPR)
1608 if (TREE_CODE (cond) == PLUS_EXPR)
1610 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1611 TREE_OPERAND (cond, 1));
1612 max = int_const_binop (PLUS_EXPR, limit, min);
1613 cond = TREE_OPERAND (cond, 0);
1615 else
1617 min = build_int_cst (TREE_TYPE (var), 0);
1618 max = limit;
1621 /* Make sure to not set TREE_OVERFLOW on the final type
1622 conversion. We are willingly interpreting large positive
1623 unsigned values as negative singed values here. */
1624 min = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (min),
1625 0, false);
1626 max = force_fit_type_double (TREE_TYPE (var), tree_to_double_int (max),
1627 0, false);
1629 /* We can transform a max, min range to an anti-range or
1630 vice-versa. Use set_and_canonicalize_value_range which does
1631 this for us. */
1632 if (cond_code == LE_EXPR)
1633 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1634 min, max, vr_p->equiv);
1635 else if (cond_code == GT_EXPR)
1636 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1637 min, max, vr_p->equiv);
1638 else
1639 gcc_unreachable ();
1641 else if (cond_code == EQ_EXPR)
1643 enum value_range_type range_type;
1645 if (limit_vr)
1647 range_type = limit_vr->type;
1648 min = limit_vr->min;
1649 max = limit_vr->max;
1651 else
1653 range_type = VR_RANGE;
1654 min = limit;
1655 max = limit;
1658 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1660 /* When asserting the equality VAR == LIMIT and LIMIT is another
1661 SSA name, the new range will also inherit the equivalence set
1662 from LIMIT. */
1663 if (TREE_CODE (limit) == SSA_NAME)
1664 add_equivalence (&vr_p->equiv, limit);
1666 else if (cond_code == NE_EXPR)
1668 /* As described above, when LIMIT's range is an anti-range and
1669 this assertion is an inequality (NE_EXPR), then we cannot
1670 derive anything from the anti-range. For instance, if
1671 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1672 not imply that VAR's range is [0, 0]. So, in the case of
1673 anti-ranges, we just assert the inequality using LIMIT and
1674 not its anti-range.
1676 If LIMIT_VR is a range, we can only use it to build a new
1677 anti-range if LIMIT_VR is a single-valued range. For
1678 instance, if LIMIT_VR is [0, 1], the predicate
1679 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1680 Rather, it means that for value 0 VAR should be ~[0, 0]
1681 and for value 1, VAR should be ~[1, 1]. We cannot
1682 represent these ranges.
1684 The only situation in which we can build a valid
1685 anti-range is when LIMIT_VR is a single-valued range
1686 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1687 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1688 if (limit_vr
1689 && limit_vr->type == VR_RANGE
1690 && compare_values (limit_vr->min, limit_vr->max) == 0)
1692 min = limit_vr->min;
1693 max = limit_vr->max;
1695 else
1697 /* In any other case, we cannot use LIMIT's range to build a
1698 valid anti-range. */
1699 min = max = limit;
1702 /* If MIN and MAX cover the whole range for their type, then
1703 just use the original LIMIT. */
1704 if (INTEGRAL_TYPE_P (type)
1705 && vrp_val_is_min (min)
1706 && vrp_val_is_max (max))
1707 min = max = limit;
1709 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1710 min, max, vr_p->equiv);
1712 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1714 min = TYPE_MIN_VALUE (type);
1716 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1717 max = limit;
1718 else
1720 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1721 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1722 LT_EXPR. */
1723 max = limit_vr->max;
1726 /* If the maximum value forces us to be out of bounds, simply punt.
1727 It would be pointless to try and do anything more since this
1728 all should be optimized away above us. */
1729 if ((cond_code == LT_EXPR
1730 && compare_values (max, min) == 0)
1731 || is_overflow_infinity (max))
1732 set_value_range_to_varying (vr_p);
1733 else
1735 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1736 if (cond_code == LT_EXPR)
1738 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1739 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1740 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1741 build_int_cst (TREE_TYPE (max), -1));
1742 else
1743 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1744 build_int_cst (TREE_TYPE (max), 1));
1745 if (EXPR_P (max))
1746 TREE_NO_WARNING (max) = 1;
1749 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1752 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1754 max = TYPE_MAX_VALUE (type);
1756 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1757 min = limit;
1758 else
1760 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1761 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1762 GT_EXPR. */
1763 min = limit_vr->min;
1766 /* If the minimum value forces us to be out of bounds, simply punt.
1767 It would be pointless to try and do anything more since this
1768 all should be optimized away above us. */
1769 if ((cond_code == GT_EXPR
1770 && compare_values (min, max) == 0)
1771 || is_overflow_infinity (min))
1772 set_value_range_to_varying (vr_p);
1773 else
1775 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1776 if (cond_code == GT_EXPR)
1778 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1779 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1780 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1781 build_int_cst (TREE_TYPE (min), -1));
1782 else
1783 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1784 build_int_cst (TREE_TYPE (min), 1));
1785 if (EXPR_P (min))
1786 TREE_NO_WARNING (min) = 1;
1789 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1792 else
1793 gcc_unreachable ();
1795 /* Finally intersect the new range with what we already know about var. */
1796 vrp_intersect_ranges (vr_p, get_value_range (var));
1800 /* Extract range information from SSA name VAR and store it in VR. If
1801 VAR has an interesting range, use it. Otherwise, create the
1802 range [VAR, VAR] and return it. This is useful in situations where
1803 we may have conditionals testing values of VARYING names. For
1804 instance,
1806 x_3 = y_5;
1807 if (x_3 > y_5)
1810 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1811 always false. */
1813 static void
1814 extract_range_from_ssa_name (value_range_t *vr, tree var)
1816 value_range_t *var_vr = get_value_range (var);
1818 if (var_vr->type != VR_UNDEFINED && var_vr->type != VR_VARYING)
1819 copy_value_range (vr, var_vr);
1820 else
1821 set_value_range (vr, VR_RANGE, var, var, NULL);
1823 add_equivalence (&vr->equiv, var);
1827 /* Wrapper around int_const_binop. If the operation overflows and we
1828 are not using wrapping arithmetic, then adjust the result to be
1829 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1830 NULL_TREE if we need to use an overflow infinity representation but
1831 the type does not support it. */
1833 static tree
1834 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1836 tree res;
1838 res = int_const_binop (code, val1, val2);
1840 /* If we are using unsigned arithmetic, operate symbolically
1841 on -INF and +INF as int_const_binop only handles signed overflow. */
1842 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1844 int checkz = compare_values (res, val1);
1845 bool overflow = false;
1847 /* Ensure that res = val1 [+*] val2 >= val1
1848 or that res = val1 - val2 <= val1. */
1849 if ((code == PLUS_EXPR
1850 && !(checkz == 1 || checkz == 0))
1851 || (code == MINUS_EXPR
1852 && !(checkz == 0 || checkz == -1)))
1854 overflow = true;
1856 /* Checking for multiplication overflow is done by dividing the
1857 output of the multiplication by the first input of the
1858 multiplication. If the result of that division operation is
1859 not equal to the second input of the multiplication, then the
1860 multiplication overflowed. */
1861 else if (code == MULT_EXPR && !integer_zerop (val1))
1863 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1864 res,
1865 val1);
1866 int check = compare_values (tmp, val2);
1868 if (check != 0)
1869 overflow = true;
1872 if (overflow)
1874 res = copy_node (res);
1875 TREE_OVERFLOW (res) = 1;
1879 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1880 /* If the singed operation wraps then int_const_binop has done
1881 everything we want. */
1883 else if ((TREE_OVERFLOW (res)
1884 && !TREE_OVERFLOW (val1)
1885 && !TREE_OVERFLOW (val2))
1886 || is_overflow_infinity (val1)
1887 || is_overflow_infinity (val2))
1889 /* If the operation overflowed but neither VAL1 nor VAL2 are
1890 overflown, return -INF or +INF depending on the operation
1891 and the combination of signs of the operands. */
1892 int sgn1 = tree_int_cst_sgn (val1);
1893 int sgn2 = tree_int_cst_sgn (val2);
1895 if (needs_overflow_infinity (TREE_TYPE (res))
1896 && !supports_overflow_infinity (TREE_TYPE (res)))
1897 return NULL_TREE;
1899 /* We have to punt on adding infinities of different signs,
1900 since we can't tell what the sign of the result should be.
1901 Likewise for subtracting infinities of the same sign. */
1902 if (((code == PLUS_EXPR && sgn1 != sgn2)
1903 || (code == MINUS_EXPR && sgn1 == sgn2))
1904 && is_overflow_infinity (val1)
1905 && is_overflow_infinity (val2))
1906 return NULL_TREE;
1908 /* Don't try to handle division or shifting of infinities. */
1909 if ((code == TRUNC_DIV_EXPR
1910 || code == FLOOR_DIV_EXPR
1911 || code == CEIL_DIV_EXPR
1912 || code == EXACT_DIV_EXPR
1913 || code == ROUND_DIV_EXPR
1914 || code == RSHIFT_EXPR)
1915 && (is_overflow_infinity (val1)
1916 || is_overflow_infinity (val2)))
1917 return NULL_TREE;
1919 /* Notice that we only need to handle the restricted set of
1920 operations handled by extract_range_from_binary_expr.
1921 Among them, only multiplication, addition and subtraction
1922 can yield overflow without overflown operands because we
1923 are working with integral types only... except in the
1924 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1925 for division too. */
1927 /* For multiplication, the sign of the overflow is given
1928 by the comparison of the signs of the operands. */
1929 if ((code == MULT_EXPR && sgn1 == sgn2)
1930 /* For addition, the operands must be of the same sign
1931 to yield an overflow. Its sign is therefore that
1932 of one of the operands, for example the first. For
1933 infinite operands X + -INF is negative, not positive. */
1934 || (code == PLUS_EXPR
1935 && (sgn1 >= 0
1936 ? !is_negative_overflow_infinity (val2)
1937 : is_positive_overflow_infinity (val2)))
1938 /* For subtraction, non-infinite operands must be of
1939 different signs to yield an overflow. Its sign is
1940 therefore that of the first operand or the opposite of
1941 that of the second operand. A first operand of 0 counts
1942 as positive here, for the corner case 0 - (-INF), which
1943 overflows, but must yield +INF. For infinite operands 0
1944 - INF is negative, not positive. */
1945 || (code == MINUS_EXPR
1946 && (sgn1 >= 0
1947 ? !is_positive_overflow_infinity (val2)
1948 : is_negative_overflow_infinity (val2)))
1949 /* We only get in here with positive shift count, so the
1950 overflow direction is the same as the sign of val1.
1951 Actually rshift does not overflow at all, but we only
1952 handle the case of shifting overflowed -INF and +INF. */
1953 || (code == RSHIFT_EXPR
1954 && sgn1 >= 0)
1955 /* For division, the only case is -INF / -1 = +INF. */
1956 || code == TRUNC_DIV_EXPR
1957 || code == FLOOR_DIV_EXPR
1958 || code == CEIL_DIV_EXPR
1959 || code == EXACT_DIV_EXPR
1960 || code == ROUND_DIV_EXPR)
1961 return (needs_overflow_infinity (TREE_TYPE (res))
1962 ? positive_overflow_infinity (TREE_TYPE (res))
1963 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1964 else
1965 return (needs_overflow_infinity (TREE_TYPE (res))
1966 ? negative_overflow_infinity (TREE_TYPE (res))
1967 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1970 return res;
1974 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1975 bitmask if some bit is unset, it means for all numbers in the range
1976 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1977 bitmask if some bit is set, it means for all numbers in the range
1978 the bit is 1, otherwise it might be 0 or 1. */
1980 static bool
1981 zero_nonzero_bits_from_vr (value_range_t *vr,
1982 double_int *may_be_nonzero,
1983 double_int *must_be_nonzero)
1985 *may_be_nonzero = double_int_minus_one;
1986 *must_be_nonzero = double_int_zero;
1987 if (!range_int_cst_p (vr)
1988 || is_overflow_infinity (vr->min)
1989 || is_overflow_infinity (vr->max))
1990 return false;
1992 if (range_int_cst_singleton_p (vr))
1994 *may_be_nonzero = tree_to_double_int (vr->min);
1995 *must_be_nonzero = *may_be_nonzero;
1997 else if (tree_int_cst_sgn (vr->min) >= 0
1998 || tree_int_cst_sgn (vr->max) < 0)
2000 double_int dmin = tree_to_double_int (vr->min);
2001 double_int dmax = tree_to_double_int (vr->max);
2002 double_int xor_mask = dmin ^ dmax;
2003 *may_be_nonzero = dmin | dmax;
2004 *must_be_nonzero = dmin & dmax;
2005 if (xor_mask.high != 0)
2007 unsigned HOST_WIDE_INT mask
2008 = ((unsigned HOST_WIDE_INT) 1
2009 << floor_log2 (xor_mask.high)) - 1;
2010 may_be_nonzero->low = ALL_ONES;
2011 may_be_nonzero->high |= mask;
2012 must_be_nonzero->low = 0;
2013 must_be_nonzero->high &= ~mask;
2015 else if (xor_mask.low != 0)
2017 unsigned HOST_WIDE_INT mask
2018 = ((unsigned HOST_WIDE_INT) 1
2019 << floor_log2 (xor_mask.low)) - 1;
2020 may_be_nonzero->low |= mask;
2021 must_be_nonzero->low &= ~mask;
2025 return true;
2028 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2029 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2030 false otherwise. If *AR can be represented with a single range
2031 *VR1 will be VR_UNDEFINED. */
2033 static bool
2034 ranges_from_anti_range (value_range_t *ar,
2035 value_range_t *vr0, value_range_t *vr1)
2037 tree type = TREE_TYPE (ar->min);
2039 vr0->type = VR_UNDEFINED;
2040 vr1->type = VR_UNDEFINED;
2042 if (ar->type != VR_ANTI_RANGE
2043 || TREE_CODE (ar->min) != INTEGER_CST
2044 || TREE_CODE (ar->max) != INTEGER_CST
2045 || !vrp_val_min (type)
2046 || !vrp_val_max (type))
2047 return false;
2049 if (!vrp_val_is_min (ar->min))
2051 vr0->type = VR_RANGE;
2052 vr0->min = vrp_val_min (type);
2053 vr0->max
2054 = double_int_to_tree (type,
2055 tree_to_double_int (ar->min) - double_int_one);
2057 if (!vrp_val_is_max (ar->max))
2059 vr1->type = VR_RANGE;
2060 vr1->min
2061 = double_int_to_tree (type,
2062 tree_to_double_int (ar->max) + double_int_one);
2063 vr1->max = vrp_val_max (type);
2065 if (vr0->type == VR_UNDEFINED)
2067 *vr0 = *vr1;
2068 vr1->type = VR_UNDEFINED;
2071 return vr0->type != VR_UNDEFINED;
2074 /* Helper to extract a value-range *VR for a multiplicative operation
2075 *VR0 CODE *VR1. */
2077 static void
2078 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2079 enum tree_code code,
2080 value_range_t *vr0, value_range_t *vr1)
2082 enum value_range_type type;
2083 tree val[4];
2084 size_t i;
2085 tree min, max;
2086 bool sop;
2087 int cmp;
2089 /* Multiplications, divisions and shifts are a bit tricky to handle,
2090 depending on the mix of signs we have in the two ranges, we
2091 need to operate on different values to get the minimum and
2092 maximum values for the new range. One approach is to figure
2093 out all the variations of range combinations and do the
2094 operations.
2096 However, this involves several calls to compare_values and it
2097 is pretty convoluted. It's simpler to do the 4 operations
2098 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2099 MAX1) and then figure the smallest and largest values to form
2100 the new range. */
2101 gcc_assert (code == MULT_EXPR
2102 || code == TRUNC_DIV_EXPR
2103 || code == FLOOR_DIV_EXPR
2104 || code == CEIL_DIV_EXPR
2105 || code == EXACT_DIV_EXPR
2106 || code == ROUND_DIV_EXPR
2107 || code == RSHIFT_EXPR
2108 || code == LSHIFT_EXPR);
2109 gcc_assert ((vr0->type == VR_RANGE
2110 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2111 && vr0->type == vr1->type);
2113 type = vr0->type;
2115 /* Compute the 4 cross operations. */
2116 sop = false;
2117 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2118 if (val[0] == NULL_TREE)
2119 sop = true;
2121 if (vr1->max == vr1->min)
2122 val[1] = NULL_TREE;
2123 else
2125 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2126 if (val[1] == NULL_TREE)
2127 sop = true;
2130 if (vr0->max == vr0->min)
2131 val[2] = NULL_TREE;
2132 else
2134 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2135 if (val[2] == NULL_TREE)
2136 sop = true;
2139 if (vr0->min == vr0->max || vr1->min == vr1->max)
2140 val[3] = NULL_TREE;
2141 else
2143 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2144 if (val[3] == NULL_TREE)
2145 sop = true;
2148 if (sop)
2150 set_value_range_to_varying (vr);
2151 return;
2154 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2155 of VAL[i]. */
2156 min = val[0];
2157 max = val[0];
2158 for (i = 1; i < 4; i++)
2160 if (!is_gimple_min_invariant (min)
2161 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2162 || !is_gimple_min_invariant (max)
2163 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2164 break;
2166 if (val[i])
2168 if (!is_gimple_min_invariant (val[i])
2169 || (TREE_OVERFLOW (val[i])
2170 && !is_overflow_infinity (val[i])))
2172 /* If we found an overflowed value, set MIN and MAX
2173 to it so that we set the resulting range to
2174 VARYING. */
2175 min = max = val[i];
2176 break;
2179 if (compare_values (val[i], min) == -1)
2180 min = val[i];
2182 if (compare_values (val[i], max) == 1)
2183 max = val[i];
2187 /* If either MIN or MAX overflowed, then set the resulting range to
2188 VARYING. But we do accept an overflow infinity
2189 representation. */
2190 if (min == NULL_TREE
2191 || !is_gimple_min_invariant (min)
2192 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2193 || max == NULL_TREE
2194 || !is_gimple_min_invariant (max)
2195 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2197 set_value_range_to_varying (vr);
2198 return;
2201 /* We punt if:
2202 1) [-INF, +INF]
2203 2) [-INF, +-INF(OVF)]
2204 3) [+-INF(OVF), +INF]
2205 4) [+-INF(OVF), +-INF(OVF)]
2206 We learn nothing when we have INF and INF(OVF) on both sides.
2207 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2208 overflow. */
2209 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2210 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2212 set_value_range_to_varying (vr);
2213 return;
2216 cmp = compare_values (min, max);
2217 if (cmp == -2 || cmp == 1)
2219 /* If the new range has its limits swapped around (MIN > MAX),
2220 then the operation caused one of them to wrap around, mark
2221 the new range VARYING. */
2222 set_value_range_to_varying (vr);
2224 else
2225 set_value_range (vr, type, min, max, NULL);
2228 /* Some quadruple precision helpers. */
2229 static int
2230 quad_int_cmp (double_int l0, double_int h0,
2231 double_int l1, double_int h1, bool uns)
2233 int c = h0.cmp (h1, uns);
2234 if (c != 0) return c;
2235 return l0.ucmp (l1);
2238 static void
2239 quad_int_pair_sort (double_int *l0, double_int *h0,
2240 double_int *l1, double_int *h1, bool uns)
2242 if (quad_int_cmp (*l0, *h0, *l1, *h1, uns) > 0)
2244 double_int tmp;
2245 tmp = *l0; *l0 = *l1; *l1 = tmp;
2246 tmp = *h0; *h0 = *h1; *h1 = tmp;
2250 /* Extract range information from a binary operation CODE based on
2251 the ranges of each of its operands, *VR0 and *VR1 with resulting
2252 type EXPR_TYPE. The resulting range is stored in *VR. */
2254 static void
2255 extract_range_from_binary_expr_1 (value_range_t *vr,
2256 enum tree_code code, tree expr_type,
2257 value_range_t *vr0_, value_range_t *vr1_)
2259 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2260 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2261 enum value_range_type type;
2262 tree min = NULL_TREE, max = NULL_TREE;
2263 int cmp;
2265 if (!INTEGRAL_TYPE_P (expr_type)
2266 && !POINTER_TYPE_P (expr_type))
2268 set_value_range_to_varying (vr);
2269 return;
2272 /* Not all binary expressions can be applied to ranges in a
2273 meaningful way. Handle only arithmetic operations. */
2274 if (code != PLUS_EXPR
2275 && code != MINUS_EXPR
2276 && code != POINTER_PLUS_EXPR
2277 && code != MULT_EXPR
2278 && code != TRUNC_DIV_EXPR
2279 && code != FLOOR_DIV_EXPR
2280 && code != CEIL_DIV_EXPR
2281 && code != EXACT_DIV_EXPR
2282 && code != ROUND_DIV_EXPR
2283 && code != TRUNC_MOD_EXPR
2284 && code != RSHIFT_EXPR
2285 && code != LSHIFT_EXPR
2286 && code != MIN_EXPR
2287 && code != MAX_EXPR
2288 && code != BIT_AND_EXPR
2289 && code != BIT_IOR_EXPR
2290 && code != BIT_XOR_EXPR)
2292 set_value_range_to_varying (vr);
2293 return;
2296 /* If both ranges are UNDEFINED, so is the result. */
2297 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2299 set_value_range_to_undefined (vr);
2300 return;
2302 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2303 code. At some point we may want to special-case operations that
2304 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2305 operand. */
2306 else if (vr0.type == VR_UNDEFINED)
2307 set_value_range_to_varying (&vr0);
2308 else if (vr1.type == VR_UNDEFINED)
2309 set_value_range_to_varying (&vr1);
2311 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2312 and express ~[] op X as ([]' op X) U ([]'' op X). */
2313 if (vr0.type == VR_ANTI_RANGE
2314 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2316 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2317 if (vrtem1.type != VR_UNDEFINED)
2319 value_range_t vrres = VR_INITIALIZER;
2320 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2321 &vrtem1, vr1_);
2322 vrp_meet (vr, &vrres);
2324 return;
2326 /* Likewise for X op ~[]. */
2327 if (vr1.type == VR_ANTI_RANGE
2328 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2330 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2331 if (vrtem1.type != VR_UNDEFINED)
2333 value_range_t vrres = VR_INITIALIZER;
2334 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2335 vr0_, &vrtem1);
2336 vrp_meet (vr, &vrres);
2338 return;
2341 /* The type of the resulting value range defaults to VR0.TYPE. */
2342 type = vr0.type;
2344 /* Refuse to operate on VARYING ranges, ranges of different kinds
2345 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2346 because we may be able to derive a useful range even if one of
2347 the operands is VR_VARYING or symbolic range. Similarly for
2348 divisions. TODO, we may be able to derive anti-ranges in
2349 some cases. */
2350 if (code != BIT_AND_EXPR
2351 && code != BIT_IOR_EXPR
2352 && code != TRUNC_DIV_EXPR
2353 && code != FLOOR_DIV_EXPR
2354 && code != CEIL_DIV_EXPR
2355 && code != EXACT_DIV_EXPR
2356 && code != ROUND_DIV_EXPR
2357 && code != TRUNC_MOD_EXPR
2358 && code != MIN_EXPR
2359 && code != MAX_EXPR
2360 && (vr0.type == VR_VARYING
2361 || vr1.type == VR_VARYING
2362 || vr0.type != vr1.type
2363 || symbolic_range_p (&vr0)
2364 || symbolic_range_p (&vr1)))
2366 set_value_range_to_varying (vr);
2367 return;
2370 /* Now evaluate the expression to determine the new range. */
2371 if (POINTER_TYPE_P (expr_type))
2373 if (code == MIN_EXPR || code == MAX_EXPR)
2375 /* For MIN/MAX expressions with pointers, we only care about
2376 nullness, if both are non null, then the result is nonnull.
2377 If both are null, then the result is null. Otherwise they
2378 are varying. */
2379 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2380 set_value_range_to_nonnull (vr, expr_type);
2381 else if (range_is_null (&vr0) && range_is_null (&vr1))
2382 set_value_range_to_null (vr, expr_type);
2383 else
2384 set_value_range_to_varying (vr);
2386 else if (code == POINTER_PLUS_EXPR)
2388 /* For pointer types, we are really only interested in asserting
2389 whether the expression evaluates to non-NULL. */
2390 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2391 set_value_range_to_nonnull (vr, expr_type);
2392 else if (range_is_null (&vr0) && range_is_null (&vr1))
2393 set_value_range_to_null (vr, expr_type);
2394 else
2395 set_value_range_to_varying (vr);
2397 else if (code == BIT_AND_EXPR)
2399 /* For pointer types, we are really only interested in asserting
2400 whether the expression evaluates to non-NULL. */
2401 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2402 set_value_range_to_nonnull (vr, expr_type);
2403 else if (range_is_null (&vr0) || range_is_null (&vr1))
2404 set_value_range_to_null (vr, expr_type);
2405 else
2406 set_value_range_to_varying (vr);
2408 else
2409 set_value_range_to_varying (vr);
2411 return;
2414 /* For integer ranges, apply the operation to each end of the
2415 range and see what we end up with. */
2416 if (code == PLUS_EXPR || code == MINUS_EXPR)
2418 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2419 ranges compute the precise range for such case if possible. */
2420 if (range_int_cst_p (&vr0)
2421 && range_int_cst_p (&vr1)
2422 /* We need as many bits as the possibly unsigned inputs. */
2423 && TYPE_PRECISION (expr_type) <= HOST_BITS_PER_DOUBLE_INT)
2425 double_int min0 = tree_to_double_int (vr0.min);
2426 double_int max0 = tree_to_double_int (vr0.max);
2427 double_int min1 = tree_to_double_int (vr1.min);
2428 double_int max1 = tree_to_double_int (vr1.max);
2429 bool uns = TYPE_UNSIGNED (expr_type);
2430 double_int type_min
2431 = double_int::min_value (TYPE_PRECISION (expr_type), uns);
2432 double_int type_max
2433 = double_int::max_value (TYPE_PRECISION (expr_type), uns);
2434 double_int dmin, dmax;
2435 int min_ovf = 0;
2436 int max_ovf = 0;
2438 if (code == PLUS_EXPR)
2440 dmin = min0 + min1;
2441 dmax = max0 + max1;
2443 /* Check for overflow in double_int. */
2444 if (min1.cmp (double_int_zero, uns) != dmin.cmp (min0, uns))
2445 min_ovf = min0.cmp (dmin, uns);
2446 if (max1.cmp (double_int_zero, uns) != dmax.cmp (max0, uns))
2447 max_ovf = max0.cmp (dmax, uns);
2449 else /* if (code == MINUS_EXPR) */
2451 dmin = min0 - max1;
2452 dmax = max0 - min1;
2454 if (double_int_zero.cmp (max1, uns) != dmin.cmp (min0, uns))
2455 min_ovf = min0.cmp (max1, uns);
2456 if (double_int_zero.cmp (min1, uns) != dmax.cmp (max0, uns))
2457 max_ovf = max0.cmp (min1, uns);
2460 /* For non-wrapping arithmetic look at possibly smaller
2461 value-ranges of the type. */
2462 if (!TYPE_OVERFLOW_WRAPS (expr_type))
2464 if (vrp_val_min (expr_type))
2465 type_min = tree_to_double_int (vrp_val_min (expr_type));
2466 if (vrp_val_max (expr_type))
2467 type_max = tree_to_double_int (vrp_val_max (expr_type));
2470 /* Check for type overflow. */
2471 if (min_ovf == 0)
2473 if (dmin.cmp (type_min, uns) == -1)
2474 min_ovf = -1;
2475 else if (dmin.cmp (type_max, uns) == 1)
2476 min_ovf = 1;
2478 if (max_ovf == 0)
2480 if (dmax.cmp (type_min, uns) == -1)
2481 max_ovf = -1;
2482 else if (dmax.cmp (type_max, uns) == 1)
2483 max_ovf = 1;
2486 if (TYPE_OVERFLOW_WRAPS (expr_type))
2488 /* If overflow wraps, truncate the values and adjust the
2489 range kind and bounds appropriately. */
2490 double_int tmin
2491 = dmin.ext (TYPE_PRECISION (expr_type), uns);
2492 double_int tmax
2493 = dmax.ext (TYPE_PRECISION (expr_type), uns);
2494 if (min_ovf == max_ovf)
2496 /* No overflow or both overflow or underflow. The
2497 range kind stays VR_RANGE. */
2498 min = double_int_to_tree (expr_type, tmin);
2499 max = double_int_to_tree (expr_type, tmax);
2501 else if (min_ovf == -1
2502 && max_ovf == 1)
2504 /* Underflow and overflow, drop to VR_VARYING. */
2505 set_value_range_to_varying (vr);
2506 return;
2508 else
2510 /* Min underflow or max overflow. The range kind
2511 changes to VR_ANTI_RANGE. */
2512 bool covers = false;
2513 double_int tem = tmin;
2514 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2515 || (max_ovf == 1 && min_ovf == 0));
2516 type = VR_ANTI_RANGE;
2517 tmin = tmax + double_int_one;
2518 if (tmin.cmp (tmax, uns) < 0)
2519 covers = true;
2520 tmax = tem + double_int_minus_one;
2521 if (tmax.cmp (tem, uns) > 0)
2522 covers = true;
2523 /* If the anti-range would cover nothing, drop to varying.
2524 Likewise if the anti-range bounds are outside of the
2525 types values. */
2526 if (covers || tmin.cmp (tmax, uns) > 0)
2528 set_value_range_to_varying (vr);
2529 return;
2531 min = double_int_to_tree (expr_type, tmin);
2532 max = double_int_to_tree (expr_type, tmax);
2535 else
2537 /* If overflow does not wrap, saturate to the types min/max
2538 value. */
2539 if (min_ovf == -1)
2541 if (needs_overflow_infinity (expr_type)
2542 && supports_overflow_infinity (expr_type))
2543 min = negative_overflow_infinity (expr_type);
2544 else
2545 min = double_int_to_tree (expr_type, type_min);
2547 else if (min_ovf == 1)
2549 if (needs_overflow_infinity (expr_type)
2550 && supports_overflow_infinity (expr_type))
2551 min = positive_overflow_infinity (expr_type);
2552 else
2553 min = double_int_to_tree (expr_type, type_max);
2555 else
2556 min = double_int_to_tree (expr_type, dmin);
2558 if (max_ovf == -1)
2560 if (needs_overflow_infinity (expr_type)
2561 && supports_overflow_infinity (expr_type))
2562 max = negative_overflow_infinity (expr_type);
2563 else
2564 max = double_int_to_tree (expr_type, type_min);
2566 else if (max_ovf == 1)
2568 if (needs_overflow_infinity (expr_type)
2569 && supports_overflow_infinity (expr_type))
2570 max = positive_overflow_infinity (expr_type);
2571 else
2572 max = double_int_to_tree (expr_type, type_max);
2574 else
2575 max = double_int_to_tree (expr_type, dmax);
2577 if (needs_overflow_infinity (expr_type)
2578 && supports_overflow_infinity (expr_type))
2580 if (is_negative_overflow_infinity (vr0.min)
2581 || (code == PLUS_EXPR
2582 ? is_negative_overflow_infinity (vr1.min)
2583 : is_positive_overflow_infinity (vr1.max)))
2584 min = negative_overflow_infinity (expr_type);
2585 if (is_positive_overflow_infinity (vr0.max)
2586 || (code == PLUS_EXPR
2587 ? is_positive_overflow_infinity (vr1.max)
2588 : is_negative_overflow_infinity (vr1.min)))
2589 max = positive_overflow_infinity (expr_type);
2592 else
2594 /* For other cases, for example if we have a PLUS_EXPR with two
2595 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2596 to compute a precise range for such a case.
2597 ??? General even mixed range kind operations can be expressed
2598 by for example transforming ~[3, 5] + [1, 2] to range-only
2599 operations and a union primitive:
2600 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2601 [-INF+1, 4] U [6, +INF(OVF)]
2602 though usually the union is not exactly representable with
2603 a single range or anti-range as the above is
2604 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2605 but one could use a scheme similar to equivalences for this. */
2606 set_value_range_to_varying (vr);
2607 return;
2610 else if (code == MIN_EXPR
2611 || code == MAX_EXPR)
2613 if (vr0.type == VR_RANGE
2614 && !symbolic_range_p (&vr0))
2616 type = VR_RANGE;
2617 if (vr1.type == VR_RANGE
2618 && !symbolic_range_p (&vr1))
2620 /* For operations that make the resulting range directly
2621 proportional to the original ranges, apply the operation to
2622 the same end of each range. */
2623 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2624 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2626 else if (code == MIN_EXPR)
2628 min = vrp_val_min (expr_type);
2629 max = vr0.max;
2631 else if (code == MAX_EXPR)
2633 min = vr0.min;
2634 max = vrp_val_max (expr_type);
2637 else if (vr1.type == VR_RANGE
2638 && !symbolic_range_p (&vr1))
2640 type = VR_RANGE;
2641 if (code == MIN_EXPR)
2643 min = vrp_val_min (expr_type);
2644 max = vr1.max;
2646 else if (code == MAX_EXPR)
2648 min = vr1.min;
2649 max = vrp_val_max (expr_type);
2652 else
2654 set_value_range_to_varying (vr);
2655 return;
2658 else if (code == MULT_EXPR)
2660 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2661 drop to varying. */
2662 if (range_int_cst_p (&vr0)
2663 && range_int_cst_p (&vr1)
2664 && TYPE_OVERFLOW_WRAPS (expr_type))
2666 double_int min0, max0, min1, max1, sizem1, size;
2667 double_int prod0l, prod0h, prod1l, prod1h,
2668 prod2l, prod2h, prod3l, prod3h;
2669 bool uns0, uns1, uns;
2671 sizem1 = double_int::max_value (TYPE_PRECISION (expr_type), true);
2672 size = sizem1 + double_int_one;
2674 min0 = tree_to_double_int (vr0.min);
2675 max0 = tree_to_double_int (vr0.max);
2676 min1 = tree_to_double_int (vr1.min);
2677 max1 = tree_to_double_int (vr1.max);
2679 uns0 = TYPE_UNSIGNED (expr_type);
2680 uns1 = uns0;
2682 /* Canonicalize the intervals. */
2683 if (TYPE_UNSIGNED (expr_type))
2685 double_int min2 = size - min0;
2686 if (!min2.is_zero () && min2.cmp (max0, true) < 0)
2688 min0 = -min2;
2689 max0 -= size;
2690 uns0 = false;
2693 min2 = size - min1;
2694 if (!min2.is_zero () && min2.cmp (max1, true) < 0)
2696 min1 = -min2;
2697 max1 -= size;
2698 uns1 = false;
2701 uns = uns0 & uns1;
2703 bool overflow;
2704 prod0l = min0.wide_mul_with_sign (min1, true, &prod0h, &overflow);
2705 if (!uns0 && min0.is_negative ())
2706 prod0h -= min1;
2707 if (!uns1 && min1.is_negative ())
2708 prod0h -= min0;
2710 prod1l = min0.wide_mul_with_sign (max1, true, &prod1h, &overflow);
2711 if (!uns0 && min0.is_negative ())
2712 prod1h -= max1;
2713 if (!uns1 && max1.is_negative ())
2714 prod1h -= min0;
2716 prod2l = max0.wide_mul_with_sign (min1, true, &prod2h, &overflow);
2717 if (!uns0 && max0.is_negative ())
2718 prod2h -= min1;
2719 if (!uns1 && min1.is_negative ())
2720 prod2h -= max0;
2722 prod3l = max0.wide_mul_with_sign (max1, true, &prod3h, &overflow);
2723 if (!uns0 && max0.is_negative ())
2724 prod3h -= max1;
2725 if (!uns1 && max1.is_negative ())
2726 prod3h -= max0;
2728 /* Sort the 4 products. */
2729 quad_int_pair_sort (&prod0l, &prod0h, &prod3l, &prod3h, uns);
2730 quad_int_pair_sort (&prod1l, &prod1h, &prod2l, &prod2h, uns);
2731 quad_int_pair_sort (&prod0l, &prod0h, &prod1l, &prod1h, uns);
2732 quad_int_pair_sort (&prod2l, &prod2h, &prod3l, &prod3h, uns);
2734 /* Max - min. */
2735 if (prod0l.is_zero ())
2737 prod1l = double_int_zero;
2738 prod1h = -prod0h;
2740 else
2742 prod1l = -prod0l;
2743 prod1h = ~prod0h;
2745 prod2l = prod3l + prod1l;
2746 prod2h = prod3h + prod1h;
2747 if (prod2l.ult (prod3l))
2748 prod2h += double_int_one; /* carry */
2750 if (!prod2h.is_zero ()
2751 || prod2l.cmp (sizem1, true) >= 0)
2753 /* the range covers all values. */
2754 set_value_range_to_varying (vr);
2755 return;
2758 /* The following should handle the wrapping and selecting
2759 VR_ANTI_RANGE for us. */
2760 min = double_int_to_tree (expr_type, prod0l);
2761 max = double_int_to_tree (expr_type, prod3l);
2762 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2763 return;
2766 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2767 drop to VR_VARYING. It would take more effort to compute a
2768 precise range for such a case. For example, if we have
2769 op0 == 65536 and op1 == 65536 with their ranges both being
2770 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2771 we cannot claim that the product is in ~[0,0]. Note that we
2772 are guaranteed to have vr0.type == vr1.type at this
2773 point. */
2774 if (vr0.type == VR_ANTI_RANGE
2775 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2777 set_value_range_to_varying (vr);
2778 return;
2781 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2782 return;
2784 else if (code == RSHIFT_EXPR
2785 || code == LSHIFT_EXPR)
2787 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2788 then drop to VR_VARYING. Outside of this range we get undefined
2789 behavior from the shift operation. We cannot even trust
2790 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2791 shifts, and the operation at the tree level may be widened. */
2792 if (range_int_cst_p (&vr1)
2793 && compare_tree_int (vr1.min, 0) >= 0
2794 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2796 if (code == RSHIFT_EXPR)
2798 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2799 return;
2801 /* We can map lshifts by constants to MULT_EXPR handling. */
2802 else if (code == LSHIFT_EXPR
2803 && range_int_cst_singleton_p (&vr1))
2805 bool saved_flag_wrapv;
2806 value_range_t vr1p = VR_INITIALIZER;
2807 vr1p.type = VR_RANGE;
2808 vr1p.min
2809 = double_int_to_tree (expr_type,
2810 double_int_one
2811 .llshift (TREE_INT_CST_LOW (vr1.min),
2812 TYPE_PRECISION (expr_type)));
2813 vr1p.max = vr1p.min;
2814 /* We have to use a wrapping multiply though as signed overflow
2815 on lshifts is implementation defined in C89. */
2816 saved_flag_wrapv = flag_wrapv;
2817 flag_wrapv = 1;
2818 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2819 &vr0, &vr1p);
2820 flag_wrapv = saved_flag_wrapv;
2821 return;
2823 else if (code == LSHIFT_EXPR
2824 && range_int_cst_p (&vr0))
2826 int prec = TYPE_PRECISION (expr_type);
2827 int overflow_pos = prec;
2828 int bound_shift;
2829 double_int bound, complement, low_bound, high_bound;
2830 bool uns = TYPE_UNSIGNED (expr_type);
2831 bool in_bounds = false;
2833 if (!uns)
2834 overflow_pos -= 1;
2836 bound_shift = overflow_pos - TREE_INT_CST_LOW (vr1.max);
2837 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2838 overflow. However, for that to happen, vr1.max needs to be
2839 zero, which means vr1 is a singleton range of zero, which
2840 means it should be handled by the previous LSHIFT_EXPR
2841 if-clause. */
2842 bound = double_int_one.llshift (bound_shift, prec);
2843 complement = ~(bound - double_int_one);
2845 if (uns)
2847 low_bound = bound.zext (prec);
2848 high_bound = complement.zext (prec);
2849 if (tree_to_double_int (vr0.max).ult (low_bound))
2851 /* [5, 6] << [1, 2] == [10, 24]. */
2852 /* We're shifting out only zeroes, the value increases
2853 monotonically. */
2854 in_bounds = true;
2856 else if (high_bound.ult (tree_to_double_int (vr0.min)))
2858 /* [0xffffff00, 0xffffffff] << [1, 2]
2859 == [0xfffffc00, 0xfffffffe]. */
2860 /* We're shifting out only ones, the value decreases
2861 monotonically. */
2862 in_bounds = true;
2865 else
2867 /* [-1, 1] << [1, 2] == [-4, 4]. */
2868 low_bound = complement.sext (prec);
2869 high_bound = bound;
2870 if (tree_to_double_int (vr0.max).slt (high_bound)
2871 && low_bound.slt (tree_to_double_int (vr0.min)))
2873 /* For non-negative numbers, we're shifting out only
2874 zeroes, the value increases monotonically.
2875 For negative numbers, we're shifting out only ones, the
2876 value decreases monotomically. */
2877 in_bounds = true;
2881 if (in_bounds)
2883 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2884 return;
2888 set_value_range_to_varying (vr);
2889 return;
2891 else if (code == TRUNC_DIV_EXPR
2892 || code == FLOOR_DIV_EXPR
2893 || code == CEIL_DIV_EXPR
2894 || code == EXACT_DIV_EXPR
2895 || code == ROUND_DIV_EXPR)
2897 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2899 /* For division, if op1 has VR_RANGE but op0 does not, something
2900 can be deduced just from that range. Say [min, max] / [4, max]
2901 gives [min / 4, max / 4] range. */
2902 if (vr1.type == VR_RANGE
2903 && !symbolic_range_p (&vr1)
2904 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2906 vr0.type = type = VR_RANGE;
2907 vr0.min = vrp_val_min (expr_type);
2908 vr0.max = vrp_val_max (expr_type);
2910 else
2912 set_value_range_to_varying (vr);
2913 return;
2917 /* For divisions, if flag_non_call_exceptions is true, we must
2918 not eliminate a division by zero. */
2919 if (cfun->can_throw_non_call_exceptions
2920 && (vr1.type != VR_RANGE
2921 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2923 set_value_range_to_varying (vr);
2924 return;
2927 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2928 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2929 include 0. */
2930 if (vr0.type == VR_RANGE
2931 && (vr1.type != VR_RANGE
2932 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2934 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2935 int cmp;
2937 min = NULL_TREE;
2938 max = NULL_TREE;
2939 if (TYPE_UNSIGNED (expr_type)
2940 || value_range_nonnegative_p (&vr1))
2942 /* For unsigned division or when divisor is known
2943 to be non-negative, the range has to cover
2944 all numbers from 0 to max for positive max
2945 and all numbers from min to 0 for negative min. */
2946 cmp = compare_values (vr0.max, zero);
2947 if (cmp == -1)
2948 max = zero;
2949 else if (cmp == 0 || cmp == 1)
2950 max = vr0.max;
2951 else
2952 type = VR_VARYING;
2953 cmp = compare_values (vr0.min, zero);
2954 if (cmp == 1)
2955 min = zero;
2956 else if (cmp == 0 || cmp == -1)
2957 min = vr0.min;
2958 else
2959 type = VR_VARYING;
2961 else
2963 /* Otherwise the range is -max .. max or min .. -min
2964 depending on which bound is bigger in absolute value,
2965 as the division can change the sign. */
2966 abs_extent_range (vr, vr0.min, vr0.max);
2967 return;
2969 if (type == VR_VARYING)
2971 set_value_range_to_varying (vr);
2972 return;
2975 else
2977 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2978 return;
2981 else if (code == TRUNC_MOD_EXPR)
2983 if (vr1.type != VR_RANGE
2984 || range_includes_zero_p (vr1.min, vr1.max) != 0
2985 || vrp_val_is_min (vr1.min))
2987 set_value_range_to_varying (vr);
2988 return;
2990 type = VR_RANGE;
2991 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
2992 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
2993 if (tree_int_cst_lt (max, vr1.max))
2994 max = vr1.max;
2995 max = int_const_binop (MINUS_EXPR, max, integer_one_node);
2996 /* If the dividend is non-negative the modulus will be
2997 non-negative as well. */
2998 if (TYPE_UNSIGNED (expr_type)
2999 || value_range_nonnegative_p (&vr0))
3000 min = build_int_cst (TREE_TYPE (max), 0);
3001 else
3002 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3004 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3006 bool int_cst_range0, int_cst_range1;
3007 double_int may_be_nonzero0, may_be_nonzero1;
3008 double_int must_be_nonzero0, must_be_nonzero1;
3010 int_cst_range0 = zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0,
3011 &must_be_nonzero0);
3012 int_cst_range1 = zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1,
3013 &must_be_nonzero1);
3015 type = VR_RANGE;
3016 if (code == BIT_AND_EXPR)
3018 double_int dmax;
3019 min = double_int_to_tree (expr_type,
3020 must_be_nonzero0 & must_be_nonzero1);
3021 dmax = may_be_nonzero0 & may_be_nonzero1;
3022 /* If both input ranges contain only negative values we can
3023 truncate the result range maximum to the minimum of the
3024 input range maxima. */
3025 if (int_cst_range0 && int_cst_range1
3026 && tree_int_cst_sgn (vr0.max) < 0
3027 && tree_int_cst_sgn (vr1.max) < 0)
3029 dmax = dmax.min (tree_to_double_int (vr0.max),
3030 TYPE_UNSIGNED (expr_type));
3031 dmax = dmax.min (tree_to_double_int (vr1.max),
3032 TYPE_UNSIGNED (expr_type));
3034 /* If either input range contains only non-negative values
3035 we can truncate the result range maximum to the respective
3036 maximum of the input range. */
3037 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3038 dmax = dmax.min (tree_to_double_int (vr0.max),
3039 TYPE_UNSIGNED (expr_type));
3040 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3041 dmax = dmax.min (tree_to_double_int (vr1.max),
3042 TYPE_UNSIGNED (expr_type));
3043 max = double_int_to_tree (expr_type, dmax);
3045 else if (code == BIT_IOR_EXPR)
3047 double_int dmin;
3048 max = double_int_to_tree (expr_type,
3049 may_be_nonzero0 | may_be_nonzero1);
3050 dmin = must_be_nonzero0 | must_be_nonzero1;
3051 /* If the input ranges contain only positive values we can
3052 truncate the minimum of the result range to the maximum
3053 of the input range minima. */
3054 if (int_cst_range0 && int_cst_range1
3055 && tree_int_cst_sgn (vr0.min) >= 0
3056 && tree_int_cst_sgn (vr1.min) >= 0)
3058 dmin = dmin.max (tree_to_double_int (vr0.min),
3059 TYPE_UNSIGNED (expr_type));
3060 dmin = dmin.max (tree_to_double_int (vr1.min),
3061 TYPE_UNSIGNED (expr_type));
3063 /* If either input range contains only negative values
3064 we can truncate the minimum of the result range to the
3065 respective minimum range. */
3066 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3067 dmin = dmin.max (tree_to_double_int (vr0.min),
3068 TYPE_UNSIGNED (expr_type));
3069 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3070 dmin = dmin.max (tree_to_double_int (vr1.min),
3071 TYPE_UNSIGNED (expr_type));
3072 min = double_int_to_tree (expr_type, dmin);
3074 else if (code == BIT_XOR_EXPR)
3076 double_int result_zero_bits, result_one_bits;
3077 result_zero_bits = (must_be_nonzero0 & must_be_nonzero1)
3078 | ~(may_be_nonzero0 | may_be_nonzero1);
3079 result_one_bits = must_be_nonzero0.and_not (may_be_nonzero1)
3080 | must_be_nonzero1.and_not (may_be_nonzero0);
3081 max = double_int_to_tree (expr_type, ~result_zero_bits);
3082 min = double_int_to_tree (expr_type, result_one_bits);
3083 /* If the range has all positive or all negative values the
3084 result is better than VARYING. */
3085 if (tree_int_cst_sgn (min) < 0
3086 || tree_int_cst_sgn (max) >= 0)
3088 else
3089 max = min = NULL_TREE;
3092 else
3093 gcc_unreachable ();
3095 /* If either MIN or MAX overflowed, then set the resulting range to
3096 VARYING. But we do accept an overflow infinity
3097 representation. */
3098 if (min == NULL_TREE
3099 || !is_gimple_min_invariant (min)
3100 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
3101 || max == NULL_TREE
3102 || !is_gimple_min_invariant (max)
3103 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
3105 set_value_range_to_varying (vr);
3106 return;
3109 /* We punt if:
3110 1) [-INF, +INF]
3111 2) [-INF, +-INF(OVF)]
3112 3) [+-INF(OVF), +INF]
3113 4) [+-INF(OVF), +-INF(OVF)]
3114 We learn nothing when we have INF and INF(OVF) on both sides.
3115 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3116 overflow. */
3117 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3118 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3120 set_value_range_to_varying (vr);
3121 return;
3124 cmp = compare_values (min, max);
3125 if (cmp == -2 || cmp == 1)
3127 /* If the new range has its limits swapped around (MIN > MAX),
3128 then the operation caused one of them to wrap around, mark
3129 the new range VARYING. */
3130 set_value_range_to_varying (vr);
3132 else
3133 set_value_range (vr, type, min, max, NULL);
3136 /* Extract range information from a binary expression OP0 CODE OP1 based on
3137 the ranges of each of its operands with resulting type EXPR_TYPE.
3138 The resulting range is stored in *VR. */
3140 static void
3141 extract_range_from_binary_expr (value_range_t *vr,
3142 enum tree_code code,
3143 tree expr_type, tree op0, tree op1)
3145 value_range_t vr0 = VR_INITIALIZER;
3146 value_range_t vr1 = VR_INITIALIZER;
3148 /* Get value ranges for each operand. For constant operands, create
3149 a new value range with the operand to simplify processing. */
3150 if (TREE_CODE (op0) == SSA_NAME)
3151 vr0 = *(get_value_range (op0));
3152 else if (is_gimple_min_invariant (op0))
3153 set_value_range_to_value (&vr0, op0, NULL);
3154 else
3155 set_value_range_to_varying (&vr0);
3157 if (TREE_CODE (op1) == SSA_NAME)
3158 vr1 = *(get_value_range (op1));
3159 else if (is_gimple_min_invariant (op1))
3160 set_value_range_to_value (&vr1, op1, NULL);
3161 else
3162 set_value_range_to_varying (&vr1);
3164 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3167 /* Extract range information from a unary operation CODE based on
3168 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3169 The The resulting range is stored in *VR. */
3171 static void
3172 extract_range_from_unary_expr_1 (value_range_t *vr,
3173 enum tree_code code, tree type,
3174 value_range_t *vr0_, tree op0_type)
3176 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3178 /* VRP only operates on integral and pointer types. */
3179 if (!(INTEGRAL_TYPE_P (op0_type)
3180 || POINTER_TYPE_P (op0_type))
3181 || !(INTEGRAL_TYPE_P (type)
3182 || POINTER_TYPE_P (type)))
3184 set_value_range_to_varying (vr);
3185 return;
3188 /* If VR0 is UNDEFINED, so is the result. */
3189 if (vr0.type == VR_UNDEFINED)
3191 set_value_range_to_undefined (vr);
3192 return;
3195 /* Handle operations that we express in terms of others. */
3196 if (code == PAREN_EXPR)
3198 /* PAREN_EXPR is a simple copy. */
3199 copy_value_range (vr, &vr0);
3200 return;
3202 else if (code == NEGATE_EXPR)
3204 /* -X is simply 0 - X, so re-use existing code that also handles
3205 anti-ranges fine. */
3206 value_range_t zero = VR_INITIALIZER;
3207 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3208 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3209 return;
3211 else if (code == BIT_NOT_EXPR)
3213 /* ~X is simply -1 - X, so re-use existing code that also handles
3214 anti-ranges fine. */
3215 value_range_t minusone = VR_INITIALIZER;
3216 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3217 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3218 type, &minusone, &vr0);
3219 return;
3222 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3223 and express op ~[] as (op []') U (op []''). */
3224 if (vr0.type == VR_ANTI_RANGE
3225 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3227 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3228 if (vrtem1.type != VR_UNDEFINED)
3230 value_range_t vrres = VR_INITIALIZER;
3231 extract_range_from_unary_expr_1 (&vrres, code, type,
3232 &vrtem1, op0_type);
3233 vrp_meet (vr, &vrres);
3235 return;
3238 if (CONVERT_EXPR_CODE_P (code))
3240 tree inner_type = op0_type;
3241 tree outer_type = type;
3243 /* If the expression evaluates to a pointer, we are only interested in
3244 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3245 if (POINTER_TYPE_P (type))
3247 if (range_is_nonnull (&vr0))
3248 set_value_range_to_nonnull (vr, type);
3249 else if (range_is_null (&vr0))
3250 set_value_range_to_null (vr, type);
3251 else
3252 set_value_range_to_varying (vr);
3253 return;
3256 /* If VR0 is varying and we increase the type precision, assume
3257 a full range for the following transformation. */
3258 if (vr0.type == VR_VARYING
3259 && INTEGRAL_TYPE_P (inner_type)
3260 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3262 vr0.type = VR_RANGE;
3263 vr0.min = TYPE_MIN_VALUE (inner_type);
3264 vr0.max = TYPE_MAX_VALUE (inner_type);
3267 /* If VR0 is a constant range or anti-range and the conversion is
3268 not truncating we can convert the min and max values and
3269 canonicalize the resulting range. Otherwise we can do the
3270 conversion if the size of the range is less than what the
3271 precision of the target type can represent and the range is
3272 not an anti-range. */
3273 if ((vr0.type == VR_RANGE
3274 || vr0.type == VR_ANTI_RANGE)
3275 && TREE_CODE (vr0.min) == INTEGER_CST
3276 && TREE_CODE (vr0.max) == INTEGER_CST
3277 && (!is_overflow_infinity (vr0.min)
3278 || (vr0.type == VR_RANGE
3279 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3280 && needs_overflow_infinity (outer_type)
3281 && supports_overflow_infinity (outer_type)))
3282 && (!is_overflow_infinity (vr0.max)
3283 || (vr0.type == VR_RANGE
3284 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3285 && needs_overflow_infinity (outer_type)
3286 && supports_overflow_infinity (outer_type)))
3287 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3288 || (vr0.type == VR_RANGE
3289 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3290 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3291 size_int (TYPE_PRECISION (outer_type)))))))
3293 tree new_min, new_max;
3294 if (is_overflow_infinity (vr0.min))
3295 new_min = negative_overflow_infinity (outer_type);
3296 else
3297 new_min = force_fit_type_double (outer_type,
3298 tree_to_double_int (vr0.min),
3299 0, false);
3300 if (is_overflow_infinity (vr0.max))
3301 new_max = positive_overflow_infinity (outer_type);
3302 else
3303 new_max = force_fit_type_double (outer_type,
3304 tree_to_double_int (vr0.max),
3305 0, false);
3306 set_and_canonicalize_value_range (vr, vr0.type,
3307 new_min, new_max, NULL);
3308 return;
3311 set_value_range_to_varying (vr);
3312 return;
3314 else if (code == ABS_EXPR)
3316 tree min, max;
3317 int cmp;
3319 /* Pass through vr0 in the easy cases. */
3320 if (TYPE_UNSIGNED (type)
3321 || value_range_nonnegative_p (&vr0))
3323 copy_value_range (vr, &vr0);
3324 return;
3327 /* For the remaining varying or symbolic ranges we can't do anything
3328 useful. */
3329 if (vr0.type == VR_VARYING
3330 || symbolic_range_p (&vr0))
3332 set_value_range_to_varying (vr);
3333 return;
3336 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3337 useful range. */
3338 if (!TYPE_OVERFLOW_UNDEFINED (type)
3339 && ((vr0.type == VR_RANGE
3340 && vrp_val_is_min (vr0.min))
3341 || (vr0.type == VR_ANTI_RANGE
3342 && !vrp_val_is_min (vr0.min))))
3344 set_value_range_to_varying (vr);
3345 return;
3348 /* ABS_EXPR may flip the range around, if the original range
3349 included negative values. */
3350 if (is_overflow_infinity (vr0.min))
3351 min = positive_overflow_infinity (type);
3352 else if (!vrp_val_is_min (vr0.min))
3353 min = fold_unary_to_constant (code, type, vr0.min);
3354 else if (!needs_overflow_infinity (type))
3355 min = TYPE_MAX_VALUE (type);
3356 else if (supports_overflow_infinity (type))
3357 min = positive_overflow_infinity (type);
3358 else
3360 set_value_range_to_varying (vr);
3361 return;
3364 if (is_overflow_infinity (vr0.max))
3365 max = positive_overflow_infinity (type);
3366 else if (!vrp_val_is_min (vr0.max))
3367 max = fold_unary_to_constant (code, type, vr0.max);
3368 else if (!needs_overflow_infinity (type))
3369 max = TYPE_MAX_VALUE (type);
3370 else if (supports_overflow_infinity (type)
3371 /* We shouldn't generate [+INF, +INF] as set_value_range
3372 doesn't like this and ICEs. */
3373 && !is_positive_overflow_infinity (min))
3374 max = positive_overflow_infinity (type);
3375 else
3377 set_value_range_to_varying (vr);
3378 return;
3381 cmp = compare_values (min, max);
3383 /* If a VR_ANTI_RANGEs contains zero, then we have
3384 ~[-INF, min(MIN, MAX)]. */
3385 if (vr0.type == VR_ANTI_RANGE)
3387 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3389 /* Take the lower of the two values. */
3390 if (cmp != 1)
3391 max = min;
3393 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3394 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3395 flag_wrapv is set and the original anti-range doesn't include
3396 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3397 if (TYPE_OVERFLOW_WRAPS (type))
3399 tree type_min_value = TYPE_MIN_VALUE (type);
3401 min = (vr0.min != type_min_value
3402 ? int_const_binop (PLUS_EXPR, type_min_value,
3403 integer_one_node)
3404 : type_min_value);
3406 else
3408 if (overflow_infinity_range_p (&vr0))
3409 min = negative_overflow_infinity (type);
3410 else
3411 min = TYPE_MIN_VALUE (type);
3414 else
3416 /* All else has failed, so create the range [0, INF], even for
3417 flag_wrapv since TYPE_MIN_VALUE is in the original
3418 anti-range. */
3419 vr0.type = VR_RANGE;
3420 min = build_int_cst (type, 0);
3421 if (needs_overflow_infinity (type))
3423 if (supports_overflow_infinity (type))
3424 max = positive_overflow_infinity (type);
3425 else
3427 set_value_range_to_varying (vr);
3428 return;
3431 else
3432 max = TYPE_MAX_VALUE (type);
3436 /* If the range contains zero then we know that the minimum value in the
3437 range will be zero. */
3438 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3440 if (cmp == 1)
3441 max = min;
3442 min = build_int_cst (type, 0);
3444 else
3446 /* If the range was reversed, swap MIN and MAX. */
3447 if (cmp == 1)
3449 tree t = min;
3450 min = max;
3451 max = t;
3455 cmp = compare_values (min, max);
3456 if (cmp == -2 || cmp == 1)
3458 /* If the new range has its limits swapped around (MIN > MAX),
3459 then the operation caused one of them to wrap around, mark
3460 the new range VARYING. */
3461 set_value_range_to_varying (vr);
3463 else
3464 set_value_range (vr, vr0.type, min, max, NULL);
3465 return;
3468 /* For unhandled operations fall back to varying. */
3469 set_value_range_to_varying (vr);
3470 return;
3474 /* Extract range information from a unary expression CODE OP0 based on
3475 the range of its operand with resulting type TYPE.
3476 The resulting range is stored in *VR. */
3478 static void
3479 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3480 tree type, tree op0)
3482 value_range_t vr0 = VR_INITIALIZER;
3484 /* Get value ranges for the operand. For constant operands, create
3485 a new value range with the operand to simplify processing. */
3486 if (TREE_CODE (op0) == SSA_NAME)
3487 vr0 = *(get_value_range (op0));
3488 else if (is_gimple_min_invariant (op0))
3489 set_value_range_to_value (&vr0, op0, NULL);
3490 else
3491 set_value_range_to_varying (&vr0);
3493 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3497 /* Extract range information from a conditional expression STMT based on
3498 the ranges of each of its operands and the expression code. */
3500 static void
3501 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3503 tree op0, op1;
3504 value_range_t vr0 = VR_INITIALIZER;
3505 value_range_t vr1 = VR_INITIALIZER;
3507 /* Get value ranges for each operand. For constant operands, create
3508 a new value range with the operand to simplify processing. */
3509 op0 = gimple_assign_rhs2 (stmt);
3510 if (TREE_CODE (op0) == SSA_NAME)
3511 vr0 = *(get_value_range (op0));
3512 else if (is_gimple_min_invariant (op0))
3513 set_value_range_to_value (&vr0, op0, NULL);
3514 else
3515 set_value_range_to_varying (&vr0);
3517 op1 = gimple_assign_rhs3 (stmt);
3518 if (TREE_CODE (op1) == SSA_NAME)
3519 vr1 = *(get_value_range (op1));
3520 else if (is_gimple_min_invariant (op1))
3521 set_value_range_to_value (&vr1, op1, NULL);
3522 else
3523 set_value_range_to_varying (&vr1);
3525 /* The resulting value range is the union of the operand ranges */
3526 copy_value_range (vr, &vr0);
3527 vrp_meet (vr, &vr1);
3531 /* Extract range information from a comparison expression EXPR based
3532 on the range of its operand and the expression code. */
3534 static void
3535 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3536 tree type, tree op0, tree op1)
3538 bool sop = false;
3539 tree val;
3541 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3542 NULL);
3544 /* A disadvantage of using a special infinity as an overflow
3545 representation is that we lose the ability to record overflow
3546 when we don't have an infinity. So we have to ignore a result
3547 which relies on overflow. */
3549 if (val && !is_overflow_infinity (val) && !sop)
3551 /* Since this expression was found on the RHS of an assignment,
3552 its type may be different from _Bool. Convert VAL to EXPR's
3553 type. */
3554 val = fold_convert (type, val);
3555 if (is_gimple_min_invariant (val))
3556 set_value_range_to_value (vr, val, vr->equiv);
3557 else
3558 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3560 else
3561 /* The result of a comparison is always true or false. */
3562 set_value_range_to_truthvalue (vr, type);
3565 /* Try to derive a nonnegative or nonzero range out of STMT relying
3566 primarily on generic routines in fold in conjunction with range data.
3567 Store the result in *VR */
3569 static void
3570 extract_range_basic (value_range_t *vr, gimple stmt)
3572 bool sop = false;
3573 tree type = gimple_expr_type (stmt);
3575 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3577 tree fndecl = gimple_call_fndecl (stmt), arg;
3578 int mini, maxi, zerov = 0, prec;
3580 switch (DECL_FUNCTION_CODE (fndecl))
3582 case BUILT_IN_CONSTANT_P:
3583 /* If the call is __builtin_constant_p and the argument is a
3584 function parameter resolve it to false. This avoids bogus
3585 array bound warnings.
3586 ??? We could do this as early as inlining is finished. */
3587 arg = gimple_call_arg (stmt, 0);
3588 if (TREE_CODE (arg) == SSA_NAME
3589 && SSA_NAME_IS_DEFAULT_DEF (arg)
3590 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3592 set_value_range_to_null (vr, type);
3593 return;
3595 break;
3596 /* Both __builtin_ffs* and __builtin_popcount return
3597 [0, prec]. */
3598 CASE_INT_FN (BUILT_IN_FFS):
3599 CASE_INT_FN (BUILT_IN_POPCOUNT):
3600 arg = gimple_call_arg (stmt, 0);
3601 prec = TYPE_PRECISION (TREE_TYPE (arg));
3602 mini = 0;
3603 maxi = prec;
3604 if (TREE_CODE (arg) == SSA_NAME)
3606 value_range_t *vr0 = get_value_range (arg);
3607 /* If arg is non-zero, then ffs or popcount
3608 are non-zero. */
3609 if (((vr0->type == VR_RANGE
3610 && integer_nonzerop (vr0->min))
3611 || (vr0->type == VR_ANTI_RANGE
3612 && integer_zerop (vr0->min)))
3613 && !is_overflow_infinity (vr0->min))
3614 mini = 1;
3615 /* If some high bits are known to be zero,
3616 we can decrease the maximum. */
3617 if (vr0->type == VR_RANGE
3618 && TREE_CODE (vr0->max) == INTEGER_CST
3619 && !is_overflow_infinity (vr0->max))
3620 maxi = tree_floor_log2 (vr0->max) + 1;
3622 goto bitop_builtin;
3623 /* __builtin_parity* returns [0, 1]. */
3624 CASE_INT_FN (BUILT_IN_PARITY):
3625 mini = 0;
3626 maxi = 1;
3627 goto bitop_builtin;
3628 /* __builtin_c[lt]z* return [0, prec-1], except for
3629 when the argument is 0, but that is undefined behavior.
3630 On many targets where the CLZ RTL or optab value is defined
3631 for 0 the value is prec, so include that in the range
3632 by default. */
3633 CASE_INT_FN (BUILT_IN_CLZ):
3634 arg = gimple_call_arg (stmt, 0);
3635 prec = TYPE_PRECISION (TREE_TYPE (arg));
3636 mini = 0;
3637 maxi = prec;
3638 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3639 != CODE_FOR_nothing
3640 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3641 zerov)
3642 /* Handle only the single common value. */
3643 && zerov != prec)
3644 /* Magic value to give up, unless vr0 proves
3645 arg is non-zero. */
3646 mini = -2;
3647 if (TREE_CODE (arg) == SSA_NAME)
3649 value_range_t *vr0 = get_value_range (arg);
3650 /* From clz of VR_RANGE minimum we can compute
3651 result maximum. */
3652 if (vr0->type == VR_RANGE
3653 && TREE_CODE (vr0->min) == INTEGER_CST
3654 && !is_overflow_infinity (vr0->min))
3656 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3657 if (maxi != prec)
3658 mini = 0;
3660 else if (vr0->type == VR_ANTI_RANGE
3661 && integer_zerop (vr0->min)
3662 && !is_overflow_infinity (vr0->min))
3664 maxi = prec - 1;
3665 mini = 0;
3667 if (mini == -2)
3668 break;
3669 /* From clz of VR_RANGE maximum we can compute
3670 result minimum. */
3671 if (vr0->type == VR_RANGE
3672 && TREE_CODE (vr0->max) == INTEGER_CST
3673 && !is_overflow_infinity (vr0->max))
3675 mini = prec - 1 - tree_floor_log2 (vr0->max);
3676 if (mini == prec)
3677 break;
3680 if (mini == -2)
3681 break;
3682 goto bitop_builtin;
3683 /* __builtin_ctz* return [0, prec-1], except for
3684 when the argument is 0, but that is undefined behavior.
3685 If there is a ctz optab for this mode and
3686 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3687 otherwise just assume 0 won't be seen. */
3688 CASE_INT_FN (BUILT_IN_CTZ):
3689 arg = gimple_call_arg (stmt, 0);
3690 prec = TYPE_PRECISION (TREE_TYPE (arg));
3691 mini = 0;
3692 maxi = prec - 1;
3693 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3694 != CODE_FOR_nothing
3695 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3696 zerov))
3698 /* Handle only the two common values. */
3699 if (zerov == -1)
3700 mini = -1;
3701 else if (zerov == prec)
3702 maxi = prec;
3703 else
3704 /* Magic value to give up, unless vr0 proves
3705 arg is non-zero. */
3706 mini = -2;
3708 if (TREE_CODE (arg) == SSA_NAME)
3710 value_range_t *vr0 = get_value_range (arg);
3711 /* If arg is non-zero, then use [0, prec - 1]. */
3712 if (((vr0->type == VR_RANGE
3713 && integer_nonzerop (vr0->min))
3714 || (vr0->type == VR_ANTI_RANGE
3715 && integer_zerop (vr0->min)))
3716 && !is_overflow_infinity (vr0->min))
3718 mini = 0;
3719 maxi = prec - 1;
3721 /* If some high bits are known to be zero,
3722 we can decrease the result maximum. */
3723 if (vr0->type == VR_RANGE
3724 && TREE_CODE (vr0->max) == INTEGER_CST
3725 && !is_overflow_infinity (vr0->max))
3727 maxi = tree_floor_log2 (vr0->max);
3728 /* For vr0 [0, 0] give up. */
3729 if (maxi == -1)
3730 break;
3733 if (mini == -2)
3734 break;
3735 goto bitop_builtin;
3736 /* __builtin_clrsb* returns [0, prec-1]. */
3737 CASE_INT_FN (BUILT_IN_CLRSB):
3738 arg = gimple_call_arg (stmt, 0);
3739 prec = TYPE_PRECISION (TREE_TYPE (arg));
3740 mini = 0;
3741 maxi = prec - 1;
3742 goto bitop_builtin;
3743 bitop_builtin:
3744 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3745 build_int_cst (type, maxi), NULL);
3746 return;
3747 default:
3748 break;
3751 if (INTEGRAL_TYPE_P (type)
3752 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3753 set_value_range_to_nonnegative (vr, type,
3754 sop || stmt_overflow_infinity (stmt));
3755 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3756 && !sop)
3757 set_value_range_to_nonnull (vr, type);
3758 else
3759 set_value_range_to_varying (vr);
3763 /* Try to compute a useful range out of assignment STMT and store it
3764 in *VR. */
3766 static void
3767 extract_range_from_assignment (value_range_t *vr, gimple stmt)
3769 enum tree_code code = gimple_assign_rhs_code (stmt);
3771 if (code == ASSERT_EXPR)
3772 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3773 else if (code == SSA_NAME)
3774 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3775 else if (TREE_CODE_CLASS (code) == tcc_binary)
3776 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3777 gimple_expr_type (stmt),
3778 gimple_assign_rhs1 (stmt),
3779 gimple_assign_rhs2 (stmt));
3780 else if (TREE_CODE_CLASS (code) == tcc_unary)
3781 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3782 gimple_expr_type (stmt),
3783 gimple_assign_rhs1 (stmt));
3784 else if (code == COND_EXPR)
3785 extract_range_from_cond_expr (vr, stmt);
3786 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3787 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3788 gimple_expr_type (stmt),
3789 gimple_assign_rhs1 (stmt),
3790 gimple_assign_rhs2 (stmt));
3791 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3792 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3793 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3794 else
3795 set_value_range_to_varying (vr);
3797 if (vr->type == VR_VARYING)
3798 extract_range_basic (vr, stmt);
3801 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3802 would be profitable to adjust VR using scalar evolution information
3803 for VAR. If so, update VR with the new limits. */
3805 static void
3806 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
3807 gimple stmt, tree var)
3809 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3810 enum ev_direction dir;
3812 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3813 better opportunities than a regular range, but I'm not sure. */
3814 if (vr->type == VR_ANTI_RANGE)
3815 return;
3817 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3819 /* Like in PR19590, scev can return a constant function. */
3820 if (is_gimple_min_invariant (chrec))
3822 set_value_range_to_value (vr, chrec, vr->equiv);
3823 return;
3826 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3827 return;
3829 init = initial_condition_in_loop_num (chrec, loop->num);
3830 tem = op_with_constant_singleton_value_range (init);
3831 if (tem)
3832 init = tem;
3833 step = evolution_part_in_loop_num (chrec, loop->num);
3834 tem = op_with_constant_singleton_value_range (step);
3835 if (tem)
3836 step = tem;
3838 /* If STEP is symbolic, we can't know whether INIT will be the
3839 minimum or maximum value in the range. Also, unless INIT is
3840 a simple expression, compare_values and possibly other functions
3841 in tree-vrp won't be able to handle it. */
3842 if (step == NULL_TREE
3843 || !is_gimple_min_invariant (step)
3844 || !valid_value_p (init))
3845 return;
3847 dir = scev_direction (chrec);
3848 if (/* Do not adjust ranges if we do not know whether the iv increases
3849 or decreases, ... */
3850 dir == EV_DIR_UNKNOWN
3851 /* ... or if it may wrap. */
3852 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
3853 true))
3854 return;
3856 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3857 negative_overflow_infinity and positive_overflow_infinity,
3858 because we have concluded that the loop probably does not
3859 wrap. */
3861 type = TREE_TYPE (var);
3862 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
3863 tmin = lower_bound_in_type (type, type);
3864 else
3865 tmin = TYPE_MIN_VALUE (type);
3866 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
3867 tmax = upper_bound_in_type (type, type);
3868 else
3869 tmax = TYPE_MAX_VALUE (type);
3871 /* Try to use estimated number of iterations for the loop to constrain the
3872 final value in the evolution. */
3873 if (TREE_CODE (step) == INTEGER_CST
3874 && is_gimple_val (init)
3875 && (TREE_CODE (init) != SSA_NAME
3876 || get_value_range (init)->type == VR_RANGE))
3878 double_int nit;
3880 /* We are only entering here for loop header PHI nodes, so using
3881 the number of latch executions is the correct thing to use. */
3882 if (max_loop_iterations (loop, &nit))
3884 value_range_t maxvr = VR_INITIALIZER;
3885 double_int dtmp;
3886 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (step));
3887 bool overflow = false;
3889 dtmp = tree_to_double_int (step)
3890 .mul_with_sign (nit, unsigned_p, &overflow);
3891 /* If the multiplication overflowed we can't do a meaningful
3892 adjustment. Likewise if the result doesn't fit in the type
3893 of the induction variable. For a signed type we have to
3894 check whether the result has the expected signedness which
3895 is that of the step as number of iterations is unsigned. */
3896 if (!overflow
3897 && double_int_fits_to_tree_p (TREE_TYPE (init), dtmp)
3898 && (unsigned_p
3899 || ((dtmp.high ^ TREE_INT_CST_HIGH (step)) >= 0)))
3901 tem = double_int_to_tree (TREE_TYPE (init), dtmp);
3902 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
3903 TREE_TYPE (init), init, tem);
3904 /* Likewise if the addition did. */
3905 if (maxvr.type == VR_RANGE)
3907 tmin = maxvr.min;
3908 tmax = maxvr.max;
3914 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
3916 min = tmin;
3917 max = tmax;
3919 /* For VARYING or UNDEFINED ranges, just about anything we get
3920 from scalar evolutions should be better. */
3922 if (dir == EV_DIR_DECREASES)
3923 max = init;
3924 else
3925 min = init;
3927 /* If we would create an invalid range, then just assume we
3928 know absolutely nothing. This may be over-conservative,
3929 but it's clearly safe, and should happen only in unreachable
3930 parts of code, or for invalid programs. */
3931 if (compare_values (min, max) == 1)
3932 return;
3934 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3936 else if (vr->type == VR_RANGE)
3938 min = vr->min;
3939 max = vr->max;
3941 if (dir == EV_DIR_DECREASES)
3943 /* INIT is the maximum value. If INIT is lower than VR->MAX
3944 but no smaller than VR->MIN, set VR->MAX to INIT. */
3945 if (compare_values (init, max) == -1)
3946 max = init;
3948 /* According to the loop information, the variable does not
3949 overflow. If we think it does, probably because of an
3950 overflow due to arithmetic on a different INF value,
3951 reset now. */
3952 if (is_negative_overflow_infinity (min)
3953 || compare_values (min, tmin) == -1)
3954 min = tmin;
3957 else
3959 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3960 if (compare_values (init, min) == 1)
3961 min = init;
3963 if (is_positive_overflow_infinity (max)
3964 || compare_values (tmax, max) == -1)
3965 max = tmax;
3968 /* If we just created an invalid range with the minimum
3969 greater than the maximum, we fail conservatively.
3970 This should happen only in unreachable
3971 parts of code, or for invalid programs. */
3972 if (compare_values (min, max) == 1)
3973 return;
3975 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
3979 /* Return true if VAR may overflow at STMT. This checks any available
3980 loop information to see if we can determine that VAR does not
3981 overflow. */
3983 static bool
3984 vrp_var_may_overflow (tree var, gimple stmt)
3986 struct loop *l;
3987 tree chrec, init, step;
3989 if (current_loops == NULL)
3990 return true;
3992 l = loop_containing_stmt (stmt);
3993 if (l == NULL
3994 || !loop_outer (l))
3995 return true;
3997 chrec = instantiate_parameters (l, analyze_scalar_evolution (l, var));
3998 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3999 return true;
4001 init = initial_condition_in_loop_num (chrec, l->num);
4002 step = evolution_part_in_loop_num (chrec, l->num);
4004 if (step == NULL_TREE
4005 || !is_gimple_min_invariant (step)
4006 || !valid_value_p (init))
4007 return true;
4009 /* If we get here, we know something useful about VAR based on the
4010 loop information. If it wraps, it may overflow. */
4012 if (scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4013 true))
4014 return true;
4016 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
4018 print_generic_expr (dump_file, var, 0);
4019 fprintf (dump_file, ": loop information indicates does not overflow\n");
4022 return false;
4026 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4028 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4029 all the values in the ranges.
4031 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4033 - Return NULL_TREE if it is not always possible to determine the
4034 value of the comparison.
4036 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4037 overflow infinity was used in the test. */
4040 static tree
4041 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4042 bool *strict_overflow_p)
4044 /* VARYING or UNDEFINED ranges cannot be compared. */
4045 if (vr0->type == VR_VARYING
4046 || vr0->type == VR_UNDEFINED
4047 || vr1->type == VR_VARYING
4048 || vr1->type == VR_UNDEFINED)
4049 return NULL_TREE;
4051 /* Anti-ranges need to be handled separately. */
4052 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4054 /* If both are anti-ranges, then we cannot compute any
4055 comparison. */
4056 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4057 return NULL_TREE;
4059 /* These comparisons are never statically computable. */
4060 if (comp == GT_EXPR
4061 || comp == GE_EXPR
4062 || comp == LT_EXPR
4063 || comp == LE_EXPR)
4064 return NULL_TREE;
4066 /* Equality can be computed only between a range and an
4067 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4068 if (vr0->type == VR_RANGE)
4070 /* To simplify processing, make VR0 the anti-range. */
4071 value_range_t *tmp = vr0;
4072 vr0 = vr1;
4073 vr1 = tmp;
4076 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4078 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4079 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4080 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4082 return NULL_TREE;
4085 if (!usable_range_p (vr0, strict_overflow_p)
4086 || !usable_range_p (vr1, strict_overflow_p))
4087 return NULL_TREE;
4089 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4090 operands around and change the comparison code. */
4091 if (comp == GT_EXPR || comp == GE_EXPR)
4093 value_range_t *tmp;
4094 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4095 tmp = vr0;
4096 vr0 = vr1;
4097 vr1 = tmp;
4100 if (comp == EQ_EXPR)
4102 /* Equality may only be computed if both ranges represent
4103 exactly one value. */
4104 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4105 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4107 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4108 strict_overflow_p);
4109 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4110 strict_overflow_p);
4111 if (cmp_min == 0 && cmp_max == 0)
4112 return boolean_true_node;
4113 else if (cmp_min != -2 && cmp_max != -2)
4114 return boolean_false_node;
4116 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4117 else if (compare_values_warnv (vr0->min, vr1->max,
4118 strict_overflow_p) == 1
4119 || compare_values_warnv (vr1->min, vr0->max,
4120 strict_overflow_p) == 1)
4121 return boolean_false_node;
4123 return NULL_TREE;
4125 else if (comp == NE_EXPR)
4127 int cmp1, cmp2;
4129 /* If VR0 is completely to the left or completely to the right
4130 of VR1, they are always different. Notice that we need to
4131 make sure that both comparisons yield similar results to
4132 avoid comparing values that cannot be compared at
4133 compile-time. */
4134 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4135 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4136 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4137 return boolean_true_node;
4139 /* If VR0 and VR1 represent a single value and are identical,
4140 return false. */
4141 else if (compare_values_warnv (vr0->min, vr0->max,
4142 strict_overflow_p) == 0
4143 && compare_values_warnv (vr1->min, vr1->max,
4144 strict_overflow_p) == 0
4145 && compare_values_warnv (vr0->min, vr1->min,
4146 strict_overflow_p) == 0
4147 && compare_values_warnv (vr0->max, vr1->max,
4148 strict_overflow_p) == 0)
4149 return boolean_false_node;
4151 /* Otherwise, they may or may not be different. */
4152 else
4153 return NULL_TREE;
4155 else if (comp == LT_EXPR || comp == LE_EXPR)
4157 int tst;
4159 /* If VR0 is to the left of VR1, return true. */
4160 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4161 if ((comp == LT_EXPR && tst == -1)
4162 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4164 if (overflow_infinity_range_p (vr0)
4165 || overflow_infinity_range_p (vr1))
4166 *strict_overflow_p = true;
4167 return boolean_true_node;
4170 /* If VR0 is to the right of VR1, return false. */
4171 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4172 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4173 || (comp == LE_EXPR && tst == 1))
4175 if (overflow_infinity_range_p (vr0)
4176 || overflow_infinity_range_p (vr1))
4177 *strict_overflow_p = true;
4178 return boolean_false_node;
4181 /* Otherwise, we don't know. */
4182 return NULL_TREE;
4185 gcc_unreachable ();
4189 /* Given a value range VR, a value VAL and a comparison code COMP, return
4190 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4191 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4192 always returns false. Return NULL_TREE if it is not always
4193 possible to determine the value of the comparison. Also set
4194 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4195 infinity was used in the test. */
4197 static tree
4198 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4199 bool *strict_overflow_p)
4201 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4202 return NULL_TREE;
4204 /* Anti-ranges need to be handled separately. */
4205 if (vr->type == VR_ANTI_RANGE)
4207 /* For anti-ranges, the only predicates that we can compute at
4208 compile time are equality and inequality. */
4209 if (comp == GT_EXPR
4210 || comp == GE_EXPR
4211 || comp == LT_EXPR
4212 || comp == LE_EXPR)
4213 return NULL_TREE;
4215 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4216 if (value_inside_range (val, vr->min, vr->max) == 1)
4217 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4219 return NULL_TREE;
4222 if (!usable_range_p (vr, strict_overflow_p))
4223 return NULL_TREE;
4225 if (comp == EQ_EXPR)
4227 /* EQ_EXPR may only be computed if VR represents exactly
4228 one value. */
4229 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4231 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4232 if (cmp == 0)
4233 return boolean_true_node;
4234 else if (cmp == -1 || cmp == 1 || cmp == 2)
4235 return boolean_false_node;
4237 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4238 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4239 return boolean_false_node;
4241 return NULL_TREE;
4243 else if (comp == NE_EXPR)
4245 /* If VAL is not inside VR, then they are always different. */
4246 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4247 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4248 return boolean_true_node;
4250 /* If VR represents exactly one value equal to VAL, then return
4251 false. */
4252 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4253 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4254 return boolean_false_node;
4256 /* Otherwise, they may or may not be different. */
4257 return NULL_TREE;
4259 else if (comp == LT_EXPR || comp == LE_EXPR)
4261 int tst;
4263 /* If VR is to the left of VAL, return true. */
4264 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4265 if ((comp == LT_EXPR && tst == -1)
4266 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4268 if (overflow_infinity_range_p (vr))
4269 *strict_overflow_p = true;
4270 return boolean_true_node;
4273 /* If VR is to the right of VAL, return false. */
4274 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4275 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4276 || (comp == LE_EXPR && tst == 1))
4278 if (overflow_infinity_range_p (vr))
4279 *strict_overflow_p = true;
4280 return boolean_false_node;
4283 /* Otherwise, we don't know. */
4284 return NULL_TREE;
4286 else if (comp == GT_EXPR || comp == GE_EXPR)
4288 int tst;
4290 /* If VR is to the right of VAL, return true. */
4291 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4292 if ((comp == GT_EXPR && tst == 1)
4293 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4295 if (overflow_infinity_range_p (vr))
4296 *strict_overflow_p = true;
4297 return boolean_true_node;
4300 /* If VR is to the left of VAL, return false. */
4301 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4302 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4303 || (comp == GE_EXPR && tst == -1))
4305 if (overflow_infinity_range_p (vr))
4306 *strict_overflow_p = true;
4307 return boolean_false_node;
4310 /* Otherwise, we don't know. */
4311 return NULL_TREE;
4314 gcc_unreachable ();
4318 /* Debugging dumps. */
4320 void dump_value_range (FILE *, value_range_t *);
4321 void debug_value_range (value_range_t *);
4322 void dump_all_value_ranges (FILE *);
4323 void debug_all_value_ranges (void);
4324 void dump_vr_equiv (FILE *, bitmap);
4325 void debug_vr_equiv (bitmap);
4328 /* Dump value range VR to FILE. */
4330 void
4331 dump_value_range (FILE *file, value_range_t *vr)
4333 if (vr == NULL)
4334 fprintf (file, "[]");
4335 else if (vr->type == VR_UNDEFINED)
4336 fprintf (file, "UNDEFINED");
4337 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4339 tree type = TREE_TYPE (vr->min);
4341 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4343 if (is_negative_overflow_infinity (vr->min))
4344 fprintf (file, "-INF(OVF)");
4345 else if (INTEGRAL_TYPE_P (type)
4346 && !TYPE_UNSIGNED (type)
4347 && vrp_val_is_min (vr->min))
4348 fprintf (file, "-INF");
4349 else
4350 print_generic_expr (file, vr->min, 0);
4352 fprintf (file, ", ");
4354 if (is_positive_overflow_infinity (vr->max))
4355 fprintf (file, "+INF(OVF)");
4356 else if (INTEGRAL_TYPE_P (type)
4357 && vrp_val_is_max (vr->max))
4358 fprintf (file, "+INF");
4359 else
4360 print_generic_expr (file, vr->max, 0);
4362 fprintf (file, "]");
4364 if (vr->equiv)
4366 bitmap_iterator bi;
4367 unsigned i, c = 0;
4369 fprintf (file, " EQUIVALENCES: { ");
4371 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4373 print_generic_expr (file, ssa_name (i), 0);
4374 fprintf (file, " ");
4375 c++;
4378 fprintf (file, "} (%u elements)", c);
4381 else if (vr->type == VR_VARYING)
4382 fprintf (file, "VARYING");
4383 else
4384 fprintf (file, "INVALID RANGE");
4388 /* Dump value range VR to stderr. */
4390 DEBUG_FUNCTION void
4391 debug_value_range (value_range_t *vr)
4393 dump_value_range (stderr, vr);
4394 fprintf (stderr, "\n");
4398 /* Dump value ranges of all SSA_NAMEs to FILE. */
4400 void
4401 dump_all_value_ranges (FILE *file)
4403 size_t i;
4405 for (i = 0; i < num_vr_values; i++)
4407 if (vr_value[i])
4409 print_generic_expr (file, ssa_name (i), 0);
4410 fprintf (file, ": ");
4411 dump_value_range (file, vr_value[i]);
4412 fprintf (file, "\n");
4416 fprintf (file, "\n");
4420 /* Dump all value ranges to stderr. */
4422 DEBUG_FUNCTION void
4423 debug_all_value_ranges (void)
4425 dump_all_value_ranges (stderr);
4429 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4430 create a new SSA name N and return the assertion assignment
4431 'V = ASSERT_EXPR <V, V OP W>'. */
4433 static gimple
4434 build_assert_expr_for (tree cond, tree v)
4436 tree a;
4437 gimple assertion;
4439 gcc_assert (TREE_CODE (v) == SSA_NAME
4440 && COMPARISON_CLASS_P (cond));
4442 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4443 assertion = gimple_build_assign (NULL_TREE, a);
4445 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4446 operand of the ASSERT_EXPR. Create it so the new name and the old one
4447 are registered in the replacement table so that we can fix the SSA web
4448 after adding all the ASSERT_EXPRs. */
4449 create_new_def_for (v, assertion, NULL);
4451 return assertion;
4455 /* Return false if EXPR is a predicate expression involving floating
4456 point values. */
4458 static inline bool
4459 fp_predicate (gimple stmt)
4461 GIMPLE_CHECK (stmt, GIMPLE_COND);
4463 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4466 /* If the range of values taken by OP can be inferred after STMT executes,
4467 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4468 describes the inferred range. Return true if a range could be
4469 inferred. */
4471 static bool
4472 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4474 *val_p = NULL_TREE;
4475 *comp_code_p = ERROR_MARK;
4477 /* Do not attempt to infer anything in names that flow through
4478 abnormal edges. */
4479 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4480 return false;
4482 /* Similarly, don't infer anything from statements that may throw
4483 exceptions. ??? Relax this requirement? */
4484 if (stmt_could_throw_p (stmt))
4485 return false;
4487 /* If STMT is the last statement of a basic block with no
4488 successors, there is no point inferring anything about any of its
4489 operands. We would not be able to find a proper insertion point
4490 for the assertion, anyway. */
4491 if (stmt_ends_bb_p (stmt) && EDGE_COUNT (gimple_bb (stmt)->succs) == 0)
4492 return false;
4494 if (infer_nonnull_range (stmt, op))
4496 *val_p = build_int_cst (TREE_TYPE (op), 0);
4497 *comp_code_p = NE_EXPR;
4498 return true;
4501 return false;
4505 void dump_asserts_for (FILE *, tree);
4506 void debug_asserts_for (tree);
4507 void dump_all_asserts (FILE *);
4508 void debug_all_asserts (void);
4510 /* Dump all the registered assertions for NAME to FILE. */
4512 void
4513 dump_asserts_for (FILE *file, tree name)
4515 assert_locus_t loc;
4517 fprintf (file, "Assertions to be inserted for ");
4518 print_generic_expr (file, name, 0);
4519 fprintf (file, "\n");
4521 loc = asserts_for[SSA_NAME_VERSION (name)];
4522 while (loc)
4524 fprintf (file, "\t");
4525 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4526 fprintf (file, "\n\tBB #%d", loc->bb->index);
4527 if (loc->e)
4529 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4530 loc->e->dest->index);
4531 dump_edge_info (file, loc->e, dump_flags, 0);
4533 fprintf (file, "\n\tPREDICATE: ");
4534 print_generic_expr (file, name, 0);
4535 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4536 print_generic_expr (file, loc->val, 0);
4537 fprintf (file, "\n\n");
4538 loc = loc->next;
4541 fprintf (file, "\n");
4545 /* Dump all the registered assertions for NAME to stderr. */
4547 DEBUG_FUNCTION void
4548 debug_asserts_for (tree name)
4550 dump_asserts_for (stderr, name);
4554 /* Dump all the registered assertions for all the names to FILE. */
4556 void
4557 dump_all_asserts (FILE *file)
4559 unsigned i;
4560 bitmap_iterator bi;
4562 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4563 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4564 dump_asserts_for (file, ssa_name (i));
4565 fprintf (file, "\n");
4569 /* Dump all the registered assertions for all the names to stderr. */
4571 DEBUG_FUNCTION void
4572 debug_all_asserts (void)
4574 dump_all_asserts (stderr);
4578 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4579 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4580 E->DEST, then register this location as a possible insertion point
4581 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4583 BB, E and SI provide the exact insertion point for the new
4584 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4585 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4586 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4587 must not be NULL. */
4589 static void
4590 register_new_assert_for (tree name, tree expr,
4591 enum tree_code comp_code,
4592 tree val,
4593 basic_block bb,
4594 edge e,
4595 gimple_stmt_iterator si)
4597 assert_locus_t n, loc, last_loc;
4598 basic_block dest_bb;
4600 gcc_checking_assert (bb == NULL || e == NULL);
4602 if (e == NULL)
4603 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4604 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4606 /* Never build an assert comparing against an integer constant with
4607 TREE_OVERFLOW set. This confuses our undefined overflow warning
4608 machinery. */
4609 if (TREE_OVERFLOW_P (val))
4610 val = drop_tree_overflow (val);
4612 /* The new assertion A will be inserted at BB or E. We need to
4613 determine if the new location is dominated by a previously
4614 registered location for A. If we are doing an edge insertion,
4615 assume that A will be inserted at E->DEST. Note that this is not
4616 necessarily true.
4618 If E is a critical edge, it will be split. But even if E is
4619 split, the new block will dominate the same set of blocks that
4620 E->DEST dominates.
4622 The reverse, however, is not true, blocks dominated by E->DEST
4623 will not be dominated by the new block created to split E. So,
4624 if the insertion location is on a critical edge, we will not use
4625 the new location to move another assertion previously registered
4626 at a block dominated by E->DEST. */
4627 dest_bb = (bb) ? bb : e->dest;
4629 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4630 VAL at a block dominating DEST_BB, then we don't need to insert a new
4631 one. Similarly, if the same assertion already exists at a block
4632 dominated by DEST_BB and the new location is not on a critical
4633 edge, then update the existing location for the assertion (i.e.,
4634 move the assertion up in the dominance tree).
4636 Note, this is implemented as a simple linked list because there
4637 should not be more than a handful of assertions registered per
4638 name. If this becomes a performance problem, a table hashed by
4639 COMP_CODE and VAL could be implemented. */
4640 loc = asserts_for[SSA_NAME_VERSION (name)];
4641 last_loc = loc;
4642 while (loc)
4644 if (loc->comp_code == comp_code
4645 && (loc->val == val
4646 || operand_equal_p (loc->val, val, 0))
4647 && (loc->expr == expr
4648 || operand_equal_p (loc->expr, expr, 0)))
4650 /* If E is not a critical edge and DEST_BB
4651 dominates the existing location for the assertion, move
4652 the assertion up in the dominance tree by updating its
4653 location information. */
4654 if ((e == NULL || !EDGE_CRITICAL_P (e))
4655 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4657 loc->bb = dest_bb;
4658 loc->e = e;
4659 loc->si = si;
4660 return;
4664 /* Update the last node of the list and move to the next one. */
4665 last_loc = loc;
4666 loc = loc->next;
4669 /* If we didn't find an assertion already registered for
4670 NAME COMP_CODE VAL, add a new one at the end of the list of
4671 assertions associated with NAME. */
4672 n = XNEW (struct assert_locus_d);
4673 n->bb = dest_bb;
4674 n->e = e;
4675 n->si = si;
4676 n->comp_code = comp_code;
4677 n->val = val;
4678 n->expr = expr;
4679 n->next = NULL;
4681 if (last_loc)
4682 last_loc->next = n;
4683 else
4684 asserts_for[SSA_NAME_VERSION (name)] = n;
4686 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4689 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4690 Extract a suitable test code and value and store them into *CODE_P and
4691 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4693 If no extraction was possible, return FALSE, otherwise return TRUE.
4695 If INVERT is true, then we invert the result stored into *CODE_P. */
4697 static bool
4698 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4699 tree cond_op0, tree cond_op1,
4700 bool invert, enum tree_code *code_p,
4701 tree *val_p)
4703 enum tree_code comp_code;
4704 tree val;
4706 /* Otherwise, we have a comparison of the form NAME COMP VAL
4707 or VAL COMP NAME. */
4708 if (name == cond_op1)
4710 /* If the predicate is of the form VAL COMP NAME, flip
4711 COMP around because we need to register NAME as the
4712 first operand in the predicate. */
4713 comp_code = swap_tree_comparison (cond_code);
4714 val = cond_op0;
4716 else
4718 /* The comparison is of the form NAME COMP VAL, so the
4719 comparison code remains unchanged. */
4720 comp_code = cond_code;
4721 val = cond_op1;
4724 /* Invert the comparison code as necessary. */
4725 if (invert)
4726 comp_code = invert_tree_comparison (comp_code, 0);
4728 /* VRP does not handle float types. */
4729 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4730 return false;
4732 /* Do not register always-false predicates.
4733 FIXME: this works around a limitation in fold() when dealing with
4734 enumerations. Given 'enum { N1, N2 } x;', fold will not
4735 fold 'if (x > N2)' to 'if (0)'. */
4736 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4737 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4739 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4740 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4742 if (comp_code == GT_EXPR
4743 && (!max
4744 || compare_values (val, max) == 0))
4745 return false;
4747 if (comp_code == LT_EXPR
4748 && (!min
4749 || compare_values (val, min) == 0))
4750 return false;
4752 *code_p = comp_code;
4753 *val_p = val;
4754 return true;
4757 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4758 (otherwise return VAL). VAL and MASK must be zero-extended for
4759 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4760 (to transform signed values into unsigned) and at the end xor
4761 SGNBIT back. */
4763 static double_int
4764 masked_increment (double_int val, double_int mask, double_int sgnbit,
4765 unsigned int prec)
4767 double_int bit = double_int_one, res;
4768 unsigned int i;
4770 val ^= sgnbit;
4771 for (i = 0; i < prec; i++, bit += bit)
4773 res = mask;
4774 if ((res & bit).is_zero ())
4775 continue;
4776 res = bit - double_int_one;
4777 res = (val + bit).and_not (res);
4778 res &= mask;
4779 if (res.ugt (val))
4780 return res ^ sgnbit;
4782 return val ^ sgnbit;
4785 /* Try to register an edge assertion for SSA name NAME on edge E for
4786 the condition COND contributing to the conditional jump pointed to by BSI.
4787 Invert the condition COND if INVERT is true.
4788 Return true if an assertion for NAME could be registered. */
4790 static bool
4791 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4792 enum tree_code cond_code,
4793 tree cond_op0, tree cond_op1, bool invert)
4795 tree val;
4796 enum tree_code comp_code;
4797 bool retval = false;
4799 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4800 cond_op0,
4801 cond_op1,
4802 invert, &comp_code, &val))
4803 return false;
4805 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4806 reachable from E. */
4807 if (live_on_edge (e, name)
4808 && !has_single_use (name))
4810 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
4811 retval = true;
4814 /* In the case of NAME <= CST and NAME being defined as
4815 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4816 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4817 This catches range and anti-range tests. */
4818 if ((comp_code == LE_EXPR
4819 || comp_code == GT_EXPR)
4820 && TREE_CODE (val) == INTEGER_CST
4821 && TYPE_UNSIGNED (TREE_TYPE (val)))
4823 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4824 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
4826 /* Extract CST2 from the (optional) addition. */
4827 if (is_gimple_assign (def_stmt)
4828 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
4830 name2 = gimple_assign_rhs1 (def_stmt);
4831 cst2 = gimple_assign_rhs2 (def_stmt);
4832 if (TREE_CODE (name2) == SSA_NAME
4833 && TREE_CODE (cst2) == INTEGER_CST)
4834 def_stmt = SSA_NAME_DEF_STMT (name2);
4837 /* Extract NAME2 from the (optional) sign-changing cast. */
4838 if (gimple_assign_cast_p (def_stmt))
4840 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
4841 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
4842 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
4843 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
4844 name3 = gimple_assign_rhs1 (def_stmt);
4847 /* If name3 is used later, create an ASSERT_EXPR for it. */
4848 if (name3 != NULL_TREE
4849 && TREE_CODE (name3) == SSA_NAME
4850 && (cst2 == NULL_TREE
4851 || TREE_CODE (cst2) == INTEGER_CST)
4852 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
4853 && live_on_edge (e, name3)
4854 && !has_single_use (name3))
4856 tree tmp;
4858 /* Build an expression for the range test. */
4859 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
4860 if (cst2 != NULL_TREE)
4861 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4863 if (dump_file)
4865 fprintf (dump_file, "Adding assert for ");
4866 print_generic_expr (dump_file, name3, 0);
4867 fprintf (dump_file, " from ");
4868 print_generic_expr (dump_file, tmp, 0);
4869 fprintf (dump_file, "\n");
4872 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
4874 retval = true;
4877 /* If name2 is used later, create an ASSERT_EXPR for it. */
4878 if (name2 != NULL_TREE
4879 && TREE_CODE (name2) == SSA_NAME
4880 && TREE_CODE (cst2) == INTEGER_CST
4881 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4882 && live_on_edge (e, name2)
4883 && !has_single_use (name2))
4885 tree tmp;
4887 /* Build an expression for the range test. */
4888 tmp = name2;
4889 if (TREE_TYPE (name) != TREE_TYPE (name2))
4890 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
4891 if (cst2 != NULL_TREE)
4892 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
4894 if (dump_file)
4896 fprintf (dump_file, "Adding assert for ");
4897 print_generic_expr (dump_file, name2, 0);
4898 fprintf (dump_file, " from ");
4899 print_generic_expr (dump_file, tmp, 0);
4900 fprintf (dump_file, "\n");
4903 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
4905 retval = true;
4909 /* In the case of post-in/decrement tests like if (i++) ... and uses
4910 of the in/decremented value on the edge the extra name we want to
4911 assert for is not on the def chain of the name compared. Instead
4912 it is in the set of use stmts. */
4913 if ((comp_code == NE_EXPR
4914 || comp_code == EQ_EXPR)
4915 && TREE_CODE (val) == INTEGER_CST)
4917 imm_use_iterator ui;
4918 gimple use_stmt;
4919 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
4921 /* Cut off to use-stmts that are in the predecessor. */
4922 if (gimple_bb (use_stmt) != e->src)
4923 continue;
4925 if (!is_gimple_assign (use_stmt))
4926 continue;
4928 enum tree_code code = gimple_assign_rhs_code (use_stmt);
4929 if (code != PLUS_EXPR
4930 && code != MINUS_EXPR)
4931 continue;
4933 tree cst = gimple_assign_rhs2 (use_stmt);
4934 if (TREE_CODE (cst) != INTEGER_CST)
4935 continue;
4937 tree name2 = gimple_assign_lhs (use_stmt);
4938 if (live_on_edge (e, name2))
4940 cst = int_const_binop (code, val, cst);
4941 register_new_assert_for (name2, name2, comp_code, cst,
4942 NULL, e, bsi);
4943 retval = true;
4948 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
4949 && TREE_CODE (val) == INTEGER_CST)
4951 gimple def_stmt = SSA_NAME_DEF_STMT (name);
4952 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
4953 tree val2 = NULL_TREE;
4954 double_int mask = double_int_zero;
4955 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
4956 unsigned int nprec = prec;
4957 enum tree_code rhs_code = ERROR_MARK;
4959 if (is_gimple_assign (def_stmt))
4960 rhs_code = gimple_assign_rhs_code (def_stmt);
4962 /* Add asserts for NAME cmp CST and NAME being defined
4963 as NAME = (int) NAME2. */
4964 if (!TYPE_UNSIGNED (TREE_TYPE (val))
4965 && (comp_code == LE_EXPR || comp_code == LT_EXPR
4966 || comp_code == GT_EXPR || comp_code == GE_EXPR)
4967 && gimple_assign_cast_p (def_stmt))
4969 name2 = gimple_assign_rhs1 (def_stmt);
4970 if (CONVERT_EXPR_CODE_P (rhs_code)
4971 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
4972 && TYPE_UNSIGNED (TREE_TYPE (name2))
4973 && prec == TYPE_PRECISION (TREE_TYPE (name2))
4974 && (comp_code == LE_EXPR || comp_code == GT_EXPR
4975 || !tree_int_cst_equal (val,
4976 TYPE_MIN_VALUE (TREE_TYPE (val))))
4977 && live_on_edge (e, name2)
4978 && !has_single_use (name2))
4980 tree tmp, cst;
4981 enum tree_code new_comp_code = comp_code;
4983 cst = fold_convert (TREE_TYPE (name2),
4984 TYPE_MIN_VALUE (TREE_TYPE (val)));
4985 /* Build an expression for the range test. */
4986 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
4987 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
4988 fold_convert (TREE_TYPE (name2), val));
4989 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
4991 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
4992 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
4993 build_int_cst (TREE_TYPE (name2), 1));
4996 if (dump_file)
4998 fprintf (dump_file, "Adding assert for ");
4999 print_generic_expr (dump_file, name2, 0);
5000 fprintf (dump_file, " from ");
5001 print_generic_expr (dump_file, tmp, 0);
5002 fprintf (dump_file, "\n");
5005 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5006 e, bsi);
5008 retval = true;
5012 /* Add asserts for NAME cmp CST and NAME being defined as
5013 NAME = NAME2 >> CST2.
5015 Extract CST2 from the right shift. */
5016 if (rhs_code == RSHIFT_EXPR)
5018 name2 = gimple_assign_rhs1 (def_stmt);
5019 cst2 = gimple_assign_rhs2 (def_stmt);
5020 if (TREE_CODE (name2) == SSA_NAME
5021 && tree_fits_uhwi_p (cst2)
5022 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5023 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5024 && prec <= HOST_BITS_PER_DOUBLE_INT
5025 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5026 && live_on_edge (e, name2)
5027 && !has_single_use (name2))
5029 mask = double_int::mask (tree_to_uhwi (cst2));
5030 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5033 if (val2 != NULL_TREE
5034 && TREE_CODE (val2) == INTEGER_CST
5035 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5036 TREE_TYPE (val),
5037 val2, cst2), val))
5039 enum tree_code new_comp_code = comp_code;
5040 tree tmp, new_val;
5042 tmp = name2;
5043 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5045 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5047 tree type = build_nonstandard_integer_type (prec, 1);
5048 tmp = build1 (NOP_EXPR, type, name2);
5049 val2 = fold_convert (type, val2);
5051 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5052 new_val = double_int_to_tree (TREE_TYPE (tmp), mask);
5053 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5055 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5057 double_int minval
5058 = double_int::min_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
5059 new_val = val2;
5060 if (minval == tree_to_double_int (new_val))
5061 new_val = NULL_TREE;
5063 else
5065 double_int maxval
5066 = double_int::max_value (prec, TYPE_UNSIGNED (TREE_TYPE (val)));
5067 mask |= tree_to_double_int (val2);
5068 if (mask == maxval)
5069 new_val = NULL_TREE;
5070 else
5071 new_val = double_int_to_tree (TREE_TYPE (val2), mask);
5074 if (new_val)
5076 if (dump_file)
5078 fprintf (dump_file, "Adding assert for ");
5079 print_generic_expr (dump_file, name2, 0);
5080 fprintf (dump_file, " from ");
5081 print_generic_expr (dump_file, tmp, 0);
5082 fprintf (dump_file, "\n");
5085 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5086 NULL, e, bsi);
5087 retval = true;
5091 /* Add asserts for NAME cmp CST and NAME being defined as
5092 NAME = NAME2 & CST2.
5094 Extract CST2 from the and.
5096 Also handle
5097 NAME = (unsigned) NAME2;
5098 casts where NAME's type is unsigned and has smaller precision
5099 than NAME2's type as if it was NAME = NAME2 & MASK. */
5100 names[0] = NULL_TREE;
5101 names[1] = NULL_TREE;
5102 cst2 = NULL_TREE;
5103 if (rhs_code == BIT_AND_EXPR
5104 || (CONVERT_EXPR_CODE_P (rhs_code)
5105 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5106 && TYPE_UNSIGNED (TREE_TYPE (val))
5107 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5108 > prec
5109 && !retval))
5111 name2 = gimple_assign_rhs1 (def_stmt);
5112 if (rhs_code == BIT_AND_EXPR)
5113 cst2 = gimple_assign_rhs2 (def_stmt);
5114 else
5116 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5117 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5119 if (TREE_CODE (name2) == SSA_NAME
5120 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5121 && TREE_CODE (cst2) == INTEGER_CST
5122 && !integer_zerop (cst2)
5123 && nprec <= HOST_BITS_PER_DOUBLE_INT
5124 && (nprec > 1
5125 || TYPE_UNSIGNED (TREE_TYPE (val))))
5127 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5128 if (gimple_assign_cast_p (def_stmt2))
5130 names[1] = gimple_assign_rhs1 (def_stmt2);
5131 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5132 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5133 || (TYPE_PRECISION (TREE_TYPE (name2))
5134 != TYPE_PRECISION (TREE_TYPE (names[1])))
5135 || !live_on_edge (e, names[1])
5136 || has_single_use (names[1]))
5137 names[1] = NULL_TREE;
5139 if (live_on_edge (e, name2)
5140 && !has_single_use (name2))
5141 names[0] = name2;
5144 if (names[0] || names[1])
5146 double_int minv, maxv = double_int_zero, valv, cst2v;
5147 double_int tem, sgnbit;
5148 bool valid_p = false, valn = false, cst2n = false;
5149 enum tree_code ccode = comp_code;
5151 valv = tree_to_double_int (val).zext (nprec);
5152 cst2v = tree_to_double_int (cst2).zext (nprec);
5153 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5155 valn = valv.sext (nprec).is_negative ();
5156 cst2n = cst2v.sext (nprec).is_negative ();
5158 /* If CST2 doesn't have most significant bit set,
5159 but VAL is negative, we have comparison like
5160 if ((x & 0x123) > -4) (always true). Just give up. */
5161 if (!cst2n && valn)
5162 ccode = ERROR_MARK;
5163 if (cst2n)
5164 sgnbit = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
5165 else
5166 sgnbit = double_int_zero;
5167 minv = valv & cst2v;
5168 switch (ccode)
5170 case EQ_EXPR:
5171 /* Minimum unsigned value for equality is VAL & CST2
5172 (should be equal to VAL, otherwise we probably should
5173 have folded the comparison into false) and
5174 maximum unsigned value is VAL | ~CST2. */
5175 maxv = valv | ~cst2v;
5176 maxv = maxv.zext (nprec);
5177 valid_p = true;
5178 break;
5179 case NE_EXPR:
5180 tem = valv | ~cst2v;
5181 tem = tem.zext (nprec);
5182 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5183 if (valv.is_zero ())
5185 cst2n = false;
5186 sgnbit = double_int_zero;
5187 goto gt_expr;
5189 /* If (VAL | ~CST2) is all ones, handle it as
5190 (X & CST2) < VAL. */
5191 if (tem == double_int::mask (nprec))
5193 cst2n = false;
5194 valn = false;
5195 sgnbit = double_int_zero;
5196 goto lt_expr;
5198 if (!cst2n
5199 && cst2v.sext (nprec).is_negative ())
5200 sgnbit
5201 = double_int_one.llshift (nprec - 1, nprec).zext (nprec);
5202 if (!sgnbit.is_zero ())
5204 if (valv == sgnbit)
5206 cst2n = true;
5207 valn = true;
5208 goto gt_expr;
5210 if (tem == double_int::mask (nprec - 1))
5212 cst2n = true;
5213 goto lt_expr;
5215 if (!cst2n)
5216 sgnbit = double_int_zero;
5218 break;
5219 case GE_EXPR:
5220 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5221 is VAL and maximum unsigned value is ~0. For signed
5222 comparison, if CST2 doesn't have most significant bit
5223 set, handle it similarly. If CST2 has MSB set,
5224 the minimum is the same, and maximum is ~0U/2. */
5225 if (minv != valv)
5227 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5228 VAL. */
5229 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5230 if (minv == valv)
5231 break;
5233 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5234 valid_p = true;
5235 break;
5236 case GT_EXPR:
5237 gt_expr:
5238 /* Find out smallest MINV where MINV > VAL
5239 && (MINV & CST2) == MINV, if any. If VAL is signed and
5240 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5241 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5242 if (minv == valv)
5243 break;
5244 maxv = double_int::mask (nprec - (cst2n ? 1 : 0));
5245 valid_p = true;
5246 break;
5247 case LE_EXPR:
5248 /* Minimum unsigned value for <= is 0 and maximum
5249 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5250 Otherwise, find smallest VAL2 where VAL2 > VAL
5251 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5252 as maximum.
5253 For signed comparison, if CST2 doesn't have most
5254 significant bit set, handle it similarly. If CST2 has
5255 MSB set, the maximum is the same and minimum is INT_MIN. */
5256 if (minv == valv)
5257 maxv = valv;
5258 else
5260 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5261 if (maxv == valv)
5262 break;
5263 maxv -= double_int_one;
5265 maxv |= ~cst2v;
5266 maxv = maxv.zext (nprec);
5267 minv = sgnbit;
5268 valid_p = true;
5269 break;
5270 case LT_EXPR:
5271 lt_expr:
5272 /* Minimum unsigned value for < is 0 and maximum
5273 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5274 Otherwise, find smallest VAL2 where VAL2 > VAL
5275 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5276 as maximum.
5277 For signed comparison, if CST2 doesn't have most
5278 significant bit set, handle it similarly. If CST2 has
5279 MSB set, the maximum is the same and minimum is INT_MIN. */
5280 if (minv == valv)
5282 if (valv == sgnbit)
5283 break;
5284 maxv = valv;
5286 else
5288 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5289 if (maxv == valv)
5290 break;
5292 maxv -= double_int_one;
5293 maxv |= ~cst2v;
5294 maxv = maxv.zext (nprec);
5295 minv = sgnbit;
5296 valid_p = true;
5297 break;
5298 default:
5299 break;
5301 if (valid_p
5302 && (maxv - minv).zext (nprec) != double_int::mask (nprec))
5304 tree tmp, new_val, type;
5305 int i;
5307 for (i = 0; i < 2; i++)
5308 if (names[i])
5310 double_int maxv2 = maxv;
5311 tmp = names[i];
5312 type = TREE_TYPE (names[i]);
5313 if (!TYPE_UNSIGNED (type))
5315 type = build_nonstandard_integer_type (nprec, 1);
5316 tmp = build1 (NOP_EXPR, type, names[i]);
5318 if (!minv.is_zero ())
5320 tmp = build2 (PLUS_EXPR, type, tmp,
5321 double_int_to_tree (type, -minv));
5322 maxv2 = maxv - minv;
5324 new_val = double_int_to_tree (type, maxv2);
5326 if (dump_file)
5328 fprintf (dump_file, "Adding assert for ");
5329 print_generic_expr (dump_file, names[i], 0);
5330 fprintf (dump_file, " from ");
5331 print_generic_expr (dump_file, tmp, 0);
5332 fprintf (dump_file, "\n");
5335 register_new_assert_for (names[i], tmp, LE_EXPR,
5336 new_val, NULL, e, bsi);
5337 retval = true;
5343 return retval;
5346 /* OP is an operand of a truth value expression which is known to have
5347 a particular value. Register any asserts for OP and for any
5348 operands in OP's defining statement.
5350 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5351 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5353 static bool
5354 register_edge_assert_for_1 (tree op, enum tree_code code,
5355 edge e, gimple_stmt_iterator bsi)
5357 bool retval = false;
5358 gimple op_def;
5359 tree val;
5360 enum tree_code rhs_code;
5362 /* We only care about SSA_NAMEs. */
5363 if (TREE_CODE (op) != SSA_NAME)
5364 return false;
5366 /* We know that OP will have a zero or nonzero value. If OP is used
5367 more than once go ahead and register an assert for OP.
5369 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5370 it will always be set for OP (because OP is used in a COND_EXPR in
5371 the subgraph). */
5372 if (!has_single_use (op))
5374 val = build_int_cst (TREE_TYPE (op), 0);
5375 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5376 retval = true;
5379 /* Now look at how OP is set. If it's set from a comparison,
5380 a truth operation or some bit operations, then we may be able
5381 to register information about the operands of that assignment. */
5382 op_def = SSA_NAME_DEF_STMT (op);
5383 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5384 return retval;
5386 rhs_code = gimple_assign_rhs_code (op_def);
5388 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5390 bool invert = (code == EQ_EXPR ? true : false);
5391 tree op0 = gimple_assign_rhs1 (op_def);
5392 tree op1 = gimple_assign_rhs2 (op_def);
5394 if (TREE_CODE (op0) == SSA_NAME)
5395 retval |= register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1,
5396 invert);
5397 if (TREE_CODE (op1) == SSA_NAME)
5398 retval |= register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1,
5399 invert);
5401 else if ((code == NE_EXPR
5402 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5403 || (code == EQ_EXPR
5404 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5406 /* Recurse on each operand. */
5407 tree op0 = gimple_assign_rhs1 (op_def);
5408 tree op1 = gimple_assign_rhs2 (op_def);
5409 if (TREE_CODE (op0) == SSA_NAME
5410 && has_single_use (op0))
5411 retval |= register_edge_assert_for_1 (op0, code, e, bsi);
5412 if (TREE_CODE (op1) == SSA_NAME
5413 && has_single_use (op1))
5414 retval |= register_edge_assert_for_1 (op1, code, e, bsi);
5416 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5417 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5419 /* Recurse, flipping CODE. */
5420 code = invert_tree_comparison (code, false);
5421 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5422 code, e, bsi);
5424 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5426 /* Recurse through the copy. */
5427 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5428 code, e, bsi);
5430 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5432 /* Recurse through the type conversion. */
5433 retval |= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def),
5434 code, e, bsi);
5437 return retval;
5440 /* Try to register an edge assertion for SSA name NAME on edge E for
5441 the condition COND contributing to the conditional jump pointed to by SI.
5442 Return true if an assertion for NAME could be registered. */
5444 static bool
5445 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5446 enum tree_code cond_code, tree cond_op0,
5447 tree cond_op1)
5449 tree val;
5450 enum tree_code comp_code;
5451 bool retval = false;
5452 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5454 /* Do not attempt to infer anything in names that flow through
5455 abnormal edges. */
5456 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5457 return false;
5459 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5460 cond_op0, cond_op1,
5461 is_else_edge,
5462 &comp_code, &val))
5463 return false;
5465 /* Register ASSERT_EXPRs for name. */
5466 retval |= register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5467 cond_op1, is_else_edge);
5470 /* If COND is effectively an equality test of an SSA_NAME against
5471 the value zero or one, then we may be able to assert values
5472 for SSA_NAMEs which flow into COND. */
5474 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5475 statement of NAME we can assert both operands of the BIT_AND_EXPR
5476 have nonzero value. */
5477 if (((comp_code == EQ_EXPR && integer_onep (val))
5478 || (comp_code == NE_EXPR && integer_zerop (val))))
5480 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5482 if (is_gimple_assign (def_stmt)
5483 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5485 tree op0 = gimple_assign_rhs1 (def_stmt);
5486 tree op1 = gimple_assign_rhs2 (def_stmt);
5487 retval |= register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5488 retval |= register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5492 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5493 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5494 have zero value. */
5495 if (((comp_code == EQ_EXPR && integer_zerop (val))
5496 || (comp_code == NE_EXPR && integer_onep (val))))
5498 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5500 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5501 necessarily zero value, or if type-precision is one. */
5502 if (is_gimple_assign (def_stmt)
5503 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5504 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5505 || comp_code == EQ_EXPR)))
5507 tree op0 = gimple_assign_rhs1 (def_stmt);
5508 tree op1 = gimple_assign_rhs2 (def_stmt);
5509 retval |= register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5510 retval |= register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5514 return retval;
5518 /* Determine whether the outgoing edges of BB should receive an
5519 ASSERT_EXPR for each of the operands of BB's LAST statement.
5520 The last statement of BB must be a COND_EXPR.
5522 If any of the sub-graphs rooted at BB have an interesting use of
5523 the predicate operands, an assert location node is added to the
5524 list of assertions for the corresponding operands. */
5526 static bool
5527 find_conditional_asserts (basic_block bb, gimple last)
5529 bool need_assert;
5530 gimple_stmt_iterator bsi;
5531 tree op;
5532 edge_iterator ei;
5533 edge e;
5534 ssa_op_iter iter;
5536 need_assert = false;
5537 bsi = gsi_for_stmt (last);
5539 /* Look for uses of the operands in each of the sub-graphs
5540 rooted at BB. We need to check each of the outgoing edges
5541 separately, so that we know what kind of ASSERT_EXPR to
5542 insert. */
5543 FOR_EACH_EDGE (e, ei, bb->succs)
5545 if (e->dest == bb)
5546 continue;
5548 /* Register the necessary assertions for each operand in the
5549 conditional predicate. */
5550 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5552 need_assert |= register_edge_assert_for (op, e, bsi,
5553 gimple_cond_code (last),
5554 gimple_cond_lhs (last),
5555 gimple_cond_rhs (last));
5559 return need_assert;
5562 struct case_info
5564 tree expr;
5565 basic_block bb;
5568 /* Compare two case labels sorting first by the destination bb index
5569 and then by the case value. */
5571 static int
5572 compare_case_labels (const void *p1, const void *p2)
5574 const struct case_info *ci1 = (const struct case_info *) p1;
5575 const struct case_info *ci2 = (const struct case_info *) p2;
5576 int idx1 = ci1->bb->index;
5577 int idx2 = ci2->bb->index;
5579 if (idx1 < idx2)
5580 return -1;
5581 else if (idx1 == idx2)
5583 /* Make sure the default label is first in a group. */
5584 if (!CASE_LOW (ci1->expr))
5585 return -1;
5586 else if (!CASE_LOW (ci2->expr))
5587 return 1;
5588 else
5589 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5590 CASE_LOW (ci2->expr));
5592 else
5593 return 1;
5596 /* Determine whether the outgoing edges of BB should receive an
5597 ASSERT_EXPR for each of the operands of BB's LAST statement.
5598 The last statement of BB must be a SWITCH_EXPR.
5600 If any of the sub-graphs rooted at BB have an interesting use of
5601 the predicate operands, an assert location node is added to the
5602 list of assertions for the corresponding operands. */
5604 static bool
5605 find_switch_asserts (basic_block bb, gimple last)
5607 bool need_assert;
5608 gimple_stmt_iterator bsi;
5609 tree op;
5610 edge e;
5611 struct case_info *ci;
5612 size_t n = gimple_switch_num_labels (last);
5613 #if GCC_VERSION >= 4000
5614 unsigned int idx;
5615 #else
5616 /* Work around GCC 3.4 bug (PR 37086). */
5617 volatile unsigned int idx;
5618 #endif
5620 need_assert = false;
5621 bsi = gsi_for_stmt (last);
5622 op = gimple_switch_index (last);
5623 if (TREE_CODE (op) != SSA_NAME)
5624 return false;
5626 /* Build a vector of case labels sorted by destination label. */
5627 ci = XNEWVEC (struct case_info, n);
5628 for (idx = 0; idx < n; ++idx)
5630 ci[idx].expr = gimple_switch_label (last, idx);
5631 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5633 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5635 for (idx = 0; idx < n; ++idx)
5637 tree min, max;
5638 tree cl = ci[idx].expr;
5639 basic_block cbb = ci[idx].bb;
5641 min = CASE_LOW (cl);
5642 max = CASE_HIGH (cl);
5644 /* If there are multiple case labels with the same destination
5645 we need to combine them to a single value range for the edge. */
5646 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5648 /* Skip labels until the last of the group. */
5649 do {
5650 ++idx;
5651 } while (idx < n && cbb == ci[idx].bb);
5652 --idx;
5654 /* Pick up the maximum of the case label range. */
5655 if (CASE_HIGH (ci[idx].expr))
5656 max = CASE_HIGH (ci[idx].expr);
5657 else
5658 max = CASE_LOW (ci[idx].expr);
5661 /* Nothing to do if the range includes the default label until we
5662 can register anti-ranges. */
5663 if (min == NULL_TREE)
5664 continue;
5666 /* Find the edge to register the assert expr on. */
5667 e = find_edge (bb, cbb);
5669 /* Register the necessary assertions for the operand in the
5670 SWITCH_EXPR. */
5671 need_assert |= register_edge_assert_for (op, e, bsi,
5672 max ? GE_EXPR : EQ_EXPR,
5674 fold_convert (TREE_TYPE (op),
5675 min));
5676 if (max)
5678 need_assert |= register_edge_assert_for (op, e, bsi, LE_EXPR,
5680 fold_convert (TREE_TYPE (op),
5681 max));
5685 XDELETEVEC (ci);
5686 return need_assert;
5690 /* Traverse all the statements in block BB looking for statements that
5691 may generate useful assertions for the SSA names in their operand.
5692 If a statement produces a useful assertion A for name N_i, then the
5693 list of assertions already generated for N_i is scanned to
5694 determine if A is actually needed.
5696 If N_i already had the assertion A at a location dominating the
5697 current location, then nothing needs to be done. Otherwise, the
5698 new location for A is recorded instead.
5700 1- For every statement S in BB, all the variables used by S are
5701 added to bitmap FOUND_IN_SUBGRAPH.
5703 2- If statement S uses an operand N in a way that exposes a known
5704 value range for N, then if N was not already generated by an
5705 ASSERT_EXPR, create a new assert location for N. For instance,
5706 if N is a pointer and the statement dereferences it, we can
5707 assume that N is not NULL.
5709 3- COND_EXPRs are a special case of #2. We can derive range
5710 information from the predicate but need to insert different
5711 ASSERT_EXPRs for each of the sub-graphs rooted at the
5712 conditional block. If the last statement of BB is a conditional
5713 expression of the form 'X op Y', then
5715 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5717 b) If the conditional is the only entry point to the sub-graph
5718 corresponding to the THEN_CLAUSE, recurse into it. On
5719 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5720 an ASSERT_EXPR is added for the corresponding variable.
5722 c) Repeat step (b) on the ELSE_CLAUSE.
5724 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5726 For instance,
5728 if (a == 9)
5729 b = a;
5730 else
5731 b = c + 1;
5733 In this case, an assertion on the THEN clause is useful to
5734 determine that 'a' is always 9 on that edge. However, an assertion
5735 on the ELSE clause would be unnecessary.
5737 4- If BB does not end in a conditional expression, then we recurse
5738 into BB's dominator children.
5740 At the end of the recursive traversal, every SSA name will have a
5741 list of locations where ASSERT_EXPRs should be added. When a new
5742 location for name N is found, it is registered by calling
5743 register_new_assert_for. That function keeps track of all the
5744 registered assertions to prevent adding unnecessary assertions.
5745 For instance, if a pointer P_4 is dereferenced more than once in a
5746 dominator tree, only the location dominating all the dereference of
5747 P_4 will receive an ASSERT_EXPR.
5749 If this function returns true, then it means that there are names
5750 for which we need to generate ASSERT_EXPRs. Those assertions are
5751 inserted by process_assert_insertions. */
5753 static bool
5754 find_assert_locations_1 (basic_block bb, sbitmap live)
5756 gimple_stmt_iterator si;
5757 gimple last;
5758 bool need_assert;
5760 need_assert = false;
5761 last = last_stmt (bb);
5763 /* If BB's last statement is a conditional statement involving integer
5764 operands, determine if we need to add ASSERT_EXPRs. */
5765 if (last
5766 && gimple_code (last) == GIMPLE_COND
5767 && !fp_predicate (last)
5768 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5769 need_assert |= find_conditional_asserts (bb, last);
5771 /* If BB's last statement is a switch statement involving integer
5772 operands, determine if we need to add ASSERT_EXPRs. */
5773 if (last
5774 && gimple_code (last) == GIMPLE_SWITCH
5775 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5776 need_assert |= find_switch_asserts (bb, last);
5778 /* Traverse all the statements in BB marking used names and looking
5779 for statements that may infer assertions for their used operands. */
5780 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5782 gimple stmt;
5783 tree op;
5784 ssa_op_iter i;
5786 stmt = gsi_stmt (si);
5788 if (is_gimple_debug (stmt))
5789 continue;
5791 /* See if we can derive an assertion for any of STMT's operands. */
5792 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5794 tree value;
5795 enum tree_code comp_code;
5797 /* If op is not live beyond this stmt, do not bother to insert
5798 asserts for it. */
5799 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5800 continue;
5802 /* If OP is used in such a way that we can infer a value
5803 range for it, and we don't find a previous assertion for
5804 it, create a new assertion location node for OP. */
5805 if (infer_value_range (stmt, op, &comp_code, &value))
5807 /* If we are able to infer a nonzero value range for OP,
5808 then walk backwards through the use-def chain to see if OP
5809 was set via a typecast.
5811 If so, then we can also infer a nonzero value range
5812 for the operand of the NOP_EXPR. */
5813 if (comp_code == NE_EXPR && integer_zerop (value))
5815 tree t = op;
5816 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5818 while (is_gimple_assign (def_stmt)
5819 && gimple_assign_rhs_code (def_stmt) == NOP_EXPR
5820 && TREE_CODE
5821 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5822 && POINTER_TYPE_P
5823 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5825 t = gimple_assign_rhs1 (def_stmt);
5826 def_stmt = SSA_NAME_DEF_STMT (t);
5828 /* Note we want to register the assert for the
5829 operand of the NOP_EXPR after SI, not after the
5830 conversion. */
5831 if (! has_single_use (t))
5833 register_new_assert_for (t, t, comp_code, value,
5834 bb, NULL, si);
5835 need_assert = true;
5840 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5841 need_assert = true;
5845 /* Update live. */
5846 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5847 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5848 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5849 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5852 /* Traverse all PHI nodes in BB, updating live. */
5853 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5855 use_operand_p arg_p;
5856 ssa_op_iter i;
5857 gimple phi = gsi_stmt (si);
5858 tree res = gimple_phi_result (phi);
5860 if (virtual_operand_p (res))
5861 continue;
5863 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
5865 tree arg = USE_FROM_PTR (arg_p);
5866 if (TREE_CODE (arg) == SSA_NAME)
5867 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
5870 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
5873 return need_assert;
5876 /* Do an RPO walk over the function computing SSA name liveness
5877 on-the-fly and deciding on assert expressions to insert.
5878 Returns true if there are assert expressions to be inserted. */
5880 static bool
5881 find_assert_locations (void)
5883 int *rpo = XNEWVEC (int, last_basic_block);
5884 int *bb_rpo = XNEWVEC (int, last_basic_block);
5885 int *last_rpo = XCNEWVEC (int, last_basic_block);
5886 int rpo_cnt, i;
5887 bool need_asserts;
5889 live = XCNEWVEC (sbitmap, last_basic_block);
5890 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
5891 for (i = 0; i < rpo_cnt; ++i)
5892 bb_rpo[rpo[i]] = i;
5894 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
5895 the order we compute liveness and insert asserts we otherwise
5896 fail to insert asserts into the loop latch. */
5897 loop_p loop;
5898 FOR_EACH_LOOP (loop, 0)
5900 i = loop->latch->index;
5901 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
5902 for (gimple_stmt_iterator gsi = gsi_start_phis (loop->header);
5903 !gsi_end_p (gsi); gsi_next (&gsi))
5905 gimple phi = gsi_stmt (gsi);
5906 if (virtual_operand_p (gimple_phi_result (phi)))
5907 continue;
5908 tree arg = gimple_phi_arg_def (phi, j);
5909 if (TREE_CODE (arg) == SSA_NAME)
5911 if (live[i] == NULL)
5913 live[i] = sbitmap_alloc (num_ssa_names);
5914 bitmap_clear (live[i]);
5916 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
5921 need_asserts = false;
5922 for (i = rpo_cnt - 1; i >= 0; --i)
5924 basic_block bb = BASIC_BLOCK (rpo[i]);
5925 edge e;
5926 edge_iterator ei;
5928 if (!live[rpo[i]])
5930 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
5931 bitmap_clear (live[rpo[i]]);
5934 /* Process BB and update the live information with uses in
5935 this block. */
5936 need_asserts |= find_assert_locations_1 (bb, live[rpo[i]]);
5938 /* Merge liveness into the predecessor blocks and free it. */
5939 if (!bitmap_empty_p (live[rpo[i]]))
5941 int pred_rpo = i;
5942 FOR_EACH_EDGE (e, ei, bb->preds)
5944 int pred = e->src->index;
5945 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
5946 continue;
5948 if (!live[pred])
5950 live[pred] = sbitmap_alloc (num_ssa_names);
5951 bitmap_clear (live[pred]);
5953 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
5955 if (bb_rpo[pred] < pred_rpo)
5956 pred_rpo = bb_rpo[pred];
5959 /* Record the RPO number of the last visited block that needs
5960 live information from this block. */
5961 last_rpo[rpo[i]] = pred_rpo;
5963 else
5965 sbitmap_free (live[rpo[i]]);
5966 live[rpo[i]] = NULL;
5969 /* We can free all successors live bitmaps if all their
5970 predecessors have been visited already. */
5971 FOR_EACH_EDGE (e, ei, bb->succs)
5972 if (last_rpo[e->dest->index] == i
5973 && live[e->dest->index])
5975 sbitmap_free (live[e->dest->index]);
5976 live[e->dest->index] = NULL;
5980 XDELETEVEC (rpo);
5981 XDELETEVEC (bb_rpo);
5982 XDELETEVEC (last_rpo);
5983 for (i = 0; i < last_basic_block; ++i)
5984 if (live[i])
5985 sbitmap_free (live[i]);
5986 XDELETEVEC (live);
5988 return need_asserts;
5991 /* Create an ASSERT_EXPR for NAME and insert it in the location
5992 indicated by LOC. Return true if we made any edge insertions. */
5994 static bool
5995 process_assert_insertions_for (tree name, assert_locus_t loc)
5997 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5998 gimple stmt;
5999 tree cond;
6000 gimple assert_stmt;
6001 edge_iterator ei;
6002 edge e;
6004 /* If we have X <=> X do not insert an assert expr for that. */
6005 if (loc->expr == loc->val)
6006 return false;
6008 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6009 assert_stmt = build_assert_expr_for (cond, name);
6010 if (loc->e)
6012 /* We have been asked to insert the assertion on an edge. This
6013 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6014 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6015 || (gimple_code (gsi_stmt (loc->si))
6016 == GIMPLE_SWITCH));
6018 gsi_insert_on_edge (loc->e, assert_stmt);
6019 return true;
6022 /* Otherwise, we can insert right after LOC->SI iff the
6023 statement must not be the last statement in the block. */
6024 stmt = gsi_stmt (loc->si);
6025 if (!stmt_ends_bb_p (stmt))
6027 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6028 return false;
6031 /* If STMT must be the last statement in BB, we can only insert new
6032 assertions on the non-abnormal edge out of BB. Note that since
6033 STMT is not control flow, there may only be one non-abnormal edge
6034 out of BB. */
6035 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6036 if (!(e->flags & EDGE_ABNORMAL))
6038 gsi_insert_on_edge (e, assert_stmt);
6039 return true;
6042 gcc_unreachable ();
6046 /* Process all the insertions registered for every name N_i registered
6047 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6048 found in ASSERTS_FOR[i]. */
6050 static void
6051 process_assert_insertions (void)
6053 unsigned i;
6054 bitmap_iterator bi;
6055 bool update_edges_p = false;
6056 int num_asserts = 0;
6058 if (dump_file && (dump_flags & TDF_DETAILS))
6059 dump_all_asserts (dump_file);
6061 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6063 assert_locus_t loc = asserts_for[i];
6064 gcc_assert (loc);
6066 while (loc)
6068 assert_locus_t next = loc->next;
6069 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6070 free (loc);
6071 loc = next;
6072 num_asserts++;
6076 if (update_edges_p)
6077 gsi_commit_edge_inserts ();
6079 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6080 num_asserts);
6084 /* Traverse the flowgraph looking for conditional jumps to insert range
6085 expressions. These range expressions are meant to provide information
6086 to optimizations that need to reason in terms of value ranges. They
6087 will not be expanded into RTL. For instance, given:
6089 x = ...
6090 y = ...
6091 if (x < y)
6092 y = x - 2;
6093 else
6094 x = y + 3;
6096 this pass will transform the code into:
6098 x = ...
6099 y = ...
6100 if (x < y)
6102 x = ASSERT_EXPR <x, x < y>
6103 y = x - 2
6105 else
6107 y = ASSERT_EXPR <y, x <= y>
6108 x = y + 3
6111 The idea is that once copy and constant propagation have run, other
6112 optimizations will be able to determine what ranges of values can 'x'
6113 take in different paths of the code, simply by checking the reaching
6114 definition of 'x'. */
6116 static void
6117 insert_range_assertions (void)
6119 need_assert_for = BITMAP_ALLOC (NULL);
6120 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6122 calculate_dominance_info (CDI_DOMINATORS);
6124 if (find_assert_locations ())
6126 process_assert_insertions ();
6127 update_ssa (TODO_update_ssa_no_phi);
6130 if (dump_file && (dump_flags & TDF_DETAILS))
6132 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6133 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6136 free (asserts_for);
6137 BITMAP_FREE (need_assert_for);
6140 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6141 and "struct" hacks. If VRP can determine that the
6142 array subscript is a constant, check if it is outside valid
6143 range. If the array subscript is a RANGE, warn if it is
6144 non-overlapping with valid range.
6145 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6147 static void
6148 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6150 value_range_t* vr = NULL;
6151 tree low_sub, up_sub;
6152 tree low_bound, up_bound, up_bound_p1;
6153 tree base;
6155 if (TREE_NO_WARNING (ref))
6156 return;
6158 low_sub = up_sub = TREE_OPERAND (ref, 1);
6159 up_bound = array_ref_up_bound (ref);
6161 /* Can not check flexible arrays. */
6162 if (!up_bound
6163 || TREE_CODE (up_bound) != INTEGER_CST)
6164 return;
6166 /* Accesses to trailing arrays via pointers may access storage
6167 beyond the types array bounds. */
6168 base = get_base_address (ref);
6169 if (base && TREE_CODE (base) == MEM_REF)
6171 tree cref, next = NULL_TREE;
6173 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6174 return;
6176 cref = TREE_OPERAND (ref, 0);
6177 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6178 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6179 next && TREE_CODE (next) != FIELD_DECL;
6180 next = DECL_CHAIN (next))
6183 /* If this is the last field in a struct type or a field in a
6184 union type do not warn. */
6185 if (!next)
6186 return;
6189 low_bound = array_ref_low_bound (ref);
6190 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, integer_one_node);
6192 if (TREE_CODE (low_sub) == SSA_NAME)
6194 vr = get_value_range (low_sub);
6195 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6197 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6198 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6202 if (vr && vr->type == VR_ANTI_RANGE)
6204 if (TREE_CODE (up_sub) == INTEGER_CST
6205 && tree_int_cst_lt (up_bound, up_sub)
6206 && TREE_CODE (low_sub) == INTEGER_CST
6207 && tree_int_cst_lt (low_sub, low_bound))
6209 warning_at (location, OPT_Warray_bounds,
6210 "array subscript is outside array bounds");
6211 TREE_NO_WARNING (ref) = 1;
6214 else if (TREE_CODE (up_sub) == INTEGER_CST
6215 && (ignore_off_by_one
6216 ? (tree_int_cst_lt (up_bound, up_sub)
6217 && !tree_int_cst_equal (up_bound_p1, up_sub))
6218 : (tree_int_cst_lt (up_bound, up_sub)
6219 || tree_int_cst_equal (up_bound_p1, up_sub))))
6221 if (dump_file && (dump_flags & TDF_DETAILS))
6223 fprintf (dump_file, "Array bound warning for ");
6224 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6225 fprintf (dump_file, "\n");
6227 warning_at (location, OPT_Warray_bounds,
6228 "array subscript is above array bounds");
6229 TREE_NO_WARNING (ref) = 1;
6231 else if (TREE_CODE (low_sub) == INTEGER_CST
6232 && tree_int_cst_lt (low_sub, low_bound))
6234 if (dump_file && (dump_flags & TDF_DETAILS))
6236 fprintf (dump_file, "Array bound warning for ");
6237 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6238 fprintf (dump_file, "\n");
6240 warning_at (location, OPT_Warray_bounds,
6241 "array subscript is below array bounds");
6242 TREE_NO_WARNING (ref) = 1;
6246 /* Searches if the expr T, located at LOCATION computes
6247 address of an ARRAY_REF, and call check_array_ref on it. */
6249 static void
6250 search_for_addr_array (tree t, location_t location)
6252 while (TREE_CODE (t) == SSA_NAME)
6254 gimple g = SSA_NAME_DEF_STMT (t);
6256 if (gimple_code (g) != GIMPLE_ASSIGN)
6257 return;
6259 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6260 != GIMPLE_SINGLE_RHS)
6261 return;
6263 t = gimple_assign_rhs1 (g);
6267 /* We are only interested in addresses of ARRAY_REF's. */
6268 if (TREE_CODE (t) != ADDR_EXPR)
6269 return;
6271 /* Check each ARRAY_REFs in the reference chain. */
6274 if (TREE_CODE (t) == ARRAY_REF)
6275 check_array_ref (location, t, true /*ignore_off_by_one*/);
6277 t = TREE_OPERAND (t, 0);
6279 while (handled_component_p (t));
6281 if (TREE_CODE (t) == MEM_REF
6282 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6283 && !TREE_NO_WARNING (t))
6285 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6286 tree low_bound, up_bound, el_sz;
6287 double_int idx;
6288 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6289 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6290 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6291 return;
6293 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6294 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6295 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6296 if (!low_bound
6297 || TREE_CODE (low_bound) != INTEGER_CST
6298 || !up_bound
6299 || TREE_CODE (up_bound) != INTEGER_CST
6300 || !el_sz
6301 || TREE_CODE (el_sz) != INTEGER_CST)
6302 return;
6304 idx = mem_ref_offset (t);
6305 idx = idx.sdiv (tree_to_double_int (el_sz), TRUNC_DIV_EXPR);
6306 if (idx.slt (double_int_zero))
6308 if (dump_file && (dump_flags & TDF_DETAILS))
6310 fprintf (dump_file, "Array bound warning for ");
6311 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6312 fprintf (dump_file, "\n");
6314 warning_at (location, OPT_Warray_bounds,
6315 "array subscript is below array bounds");
6316 TREE_NO_WARNING (t) = 1;
6318 else if (idx.sgt (tree_to_double_int (up_bound)
6319 - tree_to_double_int (low_bound)
6320 + double_int_one))
6322 if (dump_file && (dump_flags & TDF_DETAILS))
6324 fprintf (dump_file, "Array bound warning for ");
6325 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6326 fprintf (dump_file, "\n");
6328 warning_at (location, OPT_Warray_bounds,
6329 "array subscript is above array bounds");
6330 TREE_NO_WARNING (t) = 1;
6335 /* walk_tree() callback that checks if *TP is
6336 an ARRAY_REF inside an ADDR_EXPR (in which an array
6337 subscript one outside the valid range is allowed). Call
6338 check_array_ref for each ARRAY_REF found. The location is
6339 passed in DATA. */
6341 static tree
6342 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6344 tree t = *tp;
6345 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6346 location_t location;
6348 if (EXPR_HAS_LOCATION (t))
6349 location = EXPR_LOCATION (t);
6350 else
6352 location_t *locp = (location_t *) wi->info;
6353 location = *locp;
6356 *walk_subtree = TRUE;
6358 if (TREE_CODE (t) == ARRAY_REF)
6359 check_array_ref (location, t, false /*ignore_off_by_one*/);
6361 if (TREE_CODE (t) == MEM_REF
6362 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6363 search_for_addr_array (TREE_OPERAND (t, 0), location);
6365 if (TREE_CODE (t) == ADDR_EXPR)
6366 *walk_subtree = FALSE;
6368 return NULL_TREE;
6371 /* Walk over all statements of all reachable BBs and call check_array_bounds
6372 on them. */
6374 static void
6375 check_all_array_refs (void)
6377 basic_block bb;
6378 gimple_stmt_iterator si;
6380 FOR_EACH_BB (bb)
6382 edge_iterator ei;
6383 edge e;
6384 bool executable = false;
6386 /* Skip blocks that were found to be unreachable. */
6387 FOR_EACH_EDGE (e, ei, bb->preds)
6388 executable |= !!(e->flags & EDGE_EXECUTABLE);
6389 if (!executable)
6390 continue;
6392 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6394 gimple stmt = gsi_stmt (si);
6395 struct walk_stmt_info wi;
6396 if (!gimple_has_location (stmt))
6397 continue;
6399 if (is_gimple_call (stmt))
6401 size_t i;
6402 size_t n = gimple_call_num_args (stmt);
6403 for (i = 0; i < n; i++)
6405 tree arg = gimple_call_arg (stmt, i);
6406 search_for_addr_array (arg, gimple_location (stmt));
6409 else
6411 memset (&wi, 0, sizeof (wi));
6412 wi.info = CONST_CAST (void *, (const void *)
6413 gimple_location_ptr (stmt));
6415 walk_gimple_op (gsi_stmt (si),
6416 check_array_bounds,
6417 &wi);
6423 /* Return true if all imm uses of VAR are either in STMT, or
6424 feed (optionally through a chain of single imm uses) GIMPLE_COND
6425 in basic block COND_BB. */
6427 static bool
6428 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6430 use_operand_p use_p, use2_p;
6431 imm_use_iterator iter;
6433 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6434 if (USE_STMT (use_p) != stmt)
6436 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6437 if (is_gimple_debug (use_stmt))
6438 continue;
6439 while (is_gimple_assign (use_stmt)
6440 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6441 && single_imm_use (gimple_assign_lhs (use_stmt),
6442 &use2_p, &use_stmt2))
6443 use_stmt = use_stmt2;
6444 if (gimple_code (use_stmt) != GIMPLE_COND
6445 || gimple_bb (use_stmt) != cond_bb)
6446 return false;
6448 return true;
6451 /* Handle
6452 _4 = x_3 & 31;
6453 if (_4 != 0)
6454 goto <bb 6>;
6455 else
6456 goto <bb 7>;
6457 <bb 6>:
6458 __builtin_unreachable ();
6459 <bb 7>:
6460 x_5 = ASSERT_EXPR <x_3, ...>;
6461 If x_3 has no other immediate uses (checked by caller),
6462 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6463 from the non-zero bitmask. */
6465 static void
6466 maybe_set_nonzero_bits (basic_block bb, tree var)
6468 edge e = single_pred_edge (bb);
6469 basic_block cond_bb = e->src;
6470 gimple stmt = last_stmt (cond_bb);
6471 tree cst;
6473 if (stmt == NULL
6474 || gimple_code (stmt) != GIMPLE_COND
6475 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6476 ? EQ_EXPR : NE_EXPR)
6477 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6478 || !integer_zerop (gimple_cond_rhs (stmt)))
6479 return;
6481 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6482 if (!is_gimple_assign (stmt)
6483 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6484 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6485 return;
6486 if (gimple_assign_rhs1 (stmt) != var)
6488 gimple stmt2;
6490 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6491 return;
6492 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6493 if (!gimple_assign_cast_p (stmt2)
6494 || gimple_assign_rhs1 (stmt2) != var
6495 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6496 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6497 != TYPE_PRECISION (TREE_TYPE (var))))
6498 return;
6500 cst = gimple_assign_rhs2 (stmt);
6501 set_nonzero_bits (var, (get_nonzero_bits (var)
6502 & ~tree_to_double_int (cst)));
6505 /* Convert range assertion expressions into the implied copies and
6506 copy propagate away the copies. Doing the trivial copy propagation
6507 here avoids the need to run the full copy propagation pass after
6508 VRP.
6510 FIXME, this will eventually lead to copy propagation removing the
6511 names that had useful range information attached to them. For
6512 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6513 then N_i will have the range [3, +INF].
6515 However, by converting the assertion into the implied copy
6516 operation N_i = N_j, we will then copy-propagate N_j into the uses
6517 of N_i and lose the range information. We may want to hold on to
6518 ASSERT_EXPRs a little while longer as the ranges could be used in
6519 things like jump threading.
6521 The problem with keeping ASSERT_EXPRs around is that passes after
6522 VRP need to handle them appropriately.
6524 Another approach would be to make the range information a first
6525 class property of the SSA_NAME so that it can be queried from
6526 any pass. This is made somewhat more complex by the need for
6527 multiple ranges to be associated with one SSA_NAME. */
6529 static void
6530 remove_range_assertions (void)
6532 basic_block bb;
6533 gimple_stmt_iterator si;
6534 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6535 a basic block preceeded by GIMPLE_COND branching to it and
6536 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6537 int is_unreachable;
6539 /* Note that the BSI iterator bump happens at the bottom of the
6540 loop and no bump is necessary if we're removing the statement
6541 referenced by the current BSI. */
6542 FOR_EACH_BB (bb)
6543 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6545 gimple stmt = gsi_stmt (si);
6546 gimple use_stmt;
6548 if (is_gimple_assign (stmt)
6549 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6551 tree lhs = gimple_assign_lhs (stmt);
6552 tree rhs = gimple_assign_rhs1 (stmt);
6553 tree var;
6554 tree cond = fold (ASSERT_EXPR_COND (rhs));
6555 use_operand_p use_p;
6556 imm_use_iterator iter;
6558 gcc_assert (cond != boolean_false_node);
6560 var = ASSERT_EXPR_VAR (rhs);
6561 gcc_assert (TREE_CODE (var) == SSA_NAME);
6563 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6564 && SSA_NAME_RANGE_INFO (lhs))
6566 if (is_unreachable == -1)
6568 is_unreachable = 0;
6569 if (single_pred_p (bb)
6570 && assert_unreachable_fallthru_edge_p
6571 (single_pred_edge (bb)))
6572 is_unreachable = 1;
6574 /* Handle
6575 if (x_7 >= 10 && x_7 < 20)
6576 __builtin_unreachable ();
6577 x_8 = ASSERT_EXPR <x_7, ...>;
6578 if the only uses of x_7 are in the ASSERT_EXPR and
6579 in the condition. In that case, we can copy the
6580 range info from x_8 computed in this pass also
6581 for x_7. */
6582 if (is_unreachable
6583 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6584 single_pred (bb)))
6586 set_range_info (var, SSA_NAME_RANGE_INFO (lhs)->min,
6587 SSA_NAME_RANGE_INFO (lhs)->max);
6588 maybe_set_nonzero_bits (bb, var);
6592 /* Propagate the RHS into every use of the LHS. */
6593 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6594 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6595 SET_USE (use_p, var);
6597 /* And finally, remove the copy, it is not needed. */
6598 gsi_remove (&si, true);
6599 release_defs (stmt);
6601 else
6603 gsi_next (&si);
6604 is_unreachable = 0;
6610 /* Return true if STMT is interesting for VRP. */
6612 static bool
6613 stmt_interesting_for_vrp (gimple stmt)
6615 if (gimple_code (stmt) == GIMPLE_PHI)
6617 tree res = gimple_phi_result (stmt);
6618 return (!virtual_operand_p (res)
6619 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6620 || POINTER_TYPE_P (TREE_TYPE (res))));
6622 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6624 tree lhs = gimple_get_lhs (stmt);
6626 /* In general, assignments with virtual operands are not useful
6627 for deriving ranges, with the obvious exception of calls to
6628 builtin functions. */
6629 if (lhs && TREE_CODE (lhs) == SSA_NAME
6630 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6631 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6632 && (is_gimple_call (stmt)
6633 || !gimple_vuse (stmt)))
6634 return true;
6636 else if (gimple_code (stmt) == GIMPLE_COND
6637 || gimple_code (stmt) == GIMPLE_SWITCH)
6638 return true;
6640 return false;
6644 /* Initialize local data structures for VRP. */
6646 static void
6647 vrp_initialize (void)
6649 basic_block bb;
6651 values_propagated = false;
6652 num_vr_values = num_ssa_names;
6653 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6654 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6656 FOR_EACH_BB (bb)
6658 gimple_stmt_iterator si;
6660 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6662 gimple phi = gsi_stmt (si);
6663 if (!stmt_interesting_for_vrp (phi))
6665 tree lhs = PHI_RESULT (phi);
6666 set_value_range_to_varying (get_value_range (lhs));
6667 prop_set_simulate_again (phi, false);
6669 else
6670 prop_set_simulate_again (phi, true);
6673 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6675 gimple stmt = gsi_stmt (si);
6677 /* If the statement is a control insn, then we do not
6678 want to avoid simulating the statement once. Failure
6679 to do so means that those edges will never get added. */
6680 if (stmt_ends_bb_p (stmt))
6681 prop_set_simulate_again (stmt, true);
6682 else if (!stmt_interesting_for_vrp (stmt))
6684 ssa_op_iter i;
6685 tree def;
6686 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6687 set_value_range_to_varying (get_value_range (def));
6688 prop_set_simulate_again (stmt, false);
6690 else
6691 prop_set_simulate_again (stmt, true);
6696 /* Return the singleton value-range for NAME or NAME. */
6698 static inline tree
6699 vrp_valueize (tree name)
6701 if (TREE_CODE (name) == SSA_NAME)
6703 value_range_t *vr = get_value_range (name);
6704 if (vr->type == VR_RANGE
6705 && (vr->min == vr->max
6706 || operand_equal_p (vr->min, vr->max, 0)))
6707 return vr->min;
6709 return name;
6712 /* Visit assignment STMT. If it produces an interesting range, record
6713 the SSA name in *OUTPUT_P. */
6715 static enum ssa_prop_result
6716 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6718 tree def, lhs;
6719 ssa_op_iter iter;
6720 enum gimple_code code = gimple_code (stmt);
6721 lhs = gimple_get_lhs (stmt);
6723 /* We only keep track of ranges in integral and pointer types. */
6724 if (TREE_CODE (lhs) == SSA_NAME
6725 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6726 /* It is valid to have NULL MIN/MAX values on a type. See
6727 build_range_type. */
6728 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6729 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6730 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6732 value_range_t new_vr = VR_INITIALIZER;
6734 /* Try folding the statement to a constant first. */
6735 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6736 if (tem && !is_overflow_infinity (tem))
6737 set_value_range (&new_vr, VR_RANGE, tem, tem, NULL);
6738 /* Then dispatch to value-range extracting functions. */
6739 else if (code == GIMPLE_CALL)
6740 extract_range_basic (&new_vr, stmt);
6741 else
6742 extract_range_from_assignment (&new_vr, stmt);
6744 if (update_value_range (lhs, &new_vr))
6746 *output_p = lhs;
6748 if (dump_file && (dump_flags & TDF_DETAILS))
6750 fprintf (dump_file, "Found new range for ");
6751 print_generic_expr (dump_file, lhs, 0);
6752 fprintf (dump_file, ": ");
6753 dump_value_range (dump_file, &new_vr);
6754 fprintf (dump_file, "\n\n");
6757 if (new_vr.type == VR_VARYING)
6758 return SSA_PROP_VARYING;
6760 return SSA_PROP_INTERESTING;
6763 return SSA_PROP_NOT_INTERESTING;
6766 /* Every other statement produces no useful ranges. */
6767 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6768 set_value_range_to_varying (get_value_range (def));
6770 return SSA_PROP_VARYING;
6773 /* Helper that gets the value range of the SSA_NAME with version I
6774 or a symbolic range containing the SSA_NAME only if the value range
6775 is varying or undefined. */
6777 static inline value_range_t
6778 get_vr_for_comparison (int i)
6780 value_range_t vr = *get_value_range (ssa_name (i));
6782 /* If name N_i does not have a valid range, use N_i as its own
6783 range. This allows us to compare against names that may
6784 have N_i in their ranges. */
6785 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6787 vr.type = VR_RANGE;
6788 vr.min = ssa_name (i);
6789 vr.max = ssa_name (i);
6792 return vr;
6795 /* Compare all the value ranges for names equivalent to VAR with VAL
6796 using comparison code COMP. Return the same value returned by
6797 compare_range_with_value, including the setting of
6798 *STRICT_OVERFLOW_P. */
6800 static tree
6801 compare_name_with_value (enum tree_code comp, tree var, tree val,
6802 bool *strict_overflow_p)
6804 bitmap_iterator bi;
6805 unsigned i;
6806 bitmap e;
6807 tree retval, t;
6808 int used_strict_overflow;
6809 bool sop;
6810 value_range_t equiv_vr;
6812 /* Get the set of equivalences for VAR. */
6813 e = get_value_range (var)->equiv;
6815 /* Start at -1. Set it to 0 if we do a comparison without relying
6816 on overflow, or 1 if all comparisons rely on overflow. */
6817 used_strict_overflow = -1;
6819 /* Compare vars' value range with val. */
6820 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6821 sop = false;
6822 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6823 if (retval)
6824 used_strict_overflow = sop ? 1 : 0;
6826 /* If the equiv set is empty we have done all work we need to do. */
6827 if (e == NULL)
6829 if (retval
6830 && used_strict_overflow > 0)
6831 *strict_overflow_p = true;
6832 return retval;
6835 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6837 equiv_vr = get_vr_for_comparison (i);
6838 sop = false;
6839 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6840 if (t)
6842 /* If we get different answers from different members
6843 of the equivalence set this check must be in a dead
6844 code region. Folding it to a trap representation
6845 would be correct here. For now just return don't-know. */
6846 if (retval != NULL
6847 && t != retval)
6849 retval = NULL_TREE;
6850 break;
6852 retval = t;
6854 if (!sop)
6855 used_strict_overflow = 0;
6856 else if (used_strict_overflow < 0)
6857 used_strict_overflow = 1;
6861 if (retval
6862 && used_strict_overflow > 0)
6863 *strict_overflow_p = true;
6865 return retval;
6869 /* Given a comparison code COMP and names N1 and N2, compare all the
6870 ranges equivalent to N1 against all the ranges equivalent to N2
6871 to determine the value of N1 COMP N2. Return the same value
6872 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6873 whether we relied on an overflow infinity in the comparison. */
6876 static tree
6877 compare_names (enum tree_code comp, tree n1, tree n2,
6878 bool *strict_overflow_p)
6880 tree t, retval;
6881 bitmap e1, e2;
6882 bitmap_iterator bi1, bi2;
6883 unsigned i1, i2;
6884 int used_strict_overflow;
6885 static bitmap_obstack *s_obstack = NULL;
6886 static bitmap s_e1 = NULL, s_e2 = NULL;
6888 /* Compare the ranges of every name equivalent to N1 against the
6889 ranges of every name equivalent to N2. */
6890 e1 = get_value_range (n1)->equiv;
6891 e2 = get_value_range (n2)->equiv;
6893 /* Use the fake bitmaps if e1 or e2 are not available. */
6894 if (s_obstack == NULL)
6896 s_obstack = XNEW (bitmap_obstack);
6897 bitmap_obstack_initialize (s_obstack);
6898 s_e1 = BITMAP_ALLOC (s_obstack);
6899 s_e2 = BITMAP_ALLOC (s_obstack);
6901 if (e1 == NULL)
6902 e1 = s_e1;
6903 if (e2 == NULL)
6904 e2 = s_e2;
6906 /* Add N1 and N2 to their own set of equivalences to avoid
6907 duplicating the body of the loop just to check N1 and N2
6908 ranges. */
6909 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
6910 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
6912 /* If the equivalence sets have a common intersection, then the two
6913 names can be compared without checking their ranges. */
6914 if (bitmap_intersect_p (e1, e2))
6916 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6917 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6919 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
6920 ? boolean_true_node
6921 : boolean_false_node;
6924 /* Start at -1. Set it to 0 if we do a comparison without relying
6925 on overflow, or 1 if all comparisons rely on overflow. */
6926 used_strict_overflow = -1;
6928 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6929 N2 to their own set of equivalences to avoid duplicating the body
6930 of the loop just to check N1 and N2 ranges. */
6931 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
6933 value_range_t vr1 = get_vr_for_comparison (i1);
6935 t = retval = NULL_TREE;
6936 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
6938 bool sop = false;
6940 value_range_t vr2 = get_vr_for_comparison (i2);
6942 t = compare_ranges (comp, &vr1, &vr2, &sop);
6943 if (t)
6945 /* If we get different answers from different members
6946 of the equivalence set this check must be in a dead
6947 code region. Folding it to a trap representation
6948 would be correct here. For now just return don't-know. */
6949 if (retval != NULL
6950 && t != retval)
6952 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6953 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6954 return NULL_TREE;
6956 retval = t;
6958 if (!sop)
6959 used_strict_overflow = 0;
6960 else if (used_strict_overflow < 0)
6961 used_strict_overflow = 1;
6965 if (retval)
6967 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6968 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6969 if (used_strict_overflow > 0)
6970 *strict_overflow_p = true;
6971 return retval;
6975 /* None of the equivalent ranges are useful in computing this
6976 comparison. */
6977 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
6978 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
6979 return NULL_TREE;
6982 /* Helper function for vrp_evaluate_conditional_warnv. */
6984 static tree
6985 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
6986 tree op0, tree op1,
6987 bool * strict_overflow_p)
6989 value_range_t *vr0, *vr1;
6991 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
6992 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
6994 if (vr0 && vr1)
6995 return compare_ranges (code, vr0, vr1, strict_overflow_p);
6996 else if (vr0 && vr1 == NULL)
6997 return compare_range_with_value (code, vr0, op1, strict_overflow_p);
6998 else if (vr0 == NULL && vr1)
6999 return (compare_range_with_value
7000 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7001 return NULL;
7004 /* Helper function for vrp_evaluate_conditional_warnv. */
7006 static tree
7007 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7008 tree op1, bool use_equiv_p,
7009 bool *strict_overflow_p, bool *only_ranges)
7011 tree ret;
7012 if (only_ranges)
7013 *only_ranges = true;
7015 /* We only deal with integral and pointer types. */
7016 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7017 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7018 return NULL_TREE;
7020 if (use_equiv_p)
7022 if (only_ranges
7023 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7024 (code, op0, op1, strict_overflow_p)))
7025 return ret;
7026 *only_ranges = false;
7027 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7028 return compare_names (code, op0, op1, strict_overflow_p);
7029 else if (TREE_CODE (op0) == SSA_NAME)
7030 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7031 else if (TREE_CODE (op1) == SSA_NAME)
7032 return (compare_name_with_value
7033 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7035 else
7036 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7037 strict_overflow_p);
7038 return NULL_TREE;
7041 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7042 information. Return NULL if the conditional can not be evaluated.
7043 The ranges of all the names equivalent with the operands in COND
7044 will be used when trying to compute the value. If the result is
7045 based on undefined signed overflow, issue a warning if
7046 appropriate. */
7048 static tree
7049 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7051 bool sop;
7052 tree ret;
7053 bool only_ranges;
7055 /* Some passes and foldings leak constants with overflow flag set
7056 into the IL. Avoid doing wrong things with these and bail out. */
7057 if ((TREE_CODE (op0) == INTEGER_CST
7058 && TREE_OVERFLOW (op0))
7059 || (TREE_CODE (op1) == INTEGER_CST
7060 && TREE_OVERFLOW (op1)))
7061 return NULL_TREE;
7063 sop = false;
7064 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7065 &only_ranges);
7067 if (ret && sop)
7069 enum warn_strict_overflow_code wc;
7070 const char* warnmsg;
7072 if (is_gimple_min_invariant (ret))
7074 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7075 warnmsg = G_("assuming signed overflow does not occur when "
7076 "simplifying conditional to constant");
7078 else
7080 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7081 warnmsg = G_("assuming signed overflow does not occur when "
7082 "simplifying conditional");
7085 if (issue_strict_overflow_warning (wc))
7087 location_t location;
7089 if (!gimple_has_location (stmt))
7090 location = input_location;
7091 else
7092 location = gimple_location (stmt);
7093 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7097 if (warn_type_limits
7098 && ret && only_ranges
7099 && TREE_CODE_CLASS (code) == tcc_comparison
7100 && TREE_CODE (op0) == SSA_NAME)
7102 /* If the comparison is being folded and the operand on the LHS
7103 is being compared against a constant value that is outside of
7104 the natural range of OP0's type, then the predicate will
7105 always fold regardless of the value of OP0. If -Wtype-limits
7106 was specified, emit a warning. */
7107 tree type = TREE_TYPE (op0);
7108 value_range_t *vr0 = get_value_range (op0);
7110 if (vr0->type != VR_VARYING
7111 && INTEGRAL_TYPE_P (type)
7112 && vrp_val_is_min (vr0->min)
7113 && vrp_val_is_max (vr0->max)
7114 && is_gimple_min_invariant (op1))
7116 location_t location;
7118 if (!gimple_has_location (stmt))
7119 location = input_location;
7120 else
7121 location = gimple_location (stmt);
7123 warning_at (location, OPT_Wtype_limits,
7124 integer_zerop (ret)
7125 ? G_("comparison always false "
7126 "due to limited range of data type")
7127 : G_("comparison always true "
7128 "due to limited range of data type"));
7132 return ret;
7136 /* Visit conditional statement STMT. If we can determine which edge
7137 will be taken out of STMT's basic block, record it in
7138 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7139 SSA_PROP_VARYING. */
7141 static enum ssa_prop_result
7142 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7144 tree val;
7145 bool sop;
7147 *taken_edge_p = NULL;
7149 if (dump_file && (dump_flags & TDF_DETAILS))
7151 tree use;
7152 ssa_op_iter i;
7154 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7155 print_gimple_stmt (dump_file, stmt, 0, 0);
7156 fprintf (dump_file, "\nWith known ranges\n");
7158 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7160 fprintf (dump_file, "\t");
7161 print_generic_expr (dump_file, use, 0);
7162 fprintf (dump_file, ": ");
7163 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7166 fprintf (dump_file, "\n");
7169 /* Compute the value of the predicate COND by checking the known
7170 ranges of each of its operands.
7172 Note that we cannot evaluate all the equivalent ranges here
7173 because those ranges may not yet be final and with the current
7174 propagation strategy, we cannot determine when the value ranges
7175 of the names in the equivalence set have changed.
7177 For instance, given the following code fragment
7179 i_5 = PHI <8, i_13>
7181 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7182 if (i_14 == 1)
7185 Assume that on the first visit to i_14, i_5 has the temporary
7186 range [8, 8] because the second argument to the PHI function is
7187 not yet executable. We derive the range ~[0, 0] for i_14 and the
7188 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7189 the first time, since i_14 is equivalent to the range [8, 8], we
7190 determine that the predicate is always false.
7192 On the next round of propagation, i_13 is determined to be
7193 VARYING, which causes i_5 to drop down to VARYING. So, another
7194 visit to i_14 is scheduled. In this second visit, we compute the
7195 exact same range and equivalence set for i_14, namely ~[0, 0] and
7196 { i_5 }. But we did not have the previous range for i_5
7197 registered, so vrp_visit_assignment thinks that the range for
7198 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7199 is not visited again, which stops propagation from visiting
7200 statements in the THEN clause of that if().
7202 To properly fix this we would need to keep the previous range
7203 value for the names in the equivalence set. This way we would've
7204 discovered that from one visit to the other i_5 changed from
7205 range [8, 8] to VR_VARYING.
7207 However, fixing this apparent limitation may not be worth the
7208 additional checking. Testing on several code bases (GCC, DLV,
7209 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7210 4 more predicates folded in SPEC. */
7211 sop = false;
7213 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7214 gimple_cond_lhs (stmt),
7215 gimple_cond_rhs (stmt),
7216 false, &sop, NULL);
7217 if (val)
7219 if (!sop)
7220 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7221 else
7223 if (dump_file && (dump_flags & TDF_DETAILS))
7224 fprintf (dump_file,
7225 "\nIgnoring predicate evaluation because "
7226 "it assumes that signed overflow is undefined");
7227 val = NULL_TREE;
7231 if (dump_file && (dump_flags & TDF_DETAILS))
7233 fprintf (dump_file, "\nPredicate evaluates to: ");
7234 if (val == NULL_TREE)
7235 fprintf (dump_file, "DON'T KNOW\n");
7236 else
7237 print_generic_stmt (dump_file, val, 0);
7240 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7243 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7244 that includes the value VAL. The search is restricted to the range
7245 [START_IDX, n - 1] where n is the size of VEC.
7247 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7248 returned.
7250 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7251 it is placed in IDX and false is returned.
7253 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7254 returned. */
7256 static bool
7257 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7259 size_t n = gimple_switch_num_labels (stmt);
7260 size_t low, high;
7262 /* Find case label for minimum of the value range or the next one.
7263 At each iteration we are searching in [low, high - 1]. */
7265 for (low = start_idx, high = n; high != low; )
7267 tree t;
7268 int cmp;
7269 /* Note that i != high, so we never ask for n. */
7270 size_t i = (high + low) / 2;
7271 t = gimple_switch_label (stmt, i);
7273 /* Cache the result of comparing CASE_LOW and val. */
7274 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7276 if (cmp == 0)
7278 /* Ranges cannot be empty. */
7279 *idx = i;
7280 return true;
7282 else if (cmp > 0)
7283 high = i;
7284 else
7286 low = i + 1;
7287 if (CASE_HIGH (t) != NULL
7288 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7290 *idx = i;
7291 return true;
7296 *idx = high;
7297 return false;
7300 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7301 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7302 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7303 then MAX_IDX < MIN_IDX.
7304 Returns true if the default label is not needed. */
7306 static bool
7307 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7308 size_t *max_idx)
7310 size_t i, j;
7311 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7312 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7314 if (i == j
7315 && min_take_default
7316 && max_take_default)
7318 /* Only the default case label reached.
7319 Return an empty range. */
7320 *min_idx = 1;
7321 *max_idx = 0;
7322 return false;
7324 else
7326 bool take_default = min_take_default || max_take_default;
7327 tree low, high;
7328 size_t k;
7330 if (max_take_default)
7331 j--;
7333 /* If the case label range is continuous, we do not need
7334 the default case label. Verify that. */
7335 high = CASE_LOW (gimple_switch_label (stmt, i));
7336 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7337 high = CASE_HIGH (gimple_switch_label (stmt, i));
7338 for (k = i + 1; k <= j; ++k)
7340 low = CASE_LOW (gimple_switch_label (stmt, k));
7341 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7343 take_default = true;
7344 break;
7346 high = low;
7347 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7348 high = CASE_HIGH (gimple_switch_label (stmt, k));
7351 *min_idx = i;
7352 *max_idx = j;
7353 return !take_default;
7357 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7358 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7359 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7360 Returns true if the default label is not needed. */
7362 static bool
7363 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7364 size_t *max_idx1, size_t *min_idx2,
7365 size_t *max_idx2)
7367 size_t i, j, k, l;
7368 unsigned int n = gimple_switch_num_labels (stmt);
7369 bool take_default;
7370 tree case_low, case_high;
7371 tree min = vr->min, max = vr->max;
7373 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7375 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7377 /* Set second range to emtpy. */
7378 *min_idx2 = 1;
7379 *max_idx2 = 0;
7381 if (vr->type == VR_RANGE)
7383 *min_idx1 = i;
7384 *max_idx1 = j;
7385 return !take_default;
7388 /* Set first range to all case labels. */
7389 *min_idx1 = 1;
7390 *max_idx1 = n - 1;
7392 if (i > j)
7393 return false;
7395 /* Make sure all the values of case labels [i , j] are contained in
7396 range [MIN, MAX]. */
7397 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7398 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7399 if (tree_int_cst_compare (case_low, min) < 0)
7400 i += 1;
7401 if (case_high != NULL_TREE
7402 && tree_int_cst_compare (max, case_high) < 0)
7403 j -= 1;
7405 if (i > j)
7406 return false;
7408 /* If the range spans case labels [i, j], the corresponding anti-range spans
7409 the labels [1, i - 1] and [j + 1, n - 1]. */
7410 k = j + 1;
7411 l = n - 1;
7412 if (k > l)
7414 k = 1;
7415 l = 0;
7418 j = i - 1;
7419 i = 1;
7420 if (i > j)
7422 i = k;
7423 j = l;
7424 k = 1;
7425 l = 0;
7428 *min_idx1 = i;
7429 *max_idx1 = j;
7430 *min_idx2 = k;
7431 *max_idx2 = l;
7432 return false;
7435 /* Visit switch statement STMT. If we can determine which edge
7436 will be taken out of STMT's basic block, record it in
7437 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7438 SSA_PROP_VARYING. */
7440 static enum ssa_prop_result
7441 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7443 tree op, val;
7444 value_range_t *vr;
7445 size_t i = 0, j = 0, k, l;
7446 bool take_default;
7448 *taken_edge_p = NULL;
7449 op = gimple_switch_index (stmt);
7450 if (TREE_CODE (op) != SSA_NAME)
7451 return SSA_PROP_VARYING;
7453 vr = get_value_range (op);
7454 if (dump_file && (dump_flags & TDF_DETAILS))
7456 fprintf (dump_file, "\nVisiting switch expression with operand ");
7457 print_generic_expr (dump_file, op, 0);
7458 fprintf (dump_file, " with known range ");
7459 dump_value_range (dump_file, vr);
7460 fprintf (dump_file, "\n");
7463 if ((vr->type != VR_RANGE
7464 && vr->type != VR_ANTI_RANGE)
7465 || symbolic_range_p (vr))
7466 return SSA_PROP_VARYING;
7468 /* Find the single edge that is taken from the switch expression. */
7469 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7471 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7472 label */
7473 if (j < i)
7475 gcc_assert (take_default);
7476 val = gimple_switch_default_label (stmt);
7478 else
7480 /* Check if labels with index i to j and maybe the default label
7481 are all reaching the same label. */
7483 val = gimple_switch_label (stmt, i);
7484 if (take_default
7485 && CASE_LABEL (gimple_switch_default_label (stmt))
7486 != CASE_LABEL (val))
7488 if (dump_file && (dump_flags & TDF_DETAILS))
7489 fprintf (dump_file, " not a single destination for this "
7490 "range\n");
7491 return SSA_PROP_VARYING;
7493 for (++i; i <= j; ++i)
7495 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7497 if (dump_file && (dump_flags & TDF_DETAILS))
7498 fprintf (dump_file, " not a single destination for this "
7499 "range\n");
7500 return SSA_PROP_VARYING;
7503 for (; k <= l; ++k)
7505 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7507 if (dump_file && (dump_flags & TDF_DETAILS))
7508 fprintf (dump_file, " not a single destination for this "
7509 "range\n");
7510 return SSA_PROP_VARYING;
7515 *taken_edge_p = find_edge (gimple_bb (stmt),
7516 label_to_block (CASE_LABEL (val)));
7518 if (dump_file && (dump_flags & TDF_DETAILS))
7520 fprintf (dump_file, " will take edge to ");
7521 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7524 return SSA_PROP_INTERESTING;
7528 /* Evaluate statement STMT. If the statement produces a useful range,
7529 return SSA_PROP_INTERESTING and record the SSA name with the
7530 interesting range into *OUTPUT_P.
7532 If STMT is a conditional branch and we can determine its truth
7533 value, the taken edge is recorded in *TAKEN_EDGE_P.
7535 If STMT produces a varying value, return SSA_PROP_VARYING. */
7537 static enum ssa_prop_result
7538 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7540 tree def;
7541 ssa_op_iter iter;
7543 if (dump_file && (dump_flags & TDF_DETAILS))
7545 fprintf (dump_file, "\nVisiting statement:\n");
7546 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7547 fprintf (dump_file, "\n");
7550 if (!stmt_interesting_for_vrp (stmt))
7551 gcc_assert (stmt_ends_bb_p (stmt));
7552 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7553 return vrp_visit_assignment_or_call (stmt, output_p);
7554 else if (gimple_code (stmt) == GIMPLE_COND)
7555 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7556 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7557 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7559 /* All other statements produce nothing of interest for VRP, so mark
7560 their outputs varying and prevent further simulation. */
7561 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7562 set_value_range_to_varying (get_value_range (def));
7564 return SSA_PROP_VARYING;
7567 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7568 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7569 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7570 possible such range. The resulting range is not canonicalized. */
7572 static void
7573 union_ranges (enum value_range_type *vr0type,
7574 tree *vr0min, tree *vr0max,
7575 enum value_range_type vr1type,
7576 tree vr1min, tree vr1max)
7578 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7579 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7581 /* [] is vr0, () is vr1 in the following classification comments. */
7582 if (mineq && maxeq)
7584 /* [( )] */
7585 if (*vr0type == vr1type)
7586 /* Nothing to do for equal ranges. */
7588 else if ((*vr0type == VR_RANGE
7589 && vr1type == VR_ANTI_RANGE)
7590 || (*vr0type == VR_ANTI_RANGE
7591 && vr1type == VR_RANGE))
7593 /* For anti-range with range union the result is varying. */
7594 goto give_up;
7596 else
7597 gcc_unreachable ();
7599 else if (operand_less_p (*vr0max, vr1min) == 1
7600 || operand_less_p (vr1max, *vr0min) == 1)
7602 /* [ ] ( ) or ( ) [ ]
7603 If the ranges have an empty intersection, result of the union
7604 operation is the anti-range or if both are anti-ranges
7605 it covers all. */
7606 if (*vr0type == VR_ANTI_RANGE
7607 && vr1type == VR_ANTI_RANGE)
7608 goto give_up;
7609 else if (*vr0type == VR_ANTI_RANGE
7610 && vr1type == VR_RANGE)
7612 else if (*vr0type == VR_RANGE
7613 && vr1type == VR_ANTI_RANGE)
7615 *vr0type = vr1type;
7616 *vr0min = vr1min;
7617 *vr0max = vr1max;
7619 else if (*vr0type == VR_RANGE
7620 && vr1type == VR_RANGE)
7622 /* The result is the convex hull of both ranges. */
7623 if (operand_less_p (*vr0max, vr1min) == 1)
7625 /* If the result can be an anti-range, create one. */
7626 if (TREE_CODE (*vr0max) == INTEGER_CST
7627 && TREE_CODE (vr1min) == INTEGER_CST
7628 && vrp_val_is_min (*vr0min)
7629 && vrp_val_is_max (vr1max))
7631 tree min = int_const_binop (PLUS_EXPR,
7632 *vr0max, integer_one_node);
7633 tree max = int_const_binop (MINUS_EXPR,
7634 vr1min, integer_one_node);
7635 if (!operand_less_p (max, min))
7637 *vr0type = VR_ANTI_RANGE;
7638 *vr0min = min;
7639 *vr0max = max;
7641 else
7642 *vr0max = vr1max;
7644 else
7645 *vr0max = vr1max;
7647 else
7649 /* If the result can be an anti-range, create one. */
7650 if (TREE_CODE (vr1max) == INTEGER_CST
7651 && TREE_CODE (*vr0min) == INTEGER_CST
7652 && vrp_val_is_min (vr1min)
7653 && vrp_val_is_max (*vr0max))
7655 tree min = int_const_binop (PLUS_EXPR,
7656 vr1max, integer_one_node);
7657 tree max = int_const_binop (MINUS_EXPR,
7658 *vr0min, integer_one_node);
7659 if (!operand_less_p (max, min))
7661 *vr0type = VR_ANTI_RANGE;
7662 *vr0min = min;
7663 *vr0max = max;
7665 else
7666 *vr0min = vr1min;
7668 else
7669 *vr0min = vr1min;
7672 else
7673 gcc_unreachable ();
7675 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7676 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7678 /* [ ( ) ] or [( ) ] or [ ( )] */
7679 if (*vr0type == VR_RANGE
7680 && vr1type == VR_RANGE)
7682 else if (*vr0type == VR_ANTI_RANGE
7683 && vr1type == VR_ANTI_RANGE)
7685 *vr0type = vr1type;
7686 *vr0min = vr1min;
7687 *vr0max = vr1max;
7689 else if (*vr0type == VR_ANTI_RANGE
7690 && vr1type == VR_RANGE)
7692 /* Arbitrarily choose the right or left gap. */
7693 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7694 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7695 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7696 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7697 else
7698 goto give_up;
7700 else if (*vr0type == VR_RANGE
7701 && vr1type == VR_ANTI_RANGE)
7702 /* The result covers everything. */
7703 goto give_up;
7704 else
7705 gcc_unreachable ();
7707 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7708 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7710 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7711 if (*vr0type == VR_RANGE
7712 && vr1type == VR_RANGE)
7714 *vr0type = vr1type;
7715 *vr0min = vr1min;
7716 *vr0max = vr1max;
7718 else if (*vr0type == VR_ANTI_RANGE
7719 && vr1type == VR_ANTI_RANGE)
7721 else if (*vr0type == VR_RANGE
7722 && vr1type == VR_ANTI_RANGE)
7724 *vr0type = VR_ANTI_RANGE;
7725 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7727 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7728 *vr0min = vr1min;
7730 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7732 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7733 *vr0max = vr1max;
7735 else
7736 goto give_up;
7738 else if (*vr0type == VR_ANTI_RANGE
7739 && vr1type == VR_RANGE)
7740 /* The result covers everything. */
7741 goto give_up;
7742 else
7743 gcc_unreachable ();
7745 else if ((operand_less_p (vr1min, *vr0max) == 1
7746 || operand_equal_p (vr1min, *vr0max, 0))
7747 && operand_less_p (*vr0min, vr1min) == 1)
7749 /* [ ( ] ) or [ ]( ) */
7750 if (*vr0type == VR_RANGE
7751 && vr1type == VR_RANGE)
7752 *vr0max = vr1max;
7753 else if (*vr0type == VR_ANTI_RANGE
7754 && vr1type == VR_ANTI_RANGE)
7755 *vr0min = vr1min;
7756 else if (*vr0type == VR_ANTI_RANGE
7757 && vr1type == VR_RANGE)
7759 if (TREE_CODE (vr1min) == INTEGER_CST)
7760 *vr0max = int_const_binop (MINUS_EXPR, vr1min, integer_one_node);
7761 else
7762 goto give_up;
7764 else if (*vr0type == VR_RANGE
7765 && vr1type == VR_ANTI_RANGE)
7767 if (TREE_CODE (*vr0max) == INTEGER_CST)
7769 *vr0type = vr1type;
7770 *vr0min = int_const_binop (PLUS_EXPR, *vr0max, integer_one_node);
7771 *vr0max = vr1max;
7773 else
7774 goto give_up;
7776 else
7777 gcc_unreachable ();
7779 else if ((operand_less_p (*vr0min, vr1max) == 1
7780 || operand_equal_p (*vr0min, vr1max, 0))
7781 && operand_less_p (vr1min, *vr0min) == 1)
7783 /* ( [ ) ] or ( )[ ] */
7784 if (*vr0type == VR_RANGE
7785 && vr1type == VR_RANGE)
7786 *vr0min = vr1min;
7787 else if (*vr0type == VR_ANTI_RANGE
7788 && vr1type == VR_ANTI_RANGE)
7789 *vr0max = vr1max;
7790 else if (*vr0type == VR_ANTI_RANGE
7791 && vr1type == VR_RANGE)
7793 if (TREE_CODE (vr1max) == INTEGER_CST)
7794 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7795 else
7796 goto give_up;
7798 else if (*vr0type == VR_RANGE
7799 && vr1type == VR_ANTI_RANGE)
7801 if (TREE_CODE (*vr0min) == INTEGER_CST)
7803 *vr0type = vr1type;
7804 *vr0min = vr1min;
7805 *vr0max = int_const_binop (MINUS_EXPR, *vr0min, integer_one_node);
7807 else
7808 goto give_up;
7810 else
7811 gcc_unreachable ();
7813 else
7814 goto give_up;
7816 return;
7818 give_up:
7819 *vr0type = VR_VARYING;
7820 *vr0min = NULL_TREE;
7821 *vr0max = NULL_TREE;
7824 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7825 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7826 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7827 possible such range. The resulting range is not canonicalized. */
7829 static void
7830 intersect_ranges (enum value_range_type *vr0type,
7831 tree *vr0min, tree *vr0max,
7832 enum value_range_type vr1type,
7833 tree vr1min, tree vr1max)
7835 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7836 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7838 /* [] is vr0, () is vr1 in the following classification comments. */
7839 if (mineq && maxeq)
7841 /* [( )] */
7842 if (*vr0type == vr1type)
7843 /* Nothing to do for equal ranges. */
7845 else if ((*vr0type == VR_RANGE
7846 && vr1type == VR_ANTI_RANGE)
7847 || (*vr0type == VR_ANTI_RANGE
7848 && vr1type == VR_RANGE))
7850 /* For anti-range with range intersection the result is empty. */
7851 *vr0type = VR_UNDEFINED;
7852 *vr0min = NULL_TREE;
7853 *vr0max = NULL_TREE;
7855 else
7856 gcc_unreachable ();
7858 else if (operand_less_p (*vr0max, vr1min) == 1
7859 || operand_less_p (vr1max, *vr0min) == 1)
7861 /* [ ] ( ) or ( ) [ ]
7862 If the ranges have an empty intersection, the result of the
7863 intersect operation is the range for intersecting an
7864 anti-range with a range or empty when intersecting two ranges. */
7865 if (*vr0type == VR_RANGE
7866 && vr1type == VR_ANTI_RANGE)
7868 else if (*vr0type == VR_ANTI_RANGE
7869 && vr1type == VR_RANGE)
7871 *vr0type = vr1type;
7872 *vr0min = vr1min;
7873 *vr0max = vr1max;
7875 else if (*vr0type == VR_RANGE
7876 && vr1type == VR_RANGE)
7878 *vr0type = VR_UNDEFINED;
7879 *vr0min = NULL_TREE;
7880 *vr0max = NULL_TREE;
7882 else if (*vr0type == VR_ANTI_RANGE
7883 && vr1type == VR_ANTI_RANGE)
7885 /* If the anti-ranges are adjacent to each other merge them. */
7886 if (TREE_CODE (*vr0max) == INTEGER_CST
7887 && TREE_CODE (vr1min) == INTEGER_CST
7888 && operand_less_p (*vr0max, vr1min) == 1
7889 && integer_onep (int_const_binop (MINUS_EXPR,
7890 vr1min, *vr0max)))
7891 *vr0max = vr1max;
7892 else if (TREE_CODE (vr1max) == INTEGER_CST
7893 && TREE_CODE (*vr0min) == INTEGER_CST
7894 && operand_less_p (vr1max, *vr0min) == 1
7895 && integer_onep (int_const_binop (MINUS_EXPR,
7896 *vr0min, vr1max)))
7897 *vr0min = vr1min;
7898 /* Else arbitrarily take VR0. */
7901 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7902 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7904 /* [ ( ) ] or [( ) ] or [ ( )] */
7905 if (*vr0type == VR_RANGE
7906 && vr1type == VR_RANGE)
7908 /* If both are ranges the result is the inner one. */
7909 *vr0type = vr1type;
7910 *vr0min = vr1min;
7911 *vr0max = vr1max;
7913 else if (*vr0type == VR_RANGE
7914 && vr1type == VR_ANTI_RANGE)
7916 /* Choose the right gap if the left one is empty. */
7917 if (mineq)
7919 if (TREE_CODE (vr1max) == INTEGER_CST)
7920 *vr0min = int_const_binop (PLUS_EXPR, vr1max, integer_one_node);
7921 else
7922 *vr0min = vr1max;
7924 /* Choose the left gap if the right one is empty. */
7925 else if (maxeq)
7927 if (TREE_CODE (vr1min) == INTEGER_CST)
7928 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7929 integer_one_node);
7930 else
7931 *vr0max = vr1min;
7933 /* Choose the anti-range if the range is effectively varying. */
7934 else if (vrp_val_is_min (*vr0min)
7935 && vrp_val_is_max (*vr0max))
7937 *vr0type = vr1type;
7938 *vr0min = vr1min;
7939 *vr0max = vr1max;
7941 /* Else choose the range. */
7943 else if (*vr0type == VR_ANTI_RANGE
7944 && vr1type == VR_ANTI_RANGE)
7945 /* If both are anti-ranges the result is the outer one. */
7947 else if (*vr0type == VR_ANTI_RANGE
7948 && vr1type == VR_RANGE)
7950 /* The intersection is empty. */
7951 *vr0type = VR_UNDEFINED;
7952 *vr0min = NULL_TREE;
7953 *vr0max = NULL_TREE;
7955 else
7956 gcc_unreachable ();
7958 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7959 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7961 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7962 if (*vr0type == VR_RANGE
7963 && vr1type == VR_RANGE)
7964 /* Choose the inner range. */
7966 else if (*vr0type == VR_ANTI_RANGE
7967 && vr1type == VR_RANGE)
7969 /* Choose the right gap if the left is empty. */
7970 if (mineq)
7972 *vr0type = VR_RANGE;
7973 if (TREE_CODE (*vr0max) == INTEGER_CST)
7974 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7975 integer_one_node);
7976 else
7977 *vr0min = *vr0max;
7978 *vr0max = vr1max;
7980 /* Choose the left gap if the right is empty. */
7981 else if (maxeq)
7983 *vr0type = VR_RANGE;
7984 if (TREE_CODE (*vr0min) == INTEGER_CST)
7985 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7986 integer_one_node);
7987 else
7988 *vr0max = *vr0min;
7989 *vr0min = vr1min;
7991 /* Choose the anti-range if the range is effectively varying. */
7992 else if (vrp_val_is_min (vr1min)
7993 && vrp_val_is_max (vr1max))
7995 /* Else choose the range. */
7996 else
7998 *vr0type = vr1type;
7999 *vr0min = vr1min;
8000 *vr0max = vr1max;
8003 else if (*vr0type == VR_ANTI_RANGE
8004 && vr1type == VR_ANTI_RANGE)
8006 /* If both are anti-ranges the result is the outer one. */
8007 *vr0type = vr1type;
8008 *vr0min = vr1min;
8009 *vr0max = vr1max;
8011 else if (vr1type == VR_ANTI_RANGE
8012 && *vr0type == VR_RANGE)
8014 /* The intersection is empty. */
8015 *vr0type = VR_UNDEFINED;
8016 *vr0min = NULL_TREE;
8017 *vr0max = NULL_TREE;
8019 else
8020 gcc_unreachable ();
8022 else if ((operand_less_p (vr1min, *vr0max) == 1
8023 || operand_equal_p (vr1min, *vr0max, 0))
8024 && operand_less_p (*vr0min, vr1min) == 1)
8026 /* [ ( ] ) or [ ]( ) */
8027 if (*vr0type == VR_ANTI_RANGE
8028 && vr1type == VR_ANTI_RANGE)
8029 *vr0max = vr1max;
8030 else if (*vr0type == VR_RANGE
8031 && vr1type == VR_RANGE)
8032 *vr0min = vr1min;
8033 else if (*vr0type == VR_RANGE
8034 && vr1type == VR_ANTI_RANGE)
8036 if (TREE_CODE (vr1min) == INTEGER_CST)
8037 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8038 integer_one_node);
8039 else
8040 *vr0max = vr1min;
8042 else if (*vr0type == VR_ANTI_RANGE
8043 && vr1type == VR_RANGE)
8045 *vr0type = VR_RANGE;
8046 if (TREE_CODE (*vr0max) == INTEGER_CST)
8047 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8048 integer_one_node);
8049 else
8050 *vr0min = *vr0max;
8051 *vr0max = vr1max;
8053 else
8054 gcc_unreachable ();
8056 else if ((operand_less_p (*vr0min, vr1max) == 1
8057 || operand_equal_p (*vr0min, vr1max, 0))
8058 && operand_less_p (vr1min, *vr0min) == 1)
8060 /* ( [ ) ] or ( )[ ] */
8061 if (*vr0type == VR_ANTI_RANGE
8062 && vr1type == VR_ANTI_RANGE)
8063 *vr0min = vr1min;
8064 else if (*vr0type == VR_RANGE
8065 && vr1type == VR_RANGE)
8066 *vr0max = vr1max;
8067 else if (*vr0type == VR_RANGE
8068 && vr1type == VR_ANTI_RANGE)
8070 if (TREE_CODE (vr1max) == INTEGER_CST)
8071 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8072 integer_one_node);
8073 else
8074 *vr0min = vr1max;
8076 else if (*vr0type == VR_ANTI_RANGE
8077 && vr1type == VR_RANGE)
8079 *vr0type = VR_RANGE;
8080 if (TREE_CODE (*vr0min) == INTEGER_CST)
8081 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8082 integer_one_node);
8083 else
8084 *vr0max = *vr0min;
8085 *vr0min = vr1min;
8087 else
8088 gcc_unreachable ();
8091 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8092 result for the intersection. That's always a conservative
8093 correct estimate. */
8095 return;
8099 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8100 in *VR0. This may not be the smallest possible such range. */
8102 static void
8103 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8105 value_range_t saved;
8107 /* If either range is VR_VARYING the other one wins. */
8108 if (vr1->type == VR_VARYING)
8109 return;
8110 if (vr0->type == VR_VARYING)
8112 copy_value_range (vr0, vr1);
8113 return;
8116 /* When either range is VR_UNDEFINED the resulting range is
8117 VR_UNDEFINED, too. */
8118 if (vr0->type == VR_UNDEFINED)
8119 return;
8120 if (vr1->type == VR_UNDEFINED)
8122 set_value_range_to_undefined (vr0);
8123 return;
8126 /* Save the original vr0 so we can return it as conservative intersection
8127 result when our worker turns things to varying. */
8128 saved = *vr0;
8129 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8130 vr1->type, vr1->min, vr1->max);
8131 /* Make sure to canonicalize the result though as the inversion of a
8132 VR_RANGE can still be a VR_RANGE. */
8133 set_and_canonicalize_value_range (vr0, vr0->type,
8134 vr0->min, vr0->max, vr0->equiv);
8135 /* If that failed, use the saved original VR0. */
8136 if (vr0->type == VR_VARYING)
8138 *vr0 = saved;
8139 return;
8141 /* If the result is VR_UNDEFINED there is no need to mess with
8142 the equivalencies. */
8143 if (vr0->type == VR_UNDEFINED)
8144 return;
8146 /* The resulting set of equivalences for range intersection is the union of
8147 the two sets. */
8148 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8149 bitmap_ior_into (vr0->equiv, vr1->equiv);
8150 else if (vr1->equiv && !vr0->equiv)
8151 bitmap_copy (vr0->equiv, vr1->equiv);
8154 static void
8155 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8157 if (dump_file && (dump_flags & TDF_DETAILS))
8159 fprintf (dump_file, "Intersecting\n ");
8160 dump_value_range (dump_file, vr0);
8161 fprintf (dump_file, "\nand\n ");
8162 dump_value_range (dump_file, vr1);
8163 fprintf (dump_file, "\n");
8165 vrp_intersect_ranges_1 (vr0, vr1);
8166 if (dump_file && (dump_flags & TDF_DETAILS))
8168 fprintf (dump_file, "to\n ");
8169 dump_value_range (dump_file, vr0);
8170 fprintf (dump_file, "\n");
8174 /* Meet operation for value ranges. Given two value ranges VR0 and
8175 VR1, store in VR0 a range that contains both VR0 and VR1. This
8176 may not be the smallest possible such range. */
8178 static void
8179 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8181 value_range_t saved;
8183 if (vr0->type == VR_UNDEFINED)
8185 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8186 return;
8189 if (vr1->type == VR_UNDEFINED)
8191 /* VR0 already has the resulting range. */
8192 return;
8195 if (vr0->type == VR_VARYING)
8197 /* Nothing to do. VR0 already has the resulting range. */
8198 return;
8201 if (vr1->type == VR_VARYING)
8203 set_value_range_to_varying (vr0);
8204 return;
8207 saved = *vr0;
8208 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8209 vr1->type, vr1->min, vr1->max);
8210 if (vr0->type == VR_VARYING)
8212 /* Failed to find an efficient meet. Before giving up and setting
8213 the result to VARYING, see if we can at least derive a useful
8214 anti-range. FIXME, all this nonsense about distinguishing
8215 anti-ranges from ranges is necessary because of the odd
8216 semantics of range_includes_zero_p and friends. */
8217 if (((saved.type == VR_RANGE
8218 && range_includes_zero_p (saved.min, saved.max) == 0)
8219 || (saved.type == VR_ANTI_RANGE
8220 && range_includes_zero_p (saved.min, saved.max) == 1))
8221 && ((vr1->type == VR_RANGE
8222 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8223 || (vr1->type == VR_ANTI_RANGE
8224 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8226 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8228 /* Since this meet operation did not result from the meeting of
8229 two equivalent names, VR0 cannot have any equivalences. */
8230 if (vr0->equiv)
8231 bitmap_clear (vr0->equiv);
8232 return;
8235 set_value_range_to_varying (vr0);
8236 return;
8238 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8239 vr0->equiv);
8240 if (vr0->type == VR_VARYING)
8241 return;
8243 /* The resulting set of equivalences is always the intersection of
8244 the two sets. */
8245 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8246 bitmap_and_into (vr0->equiv, vr1->equiv);
8247 else if (vr0->equiv && !vr1->equiv)
8248 bitmap_clear (vr0->equiv);
8251 static void
8252 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8254 if (dump_file && (dump_flags & TDF_DETAILS))
8256 fprintf (dump_file, "Meeting\n ");
8257 dump_value_range (dump_file, vr0);
8258 fprintf (dump_file, "\nand\n ");
8259 dump_value_range (dump_file, vr1);
8260 fprintf (dump_file, "\n");
8262 vrp_meet_1 (vr0, vr1);
8263 if (dump_file && (dump_flags & TDF_DETAILS))
8265 fprintf (dump_file, "to\n ");
8266 dump_value_range (dump_file, vr0);
8267 fprintf (dump_file, "\n");
8272 /* Visit all arguments for PHI node PHI that flow through executable
8273 edges. If a valid value range can be derived from all the incoming
8274 value ranges, set a new range for the LHS of PHI. */
8276 static enum ssa_prop_result
8277 vrp_visit_phi_node (gimple phi)
8279 size_t i;
8280 tree lhs = PHI_RESULT (phi);
8281 value_range_t *lhs_vr = get_value_range (lhs);
8282 value_range_t vr_result = VR_INITIALIZER;
8283 bool first = true;
8284 int edges, old_edges;
8285 struct loop *l;
8287 if (dump_file && (dump_flags & TDF_DETAILS))
8289 fprintf (dump_file, "\nVisiting PHI node: ");
8290 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8293 edges = 0;
8294 for (i = 0; i < gimple_phi_num_args (phi); i++)
8296 edge e = gimple_phi_arg_edge (phi, i);
8298 if (dump_file && (dump_flags & TDF_DETAILS))
8300 fprintf (dump_file,
8301 "\n Argument #%d (%d -> %d %sexecutable)\n",
8302 (int) i, e->src->index, e->dest->index,
8303 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8306 if (e->flags & EDGE_EXECUTABLE)
8308 tree arg = PHI_ARG_DEF (phi, i);
8309 value_range_t vr_arg;
8311 ++edges;
8313 if (TREE_CODE (arg) == SSA_NAME)
8315 vr_arg = *(get_value_range (arg));
8316 /* Do not allow equivalences or symbolic ranges to leak in from
8317 backedges. That creates invalid equivalencies.
8318 See PR53465 and PR54767. */
8319 if (e->flags & EDGE_DFS_BACK
8320 && (vr_arg.type == VR_RANGE
8321 || vr_arg.type == VR_ANTI_RANGE))
8323 vr_arg.equiv = NULL;
8324 if (symbolic_range_p (&vr_arg))
8326 vr_arg.type = VR_VARYING;
8327 vr_arg.min = NULL_TREE;
8328 vr_arg.max = NULL_TREE;
8332 else
8334 if (is_overflow_infinity (arg))
8335 arg = drop_tree_overflow (arg);
8337 vr_arg.type = VR_RANGE;
8338 vr_arg.min = arg;
8339 vr_arg.max = arg;
8340 vr_arg.equiv = NULL;
8343 if (dump_file && (dump_flags & TDF_DETAILS))
8345 fprintf (dump_file, "\t");
8346 print_generic_expr (dump_file, arg, dump_flags);
8347 fprintf (dump_file, "\n\tValue: ");
8348 dump_value_range (dump_file, &vr_arg);
8349 fprintf (dump_file, "\n");
8352 if (first)
8353 copy_value_range (&vr_result, &vr_arg);
8354 else
8355 vrp_meet (&vr_result, &vr_arg);
8356 first = false;
8358 if (vr_result.type == VR_VARYING)
8359 break;
8363 if (vr_result.type == VR_VARYING)
8364 goto varying;
8365 else if (vr_result.type == VR_UNDEFINED)
8366 goto update_range;
8368 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8369 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8371 /* To prevent infinite iterations in the algorithm, derive ranges
8372 when the new value is slightly bigger or smaller than the
8373 previous one. We don't do this if we have seen a new executable
8374 edge; this helps us avoid an overflow infinity for conditionals
8375 which are not in a loop. If the old value-range was VR_UNDEFINED
8376 use the updated range and iterate one more time. */
8377 if (edges > 0
8378 && gimple_phi_num_args (phi) > 1
8379 && edges == old_edges
8380 && lhs_vr->type != VR_UNDEFINED)
8382 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8383 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8385 /* For non VR_RANGE or for pointers fall back to varying if
8386 the range changed. */
8387 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8388 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8389 && (cmp_min != 0 || cmp_max != 0))
8390 goto varying;
8392 /* If the new minimum is smaller or larger than the previous
8393 one, go all the way to -INF. In the first case, to avoid
8394 iterating millions of times to reach -INF, and in the
8395 other case to avoid infinite bouncing between different
8396 minimums. */
8397 if (cmp_min > 0 || cmp_min < 0)
8399 if (!needs_overflow_infinity (TREE_TYPE (vr_result.min))
8400 || !vrp_var_may_overflow (lhs, phi))
8401 vr_result.min = TYPE_MIN_VALUE (TREE_TYPE (vr_result.min));
8402 else if (supports_overflow_infinity (TREE_TYPE (vr_result.min)))
8403 vr_result.min =
8404 negative_overflow_infinity (TREE_TYPE (vr_result.min));
8407 /* Similarly, if the new maximum is smaller or larger than
8408 the previous one, go all the way to +INF. */
8409 if (cmp_max < 0 || cmp_max > 0)
8411 if (!needs_overflow_infinity (TREE_TYPE (vr_result.max))
8412 || !vrp_var_may_overflow (lhs, phi))
8413 vr_result.max = TYPE_MAX_VALUE (TREE_TYPE (vr_result.max));
8414 else if (supports_overflow_infinity (TREE_TYPE (vr_result.max)))
8415 vr_result.max =
8416 positive_overflow_infinity (TREE_TYPE (vr_result.max));
8419 /* If we dropped either bound to +-INF then if this is a loop
8420 PHI node SCEV may known more about its value-range. */
8421 if ((cmp_min > 0 || cmp_min < 0
8422 || cmp_max < 0 || cmp_max > 0)
8423 && current_loops
8424 && (l = loop_containing_stmt (phi))
8425 && l->header == gimple_bb (phi))
8426 adjust_range_with_scev (&vr_result, l, phi, lhs);
8428 /* If we will end up with a (-INF, +INF) range, set it to
8429 VARYING. Same if the previous max value was invalid for
8430 the type and we end up with vr_result.min > vr_result.max. */
8431 if ((vrp_val_is_max (vr_result.max)
8432 && vrp_val_is_min (vr_result.min))
8433 || compare_values (vr_result.min,
8434 vr_result.max) > 0)
8435 goto varying;
8438 /* If the new range is different than the previous value, keep
8439 iterating. */
8440 update_range:
8441 if (update_value_range (lhs, &vr_result))
8443 if (dump_file && (dump_flags & TDF_DETAILS))
8445 fprintf (dump_file, "Found new range for ");
8446 print_generic_expr (dump_file, lhs, 0);
8447 fprintf (dump_file, ": ");
8448 dump_value_range (dump_file, &vr_result);
8449 fprintf (dump_file, "\n\n");
8452 return SSA_PROP_INTERESTING;
8455 /* Nothing changed, don't add outgoing edges. */
8456 return SSA_PROP_NOT_INTERESTING;
8458 /* No match found. Set the LHS to VARYING. */
8459 varying:
8460 set_value_range_to_varying (lhs_vr);
8461 return SSA_PROP_VARYING;
8464 /* Simplify boolean operations if the source is known
8465 to be already a boolean. */
8466 static bool
8467 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8469 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8470 tree lhs, op0, op1;
8471 bool need_conversion;
8473 /* We handle only !=/== case here. */
8474 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8476 op0 = gimple_assign_rhs1 (stmt);
8477 if (!op_with_boolean_value_range_p (op0))
8478 return false;
8480 op1 = gimple_assign_rhs2 (stmt);
8481 if (!op_with_boolean_value_range_p (op1))
8482 return false;
8484 /* Reduce number of cases to handle to NE_EXPR. As there is no
8485 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8486 if (rhs_code == EQ_EXPR)
8488 if (TREE_CODE (op1) == INTEGER_CST)
8489 op1 = int_const_binop (BIT_XOR_EXPR, op1, integer_one_node);
8490 else
8491 return false;
8494 lhs = gimple_assign_lhs (stmt);
8495 need_conversion
8496 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8498 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8499 if (need_conversion
8500 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8501 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8502 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8503 return false;
8505 /* For A != 0 we can substitute A itself. */
8506 if (integer_zerop (op1))
8507 gimple_assign_set_rhs_with_ops (gsi,
8508 need_conversion
8509 ? NOP_EXPR : TREE_CODE (op0),
8510 op0, NULL_TREE);
8511 /* For A != B we substitute A ^ B. Either with conversion. */
8512 else if (need_conversion)
8514 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8515 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8516 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8517 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8519 /* Or without. */
8520 else
8521 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8522 update_stmt (gsi_stmt (*gsi));
8524 return true;
8527 /* Simplify a division or modulo operator to a right shift or
8528 bitwise and if the first operand is unsigned or is greater
8529 than zero and the second operand is an exact power of two. */
8531 static bool
8532 simplify_div_or_mod_using_ranges (gimple stmt)
8534 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8535 tree val = NULL;
8536 tree op0 = gimple_assign_rhs1 (stmt);
8537 tree op1 = gimple_assign_rhs2 (stmt);
8538 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8540 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8542 val = integer_one_node;
8544 else
8546 bool sop = false;
8548 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8550 if (val
8551 && sop
8552 && integer_onep (val)
8553 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8555 location_t location;
8557 if (!gimple_has_location (stmt))
8558 location = input_location;
8559 else
8560 location = gimple_location (stmt);
8561 warning_at (location, OPT_Wstrict_overflow,
8562 "assuming signed overflow does not occur when "
8563 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8567 if (val && integer_onep (val))
8569 tree t;
8571 if (rhs_code == TRUNC_DIV_EXPR)
8573 t = build_int_cst (integer_type_node, tree_log2 (op1));
8574 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8575 gimple_assign_set_rhs1 (stmt, op0);
8576 gimple_assign_set_rhs2 (stmt, t);
8578 else
8580 t = build_int_cst (TREE_TYPE (op1), 1);
8581 t = int_const_binop (MINUS_EXPR, op1, t);
8582 t = fold_convert (TREE_TYPE (op0), t);
8584 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8585 gimple_assign_set_rhs1 (stmt, op0);
8586 gimple_assign_set_rhs2 (stmt, t);
8589 update_stmt (stmt);
8590 return true;
8593 return false;
8596 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8597 ABS_EXPR. If the operand is <= 0, then simplify the
8598 ABS_EXPR into a NEGATE_EXPR. */
8600 static bool
8601 simplify_abs_using_ranges (gimple stmt)
8603 tree val = NULL;
8604 tree op = gimple_assign_rhs1 (stmt);
8605 tree type = TREE_TYPE (op);
8606 value_range_t *vr = get_value_range (op);
8608 if (TYPE_UNSIGNED (type))
8610 val = integer_zero_node;
8612 else if (vr)
8614 bool sop = false;
8616 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8617 if (!val)
8619 sop = false;
8620 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8621 &sop);
8623 if (val)
8625 if (integer_zerop (val))
8626 val = integer_one_node;
8627 else if (integer_onep (val))
8628 val = integer_zero_node;
8632 if (val
8633 && (integer_onep (val) || integer_zerop (val)))
8635 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8637 location_t location;
8639 if (!gimple_has_location (stmt))
8640 location = input_location;
8641 else
8642 location = gimple_location (stmt);
8643 warning_at (location, OPT_Wstrict_overflow,
8644 "assuming signed overflow does not occur when "
8645 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8648 gimple_assign_set_rhs1 (stmt, op);
8649 if (integer_onep (val))
8650 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8651 else
8652 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8653 update_stmt (stmt);
8654 return true;
8658 return false;
8661 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8662 If all the bits that are being cleared by & are already
8663 known to be zero from VR, or all the bits that are being
8664 set by | are already known to be one from VR, the bit
8665 operation is redundant. */
8667 static bool
8668 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8670 tree op0 = gimple_assign_rhs1 (stmt);
8671 tree op1 = gimple_assign_rhs2 (stmt);
8672 tree op = NULL_TREE;
8673 value_range_t vr0 = VR_INITIALIZER;
8674 value_range_t vr1 = VR_INITIALIZER;
8675 double_int may_be_nonzero0, may_be_nonzero1;
8676 double_int must_be_nonzero0, must_be_nonzero1;
8677 double_int mask;
8679 if (TREE_CODE (op0) == SSA_NAME)
8680 vr0 = *(get_value_range (op0));
8681 else if (is_gimple_min_invariant (op0))
8682 set_value_range_to_value (&vr0, op0, NULL);
8683 else
8684 return false;
8686 if (TREE_CODE (op1) == SSA_NAME)
8687 vr1 = *(get_value_range (op1));
8688 else if (is_gimple_min_invariant (op1))
8689 set_value_range_to_value (&vr1, op1, NULL);
8690 else
8691 return false;
8693 if (!zero_nonzero_bits_from_vr (&vr0, &may_be_nonzero0, &must_be_nonzero0))
8694 return false;
8695 if (!zero_nonzero_bits_from_vr (&vr1, &may_be_nonzero1, &must_be_nonzero1))
8696 return false;
8698 switch (gimple_assign_rhs_code (stmt))
8700 case BIT_AND_EXPR:
8701 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8702 if (mask.is_zero ())
8704 op = op0;
8705 break;
8707 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8708 if (mask.is_zero ())
8710 op = op1;
8711 break;
8713 break;
8714 case BIT_IOR_EXPR:
8715 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8716 if (mask.is_zero ())
8718 op = op1;
8719 break;
8721 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8722 if (mask.is_zero ())
8724 op = op0;
8725 break;
8727 break;
8728 default:
8729 gcc_unreachable ();
8732 if (op == NULL_TREE)
8733 return false;
8735 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8736 update_stmt (gsi_stmt (*gsi));
8737 return true;
8740 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8741 a known value range VR.
8743 If there is one and only one value which will satisfy the
8744 conditional, then return that value. Else return NULL. */
8746 static tree
8747 test_for_singularity (enum tree_code cond_code, tree op0,
8748 tree op1, value_range_t *vr)
8750 tree min = NULL;
8751 tree max = NULL;
8753 /* Extract minimum/maximum values which satisfy the
8754 the conditional as it was written. */
8755 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8757 /* This should not be negative infinity; there is no overflow
8758 here. */
8759 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8761 max = op1;
8762 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8764 tree one = build_int_cst (TREE_TYPE (op0), 1);
8765 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8766 if (EXPR_P (max))
8767 TREE_NO_WARNING (max) = 1;
8770 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8772 /* This should not be positive infinity; there is no overflow
8773 here. */
8774 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8776 min = op1;
8777 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8779 tree one = build_int_cst (TREE_TYPE (op0), 1);
8780 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8781 if (EXPR_P (min))
8782 TREE_NO_WARNING (min) = 1;
8786 /* Now refine the minimum and maximum values using any
8787 value range information we have for op0. */
8788 if (min && max)
8790 if (compare_values (vr->min, min) == 1)
8791 min = vr->min;
8792 if (compare_values (vr->max, max) == -1)
8793 max = vr->max;
8795 /* If the new min/max values have converged to a single value,
8796 then there is only one value which can satisfy the condition,
8797 return that value. */
8798 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8799 return min;
8801 return NULL;
8804 /* Return whether the value range *VR fits in an integer type specified
8805 by PRECISION and UNSIGNED_P. */
8807 static bool
8808 range_fits_type_p (value_range_t *vr, unsigned precision, bool unsigned_p)
8810 tree src_type;
8811 unsigned src_precision;
8812 double_int tem;
8814 /* We can only handle integral and pointer types. */
8815 src_type = TREE_TYPE (vr->min);
8816 if (!INTEGRAL_TYPE_P (src_type)
8817 && !POINTER_TYPE_P (src_type))
8818 return false;
8820 /* An extension is fine unless VR is signed and unsigned_p,
8821 and so is an identity transform. */
8822 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8823 if ((src_precision < precision
8824 && !(unsigned_p && !TYPE_UNSIGNED (src_type)))
8825 || (src_precision == precision
8826 && TYPE_UNSIGNED (src_type) == unsigned_p))
8827 return true;
8829 /* Now we can only handle ranges with constant bounds. */
8830 if (vr->type != VR_RANGE
8831 || TREE_CODE (vr->min) != INTEGER_CST
8832 || TREE_CODE (vr->max) != INTEGER_CST)
8833 return false;
8835 /* For sign changes, the MSB of the double_int has to be clear.
8836 An unsigned value with its MSB set cannot be represented by
8837 a signed double_int, while a negative value cannot be represented
8838 by an unsigned double_int. */
8839 if (TYPE_UNSIGNED (src_type) != unsigned_p
8840 && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
8841 return false;
8843 /* Then we can perform the conversion on both ends and compare
8844 the result for equality. */
8845 tem = tree_to_double_int (vr->min).ext (precision, unsigned_p);
8846 if (tree_to_double_int (vr->min) != tem)
8847 return false;
8848 tem = tree_to_double_int (vr->max).ext (precision, unsigned_p);
8849 if (tree_to_double_int (vr->max) != tem)
8850 return false;
8852 return true;
8855 /* Simplify a conditional using a relational operator to an equality
8856 test if the range information indicates only one value can satisfy
8857 the original conditional. */
8859 static bool
8860 simplify_cond_using_ranges (gimple stmt)
8862 tree op0 = gimple_cond_lhs (stmt);
8863 tree op1 = gimple_cond_rhs (stmt);
8864 enum tree_code cond_code = gimple_cond_code (stmt);
8866 if (cond_code != NE_EXPR
8867 && cond_code != EQ_EXPR
8868 && TREE_CODE (op0) == SSA_NAME
8869 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8870 && is_gimple_min_invariant (op1))
8872 value_range_t *vr = get_value_range (op0);
8874 /* If we have range information for OP0, then we might be
8875 able to simplify this conditional. */
8876 if (vr->type == VR_RANGE)
8878 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
8880 if (new_tree)
8882 if (dump_file)
8884 fprintf (dump_file, "Simplified relational ");
8885 print_gimple_stmt (dump_file, stmt, 0, 0);
8886 fprintf (dump_file, " into ");
8889 gimple_cond_set_code (stmt, EQ_EXPR);
8890 gimple_cond_set_lhs (stmt, op0);
8891 gimple_cond_set_rhs (stmt, new_tree);
8893 update_stmt (stmt);
8895 if (dump_file)
8897 print_gimple_stmt (dump_file, stmt, 0, 0);
8898 fprintf (dump_file, "\n");
8901 return true;
8904 /* Try again after inverting the condition. We only deal
8905 with integral types here, so no need to worry about
8906 issues with inverting FP comparisons. */
8907 cond_code = invert_tree_comparison (cond_code, false);
8908 new_tree = test_for_singularity (cond_code, op0, op1, vr);
8910 if (new_tree)
8912 if (dump_file)
8914 fprintf (dump_file, "Simplified relational ");
8915 print_gimple_stmt (dump_file, stmt, 0, 0);
8916 fprintf (dump_file, " into ");
8919 gimple_cond_set_code (stmt, NE_EXPR);
8920 gimple_cond_set_lhs (stmt, op0);
8921 gimple_cond_set_rhs (stmt, new_tree);
8923 update_stmt (stmt);
8925 if (dump_file)
8927 print_gimple_stmt (dump_file, stmt, 0, 0);
8928 fprintf (dump_file, "\n");
8931 return true;
8936 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8937 see if OP0 was set by a type conversion where the source of
8938 the conversion is another SSA_NAME with a range that fits
8939 into the range of OP0's type.
8941 If so, the conversion is redundant as the earlier SSA_NAME can be
8942 used for the comparison directly if we just massage the constant in the
8943 comparison. */
8944 if (TREE_CODE (op0) == SSA_NAME
8945 && TREE_CODE (op1) == INTEGER_CST)
8947 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
8948 tree innerop;
8950 if (!is_gimple_assign (def_stmt)
8951 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
8952 return false;
8954 innerop = gimple_assign_rhs1 (def_stmt);
8956 if (TREE_CODE (innerop) == SSA_NAME
8957 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
8959 value_range_t *vr = get_value_range (innerop);
8961 if (range_int_cst_p (vr)
8962 && range_fits_type_p (vr,
8963 TYPE_PRECISION (TREE_TYPE (op0)),
8964 TYPE_UNSIGNED (TREE_TYPE (op0)))
8965 && int_fits_type_p (op1, TREE_TYPE (innerop))
8966 /* The range must not have overflowed, or if it did overflow
8967 we must not be wrapping/trapping overflow and optimizing
8968 with strict overflow semantics. */
8969 && ((!is_negative_overflow_infinity (vr->min)
8970 && !is_positive_overflow_infinity (vr->max))
8971 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
8973 /* If the range overflowed and the user has asked for warnings
8974 when strict overflow semantics were used to optimize code,
8975 issue an appropriate warning. */
8976 if ((is_negative_overflow_infinity (vr->min)
8977 || is_positive_overflow_infinity (vr->max))
8978 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
8980 location_t location;
8982 if (!gimple_has_location (stmt))
8983 location = input_location;
8984 else
8985 location = gimple_location (stmt);
8986 warning_at (location, OPT_Wstrict_overflow,
8987 "assuming signed overflow does not occur when "
8988 "simplifying conditional");
8991 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
8992 gimple_cond_set_lhs (stmt, innerop);
8993 gimple_cond_set_rhs (stmt, newconst);
8994 return true;
8999 return false;
9002 /* Simplify a switch statement using the value range of the switch
9003 argument. */
9005 static bool
9006 simplify_switch_using_ranges (gimple stmt)
9008 tree op = gimple_switch_index (stmt);
9009 value_range_t *vr;
9010 bool take_default;
9011 edge e;
9012 edge_iterator ei;
9013 size_t i = 0, j = 0, n, n2;
9014 tree vec2;
9015 switch_update su;
9016 size_t k = 1, l = 0;
9018 if (TREE_CODE (op) == SSA_NAME)
9020 vr = get_value_range (op);
9022 /* We can only handle integer ranges. */
9023 if ((vr->type != VR_RANGE
9024 && vr->type != VR_ANTI_RANGE)
9025 || symbolic_range_p (vr))
9026 return false;
9028 /* Find case label for min/max of the value range. */
9029 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9031 else if (TREE_CODE (op) == INTEGER_CST)
9033 take_default = !find_case_label_index (stmt, 1, op, &i);
9034 if (take_default)
9036 i = 1;
9037 j = 0;
9039 else
9041 j = i;
9044 else
9045 return false;
9047 n = gimple_switch_num_labels (stmt);
9049 /* Bail out if this is just all edges taken. */
9050 if (i == 1
9051 && j == n - 1
9052 && take_default)
9053 return false;
9055 /* Build a new vector of taken case labels. */
9056 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9057 n2 = 0;
9059 /* Add the default edge, if necessary. */
9060 if (take_default)
9061 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9063 for (; i <= j; ++i, ++n2)
9064 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9066 for (; k <= l; ++k, ++n2)
9067 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9069 /* Mark needed edges. */
9070 for (i = 0; i < n2; ++i)
9072 e = find_edge (gimple_bb (stmt),
9073 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9074 e->aux = (void *)-1;
9077 /* Queue not needed edges for later removal. */
9078 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9080 if (e->aux == (void *)-1)
9082 e->aux = NULL;
9083 continue;
9086 if (dump_file && (dump_flags & TDF_DETAILS))
9088 fprintf (dump_file, "removing unreachable case label\n");
9090 to_remove_edges.safe_push (e);
9091 e->flags &= ~EDGE_EXECUTABLE;
9094 /* And queue an update for the stmt. */
9095 su.stmt = stmt;
9096 su.vec = vec2;
9097 to_update_switch_stmts.safe_push (su);
9098 return false;
9101 /* Simplify an integral conversion from an SSA name in STMT. */
9103 static bool
9104 simplify_conversion_using_ranges (gimple stmt)
9106 tree innerop, middleop, finaltype;
9107 gimple def_stmt;
9108 value_range_t *innervr;
9109 bool inner_unsigned_p, middle_unsigned_p, final_unsigned_p;
9110 unsigned inner_prec, middle_prec, final_prec;
9111 double_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9113 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9114 if (!INTEGRAL_TYPE_P (finaltype))
9115 return false;
9116 middleop = gimple_assign_rhs1 (stmt);
9117 def_stmt = SSA_NAME_DEF_STMT (middleop);
9118 if (!is_gimple_assign (def_stmt)
9119 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9120 return false;
9121 innerop = gimple_assign_rhs1 (def_stmt);
9122 if (TREE_CODE (innerop) != SSA_NAME
9123 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9124 return false;
9126 /* Get the value-range of the inner operand. */
9127 innervr = get_value_range (innerop);
9128 if (innervr->type != VR_RANGE
9129 || TREE_CODE (innervr->min) != INTEGER_CST
9130 || TREE_CODE (innervr->max) != INTEGER_CST)
9131 return false;
9133 /* Simulate the conversion chain to check if the result is equal if
9134 the middle conversion is removed. */
9135 innermin = tree_to_double_int (innervr->min);
9136 innermax = tree_to_double_int (innervr->max);
9138 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9139 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9140 final_prec = TYPE_PRECISION (finaltype);
9142 /* If the first conversion is not injective, the second must not
9143 be widening. */
9144 if ((innermax - innermin).ugt (double_int::mask (middle_prec))
9145 && middle_prec < final_prec)
9146 return false;
9147 /* We also want a medium value so that we can track the effect that
9148 narrowing conversions with sign change have. */
9149 inner_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (innerop));
9150 if (inner_unsigned_p)
9151 innermed = double_int::mask (inner_prec).lrshift (1, inner_prec);
9152 else
9153 innermed = double_int_zero;
9154 if (innermin.cmp (innermed, inner_unsigned_p) >= 0
9155 || innermed.cmp (innermax, inner_unsigned_p) >= 0)
9156 innermed = innermin;
9158 middle_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (middleop));
9159 middlemin = innermin.ext (middle_prec, middle_unsigned_p);
9160 middlemed = innermed.ext (middle_prec, middle_unsigned_p);
9161 middlemax = innermax.ext (middle_prec, middle_unsigned_p);
9163 /* Require that the final conversion applied to both the original
9164 and the intermediate range produces the same result. */
9165 final_unsigned_p = TYPE_UNSIGNED (finaltype);
9166 if (middlemin.ext (final_prec, final_unsigned_p)
9167 != innermin.ext (final_prec, final_unsigned_p)
9168 || middlemed.ext (final_prec, final_unsigned_p)
9169 != innermed.ext (final_prec, final_unsigned_p)
9170 || middlemax.ext (final_prec, final_unsigned_p)
9171 != innermax.ext (final_prec, final_unsigned_p))
9172 return false;
9174 gimple_assign_set_rhs1 (stmt, innerop);
9175 update_stmt (stmt);
9176 return true;
9179 /* Simplify a conversion from integral SSA name to float in STMT. */
9181 static bool
9182 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9184 tree rhs1 = gimple_assign_rhs1 (stmt);
9185 value_range_t *vr = get_value_range (rhs1);
9186 enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9187 enum machine_mode mode;
9188 tree tem;
9189 gimple conv;
9191 /* We can only handle constant ranges. */
9192 if (vr->type != VR_RANGE
9193 || TREE_CODE (vr->min) != INTEGER_CST
9194 || TREE_CODE (vr->max) != INTEGER_CST)
9195 return false;
9197 /* First check if we can use a signed type in place of an unsigned. */
9198 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9199 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9200 != CODE_FOR_nothing)
9201 && range_fits_type_p (vr, GET_MODE_PRECISION
9202 (TYPE_MODE (TREE_TYPE (rhs1))), 0))
9203 mode = TYPE_MODE (TREE_TYPE (rhs1));
9204 /* If we can do the conversion in the current input mode do nothing. */
9205 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9206 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9207 return false;
9208 /* Otherwise search for a mode we can use, starting from the narrowest
9209 integer mode available. */
9210 else
9212 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9215 /* If we cannot do a signed conversion to float from mode
9216 or if the value-range does not fit in the signed type
9217 try with a wider mode. */
9218 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9219 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), 0))
9220 break;
9222 mode = GET_MODE_WIDER_MODE (mode);
9223 /* But do not widen the input. Instead leave that to the
9224 optabs expansion code. */
9225 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9226 return false;
9228 while (mode != VOIDmode);
9229 if (mode == VOIDmode)
9230 return false;
9233 /* It works, insert a truncation or sign-change before the
9234 float conversion. */
9235 tem = make_ssa_name (build_nonstandard_integer_type
9236 (GET_MODE_PRECISION (mode), 0), NULL);
9237 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9238 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9239 gimple_assign_set_rhs1 (stmt, tem);
9240 update_stmt (stmt);
9242 return true;
9245 /* Simplify STMT using ranges if possible. */
9247 static bool
9248 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9250 gimple stmt = gsi_stmt (*gsi);
9251 if (is_gimple_assign (stmt))
9253 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9254 tree rhs1 = gimple_assign_rhs1 (stmt);
9256 switch (rhs_code)
9258 case EQ_EXPR:
9259 case NE_EXPR:
9260 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9261 if the RHS is zero or one, and the LHS are known to be boolean
9262 values. */
9263 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9264 return simplify_truth_ops_using_ranges (gsi, stmt);
9265 break;
9267 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9268 and BIT_AND_EXPR respectively if the first operand is greater
9269 than zero and the second operand is an exact power of two. */
9270 case TRUNC_DIV_EXPR:
9271 case TRUNC_MOD_EXPR:
9272 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9273 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9274 return simplify_div_or_mod_using_ranges (stmt);
9275 break;
9277 /* Transform ABS (X) into X or -X as appropriate. */
9278 case ABS_EXPR:
9279 if (TREE_CODE (rhs1) == SSA_NAME
9280 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9281 return simplify_abs_using_ranges (stmt);
9282 break;
9284 case BIT_AND_EXPR:
9285 case BIT_IOR_EXPR:
9286 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9287 if all the bits being cleared are already cleared or
9288 all the bits being set are already set. */
9289 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9290 return simplify_bit_ops_using_ranges (gsi, stmt);
9291 break;
9293 CASE_CONVERT:
9294 if (TREE_CODE (rhs1) == SSA_NAME
9295 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9296 return simplify_conversion_using_ranges (stmt);
9297 break;
9299 case FLOAT_EXPR:
9300 if (TREE_CODE (rhs1) == SSA_NAME
9301 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9302 return simplify_float_conversion_using_ranges (gsi, stmt);
9303 break;
9305 default:
9306 break;
9309 else if (gimple_code (stmt) == GIMPLE_COND)
9310 return simplify_cond_using_ranges (stmt);
9311 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9312 return simplify_switch_using_ranges (stmt);
9314 return false;
9317 /* If the statement pointed by SI has a predicate whose value can be
9318 computed using the value range information computed by VRP, compute
9319 its value and return true. Otherwise, return false. */
9321 static bool
9322 fold_predicate_in (gimple_stmt_iterator *si)
9324 bool assignment_p = false;
9325 tree val;
9326 gimple stmt = gsi_stmt (*si);
9328 if (is_gimple_assign (stmt)
9329 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9331 assignment_p = true;
9332 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9333 gimple_assign_rhs1 (stmt),
9334 gimple_assign_rhs2 (stmt),
9335 stmt);
9337 else if (gimple_code (stmt) == GIMPLE_COND)
9338 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9339 gimple_cond_lhs (stmt),
9340 gimple_cond_rhs (stmt),
9341 stmt);
9342 else
9343 return false;
9345 if (val)
9347 if (assignment_p)
9348 val = fold_convert (gimple_expr_type (stmt), val);
9350 if (dump_file)
9352 fprintf (dump_file, "Folding predicate ");
9353 print_gimple_expr (dump_file, stmt, 0, 0);
9354 fprintf (dump_file, " to ");
9355 print_generic_expr (dump_file, val, 0);
9356 fprintf (dump_file, "\n");
9359 if (is_gimple_assign (stmt))
9360 gimple_assign_set_rhs_from_tree (si, val);
9361 else
9363 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9364 if (integer_zerop (val))
9365 gimple_cond_make_false (stmt);
9366 else if (integer_onep (val))
9367 gimple_cond_make_true (stmt);
9368 else
9369 gcc_unreachable ();
9372 return true;
9375 return false;
9378 /* Callback for substitute_and_fold folding the stmt at *SI. */
9380 static bool
9381 vrp_fold_stmt (gimple_stmt_iterator *si)
9383 if (fold_predicate_in (si))
9384 return true;
9386 return simplify_stmt_using_ranges (si);
9389 /* Stack of dest,src equivalency pairs that need to be restored after
9390 each attempt to thread a block's incoming edge to an outgoing edge.
9392 A NULL entry is used to mark the end of pairs which need to be
9393 restored. */
9394 static vec<tree> equiv_stack;
9396 /* A trivial wrapper so that we can present the generic jump threading
9397 code with a simple API for simplifying statements. STMT is the
9398 statement we want to simplify, WITHIN_STMT provides the location
9399 for any overflow warnings. */
9401 static tree
9402 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9404 if (gimple_code (stmt) == GIMPLE_COND)
9405 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9406 gimple_cond_lhs (stmt),
9407 gimple_cond_rhs (stmt), within_stmt);
9409 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9411 value_range_t new_vr = VR_INITIALIZER;
9412 tree lhs = gimple_assign_lhs (stmt);
9414 if (TREE_CODE (lhs) == SSA_NAME
9415 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9416 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9418 extract_range_from_assignment (&new_vr, stmt);
9419 if (range_int_cst_singleton_p (&new_vr))
9420 return new_vr.min;
9424 return NULL_TREE;
9427 /* Blocks which have more than one predecessor and more than
9428 one successor present jump threading opportunities, i.e.,
9429 when the block is reached from a specific predecessor, we
9430 may be able to determine which of the outgoing edges will
9431 be traversed. When this optimization applies, we are able
9432 to avoid conditionals at runtime and we may expose secondary
9433 optimization opportunities.
9435 This routine is effectively a driver for the generic jump
9436 threading code. It basically just presents the generic code
9437 with edges that may be suitable for jump threading.
9439 Unlike DOM, we do not iterate VRP if jump threading was successful.
9440 While iterating may expose new opportunities for VRP, it is expected
9441 those opportunities would be very limited and the compile time cost
9442 to expose those opportunities would be significant.
9444 As jump threading opportunities are discovered, they are registered
9445 for later realization. */
9447 static void
9448 identify_jump_threads (void)
9450 basic_block bb;
9451 gimple dummy;
9452 int i;
9453 edge e;
9455 /* Ugh. When substituting values earlier in this pass we can
9456 wipe the dominance information. So rebuild the dominator
9457 information as we need it within the jump threading code. */
9458 calculate_dominance_info (CDI_DOMINATORS);
9460 /* We do not allow VRP information to be used for jump threading
9461 across a back edge in the CFG. Otherwise it becomes too
9462 difficult to avoid eliminating loop exit tests. Of course
9463 EDGE_DFS_BACK is not accurate at this time so we have to
9464 recompute it. */
9465 mark_dfs_back_edges ();
9467 /* Do not thread across edges we are about to remove. Just marking
9468 them as EDGE_DFS_BACK will do. */
9469 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9470 e->flags |= EDGE_DFS_BACK;
9472 /* Allocate our unwinder stack to unwind any temporary equivalences
9473 that might be recorded. */
9474 equiv_stack.create (20);
9476 /* To avoid lots of silly node creation, we create a single
9477 conditional and just modify it in-place when attempting to
9478 thread jumps. */
9479 dummy = gimple_build_cond (EQ_EXPR,
9480 integer_zero_node, integer_zero_node,
9481 NULL, NULL);
9483 /* Walk through all the blocks finding those which present a
9484 potential jump threading opportunity. We could set this up
9485 as a dominator walker and record data during the walk, but
9486 I doubt it's worth the effort for the classes of jump
9487 threading opportunities we are trying to identify at this
9488 point in compilation. */
9489 FOR_EACH_BB (bb)
9491 gimple last;
9493 /* If the generic jump threading code does not find this block
9494 interesting, then there is nothing to do. */
9495 if (! potentially_threadable_block (bb))
9496 continue;
9498 /* We only care about blocks ending in a COND_EXPR. While there
9499 may be some value in handling SWITCH_EXPR here, I doubt it's
9500 terribly important. */
9501 last = gsi_stmt (gsi_last_bb (bb));
9503 /* We're basically looking for a switch or any kind of conditional with
9504 integral or pointer type arguments. Note the type of the second
9505 argument will be the same as the first argument, so no need to
9506 check it explicitly. */
9507 if (gimple_code (last) == GIMPLE_SWITCH
9508 || (gimple_code (last) == GIMPLE_COND
9509 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9510 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9511 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9512 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9513 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9515 edge_iterator ei;
9517 /* We've got a block with multiple predecessors and multiple
9518 successors which also ends in a suitable conditional or
9519 switch statement. For each predecessor, see if we can thread
9520 it to a specific successor. */
9521 FOR_EACH_EDGE (e, ei, bb->preds)
9523 /* Do not thread across back edges or abnormal edges
9524 in the CFG. */
9525 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9526 continue;
9528 thread_across_edge (dummy, e, true, &equiv_stack,
9529 simplify_stmt_for_jump_threading);
9534 /* We do not actually update the CFG or SSA graphs at this point as
9535 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9536 handle ASSERT_EXPRs gracefully. */
9539 /* We identified all the jump threading opportunities earlier, but could
9540 not transform the CFG at that time. This routine transforms the
9541 CFG and arranges for the dominator tree to be rebuilt if necessary.
9543 Note the SSA graph update will occur during the normal TODO
9544 processing by the pass manager. */
9545 static void
9546 finalize_jump_threads (void)
9548 thread_through_all_blocks (false);
9549 equiv_stack.release ();
9553 /* Traverse all the blocks folding conditionals with known ranges. */
9555 static void
9556 vrp_finalize (void)
9558 size_t i;
9560 values_propagated = true;
9562 if (dump_file)
9564 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9565 dump_all_value_ranges (dump_file);
9566 fprintf (dump_file, "\n");
9569 substitute_and_fold (op_with_constant_singleton_value_range,
9570 vrp_fold_stmt, false);
9572 if (warn_array_bounds)
9573 check_all_array_refs ();
9575 /* We must identify jump threading opportunities before we release
9576 the datastructures built by VRP. */
9577 identify_jump_threads ();
9579 /* Set value range to non pointer SSA_NAMEs. */
9580 for (i = 0; i < num_vr_values; i++)
9581 if (vr_value[i])
9583 tree name = ssa_name (i);
9585 if (!name
9586 || POINTER_TYPE_P (TREE_TYPE (name))
9587 || (vr_value[i]->type == VR_VARYING)
9588 || (vr_value[i]->type == VR_UNDEFINED))
9589 continue;
9591 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9592 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST))
9594 if (vr_value[i]->type == VR_RANGE)
9595 set_range_info (name,
9596 tree_to_double_int (vr_value[i]->min),
9597 tree_to_double_int (vr_value[i]->max));
9598 else if (vr_value[i]->type == VR_ANTI_RANGE)
9600 /* VR_ANTI_RANGE ~[min, max] is encoded compactly as
9601 [max + 1, min - 1] without additional attributes.
9602 When min value > max value, we know that it is
9603 VR_ANTI_RANGE; it is VR_RANGE otherwise. */
9605 /* ~[0,0] anti-range is represented as
9606 range. */
9607 if (TYPE_UNSIGNED (TREE_TYPE (name))
9608 && integer_zerop (vr_value[i]->min)
9609 && integer_zerop (vr_value[i]->max))
9610 set_range_info (name,
9611 double_int_one,
9612 double_int::max_value
9613 (TYPE_PRECISION (TREE_TYPE (name)), true));
9614 else
9615 set_range_info (name,
9616 tree_to_double_int (vr_value[i]->max)
9617 + double_int_one,
9618 tree_to_double_int (vr_value[i]->min)
9619 - double_int_one);
9624 /* Free allocated memory. */
9625 for (i = 0; i < num_vr_values; i++)
9626 if (vr_value[i])
9628 BITMAP_FREE (vr_value[i]->equiv);
9629 free (vr_value[i]);
9632 free (vr_value);
9633 free (vr_phi_edge_counts);
9635 /* So that we can distinguish between VRP data being available
9636 and not available. */
9637 vr_value = NULL;
9638 vr_phi_edge_counts = NULL;
9642 /* Main entry point to VRP (Value Range Propagation). This pass is
9643 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9644 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9645 Programming Language Design and Implementation, pp. 67-78, 1995.
9646 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9648 This is essentially an SSA-CCP pass modified to deal with ranges
9649 instead of constants.
9651 While propagating ranges, we may find that two or more SSA name
9652 have equivalent, though distinct ranges. For instance,
9654 1 x_9 = p_3->a;
9655 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9656 3 if (p_4 == q_2)
9657 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9658 5 endif
9659 6 if (q_2)
9661 In the code above, pointer p_5 has range [q_2, q_2], but from the
9662 code we can also determine that p_5 cannot be NULL and, if q_2 had
9663 a non-varying range, p_5's range should also be compatible with it.
9665 These equivalences are created by two expressions: ASSERT_EXPR and
9666 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9667 result of another assertion, then we can use the fact that p_5 and
9668 p_4 are equivalent when evaluating p_5's range.
9670 Together with value ranges, we also propagate these equivalences
9671 between names so that we can take advantage of information from
9672 multiple ranges when doing final replacement. Note that this
9673 equivalency relation is transitive but not symmetric.
9675 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9676 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9677 in contexts where that assertion does not hold (e.g., in line 6).
9679 TODO, the main difference between this pass and Patterson's is that
9680 we do not propagate edge probabilities. We only compute whether
9681 edges can be taken or not. That is, instead of having a spectrum
9682 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9683 DON'T KNOW. In the future, it may be worthwhile to propagate
9684 probabilities to aid branch prediction. */
9686 static unsigned int
9687 execute_vrp (void)
9689 int i;
9690 edge e;
9691 switch_update *su;
9693 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9694 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9695 scev_initialize ();
9697 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9698 Inserting assertions may split edges which will invalidate
9699 EDGE_DFS_BACK. */
9700 insert_range_assertions ();
9702 to_remove_edges.create (10);
9703 to_update_switch_stmts.create (5);
9704 threadedge_initialize_values ();
9706 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9707 mark_dfs_back_edges ();
9709 vrp_initialize ();
9710 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9711 vrp_finalize ();
9713 free_numbers_of_iterations_estimates ();
9715 /* ASSERT_EXPRs must be removed before finalizing jump threads
9716 as finalizing jump threads calls the CFG cleanup code which
9717 does not properly handle ASSERT_EXPRs. */
9718 remove_range_assertions ();
9720 /* If we exposed any new variables, go ahead and put them into
9721 SSA form now, before we handle jump threading. This simplifies
9722 interactions between rewriting of _DECL nodes into SSA form
9723 and rewriting SSA_NAME nodes into SSA form after block
9724 duplication and CFG manipulation. */
9725 update_ssa (TODO_update_ssa);
9727 finalize_jump_threads ();
9729 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9730 CFG in a broken state and requires a cfg_cleanup run. */
9731 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9732 remove_edge (e);
9733 /* Update SWITCH_EXPR case label vector. */
9734 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
9736 size_t j;
9737 size_t n = TREE_VEC_LENGTH (su->vec);
9738 tree label;
9739 gimple_switch_set_num_labels (su->stmt, n);
9740 for (j = 0; j < n; j++)
9741 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9742 /* As we may have replaced the default label with a regular one
9743 make sure to make it a real default label again. This ensures
9744 optimal expansion. */
9745 label = gimple_switch_label (su->stmt, 0);
9746 CASE_LOW (label) = NULL_TREE;
9747 CASE_HIGH (label) = NULL_TREE;
9750 if (to_remove_edges.length () > 0)
9752 free_dominance_info (CDI_DOMINATORS);
9753 if (current_loops)
9754 loops_state_set (LOOPS_NEED_FIXUP);
9757 to_remove_edges.release ();
9758 to_update_switch_stmts.release ();
9759 threadedge_finalize_values ();
9761 scev_finalize ();
9762 loop_optimizer_finalize ();
9763 return 0;
9766 static bool
9767 gate_vrp (void)
9769 return flag_tree_vrp != 0;
9772 namespace {
9774 const pass_data pass_data_vrp =
9776 GIMPLE_PASS, /* type */
9777 "vrp", /* name */
9778 OPTGROUP_NONE, /* optinfo_flags */
9779 true, /* has_gate */
9780 true, /* has_execute */
9781 TV_TREE_VRP, /* tv_id */
9782 PROP_ssa, /* properties_required */
9783 0, /* properties_provided */
9784 0, /* properties_destroyed */
9785 0, /* todo_flags_start */
9786 ( TODO_cleanup_cfg | TODO_update_ssa
9787 | TODO_verify_ssa
9788 | TODO_verify_flow ), /* todo_flags_finish */
9791 class pass_vrp : public gimple_opt_pass
9793 public:
9794 pass_vrp (gcc::context *ctxt)
9795 : gimple_opt_pass (pass_data_vrp, ctxt)
9798 /* opt_pass methods: */
9799 opt_pass * clone () { return new pass_vrp (m_ctxt); }
9800 bool gate () { return gate_vrp (); }
9801 unsigned int execute () { return execute_vrp (); }
9803 }; // class pass_vrp
9805 } // anon namespace
9807 gimple_opt_pass *
9808 make_pass_vrp (gcc::context *ctxt)
9810 return new pass_vrp (ctxt);