2016-09-10 Bernd Edlinger <bernd.edlinger@hotmail.de>
[official-gcc.git] / gcc / tree-vrp.c
blobe7067ab8e6ecc9950f3f8de2653fd32af9454171
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2016 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 "backend.h"
25 #include "insn-codes.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "flags.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "cfganal.h"
40 #include "gimple-fold.h"
41 #include "tree-eh.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
44 #include "tree-cfg.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
49 #include "tree-ssa.h"
50 #include "intl.h"
51 #include "cfgloop.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "tree-ssa-scopedtables.h"
57 #include "tree-ssa-threadedge.h"
58 #include "omp-low.h"
59 #include "target.h"
60 #include "case-cfn-macros.h"
61 #include "params.h"
62 #include "alloc-pool.h"
64 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
66 /* Allocation pools for tree-vrp allocations. */
67 static object_allocator<value_range> vrp_value_range_pool ("Tree VRP value ranges");
68 static bitmap_obstack vrp_equiv_obstack;
70 /* Set of SSA names found live during the RPO traversal of the function
71 for still active basic-blocks. */
72 static sbitmap *live;
74 /* Return true if the SSA name NAME is live on the edge E. */
76 static bool
77 live_on_edge (edge e, tree name)
79 return (live[e->dest->index]
80 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
83 /* Local functions. */
84 static int compare_values (tree val1, tree val2);
85 static int compare_values_warnv (tree val1, tree val2, bool *);
86 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
87 tree, tree, bool, bool *,
88 bool *);
90 /* Location information for ASSERT_EXPRs. Each instance of this
91 structure describes an ASSERT_EXPR for an SSA name. Since a single
92 SSA name may have more than one assertion associated with it, these
93 locations are kept in a linked list attached to the corresponding
94 SSA name. */
95 struct assert_locus
97 /* Basic block where the assertion would be inserted. */
98 basic_block bb;
100 /* Some assertions need to be inserted on an edge (e.g., assertions
101 generated by COND_EXPRs). In those cases, BB will be NULL. */
102 edge e;
104 /* Pointer to the statement that generated this assertion. */
105 gimple_stmt_iterator si;
107 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
108 enum tree_code comp_code;
110 /* Value being compared against. */
111 tree val;
113 /* Expression to compare. */
114 tree expr;
116 /* Next node in the linked list. */
117 assert_locus *next;
120 /* If bit I is present, it means that SSA name N_i has a list of
121 assertions that should be inserted in the IL. */
122 static bitmap need_assert_for;
124 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
125 holds a list of ASSERT_LOCUS_T nodes that describe where
126 ASSERT_EXPRs for SSA name N_I should be inserted. */
127 static assert_locus **asserts_for;
129 /* Value range array. After propagation, VR_VALUE[I] holds the range
130 of values that SSA name N_I may take. */
131 static unsigned num_vr_values;
132 static value_range **vr_value;
133 static bool values_propagated;
135 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
136 number of executable edges we saw the last time we visited the
137 node. */
138 static int *vr_phi_edge_counts;
140 struct switch_update {
141 gswitch *stmt;
142 tree vec;
145 static vec<edge> to_remove_edges;
146 static vec<switch_update> to_update_switch_stmts;
149 /* Return the maximum value for TYPE. */
151 static inline tree
152 vrp_val_max (const_tree type)
154 if (!INTEGRAL_TYPE_P (type))
155 return NULL_TREE;
157 return TYPE_MAX_VALUE (type);
160 /* Return the minimum value for TYPE. */
162 static inline tree
163 vrp_val_min (const_tree type)
165 if (!INTEGRAL_TYPE_P (type))
166 return NULL_TREE;
168 return TYPE_MIN_VALUE (type);
171 /* Return whether VAL is equal to the maximum value of its type. This
172 will be true for a positive overflow infinity. We can't do a
173 simple equality comparison with TYPE_MAX_VALUE because C typedefs
174 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
175 to the integer constant with the same value in the type. */
177 static inline bool
178 vrp_val_is_max (const_tree val)
180 tree type_max = vrp_val_max (TREE_TYPE (val));
181 return (val == type_max
182 || (type_max != NULL_TREE
183 && operand_equal_p (val, type_max, 0)));
186 /* Return whether VAL is equal to the minimum value of its type. This
187 will be true for a negative overflow infinity. */
189 static inline bool
190 vrp_val_is_min (const_tree val)
192 tree type_min = vrp_val_min (TREE_TYPE (val));
193 return (val == type_min
194 || (type_min != NULL_TREE
195 && operand_equal_p (val, type_min, 0)));
199 /* Return whether TYPE should use an overflow infinity distinct from
200 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
201 represent a signed overflow during VRP computations. An infinity
202 is distinct from a half-range, which will go from some number to
203 TYPE_{MIN,MAX}_VALUE. */
205 static inline bool
206 needs_overflow_infinity (const_tree type)
208 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
211 /* Return whether TYPE can support our overflow infinity
212 representation: we use the TREE_OVERFLOW flag, which only exists
213 for constants. If TYPE doesn't support this, we don't optimize
214 cases which would require signed overflow--we drop them to
215 VARYING. */
217 static inline bool
218 supports_overflow_infinity (const_tree type)
220 tree min = vrp_val_min (type), max = vrp_val_max (type);
221 gcc_checking_assert (needs_overflow_infinity (type));
222 return (min != NULL_TREE
223 && CONSTANT_CLASS_P (min)
224 && max != NULL_TREE
225 && CONSTANT_CLASS_P (max));
228 /* VAL is the maximum or minimum value of a type. Return a
229 corresponding overflow infinity. */
231 static inline tree
232 make_overflow_infinity (tree val)
234 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
235 val = copy_node (val);
236 TREE_OVERFLOW (val) = 1;
237 return val;
240 /* Return a negative overflow infinity for TYPE. */
242 static inline tree
243 negative_overflow_infinity (tree type)
245 gcc_checking_assert (supports_overflow_infinity (type));
246 return make_overflow_infinity (vrp_val_min (type));
249 /* Return a positive overflow infinity for TYPE. */
251 static inline tree
252 positive_overflow_infinity (tree type)
254 gcc_checking_assert (supports_overflow_infinity (type));
255 return make_overflow_infinity (vrp_val_max (type));
258 /* Return whether VAL is a negative overflow infinity. */
260 static inline bool
261 is_negative_overflow_infinity (const_tree val)
263 return (TREE_OVERFLOW_P (val)
264 && needs_overflow_infinity (TREE_TYPE (val))
265 && vrp_val_is_min (val));
268 /* Return whether VAL is a positive overflow infinity. */
270 static inline bool
271 is_positive_overflow_infinity (const_tree val)
273 return (TREE_OVERFLOW_P (val)
274 && needs_overflow_infinity (TREE_TYPE (val))
275 && vrp_val_is_max (val));
278 /* Return whether VAL is a positive or negative overflow infinity. */
280 static inline bool
281 is_overflow_infinity (const_tree val)
283 return (TREE_OVERFLOW_P (val)
284 && needs_overflow_infinity (TREE_TYPE (val))
285 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
288 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
290 static inline bool
291 stmt_overflow_infinity (gimple *stmt)
293 if (is_gimple_assign (stmt)
294 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
295 GIMPLE_SINGLE_RHS)
296 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
297 return false;
300 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
301 the same value with TREE_OVERFLOW clear. This can be used to avoid
302 confusing a regular value with an overflow value. */
304 static inline tree
305 avoid_overflow_infinity (tree val)
307 if (!is_overflow_infinity (val))
308 return val;
310 if (vrp_val_is_max (val))
311 return vrp_val_max (TREE_TYPE (val));
312 else
314 gcc_checking_assert (vrp_val_is_min (val));
315 return vrp_val_min (TREE_TYPE (val));
320 /* Set value range VR to VR_UNDEFINED. */
322 static inline void
323 set_value_range_to_undefined (value_range *vr)
325 vr->type = VR_UNDEFINED;
326 vr->min = vr->max = NULL_TREE;
327 if (vr->equiv)
328 bitmap_clear (vr->equiv);
332 /* Set value range VR to VR_VARYING. */
334 static inline void
335 set_value_range_to_varying (value_range *vr)
337 vr->type = VR_VARYING;
338 vr->min = vr->max = NULL_TREE;
339 if (vr->equiv)
340 bitmap_clear (vr->equiv);
344 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
346 static void
347 set_value_range (value_range *vr, enum value_range_type t, tree min,
348 tree max, bitmap equiv)
350 /* Check the validity of the range. */
351 if (flag_checking
352 && (t == VR_RANGE || t == VR_ANTI_RANGE))
354 int cmp;
356 gcc_assert (min && max);
358 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
359 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
361 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
362 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
364 cmp = compare_values (min, max);
365 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
367 if (needs_overflow_infinity (TREE_TYPE (min)))
368 gcc_assert (!is_overflow_infinity (min)
369 || !is_overflow_infinity (max));
372 if (flag_checking
373 && (t == VR_UNDEFINED || t == VR_VARYING))
375 gcc_assert (min == NULL_TREE && max == NULL_TREE);
376 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
379 vr->type = t;
380 vr->min = min;
381 vr->max = max;
383 /* Since updating the equivalence set involves deep copying the
384 bitmaps, only do it if absolutely necessary. */
385 if (vr->equiv == NULL
386 && equiv != NULL)
387 vr->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
389 if (equiv != vr->equiv)
391 if (equiv && !bitmap_empty_p (equiv))
392 bitmap_copy (vr->equiv, equiv);
393 else
394 bitmap_clear (vr->equiv);
399 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
400 This means adjusting T, MIN and MAX representing the case of a
401 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
402 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
403 In corner cases where MAX+1 or MIN-1 wraps this will fall back
404 to varying.
405 This routine exists to ease canonicalization in the case where we
406 extract ranges from var + CST op limit. */
408 static void
409 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
410 tree min, tree max, bitmap equiv)
412 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
413 if (t == VR_UNDEFINED)
415 set_value_range_to_undefined (vr);
416 return;
418 else if (t == VR_VARYING)
420 set_value_range_to_varying (vr);
421 return;
424 /* Nothing to canonicalize for symbolic ranges. */
425 if (TREE_CODE (min) != INTEGER_CST
426 || TREE_CODE (max) != INTEGER_CST)
428 set_value_range (vr, t, min, max, equiv);
429 return;
432 /* Wrong order for min and max, to swap them and the VR type we need
433 to adjust them. */
434 if (tree_int_cst_lt (max, min))
436 tree one, tmp;
438 /* For one bit precision if max < min, then the swapped
439 range covers all values, so for VR_RANGE it is varying and
440 for VR_ANTI_RANGE empty range, so drop to varying as well. */
441 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
443 set_value_range_to_varying (vr);
444 return;
447 one = build_int_cst (TREE_TYPE (min), 1);
448 tmp = int_const_binop (PLUS_EXPR, max, one);
449 max = int_const_binop (MINUS_EXPR, min, one);
450 min = tmp;
452 /* There's one corner case, if we had [C+1, C] before we now have
453 that again. But this represents an empty value range, so drop
454 to varying in this case. */
455 if (tree_int_cst_lt (max, min))
457 set_value_range_to_varying (vr);
458 return;
461 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
464 /* Anti-ranges that can be represented as ranges should be so. */
465 if (t == VR_ANTI_RANGE)
467 bool is_min = vrp_val_is_min (min);
468 bool is_max = vrp_val_is_max (max);
470 if (is_min && is_max)
472 /* We cannot deal with empty ranges, drop to varying.
473 ??? This could be VR_UNDEFINED instead. */
474 set_value_range_to_varying (vr);
475 return;
477 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
478 && (is_min || is_max))
480 /* Non-empty boolean ranges can always be represented
481 as a singleton range. */
482 if (is_min)
483 min = max = vrp_val_max (TREE_TYPE (min));
484 else
485 min = max = vrp_val_min (TREE_TYPE (min));
486 t = VR_RANGE;
488 else if (is_min
489 /* As a special exception preserve non-null ranges. */
490 && !(TYPE_UNSIGNED (TREE_TYPE (min))
491 && integer_zerop (max)))
493 tree one = build_int_cst (TREE_TYPE (max), 1);
494 min = int_const_binop (PLUS_EXPR, max, one);
495 max = vrp_val_max (TREE_TYPE (max));
496 t = VR_RANGE;
498 else if (is_max)
500 tree one = build_int_cst (TREE_TYPE (min), 1);
501 max = int_const_binop (MINUS_EXPR, min, one);
502 min = vrp_val_min (TREE_TYPE (min));
503 t = VR_RANGE;
507 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
508 if (needs_overflow_infinity (TREE_TYPE (min))
509 && is_overflow_infinity (min)
510 && is_overflow_infinity (max))
512 set_value_range_to_varying (vr);
513 return;
516 set_value_range (vr, t, min, max, equiv);
519 /* Copy value range FROM into value range TO. */
521 static inline void
522 copy_value_range (value_range *to, value_range *from)
524 set_value_range (to, from->type, from->min, from->max, from->equiv);
527 /* Set value range VR to a single value. This function is only called
528 with values we get from statements, and exists to clear the
529 TREE_OVERFLOW flag so that we don't think we have an overflow
530 infinity when we shouldn't. */
532 static inline void
533 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
535 gcc_assert (is_gimple_min_invariant (val));
536 if (TREE_OVERFLOW_P (val))
537 val = drop_tree_overflow (val);
538 set_value_range (vr, VR_RANGE, val, val, equiv);
541 /* Set value range VR to a non-negative range of type TYPE.
542 OVERFLOW_INFINITY indicates whether to use an overflow infinity
543 rather than TYPE_MAX_VALUE; this should be true if we determine
544 that the range is nonnegative based on the assumption that signed
545 overflow does not occur. */
547 static inline void
548 set_value_range_to_nonnegative (value_range *vr, tree type,
549 bool overflow_infinity)
551 tree zero;
553 if (overflow_infinity && !supports_overflow_infinity (type))
555 set_value_range_to_varying (vr);
556 return;
559 zero = build_int_cst (type, 0);
560 set_value_range (vr, VR_RANGE, zero,
561 (overflow_infinity
562 ? positive_overflow_infinity (type)
563 : TYPE_MAX_VALUE (type)),
564 vr->equiv);
567 /* Set value range VR to a non-NULL range of type TYPE. */
569 static inline void
570 set_value_range_to_nonnull (value_range *vr, tree type)
572 tree zero = build_int_cst (type, 0);
573 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
577 /* Set value range VR to a NULL range of type TYPE. */
579 static inline void
580 set_value_range_to_null (value_range *vr, tree type)
582 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
586 /* Set value range VR to a range of a truthvalue of type TYPE. */
588 static inline void
589 set_value_range_to_truthvalue (value_range *vr, tree type)
591 if (TYPE_PRECISION (type) == 1)
592 set_value_range_to_varying (vr);
593 else
594 set_value_range (vr, VR_RANGE,
595 build_int_cst (type, 0), build_int_cst (type, 1),
596 vr->equiv);
600 /* If abs (min) < abs (max), set VR to [-max, max], if
601 abs (min) >= abs (max), set VR to [-min, min]. */
603 static void
604 abs_extent_range (value_range *vr, tree min, tree max)
606 int cmp;
608 gcc_assert (TREE_CODE (min) == INTEGER_CST);
609 gcc_assert (TREE_CODE (max) == INTEGER_CST);
610 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
611 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
612 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
613 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
614 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
616 set_value_range_to_varying (vr);
617 return;
619 cmp = compare_values (min, max);
620 if (cmp == -1)
621 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
622 else if (cmp == 0 || cmp == 1)
624 max = min;
625 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
627 else
629 set_value_range_to_varying (vr);
630 return;
632 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
636 /* Return value range information for VAR.
638 If we have no values ranges recorded (ie, VRP is not running), then
639 return NULL. Otherwise create an empty range if none existed for VAR. */
641 static value_range *
642 get_value_range (const_tree var)
644 static const value_range vr_const_varying
645 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
646 value_range *vr;
647 tree sym;
648 unsigned ver = SSA_NAME_VERSION (var);
650 /* If we have no recorded ranges, then return NULL. */
651 if (! vr_value)
652 return NULL;
654 /* If we query the range for a new SSA name return an unmodifiable VARYING.
655 We should get here at most from the substitute-and-fold stage which
656 will never try to change values. */
657 if (ver >= num_vr_values)
658 return CONST_CAST (value_range *, &vr_const_varying);
660 vr = vr_value[ver];
661 if (vr)
662 return vr;
664 /* After propagation finished do not allocate new value-ranges. */
665 if (values_propagated)
666 return CONST_CAST (value_range *, &vr_const_varying);
668 /* Create a default value range. */
669 vr_value[ver] = vr = vrp_value_range_pool.allocate ();
670 memset (vr, 0, sizeof (*vr));
672 /* Defer allocating the equivalence set. */
673 vr->equiv = NULL;
675 /* If VAR is a default definition of a parameter, the variable can
676 take any value in VAR's type. */
677 if (SSA_NAME_IS_DEFAULT_DEF (var))
679 sym = SSA_NAME_VAR (var);
680 if (TREE_CODE (sym) == PARM_DECL)
682 /* Try to use the "nonnull" attribute to create ~[0, 0]
683 anti-ranges for pointers. Note that this is only valid with
684 default definitions of PARM_DECLs. */
685 if (POINTER_TYPE_P (TREE_TYPE (sym))
686 && nonnull_arg_p (sym))
687 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
688 else
689 set_value_range_to_varying (vr);
691 else if (TREE_CODE (sym) == RESULT_DECL
692 && DECL_BY_REFERENCE (sym))
693 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
696 return vr;
699 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
701 static inline bool
702 vrp_operand_equal_p (const_tree val1, const_tree val2)
704 if (val1 == val2)
705 return true;
706 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
707 return false;
708 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
711 /* Return true, if the bitmaps B1 and B2 are equal. */
713 static inline bool
714 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
716 return (b1 == b2
717 || ((!b1 || bitmap_empty_p (b1))
718 && (!b2 || bitmap_empty_p (b2)))
719 || (b1 && b2
720 && bitmap_equal_p (b1, b2)));
723 /* Update the value range and equivalence set for variable VAR to
724 NEW_VR. Return true if NEW_VR is different from VAR's previous
725 value.
727 NOTE: This function assumes that NEW_VR is a temporary value range
728 object created for the sole purpose of updating VAR's range. The
729 storage used by the equivalence set from NEW_VR will be freed by
730 this function. Do not call update_value_range when NEW_VR
731 is the range object associated with another SSA name. */
733 static inline bool
734 update_value_range (const_tree var, value_range *new_vr)
736 value_range *old_vr;
737 bool is_new;
739 /* If there is a value-range on the SSA name from earlier analysis
740 factor that in. */
741 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
743 wide_int min, max;
744 value_range_type rtype = get_range_info (var, &min, &max);
745 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
747 tree nr_min, nr_max;
748 /* Range info on SSA names doesn't carry overflow information
749 so make sure to preserve the overflow bit on the lattice. */
750 if (rtype == VR_RANGE
751 && needs_overflow_infinity (TREE_TYPE (var))
752 && (new_vr->type == VR_VARYING
753 || (new_vr->type == VR_RANGE
754 && is_negative_overflow_infinity (new_vr->min)))
755 && wi::eq_p (vrp_val_min (TREE_TYPE (var)), min))
756 nr_min = negative_overflow_infinity (TREE_TYPE (var));
757 else
758 nr_min = wide_int_to_tree (TREE_TYPE (var), min);
759 if (rtype == VR_RANGE
760 && needs_overflow_infinity (TREE_TYPE (var))
761 && (new_vr->type == VR_VARYING
762 || (new_vr->type == VR_RANGE
763 && is_positive_overflow_infinity (new_vr->max)))
764 && wi::eq_p (vrp_val_max (TREE_TYPE (var)), max))
765 nr_max = positive_overflow_infinity (TREE_TYPE (var));
766 else
767 nr_max = wide_int_to_tree (TREE_TYPE (var), max);
768 value_range nr = VR_INITIALIZER;
769 set_and_canonicalize_value_range (&nr, rtype, nr_min, nr_max, NULL);
770 vrp_intersect_ranges (new_vr, &nr);
774 /* Update the value range, if necessary. */
775 old_vr = get_value_range (var);
776 is_new = old_vr->type != new_vr->type
777 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
778 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
779 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
781 if (is_new)
783 /* Do not allow transitions up the lattice. The following
784 is slightly more awkward than just new_vr->type < old_vr->type
785 because VR_RANGE and VR_ANTI_RANGE need to be considered
786 the same. We may not have is_new when transitioning to
787 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
788 called. */
789 if (new_vr->type == VR_UNDEFINED)
791 BITMAP_FREE (new_vr->equiv);
792 set_value_range_to_varying (old_vr);
793 set_value_range_to_varying (new_vr);
794 return true;
796 else
797 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
798 new_vr->equiv);
801 BITMAP_FREE (new_vr->equiv);
803 return is_new;
807 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
808 point where equivalence processing can be turned on/off. */
810 static void
811 add_equivalence (bitmap *equiv, const_tree var)
813 unsigned ver = SSA_NAME_VERSION (var);
814 value_range *vr = vr_value[ver];
816 if (*equiv == NULL)
817 *equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
818 bitmap_set_bit (*equiv, ver);
819 if (vr && vr->equiv)
820 bitmap_ior_into (*equiv, vr->equiv);
824 /* Return true if VR is ~[0, 0]. */
826 static inline bool
827 range_is_nonnull (value_range *vr)
829 return vr->type == VR_ANTI_RANGE
830 && integer_zerop (vr->min)
831 && integer_zerop (vr->max);
835 /* Return true if VR is [0, 0]. */
837 static inline bool
838 range_is_null (value_range *vr)
840 return vr->type == VR_RANGE
841 && integer_zerop (vr->min)
842 && integer_zerop (vr->max);
845 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
846 a singleton. */
848 static inline bool
849 range_int_cst_p (value_range *vr)
851 return (vr->type == VR_RANGE
852 && TREE_CODE (vr->max) == INTEGER_CST
853 && TREE_CODE (vr->min) == INTEGER_CST);
856 /* Return true if VR is a INTEGER_CST singleton. */
858 static inline bool
859 range_int_cst_singleton_p (value_range *vr)
861 return (range_int_cst_p (vr)
862 && !is_overflow_infinity (vr->min)
863 && !is_overflow_infinity (vr->max)
864 && tree_int_cst_equal (vr->min, vr->max));
867 /* Return true if value range VR involves at least one symbol. */
869 static inline bool
870 symbolic_range_p (value_range *vr)
872 return (!is_gimple_min_invariant (vr->min)
873 || !is_gimple_min_invariant (vr->max));
876 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
877 otherwise. We only handle additive operations and set NEG to true if the
878 symbol is negated and INV to the invariant part, if any. */
880 static tree
881 get_single_symbol (tree t, bool *neg, tree *inv)
883 bool neg_;
884 tree inv_;
886 *inv = NULL_TREE;
887 *neg = false;
889 if (TREE_CODE (t) == PLUS_EXPR
890 || TREE_CODE (t) == POINTER_PLUS_EXPR
891 || TREE_CODE (t) == MINUS_EXPR)
893 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
895 neg_ = (TREE_CODE (t) == MINUS_EXPR);
896 inv_ = TREE_OPERAND (t, 0);
897 t = TREE_OPERAND (t, 1);
899 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
901 neg_ = false;
902 inv_ = TREE_OPERAND (t, 1);
903 t = TREE_OPERAND (t, 0);
905 else
906 return NULL_TREE;
908 else
910 neg_ = false;
911 inv_ = NULL_TREE;
914 if (TREE_CODE (t) == NEGATE_EXPR)
916 t = TREE_OPERAND (t, 0);
917 neg_ = !neg_;
920 if (TREE_CODE (t) != SSA_NAME)
921 return NULL_TREE;
923 *neg = neg_;
924 *inv = inv_;
925 return t;
928 /* The reverse operation: build a symbolic expression with TYPE
929 from symbol SYM, negated according to NEG, and invariant INV. */
931 static tree
932 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
934 const bool pointer_p = POINTER_TYPE_P (type);
935 tree t = sym;
937 if (neg)
938 t = build1 (NEGATE_EXPR, type, t);
940 if (integer_zerop (inv))
941 return t;
943 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
946 /* Return true if value range VR involves exactly one symbol SYM. */
948 static bool
949 symbolic_range_based_on_p (value_range *vr, const_tree sym)
951 bool neg, min_has_symbol, max_has_symbol;
952 tree inv;
954 if (is_gimple_min_invariant (vr->min))
955 min_has_symbol = false;
956 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
957 min_has_symbol = true;
958 else
959 return false;
961 if (is_gimple_min_invariant (vr->max))
962 max_has_symbol = false;
963 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
964 max_has_symbol = true;
965 else
966 return false;
968 return (min_has_symbol || max_has_symbol);
971 /* Return true if value range VR uses an overflow infinity. */
973 static inline bool
974 overflow_infinity_range_p (value_range *vr)
976 return (vr->type == VR_RANGE
977 && (is_overflow_infinity (vr->min)
978 || is_overflow_infinity (vr->max)));
981 /* Return false if we can not make a valid comparison based on VR;
982 this will be the case if it uses an overflow infinity and overflow
983 is not undefined (i.e., -fno-strict-overflow is in effect).
984 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
985 uses an overflow infinity. */
987 static bool
988 usable_range_p (value_range *vr, bool *strict_overflow_p)
990 gcc_assert (vr->type == VR_RANGE);
991 if (is_overflow_infinity (vr->min))
993 *strict_overflow_p = true;
994 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
995 return false;
997 if (is_overflow_infinity (vr->max))
999 *strict_overflow_p = true;
1000 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1001 return false;
1003 return true;
1006 /* Return true if the result of assignment STMT is know to be non-zero.
1007 If the return value is based on the assumption that signed overflow is
1008 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1009 *STRICT_OVERFLOW_P.*/
1011 static bool
1012 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1014 enum tree_code code = gimple_assign_rhs_code (stmt);
1015 switch (get_gimple_rhs_class (code))
1017 case GIMPLE_UNARY_RHS:
1018 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1019 gimple_expr_type (stmt),
1020 gimple_assign_rhs1 (stmt),
1021 strict_overflow_p);
1022 case GIMPLE_BINARY_RHS:
1023 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1024 gimple_expr_type (stmt),
1025 gimple_assign_rhs1 (stmt),
1026 gimple_assign_rhs2 (stmt),
1027 strict_overflow_p);
1028 case GIMPLE_TERNARY_RHS:
1029 return false;
1030 case GIMPLE_SINGLE_RHS:
1031 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1032 strict_overflow_p);
1033 case GIMPLE_INVALID_RHS:
1034 gcc_unreachable ();
1035 default:
1036 gcc_unreachable ();
1040 /* Return true if STMT is known to compute a non-zero value.
1041 If the return value is based on the assumption that signed overflow is
1042 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1043 *STRICT_OVERFLOW_P.*/
1045 static bool
1046 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1048 switch (gimple_code (stmt))
1050 case GIMPLE_ASSIGN:
1051 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1052 case GIMPLE_CALL:
1054 tree fndecl = gimple_call_fndecl (stmt);
1055 if (!fndecl) return false;
1056 if (flag_delete_null_pointer_checks && !flag_check_new
1057 && DECL_IS_OPERATOR_NEW (fndecl)
1058 && !TREE_NOTHROW (fndecl))
1059 return true;
1060 /* References are always non-NULL. */
1061 if (flag_delete_null_pointer_checks
1062 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1063 return true;
1064 if (flag_delete_null_pointer_checks &&
1065 lookup_attribute ("returns_nonnull",
1066 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1067 return true;
1068 return gimple_alloca_call_p (stmt);
1070 default:
1071 gcc_unreachable ();
1075 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1076 obtained so far. */
1078 static bool
1079 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1081 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1082 return true;
1084 /* If we have an expression of the form &X->a, then the expression
1085 is nonnull if X is nonnull. */
1086 if (is_gimple_assign (stmt)
1087 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1089 tree expr = gimple_assign_rhs1 (stmt);
1090 tree base = get_base_address (TREE_OPERAND (expr, 0));
1092 if (base != NULL_TREE
1093 && TREE_CODE (base) == MEM_REF
1094 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1096 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1097 if (range_is_nonnull (vr))
1098 return true;
1102 return false;
1105 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1106 a gimple invariant, or SSA_NAME +- CST. */
1108 static bool
1109 valid_value_p (tree expr)
1111 if (TREE_CODE (expr) == SSA_NAME)
1112 return true;
1114 if (TREE_CODE (expr) == PLUS_EXPR
1115 || TREE_CODE (expr) == MINUS_EXPR)
1116 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1117 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1119 return is_gimple_min_invariant (expr);
1122 /* Return
1123 1 if VAL < VAL2
1124 0 if !(VAL < VAL2)
1125 -2 if those are incomparable. */
1126 static inline int
1127 operand_less_p (tree val, tree val2)
1129 /* LT is folded faster than GE and others. Inline the common case. */
1130 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1132 if (! is_positive_overflow_infinity (val2))
1133 return tree_int_cst_lt (val, val2);
1135 else
1137 tree tcmp;
1139 fold_defer_overflow_warnings ();
1141 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1143 fold_undefer_and_ignore_overflow_warnings ();
1145 if (!tcmp
1146 || TREE_CODE (tcmp) != INTEGER_CST)
1147 return -2;
1149 if (!integer_zerop (tcmp))
1150 return 1;
1153 /* val >= val2, not considering overflow infinity. */
1154 if (is_negative_overflow_infinity (val))
1155 return is_negative_overflow_infinity (val2) ? 0 : 1;
1156 else if (is_positive_overflow_infinity (val2))
1157 return is_positive_overflow_infinity (val) ? 0 : 1;
1159 return 0;
1162 /* Compare two values VAL1 and VAL2. Return
1164 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1165 -1 if VAL1 < VAL2,
1166 0 if VAL1 == VAL2,
1167 +1 if VAL1 > VAL2, and
1168 +2 if VAL1 != VAL2
1170 This is similar to tree_int_cst_compare but supports pointer values
1171 and values that cannot be compared at compile time.
1173 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1174 true if the return value is only valid if we assume that signed
1175 overflow is undefined. */
1177 static int
1178 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1180 if (val1 == val2)
1181 return 0;
1183 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1184 both integers. */
1185 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1186 == POINTER_TYPE_P (TREE_TYPE (val2)));
1188 /* Convert the two values into the same type. This is needed because
1189 sizetype causes sign extension even for unsigned types. */
1190 val2 = fold_convert (TREE_TYPE (val1), val2);
1191 STRIP_USELESS_TYPE_CONVERSION (val2);
1193 const bool overflow_undefined
1194 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
1195 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
1196 tree inv1, inv2;
1197 bool neg1, neg2;
1198 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
1199 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
1201 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1202 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1203 if (sym1 && sym2)
1205 /* Both values must use the same name with the same sign. */
1206 if (sym1 != sym2 || neg1 != neg2)
1207 return -2;
1209 /* [-]NAME + CST == [-]NAME + CST. */
1210 if (inv1 == inv2)
1211 return 0;
1213 /* If overflow is defined we cannot simplify more. */
1214 if (!overflow_undefined)
1215 return -2;
1217 if (strict_overflow_p != NULL
1218 && (!inv1 || !TREE_NO_WARNING (val1))
1219 && (!inv2 || !TREE_NO_WARNING (val2)))
1220 *strict_overflow_p = true;
1222 if (!inv1)
1223 inv1 = build_int_cst (TREE_TYPE (val1), 0);
1224 if (!inv2)
1225 inv2 = build_int_cst (TREE_TYPE (val2), 0);
1227 return compare_values_warnv (inv1, inv2, strict_overflow_p);
1230 const bool cst1 = is_gimple_min_invariant (val1);
1231 const bool cst2 = is_gimple_min_invariant (val2);
1233 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1234 it might be possible to say something depending on the constants. */
1235 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
1237 if (!overflow_undefined)
1238 return -2;
1240 if (strict_overflow_p != NULL
1241 && (!sym1 || !TREE_NO_WARNING (val1))
1242 && (!sym2 || !TREE_NO_WARNING (val2)))
1243 *strict_overflow_p = true;
1245 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
1246 tree cst = cst1 ? val1 : val2;
1247 tree inv = cst1 ? inv2 : inv1;
1249 /* Compute the difference between the constants. If it overflows or
1250 underflows, this means that we can trivially compare the NAME with
1251 it and, consequently, the two values with each other. */
1252 wide_int diff = wi::sub (cst, inv);
1253 if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn))
1255 const int res = wi::cmp (cst, inv, sgn);
1256 return cst1 ? res : -res;
1259 return -2;
1262 /* We cannot say anything more for non-constants. */
1263 if (!cst1 || !cst2)
1264 return -2;
1266 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1268 /* We cannot compare overflowed values, except for overflow
1269 infinities. */
1270 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1272 if (strict_overflow_p != NULL)
1273 *strict_overflow_p = true;
1274 if (is_negative_overflow_infinity (val1))
1275 return is_negative_overflow_infinity (val2) ? 0 : -1;
1276 else if (is_negative_overflow_infinity (val2))
1277 return 1;
1278 else if (is_positive_overflow_infinity (val1))
1279 return is_positive_overflow_infinity (val2) ? 0 : 1;
1280 else if (is_positive_overflow_infinity (val2))
1281 return -1;
1282 return -2;
1285 return tree_int_cst_compare (val1, val2);
1287 else
1289 tree t;
1291 /* First see if VAL1 and VAL2 are not the same. */
1292 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1293 return 0;
1295 /* If VAL1 is a lower address than VAL2, return -1. */
1296 if (operand_less_p (val1, val2) == 1)
1297 return -1;
1299 /* If VAL1 is a higher address than VAL2, return +1. */
1300 if (operand_less_p (val2, val1) == 1)
1301 return 1;
1303 /* If VAL1 is different than VAL2, return +2.
1304 For integer constants we either have already returned -1 or 1
1305 or they are equivalent. We still might succeed in proving
1306 something about non-trivial operands. */
1307 if (TREE_CODE (val1) != INTEGER_CST
1308 || TREE_CODE (val2) != INTEGER_CST)
1310 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1311 if (t && integer_onep (t))
1312 return 2;
1315 return -2;
1319 /* Compare values like compare_values_warnv, but treat comparisons of
1320 nonconstants which rely on undefined overflow as incomparable. */
1322 static int
1323 compare_values (tree val1, tree val2)
1325 bool sop;
1326 int ret;
1328 sop = false;
1329 ret = compare_values_warnv (val1, val2, &sop);
1330 if (sop
1331 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1332 ret = -2;
1333 return ret;
1337 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1338 0 if VAL is not inside [MIN, MAX],
1339 -2 if we cannot tell either way.
1341 Benchmark compile/20001226-1.c compilation time after changing this
1342 function. */
1344 static inline int
1345 value_inside_range (tree val, tree min, tree max)
1347 int cmp1, cmp2;
1349 cmp1 = operand_less_p (val, min);
1350 if (cmp1 == -2)
1351 return -2;
1352 if (cmp1 == 1)
1353 return 0;
1355 cmp2 = operand_less_p (max, val);
1356 if (cmp2 == -2)
1357 return -2;
1359 return !cmp2;
1363 /* Return true if value ranges VR0 and VR1 have a non-empty
1364 intersection.
1366 Benchmark compile/20001226-1.c compilation time after changing this
1367 function.
1370 static inline bool
1371 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1373 /* The value ranges do not intersect if the maximum of the first range is
1374 less than the minimum of the second range or vice versa.
1375 When those relations are unknown, we can't do any better. */
1376 if (operand_less_p (vr0->max, vr1->min) != 0)
1377 return false;
1378 if (operand_less_p (vr1->max, vr0->min) != 0)
1379 return false;
1380 return true;
1384 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1385 include the value zero, -2 if we cannot tell. */
1387 static inline int
1388 range_includes_zero_p (tree min, tree max)
1390 tree zero = build_int_cst (TREE_TYPE (min), 0);
1391 return value_inside_range (zero, min, max);
1394 /* Return true if *VR is know to only contain nonnegative values. */
1396 static inline bool
1397 value_range_nonnegative_p (value_range *vr)
1399 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1400 which would return a useful value should be encoded as a
1401 VR_RANGE. */
1402 if (vr->type == VR_RANGE)
1404 int result = compare_values (vr->min, integer_zero_node);
1405 return (result == 0 || result == 1);
1408 return false;
1411 /* If *VR has a value rante that is a single constant value return that,
1412 otherwise return NULL_TREE. */
1414 static tree
1415 value_range_constant_singleton (value_range *vr)
1417 if (vr->type == VR_RANGE
1418 && vrp_operand_equal_p (vr->min, vr->max)
1419 && is_gimple_min_invariant (vr->min))
1420 return vr->min;
1422 return NULL_TREE;
1425 /* If OP has a value range with a single constant value return that,
1426 otherwise return NULL_TREE. This returns OP itself if OP is a
1427 constant. */
1429 static tree
1430 op_with_constant_singleton_value_range (tree op)
1432 if (is_gimple_min_invariant (op))
1433 return op;
1435 if (TREE_CODE (op) != SSA_NAME)
1436 return NULL_TREE;
1438 return value_range_constant_singleton (get_value_range (op));
1441 /* Return true if op is in a boolean [0, 1] value-range. */
1443 static bool
1444 op_with_boolean_value_range_p (tree op)
1446 value_range *vr;
1448 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1449 return true;
1451 if (integer_zerop (op)
1452 || integer_onep (op))
1453 return true;
1455 if (TREE_CODE (op) != SSA_NAME)
1456 return false;
1458 vr = get_value_range (op);
1459 return (vr->type == VR_RANGE
1460 && integer_zerop (vr->min)
1461 && integer_onep (vr->max));
1464 /* Extract value range information from an ASSERT_EXPR EXPR and store
1465 it in *VR_P. */
1467 static void
1468 extract_range_from_assert (value_range *vr_p, tree expr)
1470 tree var, cond, limit, min, max, type;
1471 value_range *limit_vr;
1472 enum tree_code cond_code;
1474 var = ASSERT_EXPR_VAR (expr);
1475 cond = ASSERT_EXPR_COND (expr);
1477 gcc_assert (COMPARISON_CLASS_P (cond));
1479 /* Find VAR in the ASSERT_EXPR conditional. */
1480 if (var == TREE_OPERAND (cond, 0)
1481 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1482 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1484 /* If the predicate is of the form VAR COMP LIMIT, then we just
1485 take LIMIT from the RHS and use the same comparison code. */
1486 cond_code = TREE_CODE (cond);
1487 limit = TREE_OPERAND (cond, 1);
1488 cond = TREE_OPERAND (cond, 0);
1490 else
1492 /* If the predicate is of the form LIMIT COMP VAR, then we need
1493 to flip around the comparison code to create the proper range
1494 for VAR. */
1495 cond_code = swap_tree_comparison (TREE_CODE (cond));
1496 limit = TREE_OPERAND (cond, 0);
1497 cond = TREE_OPERAND (cond, 1);
1500 limit = avoid_overflow_infinity (limit);
1502 type = TREE_TYPE (var);
1503 gcc_assert (limit != var);
1505 /* For pointer arithmetic, we only keep track of pointer equality
1506 and inequality. */
1507 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1509 set_value_range_to_varying (vr_p);
1510 return;
1513 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1514 try to use LIMIT's range to avoid creating symbolic ranges
1515 unnecessarily. */
1516 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1518 /* LIMIT's range is only interesting if it has any useful information. */
1519 if (! limit_vr
1520 || limit_vr->type == VR_UNDEFINED
1521 || limit_vr->type == VR_VARYING
1522 || (symbolic_range_p (limit_vr)
1523 && ! (limit_vr->type == VR_RANGE
1524 && (limit_vr->min == limit_vr->max
1525 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
1526 limit_vr = NULL;
1528 /* Initially, the new range has the same set of equivalences of
1529 VAR's range. This will be revised before returning the final
1530 value. Since assertions may be chained via mutually exclusive
1531 predicates, we will need to trim the set of equivalences before
1532 we are done. */
1533 gcc_assert (vr_p->equiv == NULL);
1534 add_equivalence (&vr_p->equiv, var);
1536 /* Extract a new range based on the asserted comparison for VAR and
1537 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1538 will only use it for equality comparisons (EQ_EXPR). For any
1539 other kind of assertion, we cannot derive a range from LIMIT's
1540 anti-range that can be used to describe the new range. For
1541 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1542 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1543 no single range for x_2 that could describe LE_EXPR, so we might
1544 as well build the range [b_4, +INF] for it.
1545 One special case we handle is extracting a range from a
1546 range test encoded as (unsigned)var + CST <= limit. */
1547 if (TREE_CODE (cond) == NOP_EXPR
1548 || TREE_CODE (cond) == PLUS_EXPR)
1550 if (TREE_CODE (cond) == PLUS_EXPR)
1552 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1553 TREE_OPERAND (cond, 1));
1554 max = int_const_binop (PLUS_EXPR, limit, min);
1555 cond = TREE_OPERAND (cond, 0);
1557 else
1559 min = build_int_cst (TREE_TYPE (var), 0);
1560 max = limit;
1563 /* Make sure to not set TREE_OVERFLOW on the final type
1564 conversion. We are willingly interpreting large positive
1565 unsigned values as negative signed values here. */
1566 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1567 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1569 /* We can transform a max, min range to an anti-range or
1570 vice-versa. Use set_and_canonicalize_value_range which does
1571 this for us. */
1572 if (cond_code == LE_EXPR)
1573 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1574 min, max, vr_p->equiv);
1575 else if (cond_code == GT_EXPR)
1576 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1577 min, max, vr_p->equiv);
1578 else
1579 gcc_unreachable ();
1581 else if (cond_code == EQ_EXPR)
1583 enum value_range_type range_type;
1585 if (limit_vr)
1587 range_type = limit_vr->type;
1588 min = limit_vr->min;
1589 max = limit_vr->max;
1591 else
1593 range_type = VR_RANGE;
1594 min = limit;
1595 max = limit;
1598 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1600 /* When asserting the equality VAR == LIMIT and LIMIT is another
1601 SSA name, the new range will also inherit the equivalence set
1602 from LIMIT. */
1603 if (TREE_CODE (limit) == SSA_NAME)
1604 add_equivalence (&vr_p->equiv, limit);
1606 else if (cond_code == NE_EXPR)
1608 /* As described above, when LIMIT's range is an anti-range and
1609 this assertion is an inequality (NE_EXPR), then we cannot
1610 derive anything from the anti-range. For instance, if
1611 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1612 not imply that VAR's range is [0, 0]. So, in the case of
1613 anti-ranges, we just assert the inequality using LIMIT and
1614 not its anti-range.
1616 If LIMIT_VR is a range, we can only use it to build a new
1617 anti-range if LIMIT_VR is a single-valued range. For
1618 instance, if LIMIT_VR is [0, 1], the predicate
1619 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1620 Rather, it means that for value 0 VAR should be ~[0, 0]
1621 and for value 1, VAR should be ~[1, 1]. We cannot
1622 represent these ranges.
1624 The only situation in which we can build a valid
1625 anti-range is when LIMIT_VR is a single-valued range
1626 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1627 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1628 if (limit_vr
1629 && limit_vr->type == VR_RANGE
1630 && compare_values (limit_vr->min, limit_vr->max) == 0)
1632 min = limit_vr->min;
1633 max = limit_vr->max;
1635 else
1637 /* In any other case, we cannot use LIMIT's range to build a
1638 valid anti-range. */
1639 min = max = limit;
1642 /* If MIN and MAX cover the whole range for their type, then
1643 just use the original LIMIT. */
1644 if (INTEGRAL_TYPE_P (type)
1645 && vrp_val_is_min (min)
1646 && vrp_val_is_max (max))
1647 min = max = limit;
1649 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1650 min, max, vr_p->equiv);
1652 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1654 min = TYPE_MIN_VALUE (type);
1656 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1657 max = limit;
1658 else
1660 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1661 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1662 LT_EXPR. */
1663 max = limit_vr->max;
1666 /* If the maximum value forces us to be out of bounds, simply punt.
1667 It would be pointless to try and do anything more since this
1668 all should be optimized away above us. */
1669 if ((cond_code == LT_EXPR
1670 && compare_values (max, min) == 0)
1671 || is_overflow_infinity (max))
1672 set_value_range_to_varying (vr_p);
1673 else
1675 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1676 if (cond_code == LT_EXPR)
1678 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1679 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1680 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1681 build_int_cst (TREE_TYPE (max), -1));
1682 else
1683 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1684 build_int_cst (TREE_TYPE (max), 1));
1685 if (EXPR_P (max))
1686 TREE_NO_WARNING (max) = 1;
1689 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1692 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1694 max = TYPE_MAX_VALUE (type);
1696 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1697 min = limit;
1698 else
1700 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1701 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1702 GT_EXPR. */
1703 min = limit_vr->min;
1706 /* If the minimum value forces us to be out of bounds, simply punt.
1707 It would be pointless to try and do anything more since this
1708 all should be optimized away above us. */
1709 if ((cond_code == GT_EXPR
1710 && compare_values (min, max) == 0)
1711 || is_overflow_infinity (min))
1712 set_value_range_to_varying (vr_p);
1713 else
1715 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1716 if (cond_code == GT_EXPR)
1718 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1719 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1720 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1721 build_int_cst (TREE_TYPE (min), -1));
1722 else
1723 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1724 build_int_cst (TREE_TYPE (min), 1));
1725 if (EXPR_P (min))
1726 TREE_NO_WARNING (min) = 1;
1729 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1732 else
1733 gcc_unreachable ();
1735 /* Finally intersect the new range with what we already know about var. */
1736 vrp_intersect_ranges (vr_p, get_value_range (var));
1740 /* Extract range information from SSA name VAR and store it in VR. If
1741 VAR has an interesting range, use it. Otherwise, create the
1742 range [VAR, VAR] and return it. This is useful in situations where
1743 we may have conditionals testing values of VARYING names. For
1744 instance,
1746 x_3 = y_5;
1747 if (x_3 > y_5)
1750 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1751 always false. */
1753 static void
1754 extract_range_from_ssa_name (value_range *vr, tree var)
1756 value_range *var_vr = get_value_range (var);
1758 if (var_vr->type != VR_VARYING)
1759 copy_value_range (vr, var_vr);
1760 else
1761 set_value_range (vr, VR_RANGE, var, var, NULL);
1763 add_equivalence (&vr->equiv, var);
1767 /* Wrapper around int_const_binop. If the operation overflows and we
1768 are not using wrapping arithmetic, then adjust the result to be
1769 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1770 NULL_TREE if we need to use an overflow infinity representation but
1771 the type does not support it. */
1773 static tree
1774 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1776 tree res;
1778 res = int_const_binop (code, val1, val2);
1780 /* If we are using unsigned arithmetic, operate symbolically
1781 on -INF and +INF as int_const_binop only handles signed overflow. */
1782 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1784 int checkz = compare_values (res, val1);
1785 bool overflow = false;
1787 /* Ensure that res = val1 [+*] val2 >= val1
1788 or that res = val1 - val2 <= val1. */
1789 if ((code == PLUS_EXPR
1790 && !(checkz == 1 || checkz == 0))
1791 || (code == MINUS_EXPR
1792 && !(checkz == 0 || checkz == -1)))
1794 overflow = true;
1796 /* Checking for multiplication overflow is done by dividing the
1797 output of the multiplication by the first input of the
1798 multiplication. If the result of that division operation is
1799 not equal to the second input of the multiplication, then the
1800 multiplication overflowed. */
1801 else if (code == MULT_EXPR && !integer_zerop (val1))
1803 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1804 res,
1805 val1);
1806 int check = compare_values (tmp, val2);
1808 if (check != 0)
1809 overflow = true;
1812 if (overflow)
1814 res = copy_node (res);
1815 TREE_OVERFLOW (res) = 1;
1819 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1820 /* If the singed operation wraps then int_const_binop has done
1821 everything we want. */
1823 /* Signed division of -1/0 overflows and by the time it gets here
1824 returns NULL_TREE. */
1825 else if (!res)
1826 return NULL_TREE;
1827 else if ((TREE_OVERFLOW (res)
1828 && !TREE_OVERFLOW (val1)
1829 && !TREE_OVERFLOW (val2))
1830 || is_overflow_infinity (val1)
1831 || is_overflow_infinity (val2))
1833 /* If the operation overflowed but neither VAL1 nor VAL2 are
1834 overflown, return -INF or +INF depending on the operation
1835 and the combination of signs of the operands. */
1836 int sgn1 = tree_int_cst_sgn (val1);
1837 int sgn2 = tree_int_cst_sgn (val2);
1839 if (needs_overflow_infinity (TREE_TYPE (res))
1840 && !supports_overflow_infinity (TREE_TYPE (res)))
1841 return NULL_TREE;
1843 /* We have to punt on adding infinities of different signs,
1844 since we can't tell what the sign of the result should be.
1845 Likewise for subtracting infinities of the same sign. */
1846 if (((code == PLUS_EXPR && sgn1 != sgn2)
1847 || (code == MINUS_EXPR && sgn1 == sgn2))
1848 && is_overflow_infinity (val1)
1849 && is_overflow_infinity (val2))
1850 return NULL_TREE;
1852 /* Don't try to handle division or shifting of infinities. */
1853 if ((code == TRUNC_DIV_EXPR
1854 || code == FLOOR_DIV_EXPR
1855 || code == CEIL_DIV_EXPR
1856 || code == EXACT_DIV_EXPR
1857 || code == ROUND_DIV_EXPR
1858 || code == RSHIFT_EXPR)
1859 && (is_overflow_infinity (val1)
1860 || is_overflow_infinity (val2)))
1861 return NULL_TREE;
1863 /* Notice that we only need to handle the restricted set of
1864 operations handled by extract_range_from_binary_expr.
1865 Among them, only multiplication, addition and subtraction
1866 can yield overflow without overflown operands because we
1867 are working with integral types only... except in the
1868 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1869 for division too. */
1871 /* For multiplication, the sign of the overflow is given
1872 by the comparison of the signs of the operands. */
1873 if ((code == MULT_EXPR && sgn1 == sgn2)
1874 /* For addition, the operands must be of the same sign
1875 to yield an overflow. Its sign is therefore that
1876 of one of the operands, for example the first. For
1877 infinite operands X + -INF is negative, not positive. */
1878 || (code == PLUS_EXPR
1879 && (sgn1 >= 0
1880 ? !is_negative_overflow_infinity (val2)
1881 : is_positive_overflow_infinity (val2)))
1882 /* For subtraction, non-infinite operands must be of
1883 different signs to yield an overflow. Its sign is
1884 therefore that of the first operand or the opposite of
1885 that of the second operand. A first operand of 0 counts
1886 as positive here, for the corner case 0 - (-INF), which
1887 overflows, but must yield +INF. For infinite operands 0
1888 - INF is negative, not positive. */
1889 || (code == MINUS_EXPR
1890 && (sgn1 >= 0
1891 ? !is_positive_overflow_infinity (val2)
1892 : is_negative_overflow_infinity (val2)))
1893 /* We only get in here with positive shift count, so the
1894 overflow direction is the same as the sign of val1.
1895 Actually rshift does not overflow at all, but we only
1896 handle the case of shifting overflowed -INF and +INF. */
1897 || (code == RSHIFT_EXPR
1898 && sgn1 >= 0)
1899 /* For division, the only case is -INF / -1 = +INF. */
1900 || code == TRUNC_DIV_EXPR
1901 || code == FLOOR_DIV_EXPR
1902 || code == CEIL_DIV_EXPR
1903 || code == EXACT_DIV_EXPR
1904 || code == ROUND_DIV_EXPR)
1905 return (needs_overflow_infinity (TREE_TYPE (res))
1906 ? positive_overflow_infinity (TREE_TYPE (res))
1907 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1908 else
1909 return (needs_overflow_infinity (TREE_TYPE (res))
1910 ? negative_overflow_infinity (TREE_TYPE (res))
1911 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1914 return res;
1918 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1919 bitmask if some bit is unset, it means for all numbers in the range
1920 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1921 bitmask if some bit is set, it means for all numbers in the range
1922 the bit is 1, otherwise it might be 0 or 1. */
1924 static bool
1925 zero_nonzero_bits_from_vr (const tree expr_type,
1926 value_range *vr,
1927 wide_int *may_be_nonzero,
1928 wide_int *must_be_nonzero)
1930 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1931 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1932 if (!range_int_cst_p (vr)
1933 || is_overflow_infinity (vr->min)
1934 || is_overflow_infinity (vr->max))
1935 return false;
1937 if (range_int_cst_singleton_p (vr))
1939 *may_be_nonzero = vr->min;
1940 *must_be_nonzero = *may_be_nonzero;
1942 else if (tree_int_cst_sgn (vr->min) >= 0
1943 || tree_int_cst_sgn (vr->max) < 0)
1945 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1946 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1947 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1948 if (xor_mask != 0)
1950 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1951 may_be_nonzero->get_precision ());
1952 *may_be_nonzero = *may_be_nonzero | mask;
1953 *must_be_nonzero = must_be_nonzero->and_not (mask);
1957 return true;
1960 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1961 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1962 false otherwise. If *AR can be represented with a single range
1963 *VR1 will be VR_UNDEFINED. */
1965 static bool
1966 ranges_from_anti_range (value_range *ar,
1967 value_range *vr0, value_range *vr1)
1969 tree type = TREE_TYPE (ar->min);
1971 vr0->type = VR_UNDEFINED;
1972 vr1->type = VR_UNDEFINED;
1974 if (ar->type != VR_ANTI_RANGE
1975 || TREE_CODE (ar->min) != INTEGER_CST
1976 || TREE_CODE (ar->max) != INTEGER_CST
1977 || !vrp_val_min (type)
1978 || !vrp_val_max (type))
1979 return false;
1981 if (!vrp_val_is_min (ar->min))
1983 vr0->type = VR_RANGE;
1984 vr0->min = vrp_val_min (type);
1985 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
1987 if (!vrp_val_is_max (ar->max))
1989 vr1->type = VR_RANGE;
1990 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
1991 vr1->max = vrp_val_max (type);
1993 if (vr0->type == VR_UNDEFINED)
1995 *vr0 = *vr1;
1996 vr1->type = VR_UNDEFINED;
1999 return vr0->type != VR_UNDEFINED;
2002 /* Helper to extract a value-range *VR for a multiplicative operation
2003 *VR0 CODE *VR1. */
2005 static void
2006 extract_range_from_multiplicative_op_1 (value_range *vr,
2007 enum tree_code code,
2008 value_range *vr0, value_range *vr1)
2010 enum value_range_type type;
2011 tree val[4];
2012 size_t i;
2013 tree min, max;
2014 bool sop;
2015 int cmp;
2017 /* Multiplications, divisions and shifts are a bit tricky to handle,
2018 depending on the mix of signs we have in the two ranges, we
2019 need to operate on different values to get the minimum and
2020 maximum values for the new range. One approach is to figure
2021 out all the variations of range combinations and do the
2022 operations.
2024 However, this involves several calls to compare_values and it
2025 is pretty convoluted. It's simpler to do the 4 operations
2026 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2027 MAX1) and then figure the smallest and largest values to form
2028 the new range. */
2029 gcc_assert (code == MULT_EXPR
2030 || code == TRUNC_DIV_EXPR
2031 || code == FLOOR_DIV_EXPR
2032 || code == CEIL_DIV_EXPR
2033 || code == EXACT_DIV_EXPR
2034 || code == ROUND_DIV_EXPR
2035 || code == RSHIFT_EXPR
2036 || code == LSHIFT_EXPR);
2037 gcc_assert ((vr0->type == VR_RANGE
2038 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2039 && vr0->type == vr1->type);
2041 type = vr0->type;
2043 /* Compute the 4 cross operations. */
2044 sop = false;
2045 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2046 if (val[0] == NULL_TREE)
2047 sop = true;
2049 if (vr1->max == vr1->min)
2050 val[1] = NULL_TREE;
2051 else
2053 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2054 if (val[1] == NULL_TREE)
2055 sop = true;
2058 if (vr0->max == vr0->min)
2059 val[2] = NULL_TREE;
2060 else
2062 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2063 if (val[2] == NULL_TREE)
2064 sop = true;
2067 if (vr0->min == vr0->max || vr1->min == vr1->max)
2068 val[3] = NULL_TREE;
2069 else
2071 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2072 if (val[3] == NULL_TREE)
2073 sop = true;
2076 if (sop)
2078 set_value_range_to_varying (vr);
2079 return;
2082 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2083 of VAL[i]. */
2084 min = val[0];
2085 max = val[0];
2086 for (i = 1; i < 4; i++)
2088 if (!is_gimple_min_invariant (min)
2089 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2090 || !is_gimple_min_invariant (max)
2091 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2092 break;
2094 if (val[i])
2096 if (!is_gimple_min_invariant (val[i])
2097 || (TREE_OVERFLOW (val[i])
2098 && !is_overflow_infinity (val[i])))
2100 /* If we found an overflowed value, set MIN and MAX
2101 to it so that we set the resulting range to
2102 VARYING. */
2103 min = max = val[i];
2104 break;
2107 if (compare_values (val[i], min) == -1)
2108 min = val[i];
2110 if (compare_values (val[i], max) == 1)
2111 max = val[i];
2115 /* If either MIN or MAX overflowed, then set the resulting range to
2116 VARYING. But we do accept an overflow infinity
2117 representation. */
2118 if (min == NULL_TREE
2119 || !is_gimple_min_invariant (min)
2120 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2121 || max == NULL_TREE
2122 || !is_gimple_min_invariant (max)
2123 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2125 set_value_range_to_varying (vr);
2126 return;
2129 /* We punt if:
2130 1) [-INF, +INF]
2131 2) [-INF, +-INF(OVF)]
2132 3) [+-INF(OVF), +INF]
2133 4) [+-INF(OVF), +-INF(OVF)]
2134 We learn nothing when we have INF and INF(OVF) on both sides.
2135 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2136 overflow. */
2137 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2138 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2140 set_value_range_to_varying (vr);
2141 return;
2144 cmp = compare_values (min, max);
2145 if (cmp == -2 || cmp == 1)
2147 /* If the new range has its limits swapped around (MIN > MAX),
2148 then the operation caused one of them to wrap around, mark
2149 the new range VARYING. */
2150 set_value_range_to_varying (vr);
2152 else
2153 set_value_range (vr, type, min, max, NULL);
2156 /* Extract range information from a binary operation CODE based on
2157 the ranges of each of its operands *VR0 and *VR1 with resulting
2158 type EXPR_TYPE. The resulting range is stored in *VR. */
2160 static void
2161 extract_range_from_binary_expr_1 (value_range *vr,
2162 enum tree_code code, tree expr_type,
2163 value_range *vr0_, value_range *vr1_)
2165 value_range vr0 = *vr0_, vr1 = *vr1_;
2166 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2167 enum value_range_type type;
2168 tree min = NULL_TREE, max = NULL_TREE;
2169 int cmp;
2171 if (!INTEGRAL_TYPE_P (expr_type)
2172 && !POINTER_TYPE_P (expr_type))
2174 set_value_range_to_varying (vr);
2175 return;
2178 /* Not all binary expressions can be applied to ranges in a
2179 meaningful way. Handle only arithmetic operations. */
2180 if (code != PLUS_EXPR
2181 && code != MINUS_EXPR
2182 && code != POINTER_PLUS_EXPR
2183 && code != MULT_EXPR
2184 && code != TRUNC_DIV_EXPR
2185 && code != FLOOR_DIV_EXPR
2186 && code != CEIL_DIV_EXPR
2187 && code != EXACT_DIV_EXPR
2188 && code != ROUND_DIV_EXPR
2189 && code != TRUNC_MOD_EXPR
2190 && code != RSHIFT_EXPR
2191 && code != LSHIFT_EXPR
2192 && code != MIN_EXPR
2193 && code != MAX_EXPR
2194 && code != BIT_AND_EXPR
2195 && code != BIT_IOR_EXPR
2196 && code != BIT_XOR_EXPR)
2198 set_value_range_to_varying (vr);
2199 return;
2202 /* If both ranges are UNDEFINED, so is the result. */
2203 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2205 set_value_range_to_undefined (vr);
2206 return;
2208 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2209 code. At some point we may want to special-case operations that
2210 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2211 operand. */
2212 else if (vr0.type == VR_UNDEFINED)
2213 set_value_range_to_varying (&vr0);
2214 else if (vr1.type == VR_UNDEFINED)
2215 set_value_range_to_varying (&vr1);
2217 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2218 and express ~[] op X as ([]' op X) U ([]'' op X). */
2219 if (vr0.type == VR_ANTI_RANGE
2220 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2222 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2223 if (vrtem1.type != VR_UNDEFINED)
2225 value_range vrres = VR_INITIALIZER;
2226 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2227 &vrtem1, vr1_);
2228 vrp_meet (vr, &vrres);
2230 return;
2232 /* Likewise for X op ~[]. */
2233 if (vr1.type == VR_ANTI_RANGE
2234 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2236 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2237 if (vrtem1.type != VR_UNDEFINED)
2239 value_range vrres = VR_INITIALIZER;
2240 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2241 vr0_, &vrtem1);
2242 vrp_meet (vr, &vrres);
2244 return;
2247 /* The type of the resulting value range defaults to VR0.TYPE. */
2248 type = vr0.type;
2250 /* Refuse to operate on VARYING ranges, ranges of different kinds
2251 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2252 because we may be able to derive a useful range even if one of
2253 the operands is VR_VARYING or symbolic range. Similarly for
2254 divisions, MIN/MAX and PLUS/MINUS.
2256 TODO, we may be able to derive anti-ranges in some cases. */
2257 if (code != BIT_AND_EXPR
2258 && code != BIT_IOR_EXPR
2259 && code != TRUNC_DIV_EXPR
2260 && code != FLOOR_DIV_EXPR
2261 && code != CEIL_DIV_EXPR
2262 && code != EXACT_DIV_EXPR
2263 && code != ROUND_DIV_EXPR
2264 && code != TRUNC_MOD_EXPR
2265 && code != MIN_EXPR
2266 && code != MAX_EXPR
2267 && code != PLUS_EXPR
2268 && code != MINUS_EXPR
2269 && code != RSHIFT_EXPR
2270 && (vr0.type == VR_VARYING
2271 || vr1.type == VR_VARYING
2272 || vr0.type != vr1.type
2273 || symbolic_range_p (&vr0)
2274 || symbolic_range_p (&vr1)))
2276 set_value_range_to_varying (vr);
2277 return;
2280 /* Now evaluate the expression to determine the new range. */
2281 if (POINTER_TYPE_P (expr_type))
2283 if (code == MIN_EXPR || code == MAX_EXPR)
2285 /* For MIN/MAX expressions with pointers, we only care about
2286 nullness, if both are non null, then the result is nonnull.
2287 If both are null, then the result is null. Otherwise they
2288 are varying. */
2289 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2290 set_value_range_to_nonnull (vr, expr_type);
2291 else if (range_is_null (&vr0) && range_is_null (&vr1))
2292 set_value_range_to_null (vr, expr_type);
2293 else
2294 set_value_range_to_varying (vr);
2296 else if (code == POINTER_PLUS_EXPR)
2298 /* For pointer types, we are really only interested in asserting
2299 whether the expression evaluates to non-NULL. */
2300 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2301 set_value_range_to_nonnull (vr, expr_type);
2302 else if (range_is_null (&vr0) && range_is_null (&vr1))
2303 set_value_range_to_null (vr, expr_type);
2304 else
2305 set_value_range_to_varying (vr);
2307 else if (code == BIT_AND_EXPR)
2309 /* For pointer types, we are really only interested in asserting
2310 whether the expression evaluates to non-NULL. */
2311 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2312 set_value_range_to_nonnull (vr, expr_type);
2313 else if (range_is_null (&vr0) || range_is_null (&vr1))
2314 set_value_range_to_null (vr, expr_type);
2315 else
2316 set_value_range_to_varying (vr);
2318 else
2319 set_value_range_to_varying (vr);
2321 return;
2324 /* For integer ranges, apply the operation to each end of the
2325 range and see what we end up with. */
2326 if (code == PLUS_EXPR || code == MINUS_EXPR)
2328 const bool minus_p = (code == MINUS_EXPR);
2329 tree min_op0 = vr0.min;
2330 tree min_op1 = minus_p ? vr1.max : vr1.min;
2331 tree max_op0 = vr0.max;
2332 tree max_op1 = minus_p ? vr1.min : vr1.max;
2333 tree sym_min_op0 = NULL_TREE;
2334 tree sym_min_op1 = NULL_TREE;
2335 tree sym_max_op0 = NULL_TREE;
2336 tree sym_max_op1 = NULL_TREE;
2337 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2339 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2340 single-symbolic ranges, try to compute the precise resulting range,
2341 but only if we know that this resulting range will also be constant
2342 or single-symbolic. */
2343 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2344 && (TREE_CODE (min_op0) == INTEGER_CST
2345 || (sym_min_op0
2346 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2347 && (TREE_CODE (min_op1) == INTEGER_CST
2348 || (sym_min_op1
2349 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2350 && (!(sym_min_op0 && sym_min_op1)
2351 || (sym_min_op0 == sym_min_op1
2352 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2353 && (TREE_CODE (max_op0) == INTEGER_CST
2354 || (sym_max_op0
2355 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2356 && (TREE_CODE (max_op1) == INTEGER_CST
2357 || (sym_max_op1
2358 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2359 && (!(sym_max_op0 && sym_max_op1)
2360 || (sym_max_op0 == sym_max_op1
2361 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2363 const signop sgn = TYPE_SIGN (expr_type);
2364 const unsigned int prec = TYPE_PRECISION (expr_type);
2365 wide_int type_min, type_max, wmin, wmax;
2366 int min_ovf = 0;
2367 int max_ovf = 0;
2369 /* Get the lower and upper bounds of the type. */
2370 if (TYPE_OVERFLOW_WRAPS (expr_type))
2372 type_min = wi::min_value (prec, sgn);
2373 type_max = wi::max_value (prec, sgn);
2375 else
2377 type_min = vrp_val_min (expr_type);
2378 type_max = vrp_val_max (expr_type);
2381 /* Combine the lower bounds, if any. */
2382 if (min_op0 && min_op1)
2384 if (minus_p)
2386 wmin = wi::sub (min_op0, min_op1);
2388 /* Check for overflow. */
2389 if (wi::cmp (0, min_op1, sgn)
2390 != wi::cmp (wmin, min_op0, sgn))
2391 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2393 else
2395 wmin = wi::add (min_op0, min_op1);
2397 /* Check for overflow. */
2398 if (wi::cmp (min_op1, 0, sgn)
2399 != wi::cmp (wmin, min_op0, sgn))
2400 min_ovf = wi::cmp (min_op0, wmin, sgn);
2403 else if (min_op0)
2404 wmin = min_op0;
2405 else if (min_op1)
2406 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2407 else
2408 wmin = wi::shwi (0, prec);
2410 /* Combine the upper bounds, if any. */
2411 if (max_op0 && max_op1)
2413 if (minus_p)
2415 wmax = wi::sub (max_op0, max_op1);
2417 /* Check for overflow. */
2418 if (wi::cmp (0, max_op1, sgn)
2419 != wi::cmp (wmax, max_op0, sgn))
2420 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2422 else
2424 wmax = wi::add (max_op0, max_op1);
2426 if (wi::cmp (max_op1, 0, sgn)
2427 != wi::cmp (wmax, max_op0, sgn))
2428 max_ovf = wi::cmp (max_op0, wmax, sgn);
2431 else if (max_op0)
2432 wmax = max_op0;
2433 else if (max_op1)
2434 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2435 else
2436 wmax = wi::shwi (0, prec);
2438 /* Check for type overflow. */
2439 if (min_ovf == 0)
2441 if (wi::cmp (wmin, type_min, sgn) == -1)
2442 min_ovf = -1;
2443 else if (wi::cmp (wmin, type_max, sgn) == 1)
2444 min_ovf = 1;
2446 if (max_ovf == 0)
2448 if (wi::cmp (wmax, type_min, sgn) == -1)
2449 max_ovf = -1;
2450 else if (wi::cmp (wmax, type_max, sgn) == 1)
2451 max_ovf = 1;
2454 /* If we have overflow for the constant part and the resulting
2455 range will be symbolic, drop to VR_VARYING. */
2456 if ((min_ovf && sym_min_op0 != sym_min_op1)
2457 || (max_ovf && sym_max_op0 != sym_max_op1))
2459 set_value_range_to_varying (vr);
2460 return;
2463 if (TYPE_OVERFLOW_WRAPS (expr_type))
2465 /* If overflow wraps, truncate the values and adjust the
2466 range kind and bounds appropriately. */
2467 wide_int tmin = wide_int::from (wmin, prec, sgn);
2468 wide_int tmax = wide_int::from (wmax, prec, sgn);
2469 if (min_ovf == max_ovf)
2471 /* No overflow or both overflow or underflow. The
2472 range kind stays VR_RANGE. */
2473 min = wide_int_to_tree (expr_type, tmin);
2474 max = wide_int_to_tree (expr_type, tmax);
2476 else if ((min_ovf == -1 && max_ovf == 0)
2477 || (max_ovf == 1 && min_ovf == 0))
2479 /* Min underflow or max overflow. The range kind
2480 changes to VR_ANTI_RANGE. */
2481 bool covers = false;
2482 wide_int tem = tmin;
2483 type = VR_ANTI_RANGE;
2484 tmin = tmax + 1;
2485 if (wi::cmp (tmin, tmax, sgn) < 0)
2486 covers = true;
2487 tmax = tem - 1;
2488 if (wi::cmp (tmax, tem, sgn) > 0)
2489 covers = true;
2490 /* If the anti-range would cover nothing, drop to varying.
2491 Likewise if the anti-range bounds are outside of the
2492 types values. */
2493 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2495 set_value_range_to_varying (vr);
2496 return;
2498 min = wide_int_to_tree (expr_type, tmin);
2499 max = wide_int_to_tree (expr_type, tmax);
2501 else
2503 /* Other underflow and/or overflow, drop to VR_VARYING. */
2504 set_value_range_to_varying (vr);
2505 return;
2508 else
2510 /* If overflow does not wrap, saturate to the types min/max
2511 value. */
2512 if (min_ovf == -1)
2514 if (needs_overflow_infinity (expr_type)
2515 && supports_overflow_infinity (expr_type))
2516 min = negative_overflow_infinity (expr_type);
2517 else
2518 min = wide_int_to_tree (expr_type, type_min);
2520 else if (min_ovf == 1)
2522 if (needs_overflow_infinity (expr_type)
2523 && supports_overflow_infinity (expr_type))
2524 min = positive_overflow_infinity (expr_type);
2525 else
2526 min = wide_int_to_tree (expr_type, type_max);
2528 else
2529 min = wide_int_to_tree (expr_type, wmin);
2531 if (max_ovf == -1)
2533 if (needs_overflow_infinity (expr_type)
2534 && supports_overflow_infinity (expr_type))
2535 max = negative_overflow_infinity (expr_type);
2536 else
2537 max = wide_int_to_tree (expr_type, type_min);
2539 else if (max_ovf == 1)
2541 if (needs_overflow_infinity (expr_type)
2542 && supports_overflow_infinity (expr_type))
2543 max = positive_overflow_infinity (expr_type);
2544 else
2545 max = wide_int_to_tree (expr_type, type_max);
2547 else
2548 max = wide_int_to_tree (expr_type, wmax);
2551 if (needs_overflow_infinity (expr_type)
2552 && supports_overflow_infinity (expr_type))
2554 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2555 || (min_op1
2556 && (minus_p
2557 ? is_positive_overflow_infinity (min_op1)
2558 : is_negative_overflow_infinity (min_op1))))
2559 min = negative_overflow_infinity (expr_type);
2560 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2561 || (max_op1
2562 && (minus_p
2563 ? is_negative_overflow_infinity (max_op1)
2564 : is_positive_overflow_infinity (max_op1))))
2565 max = positive_overflow_infinity (expr_type);
2568 /* If the result lower bound is constant, we're done;
2569 otherwise, build the symbolic lower bound. */
2570 if (sym_min_op0 == sym_min_op1)
2572 else if (sym_min_op0)
2573 min = build_symbolic_expr (expr_type, sym_min_op0,
2574 neg_min_op0, min);
2575 else if (sym_min_op1)
2576 min = build_symbolic_expr (expr_type, sym_min_op1,
2577 neg_min_op1 ^ minus_p, min);
2579 /* Likewise for the upper bound. */
2580 if (sym_max_op0 == sym_max_op1)
2582 else if (sym_max_op0)
2583 max = build_symbolic_expr (expr_type, sym_max_op0,
2584 neg_max_op0, max);
2585 else if (sym_max_op1)
2586 max = build_symbolic_expr (expr_type, sym_max_op1,
2587 neg_max_op1 ^ minus_p, max);
2589 else
2591 /* For other cases, for example if we have a PLUS_EXPR with two
2592 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2593 to compute a precise range for such a case.
2594 ??? General even mixed range kind operations can be expressed
2595 by for example transforming ~[3, 5] + [1, 2] to range-only
2596 operations and a union primitive:
2597 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2598 [-INF+1, 4] U [6, +INF(OVF)]
2599 though usually the union is not exactly representable with
2600 a single range or anti-range as the above is
2601 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2602 but one could use a scheme similar to equivalences for this. */
2603 set_value_range_to_varying (vr);
2604 return;
2607 else if (code == MIN_EXPR
2608 || code == MAX_EXPR)
2610 if (vr0.type == VR_RANGE
2611 && !symbolic_range_p (&vr0))
2613 type = VR_RANGE;
2614 if (vr1.type == VR_RANGE
2615 && !symbolic_range_p (&vr1))
2617 /* For operations that make the resulting range directly
2618 proportional to the original ranges, apply the operation to
2619 the same end of each range. */
2620 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2621 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2623 else if (code == MIN_EXPR)
2625 min = vrp_val_min (expr_type);
2626 max = vr0.max;
2628 else if (code == MAX_EXPR)
2630 min = vr0.min;
2631 max = vrp_val_max (expr_type);
2634 else if (vr1.type == VR_RANGE
2635 && !symbolic_range_p (&vr1))
2637 type = VR_RANGE;
2638 if (code == MIN_EXPR)
2640 min = vrp_val_min (expr_type);
2641 max = vr1.max;
2643 else if (code == MAX_EXPR)
2645 min = vr1.min;
2646 max = vrp_val_max (expr_type);
2649 else
2651 set_value_range_to_varying (vr);
2652 return;
2655 else if (code == MULT_EXPR)
2657 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2658 drop to varying. This test requires 2*prec bits if both
2659 operands are signed and 2*prec + 2 bits if either is not. */
2661 signop sign = TYPE_SIGN (expr_type);
2662 unsigned int prec = TYPE_PRECISION (expr_type);
2664 if (range_int_cst_p (&vr0)
2665 && range_int_cst_p (&vr1)
2666 && TYPE_OVERFLOW_WRAPS (expr_type))
2668 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2669 typedef generic_wide_int
2670 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2671 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2672 vrp_int size = sizem1 + 1;
2674 /* Extend the values using the sign of the result to PREC2.
2675 From here on out, everthing is just signed math no matter
2676 what the input types were. */
2677 vrp_int min0 = vrp_int_cst (vr0.min);
2678 vrp_int max0 = vrp_int_cst (vr0.max);
2679 vrp_int min1 = vrp_int_cst (vr1.min);
2680 vrp_int max1 = vrp_int_cst (vr1.max);
2681 /* Canonicalize the intervals. */
2682 if (sign == UNSIGNED)
2684 if (wi::ltu_p (size, min0 + max0))
2686 min0 -= size;
2687 max0 -= size;
2690 if (wi::ltu_p (size, min1 + max1))
2692 min1 -= size;
2693 max1 -= size;
2697 vrp_int prod0 = min0 * min1;
2698 vrp_int prod1 = min0 * max1;
2699 vrp_int prod2 = max0 * min1;
2700 vrp_int prod3 = max0 * max1;
2702 /* Sort the 4 products so that min is in prod0 and max is in
2703 prod3. */
2704 /* min0min1 > max0max1 */
2705 if (prod0 > prod3)
2706 std::swap (prod0, prod3);
2708 /* min0max1 > max0min1 */
2709 if (prod1 > prod2)
2710 std::swap (prod1, prod2);
2712 if (prod0 > prod1)
2713 std::swap (prod0, prod1);
2715 if (prod2 > prod3)
2716 std::swap (prod2, prod3);
2718 /* diff = max - min. */
2719 prod2 = prod3 - prod0;
2720 if (wi::geu_p (prod2, sizem1))
2722 /* the range covers all values. */
2723 set_value_range_to_varying (vr);
2724 return;
2727 /* The following should handle the wrapping and selecting
2728 VR_ANTI_RANGE for us. */
2729 min = wide_int_to_tree (expr_type, prod0);
2730 max = wide_int_to_tree (expr_type, prod3);
2731 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2732 return;
2735 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2736 drop to VR_VARYING. It would take more effort to compute a
2737 precise range for such a case. For example, if we have
2738 op0 == 65536 and op1 == 65536 with their ranges both being
2739 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2740 we cannot claim that the product is in ~[0,0]. Note that we
2741 are guaranteed to have vr0.type == vr1.type at this
2742 point. */
2743 if (vr0.type == VR_ANTI_RANGE
2744 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2746 set_value_range_to_varying (vr);
2747 return;
2750 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2751 return;
2753 else if (code == RSHIFT_EXPR
2754 || code == LSHIFT_EXPR)
2756 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2757 then drop to VR_VARYING. Outside of this range we get undefined
2758 behavior from the shift operation. We cannot even trust
2759 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2760 shifts, and the operation at the tree level may be widened. */
2761 if (range_int_cst_p (&vr1)
2762 && compare_tree_int (vr1.min, 0) >= 0
2763 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2765 if (code == RSHIFT_EXPR)
2767 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2768 useful ranges just from the shift count. E.g.
2769 x >> 63 for signed 64-bit x is always [-1, 0]. */
2770 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2772 vr0.type = type = VR_RANGE;
2773 vr0.min = vrp_val_min (expr_type);
2774 vr0.max = vrp_val_max (expr_type);
2776 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2777 return;
2779 /* We can map lshifts by constants to MULT_EXPR handling. */
2780 else if (code == LSHIFT_EXPR
2781 && range_int_cst_singleton_p (&vr1))
2783 bool saved_flag_wrapv;
2784 value_range vr1p = VR_INITIALIZER;
2785 vr1p.type = VR_RANGE;
2786 vr1p.min = (wide_int_to_tree
2787 (expr_type,
2788 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2789 TYPE_PRECISION (expr_type))));
2790 vr1p.max = vr1p.min;
2791 /* We have to use a wrapping multiply though as signed overflow
2792 on lshifts is implementation defined in C89. */
2793 saved_flag_wrapv = flag_wrapv;
2794 flag_wrapv = 1;
2795 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2796 &vr0, &vr1p);
2797 flag_wrapv = saved_flag_wrapv;
2798 return;
2800 else if (code == LSHIFT_EXPR
2801 && range_int_cst_p (&vr0))
2803 int prec = TYPE_PRECISION (expr_type);
2804 int overflow_pos = prec;
2805 int bound_shift;
2806 wide_int low_bound, high_bound;
2807 bool uns = TYPE_UNSIGNED (expr_type);
2808 bool in_bounds = false;
2810 if (!uns)
2811 overflow_pos -= 1;
2813 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2814 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2815 overflow. However, for that to happen, vr1.max needs to be
2816 zero, which means vr1 is a singleton range of zero, which
2817 means it should be handled by the previous LSHIFT_EXPR
2818 if-clause. */
2819 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2820 wide_int complement = ~(bound - 1);
2822 if (uns)
2824 low_bound = bound;
2825 high_bound = complement;
2826 if (wi::ltu_p (vr0.max, low_bound))
2828 /* [5, 6] << [1, 2] == [10, 24]. */
2829 /* We're shifting out only zeroes, the value increases
2830 monotonically. */
2831 in_bounds = true;
2833 else if (wi::ltu_p (high_bound, vr0.min))
2835 /* [0xffffff00, 0xffffffff] << [1, 2]
2836 == [0xfffffc00, 0xfffffffe]. */
2837 /* We're shifting out only ones, the value decreases
2838 monotonically. */
2839 in_bounds = true;
2842 else
2844 /* [-1, 1] << [1, 2] == [-4, 4]. */
2845 low_bound = complement;
2846 high_bound = bound;
2847 if (wi::lts_p (vr0.max, high_bound)
2848 && wi::lts_p (low_bound, vr0.min))
2850 /* For non-negative numbers, we're shifting out only
2851 zeroes, the value increases monotonically.
2852 For negative numbers, we're shifting out only ones, the
2853 value decreases monotomically. */
2854 in_bounds = true;
2858 if (in_bounds)
2860 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2861 return;
2865 set_value_range_to_varying (vr);
2866 return;
2868 else if (code == TRUNC_DIV_EXPR
2869 || code == FLOOR_DIV_EXPR
2870 || code == CEIL_DIV_EXPR
2871 || code == EXACT_DIV_EXPR
2872 || code == ROUND_DIV_EXPR)
2874 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2876 /* For division, if op1 has VR_RANGE but op0 does not, something
2877 can be deduced just from that range. Say [min, max] / [4, max]
2878 gives [min / 4, max / 4] range. */
2879 if (vr1.type == VR_RANGE
2880 && !symbolic_range_p (&vr1)
2881 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2883 vr0.type = type = VR_RANGE;
2884 vr0.min = vrp_val_min (expr_type);
2885 vr0.max = vrp_val_max (expr_type);
2887 else
2889 set_value_range_to_varying (vr);
2890 return;
2894 /* For divisions, if flag_non_call_exceptions is true, we must
2895 not eliminate a division by zero. */
2896 if (cfun->can_throw_non_call_exceptions
2897 && (vr1.type != VR_RANGE
2898 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2900 set_value_range_to_varying (vr);
2901 return;
2904 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2905 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2906 include 0. */
2907 if (vr0.type == VR_RANGE
2908 && (vr1.type != VR_RANGE
2909 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2911 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2912 int cmp;
2914 min = NULL_TREE;
2915 max = NULL_TREE;
2916 if (TYPE_UNSIGNED (expr_type)
2917 || value_range_nonnegative_p (&vr1))
2919 /* For unsigned division or when divisor is known
2920 to be non-negative, the range has to cover
2921 all numbers from 0 to max for positive max
2922 and all numbers from min to 0 for negative min. */
2923 cmp = compare_values (vr0.max, zero);
2924 if (cmp == -1)
2926 /* When vr0.max < 0, vr1.min != 0 and value
2927 ranges for dividend and divisor are available. */
2928 if (vr1.type == VR_RANGE
2929 && !symbolic_range_p (&vr0)
2930 && !symbolic_range_p (&vr1)
2931 && compare_values (vr1.min, zero) != 0)
2932 max = int_const_binop (code, vr0.max, vr1.min);
2933 else
2934 max = zero;
2936 else if (cmp == 0 || cmp == 1)
2937 max = vr0.max;
2938 else
2939 type = VR_VARYING;
2940 cmp = compare_values (vr0.min, zero);
2941 if (cmp == 1)
2943 /* For unsigned division when value ranges for dividend
2944 and divisor are available. */
2945 if (vr1.type == VR_RANGE
2946 && !symbolic_range_p (&vr0)
2947 && !symbolic_range_p (&vr1)
2948 && compare_values (vr1.max, zero) != 0)
2949 min = int_const_binop (code, vr0.min, vr1.max);
2950 else
2951 min = zero;
2953 else if (cmp == 0 || cmp == -1)
2954 min = vr0.min;
2955 else
2956 type = VR_VARYING;
2958 else
2960 /* Otherwise the range is -max .. max or min .. -min
2961 depending on which bound is bigger in absolute value,
2962 as the division can change the sign. */
2963 abs_extent_range (vr, vr0.min, vr0.max);
2964 return;
2966 if (type == VR_VARYING)
2968 set_value_range_to_varying (vr);
2969 return;
2972 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
2974 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2975 return;
2978 else if (code == TRUNC_MOD_EXPR)
2980 if (range_is_null (&vr1))
2982 set_value_range_to_undefined (vr);
2983 return;
2985 /* ABS (A % B) < ABS (B) and either
2986 0 <= A % B <= A or A <= A % B <= 0. */
2987 type = VR_RANGE;
2988 signop sgn = TYPE_SIGN (expr_type);
2989 unsigned int prec = TYPE_PRECISION (expr_type);
2990 wide_int wmin, wmax, tmp;
2991 wide_int zero = wi::zero (prec);
2992 wide_int one = wi::one (prec);
2993 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
2995 wmax = wi::sub (vr1.max, one);
2996 if (sgn == SIGNED)
2998 tmp = wi::sub (wi::minus_one (prec), vr1.min);
2999 wmax = wi::smax (wmax, tmp);
3002 else
3004 wmax = wi::max_value (prec, sgn);
3005 /* X % INT_MIN may be INT_MAX. */
3006 if (sgn == UNSIGNED)
3007 wmax = wmax - one;
3010 if (sgn == UNSIGNED)
3011 wmin = zero;
3012 else
3014 wmin = -wmax;
3015 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3017 tmp = vr0.min;
3018 if (wi::gts_p (tmp, zero))
3019 tmp = zero;
3020 wmin = wi::smax (wmin, tmp);
3024 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3026 tmp = vr0.max;
3027 if (sgn == SIGNED && wi::neg_p (tmp))
3028 tmp = zero;
3029 wmax = wi::min (wmax, tmp, sgn);
3032 min = wide_int_to_tree (expr_type, wmin);
3033 max = wide_int_to_tree (expr_type, wmax);
3035 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3037 bool int_cst_range0, int_cst_range1;
3038 wide_int may_be_nonzero0, may_be_nonzero1;
3039 wide_int must_be_nonzero0, must_be_nonzero1;
3041 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3042 &may_be_nonzero0,
3043 &must_be_nonzero0);
3044 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3045 &may_be_nonzero1,
3046 &must_be_nonzero1);
3048 type = VR_RANGE;
3049 if (code == BIT_AND_EXPR)
3051 min = wide_int_to_tree (expr_type,
3052 must_be_nonzero0 & must_be_nonzero1);
3053 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3054 /* If both input ranges contain only negative values we can
3055 truncate the result range maximum to the minimum of the
3056 input range maxima. */
3057 if (int_cst_range0 && int_cst_range1
3058 && tree_int_cst_sgn (vr0.max) < 0
3059 && tree_int_cst_sgn (vr1.max) < 0)
3061 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3062 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3064 /* If either input range contains only non-negative values
3065 we can truncate the result range maximum to the respective
3066 maximum of the input range. */
3067 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3068 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3069 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3070 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3071 max = wide_int_to_tree (expr_type, wmax);
3072 cmp = compare_values (min, max);
3073 /* PR68217: In case of signed & sign-bit-CST should
3074 result in [-INF, 0] instead of [-INF, INF]. */
3075 if (cmp == -2 || cmp == 1)
3077 wide_int sign_bit
3078 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
3079 TYPE_PRECISION (expr_type));
3080 if (!TYPE_UNSIGNED (expr_type)
3081 && ((value_range_constant_singleton (&vr0)
3082 && !wi::cmps (vr0.min, sign_bit))
3083 || (value_range_constant_singleton (&vr1)
3084 && !wi::cmps (vr1.min, sign_bit))))
3086 min = TYPE_MIN_VALUE (expr_type);
3087 max = build_int_cst (expr_type, 0);
3091 else if (code == BIT_IOR_EXPR)
3093 max = wide_int_to_tree (expr_type,
3094 may_be_nonzero0 | may_be_nonzero1);
3095 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3096 /* If the input ranges contain only positive values we can
3097 truncate the minimum of the result range to the maximum
3098 of the input range minima. */
3099 if (int_cst_range0 && int_cst_range1
3100 && tree_int_cst_sgn (vr0.min) >= 0
3101 && tree_int_cst_sgn (vr1.min) >= 0)
3103 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3104 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3106 /* If either input range contains only negative values
3107 we can truncate the minimum of the result range to the
3108 respective minimum range. */
3109 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3110 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3111 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3112 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3113 min = wide_int_to_tree (expr_type, wmin);
3115 else if (code == BIT_XOR_EXPR)
3117 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3118 | ~(may_be_nonzero0 | may_be_nonzero1));
3119 wide_int result_one_bits
3120 = (must_be_nonzero0.and_not (may_be_nonzero1)
3121 | must_be_nonzero1.and_not (may_be_nonzero0));
3122 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3123 min = wide_int_to_tree (expr_type, result_one_bits);
3124 /* If the range has all positive or all negative values the
3125 result is better than VARYING. */
3126 if (tree_int_cst_sgn (min) < 0
3127 || tree_int_cst_sgn (max) >= 0)
3129 else
3130 max = min = NULL_TREE;
3133 else
3134 gcc_unreachable ();
3136 /* If either MIN or MAX overflowed, then set the resulting range to
3137 VARYING. But we do accept an overflow infinity representation. */
3138 if (min == NULL_TREE
3139 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3140 || max == NULL_TREE
3141 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3143 set_value_range_to_varying (vr);
3144 return;
3147 /* We punt if:
3148 1) [-INF, +INF]
3149 2) [-INF, +-INF(OVF)]
3150 3) [+-INF(OVF), +INF]
3151 4) [+-INF(OVF), +-INF(OVF)]
3152 We learn nothing when we have INF and INF(OVF) on both sides.
3153 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3154 overflow. */
3155 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3156 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3158 set_value_range_to_varying (vr);
3159 return;
3162 cmp = compare_values (min, max);
3163 if (cmp == -2 || cmp == 1)
3165 /* If the new range has its limits swapped around (MIN > MAX),
3166 then the operation caused one of them to wrap around, mark
3167 the new range VARYING. */
3168 set_value_range_to_varying (vr);
3170 else
3171 set_value_range (vr, type, min, max, NULL);
3174 /* Extract range information from a binary expression OP0 CODE OP1 based on
3175 the ranges of each of its operands with resulting type EXPR_TYPE.
3176 The resulting range is stored in *VR. */
3178 static void
3179 extract_range_from_binary_expr (value_range *vr,
3180 enum tree_code code,
3181 tree expr_type, tree op0, tree op1)
3183 value_range vr0 = VR_INITIALIZER;
3184 value_range vr1 = VR_INITIALIZER;
3186 /* Get value ranges for each operand. For constant operands, create
3187 a new value range with the operand to simplify processing. */
3188 if (TREE_CODE (op0) == SSA_NAME)
3189 vr0 = *(get_value_range (op0));
3190 else if (is_gimple_min_invariant (op0))
3191 set_value_range_to_value (&vr0, op0, NULL);
3192 else
3193 set_value_range_to_varying (&vr0);
3195 if (TREE_CODE (op1) == SSA_NAME)
3196 vr1 = *(get_value_range (op1));
3197 else if (is_gimple_min_invariant (op1))
3198 set_value_range_to_value (&vr1, op1, NULL);
3199 else
3200 set_value_range_to_varying (&vr1);
3202 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3204 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3205 and based on the other operand, for example if it was deduced from a
3206 symbolic comparison. When a bound of the range of the first operand
3207 is invariant, we set the corresponding bound of the new range to INF
3208 in order to avoid recursing on the range of the second operand. */
3209 if (vr->type == VR_VARYING
3210 && (code == PLUS_EXPR || code == MINUS_EXPR)
3211 && TREE_CODE (op1) == SSA_NAME
3212 && vr0.type == VR_RANGE
3213 && symbolic_range_based_on_p (&vr0, op1))
3215 const bool minus_p = (code == MINUS_EXPR);
3216 value_range n_vr1 = VR_INITIALIZER;
3218 /* Try with VR0 and [-INF, OP1]. */
3219 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3220 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3222 /* Try with VR0 and [OP1, +INF]. */
3223 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3224 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3226 /* Try with VR0 and [OP1, OP1]. */
3227 else
3228 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3230 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3233 if (vr->type == VR_VARYING
3234 && (code == PLUS_EXPR || code == MINUS_EXPR)
3235 && TREE_CODE (op0) == SSA_NAME
3236 && vr1.type == VR_RANGE
3237 && symbolic_range_based_on_p (&vr1, op0))
3239 const bool minus_p = (code == MINUS_EXPR);
3240 value_range n_vr0 = VR_INITIALIZER;
3242 /* Try with [-INF, OP0] and VR1. */
3243 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3244 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3246 /* Try with [OP0, +INF] and VR1. */
3247 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3248 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3250 /* Try with [OP0, OP0] and VR1. */
3251 else
3252 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3254 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3258 /* Extract range information from a unary operation CODE based on
3259 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3260 The resulting range is stored in *VR. */
3262 static void
3263 extract_range_from_unary_expr_1 (value_range *vr,
3264 enum tree_code code, tree type,
3265 value_range *vr0_, tree op0_type)
3267 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3269 /* VRP only operates on integral and pointer types. */
3270 if (!(INTEGRAL_TYPE_P (op0_type)
3271 || POINTER_TYPE_P (op0_type))
3272 || !(INTEGRAL_TYPE_P (type)
3273 || POINTER_TYPE_P (type)))
3275 set_value_range_to_varying (vr);
3276 return;
3279 /* If VR0 is UNDEFINED, so is the result. */
3280 if (vr0.type == VR_UNDEFINED)
3282 set_value_range_to_undefined (vr);
3283 return;
3286 /* Handle operations that we express in terms of others. */
3287 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3289 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3290 copy_value_range (vr, &vr0);
3291 return;
3293 else if (code == NEGATE_EXPR)
3295 /* -X is simply 0 - X, so re-use existing code that also handles
3296 anti-ranges fine. */
3297 value_range zero = VR_INITIALIZER;
3298 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3299 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3300 return;
3302 else if (code == BIT_NOT_EXPR)
3304 /* ~X is simply -1 - X, so re-use existing code that also handles
3305 anti-ranges fine. */
3306 value_range minusone = VR_INITIALIZER;
3307 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3308 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3309 type, &minusone, &vr0);
3310 return;
3313 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3314 and express op ~[] as (op []') U (op []''). */
3315 if (vr0.type == VR_ANTI_RANGE
3316 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3318 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3319 if (vrtem1.type != VR_UNDEFINED)
3321 value_range vrres = VR_INITIALIZER;
3322 extract_range_from_unary_expr_1 (&vrres, code, type,
3323 &vrtem1, op0_type);
3324 vrp_meet (vr, &vrres);
3326 return;
3329 if (CONVERT_EXPR_CODE_P (code))
3331 tree inner_type = op0_type;
3332 tree outer_type = type;
3334 /* If the expression evaluates to a pointer, we are only interested in
3335 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3336 if (POINTER_TYPE_P (type))
3338 if (range_is_nonnull (&vr0))
3339 set_value_range_to_nonnull (vr, type);
3340 else if (range_is_null (&vr0))
3341 set_value_range_to_null (vr, type);
3342 else
3343 set_value_range_to_varying (vr);
3344 return;
3347 /* If VR0 is varying and we increase the type precision, assume
3348 a full range for the following transformation. */
3349 if (vr0.type == VR_VARYING
3350 && INTEGRAL_TYPE_P (inner_type)
3351 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3353 vr0.type = VR_RANGE;
3354 vr0.min = TYPE_MIN_VALUE (inner_type);
3355 vr0.max = TYPE_MAX_VALUE (inner_type);
3358 /* If VR0 is a constant range or anti-range and the conversion is
3359 not truncating we can convert the min and max values and
3360 canonicalize the resulting range. Otherwise we can do the
3361 conversion if the size of the range is less than what the
3362 precision of the target type can represent and the range is
3363 not an anti-range. */
3364 if ((vr0.type == VR_RANGE
3365 || vr0.type == VR_ANTI_RANGE)
3366 && TREE_CODE (vr0.min) == INTEGER_CST
3367 && TREE_CODE (vr0.max) == INTEGER_CST
3368 && (!is_overflow_infinity (vr0.min)
3369 || (vr0.type == VR_RANGE
3370 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3371 && needs_overflow_infinity (outer_type)
3372 && supports_overflow_infinity (outer_type)))
3373 && (!is_overflow_infinity (vr0.max)
3374 || (vr0.type == VR_RANGE
3375 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3376 && needs_overflow_infinity (outer_type)
3377 && supports_overflow_infinity (outer_type)))
3378 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3379 || (vr0.type == VR_RANGE
3380 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3381 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3382 size_int (TYPE_PRECISION (outer_type)))))))
3384 tree new_min, new_max;
3385 if (is_overflow_infinity (vr0.min))
3386 new_min = negative_overflow_infinity (outer_type);
3387 else
3388 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3389 0, false);
3390 if (is_overflow_infinity (vr0.max))
3391 new_max = positive_overflow_infinity (outer_type);
3392 else
3393 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3394 0, false);
3395 set_and_canonicalize_value_range (vr, vr0.type,
3396 new_min, new_max, NULL);
3397 return;
3400 set_value_range_to_varying (vr);
3401 return;
3403 else if (code == ABS_EXPR)
3405 tree min, max;
3406 int cmp;
3408 /* Pass through vr0 in the easy cases. */
3409 if (TYPE_UNSIGNED (type)
3410 || value_range_nonnegative_p (&vr0))
3412 copy_value_range (vr, &vr0);
3413 return;
3416 /* For the remaining varying or symbolic ranges we can't do anything
3417 useful. */
3418 if (vr0.type == VR_VARYING
3419 || symbolic_range_p (&vr0))
3421 set_value_range_to_varying (vr);
3422 return;
3425 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3426 useful range. */
3427 if (!TYPE_OVERFLOW_UNDEFINED (type)
3428 && ((vr0.type == VR_RANGE
3429 && vrp_val_is_min (vr0.min))
3430 || (vr0.type == VR_ANTI_RANGE
3431 && !vrp_val_is_min (vr0.min))))
3433 set_value_range_to_varying (vr);
3434 return;
3437 /* ABS_EXPR may flip the range around, if the original range
3438 included negative values. */
3439 if (is_overflow_infinity (vr0.min))
3440 min = positive_overflow_infinity (type);
3441 else if (!vrp_val_is_min (vr0.min))
3442 min = fold_unary_to_constant (code, type, vr0.min);
3443 else if (!needs_overflow_infinity (type))
3444 min = TYPE_MAX_VALUE (type);
3445 else if (supports_overflow_infinity (type))
3446 min = positive_overflow_infinity (type);
3447 else
3449 set_value_range_to_varying (vr);
3450 return;
3453 if (is_overflow_infinity (vr0.max))
3454 max = positive_overflow_infinity (type);
3455 else if (!vrp_val_is_min (vr0.max))
3456 max = fold_unary_to_constant (code, type, vr0.max);
3457 else if (!needs_overflow_infinity (type))
3458 max = TYPE_MAX_VALUE (type);
3459 else if (supports_overflow_infinity (type)
3460 /* We shouldn't generate [+INF, +INF] as set_value_range
3461 doesn't like this and ICEs. */
3462 && !is_positive_overflow_infinity (min))
3463 max = positive_overflow_infinity (type);
3464 else
3466 set_value_range_to_varying (vr);
3467 return;
3470 cmp = compare_values (min, max);
3472 /* If a VR_ANTI_RANGEs contains zero, then we have
3473 ~[-INF, min(MIN, MAX)]. */
3474 if (vr0.type == VR_ANTI_RANGE)
3476 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3478 /* Take the lower of the two values. */
3479 if (cmp != 1)
3480 max = min;
3482 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3483 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3484 flag_wrapv is set and the original anti-range doesn't include
3485 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3486 if (TYPE_OVERFLOW_WRAPS (type))
3488 tree type_min_value = TYPE_MIN_VALUE (type);
3490 min = (vr0.min != type_min_value
3491 ? int_const_binop (PLUS_EXPR, type_min_value,
3492 build_int_cst (TREE_TYPE (type_min_value), 1))
3493 : type_min_value);
3495 else
3497 if (overflow_infinity_range_p (&vr0))
3498 min = negative_overflow_infinity (type);
3499 else
3500 min = TYPE_MIN_VALUE (type);
3503 else
3505 /* All else has failed, so create the range [0, INF], even for
3506 flag_wrapv since TYPE_MIN_VALUE is in the original
3507 anti-range. */
3508 vr0.type = VR_RANGE;
3509 min = build_int_cst (type, 0);
3510 if (needs_overflow_infinity (type))
3512 if (supports_overflow_infinity (type))
3513 max = positive_overflow_infinity (type);
3514 else
3516 set_value_range_to_varying (vr);
3517 return;
3520 else
3521 max = TYPE_MAX_VALUE (type);
3525 /* If the range contains zero then we know that the minimum value in the
3526 range will be zero. */
3527 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3529 if (cmp == 1)
3530 max = min;
3531 min = build_int_cst (type, 0);
3533 else
3535 /* If the range was reversed, swap MIN and MAX. */
3536 if (cmp == 1)
3537 std::swap (min, max);
3540 cmp = compare_values (min, max);
3541 if (cmp == -2 || cmp == 1)
3543 /* If the new range has its limits swapped around (MIN > MAX),
3544 then the operation caused one of them to wrap around, mark
3545 the new range VARYING. */
3546 set_value_range_to_varying (vr);
3548 else
3549 set_value_range (vr, vr0.type, min, max, NULL);
3550 return;
3553 /* For unhandled operations fall back to varying. */
3554 set_value_range_to_varying (vr);
3555 return;
3559 /* Extract range information from a unary expression CODE OP0 based on
3560 the range of its operand with resulting type TYPE.
3561 The resulting range is stored in *VR. */
3563 static void
3564 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3565 tree type, tree op0)
3567 value_range vr0 = VR_INITIALIZER;
3569 /* Get value ranges for the operand. For constant operands, create
3570 a new value range with the operand to simplify processing. */
3571 if (TREE_CODE (op0) == SSA_NAME)
3572 vr0 = *(get_value_range (op0));
3573 else if (is_gimple_min_invariant (op0))
3574 set_value_range_to_value (&vr0, op0, NULL);
3575 else
3576 set_value_range_to_varying (&vr0);
3578 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3582 /* Extract range information from a conditional expression STMT based on
3583 the ranges of each of its operands and the expression code. */
3585 static void
3586 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3588 tree op0, op1;
3589 value_range vr0 = VR_INITIALIZER;
3590 value_range vr1 = VR_INITIALIZER;
3592 /* Get value ranges for each operand. For constant operands, create
3593 a new value range with the operand to simplify processing. */
3594 op0 = gimple_assign_rhs2 (stmt);
3595 if (TREE_CODE (op0) == SSA_NAME)
3596 vr0 = *(get_value_range (op0));
3597 else if (is_gimple_min_invariant (op0))
3598 set_value_range_to_value (&vr0, op0, NULL);
3599 else
3600 set_value_range_to_varying (&vr0);
3602 op1 = gimple_assign_rhs3 (stmt);
3603 if (TREE_CODE (op1) == SSA_NAME)
3604 vr1 = *(get_value_range (op1));
3605 else if (is_gimple_min_invariant (op1))
3606 set_value_range_to_value (&vr1, op1, NULL);
3607 else
3608 set_value_range_to_varying (&vr1);
3610 /* The resulting value range is the union of the operand ranges */
3611 copy_value_range (vr, &vr0);
3612 vrp_meet (vr, &vr1);
3616 /* Extract range information from a comparison expression EXPR based
3617 on the range of its operand and the expression code. */
3619 static void
3620 extract_range_from_comparison (value_range *vr, enum tree_code code,
3621 tree type, tree op0, tree op1)
3623 bool sop = false;
3624 tree val;
3626 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3627 NULL);
3629 /* A disadvantage of using a special infinity as an overflow
3630 representation is that we lose the ability to record overflow
3631 when we don't have an infinity. So we have to ignore a result
3632 which relies on overflow. */
3634 if (val && !is_overflow_infinity (val) && !sop)
3636 /* Since this expression was found on the RHS of an assignment,
3637 its type may be different from _Bool. Convert VAL to EXPR's
3638 type. */
3639 val = fold_convert (type, val);
3640 if (is_gimple_min_invariant (val))
3641 set_value_range_to_value (vr, val, vr->equiv);
3642 else
3643 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3645 else
3646 /* The result of a comparison is always true or false. */
3647 set_value_range_to_truthvalue (vr, type);
3650 /* Helper function for simplify_internal_call_using_ranges and
3651 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3652 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3653 always overflow. Set *OVF to true if it is known to always
3654 overflow. */
3656 static bool
3657 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3658 tree op0, tree op1, bool *ovf)
3660 value_range vr0 = VR_INITIALIZER;
3661 value_range vr1 = VR_INITIALIZER;
3662 if (TREE_CODE (op0) == SSA_NAME)
3663 vr0 = *get_value_range (op0);
3664 else if (TREE_CODE (op0) == INTEGER_CST)
3665 set_value_range_to_value (&vr0, op0, NULL);
3666 else
3667 set_value_range_to_varying (&vr0);
3669 if (TREE_CODE (op1) == SSA_NAME)
3670 vr1 = *get_value_range (op1);
3671 else if (TREE_CODE (op1) == INTEGER_CST)
3672 set_value_range_to_value (&vr1, op1, NULL);
3673 else
3674 set_value_range_to_varying (&vr1);
3676 if (!range_int_cst_p (&vr0)
3677 || TREE_OVERFLOW (vr0.min)
3678 || TREE_OVERFLOW (vr0.max))
3680 vr0.min = vrp_val_min (TREE_TYPE (op0));
3681 vr0.max = vrp_val_max (TREE_TYPE (op0));
3683 if (!range_int_cst_p (&vr1)
3684 || TREE_OVERFLOW (vr1.min)
3685 || TREE_OVERFLOW (vr1.max))
3687 vr1.min = vrp_val_min (TREE_TYPE (op1));
3688 vr1.max = vrp_val_max (TREE_TYPE (op1));
3690 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3691 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3692 if (arith_overflowed_p (subcode, type, vr0.max,
3693 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3694 return false;
3695 if (subcode == MULT_EXPR)
3697 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3698 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3699 return false;
3701 if (*ovf)
3703 /* So far we found that there is an overflow on the boundaries.
3704 That doesn't prove that there is an overflow even for all values
3705 in between the boundaries. For that compute widest_int range
3706 of the result and see if it doesn't overlap the range of
3707 type. */
3708 widest_int wmin, wmax;
3709 widest_int w[4];
3710 int i;
3711 w[0] = wi::to_widest (vr0.min);
3712 w[1] = wi::to_widest (vr0.max);
3713 w[2] = wi::to_widest (vr1.min);
3714 w[3] = wi::to_widest (vr1.max);
3715 for (i = 0; i < 4; i++)
3717 widest_int wt;
3718 switch (subcode)
3720 case PLUS_EXPR:
3721 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3722 break;
3723 case MINUS_EXPR:
3724 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3725 break;
3726 case MULT_EXPR:
3727 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3728 break;
3729 default:
3730 gcc_unreachable ();
3732 if (i == 0)
3734 wmin = wt;
3735 wmax = wt;
3737 else
3739 wmin = wi::smin (wmin, wt);
3740 wmax = wi::smax (wmax, wt);
3743 /* The result of op0 CODE op1 is known to be in range
3744 [wmin, wmax]. */
3745 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3746 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3747 /* If all values in [wmin, wmax] are smaller than
3748 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3749 the arithmetic operation will always overflow. */
3750 if (wmax < wtmin || wmin > wtmax)
3751 return true;
3752 return false;
3754 return true;
3757 /* Try to derive a nonnegative or nonzero range out of STMT relying
3758 primarily on generic routines in fold in conjunction with range data.
3759 Store the result in *VR */
3761 static void
3762 extract_range_basic (value_range *vr, gimple *stmt)
3764 bool sop = false;
3765 tree type = gimple_expr_type (stmt);
3767 if (is_gimple_call (stmt))
3769 tree arg;
3770 int mini, maxi, zerov = 0, prec;
3771 enum tree_code subcode = ERROR_MARK;
3772 combined_fn cfn = gimple_call_combined_fn (stmt);
3774 switch (cfn)
3776 case CFN_BUILT_IN_CONSTANT_P:
3777 /* If the call is __builtin_constant_p and the argument is a
3778 function parameter resolve it to false. This avoids bogus
3779 array bound warnings.
3780 ??? We could do this as early as inlining is finished. */
3781 arg = gimple_call_arg (stmt, 0);
3782 if (TREE_CODE (arg) == SSA_NAME
3783 && SSA_NAME_IS_DEFAULT_DEF (arg)
3784 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
3785 && cfun->after_inlining)
3787 set_value_range_to_null (vr, type);
3788 return;
3790 break;
3791 /* Both __builtin_ffs* and __builtin_popcount return
3792 [0, prec]. */
3793 CASE_CFN_FFS:
3794 CASE_CFN_POPCOUNT:
3795 arg = gimple_call_arg (stmt, 0);
3796 prec = TYPE_PRECISION (TREE_TYPE (arg));
3797 mini = 0;
3798 maxi = prec;
3799 if (TREE_CODE (arg) == SSA_NAME)
3801 value_range *vr0 = get_value_range (arg);
3802 /* If arg is non-zero, then ffs or popcount
3803 are non-zero. */
3804 if (((vr0->type == VR_RANGE
3805 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3806 || (vr0->type == VR_ANTI_RANGE
3807 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3808 && !is_overflow_infinity (vr0->min)
3809 && !is_overflow_infinity (vr0->max))
3810 mini = 1;
3811 /* If some high bits are known to be zero,
3812 we can decrease the maximum. */
3813 if (vr0->type == VR_RANGE
3814 && TREE_CODE (vr0->max) == INTEGER_CST
3815 && !operand_less_p (vr0->min,
3816 build_zero_cst (TREE_TYPE (vr0->min)))
3817 && !is_overflow_infinity (vr0->max))
3818 maxi = tree_floor_log2 (vr0->max) + 1;
3820 goto bitop_builtin;
3821 /* __builtin_parity* returns [0, 1]. */
3822 CASE_CFN_PARITY:
3823 mini = 0;
3824 maxi = 1;
3825 goto bitop_builtin;
3826 /* __builtin_c[lt]z* return [0, prec-1], except for
3827 when the argument is 0, but that is undefined behavior.
3828 On many targets where the CLZ RTL or optab value is defined
3829 for 0 the value is prec, so include that in the range
3830 by default. */
3831 CASE_CFN_CLZ:
3832 arg = gimple_call_arg (stmt, 0);
3833 prec = TYPE_PRECISION (TREE_TYPE (arg));
3834 mini = 0;
3835 maxi = prec;
3836 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3837 != CODE_FOR_nothing
3838 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3839 zerov)
3840 /* Handle only the single common value. */
3841 && zerov != prec)
3842 /* Magic value to give up, unless vr0 proves
3843 arg is non-zero. */
3844 mini = -2;
3845 if (TREE_CODE (arg) == SSA_NAME)
3847 value_range *vr0 = get_value_range (arg);
3848 /* From clz of VR_RANGE minimum we can compute
3849 result maximum. */
3850 if (vr0->type == VR_RANGE
3851 && TREE_CODE (vr0->min) == INTEGER_CST
3852 && !is_overflow_infinity (vr0->min))
3854 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3855 if (maxi != prec)
3856 mini = 0;
3858 else if (vr0->type == VR_ANTI_RANGE
3859 && integer_zerop (vr0->min)
3860 && !is_overflow_infinity (vr0->min))
3862 maxi = prec - 1;
3863 mini = 0;
3865 if (mini == -2)
3866 break;
3867 /* From clz of VR_RANGE maximum we can compute
3868 result minimum. */
3869 if (vr0->type == VR_RANGE
3870 && TREE_CODE (vr0->max) == INTEGER_CST
3871 && !is_overflow_infinity (vr0->max))
3873 mini = prec - 1 - tree_floor_log2 (vr0->max);
3874 if (mini == prec)
3875 break;
3878 if (mini == -2)
3879 break;
3880 goto bitop_builtin;
3881 /* __builtin_ctz* return [0, prec-1], except for
3882 when the argument is 0, but that is undefined behavior.
3883 If there is a ctz optab for this mode and
3884 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3885 otherwise just assume 0 won't be seen. */
3886 CASE_CFN_CTZ:
3887 arg = gimple_call_arg (stmt, 0);
3888 prec = TYPE_PRECISION (TREE_TYPE (arg));
3889 mini = 0;
3890 maxi = prec - 1;
3891 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3892 != CODE_FOR_nothing
3893 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3894 zerov))
3896 /* Handle only the two common values. */
3897 if (zerov == -1)
3898 mini = -1;
3899 else if (zerov == prec)
3900 maxi = prec;
3901 else
3902 /* Magic value to give up, unless vr0 proves
3903 arg is non-zero. */
3904 mini = -2;
3906 if (TREE_CODE (arg) == SSA_NAME)
3908 value_range *vr0 = get_value_range (arg);
3909 /* If arg is non-zero, then use [0, prec - 1]. */
3910 if (((vr0->type == VR_RANGE
3911 && integer_nonzerop (vr0->min))
3912 || (vr0->type == VR_ANTI_RANGE
3913 && integer_zerop (vr0->min)))
3914 && !is_overflow_infinity (vr0->min))
3916 mini = 0;
3917 maxi = prec - 1;
3919 /* If some high bits are known to be zero,
3920 we can decrease the result maximum. */
3921 if (vr0->type == VR_RANGE
3922 && TREE_CODE (vr0->max) == INTEGER_CST
3923 && !is_overflow_infinity (vr0->max))
3925 maxi = tree_floor_log2 (vr0->max);
3926 /* For vr0 [0, 0] give up. */
3927 if (maxi == -1)
3928 break;
3931 if (mini == -2)
3932 break;
3933 goto bitop_builtin;
3934 /* __builtin_clrsb* returns [0, prec-1]. */
3935 CASE_CFN_CLRSB:
3936 arg = gimple_call_arg (stmt, 0);
3937 prec = TYPE_PRECISION (TREE_TYPE (arg));
3938 mini = 0;
3939 maxi = prec - 1;
3940 goto bitop_builtin;
3941 bitop_builtin:
3942 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3943 build_int_cst (type, maxi), NULL);
3944 return;
3945 case CFN_UBSAN_CHECK_ADD:
3946 subcode = PLUS_EXPR;
3947 break;
3948 case CFN_UBSAN_CHECK_SUB:
3949 subcode = MINUS_EXPR;
3950 break;
3951 case CFN_UBSAN_CHECK_MUL:
3952 subcode = MULT_EXPR;
3953 break;
3954 case CFN_GOACC_DIM_SIZE:
3955 case CFN_GOACC_DIM_POS:
3956 /* Optimizing these two internal functions helps the loop
3957 optimizer eliminate outer comparisons. Size is [1,N]
3958 and pos is [0,N-1]. */
3960 bool is_pos = cfn == CFN_GOACC_DIM_POS;
3961 int axis = get_oacc_ifn_dim_arg (stmt);
3962 int size = get_oacc_fn_dim_size (current_function_decl, axis);
3964 if (!size)
3965 /* If it's dynamic, the backend might know a hardware
3966 limitation. */
3967 size = targetm.goacc.dim_limit (axis);
3969 tree type = TREE_TYPE (gimple_call_lhs (stmt));
3970 set_value_range (vr, VR_RANGE,
3971 build_int_cst (type, is_pos ? 0 : 1),
3972 size ? build_int_cst (type, size - is_pos)
3973 : vrp_val_max (type), NULL);
3975 return;
3976 default:
3977 break;
3979 if (subcode != ERROR_MARK)
3981 bool saved_flag_wrapv = flag_wrapv;
3982 /* Pretend the arithmetics is wrapping. If there is
3983 any overflow, we'll complain, but will actually do
3984 wrapping operation. */
3985 flag_wrapv = 1;
3986 extract_range_from_binary_expr (vr, subcode, type,
3987 gimple_call_arg (stmt, 0),
3988 gimple_call_arg (stmt, 1));
3989 flag_wrapv = saved_flag_wrapv;
3991 /* If for both arguments vrp_valueize returned non-NULL,
3992 this should have been already folded and if not, it
3993 wasn't folded because of overflow. Avoid removing the
3994 UBSAN_CHECK_* calls in that case. */
3995 if (vr->type == VR_RANGE
3996 && (vr->min == vr->max
3997 || operand_equal_p (vr->min, vr->max, 0)))
3998 set_value_range_to_varying (vr);
3999 return;
4002 /* Handle extraction of the two results (result of arithmetics and
4003 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4004 internal function. */
4005 else if (is_gimple_assign (stmt)
4006 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4007 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4008 && INTEGRAL_TYPE_P (type))
4010 enum tree_code code = gimple_assign_rhs_code (stmt);
4011 tree op = gimple_assign_rhs1 (stmt);
4012 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4014 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4015 if (is_gimple_call (g) && gimple_call_internal_p (g))
4017 enum tree_code subcode = ERROR_MARK;
4018 switch (gimple_call_internal_fn (g))
4020 case IFN_ADD_OVERFLOW:
4021 subcode = PLUS_EXPR;
4022 break;
4023 case IFN_SUB_OVERFLOW:
4024 subcode = MINUS_EXPR;
4025 break;
4026 case IFN_MUL_OVERFLOW:
4027 subcode = MULT_EXPR;
4028 break;
4029 default:
4030 break;
4032 if (subcode != ERROR_MARK)
4034 tree op0 = gimple_call_arg (g, 0);
4035 tree op1 = gimple_call_arg (g, 1);
4036 if (code == IMAGPART_EXPR)
4038 bool ovf = false;
4039 if (check_for_binary_op_overflow (subcode, type,
4040 op0, op1, &ovf))
4041 set_value_range_to_value (vr,
4042 build_int_cst (type, ovf),
4043 NULL);
4044 else if (TYPE_PRECISION (type) == 1
4045 && !TYPE_UNSIGNED (type))
4046 set_value_range_to_varying (vr);
4047 else
4048 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4049 build_int_cst (type, 1), NULL);
4051 else if (types_compatible_p (type, TREE_TYPE (op0))
4052 && types_compatible_p (type, TREE_TYPE (op1)))
4054 bool saved_flag_wrapv = flag_wrapv;
4055 /* Pretend the arithmetics is wrapping. If there is
4056 any overflow, IMAGPART_EXPR will be set. */
4057 flag_wrapv = 1;
4058 extract_range_from_binary_expr (vr, subcode, type,
4059 op0, op1);
4060 flag_wrapv = saved_flag_wrapv;
4062 else
4064 value_range vr0 = VR_INITIALIZER;
4065 value_range vr1 = VR_INITIALIZER;
4066 bool saved_flag_wrapv = flag_wrapv;
4067 /* Pretend the arithmetics is wrapping. If there is
4068 any overflow, IMAGPART_EXPR will be set. */
4069 flag_wrapv = 1;
4070 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4071 type, op0);
4072 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4073 type, op1);
4074 extract_range_from_binary_expr_1 (vr, subcode, type,
4075 &vr0, &vr1);
4076 flag_wrapv = saved_flag_wrapv;
4078 return;
4083 if (INTEGRAL_TYPE_P (type)
4084 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4085 set_value_range_to_nonnegative (vr, type,
4086 sop || stmt_overflow_infinity (stmt));
4087 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4088 && !sop)
4089 set_value_range_to_nonnull (vr, type);
4090 else
4091 set_value_range_to_varying (vr);
4095 /* Try to compute a useful range out of assignment STMT and store it
4096 in *VR. */
4098 static void
4099 extract_range_from_assignment (value_range *vr, gassign *stmt)
4101 enum tree_code code = gimple_assign_rhs_code (stmt);
4103 if (code == ASSERT_EXPR)
4104 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4105 else if (code == SSA_NAME)
4106 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4107 else if (TREE_CODE_CLASS (code) == tcc_binary)
4108 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4109 gimple_expr_type (stmt),
4110 gimple_assign_rhs1 (stmt),
4111 gimple_assign_rhs2 (stmt));
4112 else if (TREE_CODE_CLASS (code) == tcc_unary)
4113 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4114 gimple_expr_type (stmt),
4115 gimple_assign_rhs1 (stmt));
4116 else if (code == COND_EXPR)
4117 extract_range_from_cond_expr (vr, stmt);
4118 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4119 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4120 gimple_expr_type (stmt),
4121 gimple_assign_rhs1 (stmt),
4122 gimple_assign_rhs2 (stmt));
4123 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4124 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4125 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4126 else
4127 set_value_range_to_varying (vr);
4129 if (vr->type == VR_VARYING)
4130 extract_range_basic (vr, stmt);
4133 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4134 would be profitable to adjust VR using scalar evolution information
4135 for VAR. If so, update VR with the new limits. */
4137 static void
4138 adjust_range_with_scev (value_range *vr, struct loop *loop,
4139 gimple *stmt, tree var)
4141 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4142 enum ev_direction dir;
4144 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4145 better opportunities than a regular range, but I'm not sure. */
4146 if (vr->type == VR_ANTI_RANGE)
4147 return;
4149 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4151 /* Like in PR19590, scev can return a constant function. */
4152 if (is_gimple_min_invariant (chrec))
4154 set_value_range_to_value (vr, chrec, vr->equiv);
4155 return;
4158 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4159 return;
4161 init = initial_condition_in_loop_num (chrec, loop->num);
4162 tem = op_with_constant_singleton_value_range (init);
4163 if (tem)
4164 init = tem;
4165 step = evolution_part_in_loop_num (chrec, loop->num);
4166 tem = op_with_constant_singleton_value_range (step);
4167 if (tem)
4168 step = tem;
4170 /* If STEP is symbolic, we can't know whether INIT will be the
4171 minimum or maximum value in the range. Also, unless INIT is
4172 a simple expression, compare_values and possibly other functions
4173 in tree-vrp won't be able to handle it. */
4174 if (step == NULL_TREE
4175 || !is_gimple_min_invariant (step)
4176 || !valid_value_p (init))
4177 return;
4179 dir = scev_direction (chrec);
4180 if (/* Do not adjust ranges if we do not know whether the iv increases
4181 or decreases, ... */
4182 dir == EV_DIR_UNKNOWN
4183 /* ... or if it may wrap. */
4184 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
4185 get_chrec_loop (chrec), true))
4186 return;
4188 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4189 negative_overflow_infinity and positive_overflow_infinity,
4190 because we have concluded that the loop probably does not
4191 wrap. */
4193 type = TREE_TYPE (var);
4194 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4195 tmin = lower_bound_in_type (type, type);
4196 else
4197 tmin = TYPE_MIN_VALUE (type);
4198 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4199 tmax = upper_bound_in_type (type, type);
4200 else
4201 tmax = TYPE_MAX_VALUE (type);
4203 /* Try to use estimated number of iterations for the loop to constrain the
4204 final value in the evolution. */
4205 if (TREE_CODE (step) == INTEGER_CST
4206 && is_gimple_val (init)
4207 && (TREE_CODE (init) != SSA_NAME
4208 || get_value_range (init)->type == VR_RANGE))
4210 widest_int nit;
4212 /* We are only entering here for loop header PHI nodes, so using
4213 the number of latch executions is the correct thing to use. */
4214 if (max_loop_iterations (loop, &nit))
4216 value_range maxvr = VR_INITIALIZER;
4217 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4218 bool overflow;
4220 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4221 &overflow);
4222 /* If the multiplication overflowed we can't do a meaningful
4223 adjustment. Likewise if the result doesn't fit in the type
4224 of the induction variable. For a signed type we have to
4225 check whether the result has the expected signedness which
4226 is that of the step as number of iterations is unsigned. */
4227 if (!overflow
4228 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4229 && (sgn == UNSIGNED
4230 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4232 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4233 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4234 TREE_TYPE (init), init, tem);
4235 /* Likewise if the addition did. */
4236 if (maxvr.type == VR_RANGE)
4238 value_range initvr = VR_INITIALIZER;
4240 if (TREE_CODE (init) == SSA_NAME)
4241 initvr = *(get_value_range (init));
4242 else if (is_gimple_min_invariant (init))
4243 set_value_range_to_value (&initvr, init, NULL);
4244 else
4245 return;
4247 /* Check if init + nit * step overflows. Though we checked
4248 scev {init, step}_loop doesn't wrap, it is not enough
4249 because the loop may exit immediately. Overflow could
4250 happen in the plus expression in this case. */
4251 if ((dir == EV_DIR_DECREASES
4252 && (is_negative_overflow_infinity (maxvr.min)
4253 || compare_values (maxvr.min, initvr.min) != -1))
4254 || (dir == EV_DIR_GROWS
4255 && (is_positive_overflow_infinity (maxvr.max)
4256 || compare_values (maxvr.max, initvr.max) != 1)))
4257 return;
4259 tmin = maxvr.min;
4260 tmax = maxvr.max;
4266 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4268 min = tmin;
4269 max = tmax;
4271 /* For VARYING or UNDEFINED ranges, just about anything we get
4272 from scalar evolutions should be better. */
4274 if (dir == EV_DIR_DECREASES)
4275 max = init;
4276 else
4277 min = init;
4279 else if (vr->type == VR_RANGE)
4281 min = vr->min;
4282 max = vr->max;
4284 if (dir == EV_DIR_DECREASES)
4286 /* INIT is the maximum value. If INIT is lower than VR->MAX
4287 but no smaller than VR->MIN, set VR->MAX to INIT. */
4288 if (compare_values (init, max) == -1)
4289 max = init;
4291 /* According to the loop information, the variable does not
4292 overflow. If we think it does, probably because of an
4293 overflow due to arithmetic on a different INF value,
4294 reset now. */
4295 if (is_negative_overflow_infinity (min)
4296 || compare_values (min, tmin) == -1)
4297 min = tmin;
4300 else
4302 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4303 if (compare_values (init, min) == 1)
4304 min = init;
4306 if (is_positive_overflow_infinity (max)
4307 || compare_values (tmax, max) == -1)
4308 max = tmax;
4311 else
4312 return;
4314 /* If we just created an invalid range with the minimum
4315 greater than the maximum, we fail conservatively.
4316 This should happen only in unreachable
4317 parts of code, or for invalid programs. */
4318 if (compare_values (min, max) == 1
4319 || (is_negative_overflow_infinity (min)
4320 && is_positive_overflow_infinity (max)))
4321 return;
4323 /* Even for valid range info, sometimes overflow flag will leak in.
4324 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4325 drop them except for +-overflow_infinity which still need special
4326 handling in vrp pass. */
4327 if (TREE_OVERFLOW_P (min)
4328 && ! is_negative_overflow_infinity (min))
4329 min = drop_tree_overflow (min);
4330 if (TREE_OVERFLOW_P (max)
4331 && ! is_positive_overflow_infinity (max))
4332 max = drop_tree_overflow (max);
4334 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4338 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4340 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4341 all the values in the ranges.
4343 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4345 - Return NULL_TREE if it is not always possible to determine the
4346 value of the comparison.
4348 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4349 overflow infinity was used in the test. */
4352 static tree
4353 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4354 bool *strict_overflow_p)
4356 /* VARYING or UNDEFINED ranges cannot be compared. */
4357 if (vr0->type == VR_VARYING
4358 || vr0->type == VR_UNDEFINED
4359 || vr1->type == VR_VARYING
4360 || vr1->type == VR_UNDEFINED)
4361 return NULL_TREE;
4363 /* Anti-ranges need to be handled separately. */
4364 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4366 /* If both are anti-ranges, then we cannot compute any
4367 comparison. */
4368 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4369 return NULL_TREE;
4371 /* These comparisons are never statically computable. */
4372 if (comp == GT_EXPR
4373 || comp == GE_EXPR
4374 || comp == LT_EXPR
4375 || comp == LE_EXPR)
4376 return NULL_TREE;
4378 /* Equality can be computed only between a range and an
4379 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4380 if (vr0->type == VR_RANGE)
4382 /* To simplify processing, make VR0 the anti-range. */
4383 value_range *tmp = vr0;
4384 vr0 = vr1;
4385 vr1 = tmp;
4388 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4390 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4391 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4392 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4394 return NULL_TREE;
4397 if (!usable_range_p (vr0, strict_overflow_p)
4398 || !usable_range_p (vr1, strict_overflow_p))
4399 return NULL_TREE;
4401 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4402 operands around and change the comparison code. */
4403 if (comp == GT_EXPR || comp == GE_EXPR)
4405 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4406 std::swap (vr0, vr1);
4409 if (comp == EQ_EXPR)
4411 /* Equality may only be computed if both ranges represent
4412 exactly one value. */
4413 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4414 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4416 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4417 strict_overflow_p);
4418 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4419 strict_overflow_p);
4420 if (cmp_min == 0 && cmp_max == 0)
4421 return boolean_true_node;
4422 else if (cmp_min != -2 && cmp_max != -2)
4423 return boolean_false_node;
4425 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4426 else if (compare_values_warnv (vr0->min, vr1->max,
4427 strict_overflow_p) == 1
4428 || compare_values_warnv (vr1->min, vr0->max,
4429 strict_overflow_p) == 1)
4430 return boolean_false_node;
4432 return NULL_TREE;
4434 else if (comp == NE_EXPR)
4436 int cmp1, cmp2;
4438 /* If VR0 is completely to the left or completely to the right
4439 of VR1, they are always different. Notice that we need to
4440 make sure that both comparisons yield similar results to
4441 avoid comparing values that cannot be compared at
4442 compile-time. */
4443 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4444 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4445 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4446 return boolean_true_node;
4448 /* If VR0 and VR1 represent a single value and are identical,
4449 return false. */
4450 else if (compare_values_warnv (vr0->min, vr0->max,
4451 strict_overflow_p) == 0
4452 && compare_values_warnv (vr1->min, vr1->max,
4453 strict_overflow_p) == 0
4454 && compare_values_warnv (vr0->min, vr1->min,
4455 strict_overflow_p) == 0
4456 && compare_values_warnv (vr0->max, vr1->max,
4457 strict_overflow_p) == 0)
4458 return boolean_false_node;
4460 /* Otherwise, they may or may not be different. */
4461 else
4462 return NULL_TREE;
4464 else if (comp == LT_EXPR || comp == LE_EXPR)
4466 int tst;
4468 /* If VR0 is to the left of VR1, return true. */
4469 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4470 if ((comp == LT_EXPR && tst == -1)
4471 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4473 if (overflow_infinity_range_p (vr0)
4474 || overflow_infinity_range_p (vr1))
4475 *strict_overflow_p = true;
4476 return boolean_true_node;
4479 /* If VR0 is to the right of VR1, return false. */
4480 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4481 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4482 || (comp == LE_EXPR && tst == 1))
4484 if (overflow_infinity_range_p (vr0)
4485 || overflow_infinity_range_p (vr1))
4486 *strict_overflow_p = true;
4487 return boolean_false_node;
4490 /* Otherwise, we don't know. */
4491 return NULL_TREE;
4494 gcc_unreachable ();
4498 /* Given a value range VR, a value VAL and a comparison code COMP, return
4499 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4500 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4501 always returns false. Return NULL_TREE if it is not always
4502 possible to determine the value of the comparison. Also set
4503 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4504 infinity was used in the test. */
4506 static tree
4507 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4508 bool *strict_overflow_p)
4510 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4511 return NULL_TREE;
4513 /* Anti-ranges need to be handled separately. */
4514 if (vr->type == VR_ANTI_RANGE)
4516 /* For anti-ranges, the only predicates that we can compute at
4517 compile time are equality and inequality. */
4518 if (comp == GT_EXPR
4519 || comp == GE_EXPR
4520 || comp == LT_EXPR
4521 || comp == LE_EXPR)
4522 return NULL_TREE;
4524 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4525 if (value_inside_range (val, vr->min, vr->max) == 1)
4526 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4528 return NULL_TREE;
4531 if (!usable_range_p (vr, strict_overflow_p))
4532 return NULL_TREE;
4534 if (comp == EQ_EXPR)
4536 /* EQ_EXPR may only be computed if VR represents exactly
4537 one value. */
4538 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4540 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4541 if (cmp == 0)
4542 return boolean_true_node;
4543 else if (cmp == -1 || cmp == 1 || cmp == 2)
4544 return boolean_false_node;
4546 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4547 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4548 return boolean_false_node;
4550 return NULL_TREE;
4552 else if (comp == NE_EXPR)
4554 /* If VAL is not inside VR, then they are always different. */
4555 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4556 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4557 return boolean_true_node;
4559 /* If VR represents exactly one value equal to VAL, then return
4560 false. */
4561 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4562 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4563 return boolean_false_node;
4565 /* Otherwise, they may or may not be different. */
4566 return NULL_TREE;
4568 else if (comp == LT_EXPR || comp == LE_EXPR)
4570 int tst;
4572 /* If VR is to the left of VAL, return true. */
4573 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4574 if ((comp == LT_EXPR && tst == -1)
4575 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4577 if (overflow_infinity_range_p (vr))
4578 *strict_overflow_p = true;
4579 return boolean_true_node;
4582 /* If VR is to the right of VAL, return false. */
4583 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4584 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4585 || (comp == LE_EXPR && tst == 1))
4587 if (overflow_infinity_range_p (vr))
4588 *strict_overflow_p = true;
4589 return boolean_false_node;
4592 /* Otherwise, we don't know. */
4593 return NULL_TREE;
4595 else if (comp == GT_EXPR || comp == GE_EXPR)
4597 int tst;
4599 /* If VR is to the right of VAL, return true. */
4600 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4601 if ((comp == GT_EXPR && tst == 1)
4602 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4604 if (overflow_infinity_range_p (vr))
4605 *strict_overflow_p = true;
4606 return boolean_true_node;
4609 /* If VR is to the left of VAL, return false. */
4610 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4611 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4612 || (comp == GE_EXPR && tst == -1))
4614 if (overflow_infinity_range_p (vr))
4615 *strict_overflow_p = true;
4616 return boolean_false_node;
4619 /* Otherwise, we don't know. */
4620 return NULL_TREE;
4623 gcc_unreachable ();
4627 /* Debugging dumps. */
4629 void dump_value_range (FILE *, const value_range *);
4630 void debug_value_range (value_range *);
4631 void dump_all_value_ranges (FILE *);
4632 void debug_all_value_ranges (void);
4633 void dump_vr_equiv (FILE *, bitmap);
4634 void debug_vr_equiv (bitmap);
4637 /* Dump value range VR to FILE. */
4639 void
4640 dump_value_range (FILE *file, const value_range *vr)
4642 if (vr == NULL)
4643 fprintf (file, "[]");
4644 else if (vr->type == VR_UNDEFINED)
4645 fprintf (file, "UNDEFINED");
4646 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4648 tree type = TREE_TYPE (vr->min);
4650 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4652 if (is_negative_overflow_infinity (vr->min))
4653 fprintf (file, "-INF(OVF)");
4654 else if (INTEGRAL_TYPE_P (type)
4655 && !TYPE_UNSIGNED (type)
4656 && vrp_val_is_min (vr->min))
4657 fprintf (file, "-INF");
4658 else
4659 print_generic_expr (file, vr->min, 0);
4661 fprintf (file, ", ");
4663 if (is_positive_overflow_infinity (vr->max))
4664 fprintf (file, "+INF(OVF)");
4665 else if (INTEGRAL_TYPE_P (type)
4666 && vrp_val_is_max (vr->max))
4667 fprintf (file, "+INF");
4668 else
4669 print_generic_expr (file, vr->max, 0);
4671 fprintf (file, "]");
4673 if (vr->equiv)
4675 bitmap_iterator bi;
4676 unsigned i, c = 0;
4678 fprintf (file, " EQUIVALENCES: { ");
4680 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4682 print_generic_expr (file, ssa_name (i), 0);
4683 fprintf (file, " ");
4684 c++;
4687 fprintf (file, "} (%u elements)", c);
4690 else if (vr->type == VR_VARYING)
4691 fprintf (file, "VARYING");
4692 else
4693 fprintf (file, "INVALID RANGE");
4697 /* Dump value range VR to stderr. */
4699 DEBUG_FUNCTION void
4700 debug_value_range (value_range *vr)
4702 dump_value_range (stderr, vr);
4703 fprintf (stderr, "\n");
4707 /* Dump value ranges of all SSA_NAMEs to FILE. */
4709 void
4710 dump_all_value_ranges (FILE *file)
4712 size_t i;
4714 for (i = 0; i < num_vr_values; i++)
4716 if (vr_value[i])
4718 print_generic_expr (file, ssa_name (i), 0);
4719 fprintf (file, ": ");
4720 dump_value_range (file, vr_value[i]);
4721 fprintf (file, "\n");
4725 fprintf (file, "\n");
4729 /* Dump all value ranges to stderr. */
4731 DEBUG_FUNCTION void
4732 debug_all_value_ranges (void)
4734 dump_all_value_ranges (stderr);
4738 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4739 create a new SSA name N and return the assertion assignment
4740 'N = ASSERT_EXPR <V, V OP W>'. */
4742 static gimple *
4743 build_assert_expr_for (tree cond, tree v)
4745 tree a;
4746 gassign *assertion;
4748 gcc_assert (TREE_CODE (v) == SSA_NAME
4749 && COMPARISON_CLASS_P (cond));
4751 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4752 assertion = gimple_build_assign (NULL_TREE, a);
4754 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4755 operand of the ASSERT_EXPR. Create it so the new name and the old one
4756 are registered in the replacement table so that we can fix the SSA web
4757 after adding all the ASSERT_EXPRs. */
4758 create_new_def_for (v, assertion, NULL);
4760 return assertion;
4764 /* Return false if EXPR is a predicate expression involving floating
4765 point values. */
4767 static inline bool
4768 fp_predicate (gimple *stmt)
4770 GIMPLE_CHECK (stmt, GIMPLE_COND);
4772 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4775 /* If the range of values taken by OP can be inferred after STMT executes,
4776 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4777 describes the inferred range. Return true if a range could be
4778 inferred. */
4780 static bool
4781 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4783 *val_p = NULL_TREE;
4784 *comp_code_p = ERROR_MARK;
4786 /* Do not attempt to infer anything in names that flow through
4787 abnormal edges. */
4788 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4789 return false;
4791 /* If STMT is the last statement of a basic block with no normal
4792 successors, there is no point inferring anything about any of its
4793 operands. We would not be able to find a proper insertion point
4794 for the assertion, anyway. */
4795 if (stmt_ends_bb_p (stmt))
4797 edge_iterator ei;
4798 edge e;
4800 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4801 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
4802 break;
4803 if (e == NULL)
4804 return false;
4807 if (infer_nonnull_range (stmt, op))
4809 *val_p = build_int_cst (TREE_TYPE (op), 0);
4810 *comp_code_p = NE_EXPR;
4811 return true;
4814 return false;
4818 void dump_asserts_for (FILE *, tree);
4819 void debug_asserts_for (tree);
4820 void dump_all_asserts (FILE *);
4821 void debug_all_asserts (void);
4823 /* Dump all the registered assertions for NAME to FILE. */
4825 void
4826 dump_asserts_for (FILE *file, tree name)
4828 assert_locus *loc;
4830 fprintf (file, "Assertions to be inserted for ");
4831 print_generic_expr (file, name, 0);
4832 fprintf (file, "\n");
4834 loc = asserts_for[SSA_NAME_VERSION (name)];
4835 while (loc)
4837 fprintf (file, "\t");
4838 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4839 fprintf (file, "\n\tBB #%d", loc->bb->index);
4840 if (loc->e)
4842 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4843 loc->e->dest->index);
4844 dump_edge_info (file, loc->e, dump_flags, 0);
4846 fprintf (file, "\n\tPREDICATE: ");
4847 print_generic_expr (file, loc->expr, 0);
4848 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4849 print_generic_expr (file, loc->val, 0);
4850 fprintf (file, "\n\n");
4851 loc = loc->next;
4854 fprintf (file, "\n");
4858 /* Dump all the registered assertions for NAME to stderr. */
4860 DEBUG_FUNCTION void
4861 debug_asserts_for (tree name)
4863 dump_asserts_for (stderr, name);
4867 /* Dump all the registered assertions for all the names to FILE. */
4869 void
4870 dump_all_asserts (FILE *file)
4872 unsigned i;
4873 bitmap_iterator bi;
4875 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4876 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4877 dump_asserts_for (file, ssa_name (i));
4878 fprintf (file, "\n");
4882 /* Dump all the registered assertions for all the names to stderr. */
4884 DEBUG_FUNCTION void
4885 debug_all_asserts (void)
4887 dump_all_asserts (stderr);
4891 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4892 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4893 E->DEST, then register this location as a possible insertion point
4894 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4896 BB, E and SI provide the exact insertion point for the new
4897 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4898 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4899 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4900 must not be NULL. */
4902 static void
4903 register_new_assert_for (tree name, tree expr,
4904 enum tree_code comp_code,
4905 tree val,
4906 basic_block bb,
4907 edge e,
4908 gimple_stmt_iterator si)
4910 assert_locus *n, *loc, *last_loc;
4911 basic_block dest_bb;
4913 gcc_checking_assert (bb == NULL || e == NULL);
4915 if (e == NULL)
4916 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4917 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4919 /* Never build an assert comparing against an integer constant with
4920 TREE_OVERFLOW set. This confuses our undefined overflow warning
4921 machinery. */
4922 if (TREE_OVERFLOW_P (val))
4923 val = drop_tree_overflow (val);
4925 /* The new assertion A will be inserted at BB or E. We need to
4926 determine if the new location is dominated by a previously
4927 registered location for A. If we are doing an edge insertion,
4928 assume that A will be inserted at E->DEST. Note that this is not
4929 necessarily true.
4931 If E is a critical edge, it will be split. But even if E is
4932 split, the new block will dominate the same set of blocks that
4933 E->DEST dominates.
4935 The reverse, however, is not true, blocks dominated by E->DEST
4936 will not be dominated by the new block created to split E. So,
4937 if the insertion location is on a critical edge, we will not use
4938 the new location to move another assertion previously registered
4939 at a block dominated by E->DEST. */
4940 dest_bb = (bb) ? bb : e->dest;
4942 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4943 VAL at a block dominating DEST_BB, then we don't need to insert a new
4944 one. Similarly, if the same assertion already exists at a block
4945 dominated by DEST_BB and the new location is not on a critical
4946 edge, then update the existing location for the assertion (i.e.,
4947 move the assertion up in the dominance tree).
4949 Note, this is implemented as a simple linked list because there
4950 should not be more than a handful of assertions registered per
4951 name. If this becomes a performance problem, a table hashed by
4952 COMP_CODE and VAL could be implemented. */
4953 loc = asserts_for[SSA_NAME_VERSION (name)];
4954 last_loc = loc;
4955 while (loc)
4957 if (loc->comp_code == comp_code
4958 && (loc->val == val
4959 || operand_equal_p (loc->val, val, 0))
4960 && (loc->expr == expr
4961 || operand_equal_p (loc->expr, expr, 0)))
4963 /* If E is not a critical edge and DEST_BB
4964 dominates the existing location for the assertion, move
4965 the assertion up in the dominance tree by updating its
4966 location information. */
4967 if ((e == NULL || !EDGE_CRITICAL_P (e))
4968 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4970 loc->bb = dest_bb;
4971 loc->e = e;
4972 loc->si = si;
4973 return;
4977 /* Update the last node of the list and move to the next one. */
4978 last_loc = loc;
4979 loc = loc->next;
4982 /* If we didn't find an assertion already registered for
4983 NAME COMP_CODE VAL, add a new one at the end of the list of
4984 assertions associated with NAME. */
4985 n = XNEW (struct assert_locus);
4986 n->bb = dest_bb;
4987 n->e = e;
4988 n->si = si;
4989 n->comp_code = comp_code;
4990 n->val = val;
4991 n->expr = expr;
4992 n->next = NULL;
4994 if (last_loc)
4995 last_loc->next = n;
4996 else
4997 asserts_for[SSA_NAME_VERSION (name)] = n;
4999 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5002 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5003 Extract a suitable test code and value and store them into *CODE_P and
5004 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5006 If no extraction was possible, return FALSE, otherwise return TRUE.
5008 If INVERT is true, then we invert the result stored into *CODE_P. */
5010 static bool
5011 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5012 tree cond_op0, tree cond_op1,
5013 bool invert, enum tree_code *code_p,
5014 tree *val_p)
5016 enum tree_code comp_code;
5017 tree val;
5019 /* Otherwise, we have a comparison of the form NAME COMP VAL
5020 or VAL COMP NAME. */
5021 if (name == cond_op1)
5023 /* If the predicate is of the form VAL COMP NAME, flip
5024 COMP around because we need to register NAME as the
5025 first operand in the predicate. */
5026 comp_code = swap_tree_comparison (cond_code);
5027 val = cond_op0;
5029 else if (name == cond_op0)
5031 /* The comparison is of the form NAME COMP VAL, so the
5032 comparison code remains unchanged. */
5033 comp_code = cond_code;
5034 val = cond_op1;
5036 else
5037 gcc_unreachable ();
5039 /* Invert the comparison code as necessary. */
5040 if (invert)
5041 comp_code = invert_tree_comparison (comp_code, 0);
5043 /* VRP only handles integral and pointer types. */
5044 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5045 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5046 return false;
5048 /* Do not register always-false predicates.
5049 FIXME: this works around a limitation in fold() when dealing with
5050 enumerations. Given 'enum { N1, N2 } x;', fold will not
5051 fold 'if (x > N2)' to 'if (0)'. */
5052 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5053 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5055 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5056 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5058 if (comp_code == GT_EXPR
5059 && (!max
5060 || compare_values (val, max) == 0))
5061 return false;
5063 if (comp_code == LT_EXPR
5064 && (!min
5065 || compare_values (val, min) == 0))
5066 return false;
5068 *code_p = comp_code;
5069 *val_p = val;
5070 return true;
5073 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5074 (otherwise return VAL). VAL and MASK must be zero-extended for
5075 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5076 (to transform signed values into unsigned) and at the end xor
5077 SGNBIT back. */
5079 static wide_int
5080 masked_increment (const wide_int &val_in, const wide_int &mask,
5081 const wide_int &sgnbit, unsigned int prec)
5083 wide_int bit = wi::one (prec), res;
5084 unsigned int i;
5086 wide_int val = val_in ^ sgnbit;
5087 for (i = 0; i < prec; i++, bit += bit)
5089 res = mask;
5090 if ((res & bit) == 0)
5091 continue;
5092 res = bit - 1;
5093 res = (val + bit).and_not (res);
5094 res &= mask;
5095 if (wi::gtu_p (res, val))
5096 return res ^ sgnbit;
5098 return val ^ sgnbit;
5101 /* Try to register an edge assertion for SSA name NAME on edge E for
5102 the condition COND contributing to the conditional jump pointed to by BSI.
5103 Invert the condition COND if INVERT is true. */
5105 static void
5106 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5107 enum tree_code cond_code,
5108 tree cond_op0, tree cond_op1, bool invert)
5110 tree val;
5111 enum tree_code comp_code;
5113 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5114 cond_op0,
5115 cond_op1,
5116 invert, &comp_code, &val))
5117 return;
5119 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5120 reachable from E. */
5121 if (live_on_edge (e, name))
5122 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5124 /* In the case of NAME <= CST and NAME being defined as
5125 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5126 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5127 This catches range and anti-range tests. */
5128 if ((comp_code == LE_EXPR
5129 || comp_code == GT_EXPR)
5130 && TREE_CODE (val) == INTEGER_CST
5131 && TYPE_UNSIGNED (TREE_TYPE (val)))
5133 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5134 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5136 /* Extract CST2 from the (optional) addition. */
5137 if (is_gimple_assign (def_stmt)
5138 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5140 name2 = gimple_assign_rhs1 (def_stmt);
5141 cst2 = gimple_assign_rhs2 (def_stmt);
5142 if (TREE_CODE (name2) == SSA_NAME
5143 && TREE_CODE (cst2) == INTEGER_CST)
5144 def_stmt = SSA_NAME_DEF_STMT (name2);
5147 /* Extract NAME2 from the (optional) sign-changing cast. */
5148 if (gimple_assign_cast_p (def_stmt))
5150 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5151 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5152 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5153 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5154 name3 = gimple_assign_rhs1 (def_stmt);
5157 /* If name3 is used later, create an ASSERT_EXPR for it. */
5158 if (name3 != NULL_TREE
5159 && TREE_CODE (name3) == SSA_NAME
5160 && (cst2 == NULL_TREE
5161 || TREE_CODE (cst2) == INTEGER_CST)
5162 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5163 && live_on_edge (e, name3))
5165 tree tmp;
5167 /* Build an expression for the range test. */
5168 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5169 if (cst2 != NULL_TREE)
5170 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5172 if (dump_file)
5174 fprintf (dump_file, "Adding assert for ");
5175 print_generic_expr (dump_file, name3, 0);
5176 fprintf (dump_file, " from ");
5177 print_generic_expr (dump_file, tmp, 0);
5178 fprintf (dump_file, "\n");
5181 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5184 /* If name2 is used later, create an ASSERT_EXPR for it. */
5185 if (name2 != NULL_TREE
5186 && TREE_CODE (name2) == SSA_NAME
5187 && TREE_CODE (cst2) == INTEGER_CST
5188 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5189 && live_on_edge (e, name2))
5191 tree tmp;
5193 /* Build an expression for the range test. */
5194 tmp = name2;
5195 if (TREE_TYPE (name) != TREE_TYPE (name2))
5196 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5197 if (cst2 != NULL_TREE)
5198 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5200 if (dump_file)
5202 fprintf (dump_file, "Adding assert for ");
5203 print_generic_expr (dump_file, name2, 0);
5204 fprintf (dump_file, " from ");
5205 print_generic_expr (dump_file, tmp, 0);
5206 fprintf (dump_file, "\n");
5209 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5213 /* In the case of post-in/decrement tests like if (i++) ... and uses
5214 of the in/decremented value on the edge the extra name we want to
5215 assert for is not on the def chain of the name compared. Instead
5216 it is in the set of use stmts.
5217 Similar cases happen for conversions that were simplified through
5218 fold_{sign_changed,widened}_comparison. */
5219 if ((comp_code == NE_EXPR
5220 || comp_code == EQ_EXPR)
5221 && TREE_CODE (val) == INTEGER_CST)
5223 imm_use_iterator ui;
5224 gimple *use_stmt;
5225 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5227 if (!is_gimple_assign (use_stmt))
5228 continue;
5230 /* Cut off to use-stmts that are dominating the predecessor. */
5231 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5232 continue;
5234 tree name2 = gimple_assign_lhs (use_stmt);
5235 if (TREE_CODE (name2) != SSA_NAME
5236 || !live_on_edge (e, name2))
5237 continue;
5239 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5240 tree cst;
5241 if (code == PLUS_EXPR
5242 || code == MINUS_EXPR)
5244 cst = gimple_assign_rhs2 (use_stmt);
5245 if (TREE_CODE (cst) != INTEGER_CST)
5246 continue;
5247 cst = int_const_binop (code, val, cst);
5249 else if (CONVERT_EXPR_CODE_P (code))
5251 /* For truncating conversions we cannot record
5252 an inequality. */
5253 if (comp_code == NE_EXPR
5254 && (TYPE_PRECISION (TREE_TYPE (name2))
5255 < TYPE_PRECISION (TREE_TYPE (name))))
5256 continue;
5257 cst = fold_convert (TREE_TYPE (name2), val);
5259 else
5260 continue;
5262 if (TREE_OVERFLOW_P (cst))
5263 cst = drop_tree_overflow (cst);
5264 register_new_assert_for (name2, name2, comp_code, cst,
5265 NULL, e, bsi);
5269 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5270 && TREE_CODE (val) == INTEGER_CST)
5272 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5273 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5274 tree val2 = NULL_TREE;
5275 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5276 wide_int mask = wi::zero (prec);
5277 unsigned int nprec = prec;
5278 enum tree_code rhs_code = ERROR_MARK;
5280 if (is_gimple_assign (def_stmt))
5281 rhs_code = gimple_assign_rhs_code (def_stmt);
5283 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5284 assert that A != CST1 -+ CST2. */
5285 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5286 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5288 tree op0 = gimple_assign_rhs1 (def_stmt);
5289 tree op1 = gimple_assign_rhs2 (def_stmt);
5290 if (TREE_CODE (op0) == SSA_NAME
5291 && TREE_CODE (op1) == INTEGER_CST
5292 && live_on_edge (e, op0))
5294 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5295 ? MINUS_EXPR : PLUS_EXPR);
5296 op1 = int_const_binop (reverse_op, val, op1);
5297 if (TREE_OVERFLOW (op1))
5298 op1 = drop_tree_overflow (op1);
5299 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5303 /* Add asserts for NAME cmp CST and NAME being defined
5304 as NAME = (int) NAME2. */
5305 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5306 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5307 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5308 && gimple_assign_cast_p (def_stmt))
5310 name2 = gimple_assign_rhs1 (def_stmt);
5311 if (CONVERT_EXPR_CODE_P (rhs_code)
5312 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5313 && TYPE_UNSIGNED (TREE_TYPE (name2))
5314 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5315 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5316 || !tree_int_cst_equal (val,
5317 TYPE_MIN_VALUE (TREE_TYPE (val))))
5318 && live_on_edge (e, name2))
5320 tree tmp, cst;
5321 enum tree_code new_comp_code = comp_code;
5323 cst = fold_convert (TREE_TYPE (name2),
5324 TYPE_MIN_VALUE (TREE_TYPE (val)));
5325 /* Build an expression for the range test. */
5326 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5327 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5328 fold_convert (TREE_TYPE (name2), val));
5329 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5331 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5332 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5333 build_int_cst (TREE_TYPE (name2), 1));
5336 if (dump_file)
5338 fprintf (dump_file, "Adding assert for ");
5339 print_generic_expr (dump_file, name2, 0);
5340 fprintf (dump_file, " from ");
5341 print_generic_expr (dump_file, tmp, 0);
5342 fprintf (dump_file, "\n");
5345 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5346 e, bsi);
5350 /* Add asserts for NAME cmp CST and NAME being defined as
5351 NAME = NAME2 >> CST2.
5353 Extract CST2 from the right shift. */
5354 if (rhs_code == RSHIFT_EXPR)
5356 name2 = gimple_assign_rhs1 (def_stmt);
5357 cst2 = gimple_assign_rhs2 (def_stmt);
5358 if (TREE_CODE (name2) == SSA_NAME
5359 && tree_fits_uhwi_p (cst2)
5360 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5361 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5362 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5363 && live_on_edge (e, name2))
5365 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5366 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5369 if (val2 != NULL_TREE
5370 && TREE_CODE (val2) == INTEGER_CST
5371 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5372 TREE_TYPE (val),
5373 val2, cst2), val))
5375 enum tree_code new_comp_code = comp_code;
5376 tree tmp, new_val;
5378 tmp = name2;
5379 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5381 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5383 tree type = build_nonstandard_integer_type (prec, 1);
5384 tmp = build1 (NOP_EXPR, type, name2);
5385 val2 = fold_convert (type, val2);
5387 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5388 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5389 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5391 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5393 wide_int minval
5394 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5395 new_val = val2;
5396 if (minval == new_val)
5397 new_val = NULL_TREE;
5399 else
5401 wide_int maxval
5402 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5403 mask |= val2;
5404 if (mask == maxval)
5405 new_val = NULL_TREE;
5406 else
5407 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5410 if (new_val)
5412 if (dump_file)
5414 fprintf (dump_file, "Adding assert for ");
5415 print_generic_expr (dump_file, name2, 0);
5416 fprintf (dump_file, " from ");
5417 print_generic_expr (dump_file, tmp, 0);
5418 fprintf (dump_file, "\n");
5421 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5422 NULL, e, bsi);
5426 /* Add asserts for NAME cmp CST and NAME being defined as
5427 NAME = NAME2 & CST2.
5429 Extract CST2 from the and.
5431 Also handle
5432 NAME = (unsigned) NAME2;
5433 casts where NAME's type is unsigned and has smaller precision
5434 than NAME2's type as if it was NAME = NAME2 & MASK. */
5435 names[0] = NULL_TREE;
5436 names[1] = NULL_TREE;
5437 cst2 = NULL_TREE;
5438 if (rhs_code == BIT_AND_EXPR
5439 || (CONVERT_EXPR_CODE_P (rhs_code)
5440 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5441 && TYPE_UNSIGNED (TREE_TYPE (val))
5442 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5443 > prec))
5445 name2 = gimple_assign_rhs1 (def_stmt);
5446 if (rhs_code == BIT_AND_EXPR)
5447 cst2 = gimple_assign_rhs2 (def_stmt);
5448 else
5450 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5451 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5453 if (TREE_CODE (name2) == SSA_NAME
5454 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5455 && TREE_CODE (cst2) == INTEGER_CST
5456 && !integer_zerop (cst2)
5457 && (nprec > 1
5458 || TYPE_UNSIGNED (TREE_TYPE (val))))
5460 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5461 if (gimple_assign_cast_p (def_stmt2))
5463 names[1] = gimple_assign_rhs1 (def_stmt2);
5464 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5465 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5466 || (TYPE_PRECISION (TREE_TYPE (name2))
5467 != TYPE_PRECISION (TREE_TYPE (names[1])))
5468 || !live_on_edge (e, names[1]))
5469 names[1] = NULL_TREE;
5471 if (live_on_edge (e, name2))
5472 names[0] = name2;
5475 if (names[0] || names[1])
5477 wide_int minv, maxv, valv, cst2v;
5478 wide_int tem, sgnbit;
5479 bool valid_p = false, valn, cst2n;
5480 enum tree_code ccode = comp_code;
5482 valv = wide_int::from (val, nprec, UNSIGNED);
5483 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5484 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5485 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5486 /* If CST2 doesn't have most significant bit set,
5487 but VAL is negative, we have comparison like
5488 if ((x & 0x123) > -4) (always true). Just give up. */
5489 if (!cst2n && valn)
5490 ccode = ERROR_MARK;
5491 if (cst2n)
5492 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5493 else
5494 sgnbit = wi::zero (nprec);
5495 minv = valv & cst2v;
5496 switch (ccode)
5498 case EQ_EXPR:
5499 /* Minimum unsigned value for equality is VAL & CST2
5500 (should be equal to VAL, otherwise we probably should
5501 have folded the comparison into false) and
5502 maximum unsigned value is VAL | ~CST2. */
5503 maxv = valv | ~cst2v;
5504 valid_p = true;
5505 break;
5507 case NE_EXPR:
5508 tem = valv | ~cst2v;
5509 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5510 if (valv == 0)
5512 cst2n = false;
5513 sgnbit = wi::zero (nprec);
5514 goto gt_expr;
5516 /* If (VAL | ~CST2) is all ones, handle it as
5517 (X & CST2) < VAL. */
5518 if (tem == -1)
5520 cst2n = false;
5521 valn = false;
5522 sgnbit = wi::zero (nprec);
5523 goto lt_expr;
5525 if (!cst2n && wi::neg_p (cst2v))
5526 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5527 if (sgnbit != 0)
5529 if (valv == sgnbit)
5531 cst2n = true;
5532 valn = true;
5533 goto gt_expr;
5535 if (tem == wi::mask (nprec - 1, false, nprec))
5537 cst2n = true;
5538 goto lt_expr;
5540 if (!cst2n)
5541 sgnbit = wi::zero (nprec);
5543 break;
5545 case GE_EXPR:
5546 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5547 is VAL and maximum unsigned value is ~0. For signed
5548 comparison, if CST2 doesn't have most significant bit
5549 set, handle it similarly. If CST2 has MSB set,
5550 the minimum is the same, and maximum is ~0U/2. */
5551 if (minv != valv)
5553 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5554 VAL. */
5555 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5556 if (minv == valv)
5557 break;
5559 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5560 valid_p = true;
5561 break;
5563 case GT_EXPR:
5564 gt_expr:
5565 /* Find out smallest MINV where MINV > VAL
5566 && (MINV & CST2) == MINV, if any. If VAL is signed and
5567 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5568 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5569 if (minv == valv)
5570 break;
5571 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5572 valid_p = true;
5573 break;
5575 case LE_EXPR:
5576 /* Minimum unsigned value for <= is 0 and maximum
5577 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5578 Otherwise, find smallest VAL2 where VAL2 > VAL
5579 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5580 as maximum.
5581 For signed comparison, if CST2 doesn't have most
5582 significant bit set, handle it similarly. If CST2 has
5583 MSB set, the maximum is the same and minimum is INT_MIN. */
5584 if (minv == valv)
5585 maxv = valv;
5586 else
5588 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5589 if (maxv == valv)
5590 break;
5591 maxv -= 1;
5593 maxv |= ~cst2v;
5594 minv = sgnbit;
5595 valid_p = true;
5596 break;
5598 case LT_EXPR:
5599 lt_expr:
5600 /* Minimum unsigned value for < is 0 and maximum
5601 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5602 Otherwise, find smallest VAL2 where VAL2 > VAL
5603 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5604 as maximum.
5605 For signed comparison, if CST2 doesn't have most
5606 significant bit set, handle it similarly. If CST2 has
5607 MSB set, the maximum is the same and minimum is INT_MIN. */
5608 if (minv == valv)
5610 if (valv == sgnbit)
5611 break;
5612 maxv = valv;
5614 else
5616 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5617 if (maxv == valv)
5618 break;
5620 maxv -= 1;
5621 maxv |= ~cst2v;
5622 minv = sgnbit;
5623 valid_p = true;
5624 break;
5626 default:
5627 break;
5629 if (valid_p
5630 && (maxv - minv) != -1)
5632 tree tmp, new_val, type;
5633 int i;
5635 for (i = 0; i < 2; i++)
5636 if (names[i])
5638 wide_int maxv2 = maxv;
5639 tmp = names[i];
5640 type = TREE_TYPE (names[i]);
5641 if (!TYPE_UNSIGNED (type))
5643 type = build_nonstandard_integer_type (nprec, 1);
5644 tmp = build1 (NOP_EXPR, type, names[i]);
5646 if (minv != 0)
5648 tmp = build2 (PLUS_EXPR, type, tmp,
5649 wide_int_to_tree (type, -minv));
5650 maxv2 = maxv - minv;
5652 new_val = wide_int_to_tree (type, maxv2);
5654 if (dump_file)
5656 fprintf (dump_file, "Adding assert for ");
5657 print_generic_expr (dump_file, names[i], 0);
5658 fprintf (dump_file, " from ");
5659 print_generic_expr (dump_file, tmp, 0);
5660 fprintf (dump_file, "\n");
5663 register_new_assert_for (names[i], tmp, LE_EXPR,
5664 new_val, NULL, e, bsi);
5671 /* OP is an operand of a truth value expression which is known to have
5672 a particular value. Register any asserts for OP and for any
5673 operands in OP's defining statement.
5675 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5676 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5678 static void
5679 register_edge_assert_for_1 (tree op, enum tree_code code,
5680 edge e, gimple_stmt_iterator bsi)
5682 gimple *op_def;
5683 tree val;
5684 enum tree_code rhs_code;
5686 /* We only care about SSA_NAMEs. */
5687 if (TREE_CODE (op) != SSA_NAME)
5688 return;
5690 /* We know that OP will have a zero or nonzero value. If OP is used
5691 more than once go ahead and register an assert for OP. */
5692 if (live_on_edge (e, op))
5694 val = build_int_cst (TREE_TYPE (op), 0);
5695 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5698 /* Now look at how OP is set. If it's set from a comparison,
5699 a truth operation or some bit operations, then we may be able
5700 to register information about the operands of that assignment. */
5701 op_def = SSA_NAME_DEF_STMT (op);
5702 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5703 return;
5705 rhs_code = gimple_assign_rhs_code (op_def);
5707 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5709 bool invert = (code == EQ_EXPR ? true : false);
5710 tree op0 = gimple_assign_rhs1 (op_def);
5711 tree op1 = gimple_assign_rhs2 (op_def);
5713 if (TREE_CODE (op0) == SSA_NAME)
5714 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5715 if (TREE_CODE (op1) == SSA_NAME)
5716 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5718 else if ((code == NE_EXPR
5719 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5720 || (code == EQ_EXPR
5721 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5723 /* Recurse on each operand. */
5724 tree op0 = gimple_assign_rhs1 (op_def);
5725 tree op1 = gimple_assign_rhs2 (op_def);
5726 if (TREE_CODE (op0) == SSA_NAME
5727 && has_single_use (op0))
5728 register_edge_assert_for_1 (op0, code, e, bsi);
5729 if (TREE_CODE (op1) == SSA_NAME
5730 && has_single_use (op1))
5731 register_edge_assert_for_1 (op1, code, e, bsi);
5733 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5734 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5736 /* Recurse, flipping CODE. */
5737 code = invert_tree_comparison (code, false);
5738 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5740 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5742 /* Recurse through the copy. */
5743 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5745 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5747 /* Recurse through the type conversion, unless it is a narrowing
5748 conversion or conversion from non-integral type. */
5749 tree rhs = gimple_assign_rhs1 (op_def);
5750 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5751 && (TYPE_PRECISION (TREE_TYPE (rhs))
5752 <= TYPE_PRECISION (TREE_TYPE (op))))
5753 register_edge_assert_for_1 (rhs, code, e, bsi);
5757 /* Try to register an edge assertion for SSA name NAME on edge E for
5758 the condition COND contributing to the conditional jump pointed to by
5759 SI. */
5761 static void
5762 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5763 enum tree_code cond_code, tree cond_op0,
5764 tree cond_op1)
5766 tree val;
5767 enum tree_code comp_code;
5768 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5770 /* Do not attempt to infer anything in names that flow through
5771 abnormal edges. */
5772 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5773 return;
5775 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5776 cond_op0, cond_op1,
5777 is_else_edge,
5778 &comp_code, &val))
5779 return;
5781 /* Register ASSERT_EXPRs for name. */
5782 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5783 cond_op1, is_else_edge);
5786 /* If COND is effectively an equality test of an SSA_NAME against
5787 the value zero or one, then we may be able to assert values
5788 for SSA_NAMEs which flow into COND. */
5790 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5791 statement of NAME we can assert both operands of the BIT_AND_EXPR
5792 have nonzero value. */
5793 if (((comp_code == EQ_EXPR && integer_onep (val))
5794 || (comp_code == NE_EXPR && integer_zerop (val))))
5796 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5798 if (is_gimple_assign (def_stmt)
5799 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5801 tree op0 = gimple_assign_rhs1 (def_stmt);
5802 tree op1 = gimple_assign_rhs2 (def_stmt);
5803 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5804 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5808 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5809 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5810 have zero value. */
5811 if (((comp_code == EQ_EXPR && integer_zerop (val))
5812 || (comp_code == NE_EXPR && integer_onep (val))))
5814 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5816 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5817 necessarily zero value, or if type-precision is one. */
5818 if (is_gimple_assign (def_stmt)
5819 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5820 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5821 || comp_code == EQ_EXPR)))
5823 tree op0 = gimple_assign_rhs1 (def_stmt);
5824 tree op1 = gimple_assign_rhs2 (def_stmt);
5825 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5826 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5832 /* Determine whether the outgoing edges of BB should receive an
5833 ASSERT_EXPR for each of the operands of BB's LAST statement.
5834 The last statement of BB must be a COND_EXPR.
5836 If any of the sub-graphs rooted at BB have an interesting use of
5837 the predicate operands, an assert location node is added to the
5838 list of assertions for the corresponding operands. */
5840 static void
5841 find_conditional_asserts (basic_block bb, gcond *last)
5843 gimple_stmt_iterator bsi;
5844 tree op;
5845 edge_iterator ei;
5846 edge e;
5847 ssa_op_iter iter;
5849 bsi = gsi_for_stmt (last);
5851 /* Look for uses of the operands in each of the sub-graphs
5852 rooted at BB. We need to check each of the outgoing edges
5853 separately, so that we know what kind of ASSERT_EXPR to
5854 insert. */
5855 FOR_EACH_EDGE (e, ei, bb->succs)
5857 if (e->dest == bb)
5858 continue;
5860 /* Register the necessary assertions for each operand in the
5861 conditional predicate. */
5862 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5863 register_edge_assert_for (op, e, bsi,
5864 gimple_cond_code (last),
5865 gimple_cond_lhs (last),
5866 gimple_cond_rhs (last));
5870 struct case_info
5872 tree expr;
5873 basic_block bb;
5876 /* Compare two case labels sorting first by the destination bb index
5877 and then by the case value. */
5879 static int
5880 compare_case_labels (const void *p1, const void *p2)
5882 const struct case_info *ci1 = (const struct case_info *) p1;
5883 const struct case_info *ci2 = (const struct case_info *) p2;
5884 int idx1 = ci1->bb->index;
5885 int idx2 = ci2->bb->index;
5887 if (idx1 < idx2)
5888 return -1;
5889 else if (idx1 == idx2)
5891 /* Make sure the default label is first in a group. */
5892 if (!CASE_LOW (ci1->expr))
5893 return -1;
5894 else if (!CASE_LOW (ci2->expr))
5895 return 1;
5896 else
5897 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5898 CASE_LOW (ci2->expr));
5900 else
5901 return 1;
5904 /* Determine whether the outgoing edges of BB should receive an
5905 ASSERT_EXPR for each of the operands of BB's LAST statement.
5906 The last statement of BB must be a SWITCH_EXPR.
5908 If any of the sub-graphs rooted at BB have an interesting use of
5909 the predicate operands, an assert location node is added to the
5910 list of assertions for the corresponding operands. */
5912 static void
5913 find_switch_asserts (basic_block bb, gswitch *last)
5915 gimple_stmt_iterator bsi;
5916 tree op;
5917 edge e;
5918 struct case_info *ci;
5919 size_t n = gimple_switch_num_labels (last);
5920 #if GCC_VERSION >= 4000
5921 unsigned int idx;
5922 #else
5923 /* Work around GCC 3.4 bug (PR 37086). */
5924 volatile unsigned int idx;
5925 #endif
5927 bsi = gsi_for_stmt (last);
5928 op = gimple_switch_index (last);
5929 if (TREE_CODE (op) != SSA_NAME)
5930 return;
5932 /* Build a vector of case labels sorted by destination label. */
5933 ci = XNEWVEC (struct case_info, n);
5934 for (idx = 0; idx < n; ++idx)
5936 ci[idx].expr = gimple_switch_label (last, idx);
5937 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5939 edge default_edge = find_edge (bb, ci[0].bb);
5940 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5942 for (idx = 0; idx < n; ++idx)
5944 tree min, max;
5945 tree cl = ci[idx].expr;
5946 basic_block cbb = ci[idx].bb;
5948 min = CASE_LOW (cl);
5949 max = CASE_HIGH (cl);
5951 /* If there are multiple case labels with the same destination
5952 we need to combine them to a single value range for the edge. */
5953 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5955 /* Skip labels until the last of the group. */
5956 do {
5957 ++idx;
5958 } while (idx < n && cbb == ci[idx].bb);
5959 --idx;
5961 /* Pick up the maximum of the case label range. */
5962 if (CASE_HIGH (ci[idx].expr))
5963 max = CASE_HIGH (ci[idx].expr);
5964 else
5965 max = CASE_LOW (ci[idx].expr);
5968 /* Can't extract a useful assertion out of a range that includes the
5969 default label. */
5970 if (min == NULL_TREE)
5971 continue;
5973 /* Find the edge to register the assert expr on. */
5974 e = find_edge (bb, cbb);
5976 /* Register the necessary assertions for the operand in the
5977 SWITCH_EXPR. */
5978 register_edge_assert_for (op, e, bsi,
5979 max ? GE_EXPR : EQ_EXPR,
5980 op, fold_convert (TREE_TYPE (op), min));
5981 if (max)
5982 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
5983 fold_convert (TREE_TYPE (op), max));
5986 XDELETEVEC (ci);
5988 if (!live_on_edge (default_edge, op))
5989 return;
5991 /* Now register along the default label assertions that correspond to the
5992 anti-range of each label. */
5993 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
5994 for (idx = 1; idx < n; idx++)
5996 tree min, max;
5997 tree cl = gimple_switch_label (last, idx);
5999 min = CASE_LOW (cl);
6000 max = CASE_HIGH (cl);
6002 /* Combine contiguous case ranges to reduce the number of assertions
6003 to insert. */
6004 for (idx = idx + 1; idx < n; idx++)
6006 tree next_min, next_max;
6007 tree next_cl = gimple_switch_label (last, idx);
6009 next_min = CASE_LOW (next_cl);
6010 next_max = CASE_HIGH (next_cl);
6012 wide_int difference = wi::sub (next_min, max ? max : min);
6013 if (wi::eq_p (difference, 1))
6014 max = next_max ? next_max : next_min;
6015 else
6016 break;
6018 idx--;
6020 if (max == NULL_TREE)
6022 /* Register the assertion OP != MIN. */
6023 min = fold_convert (TREE_TYPE (op), min);
6024 register_edge_assert_for (op, default_edge, bsi, NE_EXPR, op, min);
6026 else
6028 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6029 which will give OP the anti-range ~[MIN,MAX]. */
6030 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
6031 min = fold_convert (TREE_TYPE (uop), min);
6032 max = fold_convert (TREE_TYPE (uop), max);
6034 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
6035 tree rhs = int_const_binop (MINUS_EXPR, max, min);
6036 register_new_assert_for (op, lhs, GT_EXPR, rhs,
6037 NULL, default_edge, bsi);
6040 if (--insertion_limit == 0)
6041 break;
6046 /* Traverse all the statements in block BB looking for statements that
6047 may generate useful assertions for the SSA names in their operand.
6048 If a statement produces a useful assertion A for name N_i, then the
6049 list of assertions already generated for N_i is scanned to
6050 determine if A is actually needed.
6052 If N_i already had the assertion A at a location dominating the
6053 current location, then nothing needs to be done. Otherwise, the
6054 new location for A is recorded instead.
6056 1- For every statement S in BB, all the variables used by S are
6057 added to bitmap FOUND_IN_SUBGRAPH.
6059 2- If statement S uses an operand N in a way that exposes a known
6060 value range for N, then if N was not already generated by an
6061 ASSERT_EXPR, create a new assert location for N. For instance,
6062 if N is a pointer and the statement dereferences it, we can
6063 assume that N is not NULL.
6065 3- COND_EXPRs are a special case of #2. We can derive range
6066 information from the predicate but need to insert different
6067 ASSERT_EXPRs for each of the sub-graphs rooted at the
6068 conditional block. If the last statement of BB is a conditional
6069 expression of the form 'X op Y', then
6071 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6073 b) If the conditional is the only entry point to the sub-graph
6074 corresponding to the THEN_CLAUSE, recurse into it. On
6075 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6076 an ASSERT_EXPR is added for the corresponding variable.
6078 c) Repeat step (b) on the ELSE_CLAUSE.
6080 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6082 For instance,
6084 if (a == 9)
6085 b = a;
6086 else
6087 b = c + 1;
6089 In this case, an assertion on the THEN clause is useful to
6090 determine that 'a' is always 9 on that edge. However, an assertion
6091 on the ELSE clause would be unnecessary.
6093 4- If BB does not end in a conditional expression, then we recurse
6094 into BB's dominator children.
6096 At the end of the recursive traversal, every SSA name will have a
6097 list of locations where ASSERT_EXPRs should be added. When a new
6098 location for name N is found, it is registered by calling
6099 register_new_assert_for. That function keeps track of all the
6100 registered assertions to prevent adding unnecessary assertions.
6101 For instance, if a pointer P_4 is dereferenced more than once in a
6102 dominator tree, only the location dominating all the dereference of
6103 P_4 will receive an ASSERT_EXPR. */
6105 static void
6106 find_assert_locations_1 (basic_block bb, sbitmap live)
6108 gimple *last;
6110 last = last_stmt (bb);
6112 /* If BB's last statement is a conditional statement involving integer
6113 operands, determine if we need to add ASSERT_EXPRs. */
6114 if (last
6115 && gimple_code (last) == GIMPLE_COND
6116 && !fp_predicate (last)
6117 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6118 find_conditional_asserts (bb, as_a <gcond *> (last));
6120 /* If BB's last statement is a switch statement involving integer
6121 operands, determine if we need to add ASSERT_EXPRs. */
6122 if (last
6123 && gimple_code (last) == GIMPLE_SWITCH
6124 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6125 find_switch_asserts (bb, as_a <gswitch *> (last));
6127 /* Traverse all the statements in BB marking used names and looking
6128 for statements that may infer assertions for their used operands. */
6129 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6130 gsi_prev (&si))
6132 gimple *stmt;
6133 tree op;
6134 ssa_op_iter i;
6136 stmt = gsi_stmt (si);
6138 if (is_gimple_debug (stmt))
6139 continue;
6141 /* See if we can derive an assertion for any of STMT's operands. */
6142 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6144 tree value;
6145 enum tree_code comp_code;
6147 /* If op is not live beyond this stmt, do not bother to insert
6148 asserts for it. */
6149 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6150 continue;
6152 /* If OP is used in such a way that we can infer a value
6153 range for it, and we don't find a previous assertion for
6154 it, create a new assertion location node for OP. */
6155 if (infer_value_range (stmt, op, &comp_code, &value))
6157 /* If we are able to infer a nonzero value range for OP,
6158 then walk backwards through the use-def chain to see if OP
6159 was set via a typecast.
6161 If so, then we can also infer a nonzero value range
6162 for the operand of the NOP_EXPR. */
6163 if (comp_code == NE_EXPR && integer_zerop (value))
6165 tree t = op;
6166 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6168 while (is_gimple_assign (def_stmt)
6169 && CONVERT_EXPR_CODE_P
6170 (gimple_assign_rhs_code (def_stmt))
6171 && TREE_CODE
6172 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6173 && POINTER_TYPE_P
6174 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6176 t = gimple_assign_rhs1 (def_stmt);
6177 def_stmt = SSA_NAME_DEF_STMT (t);
6179 /* Note we want to register the assert for the
6180 operand of the NOP_EXPR after SI, not after the
6181 conversion. */
6182 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6183 register_new_assert_for (t, t, comp_code, value,
6184 bb, NULL, si);
6188 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6192 /* Update live. */
6193 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6194 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6195 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6196 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6199 /* Traverse all PHI nodes in BB, updating live. */
6200 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6201 gsi_next (&si))
6203 use_operand_p arg_p;
6204 ssa_op_iter i;
6205 gphi *phi = si.phi ();
6206 tree res = gimple_phi_result (phi);
6208 if (virtual_operand_p (res))
6209 continue;
6211 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6213 tree arg = USE_FROM_PTR (arg_p);
6214 if (TREE_CODE (arg) == SSA_NAME)
6215 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6218 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6222 /* Do an RPO walk over the function computing SSA name liveness
6223 on-the-fly and deciding on assert expressions to insert. */
6225 static void
6226 find_assert_locations (void)
6228 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6229 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6230 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6231 int rpo_cnt, i;
6233 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6234 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6235 for (i = 0; i < rpo_cnt; ++i)
6236 bb_rpo[rpo[i]] = i;
6238 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6239 the order we compute liveness and insert asserts we otherwise
6240 fail to insert asserts into the loop latch. */
6241 loop_p loop;
6242 FOR_EACH_LOOP (loop, 0)
6244 i = loop->latch->index;
6245 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6246 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6247 !gsi_end_p (gsi); gsi_next (&gsi))
6249 gphi *phi = gsi.phi ();
6250 if (virtual_operand_p (gimple_phi_result (phi)))
6251 continue;
6252 tree arg = gimple_phi_arg_def (phi, j);
6253 if (TREE_CODE (arg) == SSA_NAME)
6255 if (live[i] == NULL)
6257 live[i] = sbitmap_alloc (num_ssa_names);
6258 bitmap_clear (live[i]);
6260 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6265 for (i = rpo_cnt - 1; i >= 0; --i)
6267 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6268 edge e;
6269 edge_iterator ei;
6271 if (!live[rpo[i]])
6273 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6274 bitmap_clear (live[rpo[i]]);
6277 /* Process BB and update the live information with uses in
6278 this block. */
6279 find_assert_locations_1 (bb, live[rpo[i]]);
6281 /* Merge liveness into the predecessor blocks and free it. */
6282 if (!bitmap_empty_p (live[rpo[i]]))
6284 int pred_rpo = i;
6285 FOR_EACH_EDGE (e, ei, bb->preds)
6287 int pred = e->src->index;
6288 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6289 continue;
6291 if (!live[pred])
6293 live[pred] = sbitmap_alloc (num_ssa_names);
6294 bitmap_clear (live[pred]);
6296 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6298 if (bb_rpo[pred] < pred_rpo)
6299 pred_rpo = bb_rpo[pred];
6302 /* Record the RPO number of the last visited block that needs
6303 live information from this block. */
6304 last_rpo[rpo[i]] = pred_rpo;
6306 else
6308 sbitmap_free (live[rpo[i]]);
6309 live[rpo[i]] = NULL;
6312 /* We can free all successors live bitmaps if all their
6313 predecessors have been visited already. */
6314 FOR_EACH_EDGE (e, ei, bb->succs)
6315 if (last_rpo[e->dest->index] == i
6316 && live[e->dest->index])
6318 sbitmap_free (live[e->dest->index]);
6319 live[e->dest->index] = NULL;
6323 XDELETEVEC (rpo);
6324 XDELETEVEC (bb_rpo);
6325 XDELETEVEC (last_rpo);
6326 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6327 if (live[i])
6328 sbitmap_free (live[i]);
6329 XDELETEVEC (live);
6332 /* Create an ASSERT_EXPR for NAME and insert it in the location
6333 indicated by LOC. Return true if we made any edge insertions. */
6335 static bool
6336 process_assert_insertions_for (tree name, assert_locus *loc)
6338 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6339 gimple *stmt;
6340 tree cond;
6341 gimple *assert_stmt;
6342 edge_iterator ei;
6343 edge e;
6345 /* If we have X <=> X do not insert an assert expr for that. */
6346 if (loc->expr == loc->val)
6347 return false;
6349 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6350 assert_stmt = build_assert_expr_for (cond, name);
6351 if (loc->e)
6353 /* We have been asked to insert the assertion on an edge. This
6354 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6355 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6356 || (gimple_code (gsi_stmt (loc->si))
6357 == GIMPLE_SWITCH));
6359 gsi_insert_on_edge (loc->e, assert_stmt);
6360 return true;
6363 /* Otherwise, we can insert right after LOC->SI iff the
6364 statement must not be the last statement in the block. */
6365 stmt = gsi_stmt (loc->si);
6366 if (!stmt_ends_bb_p (stmt))
6368 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6369 return false;
6372 /* If STMT must be the last statement in BB, we can only insert new
6373 assertions on the non-abnormal edge out of BB. Note that since
6374 STMT is not control flow, there may only be one non-abnormal/eh edge
6375 out of BB. */
6376 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6377 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
6379 gsi_insert_on_edge (e, assert_stmt);
6380 return true;
6383 gcc_unreachable ();
6387 /* Process all the insertions registered for every name N_i registered
6388 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6389 found in ASSERTS_FOR[i]. */
6391 static void
6392 process_assert_insertions (void)
6394 unsigned i;
6395 bitmap_iterator bi;
6396 bool update_edges_p = false;
6397 int num_asserts = 0;
6399 if (dump_file && (dump_flags & TDF_DETAILS))
6400 dump_all_asserts (dump_file);
6402 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6404 assert_locus *loc = asserts_for[i];
6405 gcc_assert (loc);
6407 while (loc)
6409 assert_locus *next = loc->next;
6410 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6411 free (loc);
6412 loc = next;
6413 num_asserts++;
6417 if (update_edges_p)
6418 gsi_commit_edge_inserts ();
6420 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6421 num_asserts);
6425 /* Traverse the flowgraph looking for conditional jumps to insert range
6426 expressions. These range expressions are meant to provide information
6427 to optimizations that need to reason in terms of value ranges. They
6428 will not be expanded into RTL. For instance, given:
6430 x = ...
6431 y = ...
6432 if (x < y)
6433 y = x - 2;
6434 else
6435 x = y + 3;
6437 this pass will transform the code into:
6439 x = ...
6440 y = ...
6441 if (x < y)
6443 x = ASSERT_EXPR <x, x < y>
6444 y = x - 2
6446 else
6448 y = ASSERT_EXPR <y, x >= y>
6449 x = y + 3
6452 The idea is that once copy and constant propagation have run, other
6453 optimizations will be able to determine what ranges of values can 'x'
6454 take in different paths of the code, simply by checking the reaching
6455 definition of 'x'. */
6457 static void
6458 insert_range_assertions (void)
6460 need_assert_for = BITMAP_ALLOC (NULL);
6461 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6463 calculate_dominance_info (CDI_DOMINATORS);
6465 find_assert_locations ();
6466 if (!bitmap_empty_p (need_assert_for))
6468 process_assert_insertions ();
6469 update_ssa (TODO_update_ssa_no_phi);
6472 if (dump_file && (dump_flags & TDF_DETAILS))
6474 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6475 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6478 free (asserts_for);
6479 BITMAP_FREE (need_assert_for);
6482 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6483 and "struct" hacks. If VRP can determine that the
6484 array subscript is a constant, check if it is outside valid
6485 range. If the array subscript is a RANGE, warn if it is
6486 non-overlapping with valid range.
6487 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6489 static void
6490 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6492 value_range *vr = NULL;
6493 tree low_sub, up_sub;
6494 tree low_bound, up_bound, up_bound_p1;
6496 if (TREE_NO_WARNING (ref))
6497 return;
6499 low_sub = up_sub = TREE_OPERAND (ref, 1);
6500 up_bound = array_ref_up_bound (ref);
6502 /* Can not check flexible arrays. */
6503 if (!up_bound
6504 || TREE_CODE (up_bound) != INTEGER_CST)
6505 return;
6507 /* Accesses to trailing arrays via pointers may access storage
6508 beyond the types array bounds. */
6509 if (warn_array_bounds < 2
6510 && array_at_struct_end_p (ref))
6511 return;
6513 low_bound = array_ref_low_bound (ref);
6514 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6515 build_int_cst (TREE_TYPE (up_bound), 1));
6517 /* Empty array. */
6518 if (tree_int_cst_equal (low_bound, up_bound_p1))
6520 warning_at (location, OPT_Warray_bounds,
6521 "array subscript is above array bounds");
6522 TREE_NO_WARNING (ref) = 1;
6525 if (TREE_CODE (low_sub) == SSA_NAME)
6527 vr = get_value_range (low_sub);
6528 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6530 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6531 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6535 if (vr && vr->type == VR_ANTI_RANGE)
6537 if (TREE_CODE (up_sub) == INTEGER_CST
6538 && (ignore_off_by_one
6539 ? tree_int_cst_lt (up_bound, up_sub)
6540 : tree_int_cst_le (up_bound, up_sub))
6541 && TREE_CODE (low_sub) == INTEGER_CST
6542 && tree_int_cst_le (low_sub, low_bound))
6544 warning_at (location, OPT_Warray_bounds,
6545 "array subscript is outside array bounds");
6546 TREE_NO_WARNING (ref) = 1;
6549 else if (TREE_CODE (up_sub) == INTEGER_CST
6550 && (ignore_off_by_one
6551 ? !tree_int_cst_le (up_sub, up_bound_p1)
6552 : !tree_int_cst_le (up_sub, up_bound)))
6554 if (dump_file && (dump_flags & TDF_DETAILS))
6556 fprintf (dump_file, "Array bound warning for ");
6557 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6558 fprintf (dump_file, "\n");
6560 warning_at (location, OPT_Warray_bounds,
6561 "array subscript is above array bounds");
6562 TREE_NO_WARNING (ref) = 1;
6564 else if (TREE_CODE (low_sub) == INTEGER_CST
6565 && tree_int_cst_lt (low_sub, low_bound))
6567 if (dump_file && (dump_flags & TDF_DETAILS))
6569 fprintf (dump_file, "Array bound warning for ");
6570 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6571 fprintf (dump_file, "\n");
6573 warning_at (location, OPT_Warray_bounds,
6574 "array subscript is below array bounds");
6575 TREE_NO_WARNING (ref) = 1;
6579 /* Searches if the expr T, located at LOCATION computes
6580 address of an ARRAY_REF, and call check_array_ref on it. */
6582 static void
6583 search_for_addr_array (tree t, location_t location)
6585 /* Check each ARRAY_REFs in the reference chain. */
6588 if (TREE_CODE (t) == ARRAY_REF)
6589 check_array_ref (location, t, true /*ignore_off_by_one*/);
6591 t = TREE_OPERAND (t, 0);
6593 while (handled_component_p (t));
6595 if (TREE_CODE (t) == MEM_REF
6596 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6597 && !TREE_NO_WARNING (t))
6599 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6600 tree low_bound, up_bound, el_sz;
6601 offset_int idx;
6602 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6603 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6604 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6605 return;
6607 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6608 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6609 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6610 if (!low_bound
6611 || TREE_CODE (low_bound) != INTEGER_CST
6612 || !up_bound
6613 || TREE_CODE (up_bound) != INTEGER_CST
6614 || !el_sz
6615 || TREE_CODE (el_sz) != INTEGER_CST)
6616 return;
6618 idx = mem_ref_offset (t);
6619 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6620 if (idx < 0)
6622 if (dump_file && (dump_flags & TDF_DETAILS))
6624 fprintf (dump_file, "Array bound warning for ");
6625 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6626 fprintf (dump_file, "\n");
6628 warning_at (location, OPT_Warray_bounds,
6629 "array subscript is below array bounds");
6630 TREE_NO_WARNING (t) = 1;
6632 else if (idx > (wi::to_offset (up_bound)
6633 - wi::to_offset (low_bound) + 1))
6635 if (dump_file && (dump_flags & TDF_DETAILS))
6637 fprintf (dump_file, "Array bound warning for ");
6638 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6639 fprintf (dump_file, "\n");
6641 warning_at (location, OPT_Warray_bounds,
6642 "array subscript is above array bounds");
6643 TREE_NO_WARNING (t) = 1;
6648 /* walk_tree() callback that checks if *TP is
6649 an ARRAY_REF inside an ADDR_EXPR (in which an array
6650 subscript one outside the valid range is allowed). Call
6651 check_array_ref for each ARRAY_REF found. The location is
6652 passed in DATA. */
6654 static tree
6655 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6657 tree t = *tp;
6658 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6659 location_t location;
6661 if (EXPR_HAS_LOCATION (t))
6662 location = EXPR_LOCATION (t);
6663 else
6665 location_t *locp = (location_t *) wi->info;
6666 location = *locp;
6669 *walk_subtree = TRUE;
6671 if (TREE_CODE (t) == ARRAY_REF)
6672 check_array_ref (location, t, false /*ignore_off_by_one*/);
6674 else if (TREE_CODE (t) == ADDR_EXPR)
6676 search_for_addr_array (t, location);
6677 *walk_subtree = FALSE;
6680 return NULL_TREE;
6683 /* Walk over all statements of all reachable BBs and call check_array_bounds
6684 on them. */
6686 static void
6687 check_all_array_refs (void)
6689 basic_block bb;
6690 gimple_stmt_iterator si;
6692 FOR_EACH_BB_FN (bb, cfun)
6694 edge_iterator ei;
6695 edge e;
6696 bool executable = false;
6698 /* Skip blocks that were found to be unreachable. */
6699 FOR_EACH_EDGE (e, ei, bb->preds)
6700 executable |= !!(e->flags & EDGE_EXECUTABLE);
6701 if (!executable)
6702 continue;
6704 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6706 gimple *stmt = gsi_stmt (si);
6707 struct walk_stmt_info wi;
6708 if (!gimple_has_location (stmt)
6709 || is_gimple_debug (stmt))
6710 continue;
6712 memset (&wi, 0, sizeof (wi));
6714 location_t loc = gimple_location (stmt);
6715 wi.info = &loc;
6717 walk_gimple_op (gsi_stmt (si),
6718 check_array_bounds,
6719 &wi);
6724 /* Return true if all imm uses of VAR are either in STMT, or
6725 feed (optionally through a chain of single imm uses) GIMPLE_COND
6726 in basic block COND_BB. */
6728 static bool
6729 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6731 use_operand_p use_p, use2_p;
6732 imm_use_iterator iter;
6734 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6735 if (USE_STMT (use_p) != stmt)
6737 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6738 if (is_gimple_debug (use_stmt))
6739 continue;
6740 while (is_gimple_assign (use_stmt)
6741 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6742 && single_imm_use (gimple_assign_lhs (use_stmt),
6743 &use2_p, &use_stmt2))
6744 use_stmt = use_stmt2;
6745 if (gimple_code (use_stmt) != GIMPLE_COND
6746 || gimple_bb (use_stmt) != cond_bb)
6747 return false;
6749 return true;
6752 /* Handle
6753 _4 = x_3 & 31;
6754 if (_4 != 0)
6755 goto <bb 6>;
6756 else
6757 goto <bb 7>;
6758 <bb 6>:
6759 __builtin_unreachable ();
6760 <bb 7>:
6761 x_5 = ASSERT_EXPR <x_3, ...>;
6762 If x_3 has no other immediate uses (checked by caller),
6763 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6764 from the non-zero bitmask. */
6766 static void
6767 maybe_set_nonzero_bits (basic_block bb, tree var)
6769 edge e = single_pred_edge (bb);
6770 basic_block cond_bb = e->src;
6771 gimple *stmt = last_stmt (cond_bb);
6772 tree cst;
6774 if (stmt == NULL
6775 || gimple_code (stmt) != GIMPLE_COND
6776 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6777 ? EQ_EXPR : NE_EXPR)
6778 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6779 || !integer_zerop (gimple_cond_rhs (stmt)))
6780 return;
6782 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6783 if (!is_gimple_assign (stmt)
6784 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6785 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6786 return;
6787 if (gimple_assign_rhs1 (stmt) != var)
6789 gimple *stmt2;
6791 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6792 return;
6793 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6794 if (!gimple_assign_cast_p (stmt2)
6795 || gimple_assign_rhs1 (stmt2) != var
6796 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6797 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6798 != TYPE_PRECISION (TREE_TYPE (var))))
6799 return;
6801 cst = gimple_assign_rhs2 (stmt);
6802 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6805 /* Convert range assertion expressions into the implied copies and
6806 copy propagate away the copies. Doing the trivial copy propagation
6807 here avoids the need to run the full copy propagation pass after
6808 VRP.
6810 FIXME, this will eventually lead to copy propagation removing the
6811 names that had useful range information attached to them. For
6812 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6813 then N_i will have the range [3, +INF].
6815 However, by converting the assertion into the implied copy
6816 operation N_i = N_j, we will then copy-propagate N_j into the uses
6817 of N_i and lose the range information. We may want to hold on to
6818 ASSERT_EXPRs a little while longer as the ranges could be used in
6819 things like jump threading.
6821 The problem with keeping ASSERT_EXPRs around is that passes after
6822 VRP need to handle them appropriately.
6824 Another approach would be to make the range information a first
6825 class property of the SSA_NAME so that it can be queried from
6826 any pass. This is made somewhat more complex by the need for
6827 multiple ranges to be associated with one SSA_NAME. */
6829 static void
6830 remove_range_assertions (void)
6832 basic_block bb;
6833 gimple_stmt_iterator si;
6834 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6835 a basic block preceeded by GIMPLE_COND branching to it and
6836 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6837 int is_unreachable;
6839 /* Note that the BSI iterator bump happens at the bottom of the
6840 loop and no bump is necessary if we're removing the statement
6841 referenced by the current BSI. */
6842 FOR_EACH_BB_FN (bb, cfun)
6843 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6845 gimple *stmt = gsi_stmt (si);
6846 gimple *use_stmt;
6848 if (is_gimple_assign (stmt)
6849 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6851 tree lhs = gimple_assign_lhs (stmt);
6852 tree rhs = gimple_assign_rhs1 (stmt);
6853 tree var;
6854 use_operand_p use_p;
6855 imm_use_iterator iter;
6857 var = ASSERT_EXPR_VAR (rhs);
6858 gcc_assert (TREE_CODE (var) == SSA_NAME);
6860 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6861 && SSA_NAME_RANGE_INFO (lhs))
6863 if (is_unreachable == -1)
6865 is_unreachable = 0;
6866 if (single_pred_p (bb)
6867 && assert_unreachable_fallthru_edge_p
6868 (single_pred_edge (bb)))
6869 is_unreachable = 1;
6871 /* Handle
6872 if (x_7 >= 10 && x_7 < 20)
6873 __builtin_unreachable ();
6874 x_8 = ASSERT_EXPR <x_7, ...>;
6875 if the only uses of x_7 are in the ASSERT_EXPR and
6876 in the condition. In that case, we can copy the
6877 range info from x_8 computed in this pass also
6878 for x_7. */
6879 if (is_unreachable
6880 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6881 single_pred (bb)))
6883 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6884 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6885 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6886 maybe_set_nonzero_bits (bb, var);
6890 /* Propagate the RHS into every use of the LHS. */
6891 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6892 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6893 SET_USE (use_p, var);
6895 /* And finally, remove the copy, it is not needed. */
6896 gsi_remove (&si, true);
6897 release_defs (stmt);
6899 else
6901 if (!is_gimple_debug (gsi_stmt (si)))
6902 is_unreachable = 0;
6903 gsi_next (&si);
6909 /* Return true if STMT is interesting for VRP. */
6911 static bool
6912 stmt_interesting_for_vrp (gimple *stmt)
6914 if (gimple_code (stmt) == GIMPLE_PHI)
6916 tree res = gimple_phi_result (stmt);
6917 return (!virtual_operand_p (res)
6918 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6919 || POINTER_TYPE_P (TREE_TYPE (res))));
6921 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6923 tree lhs = gimple_get_lhs (stmt);
6925 /* In general, assignments with virtual operands are not useful
6926 for deriving ranges, with the obvious exception of calls to
6927 builtin functions. */
6928 if (lhs && TREE_CODE (lhs) == SSA_NAME
6929 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6930 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6931 && (is_gimple_call (stmt)
6932 || !gimple_vuse (stmt)))
6933 return true;
6934 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6935 switch (gimple_call_internal_fn (stmt))
6937 case IFN_ADD_OVERFLOW:
6938 case IFN_SUB_OVERFLOW:
6939 case IFN_MUL_OVERFLOW:
6940 /* These internal calls return _Complex integer type,
6941 but are interesting to VRP nevertheless. */
6942 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6943 return true;
6944 break;
6945 default:
6946 break;
6949 else if (gimple_code (stmt) == GIMPLE_COND
6950 || gimple_code (stmt) == GIMPLE_SWITCH)
6951 return true;
6953 return false;
6957 /* Initialize local data structures for VRP. */
6959 static void
6960 vrp_initialize (void)
6962 basic_block bb;
6964 values_propagated = false;
6965 num_vr_values = num_ssa_names;
6966 vr_value = XCNEWVEC (value_range *, num_vr_values);
6967 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6968 bitmap_obstack_initialize (&vrp_equiv_obstack);
6970 FOR_EACH_BB_FN (bb, cfun)
6972 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6973 gsi_next (&si))
6975 gphi *phi = si.phi ();
6976 if (!stmt_interesting_for_vrp (phi))
6978 tree lhs = PHI_RESULT (phi);
6979 set_value_range_to_varying (get_value_range (lhs));
6980 prop_set_simulate_again (phi, false);
6982 else
6983 prop_set_simulate_again (phi, true);
6986 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
6987 gsi_next (&si))
6989 gimple *stmt = gsi_stmt (si);
6991 /* If the statement is a control insn, then we do not
6992 want to avoid simulating the statement once. Failure
6993 to do so means that those edges will never get added. */
6994 if (stmt_ends_bb_p (stmt))
6995 prop_set_simulate_again (stmt, true);
6996 else if (!stmt_interesting_for_vrp (stmt))
6998 ssa_op_iter i;
6999 tree def;
7000 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7001 set_value_range_to_varying (get_value_range (def));
7002 prop_set_simulate_again (stmt, false);
7004 else
7005 prop_set_simulate_again (stmt, true);
7010 /* Return the singleton value-range for NAME or NAME. */
7012 static inline tree
7013 vrp_valueize (tree name)
7015 if (TREE_CODE (name) == SSA_NAME)
7017 value_range *vr = get_value_range (name);
7018 if (vr->type == VR_RANGE
7019 && vrp_operand_equal_p (vr->min, vr->max))
7020 return vr->min;
7022 return name;
7025 /* Return the singleton value-range for NAME if that is a constant
7026 but signal to not follow SSA edges. */
7028 static inline tree
7029 vrp_valueize_1 (tree name)
7031 if (TREE_CODE (name) == SSA_NAME)
7033 /* If the definition may be simulated again we cannot follow
7034 this SSA edge as the SSA propagator does not necessarily
7035 re-visit the use. */
7036 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7037 if (!gimple_nop_p (def_stmt)
7038 && prop_simulate_again_p (def_stmt))
7039 return NULL_TREE;
7040 value_range *vr = get_value_range (name);
7041 if (range_int_cst_singleton_p (vr))
7042 return vr->min;
7044 return name;
7047 /* Visit assignment STMT. If it produces an interesting range, record
7048 the range in VR and set LHS to OUTPUT_P. */
7050 static void
7051 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p, value_range *vr)
7053 tree lhs;
7054 enum gimple_code code = gimple_code (stmt);
7055 lhs = gimple_get_lhs (stmt);
7056 *output_p = NULL_TREE;
7058 /* We only keep track of ranges in integral and pointer types. */
7059 if (TREE_CODE (lhs) == SSA_NAME
7060 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7061 /* It is valid to have NULL MIN/MAX values on a type. See
7062 build_range_type. */
7063 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7064 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7065 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7067 /* Try folding the statement to a constant first. */
7068 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7069 vrp_valueize_1);
7070 if (tem && is_gimple_min_invariant (tem))
7071 set_value_range_to_value (vr, tem, NULL);
7072 /* Then dispatch to value-range extracting functions. */
7073 else if (code == GIMPLE_CALL)
7074 extract_range_basic (vr, stmt);
7075 else
7076 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
7077 *output_p = lhs;
7081 /* Helper that gets the value range of the SSA_NAME with version I
7082 or a symbolic range containing the SSA_NAME only if the value range
7083 is varying or undefined. */
7085 static inline value_range
7086 get_vr_for_comparison (int i)
7088 value_range vr = *get_value_range (ssa_name (i));
7090 /* If name N_i does not have a valid range, use N_i as its own
7091 range. This allows us to compare against names that may
7092 have N_i in their ranges. */
7093 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7095 vr.type = VR_RANGE;
7096 vr.min = ssa_name (i);
7097 vr.max = ssa_name (i);
7100 return vr;
7103 /* Compare all the value ranges for names equivalent to VAR with VAL
7104 using comparison code COMP. Return the same value returned by
7105 compare_range_with_value, including the setting of
7106 *STRICT_OVERFLOW_P. */
7108 static tree
7109 compare_name_with_value (enum tree_code comp, tree var, tree val,
7110 bool *strict_overflow_p, bool use_equiv_p)
7112 bitmap_iterator bi;
7113 unsigned i;
7114 bitmap e;
7115 tree retval, t;
7116 int used_strict_overflow;
7117 bool sop;
7118 value_range equiv_vr;
7120 /* Get the set of equivalences for VAR. */
7121 e = get_value_range (var)->equiv;
7123 /* Start at -1. Set it to 0 if we do a comparison without relying
7124 on overflow, or 1 if all comparisons rely on overflow. */
7125 used_strict_overflow = -1;
7127 /* Compare vars' value range with val. */
7128 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7129 sop = false;
7130 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7131 if (retval)
7132 used_strict_overflow = sop ? 1 : 0;
7134 /* If the equiv set is empty we have done all work we need to do. */
7135 if (e == NULL)
7137 if (retval
7138 && used_strict_overflow > 0)
7139 *strict_overflow_p = true;
7140 return retval;
7143 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7145 if (! use_equiv_p
7146 && ! SSA_NAME_IS_DEFAULT_DEF (ssa_name (i))
7147 && prop_simulate_again_p (SSA_NAME_DEF_STMT (ssa_name (i))))
7148 continue;
7150 equiv_vr = get_vr_for_comparison (i);
7151 sop = false;
7152 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7153 if (t)
7155 /* If we get different answers from different members
7156 of the equivalence set this check must be in a dead
7157 code region. Folding it to a trap representation
7158 would be correct here. For now just return don't-know. */
7159 if (retval != NULL
7160 && t != retval)
7162 retval = NULL_TREE;
7163 break;
7165 retval = t;
7167 if (!sop)
7168 used_strict_overflow = 0;
7169 else if (used_strict_overflow < 0)
7170 used_strict_overflow = 1;
7174 if (retval
7175 && used_strict_overflow > 0)
7176 *strict_overflow_p = true;
7178 return retval;
7182 /* Given a comparison code COMP and names N1 and N2, compare all the
7183 ranges equivalent to N1 against all the ranges equivalent to N2
7184 to determine the value of N1 COMP N2. Return the same value
7185 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7186 whether we relied on an overflow infinity in the comparison. */
7189 static tree
7190 compare_names (enum tree_code comp, tree n1, tree n2,
7191 bool *strict_overflow_p)
7193 tree t, retval;
7194 bitmap e1, e2;
7195 bitmap_iterator bi1, bi2;
7196 unsigned i1, i2;
7197 int used_strict_overflow;
7198 static bitmap_obstack *s_obstack = NULL;
7199 static bitmap s_e1 = NULL, s_e2 = NULL;
7201 /* Compare the ranges of every name equivalent to N1 against the
7202 ranges of every name equivalent to N2. */
7203 e1 = get_value_range (n1)->equiv;
7204 e2 = get_value_range (n2)->equiv;
7206 /* Use the fake bitmaps if e1 or e2 are not available. */
7207 if (s_obstack == NULL)
7209 s_obstack = XNEW (bitmap_obstack);
7210 bitmap_obstack_initialize (s_obstack);
7211 s_e1 = BITMAP_ALLOC (s_obstack);
7212 s_e2 = BITMAP_ALLOC (s_obstack);
7214 if (e1 == NULL)
7215 e1 = s_e1;
7216 if (e2 == NULL)
7217 e2 = s_e2;
7219 /* Add N1 and N2 to their own set of equivalences to avoid
7220 duplicating the body of the loop just to check N1 and N2
7221 ranges. */
7222 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7223 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7225 /* If the equivalence sets have a common intersection, then the two
7226 names can be compared without checking their ranges. */
7227 if (bitmap_intersect_p (e1, e2))
7229 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7230 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7232 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7233 ? boolean_true_node
7234 : boolean_false_node;
7237 /* Start at -1. Set it to 0 if we do a comparison without relying
7238 on overflow, or 1 if all comparisons rely on overflow. */
7239 used_strict_overflow = -1;
7241 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7242 N2 to their own set of equivalences to avoid duplicating the body
7243 of the loop just to check N1 and N2 ranges. */
7244 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7246 value_range vr1 = get_vr_for_comparison (i1);
7248 t = retval = NULL_TREE;
7249 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7251 bool sop = false;
7253 value_range vr2 = get_vr_for_comparison (i2);
7255 t = compare_ranges (comp, &vr1, &vr2, &sop);
7256 if (t)
7258 /* If we get different answers from different members
7259 of the equivalence set this check must be in a dead
7260 code region. Folding it to a trap representation
7261 would be correct here. For now just return don't-know. */
7262 if (retval != NULL
7263 && t != retval)
7265 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7266 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7267 return NULL_TREE;
7269 retval = t;
7271 if (!sop)
7272 used_strict_overflow = 0;
7273 else if (used_strict_overflow < 0)
7274 used_strict_overflow = 1;
7278 if (retval)
7280 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7281 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7282 if (used_strict_overflow > 0)
7283 *strict_overflow_p = true;
7284 return retval;
7288 /* None of the equivalent ranges are useful in computing this
7289 comparison. */
7290 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7291 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7292 return NULL_TREE;
7295 /* Helper function for vrp_evaluate_conditional_warnv & other
7296 optimizers. */
7298 static tree
7299 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7300 tree op0, tree op1,
7301 bool * strict_overflow_p)
7303 value_range *vr0, *vr1;
7305 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7306 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7308 tree res = NULL_TREE;
7309 if (vr0 && vr1)
7310 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7311 if (!res && vr0)
7312 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7313 if (!res && vr1)
7314 res = (compare_range_with_value
7315 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7316 return res;
7319 /* Helper function for vrp_evaluate_conditional_warnv. */
7321 static tree
7322 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7323 tree op1, bool use_equiv_p,
7324 bool *strict_overflow_p, bool *only_ranges)
7326 tree ret;
7327 if (only_ranges)
7328 *only_ranges = true;
7330 /* We only deal with integral and pointer types. */
7331 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7332 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7333 return NULL_TREE;
7335 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7336 (code, op0, op1, strict_overflow_p)))
7337 return ret;
7338 if (only_ranges)
7339 *only_ranges = false;
7340 /* Do not use compare_names during propagation, it's quadratic. */
7341 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7342 && use_equiv_p)
7343 return compare_names (code, op0, op1, strict_overflow_p);
7344 else if (TREE_CODE (op0) == SSA_NAME)
7345 return compare_name_with_value (code, op0, op1,
7346 strict_overflow_p, use_equiv_p);
7347 else if (TREE_CODE (op1) == SSA_NAME)
7348 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7349 strict_overflow_p, use_equiv_p);
7350 return NULL_TREE;
7353 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7354 information. Return NULL if the conditional can not be evaluated.
7355 The ranges of all the names equivalent with the operands in COND
7356 will be used when trying to compute the value. If the result is
7357 based on undefined signed overflow, issue a warning if
7358 appropriate. */
7360 static tree
7361 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7363 bool sop;
7364 tree ret;
7365 bool only_ranges;
7367 /* Some passes and foldings leak constants with overflow flag set
7368 into the IL. Avoid doing wrong things with these and bail out. */
7369 if ((TREE_CODE (op0) == INTEGER_CST
7370 && TREE_OVERFLOW (op0))
7371 || (TREE_CODE (op1) == INTEGER_CST
7372 && TREE_OVERFLOW (op1)))
7373 return NULL_TREE;
7375 sop = false;
7376 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7377 &only_ranges);
7379 if (ret && sop)
7381 enum warn_strict_overflow_code wc;
7382 const char* warnmsg;
7384 if (is_gimple_min_invariant (ret))
7386 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7387 warnmsg = G_("assuming signed overflow does not occur when "
7388 "simplifying conditional to constant");
7390 else
7392 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7393 warnmsg = G_("assuming signed overflow does not occur when "
7394 "simplifying conditional");
7397 if (issue_strict_overflow_warning (wc))
7399 location_t location;
7401 if (!gimple_has_location (stmt))
7402 location = input_location;
7403 else
7404 location = gimple_location (stmt);
7405 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7409 if (warn_type_limits
7410 && ret && only_ranges
7411 && TREE_CODE_CLASS (code) == tcc_comparison
7412 && TREE_CODE (op0) == SSA_NAME)
7414 /* If the comparison is being folded and the operand on the LHS
7415 is being compared against a constant value that is outside of
7416 the natural range of OP0's type, then the predicate will
7417 always fold regardless of the value of OP0. If -Wtype-limits
7418 was specified, emit a warning. */
7419 tree type = TREE_TYPE (op0);
7420 value_range *vr0 = get_value_range (op0);
7422 if (vr0->type == VR_RANGE
7423 && INTEGRAL_TYPE_P (type)
7424 && vrp_val_is_min (vr0->min)
7425 && vrp_val_is_max (vr0->max)
7426 && is_gimple_min_invariant (op1))
7428 location_t location;
7430 if (!gimple_has_location (stmt))
7431 location = input_location;
7432 else
7433 location = gimple_location (stmt);
7435 warning_at (location, OPT_Wtype_limits,
7436 integer_zerop (ret)
7437 ? G_("comparison always false "
7438 "due to limited range of data type")
7439 : G_("comparison always true "
7440 "due to limited range of data type"));
7444 return ret;
7448 /* Visit conditional statement STMT. If we can determine which edge
7449 will be taken out of STMT's basic block, record it in
7450 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7452 static void
7453 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7455 tree val;
7456 bool sop;
7458 *taken_edge_p = NULL;
7460 if (dump_file && (dump_flags & TDF_DETAILS))
7462 tree use;
7463 ssa_op_iter i;
7465 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7466 print_gimple_stmt (dump_file, stmt, 0, 0);
7467 fprintf (dump_file, "\nWith known ranges\n");
7469 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7471 fprintf (dump_file, "\t");
7472 print_generic_expr (dump_file, use, 0);
7473 fprintf (dump_file, ": ");
7474 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7477 fprintf (dump_file, "\n");
7480 /* Compute the value of the predicate COND by checking the known
7481 ranges of each of its operands.
7483 Note that we cannot evaluate all the equivalent ranges here
7484 because those ranges may not yet be final and with the current
7485 propagation strategy, we cannot determine when the value ranges
7486 of the names in the equivalence set have changed.
7488 For instance, given the following code fragment
7490 i_5 = PHI <8, i_13>
7492 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7493 if (i_14 == 1)
7496 Assume that on the first visit to i_14, i_5 has the temporary
7497 range [8, 8] because the second argument to the PHI function is
7498 not yet executable. We derive the range ~[0, 0] for i_14 and the
7499 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7500 the first time, since i_14 is equivalent to the range [8, 8], we
7501 determine that the predicate is always false.
7503 On the next round of propagation, i_13 is determined to be
7504 VARYING, which causes i_5 to drop down to VARYING. So, another
7505 visit to i_14 is scheduled. In this second visit, we compute the
7506 exact same range and equivalence set for i_14, namely ~[0, 0] and
7507 { i_5 }. But we did not have the previous range for i_5
7508 registered, so vrp_visit_assignment thinks that the range for
7509 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7510 is not visited again, which stops propagation from visiting
7511 statements in the THEN clause of that if().
7513 To properly fix this we would need to keep the previous range
7514 value for the names in the equivalence set. This way we would've
7515 discovered that from one visit to the other i_5 changed from
7516 range [8, 8] to VR_VARYING.
7518 However, fixing this apparent limitation may not be worth the
7519 additional checking. Testing on several code bases (GCC, DLV,
7520 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7521 4 more predicates folded in SPEC. */
7522 sop = false;
7524 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7525 gimple_cond_lhs (stmt),
7526 gimple_cond_rhs (stmt),
7527 false, &sop, NULL);
7528 if (val)
7530 if (!sop)
7531 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7532 else
7534 if (dump_file && (dump_flags & TDF_DETAILS))
7535 fprintf (dump_file,
7536 "\nIgnoring predicate evaluation because "
7537 "it assumes that signed overflow is undefined");
7538 val = NULL_TREE;
7542 if (dump_file && (dump_flags & TDF_DETAILS))
7544 fprintf (dump_file, "\nPredicate evaluates to: ");
7545 if (val == NULL_TREE)
7546 fprintf (dump_file, "DON'T KNOW\n");
7547 else
7548 print_generic_stmt (dump_file, val, 0);
7552 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7553 that includes the value VAL. The search is restricted to the range
7554 [START_IDX, n - 1] where n is the size of VEC.
7556 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7557 returned.
7559 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7560 it is placed in IDX and false is returned.
7562 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7563 returned. */
7565 static bool
7566 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7568 size_t n = gimple_switch_num_labels (stmt);
7569 size_t low, high;
7571 /* Find case label for minimum of the value range or the next one.
7572 At each iteration we are searching in [low, high - 1]. */
7574 for (low = start_idx, high = n; high != low; )
7576 tree t;
7577 int cmp;
7578 /* Note that i != high, so we never ask for n. */
7579 size_t i = (high + low) / 2;
7580 t = gimple_switch_label (stmt, i);
7582 /* Cache the result of comparing CASE_LOW and val. */
7583 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7585 if (cmp == 0)
7587 /* Ranges cannot be empty. */
7588 *idx = i;
7589 return true;
7591 else if (cmp > 0)
7592 high = i;
7593 else
7595 low = i + 1;
7596 if (CASE_HIGH (t) != NULL
7597 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7599 *idx = i;
7600 return true;
7605 *idx = high;
7606 return false;
7609 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7610 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7611 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7612 then MAX_IDX < MIN_IDX.
7613 Returns true if the default label is not needed. */
7615 static bool
7616 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7617 size_t *max_idx)
7619 size_t i, j;
7620 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7621 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7623 if (i == j
7624 && min_take_default
7625 && max_take_default)
7627 /* Only the default case label reached.
7628 Return an empty range. */
7629 *min_idx = 1;
7630 *max_idx = 0;
7631 return false;
7633 else
7635 bool take_default = min_take_default || max_take_default;
7636 tree low, high;
7637 size_t k;
7639 if (max_take_default)
7640 j--;
7642 /* If the case label range is continuous, we do not need
7643 the default case label. Verify that. */
7644 high = CASE_LOW (gimple_switch_label (stmt, i));
7645 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7646 high = CASE_HIGH (gimple_switch_label (stmt, i));
7647 for (k = i + 1; k <= j; ++k)
7649 low = CASE_LOW (gimple_switch_label (stmt, k));
7650 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7652 take_default = true;
7653 break;
7655 high = low;
7656 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7657 high = CASE_HIGH (gimple_switch_label (stmt, k));
7660 *min_idx = i;
7661 *max_idx = j;
7662 return !take_default;
7666 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7667 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7668 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7669 Returns true if the default label is not needed. */
7671 static bool
7672 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7673 size_t *max_idx1, size_t *min_idx2,
7674 size_t *max_idx2)
7676 size_t i, j, k, l;
7677 unsigned int n = gimple_switch_num_labels (stmt);
7678 bool take_default;
7679 tree case_low, case_high;
7680 tree min = vr->min, max = vr->max;
7682 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7684 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7686 /* Set second range to emtpy. */
7687 *min_idx2 = 1;
7688 *max_idx2 = 0;
7690 if (vr->type == VR_RANGE)
7692 *min_idx1 = i;
7693 *max_idx1 = j;
7694 return !take_default;
7697 /* Set first range to all case labels. */
7698 *min_idx1 = 1;
7699 *max_idx1 = n - 1;
7701 if (i > j)
7702 return false;
7704 /* Make sure all the values of case labels [i , j] are contained in
7705 range [MIN, MAX]. */
7706 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7707 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7708 if (tree_int_cst_compare (case_low, min) < 0)
7709 i += 1;
7710 if (case_high != NULL_TREE
7711 && tree_int_cst_compare (max, case_high) < 0)
7712 j -= 1;
7714 if (i > j)
7715 return false;
7717 /* If the range spans case labels [i, j], the corresponding anti-range spans
7718 the labels [1, i - 1] and [j + 1, n - 1]. */
7719 k = j + 1;
7720 l = n - 1;
7721 if (k > l)
7723 k = 1;
7724 l = 0;
7727 j = i - 1;
7728 i = 1;
7729 if (i > j)
7731 i = k;
7732 j = l;
7733 k = 1;
7734 l = 0;
7737 *min_idx1 = i;
7738 *max_idx1 = j;
7739 *min_idx2 = k;
7740 *max_idx2 = l;
7741 return false;
7744 /* Visit switch statement STMT. If we can determine which edge
7745 will be taken out of STMT's basic block, record it in
7746 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7748 static void
7749 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7751 tree op, val;
7752 value_range *vr;
7753 size_t i = 0, j = 0, k, l;
7754 bool take_default;
7756 *taken_edge_p = NULL;
7757 op = gimple_switch_index (stmt);
7758 if (TREE_CODE (op) != SSA_NAME)
7759 return;
7761 vr = get_value_range (op);
7762 if (dump_file && (dump_flags & TDF_DETAILS))
7764 fprintf (dump_file, "\nVisiting switch expression with operand ");
7765 print_generic_expr (dump_file, op, 0);
7766 fprintf (dump_file, " with known range ");
7767 dump_value_range (dump_file, vr);
7768 fprintf (dump_file, "\n");
7771 if ((vr->type != VR_RANGE
7772 && vr->type != VR_ANTI_RANGE)
7773 || symbolic_range_p (vr))
7774 return;
7776 /* Find the single edge that is taken from the switch expression. */
7777 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7779 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7780 label */
7781 if (j < i)
7783 gcc_assert (take_default);
7784 val = gimple_switch_default_label (stmt);
7786 else
7788 /* Check if labels with index i to j and maybe the default label
7789 are all reaching the same label. */
7791 val = gimple_switch_label (stmt, i);
7792 if (take_default
7793 && CASE_LABEL (gimple_switch_default_label (stmt))
7794 != CASE_LABEL (val))
7796 if (dump_file && (dump_flags & TDF_DETAILS))
7797 fprintf (dump_file, " not a single destination for this "
7798 "range\n");
7799 return;
7801 for (++i; i <= j; ++i)
7803 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7805 if (dump_file && (dump_flags & TDF_DETAILS))
7806 fprintf (dump_file, " not a single destination for this "
7807 "range\n");
7808 return;
7811 for (; k <= l; ++k)
7813 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7815 if (dump_file && (dump_flags & TDF_DETAILS))
7816 fprintf (dump_file, " not a single destination for this "
7817 "range\n");
7818 return;
7823 *taken_edge_p = find_edge (gimple_bb (stmt),
7824 label_to_block (CASE_LABEL (val)));
7826 if (dump_file && (dump_flags & TDF_DETAILS))
7828 fprintf (dump_file, " will take edge to ");
7829 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7834 /* Evaluate statement STMT. If the statement produces a useful range,
7835 set VR and corepsponding OUTPUT_P.
7837 If STMT is a conditional branch and we can determine its truth
7838 value, the taken edge is recorded in *TAKEN_EDGE_P. */
7840 static void
7841 extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
7842 tree *output_p, value_range *vr)
7845 if (dump_file && (dump_flags & TDF_DETAILS))
7847 fprintf (dump_file, "\nVisiting statement:\n");
7848 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7851 if (!stmt_interesting_for_vrp (stmt))
7852 gcc_assert (stmt_ends_bb_p (stmt));
7853 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7854 vrp_visit_assignment_or_call (stmt, output_p, vr);
7855 else if (gimple_code (stmt) == GIMPLE_COND)
7856 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7857 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7858 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7861 /* Evaluate statement STMT. If the statement produces a useful range,
7862 return SSA_PROP_INTERESTING and record the SSA name with the
7863 interesting range into *OUTPUT_P.
7865 If STMT is a conditional branch and we can determine its truth
7866 value, the taken edge is recorded in *TAKEN_EDGE_P.
7868 If STMT produces a varying value, return SSA_PROP_VARYING. */
7870 static enum ssa_prop_result
7871 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7873 value_range vr = VR_INITIALIZER;
7874 tree lhs = gimple_get_lhs (stmt);
7875 tree def;
7876 ssa_op_iter iter;
7877 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
7879 if (*output_p)
7881 if (update_value_range (*output_p, &vr))
7883 if (dump_file && (dump_flags & TDF_DETAILS))
7885 fprintf (dump_file, "Found new range for ");
7886 print_generic_expr (dump_file, *output_p, 0);
7887 fprintf (dump_file, ": ");
7888 dump_value_range (dump_file, &vr);
7889 fprintf (dump_file, "\n");
7892 if (vr.type == VR_VARYING)
7893 return SSA_PROP_VARYING;
7895 return SSA_PROP_INTERESTING;
7897 return SSA_PROP_NOT_INTERESTING;
7900 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7901 switch (gimple_call_internal_fn (stmt))
7903 case IFN_ADD_OVERFLOW:
7904 case IFN_SUB_OVERFLOW:
7905 case IFN_MUL_OVERFLOW:
7906 /* These internal calls return _Complex integer type,
7907 which VRP does not track, but the immediate uses
7908 thereof might be interesting. */
7909 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7911 imm_use_iterator iter;
7912 use_operand_p use_p;
7913 enum ssa_prop_result res = SSA_PROP_VARYING;
7915 set_value_range_to_varying (get_value_range (lhs));
7917 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7919 gimple *use_stmt = USE_STMT (use_p);
7920 if (!is_gimple_assign (use_stmt))
7921 continue;
7922 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7923 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7924 continue;
7925 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7926 tree use_lhs = gimple_assign_lhs (use_stmt);
7927 if (TREE_CODE (rhs1) != rhs_code
7928 || TREE_OPERAND (rhs1, 0) != lhs
7929 || TREE_CODE (use_lhs) != SSA_NAME
7930 || !stmt_interesting_for_vrp (use_stmt)
7931 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7932 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7933 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7934 continue;
7936 /* If there is a change in the value range for any of the
7937 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7938 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7939 or IMAGPART_EXPR immediate uses, but none of them have
7940 a change in their value ranges, return
7941 SSA_PROP_NOT_INTERESTING. If there are no
7942 {REAL,IMAG}PART_EXPR uses at all,
7943 return SSA_PROP_VARYING. */
7944 value_range new_vr = VR_INITIALIZER;
7945 extract_range_basic (&new_vr, use_stmt);
7946 value_range *old_vr = get_value_range (use_lhs);
7947 if (old_vr->type != new_vr.type
7948 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7949 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7950 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7951 res = SSA_PROP_INTERESTING;
7952 else
7953 res = SSA_PROP_NOT_INTERESTING;
7954 BITMAP_FREE (new_vr.equiv);
7955 if (res == SSA_PROP_INTERESTING)
7957 *output_p = lhs;
7958 return res;
7962 return res;
7964 break;
7965 default:
7966 break;
7969 /* All other statements produce nothing of interest for VRP, so mark
7970 their outputs varying and prevent further simulation. */
7971 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7972 set_value_range_to_varying (get_value_range (def));
7974 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7977 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7978 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7979 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7980 possible such range. The resulting range is not canonicalized. */
7982 static void
7983 union_ranges (enum value_range_type *vr0type,
7984 tree *vr0min, tree *vr0max,
7985 enum value_range_type vr1type,
7986 tree vr1min, tree vr1max)
7988 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
7989 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
7991 /* [] is vr0, () is vr1 in the following classification comments. */
7992 if (mineq && maxeq)
7994 /* [( )] */
7995 if (*vr0type == vr1type)
7996 /* Nothing to do for equal ranges. */
7998 else if ((*vr0type == VR_RANGE
7999 && vr1type == VR_ANTI_RANGE)
8000 || (*vr0type == VR_ANTI_RANGE
8001 && vr1type == VR_RANGE))
8003 /* For anti-range with range union the result is varying. */
8004 goto give_up;
8006 else
8007 gcc_unreachable ();
8009 else if (operand_less_p (*vr0max, vr1min) == 1
8010 || operand_less_p (vr1max, *vr0min) == 1)
8012 /* [ ] ( ) or ( ) [ ]
8013 If the ranges have an empty intersection, result of the union
8014 operation is the anti-range or if both are anti-ranges
8015 it covers all. */
8016 if (*vr0type == VR_ANTI_RANGE
8017 && vr1type == VR_ANTI_RANGE)
8018 goto give_up;
8019 else if (*vr0type == VR_ANTI_RANGE
8020 && vr1type == VR_RANGE)
8022 else if (*vr0type == VR_RANGE
8023 && vr1type == VR_ANTI_RANGE)
8025 *vr0type = vr1type;
8026 *vr0min = vr1min;
8027 *vr0max = vr1max;
8029 else if (*vr0type == VR_RANGE
8030 && vr1type == VR_RANGE)
8032 /* The result is the convex hull of both ranges. */
8033 if (operand_less_p (*vr0max, vr1min) == 1)
8035 /* If the result can be an anti-range, create one. */
8036 if (TREE_CODE (*vr0max) == INTEGER_CST
8037 && TREE_CODE (vr1min) == INTEGER_CST
8038 && vrp_val_is_min (*vr0min)
8039 && vrp_val_is_max (vr1max))
8041 tree min = int_const_binop (PLUS_EXPR,
8042 *vr0max,
8043 build_int_cst (TREE_TYPE (*vr0max), 1));
8044 tree max = int_const_binop (MINUS_EXPR,
8045 vr1min,
8046 build_int_cst (TREE_TYPE (vr1min), 1));
8047 if (!operand_less_p (max, min))
8049 *vr0type = VR_ANTI_RANGE;
8050 *vr0min = min;
8051 *vr0max = max;
8053 else
8054 *vr0max = vr1max;
8056 else
8057 *vr0max = vr1max;
8059 else
8061 /* If the result can be an anti-range, create one. */
8062 if (TREE_CODE (vr1max) == INTEGER_CST
8063 && TREE_CODE (*vr0min) == INTEGER_CST
8064 && vrp_val_is_min (vr1min)
8065 && vrp_val_is_max (*vr0max))
8067 tree min = int_const_binop (PLUS_EXPR,
8068 vr1max,
8069 build_int_cst (TREE_TYPE (vr1max), 1));
8070 tree max = int_const_binop (MINUS_EXPR,
8071 *vr0min,
8072 build_int_cst (TREE_TYPE (*vr0min), 1));
8073 if (!operand_less_p (max, min))
8075 *vr0type = VR_ANTI_RANGE;
8076 *vr0min = min;
8077 *vr0max = max;
8079 else
8080 *vr0min = vr1min;
8082 else
8083 *vr0min = vr1min;
8086 else
8087 gcc_unreachable ();
8089 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8090 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8092 /* [ ( ) ] or [( ) ] or [ ( )] */
8093 if (*vr0type == VR_RANGE
8094 && vr1type == VR_RANGE)
8096 else if (*vr0type == VR_ANTI_RANGE
8097 && vr1type == VR_ANTI_RANGE)
8099 *vr0type = vr1type;
8100 *vr0min = vr1min;
8101 *vr0max = vr1max;
8103 else if (*vr0type == VR_ANTI_RANGE
8104 && vr1type == VR_RANGE)
8106 /* Arbitrarily choose the right or left gap. */
8107 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8108 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8109 build_int_cst (TREE_TYPE (vr1min), 1));
8110 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8111 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8112 build_int_cst (TREE_TYPE (vr1max), 1));
8113 else
8114 goto give_up;
8116 else if (*vr0type == VR_RANGE
8117 && vr1type == VR_ANTI_RANGE)
8118 /* The result covers everything. */
8119 goto give_up;
8120 else
8121 gcc_unreachable ();
8123 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8124 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8126 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8127 if (*vr0type == VR_RANGE
8128 && vr1type == VR_RANGE)
8130 *vr0type = vr1type;
8131 *vr0min = vr1min;
8132 *vr0max = vr1max;
8134 else if (*vr0type == VR_ANTI_RANGE
8135 && vr1type == VR_ANTI_RANGE)
8137 else if (*vr0type == VR_RANGE
8138 && vr1type == VR_ANTI_RANGE)
8140 *vr0type = VR_ANTI_RANGE;
8141 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8143 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8144 build_int_cst (TREE_TYPE (*vr0min), 1));
8145 *vr0min = vr1min;
8147 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8149 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8150 build_int_cst (TREE_TYPE (*vr0max), 1));
8151 *vr0max = vr1max;
8153 else
8154 goto give_up;
8156 else if (*vr0type == VR_ANTI_RANGE
8157 && vr1type == VR_RANGE)
8158 /* The result covers everything. */
8159 goto give_up;
8160 else
8161 gcc_unreachable ();
8163 else if ((operand_less_p (vr1min, *vr0max) == 1
8164 || operand_equal_p (vr1min, *vr0max, 0))
8165 && operand_less_p (*vr0min, vr1min) == 1
8166 && operand_less_p (*vr0max, vr1max) == 1)
8168 /* [ ( ] ) or [ ]( ) */
8169 if (*vr0type == VR_RANGE
8170 && vr1type == VR_RANGE)
8171 *vr0max = vr1max;
8172 else if (*vr0type == VR_ANTI_RANGE
8173 && vr1type == VR_ANTI_RANGE)
8174 *vr0min = vr1min;
8175 else if (*vr0type == VR_ANTI_RANGE
8176 && vr1type == VR_RANGE)
8178 if (TREE_CODE (vr1min) == INTEGER_CST)
8179 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8180 build_int_cst (TREE_TYPE (vr1min), 1));
8181 else
8182 goto give_up;
8184 else if (*vr0type == VR_RANGE
8185 && vr1type == VR_ANTI_RANGE)
8187 if (TREE_CODE (*vr0max) == INTEGER_CST)
8189 *vr0type = vr1type;
8190 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8191 build_int_cst (TREE_TYPE (*vr0max), 1));
8192 *vr0max = vr1max;
8194 else
8195 goto give_up;
8197 else
8198 gcc_unreachable ();
8200 else if ((operand_less_p (*vr0min, vr1max) == 1
8201 || operand_equal_p (*vr0min, vr1max, 0))
8202 && operand_less_p (vr1min, *vr0min) == 1
8203 && operand_less_p (vr1max, *vr0max) == 1)
8205 /* ( [ ) ] or ( )[ ] */
8206 if (*vr0type == VR_RANGE
8207 && vr1type == VR_RANGE)
8208 *vr0min = vr1min;
8209 else if (*vr0type == VR_ANTI_RANGE
8210 && vr1type == VR_ANTI_RANGE)
8211 *vr0max = vr1max;
8212 else if (*vr0type == VR_ANTI_RANGE
8213 && vr1type == VR_RANGE)
8215 if (TREE_CODE (vr1max) == INTEGER_CST)
8216 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8217 build_int_cst (TREE_TYPE (vr1max), 1));
8218 else
8219 goto give_up;
8221 else if (*vr0type == VR_RANGE
8222 && vr1type == VR_ANTI_RANGE)
8224 if (TREE_CODE (*vr0min) == INTEGER_CST)
8226 *vr0type = vr1type;
8227 *vr0min = vr1min;
8228 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8229 build_int_cst (TREE_TYPE (*vr0min), 1));
8231 else
8232 goto give_up;
8234 else
8235 gcc_unreachable ();
8237 else
8238 goto give_up;
8240 return;
8242 give_up:
8243 *vr0type = VR_VARYING;
8244 *vr0min = NULL_TREE;
8245 *vr0max = NULL_TREE;
8248 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8249 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8250 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8251 possible such range. The resulting range is not canonicalized. */
8253 static void
8254 intersect_ranges (enum value_range_type *vr0type,
8255 tree *vr0min, tree *vr0max,
8256 enum value_range_type vr1type,
8257 tree vr1min, tree vr1max)
8259 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8260 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8262 /* [] is vr0, () is vr1 in the following classification comments. */
8263 if (mineq && maxeq)
8265 /* [( )] */
8266 if (*vr0type == vr1type)
8267 /* Nothing to do for equal ranges. */
8269 else if ((*vr0type == VR_RANGE
8270 && vr1type == VR_ANTI_RANGE)
8271 || (*vr0type == VR_ANTI_RANGE
8272 && vr1type == VR_RANGE))
8274 /* For anti-range with range intersection the result is empty. */
8275 *vr0type = VR_UNDEFINED;
8276 *vr0min = NULL_TREE;
8277 *vr0max = NULL_TREE;
8279 else
8280 gcc_unreachable ();
8282 else if (operand_less_p (*vr0max, vr1min) == 1
8283 || operand_less_p (vr1max, *vr0min) == 1)
8285 /* [ ] ( ) or ( ) [ ]
8286 If the ranges have an empty intersection, the result of the
8287 intersect operation is the range for intersecting an
8288 anti-range with a range or empty when intersecting two ranges. */
8289 if (*vr0type == VR_RANGE
8290 && vr1type == VR_ANTI_RANGE)
8292 else if (*vr0type == VR_ANTI_RANGE
8293 && vr1type == VR_RANGE)
8295 *vr0type = vr1type;
8296 *vr0min = vr1min;
8297 *vr0max = vr1max;
8299 else if (*vr0type == VR_RANGE
8300 && vr1type == VR_RANGE)
8302 *vr0type = VR_UNDEFINED;
8303 *vr0min = NULL_TREE;
8304 *vr0max = NULL_TREE;
8306 else if (*vr0type == VR_ANTI_RANGE
8307 && vr1type == VR_ANTI_RANGE)
8309 /* If the anti-ranges are adjacent to each other merge them. */
8310 if (TREE_CODE (*vr0max) == INTEGER_CST
8311 && TREE_CODE (vr1min) == INTEGER_CST
8312 && operand_less_p (*vr0max, vr1min) == 1
8313 && integer_onep (int_const_binop (MINUS_EXPR,
8314 vr1min, *vr0max)))
8315 *vr0max = vr1max;
8316 else if (TREE_CODE (vr1max) == INTEGER_CST
8317 && TREE_CODE (*vr0min) == INTEGER_CST
8318 && operand_less_p (vr1max, *vr0min) == 1
8319 && integer_onep (int_const_binop (MINUS_EXPR,
8320 *vr0min, vr1max)))
8321 *vr0min = vr1min;
8322 /* Else arbitrarily take VR0. */
8325 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8326 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8328 /* [ ( ) ] or [( ) ] or [ ( )] */
8329 if (*vr0type == VR_RANGE
8330 && vr1type == VR_RANGE)
8332 /* If both are ranges the result is the inner one. */
8333 *vr0type = vr1type;
8334 *vr0min = vr1min;
8335 *vr0max = vr1max;
8337 else if (*vr0type == VR_RANGE
8338 && vr1type == VR_ANTI_RANGE)
8340 /* Choose the right gap if the left one is empty. */
8341 if (mineq)
8343 if (TREE_CODE (vr1max) == INTEGER_CST)
8344 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8345 build_int_cst (TREE_TYPE (vr1max), 1));
8346 else
8347 *vr0min = vr1max;
8349 /* Choose the left gap if the right one is empty. */
8350 else if (maxeq)
8352 if (TREE_CODE (vr1min) == INTEGER_CST)
8353 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8354 build_int_cst (TREE_TYPE (vr1min), 1));
8355 else
8356 *vr0max = vr1min;
8358 /* Choose the anti-range if the range is effectively varying. */
8359 else if (vrp_val_is_min (*vr0min)
8360 && vrp_val_is_max (*vr0max))
8362 *vr0type = vr1type;
8363 *vr0min = vr1min;
8364 *vr0max = vr1max;
8366 /* Else choose the range. */
8368 else if (*vr0type == VR_ANTI_RANGE
8369 && vr1type == VR_ANTI_RANGE)
8370 /* If both are anti-ranges the result is the outer one. */
8372 else if (*vr0type == VR_ANTI_RANGE
8373 && vr1type == VR_RANGE)
8375 /* The intersection is empty. */
8376 *vr0type = VR_UNDEFINED;
8377 *vr0min = NULL_TREE;
8378 *vr0max = NULL_TREE;
8380 else
8381 gcc_unreachable ();
8383 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8384 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8386 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8387 if (*vr0type == VR_RANGE
8388 && vr1type == VR_RANGE)
8389 /* Choose the inner range. */
8391 else if (*vr0type == VR_ANTI_RANGE
8392 && vr1type == VR_RANGE)
8394 /* Choose the right gap if the left is empty. */
8395 if (mineq)
8397 *vr0type = VR_RANGE;
8398 if (TREE_CODE (*vr0max) == INTEGER_CST)
8399 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8400 build_int_cst (TREE_TYPE (*vr0max), 1));
8401 else
8402 *vr0min = *vr0max;
8403 *vr0max = vr1max;
8405 /* Choose the left gap if the right is empty. */
8406 else if (maxeq)
8408 *vr0type = VR_RANGE;
8409 if (TREE_CODE (*vr0min) == INTEGER_CST)
8410 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8411 build_int_cst (TREE_TYPE (*vr0min), 1));
8412 else
8413 *vr0max = *vr0min;
8414 *vr0min = vr1min;
8416 /* Choose the anti-range if the range is effectively varying. */
8417 else if (vrp_val_is_min (vr1min)
8418 && vrp_val_is_max (vr1max))
8420 /* Else choose the range. */
8421 else
8423 *vr0type = vr1type;
8424 *vr0min = vr1min;
8425 *vr0max = vr1max;
8428 else if (*vr0type == VR_ANTI_RANGE
8429 && vr1type == VR_ANTI_RANGE)
8431 /* If both are anti-ranges the result is the outer one. */
8432 *vr0type = vr1type;
8433 *vr0min = vr1min;
8434 *vr0max = vr1max;
8436 else if (vr1type == VR_ANTI_RANGE
8437 && *vr0type == VR_RANGE)
8439 /* The intersection is empty. */
8440 *vr0type = VR_UNDEFINED;
8441 *vr0min = NULL_TREE;
8442 *vr0max = NULL_TREE;
8444 else
8445 gcc_unreachable ();
8447 else if ((operand_less_p (vr1min, *vr0max) == 1
8448 || operand_equal_p (vr1min, *vr0max, 0))
8449 && operand_less_p (*vr0min, vr1min) == 1)
8451 /* [ ( ] ) or [ ]( ) */
8452 if (*vr0type == VR_ANTI_RANGE
8453 && vr1type == VR_ANTI_RANGE)
8454 *vr0max = vr1max;
8455 else if (*vr0type == VR_RANGE
8456 && vr1type == VR_RANGE)
8457 *vr0min = vr1min;
8458 else if (*vr0type == VR_RANGE
8459 && vr1type == VR_ANTI_RANGE)
8461 if (TREE_CODE (vr1min) == INTEGER_CST)
8462 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8463 build_int_cst (TREE_TYPE (vr1min), 1));
8464 else
8465 *vr0max = vr1min;
8467 else if (*vr0type == VR_ANTI_RANGE
8468 && vr1type == VR_RANGE)
8470 *vr0type = VR_RANGE;
8471 if (TREE_CODE (*vr0max) == INTEGER_CST)
8472 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8473 build_int_cst (TREE_TYPE (*vr0max), 1));
8474 else
8475 *vr0min = *vr0max;
8476 *vr0max = vr1max;
8478 else
8479 gcc_unreachable ();
8481 else if ((operand_less_p (*vr0min, vr1max) == 1
8482 || operand_equal_p (*vr0min, vr1max, 0))
8483 && operand_less_p (vr1min, *vr0min) == 1)
8485 /* ( [ ) ] or ( )[ ] */
8486 if (*vr0type == VR_ANTI_RANGE
8487 && vr1type == VR_ANTI_RANGE)
8488 *vr0min = vr1min;
8489 else if (*vr0type == VR_RANGE
8490 && vr1type == VR_RANGE)
8491 *vr0max = vr1max;
8492 else if (*vr0type == VR_RANGE
8493 && vr1type == VR_ANTI_RANGE)
8495 if (TREE_CODE (vr1max) == INTEGER_CST)
8496 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8497 build_int_cst (TREE_TYPE (vr1max), 1));
8498 else
8499 *vr0min = vr1max;
8501 else if (*vr0type == VR_ANTI_RANGE
8502 && vr1type == VR_RANGE)
8504 *vr0type = VR_RANGE;
8505 if (TREE_CODE (*vr0min) == INTEGER_CST)
8506 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8507 build_int_cst (TREE_TYPE (*vr0min), 1));
8508 else
8509 *vr0max = *vr0min;
8510 *vr0min = vr1min;
8512 else
8513 gcc_unreachable ();
8516 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8517 result for the intersection. That's always a conservative
8518 correct estimate. */
8520 return;
8524 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8525 in *VR0. This may not be the smallest possible such range. */
8527 static void
8528 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8530 value_range saved;
8532 /* If either range is VR_VARYING the other one wins. */
8533 if (vr1->type == VR_VARYING)
8534 return;
8535 if (vr0->type == VR_VARYING)
8537 copy_value_range (vr0, vr1);
8538 return;
8541 /* When either range is VR_UNDEFINED the resulting range is
8542 VR_UNDEFINED, too. */
8543 if (vr0->type == VR_UNDEFINED)
8544 return;
8545 if (vr1->type == VR_UNDEFINED)
8547 set_value_range_to_undefined (vr0);
8548 return;
8551 /* Save the original vr0 so we can return it as conservative intersection
8552 result when our worker turns things to varying. */
8553 saved = *vr0;
8554 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8555 vr1->type, vr1->min, vr1->max);
8556 /* Make sure to canonicalize the result though as the inversion of a
8557 VR_RANGE can still be a VR_RANGE. */
8558 set_and_canonicalize_value_range (vr0, vr0->type,
8559 vr0->min, vr0->max, vr0->equiv);
8560 /* If that failed, use the saved original VR0. */
8561 if (vr0->type == VR_VARYING)
8563 *vr0 = saved;
8564 return;
8566 /* If the result is VR_UNDEFINED there is no need to mess with
8567 the equivalencies. */
8568 if (vr0->type == VR_UNDEFINED)
8569 return;
8571 /* The resulting set of equivalences for range intersection is the union of
8572 the two sets. */
8573 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8574 bitmap_ior_into (vr0->equiv, vr1->equiv);
8575 else if (vr1->equiv && !vr0->equiv)
8576 bitmap_copy (vr0->equiv, vr1->equiv);
8579 void
8580 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8582 if (dump_file && (dump_flags & TDF_DETAILS))
8584 fprintf (dump_file, "Intersecting\n ");
8585 dump_value_range (dump_file, vr0);
8586 fprintf (dump_file, "\nand\n ");
8587 dump_value_range (dump_file, vr1);
8588 fprintf (dump_file, "\n");
8590 vrp_intersect_ranges_1 (vr0, vr1);
8591 if (dump_file && (dump_flags & TDF_DETAILS))
8593 fprintf (dump_file, "to\n ");
8594 dump_value_range (dump_file, vr0);
8595 fprintf (dump_file, "\n");
8599 /* Meet operation for value ranges. Given two value ranges VR0 and
8600 VR1, store in VR0 a range that contains both VR0 and VR1. This
8601 may not be the smallest possible such range. */
8603 static void
8604 vrp_meet_1 (value_range *vr0, const value_range *vr1)
8606 value_range saved;
8608 if (vr0->type == VR_UNDEFINED)
8610 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8611 return;
8614 if (vr1->type == VR_UNDEFINED)
8616 /* VR0 already has the resulting range. */
8617 return;
8620 if (vr0->type == VR_VARYING)
8622 /* Nothing to do. VR0 already has the resulting range. */
8623 return;
8626 if (vr1->type == VR_VARYING)
8628 set_value_range_to_varying (vr0);
8629 return;
8632 saved = *vr0;
8633 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8634 vr1->type, vr1->min, vr1->max);
8635 if (vr0->type == VR_VARYING)
8637 /* Failed to find an efficient meet. Before giving up and setting
8638 the result to VARYING, see if we can at least derive a useful
8639 anti-range. FIXME, all this nonsense about distinguishing
8640 anti-ranges from ranges is necessary because of the odd
8641 semantics of range_includes_zero_p and friends. */
8642 if (((saved.type == VR_RANGE
8643 && range_includes_zero_p (saved.min, saved.max) == 0)
8644 || (saved.type == VR_ANTI_RANGE
8645 && range_includes_zero_p (saved.min, saved.max) == 1))
8646 && ((vr1->type == VR_RANGE
8647 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8648 || (vr1->type == VR_ANTI_RANGE
8649 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8651 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8653 /* Since this meet operation did not result from the meeting of
8654 two equivalent names, VR0 cannot have any equivalences. */
8655 if (vr0->equiv)
8656 bitmap_clear (vr0->equiv);
8657 return;
8660 set_value_range_to_varying (vr0);
8661 return;
8663 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8664 vr0->equiv);
8665 if (vr0->type == VR_VARYING)
8666 return;
8668 /* The resulting set of equivalences is always the intersection of
8669 the two sets. */
8670 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8671 bitmap_and_into (vr0->equiv, vr1->equiv);
8672 else if (vr0->equiv && !vr1->equiv)
8673 bitmap_clear (vr0->equiv);
8676 void
8677 vrp_meet (value_range *vr0, const value_range *vr1)
8679 if (dump_file && (dump_flags & TDF_DETAILS))
8681 fprintf (dump_file, "Meeting\n ");
8682 dump_value_range (dump_file, vr0);
8683 fprintf (dump_file, "\nand\n ");
8684 dump_value_range (dump_file, vr1);
8685 fprintf (dump_file, "\n");
8687 vrp_meet_1 (vr0, vr1);
8688 if (dump_file && (dump_flags & TDF_DETAILS))
8690 fprintf (dump_file, "to\n ");
8691 dump_value_range (dump_file, vr0);
8692 fprintf (dump_file, "\n");
8697 /* Visit all arguments for PHI node PHI that flow through executable
8698 edges. If a valid value range can be derived from all the incoming
8699 value ranges, set a new range in VR_RESULT. */
8701 static void
8702 extract_range_from_phi_node (gphi *phi, value_range *vr_result)
8704 size_t i;
8705 tree lhs = PHI_RESULT (phi);
8706 value_range *lhs_vr = get_value_range (lhs);
8707 bool first = true;
8708 int edges, old_edges;
8709 struct loop *l;
8711 if (dump_file && (dump_flags & TDF_DETAILS))
8713 fprintf (dump_file, "\nVisiting PHI node: ");
8714 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8717 bool may_simulate_backedge_again = false;
8718 edges = 0;
8719 for (i = 0; i < gimple_phi_num_args (phi); i++)
8721 edge e = gimple_phi_arg_edge (phi, i);
8723 if (dump_file && (dump_flags & TDF_DETAILS))
8725 fprintf (dump_file,
8726 " Argument #%d (%d -> %d %sexecutable)\n",
8727 (int) i, e->src->index, e->dest->index,
8728 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8731 if (e->flags & EDGE_EXECUTABLE)
8733 tree arg = PHI_ARG_DEF (phi, i);
8734 value_range vr_arg;
8736 ++edges;
8738 if (TREE_CODE (arg) == SSA_NAME)
8740 /* See if we are eventually going to change one of the args. */
8741 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
8742 if (! gimple_nop_p (def_stmt)
8743 && prop_simulate_again_p (def_stmt)
8744 && e->flags & EDGE_DFS_BACK)
8745 may_simulate_backedge_again = true;
8747 vr_arg = *(get_value_range (arg));
8748 /* Do not allow equivalences or symbolic ranges to leak in from
8749 backedges. That creates invalid equivalencies.
8750 See PR53465 and PR54767. */
8751 if (e->flags & EDGE_DFS_BACK)
8753 if (vr_arg.type == VR_RANGE
8754 || vr_arg.type == VR_ANTI_RANGE)
8756 vr_arg.equiv = NULL;
8757 if (symbolic_range_p (&vr_arg))
8759 vr_arg.type = VR_VARYING;
8760 vr_arg.min = NULL_TREE;
8761 vr_arg.max = NULL_TREE;
8765 else
8767 /* If the non-backedge arguments range is VR_VARYING then
8768 we can still try recording a simple equivalence. */
8769 if (vr_arg.type == VR_VARYING)
8771 vr_arg.type = VR_RANGE;
8772 vr_arg.min = arg;
8773 vr_arg.max = arg;
8774 vr_arg.equiv = NULL;
8778 else
8780 if (TREE_OVERFLOW_P (arg))
8781 arg = drop_tree_overflow (arg);
8783 vr_arg.type = VR_RANGE;
8784 vr_arg.min = arg;
8785 vr_arg.max = arg;
8786 vr_arg.equiv = NULL;
8789 if (dump_file && (dump_flags & TDF_DETAILS))
8791 fprintf (dump_file, "\t");
8792 print_generic_expr (dump_file, arg, dump_flags);
8793 fprintf (dump_file, ": ");
8794 dump_value_range (dump_file, &vr_arg);
8795 fprintf (dump_file, "\n");
8798 if (first)
8799 copy_value_range (vr_result, &vr_arg);
8800 else
8801 vrp_meet (vr_result, &vr_arg);
8802 first = false;
8804 if (vr_result->type == VR_VARYING)
8805 break;
8809 if (vr_result->type == VR_VARYING)
8810 goto varying;
8811 else if (vr_result->type == VR_UNDEFINED)
8812 goto update_range;
8814 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8815 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8817 /* To prevent infinite iterations in the algorithm, derive ranges
8818 when the new value is slightly bigger or smaller than the
8819 previous one. We don't do this if we have seen a new executable
8820 edge; this helps us avoid an overflow infinity for conditionals
8821 which are not in a loop. If the old value-range was VR_UNDEFINED
8822 use the updated range and iterate one more time. If we will not
8823 simulate this PHI again via the backedge allow us to iterate. */
8824 if (edges > 0
8825 && gimple_phi_num_args (phi) > 1
8826 && edges == old_edges
8827 && lhs_vr->type != VR_UNDEFINED
8828 && may_simulate_backedge_again)
8830 /* Compare old and new ranges, fall back to varying if the
8831 values are not comparable. */
8832 int cmp_min = compare_values (lhs_vr->min, vr_result->min);
8833 if (cmp_min == -2)
8834 goto varying;
8835 int cmp_max = compare_values (lhs_vr->max, vr_result->max);
8836 if (cmp_max == -2)
8837 goto varying;
8839 /* For non VR_RANGE or for pointers fall back to varying if
8840 the range changed. */
8841 if ((lhs_vr->type != VR_RANGE || vr_result->type != VR_RANGE
8842 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8843 && (cmp_min != 0 || cmp_max != 0))
8844 goto varying;
8846 /* If the new minimum is larger than the previous one
8847 retain the old value. If the new minimum value is smaller
8848 than the previous one and not -INF go all the way to -INF + 1.
8849 In the first case, to avoid infinite bouncing between different
8850 minimums, and in the other case to avoid iterating millions of
8851 times to reach -INF. Going to -INF + 1 also lets the following
8852 iteration compute whether there will be any overflow, at the
8853 expense of one additional iteration. */
8854 if (cmp_min < 0)
8855 vr_result->min = lhs_vr->min;
8856 else if (cmp_min > 0
8857 && !vrp_val_is_min (vr_result->min))
8858 vr_result->min
8859 = int_const_binop (PLUS_EXPR,
8860 vrp_val_min (TREE_TYPE (vr_result->min)),
8861 build_int_cst (TREE_TYPE (vr_result->min), 1));
8863 /* Similarly for the maximum value. */
8864 if (cmp_max > 0)
8865 vr_result->max = lhs_vr->max;
8866 else if (cmp_max < 0
8867 && !vrp_val_is_max (vr_result->max))
8868 vr_result->max
8869 = int_const_binop (MINUS_EXPR,
8870 vrp_val_max (TREE_TYPE (vr_result->min)),
8871 build_int_cst (TREE_TYPE (vr_result->min), 1));
8873 /* If we dropped either bound to +-INF then if this is a loop
8874 PHI node SCEV may known more about its value-range. */
8875 if (cmp_min > 0 || cmp_min < 0
8876 || cmp_max < 0 || cmp_max > 0)
8877 goto scev_check;
8879 goto infinite_check;
8882 goto update_range;
8884 varying:
8885 set_value_range_to_varying (vr_result);
8887 scev_check:
8888 /* If this is a loop PHI node SCEV may known more about its value-range.
8889 scev_check can be reached from two paths, one is a fall through from above
8890 "varying" label, the other is direct goto from code block which tries to
8891 avoid infinite simulation. */
8892 if ((l = loop_containing_stmt (phi))
8893 && l->header == gimple_bb (phi))
8894 adjust_range_with_scev (vr_result, l, phi, lhs);
8896 infinite_check:
8897 /* If we will end up with a (-INF, +INF) range, set it to
8898 VARYING. Same if the previous max value was invalid for
8899 the type and we end up with vr_result.min > vr_result.max. */
8900 if ((vr_result->type == VR_RANGE || vr_result->type == VR_ANTI_RANGE)
8901 && !((vrp_val_is_max (vr_result->max) && vrp_val_is_min (vr_result->min))
8902 || compare_values (vr_result->min, vr_result->max) > 0))
8904 else
8905 set_value_range_to_varying (vr_result);
8907 /* If the new range is different than the previous value, keep
8908 iterating. */
8909 update_range:
8910 return;
8913 /* Visit all arguments for PHI node PHI that flow through executable
8914 edges. If a valid value range can be derived from all the incoming
8915 value ranges, set a new range for the LHS of PHI. */
8917 static enum ssa_prop_result
8918 vrp_visit_phi_node (gphi *phi)
8920 tree lhs = PHI_RESULT (phi);
8921 value_range vr_result = VR_INITIALIZER;
8922 extract_range_from_phi_node (phi, &vr_result);
8923 if (update_value_range (lhs, &vr_result))
8925 if (dump_file && (dump_flags & TDF_DETAILS))
8927 fprintf (dump_file, "Found new range for ");
8928 print_generic_expr (dump_file, lhs, 0);
8929 fprintf (dump_file, ": ");
8930 dump_value_range (dump_file, &vr_result);
8931 fprintf (dump_file, "\n");
8934 if (vr_result.type == VR_VARYING)
8935 return SSA_PROP_VARYING;
8937 return SSA_PROP_INTERESTING;
8940 /* Nothing changed, don't add outgoing edges. */
8941 return SSA_PROP_NOT_INTERESTING;
8944 /* Simplify boolean operations if the source is known
8945 to be already a boolean. */
8946 static bool
8947 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8949 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8950 tree lhs, op0, op1;
8951 bool need_conversion;
8953 /* We handle only !=/== case here. */
8954 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8956 op0 = gimple_assign_rhs1 (stmt);
8957 if (!op_with_boolean_value_range_p (op0))
8958 return false;
8960 op1 = gimple_assign_rhs2 (stmt);
8961 if (!op_with_boolean_value_range_p (op1))
8962 return false;
8964 /* Reduce number of cases to handle to NE_EXPR. As there is no
8965 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8966 if (rhs_code == EQ_EXPR)
8968 if (TREE_CODE (op1) == INTEGER_CST)
8969 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8970 build_int_cst (TREE_TYPE (op1), 1));
8971 else
8972 return false;
8975 lhs = gimple_assign_lhs (stmt);
8976 need_conversion
8977 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8979 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8980 if (need_conversion
8981 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8982 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8983 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8984 return false;
8986 /* For A != 0 we can substitute A itself. */
8987 if (integer_zerop (op1))
8988 gimple_assign_set_rhs_with_ops (gsi,
8989 need_conversion
8990 ? NOP_EXPR : TREE_CODE (op0), op0);
8991 /* For A != B we substitute A ^ B. Either with conversion. */
8992 else if (need_conversion)
8994 tree tem = make_ssa_name (TREE_TYPE (op0));
8995 gassign *newop
8996 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8997 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8998 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8999 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
9000 set_range_info (tem, VR_RANGE,
9001 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
9002 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
9003 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9005 /* Or without. */
9006 else
9007 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9008 update_stmt (gsi_stmt (*gsi));
9010 return true;
9013 /* Simplify a division or modulo operator to a right shift or
9014 bitwise and if the first operand is unsigned or is greater
9015 than zero and the second operand is an exact power of two.
9016 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9017 into just op0 if op0's range is known to be a subset of
9018 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9019 modulo. */
9021 static bool
9022 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9024 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9025 tree val = NULL;
9026 tree op0 = gimple_assign_rhs1 (stmt);
9027 tree op1 = gimple_assign_rhs2 (stmt);
9028 value_range *vr = get_value_range (op0);
9030 if (rhs_code == TRUNC_MOD_EXPR
9031 && TREE_CODE (op1) == INTEGER_CST
9032 && tree_int_cst_sgn (op1) == 1
9033 && range_int_cst_p (vr)
9034 && tree_int_cst_lt (vr->max, op1))
9036 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9037 || tree_int_cst_sgn (vr->min) >= 0
9038 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9039 vr->min))
9041 /* If op0 already has the range op0 % op1 has,
9042 then TRUNC_MOD_EXPR won't change anything. */
9043 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9044 gimple_assign_set_rhs_from_tree (&gsi, op0);
9045 update_stmt (stmt);
9046 return true;
9050 if (!integer_pow2p (op1))
9052 /* X % -Y can be only optimized into X % Y either if
9053 X is not INT_MIN, or Y is not -1. Fold it now, as after
9054 remove_range_assertions the range info might be not available
9055 anymore. */
9056 if (rhs_code == TRUNC_MOD_EXPR
9057 && fold_stmt (gsi, follow_single_use_edges))
9058 return true;
9059 return false;
9062 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9063 val = integer_one_node;
9064 else
9066 bool sop = false;
9068 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9070 if (val
9071 && sop
9072 && integer_onep (val)
9073 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9075 location_t location;
9077 if (!gimple_has_location (stmt))
9078 location = input_location;
9079 else
9080 location = gimple_location (stmt);
9081 warning_at (location, OPT_Wstrict_overflow,
9082 "assuming signed overflow does not occur when "
9083 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9087 if (val && integer_onep (val))
9089 tree t;
9091 if (rhs_code == TRUNC_DIV_EXPR)
9093 t = build_int_cst (integer_type_node, tree_log2 (op1));
9094 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9095 gimple_assign_set_rhs1 (stmt, op0);
9096 gimple_assign_set_rhs2 (stmt, t);
9098 else
9100 t = build_int_cst (TREE_TYPE (op1), 1);
9101 t = int_const_binop (MINUS_EXPR, op1, t);
9102 t = fold_convert (TREE_TYPE (op0), t);
9104 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9105 gimple_assign_set_rhs1 (stmt, op0);
9106 gimple_assign_set_rhs2 (stmt, t);
9109 update_stmt (stmt);
9110 return true;
9113 return false;
9116 /* Simplify a min or max if the ranges of the two operands are
9117 disjoint. Return true if we do simplify. */
9119 static bool
9120 simplify_min_or_max_using_ranges (gimple *stmt)
9122 tree op0 = gimple_assign_rhs1 (stmt);
9123 tree op1 = gimple_assign_rhs2 (stmt);
9124 bool sop = false;
9125 tree val;
9127 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9128 (LE_EXPR, op0, op1, &sop));
9129 if (!val)
9131 sop = false;
9132 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9133 (LT_EXPR, op0, op1, &sop));
9136 if (val)
9138 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9140 location_t location;
9142 if (!gimple_has_location (stmt))
9143 location = input_location;
9144 else
9145 location = gimple_location (stmt);
9146 warning_at (location, OPT_Wstrict_overflow,
9147 "assuming signed overflow does not occur when "
9148 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9151 /* VAL == TRUE -> OP0 < or <= op1
9152 VAL == FALSE -> OP0 > or >= op1. */
9153 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9154 == integer_zerop (val)) ? op0 : op1;
9155 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9156 gimple_assign_set_rhs_from_tree (&gsi, res);
9157 update_stmt (stmt);
9158 return true;
9161 return false;
9164 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9165 ABS_EXPR. If the operand is <= 0, then simplify the
9166 ABS_EXPR into a NEGATE_EXPR. */
9168 static bool
9169 simplify_abs_using_ranges (gimple *stmt)
9171 tree op = gimple_assign_rhs1 (stmt);
9172 value_range *vr = get_value_range (op);
9174 if (vr)
9176 tree val = NULL;
9177 bool sop = false;
9179 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9180 if (!val)
9182 /* The range is neither <= 0 nor > 0. Now see if it is
9183 either < 0 or >= 0. */
9184 sop = false;
9185 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9186 &sop);
9189 if (val)
9191 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9193 location_t location;
9195 if (!gimple_has_location (stmt))
9196 location = input_location;
9197 else
9198 location = gimple_location (stmt);
9199 warning_at (location, OPT_Wstrict_overflow,
9200 "assuming signed overflow does not occur when "
9201 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9204 gimple_assign_set_rhs1 (stmt, op);
9205 if (integer_zerop (val))
9206 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9207 else
9208 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9209 update_stmt (stmt);
9210 return true;
9214 return false;
9217 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9218 If all the bits that are being cleared by & are already
9219 known to be zero from VR, or all the bits that are being
9220 set by | are already known to be one from VR, the bit
9221 operation is redundant. */
9223 static bool
9224 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9226 tree op0 = gimple_assign_rhs1 (stmt);
9227 tree op1 = gimple_assign_rhs2 (stmt);
9228 tree op = NULL_TREE;
9229 value_range vr0 = VR_INITIALIZER;
9230 value_range vr1 = VR_INITIALIZER;
9231 wide_int may_be_nonzero0, may_be_nonzero1;
9232 wide_int must_be_nonzero0, must_be_nonzero1;
9233 wide_int mask;
9235 if (TREE_CODE (op0) == SSA_NAME)
9236 vr0 = *(get_value_range (op0));
9237 else if (is_gimple_min_invariant (op0))
9238 set_value_range_to_value (&vr0, op0, NULL);
9239 else
9240 return false;
9242 if (TREE_CODE (op1) == SSA_NAME)
9243 vr1 = *(get_value_range (op1));
9244 else if (is_gimple_min_invariant (op1))
9245 set_value_range_to_value (&vr1, op1, NULL);
9246 else
9247 return false;
9249 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9250 &must_be_nonzero0))
9251 return false;
9252 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9253 &must_be_nonzero1))
9254 return false;
9256 switch (gimple_assign_rhs_code (stmt))
9258 case BIT_AND_EXPR:
9259 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9260 if (mask == 0)
9262 op = op0;
9263 break;
9265 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9266 if (mask == 0)
9268 op = op1;
9269 break;
9271 break;
9272 case BIT_IOR_EXPR:
9273 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9274 if (mask == 0)
9276 op = op1;
9277 break;
9279 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9280 if (mask == 0)
9282 op = op0;
9283 break;
9285 break;
9286 default:
9287 gcc_unreachable ();
9290 if (op == NULL_TREE)
9291 return false;
9293 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9294 update_stmt (gsi_stmt (*gsi));
9295 return true;
9298 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9299 a known value range VR.
9301 If there is one and only one value which will satisfy the
9302 conditional, then return that value. Else return NULL.
9304 If signed overflow must be undefined for the value to satisfy
9305 the conditional, then set *STRICT_OVERFLOW_P to true. */
9307 static tree
9308 test_for_singularity (enum tree_code cond_code, tree op0,
9309 tree op1, value_range *vr,
9310 bool *strict_overflow_p)
9312 tree min = NULL;
9313 tree max = NULL;
9315 /* Extract minimum/maximum values which satisfy the conditional as it was
9316 written. */
9317 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9319 /* This should not be negative infinity; there is no overflow
9320 here. */
9321 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9323 max = op1;
9324 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9326 tree one = build_int_cst (TREE_TYPE (op0), 1);
9327 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9328 if (EXPR_P (max))
9329 TREE_NO_WARNING (max) = 1;
9332 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9334 /* This should not be positive infinity; there is no overflow
9335 here. */
9336 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9338 min = op1;
9339 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9341 tree one = build_int_cst (TREE_TYPE (op0), 1);
9342 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9343 if (EXPR_P (min))
9344 TREE_NO_WARNING (min) = 1;
9348 /* Now refine the minimum and maximum values using any
9349 value range information we have for op0. */
9350 if (min && max)
9352 if (compare_values (vr->min, min) == 1)
9353 min = vr->min;
9354 if (compare_values (vr->max, max) == -1)
9355 max = vr->max;
9357 /* If the new min/max values have converged to a single value,
9358 then there is only one value which can satisfy the condition,
9359 return that value. */
9360 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9362 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9363 && is_overflow_infinity (vr->max))
9364 *strict_overflow_p = true;
9365 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9366 && is_overflow_infinity (vr->min))
9367 *strict_overflow_p = true;
9369 return min;
9372 return NULL;
9375 /* Return whether the value range *VR fits in an integer type specified
9376 by PRECISION and UNSIGNED_P. */
9378 static bool
9379 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9381 tree src_type;
9382 unsigned src_precision;
9383 widest_int tem;
9384 signop src_sgn;
9386 /* We can only handle integral and pointer types. */
9387 src_type = TREE_TYPE (vr->min);
9388 if (!INTEGRAL_TYPE_P (src_type)
9389 && !POINTER_TYPE_P (src_type))
9390 return false;
9392 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9393 and so is an identity transform. */
9394 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9395 src_sgn = TYPE_SIGN (src_type);
9396 if ((src_precision < dest_precision
9397 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9398 || (src_precision == dest_precision && src_sgn == dest_sgn))
9399 return true;
9401 /* Now we can only handle ranges with constant bounds. */
9402 if (vr->type != VR_RANGE
9403 || TREE_CODE (vr->min) != INTEGER_CST
9404 || TREE_CODE (vr->max) != INTEGER_CST)
9405 return false;
9407 /* For sign changes, the MSB of the wide_int has to be clear.
9408 An unsigned value with its MSB set cannot be represented by
9409 a signed wide_int, while a negative value cannot be represented
9410 by an unsigned wide_int. */
9411 if (src_sgn != dest_sgn
9412 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9413 return false;
9415 /* Then we can perform the conversion on both ends and compare
9416 the result for equality. */
9417 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9418 if (tem != wi::to_widest (vr->min))
9419 return false;
9420 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9421 if (tem != wi::to_widest (vr->max))
9422 return false;
9424 return true;
9427 /* Simplify a conditional using a relational operator to an equality
9428 test if the range information indicates only one value can satisfy
9429 the original conditional. */
9431 static bool
9432 simplify_cond_using_ranges (gcond *stmt)
9434 tree op0 = gimple_cond_lhs (stmt);
9435 tree op1 = gimple_cond_rhs (stmt);
9436 enum tree_code cond_code = gimple_cond_code (stmt);
9438 if (cond_code != NE_EXPR
9439 && cond_code != EQ_EXPR
9440 && TREE_CODE (op0) == SSA_NAME
9441 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9442 && is_gimple_min_invariant (op1))
9444 value_range *vr = get_value_range (op0);
9446 /* If we have range information for OP0, then we might be
9447 able to simplify this conditional. */
9448 if (vr->type == VR_RANGE)
9450 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9451 bool sop = false;
9452 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9454 if (new_tree
9455 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9457 if (dump_file)
9459 fprintf (dump_file, "Simplified relational ");
9460 print_gimple_stmt (dump_file, stmt, 0, 0);
9461 fprintf (dump_file, " into ");
9464 gimple_cond_set_code (stmt, EQ_EXPR);
9465 gimple_cond_set_lhs (stmt, op0);
9466 gimple_cond_set_rhs (stmt, new_tree);
9468 update_stmt (stmt);
9470 if (dump_file)
9472 print_gimple_stmt (dump_file, stmt, 0, 0);
9473 fprintf (dump_file, "\n");
9476 if (sop && issue_strict_overflow_warning (wc))
9478 location_t location = input_location;
9479 if (gimple_has_location (stmt))
9480 location = gimple_location (stmt);
9482 warning_at (location, OPT_Wstrict_overflow,
9483 "assuming signed overflow does not occur when "
9484 "simplifying conditional");
9487 return true;
9490 /* Try again after inverting the condition. We only deal
9491 with integral types here, so no need to worry about
9492 issues with inverting FP comparisons. */
9493 sop = false;
9494 new_tree = test_for_singularity
9495 (invert_tree_comparison (cond_code, false),
9496 op0, op1, vr, &sop);
9498 if (new_tree
9499 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9501 if (dump_file)
9503 fprintf (dump_file, "Simplified relational ");
9504 print_gimple_stmt (dump_file, stmt, 0, 0);
9505 fprintf (dump_file, " into ");
9508 gimple_cond_set_code (stmt, NE_EXPR);
9509 gimple_cond_set_lhs (stmt, op0);
9510 gimple_cond_set_rhs (stmt, new_tree);
9512 update_stmt (stmt);
9514 if (dump_file)
9516 print_gimple_stmt (dump_file, stmt, 0, 0);
9517 fprintf (dump_file, "\n");
9520 if (sop && issue_strict_overflow_warning (wc))
9522 location_t location = input_location;
9523 if (gimple_has_location (stmt))
9524 location = gimple_location (stmt);
9526 warning_at (location, OPT_Wstrict_overflow,
9527 "assuming signed overflow does not occur when "
9528 "simplifying conditional");
9531 return true;
9536 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9537 see if OP0 was set by a type conversion where the source of
9538 the conversion is another SSA_NAME with a range that fits
9539 into the range of OP0's type.
9541 If so, the conversion is redundant as the earlier SSA_NAME can be
9542 used for the comparison directly if we just massage the constant in the
9543 comparison. */
9544 if (TREE_CODE (op0) == SSA_NAME
9545 && TREE_CODE (op1) == INTEGER_CST)
9547 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9548 tree innerop;
9550 if (!is_gimple_assign (def_stmt)
9551 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9552 return false;
9554 innerop = gimple_assign_rhs1 (def_stmt);
9556 if (TREE_CODE (innerop) == SSA_NAME
9557 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9558 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9559 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9561 value_range *vr = get_value_range (innerop);
9563 if (range_int_cst_p (vr)
9564 && range_fits_type_p (vr,
9565 TYPE_PRECISION (TREE_TYPE (op0)),
9566 TYPE_SIGN (TREE_TYPE (op0)))
9567 && int_fits_type_p (op1, TREE_TYPE (innerop))
9568 /* The range must not have overflowed, or if it did overflow
9569 we must not be wrapping/trapping overflow and optimizing
9570 with strict overflow semantics. */
9571 && ((!is_negative_overflow_infinity (vr->min)
9572 && !is_positive_overflow_infinity (vr->max))
9573 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9575 /* If the range overflowed and the user has asked for warnings
9576 when strict overflow semantics were used to optimize code,
9577 issue an appropriate warning. */
9578 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9579 && (is_negative_overflow_infinity (vr->min)
9580 || is_positive_overflow_infinity (vr->max))
9581 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9583 location_t location;
9585 if (!gimple_has_location (stmt))
9586 location = input_location;
9587 else
9588 location = gimple_location (stmt);
9589 warning_at (location, OPT_Wstrict_overflow,
9590 "assuming signed overflow does not occur when "
9591 "simplifying conditional");
9594 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9595 gimple_cond_set_lhs (stmt, innerop);
9596 gimple_cond_set_rhs (stmt, newconst);
9597 return true;
9602 return false;
9605 /* Simplify a switch statement using the value range of the switch
9606 argument. */
9608 static bool
9609 simplify_switch_using_ranges (gswitch *stmt)
9611 tree op = gimple_switch_index (stmt);
9612 value_range *vr = NULL;
9613 bool take_default;
9614 edge e;
9615 edge_iterator ei;
9616 size_t i = 0, j = 0, n, n2;
9617 tree vec2;
9618 switch_update su;
9619 size_t k = 1, l = 0;
9621 if (TREE_CODE (op) == SSA_NAME)
9623 vr = get_value_range (op);
9625 /* We can only handle integer ranges. */
9626 if ((vr->type != VR_RANGE
9627 && vr->type != VR_ANTI_RANGE)
9628 || symbolic_range_p (vr))
9629 return false;
9631 /* Find case label for min/max of the value range. */
9632 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9634 else if (TREE_CODE (op) == INTEGER_CST)
9636 take_default = !find_case_label_index (stmt, 1, op, &i);
9637 if (take_default)
9639 i = 1;
9640 j = 0;
9642 else
9644 j = i;
9647 else
9648 return false;
9650 n = gimple_switch_num_labels (stmt);
9652 /* We can truncate the case label ranges that partially overlap with OP's
9653 value range. */
9654 size_t min_idx = 1, max_idx = 0;
9655 if (vr != NULL)
9656 find_case_label_range (stmt, vr->min, vr->max, &min_idx, &max_idx);
9657 if (min_idx <= max_idx)
9659 tree min_label = gimple_switch_label (stmt, min_idx);
9660 tree max_label = gimple_switch_label (stmt, max_idx);
9662 /* Avoid changing the type of the case labels when truncating. */
9663 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
9664 tree vr_min = fold_convert (case_label_type, vr->min);
9665 tree vr_max = fold_convert (case_label_type, vr->max);
9667 if (vr->type == VR_RANGE)
9669 /* If OP's value range is [2,8] and the low label range is
9670 0 ... 3, truncate the label's range to 2 .. 3. */
9671 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9672 && CASE_HIGH (min_label) != NULL_TREE
9673 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9674 CASE_LOW (min_label) = vr_min;
9676 /* If OP's value range is [2,8] and the high label range is
9677 7 ... 10, truncate the label's range to 7 .. 8. */
9678 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9679 && CASE_HIGH (max_label) != NULL_TREE
9680 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9681 CASE_HIGH (max_label) = vr_max;
9683 else if (vr->type == VR_ANTI_RANGE)
9685 tree one_cst = build_one_cst (case_label_type);
9687 if (min_label == max_label)
9689 /* If OP's value range is ~[7,8] and the label's range is
9690 7 ... 10, truncate the label's range to 9 ... 10. */
9691 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
9692 && CASE_HIGH (min_label) != NULL_TREE
9693 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
9694 CASE_LOW (min_label)
9695 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9697 /* If OP's value range is ~[7,8] and the label's range is
9698 5 ... 8, truncate the label's range to 5 ... 6. */
9699 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9700 && CASE_HIGH (min_label) != NULL_TREE
9701 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
9702 CASE_HIGH (min_label)
9703 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9705 else
9707 /* If OP's value range is ~[2,8] and the low label range is
9708 0 ... 3, truncate the label's range to 0 ... 1. */
9709 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9710 && CASE_HIGH (min_label) != NULL_TREE
9711 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9712 CASE_HIGH (min_label)
9713 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9715 /* If OP's value range is ~[2,8] and the high label range is
9716 7 ... 10, truncate the label's range to 9 ... 10. */
9717 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9718 && CASE_HIGH (max_label) != NULL_TREE
9719 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9720 CASE_LOW (max_label)
9721 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9725 /* Canonicalize singleton case ranges. */
9726 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
9727 CASE_HIGH (min_label) = NULL_TREE;
9728 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
9729 CASE_HIGH (max_label) = NULL_TREE;
9732 /* We can also eliminate case labels that lie completely outside OP's value
9733 range. */
9735 /* Bail out if this is just all edges taken. */
9736 if (i == 1
9737 && j == n - 1
9738 && take_default)
9739 return false;
9741 /* Build a new vector of taken case labels. */
9742 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9743 n2 = 0;
9745 /* Add the default edge, if necessary. */
9746 if (take_default)
9747 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9749 for (; i <= j; ++i, ++n2)
9750 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9752 for (; k <= l; ++k, ++n2)
9753 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9755 /* Mark needed edges. */
9756 for (i = 0; i < n2; ++i)
9758 e = find_edge (gimple_bb (stmt),
9759 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9760 e->aux = (void *)-1;
9763 /* Queue not needed edges for later removal. */
9764 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9766 if (e->aux == (void *)-1)
9768 e->aux = NULL;
9769 continue;
9772 if (dump_file && (dump_flags & TDF_DETAILS))
9774 fprintf (dump_file, "removing unreachable case label\n");
9776 to_remove_edges.safe_push (e);
9777 e->flags &= ~EDGE_EXECUTABLE;
9780 /* And queue an update for the stmt. */
9781 su.stmt = stmt;
9782 su.vec = vec2;
9783 to_update_switch_stmts.safe_push (su);
9784 return false;
9787 /* Simplify an integral conversion from an SSA name in STMT. */
9789 static bool
9790 simplify_conversion_using_ranges (gimple *stmt)
9792 tree innerop, middleop, finaltype;
9793 gimple *def_stmt;
9794 signop inner_sgn, middle_sgn, final_sgn;
9795 unsigned inner_prec, middle_prec, final_prec;
9796 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9798 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9799 if (!INTEGRAL_TYPE_P (finaltype))
9800 return false;
9801 middleop = gimple_assign_rhs1 (stmt);
9802 def_stmt = SSA_NAME_DEF_STMT (middleop);
9803 if (!is_gimple_assign (def_stmt)
9804 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9805 return false;
9806 innerop = gimple_assign_rhs1 (def_stmt);
9807 if (TREE_CODE (innerop) != SSA_NAME
9808 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9809 return false;
9811 /* Get the value-range of the inner operand. Use get_range_info in
9812 case innerop was created during substitute-and-fold. */
9813 wide_int imin, imax;
9814 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
9815 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
9816 return false;
9817 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
9818 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
9820 /* Simulate the conversion chain to check if the result is equal if
9821 the middle conversion is removed. */
9822 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9823 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9824 final_prec = TYPE_PRECISION (finaltype);
9826 /* If the first conversion is not injective, the second must not
9827 be widening. */
9828 if (wi::gtu_p (innermax - innermin,
9829 wi::mask <widest_int> (middle_prec, false))
9830 && middle_prec < final_prec)
9831 return false;
9832 /* We also want a medium value so that we can track the effect that
9833 narrowing conversions with sign change have. */
9834 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9835 if (inner_sgn == UNSIGNED)
9836 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9837 else
9838 innermed = 0;
9839 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9840 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9841 innermed = innermin;
9843 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9844 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9845 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9846 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9848 /* Require that the final conversion applied to both the original
9849 and the intermediate range produces the same result. */
9850 final_sgn = TYPE_SIGN (finaltype);
9851 if (wi::ext (middlemin, final_prec, final_sgn)
9852 != wi::ext (innermin, final_prec, final_sgn)
9853 || wi::ext (middlemed, final_prec, final_sgn)
9854 != wi::ext (innermed, final_prec, final_sgn)
9855 || wi::ext (middlemax, final_prec, final_sgn)
9856 != wi::ext (innermax, final_prec, final_sgn))
9857 return false;
9859 gimple_assign_set_rhs1 (stmt, innerop);
9860 update_stmt (stmt);
9861 return true;
9864 /* Simplify a conversion from integral SSA name to float in STMT. */
9866 static bool
9867 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9868 gimple *stmt)
9870 tree rhs1 = gimple_assign_rhs1 (stmt);
9871 value_range *vr = get_value_range (rhs1);
9872 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9873 machine_mode mode;
9874 tree tem;
9875 gassign *conv;
9877 /* We can only handle constant ranges. */
9878 if (vr->type != VR_RANGE
9879 || TREE_CODE (vr->min) != INTEGER_CST
9880 || TREE_CODE (vr->max) != INTEGER_CST)
9881 return false;
9883 /* First check if we can use a signed type in place of an unsigned. */
9884 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9885 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9886 != CODE_FOR_nothing)
9887 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9888 mode = TYPE_MODE (TREE_TYPE (rhs1));
9889 /* If we can do the conversion in the current input mode do nothing. */
9890 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9891 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9892 return false;
9893 /* Otherwise search for a mode we can use, starting from the narrowest
9894 integer mode available. */
9895 else
9897 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9900 /* If we cannot do a signed conversion to float from mode
9901 or if the value-range does not fit in the signed type
9902 try with a wider mode. */
9903 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9904 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9905 break;
9907 mode = GET_MODE_WIDER_MODE (mode);
9908 /* But do not widen the input. Instead leave that to the
9909 optabs expansion code. */
9910 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9911 return false;
9913 while (mode != VOIDmode);
9914 if (mode == VOIDmode)
9915 return false;
9918 /* It works, insert a truncation or sign-change before the
9919 float conversion. */
9920 tem = make_ssa_name (build_nonstandard_integer_type
9921 (GET_MODE_PRECISION (mode), 0));
9922 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9923 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9924 gimple_assign_set_rhs1 (stmt, tem);
9925 update_stmt (stmt);
9927 return true;
9930 /* Simplify an internal fn call using ranges if possible. */
9932 static bool
9933 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9935 enum tree_code subcode;
9936 bool is_ubsan = false;
9937 bool ovf = false;
9938 switch (gimple_call_internal_fn (stmt))
9940 case IFN_UBSAN_CHECK_ADD:
9941 subcode = PLUS_EXPR;
9942 is_ubsan = true;
9943 break;
9944 case IFN_UBSAN_CHECK_SUB:
9945 subcode = MINUS_EXPR;
9946 is_ubsan = true;
9947 break;
9948 case IFN_UBSAN_CHECK_MUL:
9949 subcode = MULT_EXPR;
9950 is_ubsan = true;
9951 break;
9952 case IFN_ADD_OVERFLOW:
9953 subcode = PLUS_EXPR;
9954 break;
9955 case IFN_SUB_OVERFLOW:
9956 subcode = MINUS_EXPR;
9957 break;
9958 case IFN_MUL_OVERFLOW:
9959 subcode = MULT_EXPR;
9960 break;
9961 default:
9962 return false;
9965 tree op0 = gimple_call_arg (stmt, 0);
9966 tree op1 = gimple_call_arg (stmt, 1);
9967 tree type;
9968 if (is_ubsan)
9969 type = TREE_TYPE (op0);
9970 else if (gimple_call_lhs (stmt) == NULL_TREE)
9971 return false;
9972 else
9973 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9974 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9975 || (is_ubsan && ovf))
9976 return false;
9978 gimple *g;
9979 location_t loc = gimple_location (stmt);
9980 if (is_ubsan)
9981 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9982 else
9984 int prec = TYPE_PRECISION (type);
9985 tree utype = type;
9986 if (ovf
9987 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9988 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9989 utype = build_nonstandard_integer_type (prec, 1);
9990 if (TREE_CODE (op0) == INTEGER_CST)
9991 op0 = fold_convert (utype, op0);
9992 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9994 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9995 gimple_set_location (g, loc);
9996 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9997 op0 = gimple_assign_lhs (g);
9999 if (TREE_CODE (op1) == INTEGER_CST)
10000 op1 = fold_convert (utype, op1);
10001 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
10003 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
10004 gimple_set_location (g, loc);
10005 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10006 op1 = gimple_assign_lhs (g);
10008 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
10009 gimple_set_location (g, loc);
10010 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10011 if (utype != type)
10013 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
10014 gimple_assign_lhs (g));
10015 gimple_set_location (g, loc);
10016 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10018 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
10019 gimple_assign_lhs (g),
10020 build_int_cst (type, ovf));
10022 gimple_set_location (g, loc);
10023 gsi_replace (gsi, g, false);
10024 return true;
10027 /* Return true if VAR is a two-valued variable. Set a and b with the
10028 two-values when it is true. Return false otherwise. */
10030 static bool
10031 two_valued_val_range_p (tree var, tree *a, tree *b)
10033 value_range *vr = get_value_range (var);
10034 if ((vr->type != VR_RANGE
10035 && vr->type != VR_ANTI_RANGE)
10036 || TREE_CODE (vr->min) != INTEGER_CST
10037 || TREE_CODE (vr->max) != INTEGER_CST)
10038 return false;
10040 if (vr->type == VR_RANGE
10041 && wi::sub (vr->max, vr->min) == 1)
10043 *a = vr->min;
10044 *b = vr->max;
10045 return true;
10048 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10049 if (vr->type == VR_ANTI_RANGE
10050 && wi::sub (vr->min, vrp_val_min (TREE_TYPE (var))) == 1
10051 && wi::sub (vrp_val_max (TREE_TYPE (var)), vr->max) == 1)
10053 *a = vrp_val_min (TREE_TYPE (var));
10054 *b = vrp_val_max (TREE_TYPE (var));
10055 return true;
10058 return false;
10061 /* Simplify STMT using ranges if possible. */
10063 static bool
10064 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
10066 gimple *stmt = gsi_stmt (*gsi);
10067 if (is_gimple_assign (stmt))
10069 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
10070 tree rhs1 = gimple_assign_rhs1 (stmt);
10071 tree rhs2 = gimple_assign_rhs2 (stmt);
10072 tree lhs = gimple_assign_lhs (stmt);
10073 tree val1 = NULL_TREE, val2 = NULL_TREE;
10074 use_operand_p use_p;
10075 gimple *use_stmt;
10077 /* Convert:
10078 LHS = CST BINOP VAR
10079 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10081 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10083 Also handles:
10084 LHS = VAR BINOP CST
10085 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10087 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10089 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
10090 && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10091 && ((TREE_CODE (rhs1) == INTEGER_CST
10092 && TREE_CODE (rhs2) == SSA_NAME)
10093 || (TREE_CODE (rhs2) == INTEGER_CST
10094 && TREE_CODE (rhs1) == SSA_NAME))
10095 && single_imm_use (lhs, &use_p, &use_stmt)
10096 && gimple_code (use_stmt) == GIMPLE_COND)
10099 tree new_rhs1 = NULL_TREE;
10100 tree new_rhs2 = NULL_TREE;
10101 tree cmp_var = NULL_TREE;
10103 if (TREE_CODE (rhs2) == SSA_NAME
10104 && two_valued_val_range_p (rhs2, &val1, &val2))
10106 /* Optimize RHS1 OP [VAL1, VAL2]. */
10107 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
10108 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
10109 cmp_var = rhs2;
10111 else if (TREE_CODE (rhs1) == SSA_NAME
10112 && two_valued_val_range_p (rhs1, &val1, &val2))
10114 /* Optimize [VAL1, VAL2] OP RHS2. */
10115 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
10116 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
10117 cmp_var = rhs1;
10120 /* If we could not find two-vals or the optimzation is invalid as
10121 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10122 if (new_rhs1 && new_rhs2)
10124 tree cond = build2 (EQ_EXPR, TREE_TYPE (cmp_var), cmp_var, val1);
10125 gimple_assign_set_rhs_with_ops (gsi,
10126 COND_EXPR, cond,
10127 new_rhs1,
10128 new_rhs2);
10129 update_stmt (gsi_stmt (*gsi));
10130 return true;
10134 switch (rhs_code)
10136 case EQ_EXPR:
10137 case NE_EXPR:
10138 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10139 if the RHS is zero or one, and the LHS are known to be boolean
10140 values. */
10141 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10142 return simplify_truth_ops_using_ranges (gsi, stmt);
10143 break;
10145 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10146 and BIT_AND_EXPR respectively if the first operand is greater
10147 than zero and the second operand is an exact power of two.
10148 Also optimize TRUNC_MOD_EXPR away if the second operand is
10149 constant and the first operand already has the right value
10150 range. */
10151 case TRUNC_DIV_EXPR:
10152 case TRUNC_MOD_EXPR:
10153 if (TREE_CODE (rhs1) == SSA_NAME
10154 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10155 return simplify_div_or_mod_using_ranges (gsi, stmt);
10156 break;
10158 /* Transform ABS (X) into X or -X as appropriate. */
10159 case ABS_EXPR:
10160 if (TREE_CODE (rhs1) == SSA_NAME
10161 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10162 return simplify_abs_using_ranges (stmt);
10163 break;
10165 case BIT_AND_EXPR:
10166 case BIT_IOR_EXPR:
10167 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10168 if all the bits being cleared are already cleared or
10169 all the bits being set are already set. */
10170 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10171 return simplify_bit_ops_using_ranges (gsi, stmt);
10172 break;
10174 CASE_CONVERT:
10175 if (TREE_CODE (rhs1) == SSA_NAME
10176 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10177 return simplify_conversion_using_ranges (stmt);
10178 break;
10180 case FLOAT_EXPR:
10181 if (TREE_CODE (rhs1) == SSA_NAME
10182 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10183 return simplify_float_conversion_using_ranges (gsi, stmt);
10184 break;
10186 case MIN_EXPR:
10187 case MAX_EXPR:
10188 return simplify_min_or_max_using_ranges (stmt);
10189 break;
10191 default:
10192 break;
10195 else if (gimple_code (stmt) == GIMPLE_COND)
10196 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
10197 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10198 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10199 else if (is_gimple_call (stmt)
10200 && gimple_call_internal_p (stmt))
10201 return simplify_internal_call_using_ranges (gsi, stmt);
10203 return false;
10206 /* If the statement pointed by SI has a predicate whose value can be
10207 computed using the value range information computed by VRP, compute
10208 its value and return true. Otherwise, return false. */
10210 static bool
10211 fold_predicate_in (gimple_stmt_iterator *si)
10213 bool assignment_p = false;
10214 tree val;
10215 gimple *stmt = gsi_stmt (*si);
10217 if (is_gimple_assign (stmt)
10218 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10220 assignment_p = true;
10221 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10222 gimple_assign_rhs1 (stmt),
10223 gimple_assign_rhs2 (stmt),
10224 stmt);
10226 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10227 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10228 gimple_cond_lhs (cond_stmt),
10229 gimple_cond_rhs (cond_stmt),
10230 stmt);
10231 else
10232 return false;
10234 if (val)
10236 if (assignment_p)
10237 val = fold_convert (gimple_expr_type (stmt), val);
10239 if (dump_file)
10241 fprintf (dump_file, "Folding predicate ");
10242 print_gimple_expr (dump_file, stmt, 0, 0);
10243 fprintf (dump_file, " to ");
10244 print_generic_expr (dump_file, val, 0);
10245 fprintf (dump_file, "\n");
10248 if (is_gimple_assign (stmt))
10249 gimple_assign_set_rhs_from_tree (si, val);
10250 else
10252 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10253 gcond *cond_stmt = as_a <gcond *> (stmt);
10254 if (integer_zerop (val))
10255 gimple_cond_make_false (cond_stmt);
10256 else if (integer_onep (val))
10257 gimple_cond_make_true (cond_stmt);
10258 else
10259 gcc_unreachable ();
10262 return true;
10265 return false;
10268 /* Callback for substitute_and_fold folding the stmt at *SI. */
10270 static bool
10271 vrp_fold_stmt (gimple_stmt_iterator *si)
10273 if (fold_predicate_in (si))
10274 return true;
10276 return simplify_stmt_using_ranges (si);
10279 /* Unwindable const/copy equivalences. */
10280 const_and_copies *equiv_stack;
10282 /* A trivial wrapper so that we can present the generic jump threading
10283 code with a simple API for simplifying statements. STMT is the
10284 statement we want to simplify, WITHIN_STMT provides the location
10285 for any overflow warnings. */
10287 static tree
10288 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10289 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10291 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10292 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10293 gimple_cond_lhs (cond_stmt),
10294 gimple_cond_rhs (cond_stmt),
10295 within_stmt);
10297 /* We simplify a switch statement by trying to determine which case label
10298 will be taken. If we are successful then we return the corresponding
10299 CASE_LABEL_EXPR. */
10300 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
10302 tree op = gimple_switch_index (switch_stmt);
10303 if (TREE_CODE (op) != SSA_NAME)
10304 return NULL_TREE;
10306 value_range *vr = get_value_range (op);
10307 if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE)
10308 || symbolic_range_p (vr))
10309 return NULL_TREE;
10311 if (vr->type == VR_RANGE)
10313 size_t i, j;
10314 /* Get the range of labels that contain a part of the operand's
10315 value range. */
10316 find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j);
10318 /* Is there only one such label? */
10319 if (i == j)
10321 tree label = gimple_switch_label (switch_stmt, i);
10323 /* The i'th label will be taken only if the value range of the
10324 operand is entirely within the bounds of this label. */
10325 if (CASE_HIGH (label) != NULL_TREE
10326 ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0
10327 && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0)
10328 : (tree_int_cst_equal (CASE_LOW (label), vr->min)
10329 && tree_int_cst_equal (vr->min, vr->max)))
10330 return label;
10333 /* If there are no such labels then the default label will be
10334 taken. */
10335 if (i > j)
10336 return gimple_switch_label (switch_stmt, 0);
10339 if (vr->type == VR_ANTI_RANGE)
10341 unsigned n = gimple_switch_num_labels (switch_stmt);
10342 tree min_label = gimple_switch_label (switch_stmt, 1);
10343 tree max_label = gimple_switch_label (switch_stmt, n - 1);
10345 /* The default label will be taken only if the anti-range of the
10346 operand is entirely outside the bounds of all the (non-default)
10347 case labels. */
10348 if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0
10349 && (CASE_HIGH (max_label) != NULL_TREE
10350 ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0
10351 : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0))
10352 return gimple_switch_label (switch_stmt, 0);
10355 return NULL_TREE;
10358 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10360 value_range new_vr = VR_INITIALIZER;
10361 tree lhs = gimple_assign_lhs (assign_stmt);
10363 if (TREE_CODE (lhs) == SSA_NAME
10364 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10365 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10367 extract_range_from_assignment (&new_vr, assign_stmt);
10368 if (range_int_cst_singleton_p (&new_vr))
10369 return new_vr.min;
10373 return NULL_TREE;
10376 /* Blocks which have more than one predecessor and more than
10377 one successor present jump threading opportunities, i.e.,
10378 when the block is reached from a specific predecessor, we
10379 may be able to determine which of the outgoing edges will
10380 be traversed. When this optimization applies, we are able
10381 to avoid conditionals at runtime and we may expose secondary
10382 optimization opportunities.
10384 This routine is effectively a driver for the generic jump
10385 threading code. It basically just presents the generic code
10386 with edges that may be suitable for jump threading.
10388 Unlike DOM, we do not iterate VRP if jump threading was successful.
10389 While iterating may expose new opportunities for VRP, it is expected
10390 those opportunities would be very limited and the compile time cost
10391 to expose those opportunities would be significant.
10393 As jump threading opportunities are discovered, they are registered
10394 for later realization. */
10396 static void
10397 identify_jump_threads (void)
10399 basic_block bb;
10400 gcond *dummy;
10401 int i;
10402 edge e;
10404 /* Ugh. When substituting values earlier in this pass we can
10405 wipe the dominance information. So rebuild the dominator
10406 information as we need it within the jump threading code. */
10407 calculate_dominance_info (CDI_DOMINATORS);
10409 /* We do not allow VRP information to be used for jump threading
10410 across a back edge in the CFG. Otherwise it becomes too
10411 difficult to avoid eliminating loop exit tests. Of course
10412 EDGE_DFS_BACK is not accurate at this time so we have to
10413 recompute it. */
10414 mark_dfs_back_edges ();
10416 /* Do not thread across edges we are about to remove. Just marking
10417 them as EDGE_IGNORE will do. */
10418 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10419 e->flags |= EDGE_IGNORE;
10421 /* Allocate our unwinder stack to unwind any temporary equivalences
10422 that might be recorded. */
10423 equiv_stack = new const_and_copies ();
10425 /* To avoid lots of silly node creation, we create a single
10426 conditional and just modify it in-place when attempting to
10427 thread jumps. */
10428 dummy = gimple_build_cond (EQ_EXPR,
10429 integer_zero_node, integer_zero_node,
10430 NULL, NULL);
10432 /* Walk through all the blocks finding those which present a
10433 potential jump threading opportunity. We could set this up
10434 as a dominator walker and record data during the walk, but
10435 I doubt it's worth the effort for the classes of jump
10436 threading opportunities we are trying to identify at this
10437 point in compilation. */
10438 FOR_EACH_BB_FN (bb, cfun)
10440 gimple *last;
10442 /* If the generic jump threading code does not find this block
10443 interesting, then there is nothing to do. */
10444 if (! potentially_threadable_block (bb))
10445 continue;
10447 last = last_stmt (bb);
10449 /* We're basically looking for a switch or any kind of conditional with
10450 integral or pointer type arguments. Note the type of the second
10451 argument will be the same as the first argument, so no need to
10452 check it explicitly.
10454 We also handle the case where there are no statements in the
10455 block. This come up with forwarder blocks that are not
10456 optimized away because they lead to a loop header. But we do
10457 want to thread through them as we can sometimes thread to the
10458 loop exit which is obviously profitable. */
10459 if (!last
10460 || gimple_code (last) == GIMPLE_SWITCH
10461 || (gimple_code (last) == GIMPLE_COND
10462 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10463 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10464 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10465 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10466 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10468 edge_iterator ei;
10470 /* We've got a block with multiple predecessors and multiple
10471 successors which also ends in a suitable conditional or
10472 switch statement. For each predecessor, see if we can thread
10473 it to a specific successor. */
10474 FOR_EACH_EDGE (e, ei, bb->preds)
10476 /* Do not thread across edges marked to ignoreor abnormal
10477 edges in the CFG. */
10478 if (e->flags & (EDGE_IGNORE | EDGE_COMPLEX))
10479 continue;
10481 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10482 simplify_stmt_for_jump_threading);
10487 /* Clear EDGE_IGNORE. */
10488 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10489 e->flags &= ~EDGE_IGNORE;
10491 /* We do not actually update the CFG or SSA graphs at this point as
10492 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10493 handle ASSERT_EXPRs gracefully. */
10496 /* We identified all the jump threading opportunities earlier, but could
10497 not transform the CFG at that time. This routine transforms the
10498 CFG and arranges for the dominator tree to be rebuilt if necessary.
10500 Note the SSA graph update will occur during the normal TODO
10501 processing by the pass manager. */
10502 static void
10503 finalize_jump_threads (void)
10505 thread_through_all_blocks (false);
10506 delete equiv_stack;
10510 /* Traverse all the blocks folding conditionals with known ranges. */
10512 static void
10513 vrp_finalize (bool warn_array_bounds_p)
10515 size_t i;
10517 values_propagated = true;
10519 if (dump_file)
10521 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10522 dump_all_value_ranges (dump_file);
10523 fprintf (dump_file, "\n");
10526 /* Set value range to non pointer SSA_NAMEs. */
10527 for (i = 0; i < num_vr_values; i++)
10528 if (vr_value[i])
10530 tree name = ssa_name (i);
10532 if (!name
10533 || POINTER_TYPE_P (TREE_TYPE (name))
10534 || (vr_value[i]->type == VR_VARYING)
10535 || (vr_value[i]->type == VR_UNDEFINED))
10536 continue;
10538 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10539 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10540 && (vr_value[i]->type == VR_RANGE
10541 || vr_value[i]->type == VR_ANTI_RANGE))
10542 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10543 vr_value[i]->max);
10546 substitute_and_fold (op_with_constant_singleton_value_range,
10547 vrp_fold_stmt, false);
10549 if (warn_array_bounds && warn_array_bounds_p)
10550 check_all_array_refs ();
10552 /* We must identify jump threading opportunities before we release
10553 the datastructures built by VRP. */
10554 identify_jump_threads ();
10556 /* Free allocated memory. */
10557 free (vr_value);
10558 free (vr_phi_edge_counts);
10559 bitmap_obstack_release (&vrp_equiv_obstack);
10560 vrp_value_range_pool.release ();
10562 /* So that we can distinguish between VRP data being available
10563 and not available. */
10564 vr_value = NULL;
10565 vr_phi_edge_counts = NULL;
10569 /* Main entry point to VRP (Value Range Propagation). This pass is
10570 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10571 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10572 Programming Language Design and Implementation, pp. 67-78, 1995.
10573 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10575 This is essentially an SSA-CCP pass modified to deal with ranges
10576 instead of constants.
10578 While propagating ranges, we may find that two or more SSA name
10579 have equivalent, though distinct ranges. For instance,
10581 1 x_9 = p_3->a;
10582 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10583 3 if (p_4 == q_2)
10584 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10585 5 endif
10586 6 if (q_2)
10588 In the code above, pointer p_5 has range [q_2, q_2], but from the
10589 code we can also determine that p_5 cannot be NULL and, if q_2 had
10590 a non-varying range, p_5's range should also be compatible with it.
10592 These equivalences are created by two expressions: ASSERT_EXPR and
10593 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10594 result of another assertion, then we can use the fact that p_5 and
10595 p_4 are equivalent when evaluating p_5's range.
10597 Together with value ranges, we also propagate these equivalences
10598 between names so that we can take advantage of information from
10599 multiple ranges when doing final replacement. Note that this
10600 equivalency relation is transitive but not symmetric.
10602 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10603 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10604 in contexts where that assertion does not hold (e.g., in line 6).
10606 TODO, the main difference between this pass and Patterson's is that
10607 we do not propagate edge probabilities. We only compute whether
10608 edges can be taken or not. That is, instead of having a spectrum
10609 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10610 DON'T KNOW. In the future, it may be worthwhile to propagate
10611 probabilities to aid branch prediction. */
10613 static unsigned int
10614 execute_vrp (bool warn_array_bounds_p)
10616 int i;
10617 edge e;
10618 switch_update *su;
10620 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10621 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10622 scev_initialize ();
10624 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10625 Inserting assertions may split edges which will invalidate
10626 EDGE_DFS_BACK. */
10627 insert_range_assertions ();
10629 to_remove_edges.create (10);
10630 to_update_switch_stmts.create (5);
10631 threadedge_initialize_values ();
10633 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10634 mark_dfs_back_edges ();
10636 vrp_initialize ();
10637 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10638 vrp_finalize (warn_array_bounds_p);
10640 free_numbers_of_iterations_estimates (cfun);
10642 /* ASSERT_EXPRs must be removed before finalizing jump threads
10643 as finalizing jump threads calls the CFG cleanup code which
10644 does not properly handle ASSERT_EXPRs. */
10645 remove_range_assertions ();
10647 /* If we exposed any new variables, go ahead and put them into
10648 SSA form now, before we handle jump threading. This simplifies
10649 interactions between rewriting of _DECL nodes into SSA form
10650 and rewriting SSA_NAME nodes into SSA form after block
10651 duplication and CFG manipulation. */
10652 update_ssa (TODO_update_ssa);
10654 finalize_jump_threads ();
10656 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10657 CFG in a broken state and requires a cfg_cleanup run. */
10658 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10659 remove_edge (e);
10660 /* Update SWITCH_EXPR case label vector. */
10661 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10663 size_t j;
10664 size_t n = TREE_VEC_LENGTH (su->vec);
10665 tree label;
10666 gimple_switch_set_num_labels (su->stmt, n);
10667 for (j = 0; j < n; j++)
10668 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10669 /* As we may have replaced the default label with a regular one
10670 make sure to make it a real default label again. This ensures
10671 optimal expansion. */
10672 label = gimple_switch_label (su->stmt, 0);
10673 CASE_LOW (label) = NULL_TREE;
10674 CASE_HIGH (label) = NULL_TREE;
10677 if (to_remove_edges.length () > 0)
10679 free_dominance_info (CDI_DOMINATORS);
10680 loops_state_set (LOOPS_NEED_FIXUP);
10683 to_remove_edges.release ();
10684 to_update_switch_stmts.release ();
10685 threadedge_finalize_values ();
10687 scev_finalize ();
10688 loop_optimizer_finalize ();
10689 return 0;
10692 namespace {
10694 const pass_data pass_data_vrp =
10696 GIMPLE_PASS, /* type */
10697 "vrp", /* name */
10698 OPTGROUP_NONE, /* optinfo_flags */
10699 TV_TREE_VRP, /* tv_id */
10700 PROP_ssa, /* properties_required */
10701 0, /* properties_provided */
10702 0, /* properties_destroyed */
10703 0, /* todo_flags_start */
10704 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10707 class pass_vrp : public gimple_opt_pass
10709 public:
10710 pass_vrp (gcc::context *ctxt)
10711 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
10714 /* opt_pass methods: */
10715 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10716 void set_pass_param (unsigned int n, bool param)
10718 gcc_assert (n == 0);
10719 warn_array_bounds_p = param;
10721 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10722 virtual unsigned int execute (function *)
10723 { return execute_vrp (warn_array_bounds_p); }
10725 private:
10726 bool warn_array_bounds_p;
10727 }; // class pass_vrp
10729 } // anon namespace
10731 gimple_opt_pass *
10732 make_pass_vrp (gcc::context *ctxt)
10734 return new pass_vrp (ctxt);