2016-09-26 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / tree-vrp.c
blob3c75a0d98852854a80cef104be02e9a888450842
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"
63 #include "domwalk.h"
64 #include "tree-cfgcleanup.h"
66 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
68 /* Allocation pools for tree-vrp allocations. */
69 static object_allocator<value_range> vrp_value_range_pool ("Tree VRP value ranges");
70 static bitmap_obstack vrp_equiv_obstack;
72 /* Set of SSA names found live during the RPO traversal of the function
73 for still active basic-blocks. */
74 static sbitmap *live;
76 /* Return true if the SSA name NAME is live on the edge E. */
78 static bool
79 live_on_edge (edge e, tree name)
81 return (live[e->dest->index]
82 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
85 /* Local functions. */
86 static int compare_values (tree val1, tree val2);
87 static int compare_values_warnv (tree val1, tree val2, bool *);
88 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
89 tree, tree, bool, bool *,
90 bool *);
92 /* Location information for ASSERT_EXPRs. Each instance of this
93 structure describes an ASSERT_EXPR for an SSA name. Since a single
94 SSA name may have more than one assertion associated with it, these
95 locations are kept in a linked list attached to the corresponding
96 SSA name. */
97 struct assert_locus
99 /* Basic block where the assertion would be inserted. */
100 basic_block bb;
102 /* Some assertions need to be inserted on an edge (e.g., assertions
103 generated by COND_EXPRs). In those cases, BB will be NULL. */
104 edge e;
106 /* Pointer to the statement that generated this assertion. */
107 gimple_stmt_iterator si;
109 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
110 enum tree_code comp_code;
112 /* Value being compared against. */
113 tree val;
115 /* Expression to compare. */
116 tree expr;
118 /* Next node in the linked list. */
119 assert_locus *next;
122 /* If bit I is present, it means that SSA name N_i has a list of
123 assertions that should be inserted in the IL. */
124 static bitmap need_assert_for;
126 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
127 holds a list of ASSERT_LOCUS_T nodes that describe where
128 ASSERT_EXPRs for SSA name N_I should be inserted. */
129 static assert_locus **asserts_for;
131 /* Value range array. After propagation, VR_VALUE[I] holds the range
132 of values that SSA name N_I may take. */
133 static unsigned num_vr_values;
134 static value_range **vr_value;
135 static bool values_propagated;
137 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
138 number of executable edges we saw the last time we visited the
139 node. */
140 static int *vr_phi_edge_counts;
142 struct switch_update {
143 gswitch *stmt;
144 tree vec;
147 static vec<edge> to_remove_edges;
148 static vec<switch_update> to_update_switch_stmts;
151 /* Return the maximum value for TYPE. */
153 static inline tree
154 vrp_val_max (const_tree type)
156 if (!INTEGRAL_TYPE_P (type))
157 return NULL_TREE;
159 return TYPE_MAX_VALUE (type);
162 /* Return the minimum value for TYPE. */
164 static inline tree
165 vrp_val_min (const_tree type)
167 if (!INTEGRAL_TYPE_P (type))
168 return NULL_TREE;
170 return TYPE_MIN_VALUE (type);
173 /* Return whether VAL is equal to the maximum value of its type. This
174 will be true for a positive overflow infinity. We can't do a
175 simple equality comparison with TYPE_MAX_VALUE because C typedefs
176 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
177 to the integer constant with the same value in the type. */
179 static inline bool
180 vrp_val_is_max (const_tree val)
182 tree type_max = vrp_val_max (TREE_TYPE (val));
183 return (val == type_max
184 || (type_max != NULL_TREE
185 && operand_equal_p (val, type_max, 0)));
188 /* Return whether VAL is equal to the minimum value of its type. This
189 will be true for a negative overflow infinity. */
191 static inline bool
192 vrp_val_is_min (const_tree val)
194 tree type_min = vrp_val_min (TREE_TYPE (val));
195 return (val == type_min
196 || (type_min != NULL_TREE
197 && operand_equal_p (val, type_min, 0)));
201 /* Return whether TYPE should use an overflow infinity distinct from
202 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
203 represent a signed overflow during VRP computations. An infinity
204 is distinct from a half-range, which will go from some number to
205 TYPE_{MIN,MAX}_VALUE. */
207 static inline bool
208 needs_overflow_infinity (const_tree type)
210 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
213 /* Return whether TYPE can support our overflow infinity
214 representation: we use the TREE_OVERFLOW flag, which only exists
215 for constants. If TYPE doesn't support this, we don't optimize
216 cases which would require signed overflow--we drop them to
217 VARYING. */
219 static inline bool
220 supports_overflow_infinity (const_tree type)
222 tree min = vrp_val_min (type), max = vrp_val_max (type);
223 gcc_checking_assert (needs_overflow_infinity (type));
224 return (min != NULL_TREE
225 && CONSTANT_CLASS_P (min)
226 && max != NULL_TREE
227 && CONSTANT_CLASS_P (max));
230 /* VAL is the maximum or minimum value of a type. Return a
231 corresponding overflow infinity. */
233 static inline tree
234 make_overflow_infinity (tree val)
236 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
237 val = copy_node (val);
238 TREE_OVERFLOW (val) = 1;
239 return val;
242 /* Return a negative overflow infinity for TYPE. */
244 static inline tree
245 negative_overflow_infinity (tree type)
247 gcc_checking_assert (supports_overflow_infinity (type));
248 return make_overflow_infinity (vrp_val_min (type));
251 /* Return a positive overflow infinity for TYPE. */
253 static inline tree
254 positive_overflow_infinity (tree type)
256 gcc_checking_assert (supports_overflow_infinity (type));
257 return make_overflow_infinity (vrp_val_max (type));
260 /* Return whether VAL is a negative overflow infinity. */
262 static inline bool
263 is_negative_overflow_infinity (const_tree val)
265 return (TREE_OVERFLOW_P (val)
266 && needs_overflow_infinity (TREE_TYPE (val))
267 && vrp_val_is_min (val));
270 /* Return whether VAL is a positive overflow infinity. */
272 static inline bool
273 is_positive_overflow_infinity (const_tree val)
275 return (TREE_OVERFLOW_P (val)
276 && needs_overflow_infinity (TREE_TYPE (val))
277 && vrp_val_is_max (val));
280 /* Return whether VAL is a positive or negative overflow infinity. */
282 static inline bool
283 is_overflow_infinity (const_tree val)
285 return (TREE_OVERFLOW_P (val)
286 && needs_overflow_infinity (TREE_TYPE (val))
287 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
290 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
292 static inline bool
293 stmt_overflow_infinity (gimple *stmt)
295 if (is_gimple_assign (stmt)
296 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
297 GIMPLE_SINGLE_RHS)
298 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
299 return false;
302 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
303 the same value with TREE_OVERFLOW clear. This can be used to avoid
304 confusing a regular value with an overflow value. */
306 static inline tree
307 avoid_overflow_infinity (tree val)
309 if (!is_overflow_infinity (val))
310 return val;
312 if (vrp_val_is_max (val))
313 return vrp_val_max (TREE_TYPE (val));
314 else
316 gcc_checking_assert (vrp_val_is_min (val));
317 return vrp_val_min (TREE_TYPE (val));
322 /* Set value range VR to VR_UNDEFINED. */
324 static inline void
325 set_value_range_to_undefined (value_range *vr)
327 vr->type = VR_UNDEFINED;
328 vr->min = vr->max = NULL_TREE;
329 if (vr->equiv)
330 bitmap_clear (vr->equiv);
334 /* Set value range VR to VR_VARYING. */
336 static inline void
337 set_value_range_to_varying (value_range *vr)
339 vr->type = VR_VARYING;
340 vr->min = vr->max = NULL_TREE;
341 if (vr->equiv)
342 bitmap_clear (vr->equiv);
346 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
348 static void
349 set_value_range (value_range *vr, enum value_range_type t, tree min,
350 tree max, bitmap equiv)
352 /* Check the validity of the range. */
353 if (flag_checking
354 && (t == VR_RANGE || t == VR_ANTI_RANGE))
356 int cmp;
358 gcc_assert (min && max);
360 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
361 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
363 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
364 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
366 cmp = compare_values (min, max);
367 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
369 if (needs_overflow_infinity (TREE_TYPE (min)))
370 gcc_assert (!is_overflow_infinity (min)
371 || !is_overflow_infinity (max));
374 if (flag_checking
375 && (t == VR_UNDEFINED || t == VR_VARYING))
377 gcc_assert (min == NULL_TREE && max == NULL_TREE);
378 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
381 vr->type = t;
382 vr->min = min;
383 vr->max = max;
385 /* Since updating the equivalence set involves deep copying the
386 bitmaps, only do it if absolutely necessary. */
387 if (vr->equiv == NULL
388 && equiv != NULL)
389 vr->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
391 if (equiv != vr->equiv)
393 if (equiv && !bitmap_empty_p (equiv))
394 bitmap_copy (vr->equiv, equiv);
395 else
396 bitmap_clear (vr->equiv);
401 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
402 This means adjusting T, MIN and MAX representing the case of a
403 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
404 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
405 In corner cases where MAX+1 or MIN-1 wraps this will fall back
406 to varying.
407 This routine exists to ease canonicalization in the case where we
408 extract ranges from var + CST op limit. */
410 static void
411 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
412 tree min, tree max, bitmap equiv)
414 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
415 if (t == VR_UNDEFINED)
417 set_value_range_to_undefined (vr);
418 return;
420 else if (t == VR_VARYING)
422 set_value_range_to_varying (vr);
423 return;
426 /* Nothing to canonicalize for symbolic ranges. */
427 if (TREE_CODE (min) != INTEGER_CST
428 || TREE_CODE (max) != INTEGER_CST)
430 set_value_range (vr, t, min, max, equiv);
431 return;
434 /* Wrong order for min and max, to swap them and the VR type we need
435 to adjust them. */
436 if (tree_int_cst_lt (max, min))
438 tree one, tmp;
440 /* For one bit precision if max < min, then the swapped
441 range covers all values, so for VR_RANGE it is varying and
442 for VR_ANTI_RANGE empty range, so drop to varying as well. */
443 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
445 set_value_range_to_varying (vr);
446 return;
449 one = build_int_cst (TREE_TYPE (min), 1);
450 tmp = int_const_binop (PLUS_EXPR, max, one);
451 max = int_const_binop (MINUS_EXPR, min, one);
452 min = tmp;
454 /* There's one corner case, if we had [C+1, C] before we now have
455 that again. But this represents an empty value range, so drop
456 to varying in this case. */
457 if (tree_int_cst_lt (max, min))
459 set_value_range_to_varying (vr);
460 return;
463 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
466 /* Anti-ranges that can be represented as ranges should be so. */
467 if (t == VR_ANTI_RANGE)
469 bool is_min = vrp_val_is_min (min);
470 bool is_max = vrp_val_is_max (max);
472 if (is_min && is_max)
474 /* We cannot deal with empty ranges, drop to varying.
475 ??? This could be VR_UNDEFINED instead. */
476 set_value_range_to_varying (vr);
477 return;
479 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
480 && (is_min || is_max))
482 /* Non-empty boolean ranges can always be represented
483 as a singleton range. */
484 if (is_min)
485 min = max = vrp_val_max (TREE_TYPE (min));
486 else
487 min = max = vrp_val_min (TREE_TYPE (min));
488 t = VR_RANGE;
490 else if (is_min
491 /* As a special exception preserve non-null ranges. */
492 && !(TYPE_UNSIGNED (TREE_TYPE (min))
493 && integer_zerop (max)))
495 tree one = build_int_cst (TREE_TYPE (max), 1);
496 min = int_const_binop (PLUS_EXPR, max, one);
497 max = vrp_val_max (TREE_TYPE (max));
498 t = VR_RANGE;
500 else if (is_max)
502 tree one = build_int_cst (TREE_TYPE (min), 1);
503 max = int_const_binop (MINUS_EXPR, min, one);
504 min = vrp_val_min (TREE_TYPE (min));
505 t = VR_RANGE;
509 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
510 if (needs_overflow_infinity (TREE_TYPE (min))
511 && is_overflow_infinity (min)
512 && is_overflow_infinity (max))
514 set_value_range_to_varying (vr);
515 return;
518 set_value_range (vr, t, min, max, equiv);
521 /* Copy value range FROM into value range TO. */
523 static inline void
524 copy_value_range (value_range *to, value_range *from)
526 set_value_range (to, from->type, from->min, from->max, from->equiv);
529 /* Set value range VR to a single value. This function is only called
530 with values we get from statements, and exists to clear the
531 TREE_OVERFLOW flag so that we don't think we have an overflow
532 infinity when we shouldn't. */
534 static inline void
535 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
537 gcc_assert (is_gimple_min_invariant (val));
538 if (TREE_OVERFLOW_P (val))
539 val = drop_tree_overflow (val);
540 set_value_range (vr, VR_RANGE, val, val, equiv);
543 /* Set value range VR to a non-negative range of type TYPE.
544 OVERFLOW_INFINITY indicates whether to use an overflow infinity
545 rather than TYPE_MAX_VALUE; this should be true if we determine
546 that the range is nonnegative based on the assumption that signed
547 overflow does not occur. */
549 static inline void
550 set_value_range_to_nonnegative (value_range *vr, tree type,
551 bool overflow_infinity)
553 tree zero;
555 if (overflow_infinity && !supports_overflow_infinity (type))
557 set_value_range_to_varying (vr);
558 return;
561 zero = build_int_cst (type, 0);
562 set_value_range (vr, VR_RANGE, zero,
563 (overflow_infinity
564 ? positive_overflow_infinity (type)
565 : TYPE_MAX_VALUE (type)),
566 vr->equiv);
569 /* Set value range VR to a non-NULL range of type TYPE. */
571 static inline void
572 set_value_range_to_nonnull (value_range *vr, tree type)
574 tree zero = build_int_cst (type, 0);
575 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
579 /* Set value range VR to a NULL range of type TYPE. */
581 static inline void
582 set_value_range_to_null (value_range *vr, tree type)
584 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
588 /* Set value range VR to a range of a truthvalue of type TYPE. */
590 static inline void
591 set_value_range_to_truthvalue (value_range *vr, tree type)
593 if (TYPE_PRECISION (type) == 1)
594 set_value_range_to_varying (vr);
595 else
596 set_value_range (vr, VR_RANGE,
597 build_int_cst (type, 0), build_int_cst (type, 1),
598 vr->equiv);
602 /* If abs (min) < abs (max), set VR to [-max, max], if
603 abs (min) >= abs (max), set VR to [-min, min]. */
605 static void
606 abs_extent_range (value_range *vr, tree min, tree max)
608 int cmp;
610 gcc_assert (TREE_CODE (min) == INTEGER_CST);
611 gcc_assert (TREE_CODE (max) == INTEGER_CST);
612 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
613 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
614 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
615 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
616 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
618 set_value_range_to_varying (vr);
619 return;
621 cmp = compare_values (min, max);
622 if (cmp == -1)
623 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
624 else if (cmp == 0 || cmp == 1)
626 max = min;
627 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
629 else
631 set_value_range_to_varying (vr);
632 return;
634 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
638 /* Return value range information for VAR.
640 If we have no values ranges recorded (ie, VRP is not running), then
641 return NULL. Otherwise create an empty range if none existed for VAR. */
643 static value_range *
644 get_value_range (const_tree var)
646 static const value_range vr_const_varying
647 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
648 value_range *vr;
649 tree sym;
650 unsigned ver = SSA_NAME_VERSION (var);
652 /* If we have no recorded ranges, then return NULL. */
653 if (! vr_value)
654 return NULL;
656 /* If we query the range for a new SSA name return an unmodifiable VARYING.
657 We should get here at most from the substitute-and-fold stage which
658 will never try to change values. */
659 if (ver >= num_vr_values)
660 return CONST_CAST (value_range *, &vr_const_varying);
662 vr = vr_value[ver];
663 if (vr)
664 return vr;
666 /* After propagation finished do not allocate new value-ranges. */
667 if (values_propagated)
668 return CONST_CAST (value_range *, &vr_const_varying);
670 /* Create a default value range. */
671 vr_value[ver] = vr = vrp_value_range_pool.allocate ();
672 memset (vr, 0, sizeof (*vr));
674 /* Defer allocating the equivalence set. */
675 vr->equiv = NULL;
677 /* If VAR is a default definition of a parameter, the variable can
678 take any value in VAR's type. */
679 if (SSA_NAME_IS_DEFAULT_DEF (var))
681 sym = SSA_NAME_VAR (var);
682 if (TREE_CODE (sym) == PARM_DECL)
684 /* Try to use the "nonnull" attribute to create ~[0, 0]
685 anti-ranges for pointers. Note that this is only valid with
686 default definitions of PARM_DECLs. */
687 if (POINTER_TYPE_P (TREE_TYPE (sym))
688 && nonnull_arg_p (sym))
689 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
690 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym)))
692 wide_int min, max;
693 value_range_type rtype = get_range_info (var, &min, &max);
694 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
695 set_value_range (vr, rtype,
696 wide_int_to_tree (TREE_TYPE (var), min),
697 wide_int_to_tree (TREE_TYPE (var), max),
698 NULL);
699 else
700 set_value_range_to_varying (vr);
702 else
703 set_value_range_to_varying (vr);
705 else if (TREE_CODE (sym) == RESULT_DECL
706 && DECL_BY_REFERENCE (sym))
707 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
710 return vr;
713 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
715 static inline bool
716 vrp_operand_equal_p (const_tree val1, const_tree val2)
718 if (val1 == val2)
719 return true;
720 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
721 return false;
722 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
725 /* Return true, if the bitmaps B1 and B2 are equal. */
727 static inline bool
728 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
730 return (b1 == b2
731 || ((!b1 || bitmap_empty_p (b1))
732 && (!b2 || bitmap_empty_p (b2)))
733 || (b1 && b2
734 && bitmap_equal_p (b1, b2)));
737 /* Update the value range and equivalence set for variable VAR to
738 NEW_VR. Return true if NEW_VR is different from VAR's previous
739 value.
741 NOTE: This function assumes that NEW_VR is a temporary value range
742 object created for the sole purpose of updating VAR's range. The
743 storage used by the equivalence set from NEW_VR will be freed by
744 this function. Do not call update_value_range when NEW_VR
745 is the range object associated with another SSA name. */
747 static inline bool
748 update_value_range (const_tree var, value_range *new_vr)
750 value_range *old_vr;
751 bool is_new;
753 /* If there is a value-range on the SSA name from earlier analysis
754 factor that in. */
755 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
757 wide_int min, max;
758 value_range_type rtype = get_range_info (var, &min, &max);
759 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
761 tree nr_min, nr_max;
762 /* Range info on SSA names doesn't carry overflow information
763 so make sure to preserve the overflow bit on the lattice. */
764 if (rtype == VR_RANGE
765 && needs_overflow_infinity (TREE_TYPE (var))
766 && (new_vr->type == VR_VARYING
767 || (new_vr->type == VR_RANGE
768 && is_negative_overflow_infinity (new_vr->min)))
769 && wi::eq_p (vrp_val_min (TREE_TYPE (var)), min))
770 nr_min = negative_overflow_infinity (TREE_TYPE (var));
771 else
772 nr_min = wide_int_to_tree (TREE_TYPE (var), min);
773 if (rtype == VR_RANGE
774 && needs_overflow_infinity (TREE_TYPE (var))
775 && (new_vr->type == VR_VARYING
776 || (new_vr->type == VR_RANGE
777 && is_positive_overflow_infinity (new_vr->max)))
778 && wi::eq_p (vrp_val_max (TREE_TYPE (var)), max))
779 nr_max = positive_overflow_infinity (TREE_TYPE (var));
780 else
781 nr_max = wide_int_to_tree (TREE_TYPE (var), max);
782 value_range nr = VR_INITIALIZER;
783 set_and_canonicalize_value_range (&nr, rtype, nr_min, nr_max, NULL);
784 vrp_intersect_ranges (new_vr, &nr);
788 /* Update the value range, if necessary. */
789 old_vr = get_value_range (var);
790 is_new = old_vr->type != new_vr->type
791 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
792 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
793 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
795 if (is_new)
797 /* Do not allow transitions up the lattice. The following
798 is slightly more awkward than just new_vr->type < old_vr->type
799 because VR_RANGE and VR_ANTI_RANGE need to be considered
800 the same. We may not have is_new when transitioning to
801 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
802 called. */
803 if (new_vr->type == VR_UNDEFINED)
805 BITMAP_FREE (new_vr->equiv);
806 set_value_range_to_varying (old_vr);
807 set_value_range_to_varying (new_vr);
808 return true;
810 else
811 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
812 new_vr->equiv);
815 BITMAP_FREE (new_vr->equiv);
817 return is_new;
821 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
822 point where equivalence processing can be turned on/off. */
824 static void
825 add_equivalence (bitmap *equiv, const_tree var)
827 unsigned ver = SSA_NAME_VERSION (var);
828 value_range *vr = vr_value[ver];
830 if (*equiv == NULL)
831 *equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
832 bitmap_set_bit (*equiv, ver);
833 if (vr && vr->equiv)
834 bitmap_ior_into (*equiv, vr->equiv);
838 /* Return true if VR is ~[0, 0]. */
840 static inline bool
841 range_is_nonnull (value_range *vr)
843 return vr->type == VR_ANTI_RANGE
844 && integer_zerop (vr->min)
845 && integer_zerop (vr->max);
849 /* Return true if VR is [0, 0]. */
851 static inline bool
852 range_is_null (value_range *vr)
854 return vr->type == VR_RANGE
855 && integer_zerop (vr->min)
856 && integer_zerop (vr->max);
859 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
860 a singleton. */
862 static inline bool
863 range_int_cst_p (value_range *vr)
865 return (vr->type == VR_RANGE
866 && TREE_CODE (vr->max) == INTEGER_CST
867 && TREE_CODE (vr->min) == INTEGER_CST);
870 /* Return true if VR is a INTEGER_CST singleton. */
872 static inline bool
873 range_int_cst_singleton_p (value_range *vr)
875 return (range_int_cst_p (vr)
876 && !is_overflow_infinity (vr->min)
877 && !is_overflow_infinity (vr->max)
878 && tree_int_cst_equal (vr->min, vr->max));
881 /* Return true if value range VR involves at least one symbol. */
883 static inline bool
884 symbolic_range_p (value_range *vr)
886 return (!is_gimple_min_invariant (vr->min)
887 || !is_gimple_min_invariant (vr->max));
890 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
891 otherwise. We only handle additive operations and set NEG to true if the
892 symbol is negated and INV to the invariant part, if any. */
894 static tree
895 get_single_symbol (tree t, bool *neg, tree *inv)
897 bool neg_;
898 tree inv_;
900 *inv = NULL_TREE;
901 *neg = false;
903 if (TREE_CODE (t) == PLUS_EXPR
904 || TREE_CODE (t) == POINTER_PLUS_EXPR
905 || TREE_CODE (t) == MINUS_EXPR)
907 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
909 neg_ = (TREE_CODE (t) == MINUS_EXPR);
910 inv_ = TREE_OPERAND (t, 0);
911 t = TREE_OPERAND (t, 1);
913 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
915 neg_ = false;
916 inv_ = TREE_OPERAND (t, 1);
917 t = TREE_OPERAND (t, 0);
919 else
920 return NULL_TREE;
922 else
924 neg_ = false;
925 inv_ = NULL_TREE;
928 if (TREE_CODE (t) == NEGATE_EXPR)
930 t = TREE_OPERAND (t, 0);
931 neg_ = !neg_;
934 if (TREE_CODE (t) != SSA_NAME)
935 return NULL_TREE;
937 *neg = neg_;
938 *inv = inv_;
939 return t;
942 /* The reverse operation: build a symbolic expression with TYPE
943 from symbol SYM, negated according to NEG, and invariant INV. */
945 static tree
946 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
948 const bool pointer_p = POINTER_TYPE_P (type);
949 tree t = sym;
951 if (neg)
952 t = build1 (NEGATE_EXPR, type, t);
954 if (integer_zerop (inv))
955 return t;
957 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
960 /* Return true if value range VR involves exactly one symbol SYM. */
962 static bool
963 symbolic_range_based_on_p (value_range *vr, const_tree sym)
965 bool neg, min_has_symbol, max_has_symbol;
966 tree inv;
968 if (is_gimple_min_invariant (vr->min))
969 min_has_symbol = false;
970 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
971 min_has_symbol = true;
972 else
973 return false;
975 if (is_gimple_min_invariant (vr->max))
976 max_has_symbol = false;
977 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
978 max_has_symbol = true;
979 else
980 return false;
982 return (min_has_symbol || max_has_symbol);
985 /* Return true if value range VR uses an overflow infinity. */
987 static inline bool
988 overflow_infinity_range_p (value_range *vr)
990 return (vr->type == VR_RANGE
991 && (is_overflow_infinity (vr->min)
992 || is_overflow_infinity (vr->max)));
995 /* Return false if we can not make a valid comparison based on VR;
996 this will be the case if it uses an overflow infinity and overflow
997 is not undefined (i.e., -fno-strict-overflow is in effect).
998 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
999 uses an overflow infinity. */
1001 static bool
1002 usable_range_p (value_range *vr, bool *strict_overflow_p)
1004 gcc_assert (vr->type == VR_RANGE);
1005 if (is_overflow_infinity (vr->min))
1007 *strict_overflow_p = true;
1008 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1009 return false;
1011 if (is_overflow_infinity (vr->max))
1013 *strict_overflow_p = true;
1014 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1015 return false;
1017 return true;
1020 /* Return true if the result of assignment STMT is know to be non-zero.
1021 If the return value is based on the assumption that signed overflow is
1022 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1023 *STRICT_OVERFLOW_P.*/
1025 static bool
1026 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1028 enum tree_code code = gimple_assign_rhs_code (stmt);
1029 switch (get_gimple_rhs_class (code))
1031 case GIMPLE_UNARY_RHS:
1032 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1033 gimple_expr_type (stmt),
1034 gimple_assign_rhs1 (stmt),
1035 strict_overflow_p);
1036 case GIMPLE_BINARY_RHS:
1037 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1038 gimple_expr_type (stmt),
1039 gimple_assign_rhs1 (stmt),
1040 gimple_assign_rhs2 (stmt),
1041 strict_overflow_p);
1042 case GIMPLE_TERNARY_RHS:
1043 return false;
1044 case GIMPLE_SINGLE_RHS:
1045 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1046 strict_overflow_p);
1047 case GIMPLE_INVALID_RHS:
1048 gcc_unreachable ();
1049 default:
1050 gcc_unreachable ();
1054 /* Return true if STMT is known to compute a non-zero value.
1055 If the return value is based on the assumption that signed overflow is
1056 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1057 *STRICT_OVERFLOW_P.*/
1059 static bool
1060 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1062 switch (gimple_code (stmt))
1064 case GIMPLE_ASSIGN:
1065 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1066 case GIMPLE_CALL:
1068 tree fndecl = gimple_call_fndecl (stmt);
1069 if (!fndecl) return false;
1070 if (flag_delete_null_pointer_checks && !flag_check_new
1071 && DECL_IS_OPERATOR_NEW (fndecl)
1072 && !TREE_NOTHROW (fndecl))
1073 return true;
1074 /* References are always non-NULL. */
1075 if (flag_delete_null_pointer_checks
1076 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1077 return true;
1078 if (flag_delete_null_pointer_checks &&
1079 lookup_attribute ("returns_nonnull",
1080 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1081 return true;
1082 return gimple_alloca_call_p (stmt);
1084 default:
1085 gcc_unreachable ();
1089 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1090 obtained so far. */
1092 static bool
1093 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1095 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1096 return true;
1098 /* If we have an expression of the form &X->a, then the expression
1099 is nonnull if X is nonnull. */
1100 if (is_gimple_assign (stmt)
1101 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1103 tree expr = gimple_assign_rhs1 (stmt);
1104 tree base = get_base_address (TREE_OPERAND (expr, 0));
1106 if (base != NULL_TREE
1107 && TREE_CODE (base) == MEM_REF
1108 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1110 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1111 if (range_is_nonnull (vr))
1112 return true;
1116 return false;
1119 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1120 a gimple invariant, or SSA_NAME +- CST. */
1122 static bool
1123 valid_value_p (tree expr)
1125 if (TREE_CODE (expr) == SSA_NAME)
1126 return true;
1128 if (TREE_CODE (expr) == PLUS_EXPR
1129 || TREE_CODE (expr) == MINUS_EXPR)
1130 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1131 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1133 return is_gimple_min_invariant (expr);
1136 /* Return
1137 1 if VAL < VAL2
1138 0 if !(VAL < VAL2)
1139 -2 if those are incomparable. */
1140 static inline int
1141 operand_less_p (tree val, tree val2)
1143 /* LT is folded faster than GE and others. Inline the common case. */
1144 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1146 if (! is_positive_overflow_infinity (val2))
1147 return tree_int_cst_lt (val, val2);
1149 else
1151 tree tcmp;
1153 fold_defer_overflow_warnings ();
1155 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1157 fold_undefer_and_ignore_overflow_warnings ();
1159 if (!tcmp
1160 || TREE_CODE (tcmp) != INTEGER_CST)
1161 return -2;
1163 if (!integer_zerop (tcmp))
1164 return 1;
1167 /* val >= val2, not considering overflow infinity. */
1168 if (is_negative_overflow_infinity (val))
1169 return is_negative_overflow_infinity (val2) ? 0 : 1;
1170 else if (is_positive_overflow_infinity (val2))
1171 return is_positive_overflow_infinity (val) ? 0 : 1;
1173 return 0;
1176 /* Compare two values VAL1 and VAL2. Return
1178 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1179 -1 if VAL1 < VAL2,
1180 0 if VAL1 == VAL2,
1181 +1 if VAL1 > VAL2, and
1182 +2 if VAL1 != VAL2
1184 This is similar to tree_int_cst_compare but supports pointer values
1185 and values that cannot be compared at compile time.
1187 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1188 true if the return value is only valid if we assume that signed
1189 overflow is undefined. */
1191 static int
1192 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1194 if (val1 == val2)
1195 return 0;
1197 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1198 both integers. */
1199 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1200 == POINTER_TYPE_P (TREE_TYPE (val2)));
1202 /* Convert the two values into the same type. This is needed because
1203 sizetype causes sign extension even for unsigned types. */
1204 val2 = fold_convert (TREE_TYPE (val1), val2);
1205 STRIP_USELESS_TYPE_CONVERSION (val2);
1207 const bool overflow_undefined
1208 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
1209 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
1210 tree inv1, inv2;
1211 bool neg1, neg2;
1212 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
1213 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
1215 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1216 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1217 if (sym1 && sym2)
1219 /* Both values must use the same name with the same sign. */
1220 if (sym1 != sym2 || neg1 != neg2)
1221 return -2;
1223 /* [-]NAME + CST == [-]NAME + CST. */
1224 if (inv1 == inv2)
1225 return 0;
1227 /* If overflow is defined we cannot simplify more. */
1228 if (!overflow_undefined)
1229 return -2;
1231 if (strict_overflow_p != NULL
1232 && (!inv1 || !TREE_NO_WARNING (val1))
1233 && (!inv2 || !TREE_NO_WARNING (val2)))
1234 *strict_overflow_p = true;
1236 if (!inv1)
1237 inv1 = build_int_cst (TREE_TYPE (val1), 0);
1238 if (!inv2)
1239 inv2 = build_int_cst (TREE_TYPE (val2), 0);
1241 return compare_values_warnv (inv1, inv2, strict_overflow_p);
1244 const bool cst1 = is_gimple_min_invariant (val1);
1245 const bool cst2 = is_gimple_min_invariant (val2);
1247 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1248 it might be possible to say something depending on the constants. */
1249 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
1251 if (!overflow_undefined)
1252 return -2;
1254 if (strict_overflow_p != NULL
1255 && (!sym1 || !TREE_NO_WARNING (val1))
1256 && (!sym2 || !TREE_NO_WARNING (val2)))
1257 *strict_overflow_p = true;
1259 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
1260 tree cst = cst1 ? val1 : val2;
1261 tree inv = cst1 ? inv2 : inv1;
1263 /* Compute the difference between the constants. If it overflows or
1264 underflows, this means that we can trivially compare the NAME with
1265 it and, consequently, the two values with each other. */
1266 wide_int diff = wi::sub (cst, inv);
1267 if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn))
1269 const int res = wi::cmp (cst, inv, sgn);
1270 return cst1 ? res : -res;
1273 return -2;
1276 /* We cannot say anything more for non-constants. */
1277 if (!cst1 || !cst2)
1278 return -2;
1280 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1282 /* We cannot compare overflowed values, except for overflow
1283 infinities. */
1284 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1286 if (strict_overflow_p != NULL)
1287 *strict_overflow_p = true;
1288 if (is_negative_overflow_infinity (val1))
1289 return is_negative_overflow_infinity (val2) ? 0 : -1;
1290 else if (is_negative_overflow_infinity (val2))
1291 return 1;
1292 else if (is_positive_overflow_infinity (val1))
1293 return is_positive_overflow_infinity (val2) ? 0 : 1;
1294 else if (is_positive_overflow_infinity (val2))
1295 return -1;
1296 return -2;
1299 return tree_int_cst_compare (val1, val2);
1301 else
1303 tree t;
1305 /* First see if VAL1 and VAL2 are not the same. */
1306 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1307 return 0;
1309 /* If VAL1 is a lower address than VAL2, return -1. */
1310 if (operand_less_p (val1, val2) == 1)
1311 return -1;
1313 /* If VAL1 is a higher address than VAL2, return +1. */
1314 if (operand_less_p (val2, val1) == 1)
1315 return 1;
1317 /* If VAL1 is different than VAL2, return +2.
1318 For integer constants we either have already returned -1 or 1
1319 or they are equivalent. We still might succeed in proving
1320 something about non-trivial operands. */
1321 if (TREE_CODE (val1) != INTEGER_CST
1322 || TREE_CODE (val2) != INTEGER_CST)
1324 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1325 if (t && integer_onep (t))
1326 return 2;
1329 return -2;
1333 /* Compare values like compare_values_warnv, but treat comparisons of
1334 nonconstants which rely on undefined overflow as incomparable. */
1336 static int
1337 compare_values (tree val1, tree val2)
1339 bool sop;
1340 int ret;
1342 sop = false;
1343 ret = compare_values_warnv (val1, val2, &sop);
1344 if (sop
1345 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1346 ret = -2;
1347 return ret;
1351 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1352 0 if VAL is not inside [MIN, MAX],
1353 -2 if we cannot tell either way.
1355 Benchmark compile/20001226-1.c compilation time after changing this
1356 function. */
1358 static inline int
1359 value_inside_range (tree val, tree min, tree max)
1361 int cmp1, cmp2;
1363 cmp1 = operand_less_p (val, min);
1364 if (cmp1 == -2)
1365 return -2;
1366 if (cmp1 == 1)
1367 return 0;
1369 cmp2 = operand_less_p (max, val);
1370 if (cmp2 == -2)
1371 return -2;
1373 return !cmp2;
1377 /* Return true if value ranges VR0 and VR1 have a non-empty
1378 intersection.
1380 Benchmark compile/20001226-1.c compilation time after changing this
1381 function.
1384 static inline bool
1385 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1387 /* The value ranges do not intersect if the maximum of the first range is
1388 less than the minimum of the second range or vice versa.
1389 When those relations are unknown, we can't do any better. */
1390 if (operand_less_p (vr0->max, vr1->min) != 0)
1391 return false;
1392 if (operand_less_p (vr1->max, vr0->min) != 0)
1393 return false;
1394 return true;
1398 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1399 include the value zero, -2 if we cannot tell. */
1401 static inline int
1402 range_includes_zero_p (tree min, tree max)
1404 tree zero = build_int_cst (TREE_TYPE (min), 0);
1405 return value_inside_range (zero, min, max);
1408 /* Return true if *VR is know to only contain nonnegative values. */
1410 static inline bool
1411 value_range_nonnegative_p (value_range *vr)
1413 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1414 which would return a useful value should be encoded as a
1415 VR_RANGE. */
1416 if (vr->type == VR_RANGE)
1418 int result = compare_values (vr->min, integer_zero_node);
1419 return (result == 0 || result == 1);
1422 return false;
1425 /* If *VR has a value rante that is a single constant value return that,
1426 otherwise return NULL_TREE. */
1428 static tree
1429 value_range_constant_singleton (value_range *vr)
1431 if (vr->type == VR_RANGE
1432 && vrp_operand_equal_p (vr->min, vr->max)
1433 && is_gimple_min_invariant (vr->min))
1434 return vr->min;
1436 return NULL_TREE;
1439 /* If OP has a value range with a single constant value return that,
1440 otherwise return NULL_TREE. This returns OP itself if OP is a
1441 constant. */
1443 static tree
1444 op_with_constant_singleton_value_range (tree op)
1446 if (is_gimple_min_invariant (op))
1447 return op;
1449 if (TREE_CODE (op) != SSA_NAME)
1450 return NULL_TREE;
1452 return value_range_constant_singleton (get_value_range (op));
1455 /* Return true if op is in a boolean [0, 1] value-range. */
1457 static bool
1458 op_with_boolean_value_range_p (tree op)
1460 value_range *vr;
1462 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1463 return true;
1465 if (integer_zerop (op)
1466 || integer_onep (op))
1467 return true;
1469 if (TREE_CODE (op) != SSA_NAME)
1470 return false;
1472 vr = get_value_range (op);
1473 return (vr->type == VR_RANGE
1474 && integer_zerop (vr->min)
1475 && integer_onep (vr->max));
1478 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1479 true and store it in *VR_P. */
1481 static void
1482 extract_range_for_var_from_comparison_expr (tree var, enum tree_code cond_code,
1483 tree op, tree limit,
1484 value_range *vr_p)
1486 tree min, max, type;
1487 value_range *limit_vr;
1488 limit = avoid_overflow_infinity (limit);
1489 type = TREE_TYPE (var);
1490 gcc_assert (limit != var);
1492 /* For pointer arithmetic, we only keep track of pointer equality
1493 and inequality. */
1494 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1496 set_value_range_to_varying (vr_p);
1497 return;
1500 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1501 try to use LIMIT's range to avoid creating symbolic ranges
1502 unnecessarily. */
1503 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1505 /* LIMIT's range is only interesting if it has any useful information. */
1506 if (! limit_vr
1507 || limit_vr->type == VR_UNDEFINED
1508 || limit_vr->type == VR_VARYING
1509 || (symbolic_range_p (limit_vr)
1510 && ! (limit_vr->type == VR_RANGE
1511 && (limit_vr->min == limit_vr->max
1512 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
1513 limit_vr = NULL;
1515 /* Initially, the new range has the same set of equivalences of
1516 VAR's range. This will be revised before returning the final
1517 value. Since assertions may be chained via mutually exclusive
1518 predicates, we will need to trim the set of equivalences before
1519 we are done. */
1520 gcc_assert (vr_p->equiv == NULL);
1521 add_equivalence (&vr_p->equiv, var);
1523 /* Extract a new range based on the asserted comparison for VAR and
1524 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1525 will only use it for equality comparisons (EQ_EXPR). For any
1526 other kind of assertion, we cannot derive a range from LIMIT's
1527 anti-range that can be used to describe the new range. For
1528 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1529 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1530 no single range for x_2 that could describe LE_EXPR, so we might
1531 as well build the range [b_4, +INF] for it.
1532 One special case we handle is extracting a range from a
1533 range test encoded as (unsigned)var + CST <= limit. */
1534 if (TREE_CODE (op) == NOP_EXPR
1535 || TREE_CODE (op) == PLUS_EXPR)
1537 if (TREE_CODE (op) == PLUS_EXPR)
1539 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
1540 TREE_OPERAND (op, 1));
1541 max = int_const_binop (PLUS_EXPR, limit, min);
1542 op = TREE_OPERAND (op, 0);
1544 else
1546 min = build_int_cst (TREE_TYPE (var), 0);
1547 max = limit;
1550 /* Make sure to not set TREE_OVERFLOW on the final type
1551 conversion. We are willingly interpreting large positive
1552 unsigned values as negative signed values here. */
1553 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1554 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1556 /* We can transform a max, min range to an anti-range or
1557 vice-versa. Use set_and_canonicalize_value_range which does
1558 this for us. */
1559 if (cond_code == LE_EXPR)
1560 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1561 min, max, vr_p->equiv);
1562 else if (cond_code == GT_EXPR)
1563 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1564 min, max, vr_p->equiv);
1565 else
1566 gcc_unreachable ();
1568 else if (cond_code == EQ_EXPR)
1570 enum value_range_type range_type;
1572 if (limit_vr)
1574 range_type = limit_vr->type;
1575 min = limit_vr->min;
1576 max = limit_vr->max;
1578 else
1580 range_type = VR_RANGE;
1581 min = limit;
1582 max = limit;
1585 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1587 /* When asserting the equality VAR == LIMIT and LIMIT is another
1588 SSA name, the new range will also inherit the equivalence set
1589 from LIMIT. */
1590 if (TREE_CODE (limit) == SSA_NAME)
1591 add_equivalence (&vr_p->equiv, limit);
1593 else if (cond_code == NE_EXPR)
1595 /* As described above, when LIMIT's range is an anti-range and
1596 this assertion is an inequality (NE_EXPR), then we cannot
1597 derive anything from the anti-range. For instance, if
1598 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1599 not imply that VAR's range is [0, 0]. So, in the case of
1600 anti-ranges, we just assert the inequality using LIMIT and
1601 not its anti-range.
1603 If LIMIT_VR is a range, we can only use it to build a new
1604 anti-range if LIMIT_VR is a single-valued range. For
1605 instance, if LIMIT_VR is [0, 1], the predicate
1606 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1607 Rather, it means that for value 0 VAR should be ~[0, 0]
1608 and for value 1, VAR should be ~[1, 1]. We cannot
1609 represent these ranges.
1611 The only situation in which we can build a valid
1612 anti-range is when LIMIT_VR is a single-valued range
1613 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1614 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1615 if (limit_vr
1616 && limit_vr->type == VR_RANGE
1617 && compare_values (limit_vr->min, limit_vr->max) == 0)
1619 min = limit_vr->min;
1620 max = limit_vr->max;
1622 else
1624 /* In any other case, we cannot use LIMIT's range to build a
1625 valid anti-range. */
1626 min = max = limit;
1629 /* If MIN and MAX cover the whole range for their type, then
1630 just use the original LIMIT. */
1631 if (INTEGRAL_TYPE_P (type)
1632 && vrp_val_is_min (min)
1633 && vrp_val_is_max (max))
1634 min = max = limit;
1636 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1637 min, max, vr_p->equiv);
1639 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1641 min = TYPE_MIN_VALUE (type);
1643 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1644 max = limit;
1645 else
1647 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1648 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1649 LT_EXPR. */
1650 max = limit_vr->max;
1653 /* If the maximum value forces us to be out of bounds, simply punt.
1654 It would be pointless to try and do anything more since this
1655 all should be optimized away above us. */
1656 if ((cond_code == LT_EXPR
1657 && compare_values (max, min) == 0)
1658 || is_overflow_infinity (max))
1659 set_value_range_to_varying (vr_p);
1660 else
1662 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1663 if (cond_code == LT_EXPR)
1665 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1666 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1667 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1668 build_int_cst (TREE_TYPE (max), -1));
1669 else
1670 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1671 build_int_cst (TREE_TYPE (max), 1));
1672 if (EXPR_P (max))
1673 TREE_NO_WARNING (max) = 1;
1676 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1679 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1681 max = TYPE_MAX_VALUE (type);
1683 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1684 min = limit;
1685 else
1687 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1688 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1689 GT_EXPR. */
1690 min = limit_vr->min;
1693 /* If the minimum value forces us to be out of bounds, simply punt.
1694 It would be pointless to try and do anything more since this
1695 all should be optimized away above us. */
1696 if ((cond_code == GT_EXPR
1697 && compare_values (min, max) == 0)
1698 || is_overflow_infinity (min))
1699 set_value_range_to_varying (vr_p);
1700 else
1702 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1703 if (cond_code == GT_EXPR)
1705 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1706 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1707 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1708 build_int_cst (TREE_TYPE (min), -1));
1709 else
1710 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1711 build_int_cst (TREE_TYPE (min), 1));
1712 if (EXPR_P (min))
1713 TREE_NO_WARNING (min) = 1;
1716 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1719 else
1720 gcc_unreachable ();
1722 /* Finally intersect the new range with what we already know about var. */
1723 vrp_intersect_ranges (vr_p, get_value_range (var));
1726 /* Extract value range information from an ASSERT_EXPR EXPR and store
1727 it in *VR_P. */
1729 static void
1730 extract_range_from_assert (value_range *vr_p, tree expr)
1732 tree var = ASSERT_EXPR_VAR (expr);
1733 tree cond = ASSERT_EXPR_COND (expr);
1734 tree limit, op;
1735 enum tree_code cond_code;
1736 gcc_assert (COMPARISON_CLASS_P (cond));
1738 /* Find VAR in the ASSERT_EXPR conditional. */
1739 if (var == TREE_OPERAND (cond, 0)
1740 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1741 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1743 /* If the predicate is of the form VAR COMP LIMIT, then we just
1744 take LIMIT from the RHS and use the same comparison code. */
1745 cond_code = TREE_CODE (cond);
1746 limit = TREE_OPERAND (cond, 1);
1747 op = TREE_OPERAND (cond, 0);
1749 else
1751 /* If the predicate is of the form LIMIT COMP VAR, then we need
1752 to flip around the comparison code to create the proper range
1753 for VAR. */
1754 cond_code = swap_tree_comparison (TREE_CODE (cond));
1755 limit = TREE_OPERAND (cond, 0);
1756 op = TREE_OPERAND (cond, 1);
1758 extract_range_for_var_from_comparison_expr (var, cond_code, op,
1759 limit, vr_p);
1762 /* Extract range information from SSA name VAR and store it in VR. If
1763 VAR has an interesting range, use it. Otherwise, create the
1764 range [VAR, VAR] and return it. This is useful in situations where
1765 we may have conditionals testing values of VARYING names. For
1766 instance,
1768 x_3 = y_5;
1769 if (x_3 > y_5)
1772 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1773 always false. */
1775 static void
1776 extract_range_from_ssa_name (value_range *vr, tree var)
1778 value_range *var_vr = get_value_range (var);
1780 if (var_vr->type != VR_VARYING)
1781 copy_value_range (vr, var_vr);
1782 else
1783 set_value_range (vr, VR_RANGE, var, var, NULL);
1785 add_equivalence (&vr->equiv, var);
1789 /* Wrapper around int_const_binop. If the operation overflows and we
1790 are not using wrapping arithmetic, then adjust the result to be
1791 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1792 NULL_TREE if we need to use an overflow infinity representation but
1793 the type does not support it. */
1795 static tree
1796 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1798 tree res;
1800 res = int_const_binop (code, val1, val2);
1802 /* If we are using unsigned arithmetic, operate symbolically
1803 on -INF and +INF as int_const_binop only handles signed overflow. */
1804 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1806 int checkz = compare_values (res, val1);
1807 bool overflow = false;
1809 /* Ensure that res = val1 [+*] val2 >= val1
1810 or that res = val1 - val2 <= val1. */
1811 if ((code == PLUS_EXPR
1812 && !(checkz == 1 || checkz == 0))
1813 || (code == MINUS_EXPR
1814 && !(checkz == 0 || checkz == -1)))
1816 overflow = true;
1818 /* Checking for multiplication overflow is done by dividing the
1819 output of the multiplication by the first input of the
1820 multiplication. If the result of that division operation is
1821 not equal to the second input of the multiplication, then the
1822 multiplication overflowed. */
1823 else if (code == MULT_EXPR && !integer_zerop (val1))
1825 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1826 res,
1827 val1);
1828 int check = compare_values (tmp, val2);
1830 if (check != 0)
1831 overflow = true;
1834 if (overflow)
1836 res = copy_node (res);
1837 TREE_OVERFLOW (res) = 1;
1841 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1842 /* If the singed operation wraps then int_const_binop has done
1843 everything we want. */
1845 /* Signed division of -1/0 overflows and by the time it gets here
1846 returns NULL_TREE. */
1847 else if (!res)
1848 return NULL_TREE;
1849 else if ((TREE_OVERFLOW (res)
1850 && !TREE_OVERFLOW (val1)
1851 && !TREE_OVERFLOW (val2))
1852 || is_overflow_infinity (val1)
1853 || is_overflow_infinity (val2))
1855 /* If the operation overflowed but neither VAL1 nor VAL2 are
1856 overflown, return -INF or +INF depending on the operation
1857 and the combination of signs of the operands. */
1858 int sgn1 = tree_int_cst_sgn (val1);
1859 int sgn2 = tree_int_cst_sgn (val2);
1861 if (needs_overflow_infinity (TREE_TYPE (res))
1862 && !supports_overflow_infinity (TREE_TYPE (res)))
1863 return NULL_TREE;
1865 /* We have to punt on adding infinities of different signs,
1866 since we can't tell what the sign of the result should be.
1867 Likewise for subtracting infinities of the same sign. */
1868 if (((code == PLUS_EXPR && sgn1 != sgn2)
1869 || (code == MINUS_EXPR && sgn1 == sgn2))
1870 && is_overflow_infinity (val1)
1871 && is_overflow_infinity (val2))
1872 return NULL_TREE;
1874 /* Don't try to handle division or shifting of infinities. */
1875 if ((code == TRUNC_DIV_EXPR
1876 || code == FLOOR_DIV_EXPR
1877 || code == CEIL_DIV_EXPR
1878 || code == EXACT_DIV_EXPR
1879 || code == ROUND_DIV_EXPR
1880 || code == RSHIFT_EXPR)
1881 && (is_overflow_infinity (val1)
1882 || is_overflow_infinity (val2)))
1883 return NULL_TREE;
1885 /* Notice that we only need to handle the restricted set of
1886 operations handled by extract_range_from_binary_expr.
1887 Among them, only multiplication, addition and subtraction
1888 can yield overflow without overflown operands because we
1889 are working with integral types only... except in the
1890 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1891 for division too. */
1893 /* For multiplication, the sign of the overflow is given
1894 by the comparison of the signs of the operands. */
1895 if ((code == MULT_EXPR && sgn1 == sgn2)
1896 /* For addition, the operands must be of the same sign
1897 to yield an overflow. Its sign is therefore that
1898 of one of the operands, for example the first. For
1899 infinite operands X + -INF is negative, not positive. */
1900 || (code == PLUS_EXPR
1901 && (sgn1 >= 0
1902 ? !is_negative_overflow_infinity (val2)
1903 : is_positive_overflow_infinity (val2)))
1904 /* For subtraction, non-infinite operands must be of
1905 different signs to yield an overflow. Its sign is
1906 therefore that of the first operand or the opposite of
1907 that of the second operand. A first operand of 0 counts
1908 as positive here, for the corner case 0 - (-INF), which
1909 overflows, but must yield +INF. For infinite operands 0
1910 - INF is negative, not positive. */
1911 || (code == MINUS_EXPR
1912 && (sgn1 >= 0
1913 ? !is_positive_overflow_infinity (val2)
1914 : is_negative_overflow_infinity (val2)))
1915 /* We only get in here with positive shift count, so the
1916 overflow direction is the same as the sign of val1.
1917 Actually rshift does not overflow at all, but we only
1918 handle the case of shifting overflowed -INF and +INF. */
1919 || (code == RSHIFT_EXPR
1920 && sgn1 >= 0)
1921 /* For division, the only case is -INF / -1 = +INF. */
1922 || code == TRUNC_DIV_EXPR
1923 || code == FLOOR_DIV_EXPR
1924 || code == CEIL_DIV_EXPR
1925 || code == EXACT_DIV_EXPR
1926 || code == ROUND_DIV_EXPR)
1927 return (needs_overflow_infinity (TREE_TYPE (res))
1928 ? positive_overflow_infinity (TREE_TYPE (res))
1929 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1930 else
1931 return (needs_overflow_infinity (TREE_TYPE (res))
1932 ? negative_overflow_infinity (TREE_TYPE (res))
1933 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1936 return res;
1940 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1941 bitmask if some bit is unset, it means for all numbers in the range
1942 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1943 bitmask if some bit is set, it means for all numbers in the range
1944 the bit is 1, otherwise it might be 0 or 1. */
1946 static bool
1947 zero_nonzero_bits_from_vr (const tree expr_type,
1948 value_range *vr,
1949 wide_int *may_be_nonzero,
1950 wide_int *must_be_nonzero)
1952 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1953 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1954 if (!range_int_cst_p (vr)
1955 || is_overflow_infinity (vr->min)
1956 || is_overflow_infinity (vr->max))
1957 return false;
1959 if (range_int_cst_singleton_p (vr))
1961 *may_be_nonzero = vr->min;
1962 *must_be_nonzero = *may_be_nonzero;
1964 else if (tree_int_cst_sgn (vr->min) >= 0
1965 || tree_int_cst_sgn (vr->max) < 0)
1967 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1968 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1969 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1970 if (xor_mask != 0)
1972 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1973 may_be_nonzero->get_precision ());
1974 *may_be_nonzero = *may_be_nonzero | mask;
1975 *must_be_nonzero = must_be_nonzero->and_not (mask);
1979 return true;
1982 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1983 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1984 false otherwise. If *AR can be represented with a single range
1985 *VR1 will be VR_UNDEFINED. */
1987 static bool
1988 ranges_from_anti_range (value_range *ar,
1989 value_range *vr0, value_range *vr1)
1991 tree type = TREE_TYPE (ar->min);
1993 vr0->type = VR_UNDEFINED;
1994 vr1->type = VR_UNDEFINED;
1996 if (ar->type != VR_ANTI_RANGE
1997 || TREE_CODE (ar->min) != INTEGER_CST
1998 || TREE_CODE (ar->max) != INTEGER_CST
1999 || !vrp_val_min (type)
2000 || !vrp_val_max (type))
2001 return false;
2003 if (!vrp_val_is_min (ar->min))
2005 vr0->type = VR_RANGE;
2006 vr0->min = vrp_val_min (type);
2007 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2009 if (!vrp_val_is_max (ar->max))
2011 vr1->type = VR_RANGE;
2012 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2013 vr1->max = vrp_val_max (type);
2015 if (vr0->type == VR_UNDEFINED)
2017 *vr0 = *vr1;
2018 vr1->type = VR_UNDEFINED;
2021 return vr0->type != VR_UNDEFINED;
2024 /* Helper to extract a value-range *VR for a multiplicative operation
2025 *VR0 CODE *VR1. */
2027 static void
2028 extract_range_from_multiplicative_op_1 (value_range *vr,
2029 enum tree_code code,
2030 value_range *vr0, value_range *vr1)
2032 enum value_range_type type;
2033 tree val[4];
2034 size_t i;
2035 tree min, max;
2036 bool sop;
2037 int cmp;
2039 /* Multiplications, divisions and shifts are a bit tricky to handle,
2040 depending on the mix of signs we have in the two ranges, we
2041 need to operate on different values to get the minimum and
2042 maximum values for the new range. One approach is to figure
2043 out all the variations of range combinations and do the
2044 operations.
2046 However, this involves several calls to compare_values and it
2047 is pretty convoluted. It's simpler to do the 4 operations
2048 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2049 MAX1) and then figure the smallest and largest values to form
2050 the new range. */
2051 gcc_assert (code == MULT_EXPR
2052 || code == TRUNC_DIV_EXPR
2053 || code == FLOOR_DIV_EXPR
2054 || code == CEIL_DIV_EXPR
2055 || code == EXACT_DIV_EXPR
2056 || code == ROUND_DIV_EXPR
2057 || code == RSHIFT_EXPR
2058 || code == LSHIFT_EXPR);
2059 gcc_assert ((vr0->type == VR_RANGE
2060 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2061 && vr0->type == vr1->type);
2063 type = vr0->type;
2065 /* Compute the 4 cross operations. */
2066 sop = false;
2067 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2068 if (val[0] == NULL_TREE)
2069 sop = true;
2071 if (vr1->max == vr1->min)
2072 val[1] = NULL_TREE;
2073 else
2075 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2076 if (val[1] == NULL_TREE)
2077 sop = true;
2080 if (vr0->max == vr0->min)
2081 val[2] = NULL_TREE;
2082 else
2084 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2085 if (val[2] == NULL_TREE)
2086 sop = true;
2089 if (vr0->min == vr0->max || vr1->min == vr1->max)
2090 val[3] = NULL_TREE;
2091 else
2093 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2094 if (val[3] == NULL_TREE)
2095 sop = true;
2098 if (sop)
2100 set_value_range_to_varying (vr);
2101 return;
2104 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2105 of VAL[i]. */
2106 min = val[0];
2107 max = val[0];
2108 for (i = 1; i < 4; i++)
2110 if (!is_gimple_min_invariant (min)
2111 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2112 || !is_gimple_min_invariant (max)
2113 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2114 break;
2116 if (val[i])
2118 if (!is_gimple_min_invariant (val[i])
2119 || (TREE_OVERFLOW (val[i])
2120 && !is_overflow_infinity (val[i])))
2122 /* If we found an overflowed value, set MIN and MAX
2123 to it so that we set the resulting range to
2124 VARYING. */
2125 min = max = val[i];
2126 break;
2129 if (compare_values (val[i], min) == -1)
2130 min = val[i];
2132 if (compare_values (val[i], max) == 1)
2133 max = val[i];
2137 /* If either MIN or MAX overflowed, then set the resulting range to
2138 VARYING. But we do accept an overflow infinity
2139 representation. */
2140 if (min == NULL_TREE
2141 || !is_gimple_min_invariant (min)
2142 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2143 || max == NULL_TREE
2144 || !is_gimple_min_invariant (max)
2145 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2147 set_value_range_to_varying (vr);
2148 return;
2151 /* We punt if:
2152 1) [-INF, +INF]
2153 2) [-INF, +-INF(OVF)]
2154 3) [+-INF(OVF), +INF]
2155 4) [+-INF(OVF), +-INF(OVF)]
2156 We learn nothing when we have INF and INF(OVF) on both sides.
2157 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2158 overflow. */
2159 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2160 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2162 set_value_range_to_varying (vr);
2163 return;
2166 cmp = compare_values (min, max);
2167 if (cmp == -2 || cmp == 1)
2169 /* If the new range has its limits swapped around (MIN > MAX),
2170 then the operation caused one of them to wrap around, mark
2171 the new range VARYING. */
2172 set_value_range_to_varying (vr);
2174 else
2175 set_value_range (vr, type, min, max, NULL);
2178 /* Extract range information from a binary operation CODE based on
2179 the ranges of each of its operands *VR0 and *VR1 with resulting
2180 type EXPR_TYPE. The resulting range is stored in *VR. */
2182 static void
2183 extract_range_from_binary_expr_1 (value_range *vr,
2184 enum tree_code code, tree expr_type,
2185 value_range *vr0_, value_range *vr1_)
2187 value_range vr0 = *vr0_, vr1 = *vr1_;
2188 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2189 enum value_range_type type;
2190 tree min = NULL_TREE, max = NULL_TREE;
2191 int cmp;
2193 if (!INTEGRAL_TYPE_P (expr_type)
2194 && !POINTER_TYPE_P (expr_type))
2196 set_value_range_to_varying (vr);
2197 return;
2200 /* Not all binary expressions can be applied to ranges in a
2201 meaningful way. Handle only arithmetic operations. */
2202 if (code != PLUS_EXPR
2203 && code != MINUS_EXPR
2204 && code != POINTER_PLUS_EXPR
2205 && code != MULT_EXPR
2206 && code != TRUNC_DIV_EXPR
2207 && code != FLOOR_DIV_EXPR
2208 && code != CEIL_DIV_EXPR
2209 && code != EXACT_DIV_EXPR
2210 && code != ROUND_DIV_EXPR
2211 && code != TRUNC_MOD_EXPR
2212 && code != RSHIFT_EXPR
2213 && code != LSHIFT_EXPR
2214 && code != MIN_EXPR
2215 && code != MAX_EXPR
2216 && code != BIT_AND_EXPR
2217 && code != BIT_IOR_EXPR
2218 && code != BIT_XOR_EXPR)
2220 set_value_range_to_varying (vr);
2221 return;
2224 /* If both ranges are UNDEFINED, so is the result. */
2225 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2227 set_value_range_to_undefined (vr);
2228 return;
2230 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2231 code. At some point we may want to special-case operations that
2232 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2233 operand. */
2234 else if (vr0.type == VR_UNDEFINED)
2235 set_value_range_to_varying (&vr0);
2236 else if (vr1.type == VR_UNDEFINED)
2237 set_value_range_to_varying (&vr1);
2239 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2240 and express ~[] op X as ([]' op X) U ([]'' op X). */
2241 if (vr0.type == VR_ANTI_RANGE
2242 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2244 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2245 if (vrtem1.type != VR_UNDEFINED)
2247 value_range vrres = VR_INITIALIZER;
2248 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2249 &vrtem1, vr1_);
2250 vrp_meet (vr, &vrres);
2252 return;
2254 /* Likewise for X op ~[]. */
2255 if (vr1.type == VR_ANTI_RANGE
2256 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2258 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2259 if (vrtem1.type != VR_UNDEFINED)
2261 value_range vrres = VR_INITIALIZER;
2262 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2263 vr0_, &vrtem1);
2264 vrp_meet (vr, &vrres);
2266 return;
2269 /* The type of the resulting value range defaults to VR0.TYPE. */
2270 type = vr0.type;
2272 /* Refuse to operate on VARYING ranges, ranges of different kinds
2273 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2274 because we may be able to derive a useful range even if one of
2275 the operands is VR_VARYING or symbolic range. Similarly for
2276 divisions, MIN/MAX and PLUS/MINUS.
2278 TODO, we may be able to derive anti-ranges in some cases. */
2279 if (code != BIT_AND_EXPR
2280 && code != BIT_IOR_EXPR
2281 && code != TRUNC_DIV_EXPR
2282 && code != FLOOR_DIV_EXPR
2283 && code != CEIL_DIV_EXPR
2284 && code != EXACT_DIV_EXPR
2285 && code != ROUND_DIV_EXPR
2286 && code != TRUNC_MOD_EXPR
2287 && code != MIN_EXPR
2288 && code != MAX_EXPR
2289 && code != PLUS_EXPR
2290 && code != MINUS_EXPR
2291 && code != RSHIFT_EXPR
2292 && (vr0.type == VR_VARYING
2293 || vr1.type == VR_VARYING
2294 || vr0.type != vr1.type
2295 || symbolic_range_p (&vr0)
2296 || symbolic_range_p (&vr1)))
2298 set_value_range_to_varying (vr);
2299 return;
2302 /* Now evaluate the expression to determine the new range. */
2303 if (POINTER_TYPE_P (expr_type))
2305 if (code == MIN_EXPR || code == MAX_EXPR)
2307 /* For MIN/MAX expressions with pointers, we only care about
2308 nullness, if both are non null, then the result is nonnull.
2309 If both are null, then the result is null. Otherwise they
2310 are varying. */
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 if (code == POINTER_PLUS_EXPR)
2320 /* For pointer types, we are really only interested in asserting
2321 whether the expression evaluates to non-NULL. */
2322 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2323 set_value_range_to_nonnull (vr, expr_type);
2324 else if (range_is_null (&vr0) && range_is_null (&vr1))
2325 set_value_range_to_null (vr, expr_type);
2326 else
2327 set_value_range_to_varying (vr);
2329 else if (code == BIT_AND_EXPR)
2331 /* For pointer types, we are really only interested in asserting
2332 whether the expression evaluates to non-NULL. */
2333 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2334 set_value_range_to_nonnull (vr, expr_type);
2335 else if (range_is_null (&vr0) || range_is_null (&vr1))
2336 set_value_range_to_null (vr, expr_type);
2337 else
2338 set_value_range_to_varying (vr);
2340 else
2341 set_value_range_to_varying (vr);
2343 return;
2346 /* For integer ranges, apply the operation to each end of the
2347 range and see what we end up with. */
2348 if (code == PLUS_EXPR || code == MINUS_EXPR)
2350 const bool minus_p = (code == MINUS_EXPR);
2351 tree min_op0 = vr0.min;
2352 tree min_op1 = minus_p ? vr1.max : vr1.min;
2353 tree max_op0 = vr0.max;
2354 tree max_op1 = minus_p ? vr1.min : vr1.max;
2355 tree sym_min_op0 = NULL_TREE;
2356 tree sym_min_op1 = NULL_TREE;
2357 tree sym_max_op0 = NULL_TREE;
2358 tree sym_max_op1 = NULL_TREE;
2359 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2361 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2362 single-symbolic ranges, try to compute the precise resulting range,
2363 but only if we know that this resulting range will also be constant
2364 or single-symbolic. */
2365 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2366 && (TREE_CODE (min_op0) == INTEGER_CST
2367 || (sym_min_op0
2368 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2369 && (TREE_CODE (min_op1) == INTEGER_CST
2370 || (sym_min_op1
2371 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2372 && (!(sym_min_op0 && sym_min_op1)
2373 || (sym_min_op0 == sym_min_op1
2374 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2375 && (TREE_CODE (max_op0) == INTEGER_CST
2376 || (sym_max_op0
2377 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2378 && (TREE_CODE (max_op1) == INTEGER_CST
2379 || (sym_max_op1
2380 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2381 && (!(sym_max_op0 && sym_max_op1)
2382 || (sym_max_op0 == sym_max_op1
2383 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2385 const signop sgn = TYPE_SIGN (expr_type);
2386 const unsigned int prec = TYPE_PRECISION (expr_type);
2387 wide_int type_min, type_max, wmin, wmax;
2388 int min_ovf = 0;
2389 int max_ovf = 0;
2391 /* Get the lower and upper bounds of the type. */
2392 if (TYPE_OVERFLOW_WRAPS (expr_type))
2394 type_min = wi::min_value (prec, sgn);
2395 type_max = wi::max_value (prec, sgn);
2397 else
2399 type_min = vrp_val_min (expr_type);
2400 type_max = vrp_val_max (expr_type);
2403 /* Combine the lower bounds, if any. */
2404 if (min_op0 && min_op1)
2406 if (minus_p)
2408 wmin = wi::sub (min_op0, min_op1);
2410 /* Check for overflow. */
2411 if (wi::cmp (0, min_op1, sgn)
2412 != wi::cmp (wmin, min_op0, sgn))
2413 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2415 else
2417 wmin = wi::add (min_op0, min_op1);
2419 /* Check for overflow. */
2420 if (wi::cmp (min_op1, 0, sgn)
2421 != wi::cmp (wmin, min_op0, sgn))
2422 min_ovf = wi::cmp (min_op0, wmin, sgn);
2425 else if (min_op0)
2426 wmin = min_op0;
2427 else if (min_op1)
2428 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2429 else
2430 wmin = wi::shwi (0, prec);
2432 /* Combine the upper bounds, if any. */
2433 if (max_op0 && max_op1)
2435 if (minus_p)
2437 wmax = wi::sub (max_op0, max_op1);
2439 /* Check for overflow. */
2440 if (wi::cmp (0, max_op1, sgn)
2441 != wi::cmp (wmax, max_op0, sgn))
2442 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2444 else
2446 wmax = wi::add (max_op0, max_op1);
2448 if (wi::cmp (max_op1, 0, sgn)
2449 != wi::cmp (wmax, max_op0, sgn))
2450 max_ovf = wi::cmp (max_op0, wmax, sgn);
2453 else if (max_op0)
2454 wmax = max_op0;
2455 else if (max_op1)
2456 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2457 else
2458 wmax = wi::shwi (0, prec);
2460 /* Check for type overflow. */
2461 if (min_ovf == 0)
2463 if (wi::cmp (wmin, type_min, sgn) == -1)
2464 min_ovf = -1;
2465 else if (wi::cmp (wmin, type_max, sgn) == 1)
2466 min_ovf = 1;
2468 if (max_ovf == 0)
2470 if (wi::cmp (wmax, type_min, sgn) == -1)
2471 max_ovf = -1;
2472 else if (wi::cmp (wmax, type_max, sgn) == 1)
2473 max_ovf = 1;
2476 /* If we have overflow for the constant part and the resulting
2477 range will be symbolic, drop to VR_VARYING. */
2478 if ((min_ovf && sym_min_op0 != sym_min_op1)
2479 || (max_ovf && sym_max_op0 != sym_max_op1))
2481 set_value_range_to_varying (vr);
2482 return;
2485 if (TYPE_OVERFLOW_WRAPS (expr_type))
2487 /* If overflow wraps, truncate the values and adjust the
2488 range kind and bounds appropriately. */
2489 wide_int tmin = wide_int::from (wmin, prec, sgn);
2490 wide_int tmax = wide_int::from (wmax, prec, sgn);
2491 if (min_ovf == max_ovf)
2493 /* No overflow or both overflow or underflow. The
2494 range kind stays VR_RANGE. */
2495 min = wide_int_to_tree (expr_type, tmin);
2496 max = wide_int_to_tree (expr_type, tmax);
2498 else if ((min_ovf == -1 && max_ovf == 0)
2499 || (max_ovf == 1 && min_ovf == 0))
2501 /* Min underflow or max overflow. The range kind
2502 changes to VR_ANTI_RANGE. */
2503 bool covers = false;
2504 wide_int tem = tmin;
2505 type = VR_ANTI_RANGE;
2506 tmin = tmax + 1;
2507 if (wi::cmp (tmin, tmax, sgn) < 0)
2508 covers = true;
2509 tmax = tem - 1;
2510 if (wi::cmp (tmax, tem, sgn) > 0)
2511 covers = true;
2512 /* If the anti-range would cover nothing, drop to varying.
2513 Likewise if the anti-range bounds are outside of the
2514 types values. */
2515 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2517 set_value_range_to_varying (vr);
2518 return;
2520 min = wide_int_to_tree (expr_type, tmin);
2521 max = wide_int_to_tree (expr_type, tmax);
2523 else
2525 /* Other underflow and/or overflow, drop to VR_VARYING. */
2526 set_value_range_to_varying (vr);
2527 return;
2530 else
2532 /* If overflow does not wrap, saturate to the types min/max
2533 value. */
2534 if (min_ovf == -1)
2536 if (needs_overflow_infinity (expr_type)
2537 && supports_overflow_infinity (expr_type))
2538 min = negative_overflow_infinity (expr_type);
2539 else
2540 min = wide_int_to_tree (expr_type, type_min);
2542 else if (min_ovf == 1)
2544 if (needs_overflow_infinity (expr_type)
2545 && supports_overflow_infinity (expr_type))
2546 min = positive_overflow_infinity (expr_type);
2547 else
2548 min = wide_int_to_tree (expr_type, type_max);
2550 else
2551 min = wide_int_to_tree (expr_type, wmin);
2553 if (max_ovf == -1)
2555 if (needs_overflow_infinity (expr_type)
2556 && supports_overflow_infinity (expr_type))
2557 max = negative_overflow_infinity (expr_type);
2558 else
2559 max = wide_int_to_tree (expr_type, type_min);
2561 else if (max_ovf == 1)
2563 if (needs_overflow_infinity (expr_type)
2564 && supports_overflow_infinity (expr_type))
2565 max = positive_overflow_infinity (expr_type);
2566 else
2567 max = wide_int_to_tree (expr_type, type_max);
2569 else
2570 max = wide_int_to_tree (expr_type, wmax);
2573 if (needs_overflow_infinity (expr_type)
2574 && supports_overflow_infinity (expr_type))
2576 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2577 || (min_op1
2578 && (minus_p
2579 ? is_positive_overflow_infinity (min_op1)
2580 : is_negative_overflow_infinity (min_op1))))
2581 min = negative_overflow_infinity (expr_type);
2582 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2583 || (max_op1
2584 && (minus_p
2585 ? is_negative_overflow_infinity (max_op1)
2586 : is_positive_overflow_infinity (max_op1))))
2587 max = positive_overflow_infinity (expr_type);
2590 /* If the result lower bound is constant, we're done;
2591 otherwise, build the symbolic lower bound. */
2592 if (sym_min_op0 == sym_min_op1)
2594 else if (sym_min_op0)
2595 min = build_symbolic_expr (expr_type, sym_min_op0,
2596 neg_min_op0, min);
2597 else if (sym_min_op1)
2598 min = build_symbolic_expr (expr_type, sym_min_op1,
2599 neg_min_op1 ^ minus_p, min);
2601 /* Likewise for the upper bound. */
2602 if (sym_max_op0 == sym_max_op1)
2604 else if (sym_max_op0)
2605 max = build_symbolic_expr (expr_type, sym_max_op0,
2606 neg_max_op0, max);
2607 else if (sym_max_op1)
2608 max = build_symbolic_expr (expr_type, sym_max_op1,
2609 neg_max_op1 ^ minus_p, max);
2611 else
2613 /* For other cases, for example if we have a PLUS_EXPR with two
2614 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2615 to compute a precise range for such a case.
2616 ??? General even mixed range kind operations can be expressed
2617 by for example transforming ~[3, 5] + [1, 2] to range-only
2618 operations and a union primitive:
2619 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2620 [-INF+1, 4] U [6, +INF(OVF)]
2621 though usually the union is not exactly representable with
2622 a single range or anti-range as the above is
2623 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2624 but one could use a scheme similar to equivalences for this. */
2625 set_value_range_to_varying (vr);
2626 return;
2629 else if (code == MIN_EXPR
2630 || code == MAX_EXPR)
2632 if (vr0.type == VR_RANGE
2633 && !symbolic_range_p (&vr0))
2635 type = VR_RANGE;
2636 if (vr1.type == VR_RANGE
2637 && !symbolic_range_p (&vr1))
2639 /* For operations that make the resulting range directly
2640 proportional to the original ranges, apply the operation to
2641 the same end of each range. */
2642 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2643 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2645 else if (code == MIN_EXPR)
2647 min = vrp_val_min (expr_type);
2648 max = vr0.max;
2650 else if (code == MAX_EXPR)
2652 min = vr0.min;
2653 max = vrp_val_max (expr_type);
2656 else if (vr1.type == VR_RANGE
2657 && !symbolic_range_p (&vr1))
2659 type = VR_RANGE;
2660 if (code == MIN_EXPR)
2662 min = vrp_val_min (expr_type);
2663 max = vr1.max;
2665 else if (code == MAX_EXPR)
2667 min = vr1.min;
2668 max = vrp_val_max (expr_type);
2671 else
2673 set_value_range_to_varying (vr);
2674 return;
2677 else if (code == MULT_EXPR)
2679 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2680 drop to varying. This test requires 2*prec bits if both
2681 operands are signed and 2*prec + 2 bits if either is not. */
2683 signop sign = TYPE_SIGN (expr_type);
2684 unsigned int prec = TYPE_PRECISION (expr_type);
2686 if (range_int_cst_p (&vr0)
2687 && range_int_cst_p (&vr1)
2688 && TYPE_OVERFLOW_WRAPS (expr_type))
2690 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2691 typedef generic_wide_int
2692 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2693 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2694 vrp_int size = sizem1 + 1;
2696 /* Extend the values using the sign of the result to PREC2.
2697 From here on out, everthing is just signed math no matter
2698 what the input types were. */
2699 vrp_int min0 = vrp_int_cst (vr0.min);
2700 vrp_int max0 = vrp_int_cst (vr0.max);
2701 vrp_int min1 = vrp_int_cst (vr1.min);
2702 vrp_int max1 = vrp_int_cst (vr1.max);
2703 /* Canonicalize the intervals. */
2704 if (sign == UNSIGNED)
2706 if (wi::ltu_p (size, min0 + max0))
2708 min0 -= size;
2709 max0 -= size;
2712 if (wi::ltu_p (size, min1 + max1))
2714 min1 -= size;
2715 max1 -= size;
2719 vrp_int prod0 = min0 * min1;
2720 vrp_int prod1 = min0 * max1;
2721 vrp_int prod2 = max0 * min1;
2722 vrp_int prod3 = max0 * max1;
2724 /* Sort the 4 products so that min is in prod0 and max is in
2725 prod3. */
2726 /* min0min1 > max0max1 */
2727 if (prod0 > prod3)
2728 std::swap (prod0, prod3);
2730 /* min0max1 > max0min1 */
2731 if (prod1 > prod2)
2732 std::swap (prod1, prod2);
2734 if (prod0 > prod1)
2735 std::swap (prod0, prod1);
2737 if (prod2 > prod3)
2738 std::swap (prod2, prod3);
2740 /* diff = max - min. */
2741 prod2 = prod3 - prod0;
2742 if (wi::geu_p (prod2, sizem1))
2744 /* the range covers all values. */
2745 set_value_range_to_varying (vr);
2746 return;
2749 /* The following should handle the wrapping and selecting
2750 VR_ANTI_RANGE for us. */
2751 min = wide_int_to_tree (expr_type, prod0);
2752 max = wide_int_to_tree (expr_type, prod3);
2753 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2754 return;
2757 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2758 drop to VR_VARYING. It would take more effort to compute a
2759 precise range for such a case. For example, if we have
2760 op0 == 65536 and op1 == 65536 with their ranges both being
2761 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2762 we cannot claim that the product is in ~[0,0]. Note that we
2763 are guaranteed to have vr0.type == vr1.type at this
2764 point. */
2765 if (vr0.type == VR_ANTI_RANGE
2766 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2768 set_value_range_to_varying (vr);
2769 return;
2772 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2773 return;
2775 else if (code == RSHIFT_EXPR
2776 || code == LSHIFT_EXPR)
2778 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2779 then drop to VR_VARYING. Outside of this range we get undefined
2780 behavior from the shift operation. We cannot even trust
2781 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2782 shifts, and the operation at the tree level may be widened. */
2783 if (range_int_cst_p (&vr1)
2784 && compare_tree_int (vr1.min, 0) >= 0
2785 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2787 if (code == RSHIFT_EXPR)
2789 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2790 useful ranges just from the shift count. E.g.
2791 x >> 63 for signed 64-bit x is always [-1, 0]. */
2792 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2794 vr0.type = type = VR_RANGE;
2795 vr0.min = vrp_val_min (expr_type);
2796 vr0.max = vrp_val_max (expr_type);
2798 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2799 return;
2801 /* We can map lshifts by constants to MULT_EXPR handling. */
2802 else if (code == LSHIFT_EXPR
2803 && range_int_cst_singleton_p (&vr1))
2805 bool saved_flag_wrapv;
2806 value_range vr1p = VR_INITIALIZER;
2807 vr1p.type = VR_RANGE;
2808 vr1p.min = (wide_int_to_tree
2809 (expr_type,
2810 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2811 TYPE_PRECISION (expr_type))));
2812 vr1p.max = vr1p.min;
2813 /* We have to use a wrapping multiply though as signed overflow
2814 on lshifts is implementation defined in C89. */
2815 saved_flag_wrapv = flag_wrapv;
2816 flag_wrapv = 1;
2817 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2818 &vr0, &vr1p);
2819 flag_wrapv = saved_flag_wrapv;
2820 return;
2822 else if (code == LSHIFT_EXPR
2823 && range_int_cst_p (&vr0))
2825 int prec = TYPE_PRECISION (expr_type);
2826 int overflow_pos = prec;
2827 int bound_shift;
2828 wide_int low_bound, high_bound;
2829 bool uns = TYPE_UNSIGNED (expr_type);
2830 bool in_bounds = false;
2832 if (!uns)
2833 overflow_pos -= 1;
2835 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2836 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2837 overflow. However, for that to happen, vr1.max needs to be
2838 zero, which means vr1 is a singleton range of zero, which
2839 means it should be handled by the previous LSHIFT_EXPR
2840 if-clause. */
2841 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2842 wide_int complement = ~(bound - 1);
2844 if (uns)
2846 low_bound = bound;
2847 high_bound = complement;
2848 if (wi::ltu_p (vr0.max, low_bound))
2850 /* [5, 6] << [1, 2] == [10, 24]. */
2851 /* We're shifting out only zeroes, the value increases
2852 monotonically. */
2853 in_bounds = true;
2855 else if (wi::ltu_p (high_bound, vr0.min))
2857 /* [0xffffff00, 0xffffffff] << [1, 2]
2858 == [0xfffffc00, 0xfffffffe]. */
2859 /* We're shifting out only ones, the value decreases
2860 monotonically. */
2861 in_bounds = true;
2864 else
2866 /* [-1, 1] << [1, 2] == [-4, 4]. */
2867 low_bound = complement;
2868 high_bound = bound;
2869 if (wi::lts_p (vr0.max, high_bound)
2870 && wi::lts_p (low_bound, vr0.min))
2872 /* For non-negative numbers, we're shifting out only
2873 zeroes, the value increases monotonically.
2874 For negative numbers, we're shifting out only ones, the
2875 value decreases monotomically. */
2876 in_bounds = true;
2880 if (in_bounds)
2882 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2883 return;
2887 set_value_range_to_varying (vr);
2888 return;
2890 else if (code == TRUNC_DIV_EXPR
2891 || code == FLOOR_DIV_EXPR
2892 || code == CEIL_DIV_EXPR
2893 || code == EXACT_DIV_EXPR
2894 || code == ROUND_DIV_EXPR)
2896 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2898 /* For division, if op1 has VR_RANGE but op0 does not, something
2899 can be deduced just from that range. Say [min, max] / [4, max]
2900 gives [min / 4, max / 4] range. */
2901 if (vr1.type == VR_RANGE
2902 && !symbolic_range_p (&vr1)
2903 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2905 vr0.type = type = VR_RANGE;
2906 vr0.min = vrp_val_min (expr_type);
2907 vr0.max = vrp_val_max (expr_type);
2909 else
2911 set_value_range_to_varying (vr);
2912 return;
2916 /* For divisions, if flag_non_call_exceptions is true, we must
2917 not eliminate a division by zero. */
2918 if (cfun->can_throw_non_call_exceptions
2919 && (vr1.type != VR_RANGE
2920 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2922 set_value_range_to_varying (vr);
2923 return;
2926 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2927 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2928 include 0. */
2929 if (vr0.type == VR_RANGE
2930 && (vr1.type != VR_RANGE
2931 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2933 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2934 int cmp;
2936 min = NULL_TREE;
2937 max = NULL_TREE;
2938 if (TYPE_UNSIGNED (expr_type)
2939 || value_range_nonnegative_p (&vr1))
2941 /* For unsigned division or when divisor is known
2942 to be non-negative, the range has to cover
2943 all numbers from 0 to max for positive max
2944 and all numbers from min to 0 for negative min. */
2945 cmp = compare_values (vr0.max, zero);
2946 if (cmp == -1)
2948 /* When vr0.max < 0, vr1.min != 0 and value
2949 ranges for dividend and divisor are available. */
2950 if (vr1.type == VR_RANGE
2951 && !symbolic_range_p (&vr0)
2952 && !symbolic_range_p (&vr1)
2953 && compare_values (vr1.min, zero) != 0)
2954 max = int_const_binop (code, vr0.max, vr1.min);
2955 else
2956 max = zero;
2958 else if (cmp == 0 || cmp == 1)
2959 max = vr0.max;
2960 else
2961 type = VR_VARYING;
2962 cmp = compare_values (vr0.min, zero);
2963 if (cmp == 1)
2965 /* For unsigned division when value ranges for dividend
2966 and divisor are available. */
2967 if (vr1.type == VR_RANGE
2968 && !symbolic_range_p (&vr0)
2969 && !symbolic_range_p (&vr1)
2970 && compare_values (vr1.max, zero) != 0)
2971 min = int_const_binop (code, vr0.min, vr1.max);
2972 else
2973 min = zero;
2975 else if (cmp == 0 || cmp == -1)
2976 min = vr0.min;
2977 else
2978 type = VR_VARYING;
2980 else
2982 /* Otherwise the range is -max .. max or min .. -min
2983 depending on which bound is bigger in absolute value,
2984 as the division can change the sign. */
2985 abs_extent_range (vr, vr0.min, vr0.max);
2986 return;
2988 if (type == VR_VARYING)
2990 set_value_range_to_varying (vr);
2991 return;
2994 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
2996 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2997 return;
3000 else if (code == TRUNC_MOD_EXPR)
3002 if (range_is_null (&vr1))
3004 set_value_range_to_undefined (vr);
3005 return;
3007 /* ABS (A % B) < ABS (B) and either
3008 0 <= A % B <= A or A <= A % B <= 0. */
3009 type = VR_RANGE;
3010 signop sgn = TYPE_SIGN (expr_type);
3011 unsigned int prec = TYPE_PRECISION (expr_type);
3012 wide_int wmin, wmax, tmp;
3013 wide_int zero = wi::zero (prec);
3014 wide_int one = wi::one (prec);
3015 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3017 wmax = wi::sub (vr1.max, one);
3018 if (sgn == SIGNED)
3020 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3021 wmax = wi::smax (wmax, tmp);
3024 else
3026 wmax = wi::max_value (prec, sgn);
3027 /* X % INT_MIN may be INT_MAX. */
3028 if (sgn == UNSIGNED)
3029 wmax = wmax - one;
3032 if (sgn == UNSIGNED)
3033 wmin = zero;
3034 else
3036 wmin = -wmax;
3037 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3039 tmp = vr0.min;
3040 if (wi::gts_p (tmp, zero))
3041 tmp = zero;
3042 wmin = wi::smax (wmin, tmp);
3046 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3048 tmp = vr0.max;
3049 if (sgn == SIGNED && wi::neg_p (tmp))
3050 tmp = zero;
3051 wmax = wi::min (wmax, tmp, sgn);
3054 min = wide_int_to_tree (expr_type, wmin);
3055 max = wide_int_to_tree (expr_type, wmax);
3057 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3059 bool int_cst_range0, int_cst_range1;
3060 wide_int may_be_nonzero0, may_be_nonzero1;
3061 wide_int must_be_nonzero0, must_be_nonzero1;
3063 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3064 &may_be_nonzero0,
3065 &must_be_nonzero0);
3066 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3067 &may_be_nonzero1,
3068 &must_be_nonzero1);
3070 type = VR_RANGE;
3071 if (code == BIT_AND_EXPR)
3073 min = wide_int_to_tree (expr_type,
3074 must_be_nonzero0 & must_be_nonzero1);
3075 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3076 /* If both input ranges contain only negative values we can
3077 truncate the result range maximum to the minimum of the
3078 input range maxima. */
3079 if (int_cst_range0 && int_cst_range1
3080 && tree_int_cst_sgn (vr0.max) < 0
3081 && tree_int_cst_sgn (vr1.max) < 0)
3083 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3084 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3086 /* If either input range contains only non-negative values
3087 we can truncate the result range maximum to the respective
3088 maximum of the input range. */
3089 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3090 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3091 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3092 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3093 max = wide_int_to_tree (expr_type, wmax);
3094 cmp = compare_values (min, max);
3095 /* PR68217: In case of signed & sign-bit-CST should
3096 result in [-INF, 0] instead of [-INF, INF]. */
3097 if (cmp == -2 || cmp == 1)
3099 wide_int sign_bit
3100 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
3101 TYPE_PRECISION (expr_type));
3102 if (!TYPE_UNSIGNED (expr_type)
3103 && ((value_range_constant_singleton (&vr0)
3104 && !wi::cmps (vr0.min, sign_bit))
3105 || (value_range_constant_singleton (&vr1)
3106 && !wi::cmps (vr1.min, sign_bit))))
3108 min = TYPE_MIN_VALUE (expr_type);
3109 max = build_int_cst (expr_type, 0);
3113 else if (code == BIT_IOR_EXPR)
3115 max = wide_int_to_tree (expr_type,
3116 may_be_nonzero0 | may_be_nonzero1);
3117 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3118 /* If the input ranges contain only positive values we can
3119 truncate the minimum of the result range to the maximum
3120 of the input range minima. */
3121 if (int_cst_range0 && int_cst_range1
3122 && tree_int_cst_sgn (vr0.min) >= 0
3123 && tree_int_cst_sgn (vr1.min) >= 0)
3125 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3126 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3128 /* If either input range contains only negative values
3129 we can truncate the minimum of the result range to the
3130 respective minimum range. */
3131 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3132 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3133 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3134 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3135 min = wide_int_to_tree (expr_type, wmin);
3137 else if (code == BIT_XOR_EXPR)
3139 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3140 | ~(may_be_nonzero0 | may_be_nonzero1));
3141 wide_int result_one_bits
3142 = (must_be_nonzero0.and_not (may_be_nonzero1)
3143 | must_be_nonzero1.and_not (may_be_nonzero0));
3144 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3145 min = wide_int_to_tree (expr_type, result_one_bits);
3146 /* If the range has all positive or all negative values the
3147 result is better than VARYING. */
3148 if (tree_int_cst_sgn (min) < 0
3149 || tree_int_cst_sgn (max) >= 0)
3151 else
3152 max = min = NULL_TREE;
3155 else
3156 gcc_unreachable ();
3158 /* If either MIN or MAX overflowed, then set the resulting range to
3159 VARYING. But we do accept an overflow infinity representation. */
3160 if (min == NULL_TREE
3161 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3162 || max == NULL_TREE
3163 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3165 set_value_range_to_varying (vr);
3166 return;
3169 /* We punt if:
3170 1) [-INF, +INF]
3171 2) [-INF, +-INF(OVF)]
3172 3) [+-INF(OVF), +INF]
3173 4) [+-INF(OVF), +-INF(OVF)]
3174 We learn nothing when we have INF and INF(OVF) on both sides.
3175 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3176 overflow. */
3177 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3178 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3180 set_value_range_to_varying (vr);
3181 return;
3184 cmp = compare_values (min, max);
3185 if (cmp == -2 || cmp == 1)
3187 /* If the new range has its limits swapped around (MIN > MAX),
3188 then the operation caused one of them to wrap around, mark
3189 the new range VARYING. */
3190 set_value_range_to_varying (vr);
3192 else
3193 set_value_range (vr, type, min, max, NULL);
3196 /* Extract range information from a binary expression OP0 CODE OP1 based on
3197 the ranges of each of its operands with resulting type EXPR_TYPE.
3198 The resulting range is stored in *VR. */
3200 static void
3201 extract_range_from_binary_expr (value_range *vr,
3202 enum tree_code code,
3203 tree expr_type, tree op0, tree op1)
3205 value_range vr0 = VR_INITIALIZER;
3206 value_range vr1 = VR_INITIALIZER;
3208 /* Get value ranges for each operand. For constant operands, create
3209 a new value range with the operand to simplify processing. */
3210 if (TREE_CODE (op0) == SSA_NAME)
3211 vr0 = *(get_value_range (op0));
3212 else if (is_gimple_min_invariant (op0))
3213 set_value_range_to_value (&vr0, op0, NULL);
3214 else
3215 set_value_range_to_varying (&vr0);
3217 if (TREE_CODE (op1) == SSA_NAME)
3218 vr1 = *(get_value_range (op1));
3219 else if (is_gimple_min_invariant (op1))
3220 set_value_range_to_value (&vr1, op1, NULL);
3221 else
3222 set_value_range_to_varying (&vr1);
3224 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3226 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3227 and based on the other operand, for example if it was deduced from a
3228 symbolic comparison. When a bound of the range of the first operand
3229 is invariant, we set the corresponding bound of the new range to INF
3230 in order to avoid recursing on the range of the second operand. */
3231 if (vr->type == VR_VARYING
3232 && (code == PLUS_EXPR || code == MINUS_EXPR)
3233 && TREE_CODE (op1) == SSA_NAME
3234 && vr0.type == VR_RANGE
3235 && symbolic_range_based_on_p (&vr0, op1))
3237 const bool minus_p = (code == MINUS_EXPR);
3238 value_range n_vr1 = VR_INITIALIZER;
3240 /* Try with VR0 and [-INF, OP1]. */
3241 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3242 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3244 /* Try with VR0 and [OP1, +INF]. */
3245 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3246 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3248 /* Try with VR0 and [OP1, OP1]. */
3249 else
3250 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3252 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3255 if (vr->type == VR_VARYING
3256 && (code == PLUS_EXPR || code == MINUS_EXPR)
3257 && TREE_CODE (op0) == SSA_NAME
3258 && vr1.type == VR_RANGE
3259 && symbolic_range_based_on_p (&vr1, op0))
3261 const bool minus_p = (code == MINUS_EXPR);
3262 value_range n_vr0 = VR_INITIALIZER;
3264 /* Try with [-INF, OP0] and VR1. */
3265 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3266 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3268 /* Try with [OP0, +INF] and VR1. */
3269 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3270 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3272 /* Try with [OP0, OP0] and VR1. */
3273 else
3274 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3276 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3280 /* Extract range information from a unary operation CODE based on
3281 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3282 The resulting range is stored in *VR. */
3284 static void
3285 extract_range_from_unary_expr_1 (value_range *vr,
3286 enum tree_code code, tree type,
3287 value_range *vr0_, tree op0_type)
3289 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3291 /* VRP only operates on integral and pointer types. */
3292 if (!(INTEGRAL_TYPE_P (op0_type)
3293 || POINTER_TYPE_P (op0_type))
3294 || !(INTEGRAL_TYPE_P (type)
3295 || POINTER_TYPE_P (type)))
3297 set_value_range_to_varying (vr);
3298 return;
3301 /* If VR0 is UNDEFINED, so is the result. */
3302 if (vr0.type == VR_UNDEFINED)
3304 set_value_range_to_undefined (vr);
3305 return;
3308 /* Handle operations that we express in terms of others. */
3309 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3311 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3312 copy_value_range (vr, &vr0);
3313 return;
3315 else if (code == NEGATE_EXPR)
3317 /* -X is simply 0 - X, so re-use existing code that also handles
3318 anti-ranges fine. */
3319 value_range zero = VR_INITIALIZER;
3320 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3321 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3322 return;
3324 else if (code == BIT_NOT_EXPR)
3326 /* ~X is simply -1 - X, so re-use existing code that also handles
3327 anti-ranges fine. */
3328 value_range minusone = VR_INITIALIZER;
3329 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3330 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3331 type, &minusone, &vr0);
3332 return;
3335 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3336 and express op ~[] as (op []') U (op []''). */
3337 if (vr0.type == VR_ANTI_RANGE
3338 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3340 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3341 if (vrtem1.type != VR_UNDEFINED)
3343 value_range vrres = VR_INITIALIZER;
3344 extract_range_from_unary_expr_1 (&vrres, code, type,
3345 &vrtem1, op0_type);
3346 vrp_meet (vr, &vrres);
3348 return;
3351 if (CONVERT_EXPR_CODE_P (code))
3353 tree inner_type = op0_type;
3354 tree outer_type = type;
3356 /* If the expression evaluates to a pointer, we are only interested in
3357 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3358 if (POINTER_TYPE_P (type))
3360 if (range_is_nonnull (&vr0))
3361 set_value_range_to_nonnull (vr, type);
3362 else if (range_is_null (&vr0))
3363 set_value_range_to_null (vr, type);
3364 else
3365 set_value_range_to_varying (vr);
3366 return;
3369 /* If VR0 is varying and we increase the type precision, assume
3370 a full range for the following transformation. */
3371 if (vr0.type == VR_VARYING
3372 && INTEGRAL_TYPE_P (inner_type)
3373 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3375 vr0.type = VR_RANGE;
3376 vr0.min = TYPE_MIN_VALUE (inner_type);
3377 vr0.max = TYPE_MAX_VALUE (inner_type);
3380 /* If VR0 is a constant range or anti-range and the conversion is
3381 not truncating we can convert the min and max values and
3382 canonicalize the resulting range. Otherwise we can do the
3383 conversion if the size of the range is less than what the
3384 precision of the target type can represent and the range is
3385 not an anti-range. */
3386 if ((vr0.type == VR_RANGE
3387 || vr0.type == VR_ANTI_RANGE)
3388 && TREE_CODE (vr0.min) == INTEGER_CST
3389 && TREE_CODE (vr0.max) == INTEGER_CST
3390 && (!is_overflow_infinity (vr0.min)
3391 || (vr0.type == VR_RANGE
3392 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3393 && needs_overflow_infinity (outer_type)
3394 && supports_overflow_infinity (outer_type)))
3395 && (!is_overflow_infinity (vr0.max)
3396 || (vr0.type == VR_RANGE
3397 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3398 && needs_overflow_infinity (outer_type)
3399 && supports_overflow_infinity (outer_type)))
3400 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3401 || (vr0.type == VR_RANGE
3402 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3403 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3404 size_int (TYPE_PRECISION (outer_type)))))))
3406 tree new_min, new_max;
3407 if (is_overflow_infinity (vr0.min))
3408 new_min = negative_overflow_infinity (outer_type);
3409 else
3410 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3411 0, false);
3412 if (is_overflow_infinity (vr0.max))
3413 new_max = positive_overflow_infinity (outer_type);
3414 else
3415 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3416 0, false);
3417 set_and_canonicalize_value_range (vr, vr0.type,
3418 new_min, new_max, NULL);
3419 return;
3422 set_value_range_to_varying (vr);
3423 return;
3425 else if (code == ABS_EXPR)
3427 tree min, max;
3428 int cmp;
3430 /* Pass through vr0 in the easy cases. */
3431 if (TYPE_UNSIGNED (type)
3432 || value_range_nonnegative_p (&vr0))
3434 copy_value_range (vr, &vr0);
3435 return;
3438 /* For the remaining varying or symbolic ranges we can't do anything
3439 useful. */
3440 if (vr0.type == VR_VARYING
3441 || symbolic_range_p (&vr0))
3443 set_value_range_to_varying (vr);
3444 return;
3447 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3448 useful range. */
3449 if (!TYPE_OVERFLOW_UNDEFINED (type)
3450 && ((vr0.type == VR_RANGE
3451 && vrp_val_is_min (vr0.min))
3452 || (vr0.type == VR_ANTI_RANGE
3453 && !vrp_val_is_min (vr0.min))))
3455 set_value_range_to_varying (vr);
3456 return;
3459 /* ABS_EXPR may flip the range around, if the original range
3460 included negative values. */
3461 if (is_overflow_infinity (vr0.min))
3462 min = positive_overflow_infinity (type);
3463 else if (!vrp_val_is_min (vr0.min))
3464 min = fold_unary_to_constant (code, type, vr0.min);
3465 else if (!needs_overflow_infinity (type))
3466 min = TYPE_MAX_VALUE (type);
3467 else if (supports_overflow_infinity (type))
3468 min = positive_overflow_infinity (type);
3469 else
3471 set_value_range_to_varying (vr);
3472 return;
3475 if (is_overflow_infinity (vr0.max))
3476 max = positive_overflow_infinity (type);
3477 else if (!vrp_val_is_min (vr0.max))
3478 max = fold_unary_to_constant (code, type, vr0.max);
3479 else if (!needs_overflow_infinity (type))
3480 max = TYPE_MAX_VALUE (type);
3481 else if (supports_overflow_infinity (type)
3482 /* We shouldn't generate [+INF, +INF] as set_value_range
3483 doesn't like this and ICEs. */
3484 && !is_positive_overflow_infinity (min))
3485 max = positive_overflow_infinity (type);
3486 else
3488 set_value_range_to_varying (vr);
3489 return;
3492 cmp = compare_values (min, max);
3494 /* If a VR_ANTI_RANGEs contains zero, then we have
3495 ~[-INF, min(MIN, MAX)]. */
3496 if (vr0.type == VR_ANTI_RANGE)
3498 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3500 /* Take the lower of the two values. */
3501 if (cmp != 1)
3502 max = min;
3504 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3505 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3506 flag_wrapv is set and the original anti-range doesn't include
3507 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3508 if (TYPE_OVERFLOW_WRAPS (type))
3510 tree type_min_value = TYPE_MIN_VALUE (type);
3512 min = (vr0.min != type_min_value
3513 ? int_const_binop (PLUS_EXPR, type_min_value,
3514 build_int_cst (TREE_TYPE (type_min_value), 1))
3515 : type_min_value);
3517 else
3519 if (overflow_infinity_range_p (&vr0))
3520 min = negative_overflow_infinity (type);
3521 else
3522 min = TYPE_MIN_VALUE (type);
3525 else
3527 /* All else has failed, so create the range [0, INF], even for
3528 flag_wrapv since TYPE_MIN_VALUE is in the original
3529 anti-range. */
3530 vr0.type = VR_RANGE;
3531 min = build_int_cst (type, 0);
3532 if (needs_overflow_infinity (type))
3534 if (supports_overflow_infinity (type))
3535 max = positive_overflow_infinity (type);
3536 else
3538 set_value_range_to_varying (vr);
3539 return;
3542 else
3543 max = TYPE_MAX_VALUE (type);
3547 /* If the range contains zero then we know that the minimum value in the
3548 range will be zero. */
3549 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3551 if (cmp == 1)
3552 max = min;
3553 min = build_int_cst (type, 0);
3555 else
3557 /* If the range was reversed, swap MIN and MAX. */
3558 if (cmp == 1)
3559 std::swap (min, max);
3562 cmp = compare_values (min, max);
3563 if (cmp == -2 || cmp == 1)
3565 /* If the new range has its limits swapped around (MIN > MAX),
3566 then the operation caused one of them to wrap around, mark
3567 the new range VARYING. */
3568 set_value_range_to_varying (vr);
3570 else
3571 set_value_range (vr, vr0.type, min, max, NULL);
3572 return;
3575 /* For unhandled operations fall back to varying. */
3576 set_value_range_to_varying (vr);
3577 return;
3581 /* Extract range information from a unary expression CODE OP0 based on
3582 the range of its operand with resulting type TYPE.
3583 The resulting range is stored in *VR. */
3585 static void
3586 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3587 tree type, tree op0)
3589 value_range vr0 = VR_INITIALIZER;
3591 /* Get value ranges for the operand. For constant operands, create
3592 a new value range with the operand to simplify processing. */
3593 if (TREE_CODE (op0) == SSA_NAME)
3594 vr0 = *(get_value_range (op0));
3595 else if (is_gimple_min_invariant (op0))
3596 set_value_range_to_value (&vr0, op0, NULL);
3597 else
3598 set_value_range_to_varying (&vr0);
3600 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3604 /* Extract range information from a conditional expression STMT based on
3605 the ranges of each of its operands and the expression code. */
3607 static void
3608 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3610 tree op0, op1;
3611 value_range vr0 = VR_INITIALIZER;
3612 value_range vr1 = VR_INITIALIZER;
3614 /* Get value ranges for each operand. For constant operands, create
3615 a new value range with the operand to simplify processing. */
3616 op0 = gimple_assign_rhs2 (stmt);
3617 if (TREE_CODE (op0) == SSA_NAME)
3618 vr0 = *(get_value_range (op0));
3619 else if (is_gimple_min_invariant (op0))
3620 set_value_range_to_value (&vr0, op0, NULL);
3621 else
3622 set_value_range_to_varying (&vr0);
3624 op1 = gimple_assign_rhs3 (stmt);
3625 if (TREE_CODE (op1) == SSA_NAME)
3626 vr1 = *(get_value_range (op1));
3627 else if (is_gimple_min_invariant (op1))
3628 set_value_range_to_value (&vr1, op1, NULL);
3629 else
3630 set_value_range_to_varying (&vr1);
3632 /* The resulting value range is the union of the operand ranges */
3633 copy_value_range (vr, &vr0);
3634 vrp_meet (vr, &vr1);
3638 /* Extract range information from a comparison expression EXPR based
3639 on the range of its operand and the expression code. */
3641 static void
3642 extract_range_from_comparison (value_range *vr, enum tree_code code,
3643 tree type, tree op0, tree op1)
3645 bool sop = false;
3646 tree val;
3648 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3649 NULL);
3651 /* A disadvantage of using a special infinity as an overflow
3652 representation is that we lose the ability to record overflow
3653 when we don't have an infinity. So we have to ignore a result
3654 which relies on overflow. */
3656 if (val && !is_overflow_infinity (val) && !sop)
3658 /* Since this expression was found on the RHS of an assignment,
3659 its type may be different from _Bool. Convert VAL to EXPR's
3660 type. */
3661 val = fold_convert (type, val);
3662 if (is_gimple_min_invariant (val))
3663 set_value_range_to_value (vr, val, vr->equiv);
3664 else
3665 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3667 else
3668 /* The result of a comparison is always true or false. */
3669 set_value_range_to_truthvalue (vr, type);
3672 /* Helper function for simplify_internal_call_using_ranges and
3673 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3674 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3675 always overflow. Set *OVF to true if it is known to always
3676 overflow. */
3678 static bool
3679 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3680 tree op0, tree op1, bool *ovf)
3682 value_range vr0 = VR_INITIALIZER;
3683 value_range vr1 = VR_INITIALIZER;
3684 if (TREE_CODE (op0) == SSA_NAME)
3685 vr0 = *get_value_range (op0);
3686 else if (TREE_CODE (op0) == INTEGER_CST)
3687 set_value_range_to_value (&vr0, op0, NULL);
3688 else
3689 set_value_range_to_varying (&vr0);
3691 if (TREE_CODE (op1) == SSA_NAME)
3692 vr1 = *get_value_range (op1);
3693 else if (TREE_CODE (op1) == INTEGER_CST)
3694 set_value_range_to_value (&vr1, op1, NULL);
3695 else
3696 set_value_range_to_varying (&vr1);
3698 if (!range_int_cst_p (&vr0)
3699 || TREE_OVERFLOW (vr0.min)
3700 || TREE_OVERFLOW (vr0.max))
3702 vr0.min = vrp_val_min (TREE_TYPE (op0));
3703 vr0.max = vrp_val_max (TREE_TYPE (op0));
3705 if (!range_int_cst_p (&vr1)
3706 || TREE_OVERFLOW (vr1.min)
3707 || TREE_OVERFLOW (vr1.max))
3709 vr1.min = vrp_val_min (TREE_TYPE (op1));
3710 vr1.max = vrp_val_max (TREE_TYPE (op1));
3712 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3713 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3714 if (arith_overflowed_p (subcode, type, vr0.max,
3715 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3716 return false;
3717 if (subcode == MULT_EXPR)
3719 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3720 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3721 return false;
3723 if (*ovf)
3725 /* So far we found that there is an overflow on the boundaries.
3726 That doesn't prove that there is an overflow even for all values
3727 in between the boundaries. For that compute widest_int range
3728 of the result and see if it doesn't overlap the range of
3729 type. */
3730 widest_int wmin, wmax;
3731 widest_int w[4];
3732 int i;
3733 w[0] = wi::to_widest (vr0.min);
3734 w[1] = wi::to_widest (vr0.max);
3735 w[2] = wi::to_widest (vr1.min);
3736 w[3] = wi::to_widest (vr1.max);
3737 for (i = 0; i < 4; i++)
3739 widest_int wt;
3740 switch (subcode)
3742 case PLUS_EXPR:
3743 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3744 break;
3745 case MINUS_EXPR:
3746 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3747 break;
3748 case MULT_EXPR:
3749 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3750 break;
3751 default:
3752 gcc_unreachable ();
3754 if (i == 0)
3756 wmin = wt;
3757 wmax = wt;
3759 else
3761 wmin = wi::smin (wmin, wt);
3762 wmax = wi::smax (wmax, wt);
3765 /* The result of op0 CODE op1 is known to be in range
3766 [wmin, wmax]. */
3767 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3768 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3769 /* If all values in [wmin, wmax] are smaller than
3770 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3771 the arithmetic operation will always overflow. */
3772 if (wmax < wtmin || wmin > wtmax)
3773 return true;
3774 return false;
3776 return true;
3779 /* Try to derive a nonnegative or nonzero range out of STMT relying
3780 primarily on generic routines in fold in conjunction with range data.
3781 Store the result in *VR */
3783 static void
3784 extract_range_basic (value_range *vr, gimple *stmt)
3786 bool sop = false;
3787 tree type = gimple_expr_type (stmt);
3789 if (is_gimple_call (stmt))
3791 tree arg;
3792 int mini, maxi, zerov = 0, prec;
3793 enum tree_code subcode = ERROR_MARK;
3794 combined_fn cfn = gimple_call_combined_fn (stmt);
3796 switch (cfn)
3798 case CFN_BUILT_IN_CONSTANT_P:
3799 /* If the call is __builtin_constant_p and the argument is a
3800 function parameter resolve it to false. This avoids bogus
3801 array bound warnings.
3802 ??? We could do this as early as inlining is finished. */
3803 arg = gimple_call_arg (stmt, 0);
3804 if (TREE_CODE (arg) == SSA_NAME
3805 && SSA_NAME_IS_DEFAULT_DEF (arg)
3806 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
3807 && cfun->after_inlining)
3809 set_value_range_to_null (vr, type);
3810 return;
3812 break;
3813 /* Both __builtin_ffs* and __builtin_popcount return
3814 [0, prec]. */
3815 CASE_CFN_FFS:
3816 CASE_CFN_POPCOUNT:
3817 arg = gimple_call_arg (stmt, 0);
3818 prec = TYPE_PRECISION (TREE_TYPE (arg));
3819 mini = 0;
3820 maxi = prec;
3821 if (TREE_CODE (arg) == SSA_NAME)
3823 value_range *vr0 = get_value_range (arg);
3824 /* If arg is non-zero, then ffs or popcount
3825 are non-zero. */
3826 if (((vr0->type == VR_RANGE
3827 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3828 || (vr0->type == VR_ANTI_RANGE
3829 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3830 && !is_overflow_infinity (vr0->min)
3831 && !is_overflow_infinity (vr0->max))
3832 mini = 1;
3833 /* If some high bits are known to be zero,
3834 we can decrease the maximum. */
3835 if (vr0->type == VR_RANGE
3836 && TREE_CODE (vr0->max) == INTEGER_CST
3837 && !operand_less_p (vr0->min,
3838 build_zero_cst (TREE_TYPE (vr0->min)))
3839 && !is_overflow_infinity (vr0->max))
3840 maxi = tree_floor_log2 (vr0->max) + 1;
3842 goto bitop_builtin;
3843 /* __builtin_parity* returns [0, 1]. */
3844 CASE_CFN_PARITY:
3845 mini = 0;
3846 maxi = 1;
3847 goto bitop_builtin;
3848 /* __builtin_c[lt]z* return [0, prec-1], except for
3849 when the argument is 0, but that is undefined behavior.
3850 On many targets where the CLZ RTL or optab value is defined
3851 for 0 the value is prec, so include that in the range
3852 by default. */
3853 CASE_CFN_CLZ:
3854 arg = gimple_call_arg (stmt, 0);
3855 prec = TYPE_PRECISION (TREE_TYPE (arg));
3856 mini = 0;
3857 maxi = prec;
3858 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3859 != CODE_FOR_nothing
3860 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3861 zerov)
3862 /* Handle only the single common value. */
3863 && zerov != prec)
3864 /* Magic value to give up, unless vr0 proves
3865 arg is non-zero. */
3866 mini = -2;
3867 if (TREE_CODE (arg) == SSA_NAME)
3869 value_range *vr0 = get_value_range (arg);
3870 /* From clz of VR_RANGE minimum we can compute
3871 result maximum. */
3872 if (vr0->type == VR_RANGE
3873 && TREE_CODE (vr0->min) == INTEGER_CST
3874 && !is_overflow_infinity (vr0->min))
3876 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3877 if (maxi != prec)
3878 mini = 0;
3880 else if (vr0->type == VR_ANTI_RANGE
3881 && integer_zerop (vr0->min)
3882 && !is_overflow_infinity (vr0->min))
3884 maxi = prec - 1;
3885 mini = 0;
3887 if (mini == -2)
3888 break;
3889 /* From clz of VR_RANGE maximum we can compute
3890 result minimum. */
3891 if (vr0->type == VR_RANGE
3892 && TREE_CODE (vr0->max) == INTEGER_CST
3893 && !is_overflow_infinity (vr0->max))
3895 mini = prec - 1 - tree_floor_log2 (vr0->max);
3896 if (mini == prec)
3897 break;
3900 if (mini == -2)
3901 break;
3902 goto bitop_builtin;
3903 /* __builtin_ctz* return [0, prec-1], except for
3904 when the argument is 0, but that is undefined behavior.
3905 If there is a ctz optab for this mode and
3906 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3907 otherwise just assume 0 won't be seen. */
3908 CASE_CFN_CTZ:
3909 arg = gimple_call_arg (stmt, 0);
3910 prec = TYPE_PRECISION (TREE_TYPE (arg));
3911 mini = 0;
3912 maxi = prec - 1;
3913 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3914 != CODE_FOR_nothing
3915 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3916 zerov))
3918 /* Handle only the two common values. */
3919 if (zerov == -1)
3920 mini = -1;
3921 else if (zerov == prec)
3922 maxi = prec;
3923 else
3924 /* Magic value to give up, unless vr0 proves
3925 arg is non-zero. */
3926 mini = -2;
3928 if (TREE_CODE (arg) == SSA_NAME)
3930 value_range *vr0 = get_value_range (arg);
3931 /* If arg is non-zero, then use [0, prec - 1]. */
3932 if (((vr0->type == VR_RANGE
3933 && integer_nonzerop (vr0->min))
3934 || (vr0->type == VR_ANTI_RANGE
3935 && integer_zerop (vr0->min)))
3936 && !is_overflow_infinity (vr0->min))
3938 mini = 0;
3939 maxi = prec - 1;
3941 /* If some high bits are known to be zero,
3942 we can decrease the result maximum. */
3943 if (vr0->type == VR_RANGE
3944 && TREE_CODE (vr0->max) == INTEGER_CST
3945 && !is_overflow_infinity (vr0->max))
3947 maxi = tree_floor_log2 (vr0->max);
3948 /* For vr0 [0, 0] give up. */
3949 if (maxi == -1)
3950 break;
3953 if (mini == -2)
3954 break;
3955 goto bitop_builtin;
3956 /* __builtin_clrsb* returns [0, prec-1]. */
3957 CASE_CFN_CLRSB:
3958 arg = gimple_call_arg (stmt, 0);
3959 prec = TYPE_PRECISION (TREE_TYPE (arg));
3960 mini = 0;
3961 maxi = prec - 1;
3962 goto bitop_builtin;
3963 bitop_builtin:
3964 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3965 build_int_cst (type, maxi), NULL);
3966 return;
3967 case CFN_UBSAN_CHECK_ADD:
3968 subcode = PLUS_EXPR;
3969 break;
3970 case CFN_UBSAN_CHECK_SUB:
3971 subcode = MINUS_EXPR;
3972 break;
3973 case CFN_UBSAN_CHECK_MUL:
3974 subcode = MULT_EXPR;
3975 break;
3976 case CFN_GOACC_DIM_SIZE:
3977 case CFN_GOACC_DIM_POS:
3978 /* Optimizing these two internal functions helps the loop
3979 optimizer eliminate outer comparisons. Size is [1,N]
3980 and pos is [0,N-1]. */
3982 bool is_pos = cfn == CFN_GOACC_DIM_POS;
3983 int axis = get_oacc_ifn_dim_arg (stmt);
3984 int size = get_oacc_fn_dim_size (current_function_decl, axis);
3986 if (!size)
3987 /* If it's dynamic, the backend might know a hardware
3988 limitation. */
3989 size = targetm.goacc.dim_limit (axis);
3991 tree type = TREE_TYPE (gimple_call_lhs (stmt));
3992 set_value_range (vr, VR_RANGE,
3993 build_int_cst (type, is_pos ? 0 : 1),
3994 size ? build_int_cst (type, size - is_pos)
3995 : vrp_val_max (type), NULL);
3997 return;
3998 default:
3999 break;
4001 if (subcode != ERROR_MARK)
4003 bool saved_flag_wrapv = flag_wrapv;
4004 /* Pretend the arithmetics is wrapping. If there is
4005 any overflow, we'll complain, but will actually do
4006 wrapping operation. */
4007 flag_wrapv = 1;
4008 extract_range_from_binary_expr (vr, subcode, type,
4009 gimple_call_arg (stmt, 0),
4010 gimple_call_arg (stmt, 1));
4011 flag_wrapv = saved_flag_wrapv;
4013 /* If for both arguments vrp_valueize returned non-NULL,
4014 this should have been already folded and if not, it
4015 wasn't folded because of overflow. Avoid removing the
4016 UBSAN_CHECK_* calls in that case. */
4017 if (vr->type == VR_RANGE
4018 && (vr->min == vr->max
4019 || operand_equal_p (vr->min, vr->max, 0)))
4020 set_value_range_to_varying (vr);
4021 return;
4024 /* Handle extraction of the two results (result of arithmetics and
4025 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4026 internal function. */
4027 else if (is_gimple_assign (stmt)
4028 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4029 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4030 && INTEGRAL_TYPE_P (type))
4032 enum tree_code code = gimple_assign_rhs_code (stmt);
4033 tree op = gimple_assign_rhs1 (stmt);
4034 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4036 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4037 if (is_gimple_call (g) && gimple_call_internal_p (g))
4039 enum tree_code subcode = ERROR_MARK;
4040 switch (gimple_call_internal_fn (g))
4042 case IFN_ADD_OVERFLOW:
4043 subcode = PLUS_EXPR;
4044 break;
4045 case IFN_SUB_OVERFLOW:
4046 subcode = MINUS_EXPR;
4047 break;
4048 case IFN_MUL_OVERFLOW:
4049 subcode = MULT_EXPR;
4050 break;
4051 default:
4052 break;
4054 if (subcode != ERROR_MARK)
4056 tree op0 = gimple_call_arg (g, 0);
4057 tree op1 = gimple_call_arg (g, 1);
4058 if (code == IMAGPART_EXPR)
4060 bool ovf = false;
4061 if (check_for_binary_op_overflow (subcode, type,
4062 op0, op1, &ovf))
4063 set_value_range_to_value (vr,
4064 build_int_cst (type, ovf),
4065 NULL);
4066 else if (TYPE_PRECISION (type) == 1
4067 && !TYPE_UNSIGNED (type))
4068 set_value_range_to_varying (vr);
4069 else
4070 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4071 build_int_cst (type, 1), NULL);
4073 else if (types_compatible_p (type, TREE_TYPE (op0))
4074 && types_compatible_p (type, TREE_TYPE (op1)))
4076 bool saved_flag_wrapv = flag_wrapv;
4077 /* Pretend the arithmetics is wrapping. If there is
4078 any overflow, IMAGPART_EXPR will be set. */
4079 flag_wrapv = 1;
4080 extract_range_from_binary_expr (vr, subcode, type,
4081 op0, op1);
4082 flag_wrapv = saved_flag_wrapv;
4084 else
4086 value_range vr0 = VR_INITIALIZER;
4087 value_range vr1 = VR_INITIALIZER;
4088 bool saved_flag_wrapv = flag_wrapv;
4089 /* Pretend the arithmetics is wrapping. If there is
4090 any overflow, IMAGPART_EXPR will be set. */
4091 flag_wrapv = 1;
4092 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4093 type, op0);
4094 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4095 type, op1);
4096 extract_range_from_binary_expr_1 (vr, subcode, type,
4097 &vr0, &vr1);
4098 flag_wrapv = saved_flag_wrapv;
4100 return;
4105 if (INTEGRAL_TYPE_P (type)
4106 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4107 set_value_range_to_nonnegative (vr, type,
4108 sop || stmt_overflow_infinity (stmt));
4109 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4110 && !sop)
4111 set_value_range_to_nonnull (vr, type);
4112 else
4113 set_value_range_to_varying (vr);
4117 /* Try to compute a useful range out of assignment STMT and store it
4118 in *VR. */
4120 static void
4121 extract_range_from_assignment (value_range *vr, gassign *stmt)
4123 enum tree_code code = gimple_assign_rhs_code (stmt);
4125 if (code == ASSERT_EXPR)
4126 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4127 else if (code == SSA_NAME)
4128 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4129 else if (TREE_CODE_CLASS (code) == tcc_binary)
4130 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4131 gimple_expr_type (stmt),
4132 gimple_assign_rhs1 (stmt),
4133 gimple_assign_rhs2 (stmt));
4134 else if (TREE_CODE_CLASS (code) == tcc_unary)
4135 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4136 gimple_expr_type (stmt),
4137 gimple_assign_rhs1 (stmt));
4138 else if (code == COND_EXPR)
4139 extract_range_from_cond_expr (vr, stmt);
4140 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4141 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4142 gimple_expr_type (stmt),
4143 gimple_assign_rhs1 (stmt),
4144 gimple_assign_rhs2 (stmt));
4145 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4146 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4147 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4148 else
4149 set_value_range_to_varying (vr);
4151 if (vr->type == VR_VARYING)
4152 extract_range_basic (vr, stmt);
4155 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4156 would be profitable to adjust VR using scalar evolution information
4157 for VAR. If so, update VR with the new limits. */
4159 static void
4160 adjust_range_with_scev (value_range *vr, struct loop *loop,
4161 gimple *stmt, tree var)
4163 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4164 enum ev_direction dir;
4166 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4167 better opportunities than a regular range, but I'm not sure. */
4168 if (vr->type == VR_ANTI_RANGE)
4169 return;
4171 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4173 /* Like in PR19590, scev can return a constant function. */
4174 if (is_gimple_min_invariant (chrec))
4176 set_value_range_to_value (vr, chrec, vr->equiv);
4177 return;
4180 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4181 return;
4183 init = initial_condition_in_loop_num (chrec, loop->num);
4184 tem = op_with_constant_singleton_value_range (init);
4185 if (tem)
4186 init = tem;
4187 step = evolution_part_in_loop_num (chrec, loop->num);
4188 tem = op_with_constant_singleton_value_range (step);
4189 if (tem)
4190 step = tem;
4192 /* If STEP is symbolic, we can't know whether INIT will be the
4193 minimum or maximum value in the range. Also, unless INIT is
4194 a simple expression, compare_values and possibly other functions
4195 in tree-vrp won't be able to handle it. */
4196 if (step == NULL_TREE
4197 || !is_gimple_min_invariant (step)
4198 || !valid_value_p (init))
4199 return;
4201 dir = scev_direction (chrec);
4202 if (/* Do not adjust ranges if we do not know whether the iv increases
4203 or decreases, ... */
4204 dir == EV_DIR_UNKNOWN
4205 /* ... or if it may wrap. */
4206 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
4207 get_chrec_loop (chrec), true))
4208 return;
4210 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4211 negative_overflow_infinity and positive_overflow_infinity,
4212 because we have concluded that the loop probably does not
4213 wrap. */
4215 type = TREE_TYPE (var);
4216 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4217 tmin = lower_bound_in_type (type, type);
4218 else
4219 tmin = TYPE_MIN_VALUE (type);
4220 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4221 tmax = upper_bound_in_type (type, type);
4222 else
4223 tmax = TYPE_MAX_VALUE (type);
4225 /* Try to use estimated number of iterations for the loop to constrain the
4226 final value in the evolution. */
4227 if (TREE_CODE (step) == INTEGER_CST
4228 && is_gimple_val (init)
4229 && (TREE_CODE (init) != SSA_NAME
4230 || get_value_range (init)->type == VR_RANGE))
4232 widest_int nit;
4234 /* We are only entering here for loop header PHI nodes, so using
4235 the number of latch executions is the correct thing to use. */
4236 if (max_loop_iterations (loop, &nit))
4238 value_range maxvr = VR_INITIALIZER;
4239 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4240 bool overflow;
4242 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4243 &overflow);
4244 /* If the multiplication overflowed we can't do a meaningful
4245 adjustment. Likewise if the result doesn't fit in the type
4246 of the induction variable. For a signed type we have to
4247 check whether the result has the expected signedness which
4248 is that of the step as number of iterations is unsigned. */
4249 if (!overflow
4250 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4251 && (sgn == UNSIGNED
4252 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4254 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4255 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4256 TREE_TYPE (init), init, tem);
4257 /* Likewise if the addition did. */
4258 if (maxvr.type == VR_RANGE)
4260 value_range initvr = VR_INITIALIZER;
4262 if (TREE_CODE (init) == SSA_NAME)
4263 initvr = *(get_value_range (init));
4264 else if (is_gimple_min_invariant (init))
4265 set_value_range_to_value (&initvr, init, NULL);
4266 else
4267 return;
4269 /* Check if init + nit * step overflows. Though we checked
4270 scev {init, step}_loop doesn't wrap, it is not enough
4271 because the loop may exit immediately. Overflow could
4272 happen in the plus expression in this case. */
4273 if ((dir == EV_DIR_DECREASES
4274 && (is_negative_overflow_infinity (maxvr.min)
4275 || compare_values (maxvr.min, initvr.min) != -1))
4276 || (dir == EV_DIR_GROWS
4277 && (is_positive_overflow_infinity (maxvr.max)
4278 || compare_values (maxvr.max, initvr.max) != 1)))
4279 return;
4281 tmin = maxvr.min;
4282 tmax = maxvr.max;
4288 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4290 min = tmin;
4291 max = tmax;
4293 /* For VARYING or UNDEFINED ranges, just about anything we get
4294 from scalar evolutions should be better. */
4296 if (dir == EV_DIR_DECREASES)
4297 max = init;
4298 else
4299 min = init;
4301 else if (vr->type == VR_RANGE)
4303 min = vr->min;
4304 max = vr->max;
4306 if (dir == EV_DIR_DECREASES)
4308 /* INIT is the maximum value. If INIT is lower than VR->MAX
4309 but no smaller than VR->MIN, set VR->MAX to INIT. */
4310 if (compare_values (init, max) == -1)
4311 max = init;
4313 /* According to the loop information, the variable does not
4314 overflow. If we think it does, probably because of an
4315 overflow due to arithmetic on a different INF value,
4316 reset now. */
4317 if (is_negative_overflow_infinity (min)
4318 || compare_values (min, tmin) == -1)
4319 min = tmin;
4322 else
4324 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4325 if (compare_values (init, min) == 1)
4326 min = init;
4328 if (is_positive_overflow_infinity (max)
4329 || compare_values (tmax, max) == -1)
4330 max = tmax;
4333 else
4334 return;
4336 /* If we just created an invalid range with the minimum
4337 greater than the maximum, we fail conservatively.
4338 This should happen only in unreachable
4339 parts of code, or for invalid programs. */
4340 if (compare_values (min, max) == 1
4341 || (is_negative_overflow_infinity (min)
4342 && is_positive_overflow_infinity (max)))
4343 return;
4345 /* Even for valid range info, sometimes overflow flag will leak in.
4346 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4347 drop them except for +-overflow_infinity which still need special
4348 handling in vrp pass. */
4349 if (TREE_OVERFLOW_P (min)
4350 && ! is_negative_overflow_infinity (min))
4351 min = drop_tree_overflow (min);
4352 if (TREE_OVERFLOW_P (max)
4353 && ! is_positive_overflow_infinity (max))
4354 max = drop_tree_overflow (max);
4356 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4360 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4362 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4363 all the values in the ranges.
4365 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4367 - Return NULL_TREE if it is not always possible to determine the
4368 value of the comparison.
4370 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4371 overflow infinity was used in the test. */
4374 static tree
4375 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4376 bool *strict_overflow_p)
4378 /* VARYING or UNDEFINED ranges cannot be compared. */
4379 if (vr0->type == VR_VARYING
4380 || vr0->type == VR_UNDEFINED
4381 || vr1->type == VR_VARYING
4382 || vr1->type == VR_UNDEFINED)
4383 return NULL_TREE;
4385 /* Anti-ranges need to be handled separately. */
4386 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4388 /* If both are anti-ranges, then we cannot compute any
4389 comparison. */
4390 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4391 return NULL_TREE;
4393 /* These comparisons are never statically computable. */
4394 if (comp == GT_EXPR
4395 || comp == GE_EXPR
4396 || comp == LT_EXPR
4397 || comp == LE_EXPR)
4398 return NULL_TREE;
4400 /* Equality can be computed only between a range and an
4401 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4402 if (vr0->type == VR_RANGE)
4404 /* To simplify processing, make VR0 the anti-range. */
4405 value_range *tmp = vr0;
4406 vr0 = vr1;
4407 vr1 = tmp;
4410 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4412 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4413 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4414 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4416 return NULL_TREE;
4419 if (!usable_range_p (vr0, strict_overflow_p)
4420 || !usable_range_p (vr1, strict_overflow_p))
4421 return NULL_TREE;
4423 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4424 operands around and change the comparison code. */
4425 if (comp == GT_EXPR || comp == GE_EXPR)
4427 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4428 std::swap (vr0, vr1);
4431 if (comp == EQ_EXPR)
4433 /* Equality may only be computed if both ranges represent
4434 exactly one value. */
4435 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4436 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4438 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4439 strict_overflow_p);
4440 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4441 strict_overflow_p);
4442 if (cmp_min == 0 && cmp_max == 0)
4443 return boolean_true_node;
4444 else if (cmp_min != -2 && cmp_max != -2)
4445 return boolean_false_node;
4447 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4448 else if (compare_values_warnv (vr0->min, vr1->max,
4449 strict_overflow_p) == 1
4450 || compare_values_warnv (vr1->min, vr0->max,
4451 strict_overflow_p) == 1)
4452 return boolean_false_node;
4454 return NULL_TREE;
4456 else if (comp == NE_EXPR)
4458 int cmp1, cmp2;
4460 /* If VR0 is completely to the left or completely to the right
4461 of VR1, they are always different. Notice that we need to
4462 make sure that both comparisons yield similar results to
4463 avoid comparing values that cannot be compared at
4464 compile-time. */
4465 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4466 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4467 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4468 return boolean_true_node;
4470 /* If VR0 and VR1 represent a single value and are identical,
4471 return false. */
4472 else if (compare_values_warnv (vr0->min, vr0->max,
4473 strict_overflow_p) == 0
4474 && compare_values_warnv (vr1->min, vr1->max,
4475 strict_overflow_p) == 0
4476 && compare_values_warnv (vr0->min, vr1->min,
4477 strict_overflow_p) == 0
4478 && compare_values_warnv (vr0->max, vr1->max,
4479 strict_overflow_p) == 0)
4480 return boolean_false_node;
4482 /* Otherwise, they may or may not be different. */
4483 else
4484 return NULL_TREE;
4486 else if (comp == LT_EXPR || comp == LE_EXPR)
4488 int tst;
4490 /* If VR0 is to the left of VR1, return true. */
4491 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4492 if ((comp == LT_EXPR && tst == -1)
4493 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4495 if (overflow_infinity_range_p (vr0)
4496 || overflow_infinity_range_p (vr1))
4497 *strict_overflow_p = true;
4498 return boolean_true_node;
4501 /* If VR0 is to the right of VR1, return false. */
4502 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4503 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4504 || (comp == LE_EXPR && tst == 1))
4506 if (overflow_infinity_range_p (vr0)
4507 || overflow_infinity_range_p (vr1))
4508 *strict_overflow_p = true;
4509 return boolean_false_node;
4512 /* Otherwise, we don't know. */
4513 return NULL_TREE;
4516 gcc_unreachable ();
4520 /* Given a value range VR, a value VAL and a comparison code COMP, return
4521 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4522 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4523 always returns false. Return NULL_TREE if it is not always
4524 possible to determine the value of the comparison. Also set
4525 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4526 infinity was used in the test. */
4528 static tree
4529 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4530 bool *strict_overflow_p)
4532 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4533 return NULL_TREE;
4535 /* Anti-ranges need to be handled separately. */
4536 if (vr->type == VR_ANTI_RANGE)
4538 /* For anti-ranges, the only predicates that we can compute at
4539 compile time are equality and inequality. */
4540 if (comp == GT_EXPR
4541 || comp == GE_EXPR
4542 || comp == LT_EXPR
4543 || comp == LE_EXPR)
4544 return NULL_TREE;
4546 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4547 if (value_inside_range (val, vr->min, vr->max) == 1)
4548 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4550 return NULL_TREE;
4553 if (!usable_range_p (vr, strict_overflow_p))
4554 return NULL_TREE;
4556 if (comp == EQ_EXPR)
4558 /* EQ_EXPR may only be computed if VR represents exactly
4559 one value. */
4560 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4562 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4563 if (cmp == 0)
4564 return boolean_true_node;
4565 else if (cmp == -1 || cmp == 1 || cmp == 2)
4566 return boolean_false_node;
4568 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4569 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4570 return boolean_false_node;
4572 return NULL_TREE;
4574 else if (comp == NE_EXPR)
4576 /* If VAL is not inside VR, then they are always different. */
4577 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4578 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4579 return boolean_true_node;
4581 /* If VR represents exactly one value equal to VAL, then return
4582 false. */
4583 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4584 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4585 return boolean_false_node;
4587 /* Otherwise, they may or may not be different. */
4588 return NULL_TREE;
4590 else if (comp == LT_EXPR || comp == LE_EXPR)
4592 int tst;
4594 /* If VR is to the left of VAL, return true. */
4595 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4596 if ((comp == LT_EXPR && tst == -1)
4597 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4599 if (overflow_infinity_range_p (vr))
4600 *strict_overflow_p = true;
4601 return boolean_true_node;
4604 /* If VR is to the right of VAL, return false. */
4605 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4606 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4607 || (comp == LE_EXPR && tst == 1))
4609 if (overflow_infinity_range_p (vr))
4610 *strict_overflow_p = true;
4611 return boolean_false_node;
4614 /* Otherwise, we don't know. */
4615 return NULL_TREE;
4617 else if (comp == GT_EXPR || comp == GE_EXPR)
4619 int tst;
4621 /* If VR is to the right of VAL, return true. */
4622 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4623 if ((comp == GT_EXPR && tst == 1)
4624 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4626 if (overflow_infinity_range_p (vr))
4627 *strict_overflow_p = true;
4628 return boolean_true_node;
4631 /* If VR is to the left of VAL, return false. */
4632 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4633 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4634 || (comp == GE_EXPR && tst == -1))
4636 if (overflow_infinity_range_p (vr))
4637 *strict_overflow_p = true;
4638 return boolean_false_node;
4641 /* Otherwise, we don't know. */
4642 return NULL_TREE;
4645 gcc_unreachable ();
4649 /* Debugging dumps. */
4651 void dump_value_range (FILE *, const value_range *);
4652 void debug_value_range (value_range *);
4653 void dump_all_value_ranges (FILE *);
4654 void debug_all_value_ranges (void);
4655 void dump_vr_equiv (FILE *, bitmap);
4656 void debug_vr_equiv (bitmap);
4659 /* Dump value range VR to FILE. */
4661 void
4662 dump_value_range (FILE *file, const value_range *vr)
4664 if (vr == NULL)
4665 fprintf (file, "[]");
4666 else if (vr->type == VR_UNDEFINED)
4667 fprintf (file, "UNDEFINED");
4668 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4670 tree type = TREE_TYPE (vr->min);
4672 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4674 if (is_negative_overflow_infinity (vr->min))
4675 fprintf (file, "-INF(OVF)");
4676 else if (INTEGRAL_TYPE_P (type)
4677 && !TYPE_UNSIGNED (type)
4678 && vrp_val_is_min (vr->min))
4679 fprintf (file, "-INF");
4680 else
4681 print_generic_expr (file, vr->min, 0);
4683 fprintf (file, ", ");
4685 if (is_positive_overflow_infinity (vr->max))
4686 fprintf (file, "+INF(OVF)");
4687 else if (INTEGRAL_TYPE_P (type)
4688 && vrp_val_is_max (vr->max))
4689 fprintf (file, "+INF");
4690 else
4691 print_generic_expr (file, vr->max, 0);
4693 fprintf (file, "]");
4695 if (vr->equiv)
4697 bitmap_iterator bi;
4698 unsigned i, c = 0;
4700 fprintf (file, " EQUIVALENCES: { ");
4702 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4704 print_generic_expr (file, ssa_name (i), 0);
4705 fprintf (file, " ");
4706 c++;
4709 fprintf (file, "} (%u elements)", c);
4712 else if (vr->type == VR_VARYING)
4713 fprintf (file, "VARYING");
4714 else
4715 fprintf (file, "INVALID RANGE");
4719 /* Dump value range VR to stderr. */
4721 DEBUG_FUNCTION void
4722 debug_value_range (value_range *vr)
4724 dump_value_range (stderr, vr);
4725 fprintf (stderr, "\n");
4729 /* Dump value ranges of all SSA_NAMEs to FILE. */
4731 void
4732 dump_all_value_ranges (FILE *file)
4734 size_t i;
4736 for (i = 0; i < num_vr_values; i++)
4738 if (vr_value[i])
4740 print_generic_expr (file, ssa_name (i), 0);
4741 fprintf (file, ": ");
4742 dump_value_range (file, vr_value[i]);
4743 fprintf (file, "\n");
4747 fprintf (file, "\n");
4751 /* Dump all value ranges to stderr. */
4753 DEBUG_FUNCTION void
4754 debug_all_value_ranges (void)
4756 dump_all_value_ranges (stderr);
4760 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4761 create a new SSA name N and return the assertion assignment
4762 'N = ASSERT_EXPR <V, V OP W>'. */
4764 static gimple *
4765 build_assert_expr_for (tree cond, tree v)
4767 tree a;
4768 gassign *assertion;
4770 gcc_assert (TREE_CODE (v) == SSA_NAME
4771 && COMPARISON_CLASS_P (cond));
4773 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4774 assertion = gimple_build_assign (NULL_TREE, a);
4776 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4777 operand of the ASSERT_EXPR. Create it so the new name and the old one
4778 are registered in the replacement table so that we can fix the SSA web
4779 after adding all the ASSERT_EXPRs. */
4780 create_new_def_for (v, assertion, NULL);
4782 return assertion;
4786 /* Return false if EXPR is a predicate expression involving floating
4787 point values. */
4789 static inline bool
4790 fp_predicate (gimple *stmt)
4792 GIMPLE_CHECK (stmt, GIMPLE_COND);
4794 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4797 /* If the range of values taken by OP can be inferred after STMT executes,
4798 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4799 describes the inferred range. Return true if a range could be
4800 inferred. */
4802 static bool
4803 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4805 *val_p = NULL_TREE;
4806 *comp_code_p = ERROR_MARK;
4808 /* Do not attempt to infer anything in names that flow through
4809 abnormal edges. */
4810 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4811 return false;
4813 /* If STMT is the last statement of a basic block with no normal
4814 successors, there is no point inferring anything about any of its
4815 operands. We would not be able to find a proper insertion point
4816 for the assertion, anyway. */
4817 if (stmt_ends_bb_p (stmt))
4819 edge_iterator ei;
4820 edge e;
4822 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4823 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
4824 break;
4825 if (e == NULL)
4826 return false;
4829 if (infer_nonnull_range (stmt, op))
4831 *val_p = build_int_cst (TREE_TYPE (op), 0);
4832 *comp_code_p = NE_EXPR;
4833 return true;
4836 return false;
4840 void dump_asserts_for (FILE *, tree);
4841 void debug_asserts_for (tree);
4842 void dump_all_asserts (FILE *);
4843 void debug_all_asserts (void);
4845 /* Dump all the registered assertions for NAME to FILE. */
4847 void
4848 dump_asserts_for (FILE *file, tree name)
4850 assert_locus *loc;
4852 fprintf (file, "Assertions to be inserted for ");
4853 print_generic_expr (file, name, 0);
4854 fprintf (file, "\n");
4856 loc = asserts_for[SSA_NAME_VERSION (name)];
4857 while (loc)
4859 fprintf (file, "\t");
4860 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4861 fprintf (file, "\n\tBB #%d", loc->bb->index);
4862 if (loc->e)
4864 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4865 loc->e->dest->index);
4866 dump_edge_info (file, loc->e, dump_flags, 0);
4868 fprintf (file, "\n\tPREDICATE: ");
4869 print_generic_expr (file, loc->expr, 0);
4870 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4871 print_generic_expr (file, loc->val, 0);
4872 fprintf (file, "\n\n");
4873 loc = loc->next;
4876 fprintf (file, "\n");
4880 /* Dump all the registered assertions for NAME to stderr. */
4882 DEBUG_FUNCTION void
4883 debug_asserts_for (tree name)
4885 dump_asserts_for (stderr, name);
4889 /* Dump all the registered assertions for all the names to FILE. */
4891 void
4892 dump_all_asserts (FILE *file)
4894 unsigned i;
4895 bitmap_iterator bi;
4897 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4898 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4899 dump_asserts_for (file, ssa_name (i));
4900 fprintf (file, "\n");
4904 /* Dump all the registered assertions for all the names to stderr. */
4906 DEBUG_FUNCTION void
4907 debug_all_asserts (void)
4909 dump_all_asserts (stderr);
4913 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4914 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4915 E->DEST, then register this location as a possible insertion point
4916 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4918 BB, E and SI provide the exact insertion point for the new
4919 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4920 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4921 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4922 must not be NULL. */
4924 static void
4925 register_new_assert_for (tree name, tree expr,
4926 enum tree_code comp_code,
4927 tree val,
4928 basic_block bb,
4929 edge e,
4930 gimple_stmt_iterator si)
4932 assert_locus *n, *loc, *last_loc;
4933 basic_block dest_bb;
4935 gcc_checking_assert (bb == NULL || e == NULL);
4937 if (e == NULL)
4938 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4939 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4941 /* Never build an assert comparing against an integer constant with
4942 TREE_OVERFLOW set. This confuses our undefined overflow warning
4943 machinery. */
4944 if (TREE_OVERFLOW_P (val))
4945 val = drop_tree_overflow (val);
4947 /* The new assertion A will be inserted at BB or E. We need to
4948 determine if the new location is dominated by a previously
4949 registered location for A. If we are doing an edge insertion,
4950 assume that A will be inserted at E->DEST. Note that this is not
4951 necessarily true.
4953 If E is a critical edge, it will be split. But even if E is
4954 split, the new block will dominate the same set of blocks that
4955 E->DEST dominates.
4957 The reverse, however, is not true, blocks dominated by E->DEST
4958 will not be dominated by the new block created to split E. So,
4959 if the insertion location is on a critical edge, we will not use
4960 the new location to move another assertion previously registered
4961 at a block dominated by E->DEST. */
4962 dest_bb = (bb) ? bb : e->dest;
4964 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4965 VAL at a block dominating DEST_BB, then we don't need to insert a new
4966 one. Similarly, if the same assertion already exists at a block
4967 dominated by DEST_BB and the new location is not on a critical
4968 edge, then update the existing location for the assertion (i.e.,
4969 move the assertion up in the dominance tree).
4971 Note, this is implemented as a simple linked list because there
4972 should not be more than a handful of assertions registered per
4973 name. If this becomes a performance problem, a table hashed by
4974 COMP_CODE and VAL could be implemented. */
4975 loc = asserts_for[SSA_NAME_VERSION (name)];
4976 last_loc = loc;
4977 while (loc)
4979 if (loc->comp_code == comp_code
4980 && (loc->val == val
4981 || operand_equal_p (loc->val, val, 0))
4982 && (loc->expr == expr
4983 || operand_equal_p (loc->expr, expr, 0)))
4985 /* If E is not a critical edge and DEST_BB
4986 dominates the existing location for the assertion, move
4987 the assertion up in the dominance tree by updating its
4988 location information. */
4989 if ((e == NULL || !EDGE_CRITICAL_P (e))
4990 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4992 loc->bb = dest_bb;
4993 loc->e = e;
4994 loc->si = si;
4995 return;
4999 /* Update the last node of the list and move to the next one. */
5000 last_loc = loc;
5001 loc = loc->next;
5004 /* If we didn't find an assertion already registered for
5005 NAME COMP_CODE VAL, add a new one at the end of the list of
5006 assertions associated with NAME. */
5007 n = XNEW (struct assert_locus);
5008 n->bb = dest_bb;
5009 n->e = e;
5010 n->si = si;
5011 n->comp_code = comp_code;
5012 n->val = val;
5013 n->expr = expr;
5014 n->next = NULL;
5016 if (last_loc)
5017 last_loc->next = n;
5018 else
5019 asserts_for[SSA_NAME_VERSION (name)] = n;
5021 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5024 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5025 Extract a suitable test code and value and store them into *CODE_P and
5026 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5028 If no extraction was possible, return FALSE, otherwise return TRUE.
5030 If INVERT is true, then we invert the result stored into *CODE_P. */
5032 static bool
5033 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5034 tree cond_op0, tree cond_op1,
5035 bool invert, enum tree_code *code_p,
5036 tree *val_p)
5038 enum tree_code comp_code;
5039 tree val;
5041 /* Otherwise, we have a comparison of the form NAME COMP VAL
5042 or VAL COMP NAME. */
5043 if (name == cond_op1)
5045 /* If the predicate is of the form VAL COMP NAME, flip
5046 COMP around because we need to register NAME as the
5047 first operand in the predicate. */
5048 comp_code = swap_tree_comparison (cond_code);
5049 val = cond_op0;
5051 else if (name == cond_op0)
5053 /* The comparison is of the form NAME COMP VAL, so the
5054 comparison code remains unchanged. */
5055 comp_code = cond_code;
5056 val = cond_op1;
5058 else
5059 gcc_unreachable ();
5061 /* Invert the comparison code as necessary. */
5062 if (invert)
5063 comp_code = invert_tree_comparison (comp_code, 0);
5065 /* VRP only handles integral and pointer types. */
5066 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5067 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5068 return false;
5070 /* Do not register always-false predicates.
5071 FIXME: this works around a limitation in fold() when dealing with
5072 enumerations. Given 'enum { N1, N2 } x;', fold will not
5073 fold 'if (x > N2)' to 'if (0)'. */
5074 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5075 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5077 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5078 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5080 if (comp_code == GT_EXPR
5081 && (!max
5082 || compare_values (val, max) == 0))
5083 return false;
5085 if (comp_code == LT_EXPR
5086 && (!min
5087 || compare_values (val, min) == 0))
5088 return false;
5090 *code_p = comp_code;
5091 *val_p = val;
5092 return true;
5095 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5096 (otherwise return VAL). VAL and MASK must be zero-extended for
5097 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5098 (to transform signed values into unsigned) and at the end xor
5099 SGNBIT back. */
5101 static wide_int
5102 masked_increment (const wide_int &val_in, const wide_int &mask,
5103 const wide_int &sgnbit, unsigned int prec)
5105 wide_int bit = wi::one (prec), res;
5106 unsigned int i;
5108 wide_int val = val_in ^ sgnbit;
5109 for (i = 0; i < prec; i++, bit += bit)
5111 res = mask;
5112 if ((res & bit) == 0)
5113 continue;
5114 res = bit - 1;
5115 res = (val + bit).and_not (res);
5116 res &= mask;
5117 if (wi::gtu_p (res, val))
5118 return res ^ sgnbit;
5120 return val ^ sgnbit;
5123 /* Try to register an edge assertion for SSA name NAME on edge E for
5124 the condition COND contributing to the conditional jump pointed to by BSI.
5125 Invert the condition COND if INVERT is true. */
5127 static void
5128 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5129 enum tree_code cond_code,
5130 tree cond_op0, tree cond_op1, bool invert)
5132 tree val;
5133 enum tree_code comp_code;
5135 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5136 cond_op0,
5137 cond_op1,
5138 invert, &comp_code, &val))
5139 return;
5141 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5142 reachable from E. */
5143 if (live_on_edge (e, name))
5144 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5146 /* In the case of NAME <= CST and NAME being defined as
5147 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5148 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5149 This catches range and anti-range tests. */
5150 if ((comp_code == LE_EXPR
5151 || comp_code == GT_EXPR)
5152 && TREE_CODE (val) == INTEGER_CST
5153 && TYPE_UNSIGNED (TREE_TYPE (val)))
5155 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5156 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5158 /* Extract CST2 from the (optional) addition. */
5159 if (is_gimple_assign (def_stmt)
5160 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5162 name2 = gimple_assign_rhs1 (def_stmt);
5163 cst2 = gimple_assign_rhs2 (def_stmt);
5164 if (TREE_CODE (name2) == SSA_NAME
5165 && TREE_CODE (cst2) == INTEGER_CST)
5166 def_stmt = SSA_NAME_DEF_STMT (name2);
5169 /* Extract NAME2 from the (optional) sign-changing cast. */
5170 if (gimple_assign_cast_p (def_stmt))
5172 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5173 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5174 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5175 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5176 name3 = gimple_assign_rhs1 (def_stmt);
5179 /* If name3 is used later, create an ASSERT_EXPR for it. */
5180 if (name3 != NULL_TREE
5181 && TREE_CODE (name3) == SSA_NAME
5182 && (cst2 == NULL_TREE
5183 || TREE_CODE (cst2) == INTEGER_CST)
5184 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5185 && live_on_edge (e, name3))
5187 tree tmp;
5189 /* Build an expression for the range test. */
5190 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5191 if (cst2 != NULL_TREE)
5192 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5194 if (dump_file)
5196 fprintf (dump_file, "Adding assert for ");
5197 print_generic_expr (dump_file, name3, 0);
5198 fprintf (dump_file, " from ");
5199 print_generic_expr (dump_file, tmp, 0);
5200 fprintf (dump_file, "\n");
5203 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5206 /* If name2 is used later, create an ASSERT_EXPR for it. */
5207 if (name2 != NULL_TREE
5208 && TREE_CODE (name2) == SSA_NAME
5209 && TREE_CODE (cst2) == INTEGER_CST
5210 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5211 && live_on_edge (e, name2))
5213 tree tmp;
5215 /* Build an expression for the range test. */
5216 tmp = name2;
5217 if (TREE_TYPE (name) != TREE_TYPE (name2))
5218 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5219 if (cst2 != NULL_TREE)
5220 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5222 if (dump_file)
5224 fprintf (dump_file, "Adding assert for ");
5225 print_generic_expr (dump_file, name2, 0);
5226 fprintf (dump_file, " from ");
5227 print_generic_expr (dump_file, tmp, 0);
5228 fprintf (dump_file, "\n");
5231 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5235 /* In the case of post-in/decrement tests like if (i++) ... and uses
5236 of the in/decremented value on the edge the extra name we want to
5237 assert for is not on the def chain of the name compared. Instead
5238 it is in the set of use stmts.
5239 Similar cases happen for conversions that were simplified through
5240 fold_{sign_changed,widened}_comparison. */
5241 if ((comp_code == NE_EXPR
5242 || comp_code == EQ_EXPR)
5243 && TREE_CODE (val) == INTEGER_CST)
5245 imm_use_iterator ui;
5246 gimple *use_stmt;
5247 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5249 if (!is_gimple_assign (use_stmt))
5250 continue;
5252 /* Cut off to use-stmts that are dominating the predecessor. */
5253 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5254 continue;
5256 tree name2 = gimple_assign_lhs (use_stmt);
5257 if (TREE_CODE (name2) != SSA_NAME
5258 || !live_on_edge (e, name2))
5259 continue;
5261 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5262 tree cst;
5263 if (code == PLUS_EXPR
5264 || code == MINUS_EXPR)
5266 cst = gimple_assign_rhs2 (use_stmt);
5267 if (TREE_CODE (cst) != INTEGER_CST)
5268 continue;
5269 cst = int_const_binop (code, val, cst);
5271 else if (CONVERT_EXPR_CODE_P (code))
5273 /* For truncating conversions we cannot record
5274 an inequality. */
5275 if (comp_code == NE_EXPR
5276 && (TYPE_PRECISION (TREE_TYPE (name2))
5277 < TYPE_PRECISION (TREE_TYPE (name))))
5278 continue;
5279 cst = fold_convert (TREE_TYPE (name2), val);
5281 else
5282 continue;
5284 if (TREE_OVERFLOW_P (cst))
5285 cst = drop_tree_overflow (cst);
5286 register_new_assert_for (name2, name2, comp_code, cst,
5287 NULL, e, bsi);
5291 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5292 && TREE_CODE (val) == INTEGER_CST)
5294 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5295 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5296 tree val2 = NULL_TREE;
5297 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5298 wide_int mask = wi::zero (prec);
5299 unsigned int nprec = prec;
5300 enum tree_code rhs_code = ERROR_MARK;
5302 if (is_gimple_assign (def_stmt))
5303 rhs_code = gimple_assign_rhs_code (def_stmt);
5305 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5306 assert that A != CST1 -+ CST2. */
5307 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5308 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5310 tree op0 = gimple_assign_rhs1 (def_stmt);
5311 tree op1 = gimple_assign_rhs2 (def_stmt);
5312 if (TREE_CODE (op0) == SSA_NAME
5313 && TREE_CODE (op1) == INTEGER_CST
5314 && live_on_edge (e, op0))
5316 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5317 ? MINUS_EXPR : PLUS_EXPR);
5318 op1 = int_const_binop (reverse_op, val, op1);
5319 if (TREE_OVERFLOW (op1))
5320 op1 = drop_tree_overflow (op1);
5321 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5325 /* Add asserts for NAME cmp CST and NAME being defined
5326 as NAME = (int) NAME2. */
5327 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5328 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5329 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5330 && gimple_assign_cast_p (def_stmt))
5332 name2 = gimple_assign_rhs1 (def_stmt);
5333 if (CONVERT_EXPR_CODE_P (rhs_code)
5334 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5335 && TYPE_UNSIGNED (TREE_TYPE (name2))
5336 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5337 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5338 || !tree_int_cst_equal (val,
5339 TYPE_MIN_VALUE (TREE_TYPE (val))))
5340 && live_on_edge (e, name2))
5342 tree tmp, cst;
5343 enum tree_code new_comp_code = comp_code;
5345 cst = fold_convert (TREE_TYPE (name2),
5346 TYPE_MIN_VALUE (TREE_TYPE (val)));
5347 /* Build an expression for the range test. */
5348 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5349 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5350 fold_convert (TREE_TYPE (name2), val));
5351 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5353 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5354 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5355 build_int_cst (TREE_TYPE (name2), 1));
5358 if (dump_file)
5360 fprintf (dump_file, "Adding assert for ");
5361 print_generic_expr (dump_file, name2, 0);
5362 fprintf (dump_file, " from ");
5363 print_generic_expr (dump_file, tmp, 0);
5364 fprintf (dump_file, "\n");
5367 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5368 e, bsi);
5372 /* Add asserts for NAME cmp CST and NAME being defined as
5373 NAME = NAME2 >> CST2.
5375 Extract CST2 from the right shift. */
5376 if (rhs_code == RSHIFT_EXPR)
5378 name2 = gimple_assign_rhs1 (def_stmt);
5379 cst2 = gimple_assign_rhs2 (def_stmt);
5380 if (TREE_CODE (name2) == SSA_NAME
5381 && tree_fits_uhwi_p (cst2)
5382 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5383 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5384 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5385 && live_on_edge (e, name2))
5387 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5388 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5391 if (val2 != NULL_TREE
5392 && TREE_CODE (val2) == INTEGER_CST
5393 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5394 TREE_TYPE (val),
5395 val2, cst2), val))
5397 enum tree_code new_comp_code = comp_code;
5398 tree tmp, new_val;
5400 tmp = name2;
5401 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5403 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5405 tree type = build_nonstandard_integer_type (prec, 1);
5406 tmp = build1 (NOP_EXPR, type, name2);
5407 val2 = fold_convert (type, val2);
5409 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5410 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5411 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5413 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5415 wide_int minval
5416 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5417 new_val = val2;
5418 if (minval == new_val)
5419 new_val = NULL_TREE;
5421 else
5423 wide_int maxval
5424 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5425 mask |= val2;
5426 if (mask == maxval)
5427 new_val = NULL_TREE;
5428 else
5429 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5432 if (new_val)
5434 if (dump_file)
5436 fprintf (dump_file, "Adding assert for ");
5437 print_generic_expr (dump_file, name2, 0);
5438 fprintf (dump_file, " from ");
5439 print_generic_expr (dump_file, tmp, 0);
5440 fprintf (dump_file, "\n");
5443 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5444 NULL, e, bsi);
5448 /* Add asserts for NAME cmp CST and NAME being defined as
5449 NAME = NAME2 & CST2.
5451 Extract CST2 from the and.
5453 Also handle
5454 NAME = (unsigned) NAME2;
5455 casts where NAME's type is unsigned and has smaller precision
5456 than NAME2's type as if it was NAME = NAME2 & MASK. */
5457 names[0] = NULL_TREE;
5458 names[1] = NULL_TREE;
5459 cst2 = NULL_TREE;
5460 if (rhs_code == BIT_AND_EXPR
5461 || (CONVERT_EXPR_CODE_P (rhs_code)
5462 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5463 && TYPE_UNSIGNED (TREE_TYPE (val))
5464 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5465 > prec))
5467 name2 = gimple_assign_rhs1 (def_stmt);
5468 if (rhs_code == BIT_AND_EXPR)
5469 cst2 = gimple_assign_rhs2 (def_stmt);
5470 else
5472 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5473 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5475 if (TREE_CODE (name2) == SSA_NAME
5476 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5477 && TREE_CODE (cst2) == INTEGER_CST
5478 && !integer_zerop (cst2)
5479 && (nprec > 1
5480 || TYPE_UNSIGNED (TREE_TYPE (val))))
5482 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5483 if (gimple_assign_cast_p (def_stmt2))
5485 names[1] = gimple_assign_rhs1 (def_stmt2);
5486 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5487 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5488 || (TYPE_PRECISION (TREE_TYPE (name2))
5489 != TYPE_PRECISION (TREE_TYPE (names[1])))
5490 || !live_on_edge (e, names[1]))
5491 names[1] = NULL_TREE;
5493 if (live_on_edge (e, name2))
5494 names[0] = name2;
5497 if (names[0] || names[1])
5499 wide_int minv, maxv, valv, cst2v;
5500 wide_int tem, sgnbit;
5501 bool valid_p = false, valn, cst2n;
5502 enum tree_code ccode = comp_code;
5504 valv = wide_int::from (val, nprec, UNSIGNED);
5505 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5506 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5507 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5508 /* If CST2 doesn't have most significant bit set,
5509 but VAL is negative, we have comparison like
5510 if ((x & 0x123) > -4) (always true). Just give up. */
5511 if (!cst2n && valn)
5512 ccode = ERROR_MARK;
5513 if (cst2n)
5514 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5515 else
5516 sgnbit = wi::zero (nprec);
5517 minv = valv & cst2v;
5518 switch (ccode)
5520 case EQ_EXPR:
5521 /* Minimum unsigned value for equality is VAL & CST2
5522 (should be equal to VAL, otherwise we probably should
5523 have folded the comparison into false) and
5524 maximum unsigned value is VAL | ~CST2. */
5525 maxv = valv | ~cst2v;
5526 valid_p = true;
5527 break;
5529 case NE_EXPR:
5530 tem = valv | ~cst2v;
5531 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5532 if (valv == 0)
5534 cst2n = false;
5535 sgnbit = wi::zero (nprec);
5536 goto gt_expr;
5538 /* If (VAL | ~CST2) is all ones, handle it as
5539 (X & CST2) < VAL. */
5540 if (tem == -1)
5542 cst2n = false;
5543 valn = false;
5544 sgnbit = wi::zero (nprec);
5545 goto lt_expr;
5547 if (!cst2n && wi::neg_p (cst2v))
5548 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5549 if (sgnbit != 0)
5551 if (valv == sgnbit)
5553 cst2n = true;
5554 valn = true;
5555 goto gt_expr;
5557 if (tem == wi::mask (nprec - 1, false, nprec))
5559 cst2n = true;
5560 goto lt_expr;
5562 if (!cst2n)
5563 sgnbit = wi::zero (nprec);
5565 break;
5567 case GE_EXPR:
5568 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5569 is VAL and maximum unsigned value is ~0. For signed
5570 comparison, if CST2 doesn't have most significant bit
5571 set, handle it similarly. If CST2 has MSB set,
5572 the minimum is the same, and maximum is ~0U/2. */
5573 if (minv != valv)
5575 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5576 VAL. */
5577 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5578 if (minv == valv)
5579 break;
5581 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5582 valid_p = true;
5583 break;
5585 case GT_EXPR:
5586 gt_expr:
5587 /* Find out smallest MINV where MINV > VAL
5588 && (MINV & CST2) == MINV, if any. If VAL is signed and
5589 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5590 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5591 if (minv == valv)
5592 break;
5593 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5594 valid_p = true;
5595 break;
5597 case LE_EXPR:
5598 /* Minimum unsigned value for <= is 0 and maximum
5599 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5600 Otherwise, find smallest VAL2 where VAL2 > VAL
5601 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5602 as maximum.
5603 For signed comparison, if CST2 doesn't have most
5604 significant bit set, handle it similarly. If CST2 has
5605 MSB set, the maximum is the same and minimum is INT_MIN. */
5606 if (minv == valv)
5607 maxv = valv;
5608 else
5610 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5611 if (maxv == valv)
5612 break;
5613 maxv -= 1;
5615 maxv |= ~cst2v;
5616 minv = sgnbit;
5617 valid_p = true;
5618 break;
5620 case LT_EXPR:
5621 lt_expr:
5622 /* Minimum unsigned value for < is 0 and maximum
5623 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5624 Otherwise, find smallest VAL2 where VAL2 > VAL
5625 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5626 as maximum.
5627 For signed comparison, if CST2 doesn't have most
5628 significant bit set, handle it similarly. If CST2 has
5629 MSB set, the maximum is the same and minimum is INT_MIN. */
5630 if (minv == valv)
5632 if (valv == sgnbit)
5633 break;
5634 maxv = valv;
5636 else
5638 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5639 if (maxv == valv)
5640 break;
5642 maxv -= 1;
5643 maxv |= ~cst2v;
5644 minv = sgnbit;
5645 valid_p = true;
5646 break;
5648 default:
5649 break;
5651 if (valid_p
5652 && (maxv - minv) != -1)
5654 tree tmp, new_val, type;
5655 int i;
5657 for (i = 0; i < 2; i++)
5658 if (names[i])
5660 wide_int maxv2 = maxv;
5661 tmp = names[i];
5662 type = TREE_TYPE (names[i]);
5663 if (!TYPE_UNSIGNED (type))
5665 type = build_nonstandard_integer_type (nprec, 1);
5666 tmp = build1 (NOP_EXPR, type, names[i]);
5668 if (minv != 0)
5670 tmp = build2 (PLUS_EXPR, type, tmp,
5671 wide_int_to_tree (type, -minv));
5672 maxv2 = maxv - minv;
5674 new_val = wide_int_to_tree (type, maxv2);
5676 if (dump_file)
5678 fprintf (dump_file, "Adding assert for ");
5679 print_generic_expr (dump_file, names[i], 0);
5680 fprintf (dump_file, " from ");
5681 print_generic_expr (dump_file, tmp, 0);
5682 fprintf (dump_file, "\n");
5685 register_new_assert_for (names[i], tmp, LE_EXPR,
5686 new_val, NULL, e, bsi);
5693 /* OP is an operand of a truth value expression which is known to have
5694 a particular value. Register any asserts for OP and for any
5695 operands in OP's defining statement.
5697 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5698 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5700 static void
5701 register_edge_assert_for_1 (tree op, enum tree_code code,
5702 edge e, gimple_stmt_iterator bsi)
5704 gimple *op_def;
5705 tree val;
5706 enum tree_code rhs_code;
5708 /* We only care about SSA_NAMEs. */
5709 if (TREE_CODE (op) != SSA_NAME)
5710 return;
5712 /* We know that OP will have a zero or nonzero value. If OP is used
5713 more than once go ahead and register an assert for OP. */
5714 if (live_on_edge (e, op))
5716 val = build_int_cst (TREE_TYPE (op), 0);
5717 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5720 /* Now look at how OP is set. If it's set from a comparison,
5721 a truth operation or some bit operations, then we may be able
5722 to register information about the operands of that assignment. */
5723 op_def = SSA_NAME_DEF_STMT (op);
5724 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5725 return;
5727 rhs_code = gimple_assign_rhs_code (op_def);
5729 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5731 bool invert = (code == EQ_EXPR ? true : false);
5732 tree op0 = gimple_assign_rhs1 (op_def);
5733 tree op1 = gimple_assign_rhs2 (op_def);
5735 if (TREE_CODE (op0) == SSA_NAME)
5736 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5737 if (TREE_CODE (op1) == SSA_NAME)
5738 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5740 else if ((code == NE_EXPR
5741 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5742 || (code == EQ_EXPR
5743 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5745 /* Recurse on each operand. */
5746 tree op0 = gimple_assign_rhs1 (op_def);
5747 tree op1 = gimple_assign_rhs2 (op_def);
5748 if (TREE_CODE (op0) == SSA_NAME
5749 && has_single_use (op0))
5750 register_edge_assert_for_1 (op0, code, e, bsi);
5751 if (TREE_CODE (op1) == SSA_NAME
5752 && has_single_use (op1))
5753 register_edge_assert_for_1 (op1, code, e, bsi);
5755 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5756 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5758 /* Recurse, flipping CODE. */
5759 code = invert_tree_comparison (code, false);
5760 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5762 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5764 /* Recurse through the copy. */
5765 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5767 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5769 /* Recurse through the type conversion, unless it is a narrowing
5770 conversion or conversion from non-integral type. */
5771 tree rhs = gimple_assign_rhs1 (op_def);
5772 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5773 && (TYPE_PRECISION (TREE_TYPE (rhs))
5774 <= TYPE_PRECISION (TREE_TYPE (op))))
5775 register_edge_assert_for_1 (rhs, code, e, bsi);
5779 /* Try to register an edge assertion for SSA name NAME on edge E for
5780 the condition COND contributing to the conditional jump pointed to by
5781 SI. */
5783 static void
5784 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5785 enum tree_code cond_code, tree cond_op0,
5786 tree cond_op1)
5788 tree val;
5789 enum tree_code comp_code;
5790 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5792 /* Do not attempt to infer anything in names that flow through
5793 abnormal edges. */
5794 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5795 return;
5797 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5798 cond_op0, cond_op1,
5799 is_else_edge,
5800 &comp_code, &val))
5801 return;
5803 /* Register ASSERT_EXPRs for name. */
5804 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5805 cond_op1, is_else_edge);
5808 /* If COND is effectively an equality test of an SSA_NAME against
5809 the value zero or one, then we may be able to assert values
5810 for SSA_NAMEs which flow into COND. */
5812 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5813 statement of NAME we can assert both operands of the BIT_AND_EXPR
5814 have nonzero value. */
5815 if (((comp_code == EQ_EXPR && integer_onep (val))
5816 || (comp_code == NE_EXPR && integer_zerop (val))))
5818 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5820 if (is_gimple_assign (def_stmt)
5821 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5823 tree op0 = gimple_assign_rhs1 (def_stmt);
5824 tree op1 = gimple_assign_rhs2 (def_stmt);
5825 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5826 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5830 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5831 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5832 have zero value. */
5833 if (((comp_code == EQ_EXPR && integer_zerop (val))
5834 || (comp_code == NE_EXPR && integer_onep (val))))
5836 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5838 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5839 necessarily zero value, or if type-precision is one. */
5840 if (is_gimple_assign (def_stmt)
5841 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5842 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5843 || comp_code == EQ_EXPR)))
5845 tree op0 = gimple_assign_rhs1 (def_stmt);
5846 tree op1 = gimple_assign_rhs2 (def_stmt);
5847 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5848 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5854 /* Determine whether the outgoing edges of BB should receive an
5855 ASSERT_EXPR for each of the operands of BB's LAST statement.
5856 The last statement of BB must be a COND_EXPR.
5858 If any of the sub-graphs rooted at BB have an interesting use of
5859 the predicate operands, an assert location node is added to the
5860 list of assertions for the corresponding operands. */
5862 static void
5863 find_conditional_asserts (basic_block bb, gcond *last)
5865 gimple_stmt_iterator bsi;
5866 tree op;
5867 edge_iterator ei;
5868 edge e;
5869 ssa_op_iter iter;
5871 bsi = gsi_for_stmt (last);
5873 /* Look for uses of the operands in each of the sub-graphs
5874 rooted at BB. We need to check each of the outgoing edges
5875 separately, so that we know what kind of ASSERT_EXPR to
5876 insert. */
5877 FOR_EACH_EDGE (e, ei, bb->succs)
5879 if (e->dest == bb)
5880 continue;
5882 /* Register the necessary assertions for each operand in the
5883 conditional predicate. */
5884 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5885 register_edge_assert_for (op, e, bsi,
5886 gimple_cond_code (last),
5887 gimple_cond_lhs (last),
5888 gimple_cond_rhs (last));
5892 struct case_info
5894 tree expr;
5895 basic_block bb;
5898 /* Compare two case labels sorting first by the destination bb index
5899 and then by the case value. */
5901 static int
5902 compare_case_labels (const void *p1, const void *p2)
5904 const struct case_info *ci1 = (const struct case_info *) p1;
5905 const struct case_info *ci2 = (const struct case_info *) p2;
5906 int idx1 = ci1->bb->index;
5907 int idx2 = ci2->bb->index;
5909 if (idx1 < idx2)
5910 return -1;
5911 else if (idx1 == idx2)
5913 /* Make sure the default label is first in a group. */
5914 if (!CASE_LOW (ci1->expr))
5915 return -1;
5916 else if (!CASE_LOW (ci2->expr))
5917 return 1;
5918 else
5919 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5920 CASE_LOW (ci2->expr));
5922 else
5923 return 1;
5926 /* Determine whether the outgoing edges of BB should receive an
5927 ASSERT_EXPR for each of the operands of BB's LAST statement.
5928 The last statement of BB must be a SWITCH_EXPR.
5930 If any of the sub-graphs rooted at BB have an interesting use of
5931 the predicate operands, an assert location node is added to the
5932 list of assertions for the corresponding operands. */
5934 static void
5935 find_switch_asserts (basic_block bb, gswitch *last)
5937 gimple_stmt_iterator bsi;
5938 tree op;
5939 edge e;
5940 struct case_info *ci;
5941 size_t n = gimple_switch_num_labels (last);
5942 #if GCC_VERSION >= 4000
5943 unsigned int idx;
5944 #else
5945 /* Work around GCC 3.4 bug (PR 37086). */
5946 volatile unsigned int idx;
5947 #endif
5949 bsi = gsi_for_stmt (last);
5950 op = gimple_switch_index (last);
5951 if (TREE_CODE (op) != SSA_NAME)
5952 return;
5954 /* Build a vector of case labels sorted by destination label. */
5955 ci = XNEWVEC (struct case_info, n);
5956 for (idx = 0; idx < n; ++idx)
5958 ci[idx].expr = gimple_switch_label (last, idx);
5959 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5961 edge default_edge = find_edge (bb, ci[0].bb);
5962 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5964 for (idx = 0; idx < n; ++idx)
5966 tree min, max;
5967 tree cl = ci[idx].expr;
5968 basic_block cbb = ci[idx].bb;
5970 min = CASE_LOW (cl);
5971 max = CASE_HIGH (cl);
5973 /* If there are multiple case labels with the same destination
5974 we need to combine them to a single value range for the edge. */
5975 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5977 /* Skip labels until the last of the group. */
5978 do {
5979 ++idx;
5980 } while (idx < n && cbb == ci[idx].bb);
5981 --idx;
5983 /* Pick up the maximum of the case label range. */
5984 if (CASE_HIGH (ci[idx].expr))
5985 max = CASE_HIGH (ci[idx].expr);
5986 else
5987 max = CASE_LOW (ci[idx].expr);
5990 /* Can't extract a useful assertion out of a range that includes the
5991 default label. */
5992 if (min == NULL_TREE)
5993 continue;
5995 /* Find the edge to register the assert expr on. */
5996 e = find_edge (bb, cbb);
5998 /* Register the necessary assertions for the operand in the
5999 SWITCH_EXPR. */
6000 register_edge_assert_for (op, e, bsi,
6001 max ? GE_EXPR : EQ_EXPR,
6002 op, fold_convert (TREE_TYPE (op), min));
6003 if (max)
6004 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6005 fold_convert (TREE_TYPE (op), max));
6008 XDELETEVEC (ci);
6010 if (!live_on_edge (default_edge, op))
6011 return;
6013 /* Now register along the default label assertions that correspond to the
6014 anti-range of each label. */
6015 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
6016 for (idx = 1; idx < n; idx++)
6018 tree min, max;
6019 tree cl = gimple_switch_label (last, idx);
6021 min = CASE_LOW (cl);
6022 max = CASE_HIGH (cl);
6024 /* Combine contiguous case ranges to reduce the number of assertions
6025 to insert. */
6026 for (idx = idx + 1; idx < n; idx++)
6028 tree next_min, next_max;
6029 tree next_cl = gimple_switch_label (last, idx);
6031 next_min = CASE_LOW (next_cl);
6032 next_max = CASE_HIGH (next_cl);
6034 wide_int difference = wi::sub (next_min, max ? max : min);
6035 if (wi::eq_p (difference, 1))
6036 max = next_max ? next_max : next_min;
6037 else
6038 break;
6040 idx--;
6042 if (max == NULL_TREE)
6044 /* Register the assertion OP != MIN. */
6045 min = fold_convert (TREE_TYPE (op), min);
6046 register_edge_assert_for (op, default_edge, bsi, NE_EXPR, op, min);
6048 else
6050 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6051 which will give OP the anti-range ~[MIN,MAX]. */
6052 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
6053 min = fold_convert (TREE_TYPE (uop), min);
6054 max = fold_convert (TREE_TYPE (uop), max);
6056 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
6057 tree rhs = int_const_binop (MINUS_EXPR, max, min);
6058 register_new_assert_for (op, lhs, GT_EXPR, rhs,
6059 NULL, default_edge, bsi);
6062 if (--insertion_limit == 0)
6063 break;
6068 /* Traverse all the statements in block BB looking for statements that
6069 may generate useful assertions for the SSA names in their operand.
6070 If a statement produces a useful assertion A for name N_i, then the
6071 list of assertions already generated for N_i is scanned to
6072 determine if A is actually needed.
6074 If N_i already had the assertion A at a location dominating the
6075 current location, then nothing needs to be done. Otherwise, the
6076 new location for A is recorded instead.
6078 1- For every statement S in BB, all the variables used by S are
6079 added to bitmap FOUND_IN_SUBGRAPH.
6081 2- If statement S uses an operand N in a way that exposes a known
6082 value range for N, then if N was not already generated by an
6083 ASSERT_EXPR, create a new assert location for N. For instance,
6084 if N is a pointer and the statement dereferences it, we can
6085 assume that N is not NULL.
6087 3- COND_EXPRs are a special case of #2. We can derive range
6088 information from the predicate but need to insert different
6089 ASSERT_EXPRs for each of the sub-graphs rooted at the
6090 conditional block. If the last statement of BB is a conditional
6091 expression of the form 'X op Y', then
6093 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6095 b) If the conditional is the only entry point to the sub-graph
6096 corresponding to the THEN_CLAUSE, recurse into it. On
6097 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6098 an ASSERT_EXPR is added for the corresponding variable.
6100 c) Repeat step (b) on the ELSE_CLAUSE.
6102 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6104 For instance,
6106 if (a == 9)
6107 b = a;
6108 else
6109 b = c + 1;
6111 In this case, an assertion on the THEN clause is useful to
6112 determine that 'a' is always 9 on that edge. However, an assertion
6113 on the ELSE clause would be unnecessary.
6115 4- If BB does not end in a conditional expression, then we recurse
6116 into BB's dominator children.
6118 At the end of the recursive traversal, every SSA name will have a
6119 list of locations where ASSERT_EXPRs should be added. When a new
6120 location for name N is found, it is registered by calling
6121 register_new_assert_for. That function keeps track of all the
6122 registered assertions to prevent adding unnecessary assertions.
6123 For instance, if a pointer P_4 is dereferenced more than once in a
6124 dominator tree, only the location dominating all the dereference of
6125 P_4 will receive an ASSERT_EXPR. */
6127 static void
6128 find_assert_locations_1 (basic_block bb, sbitmap live)
6130 gimple *last;
6132 last = last_stmt (bb);
6134 /* If BB's last statement is a conditional statement involving integer
6135 operands, determine if we need to add ASSERT_EXPRs. */
6136 if (last
6137 && gimple_code (last) == GIMPLE_COND
6138 && !fp_predicate (last)
6139 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6140 find_conditional_asserts (bb, as_a <gcond *> (last));
6142 /* If BB's last statement is a switch statement involving integer
6143 operands, determine if we need to add ASSERT_EXPRs. */
6144 if (last
6145 && gimple_code (last) == GIMPLE_SWITCH
6146 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6147 find_switch_asserts (bb, as_a <gswitch *> (last));
6149 /* Traverse all the statements in BB marking used names and looking
6150 for statements that may infer assertions for their used operands. */
6151 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6152 gsi_prev (&si))
6154 gimple *stmt;
6155 tree op;
6156 ssa_op_iter i;
6158 stmt = gsi_stmt (si);
6160 if (is_gimple_debug (stmt))
6161 continue;
6163 /* See if we can derive an assertion for any of STMT's operands. */
6164 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6166 tree value;
6167 enum tree_code comp_code;
6169 /* If op is not live beyond this stmt, do not bother to insert
6170 asserts for it. */
6171 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6172 continue;
6174 /* If OP is used in such a way that we can infer a value
6175 range for it, and we don't find a previous assertion for
6176 it, create a new assertion location node for OP. */
6177 if (infer_value_range (stmt, op, &comp_code, &value))
6179 /* If we are able to infer a nonzero value range for OP,
6180 then walk backwards through the use-def chain to see if OP
6181 was set via a typecast.
6183 If so, then we can also infer a nonzero value range
6184 for the operand of the NOP_EXPR. */
6185 if (comp_code == NE_EXPR && integer_zerop (value))
6187 tree t = op;
6188 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6190 while (is_gimple_assign (def_stmt)
6191 && CONVERT_EXPR_CODE_P
6192 (gimple_assign_rhs_code (def_stmt))
6193 && TREE_CODE
6194 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6195 && POINTER_TYPE_P
6196 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6198 t = gimple_assign_rhs1 (def_stmt);
6199 def_stmt = SSA_NAME_DEF_STMT (t);
6201 /* Note we want to register the assert for the
6202 operand of the NOP_EXPR after SI, not after the
6203 conversion. */
6204 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6205 register_new_assert_for (t, t, comp_code, value,
6206 bb, NULL, si);
6210 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6214 /* Update live. */
6215 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6216 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6217 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6218 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6221 /* Traverse all PHI nodes in BB, updating live. */
6222 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6223 gsi_next (&si))
6225 use_operand_p arg_p;
6226 ssa_op_iter i;
6227 gphi *phi = si.phi ();
6228 tree res = gimple_phi_result (phi);
6230 if (virtual_operand_p (res))
6231 continue;
6233 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6235 tree arg = USE_FROM_PTR (arg_p);
6236 if (TREE_CODE (arg) == SSA_NAME)
6237 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6240 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6244 /* Do an RPO walk over the function computing SSA name liveness
6245 on-the-fly and deciding on assert expressions to insert. */
6247 static void
6248 find_assert_locations (void)
6250 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6251 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6252 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6253 int rpo_cnt, i;
6255 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6256 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6257 for (i = 0; i < rpo_cnt; ++i)
6258 bb_rpo[rpo[i]] = i;
6260 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6261 the order we compute liveness and insert asserts we otherwise
6262 fail to insert asserts into the loop latch. */
6263 loop_p loop;
6264 FOR_EACH_LOOP (loop, 0)
6266 i = loop->latch->index;
6267 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6268 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6269 !gsi_end_p (gsi); gsi_next (&gsi))
6271 gphi *phi = gsi.phi ();
6272 if (virtual_operand_p (gimple_phi_result (phi)))
6273 continue;
6274 tree arg = gimple_phi_arg_def (phi, j);
6275 if (TREE_CODE (arg) == SSA_NAME)
6277 if (live[i] == NULL)
6279 live[i] = sbitmap_alloc (num_ssa_names);
6280 bitmap_clear (live[i]);
6282 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6287 for (i = rpo_cnt - 1; i >= 0; --i)
6289 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6290 edge e;
6291 edge_iterator ei;
6293 if (!live[rpo[i]])
6295 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6296 bitmap_clear (live[rpo[i]]);
6299 /* Process BB and update the live information with uses in
6300 this block. */
6301 find_assert_locations_1 (bb, live[rpo[i]]);
6303 /* Merge liveness into the predecessor blocks and free it. */
6304 if (!bitmap_empty_p (live[rpo[i]]))
6306 int pred_rpo = i;
6307 FOR_EACH_EDGE (e, ei, bb->preds)
6309 int pred = e->src->index;
6310 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6311 continue;
6313 if (!live[pred])
6315 live[pred] = sbitmap_alloc (num_ssa_names);
6316 bitmap_clear (live[pred]);
6318 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6320 if (bb_rpo[pred] < pred_rpo)
6321 pred_rpo = bb_rpo[pred];
6324 /* Record the RPO number of the last visited block that needs
6325 live information from this block. */
6326 last_rpo[rpo[i]] = pred_rpo;
6328 else
6330 sbitmap_free (live[rpo[i]]);
6331 live[rpo[i]] = NULL;
6334 /* We can free all successors live bitmaps if all their
6335 predecessors have been visited already. */
6336 FOR_EACH_EDGE (e, ei, bb->succs)
6337 if (last_rpo[e->dest->index] == i
6338 && live[e->dest->index])
6340 sbitmap_free (live[e->dest->index]);
6341 live[e->dest->index] = NULL;
6345 XDELETEVEC (rpo);
6346 XDELETEVEC (bb_rpo);
6347 XDELETEVEC (last_rpo);
6348 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6349 if (live[i])
6350 sbitmap_free (live[i]);
6351 XDELETEVEC (live);
6354 /* Create an ASSERT_EXPR for NAME and insert it in the location
6355 indicated by LOC. Return true if we made any edge insertions. */
6357 static bool
6358 process_assert_insertions_for (tree name, assert_locus *loc)
6360 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6361 gimple *stmt;
6362 tree cond;
6363 gimple *assert_stmt;
6364 edge_iterator ei;
6365 edge e;
6367 /* If we have X <=> X do not insert an assert expr for that. */
6368 if (loc->expr == loc->val)
6369 return false;
6371 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6372 assert_stmt = build_assert_expr_for (cond, name);
6373 if (loc->e)
6375 /* We have been asked to insert the assertion on an edge. This
6376 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6377 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6378 || (gimple_code (gsi_stmt (loc->si))
6379 == GIMPLE_SWITCH));
6381 gsi_insert_on_edge (loc->e, assert_stmt);
6382 return true;
6385 /* Otherwise, we can insert right after LOC->SI iff the
6386 statement must not be the last statement in the block. */
6387 stmt = gsi_stmt (loc->si);
6388 if (!stmt_ends_bb_p (stmt))
6390 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6391 return false;
6394 /* If STMT must be the last statement in BB, we can only insert new
6395 assertions on the non-abnormal edge out of BB. Note that since
6396 STMT is not control flow, there may only be one non-abnormal/eh edge
6397 out of BB. */
6398 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6399 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
6401 gsi_insert_on_edge (e, assert_stmt);
6402 return true;
6405 gcc_unreachable ();
6409 /* Process all the insertions registered for every name N_i registered
6410 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6411 found in ASSERTS_FOR[i]. */
6413 static void
6414 process_assert_insertions (void)
6416 unsigned i;
6417 bitmap_iterator bi;
6418 bool update_edges_p = false;
6419 int num_asserts = 0;
6421 if (dump_file && (dump_flags & TDF_DETAILS))
6422 dump_all_asserts (dump_file);
6424 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6426 assert_locus *loc = asserts_for[i];
6427 gcc_assert (loc);
6429 while (loc)
6431 assert_locus *next = loc->next;
6432 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6433 free (loc);
6434 loc = next;
6435 num_asserts++;
6439 if (update_edges_p)
6440 gsi_commit_edge_inserts ();
6442 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6443 num_asserts);
6447 /* Traverse the flowgraph looking for conditional jumps to insert range
6448 expressions. These range expressions are meant to provide information
6449 to optimizations that need to reason in terms of value ranges. They
6450 will not be expanded into RTL. For instance, given:
6452 x = ...
6453 y = ...
6454 if (x < y)
6455 y = x - 2;
6456 else
6457 x = y + 3;
6459 this pass will transform the code into:
6461 x = ...
6462 y = ...
6463 if (x < y)
6465 x = ASSERT_EXPR <x, x < y>
6466 y = x - 2
6468 else
6470 y = ASSERT_EXPR <y, x >= y>
6471 x = y + 3
6474 The idea is that once copy and constant propagation have run, other
6475 optimizations will be able to determine what ranges of values can 'x'
6476 take in different paths of the code, simply by checking the reaching
6477 definition of 'x'. */
6479 static void
6480 insert_range_assertions (void)
6482 need_assert_for = BITMAP_ALLOC (NULL);
6483 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6485 calculate_dominance_info (CDI_DOMINATORS);
6487 find_assert_locations ();
6488 if (!bitmap_empty_p (need_assert_for))
6490 process_assert_insertions ();
6491 update_ssa (TODO_update_ssa_no_phi);
6494 if (dump_file && (dump_flags & TDF_DETAILS))
6496 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6497 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6500 free (asserts_for);
6501 BITMAP_FREE (need_assert_for);
6504 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6505 and "struct" hacks. If VRP can determine that the
6506 array subscript is a constant, check if it is outside valid
6507 range. If the array subscript is a RANGE, warn if it is
6508 non-overlapping with valid range.
6509 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6511 static void
6512 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6514 value_range *vr = NULL;
6515 tree low_sub, up_sub;
6516 tree low_bound, up_bound, up_bound_p1;
6518 if (TREE_NO_WARNING (ref))
6519 return;
6521 low_sub = up_sub = TREE_OPERAND (ref, 1);
6522 up_bound = array_ref_up_bound (ref);
6524 /* Can not check flexible arrays. */
6525 if (!up_bound
6526 || TREE_CODE (up_bound) != INTEGER_CST)
6527 return;
6529 /* Accesses to trailing arrays via pointers may access storage
6530 beyond the types array bounds. */
6531 if (warn_array_bounds < 2
6532 && array_at_struct_end_p (ref))
6533 return;
6535 low_bound = array_ref_low_bound (ref);
6536 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6537 build_int_cst (TREE_TYPE (up_bound), 1));
6539 /* Empty array. */
6540 if (tree_int_cst_equal (low_bound, up_bound_p1))
6542 warning_at (location, OPT_Warray_bounds,
6543 "array subscript is above array bounds");
6544 TREE_NO_WARNING (ref) = 1;
6547 if (TREE_CODE (low_sub) == SSA_NAME)
6549 vr = get_value_range (low_sub);
6550 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6552 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6553 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6557 if (vr && vr->type == VR_ANTI_RANGE)
6559 if (TREE_CODE (up_sub) == INTEGER_CST
6560 && (ignore_off_by_one
6561 ? tree_int_cst_lt (up_bound, up_sub)
6562 : tree_int_cst_le (up_bound, up_sub))
6563 && TREE_CODE (low_sub) == INTEGER_CST
6564 && tree_int_cst_le (low_sub, low_bound))
6566 warning_at (location, OPT_Warray_bounds,
6567 "array subscript is outside array bounds");
6568 TREE_NO_WARNING (ref) = 1;
6571 else if (TREE_CODE (up_sub) == INTEGER_CST
6572 && (ignore_off_by_one
6573 ? !tree_int_cst_le (up_sub, up_bound_p1)
6574 : !tree_int_cst_le (up_sub, up_bound)))
6576 if (dump_file && (dump_flags & TDF_DETAILS))
6578 fprintf (dump_file, "Array bound warning for ");
6579 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6580 fprintf (dump_file, "\n");
6582 warning_at (location, OPT_Warray_bounds,
6583 "array subscript is above array bounds");
6584 TREE_NO_WARNING (ref) = 1;
6586 else if (TREE_CODE (low_sub) == INTEGER_CST
6587 && tree_int_cst_lt (low_sub, low_bound))
6589 if (dump_file && (dump_flags & TDF_DETAILS))
6591 fprintf (dump_file, "Array bound warning for ");
6592 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6593 fprintf (dump_file, "\n");
6595 warning_at (location, OPT_Warray_bounds,
6596 "array subscript is below array bounds");
6597 TREE_NO_WARNING (ref) = 1;
6601 /* Searches if the expr T, located at LOCATION computes
6602 address of an ARRAY_REF, and call check_array_ref on it. */
6604 static void
6605 search_for_addr_array (tree t, location_t location)
6607 /* Check each ARRAY_REFs in the reference chain. */
6610 if (TREE_CODE (t) == ARRAY_REF)
6611 check_array_ref (location, t, true /*ignore_off_by_one*/);
6613 t = TREE_OPERAND (t, 0);
6615 while (handled_component_p (t));
6617 if (TREE_CODE (t) == MEM_REF
6618 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6619 && !TREE_NO_WARNING (t))
6621 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6622 tree low_bound, up_bound, el_sz;
6623 offset_int idx;
6624 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6625 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6626 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6627 return;
6629 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6630 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6631 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6632 if (!low_bound
6633 || TREE_CODE (low_bound) != INTEGER_CST
6634 || !up_bound
6635 || TREE_CODE (up_bound) != INTEGER_CST
6636 || !el_sz
6637 || TREE_CODE (el_sz) != INTEGER_CST)
6638 return;
6640 idx = mem_ref_offset (t);
6641 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6642 if (idx < 0)
6644 if (dump_file && (dump_flags & TDF_DETAILS))
6646 fprintf (dump_file, "Array bound warning for ");
6647 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6648 fprintf (dump_file, "\n");
6650 warning_at (location, OPT_Warray_bounds,
6651 "array subscript is below array bounds");
6652 TREE_NO_WARNING (t) = 1;
6654 else if (idx > (wi::to_offset (up_bound)
6655 - wi::to_offset (low_bound) + 1))
6657 if (dump_file && (dump_flags & TDF_DETAILS))
6659 fprintf (dump_file, "Array bound warning for ");
6660 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6661 fprintf (dump_file, "\n");
6663 warning_at (location, OPT_Warray_bounds,
6664 "array subscript is above array bounds");
6665 TREE_NO_WARNING (t) = 1;
6670 /* walk_tree() callback that checks if *TP is
6671 an ARRAY_REF inside an ADDR_EXPR (in which an array
6672 subscript one outside the valid range is allowed). Call
6673 check_array_ref for each ARRAY_REF found. The location is
6674 passed in DATA. */
6676 static tree
6677 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6679 tree t = *tp;
6680 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6681 location_t location;
6683 if (EXPR_HAS_LOCATION (t))
6684 location = EXPR_LOCATION (t);
6685 else
6687 location_t *locp = (location_t *) wi->info;
6688 location = *locp;
6691 *walk_subtree = TRUE;
6693 if (TREE_CODE (t) == ARRAY_REF)
6694 check_array_ref (location, t, false /*ignore_off_by_one*/);
6696 else if (TREE_CODE (t) == ADDR_EXPR)
6698 search_for_addr_array (t, location);
6699 *walk_subtree = FALSE;
6702 return NULL_TREE;
6705 /* Walk over all statements of all reachable BBs and call check_array_bounds
6706 on them. */
6708 static void
6709 check_all_array_refs (void)
6711 basic_block bb;
6712 gimple_stmt_iterator si;
6714 FOR_EACH_BB_FN (bb, cfun)
6716 edge_iterator ei;
6717 edge e;
6718 bool executable = false;
6720 /* Skip blocks that were found to be unreachable. */
6721 FOR_EACH_EDGE (e, ei, bb->preds)
6722 executable |= !!(e->flags & EDGE_EXECUTABLE);
6723 if (!executable)
6724 continue;
6726 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6728 gimple *stmt = gsi_stmt (si);
6729 struct walk_stmt_info wi;
6730 if (!gimple_has_location (stmt)
6731 || is_gimple_debug (stmt))
6732 continue;
6734 memset (&wi, 0, sizeof (wi));
6736 location_t loc = gimple_location (stmt);
6737 wi.info = &loc;
6739 walk_gimple_op (gsi_stmt (si),
6740 check_array_bounds,
6741 &wi);
6746 /* Return true if all imm uses of VAR are either in STMT, or
6747 feed (optionally through a chain of single imm uses) GIMPLE_COND
6748 in basic block COND_BB. */
6750 static bool
6751 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6753 use_operand_p use_p, use2_p;
6754 imm_use_iterator iter;
6756 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6757 if (USE_STMT (use_p) != stmt)
6759 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6760 if (is_gimple_debug (use_stmt))
6761 continue;
6762 while (is_gimple_assign (use_stmt)
6763 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6764 && single_imm_use (gimple_assign_lhs (use_stmt),
6765 &use2_p, &use_stmt2))
6766 use_stmt = use_stmt2;
6767 if (gimple_code (use_stmt) != GIMPLE_COND
6768 || gimple_bb (use_stmt) != cond_bb)
6769 return false;
6771 return true;
6774 /* Handle
6775 _4 = x_3 & 31;
6776 if (_4 != 0)
6777 goto <bb 6>;
6778 else
6779 goto <bb 7>;
6780 <bb 6>:
6781 __builtin_unreachable ();
6782 <bb 7>:
6783 x_5 = ASSERT_EXPR <x_3, ...>;
6784 If x_3 has no other immediate uses (checked by caller),
6785 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6786 from the non-zero bitmask. */
6788 static void
6789 maybe_set_nonzero_bits (basic_block bb, tree var)
6791 edge e = single_pred_edge (bb);
6792 basic_block cond_bb = e->src;
6793 gimple *stmt = last_stmt (cond_bb);
6794 tree cst;
6796 if (stmt == NULL
6797 || gimple_code (stmt) != GIMPLE_COND
6798 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6799 ? EQ_EXPR : NE_EXPR)
6800 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6801 || !integer_zerop (gimple_cond_rhs (stmt)))
6802 return;
6804 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6805 if (!is_gimple_assign (stmt)
6806 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6807 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6808 return;
6809 if (gimple_assign_rhs1 (stmt) != var)
6811 gimple *stmt2;
6813 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6814 return;
6815 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6816 if (!gimple_assign_cast_p (stmt2)
6817 || gimple_assign_rhs1 (stmt2) != var
6818 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6819 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6820 != TYPE_PRECISION (TREE_TYPE (var))))
6821 return;
6823 cst = gimple_assign_rhs2 (stmt);
6824 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6827 /* Convert range assertion expressions into the implied copies and
6828 copy propagate away the copies. Doing the trivial copy propagation
6829 here avoids the need to run the full copy propagation pass after
6830 VRP.
6832 FIXME, this will eventually lead to copy propagation removing the
6833 names that had useful range information attached to them. For
6834 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6835 then N_i will have the range [3, +INF].
6837 However, by converting the assertion into the implied copy
6838 operation N_i = N_j, we will then copy-propagate N_j into the uses
6839 of N_i and lose the range information. We may want to hold on to
6840 ASSERT_EXPRs a little while longer as the ranges could be used in
6841 things like jump threading.
6843 The problem with keeping ASSERT_EXPRs around is that passes after
6844 VRP need to handle them appropriately.
6846 Another approach would be to make the range information a first
6847 class property of the SSA_NAME so that it can be queried from
6848 any pass. This is made somewhat more complex by the need for
6849 multiple ranges to be associated with one SSA_NAME. */
6851 static void
6852 remove_range_assertions (void)
6854 basic_block bb;
6855 gimple_stmt_iterator si;
6856 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6857 a basic block preceeded by GIMPLE_COND branching to it and
6858 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6859 int is_unreachable;
6861 /* Note that the BSI iterator bump happens at the bottom of the
6862 loop and no bump is necessary if we're removing the statement
6863 referenced by the current BSI. */
6864 FOR_EACH_BB_FN (bb, cfun)
6865 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6867 gimple *stmt = gsi_stmt (si);
6868 gimple *use_stmt;
6870 if (is_gimple_assign (stmt)
6871 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6873 tree lhs = gimple_assign_lhs (stmt);
6874 tree rhs = gimple_assign_rhs1 (stmt);
6875 tree var;
6876 use_operand_p use_p;
6877 imm_use_iterator iter;
6879 var = ASSERT_EXPR_VAR (rhs);
6880 gcc_assert (TREE_CODE (var) == SSA_NAME);
6882 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6883 && SSA_NAME_RANGE_INFO (lhs))
6885 if (is_unreachable == -1)
6887 is_unreachable = 0;
6888 if (single_pred_p (bb)
6889 && assert_unreachable_fallthru_edge_p
6890 (single_pred_edge (bb)))
6891 is_unreachable = 1;
6893 /* Handle
6894 if (x_7 >= 10 && x_7 < 20)
6895 __builtin_unreachable ();
6896 x_8 = ASSERT_EXPR <x_7, ...>;
6897 if the only uses of x_7 are in the ASSERT_EXPR and
6898 in the condition. In that case, we can copy the
6899 range info from x_8 computed in this pass also
6900 for x_7. */
6901 if (is_unreachable
6902 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6903 single_pred (bb)))
6905 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6906 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6907 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6908 maybe_set_nonzero_bits (bb, var);
6912 /* Propagate the RHS into every use of the LHS. */
6913 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6914 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6915 SET_USE (use_p, var);
6917 /* And finally, remove the copy, it is not needed. */
6918 gsi_remove (&si, true);
6919 release_defs (stmt);
6921 else
6923 if (!is_gimple_debug (gsi_stmt (si)))
6924 is_unreachable = 0;
6925 gsi_next (&si);
6931 /* Return true if STMT is interesting for VRP. */
6933 static bool
6934 stmt_interesting_for_vrp (gimple *stmt)
6936 if (gimple_code (stmt) == GIMPLE_PHI)
6938 tree res = gimple_phi_result (stmt);
6939 return (!virtual_operand_p (res)
6940 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6941 || POINTER_TYPE_P (TREE_TYPE (res))));
6943 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6945 tree lhs = gimple_get_lhs (stmt);
6947 /* In general, assignments with virtual operands are not useful
6948 for deriving ranges, with the obvious exception of calls to
6949 builtin functions. */
6950 if (lhs && TREE_CODE (lhs) == SSA_NAME
6951 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6952 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6953 && (is_gimple_call (stmt)
6954 || !gimple_vuse (stmt)))
6955 return true;
6956 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6957 switch (gimple_call_internal_fn (stmt))
6959 case IFN_ADD_OVERFLOW:
6960 case IFN_SUB_OVERFLOW:
6961 case IFN_MUL_OVERFLOW:
6962 /* These internal calls return _Complex integer type,
6963 but are interesting to VRP nevertheless. */
6964 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6965 return true;
6966 break;
6967 default:
6968 break;
6971 else if (gimple_code (stmt) == GIMPLE_COND
6972 || gimple_code (stmt) == GIMPLE_SWITCH)
6973 return true;
6975 return false;
6978 /* Initialize VRP lattice. */
6980 static void
6981 vrp_initialize_lattice ()
6983 values_propagated = false;
6984 num_vr_values = num_ssa_names;
6985 vr_value = XCNEWVEC (value_range *, num_vr_values);
6986 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6987 bitmap_obstack_initialize (&vrp_equiv_obstack);
6990 /* Initialization required by ssa_propagate engine. */
6992 static void
6993 vrp_initialize ()
6995 basic_block bb;
6997 FOR_EACH_BB_FN (bb, cfun)
6999 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7000 gsi_next (&si))
7002 gphi *phi = si.phi ();
7003 if (!stmt_interesting_for_vrp (phi))
7005 tree lhs = PHI_RESULT (phi);
7006 set_value_range_to_varying (get_value_range (lhs));
7007 prop_set_simulate_again (phi, false);
7009 else
7010 prop_set_simulate_again (phi, true);
7013 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7014 gsi_next (&si))
7016 gimple *stmt = gsi_stmt (si);
7018 /* If the statement is a control insn, then we do not
7019 want to avoid simulating the statement once. Failure
7020 to do so means that those edges will never get added. */
7021 if (stmt_ends_bb_p (stmt))
7022 prop_set_simulate_again (stmt, true);
7023 else if (!stmt_interesting_for_vrp (stmt))
7025 ssa_op_iter i;
7026 tree def;
7027 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7028 set_value_range_to_varying (get_value_range (def));
7029 prop_set_simulate_again (stmt, false);
7031 else
7032 prop_set_simulate_again (stmt, true);
7037 /* Return the singleton value-range for NAME or NAME. */
7039 static inline tree
7040 vrp_valueize (tree name)
7042 if (TREE_CODE (name) == SSA_NAME)
7044 value_range *vr = get_value_range (name);
7045 if (vr->type == VR_RANGE
7046 && (TREE_CODE (vr->min) == SSA_NAME
7047 || is_gimple_min_invariant (vr->min))
7048 && vrp_operand_equal_p (vr->min, vr->max))
7049 return vr->min;
7051 return name;
7054 /* Return the singleton value-range for NAME if that is a constant
7055 but signal to not follow SSA edges. */
7057 static inline tree
7058 vrp_valueize_1 (tree name)
7060 if (TREE_CODE (name) == SSA_NAME)
7062 /* If the definition may be simulated again we cannot follow
7063 this SSA edge as the SSA propagator does not necessarily
7064 re-visit the use. */
7065 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7066 if (!gimple_nop_p (def_stmt)
7067 && prop_simulate_again_p (def_stmt))
7068 return NULL_TREE;
7069 value_range *vr = get_value_range (name);
7070 if (range_int_cst_singleton_p (vr))
7071 return vr->min;
7073 return name;
7076 /* Visit assignment STMT. If it produces an interesting range, record
7077 the range in VR and set LHS to OUTPUT_P. */
7079 static void
7080 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p, value_range *vr)
7082 tree lhs;
7083 enum gimple_code code = gimple_code (stmt);
7084 lhs = gimple_get_lhs (stmt);
7085 *output_p = NULL_TREE;
7087 /* We only keep track of ranges in integral and pointer types. */
7088 if (TREE_CODE (lhs) == SSA_NAME
7089 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7090 /* It is valid to have NULL MIN/MAX values on a type. See
7091 build_range_type. */
7092 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7093 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7094 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7096 /* Try folding the statement to a constant first. */
7097 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7098 vrp_valueize_1);
7099 if (tem && is_gimple_min_invariant (tem))
7100 set_value_range_to_value (vr, tem, NULL);
7101 /* Then dispatch to value-range extracting functions. */
7102 else if (code == GIMPLE_CALL)
7103 extract_range_basic (vr, stmt);
7104 else
7105 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
7106 *output_p = lhs;
7110 /* Helper that gets the value range of the SSA_NAME with version I
7111 or a symbolic range containing the SSA_NAME only if the value range
7112 is varying or undefined. */
7114 static inline value_range
7115 get_vr_for_comparison (int i)
7117 value_range vr = *get_value_range (ssa_name (i));
7119 /* If name N_i does not have a valid range, use N_i as its own
7120 range. This allows us to compare against names that may
7121 have N_i in their ranges. */
7122 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7124 vr.type = VR_RANGE;
7125 vr.min = ssa_name (i);
7126 vr.max = ssa_name (i);
7129 return vr;
7132 /* Compare all the value ranges for names equivalent to VAR with VAL
7133 using comparison code COMP. Return the same value returned by
7134 compare_range_with_value, including the setting of
7135 *STRICT_OVERFLOW_P. */
7137 static tree
7138 compare_name_with_value (enum tree_code comp, tree var, tree val,
7139 bool *strict_overflow_p, bool use_equiv_p)
7141 bitmap_iterator bi;
7142 unsigned i;
7143 bitmap e;
7144 tree retval, t;
7145 int used_strict_overflow;
7146 bool sop;
7147 value_range equiv_vr;
7149 /* Get the set of equivalences for VAR. */
7150 e = get_value_range (var)->equiv;
7152 /* Start at -1. Set it to 0 if we do a comparison without relying
7153 on overflow, or 1 if all comparisons rely on overflow. */
7154 used_strict_overflow = -1;
7156 /* Compare vars' value range with val. */
7157 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7158 sop = false;
7159 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7160 if (retval)
7161 used_strict_overflow = sop ? 1 : 0;
7163 /* If the equiv set is empty we have done all work we need to do. */
7164 if (e == NULL)
7166 if (retval
7167 && used_strict_overflow > 0)
7168 *strict_overflow_p = true;
7169 return retval;
7172 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7174 if (! use_equiv_p
7175 && ! SSA_NAME_IS_DEFAULT_DEF (ssa_name (i))
7176 && prop_simulate_again_p (SSA_NAME_DEF_STMT (ssa_name (i))))
7177 continue;
7179 equiv_vr = get_vr_for_comparison (i);
7180 sop = false;
7181 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7182 if (t)
7184 /* If we get different answers from different members
7185 of the equivalence set this check must be in a dead
7186 code region. Folding it to a trap representation
7187 would be correct here. For now just return don't-know. */
7188 if (retval != NULL
7189 && t != retval)
7191 retval = NULL_TREE;
7192 break;
7194 retval = t;
7196 if (!sop)
7197 used_strict_overflow = 0;
7198 else if (used_strict_overflow < 0)
7199 used_strict_overflow = 1;
7203 if (retval
7204 && used_strict_overflow > 0)
7205 *strict_overflow_p = true;
7207 return retval;
7211 /* Given a comparison code COMP and names N1 and N2, compare all the
7212 ranges equivalent to N1 against all the ranges equivalent to N2
7213 to determine the value of N1 COMP N2. Return the same value
7214 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7215 whether we relied on an overflow infinity in the comparison. */
7218 static tree
7219 compare_names (enum tree_code comp, tree n1, tree n2,
7220 bool *strict_overflow_p)
7222 tree t, retval;
7223 bitmap e1, e2;
7224 bitmap_iterator bi1, bi2;
7225 unsigned i1, i2;
7226 int used_strict_overflow;
7227 static bitmap_obstack *s_obstack = NULL;
7228 static bitmap s_e1 = NULL, s_e2 = NULL;
7230 /* Compare the ranges of every name equivalent to N1 against the
7231 ranges of every name equivalent to N2. */
7232 e1 = get_value_range (n1)->equiv;
7233 e2 = get_value_range (n2)->equiv;
7235 /* Use the fake bitmaps if e1 or e2 are not available. */
7236 if (s_obstack == NULL)
7238 s_obstack = XNEW (bitmap_obstack);
7239 bitmap_obstack_initialize (s_obstack);
7240 s_e1 = BITMAP_ALLOC (s_obstack);
7241 s_e2 = BITMAP_ALLOC (s_obstack);
7243 if (e1 == NULL)
7244 e1 = s_e1;
7245 if (e2 == NULL)
7246 e2 = s_e2;
7248 /* Add N1 and N2 to their own set of equivalences to avoid
7249 duplicating the body of the loop just to check N1 and N2
7250 ranges. */
7251 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7252 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7254 /* If the equivalence sets have a common intersection, then the two
7255 names can be compared without checking their ranges. */
7256 if (bitmap_intersect_p (e1, e2))
7258 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7259 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7261 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7262 ? boolean_true_node
7263 : boolean_false_node;
7266 /* Start at -1. Set it to 0 if we do a comparison without relying
7267 on overflow, or 1 if all comparisons rely on overflow. */
7268 used_strict_overflow = -1;
7270 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7271 N2 to their own set of equivalences to avoid duplicating the body
7272 of the loop just to check N1 and N2 ranges. */
7273 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7275 value_range vr1 = get_vr_for_comparison (i1);
7277 t = retval = NULL_TREE;
7278 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7280 bool sop = false;
7282 value_range vr2 = get_vr_for_comparison (i2);
7284 t = compare_ranges (comp, &vr1, &vr2, &sop);
7285 if (t)
7287 /* If we get different answers from different members
7288 of the equivalence set this check must be in a dead
7289 code region. Folding it to a trap representation
7290 would be correct here. For now just return don't-know. */
7291 if (retval != NULL
7292 && t != retval)
7294 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7295 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7296 return NULL_TREE;
7298 retval = t;
7300 if (!sop)
7301 used_strict_overflow = 0;
7302 else if (used_strict_overflow < 0)
7303 used_strict_overflow = 1;
7307 if (retval)
7309 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7310 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7311 if (used_strict_overflow > 0)
7312 *strict_overflow_p = true;
7313 return retval;
7317 /* None of the equivalent ranges are useful in computing this
7318 comparison. */
7319 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7320 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7321 return NULL_TREE;
7324 /* Helper function for vrp_evaluate_conditional_warnv & other
7325 optimizers. */
7327 static tree
7328 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7329 tree op0, tree op1,
7330 bool * strict_overflow_p)
7332 value_range *vr0, *vr1;
7334 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7335 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7337 tree res = NULL_TREE;
7338 if (vr0 && vr1)
7339 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7340 if (!res && vr0)
7341 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7342 if (!res && vr1)
7343 res = (compare_range_with_value
7344 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7345 return res;
7348 /* Helper function for vrp_evaluate_conditional_warnv. */
7350 static tree
7351 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7352 tree op1, bool use_equiv_p,
7353 bool *strict_overflow_p, bool *only_ranges)
7355 tree ret;
7356 if (only_ranges)
7357 *only_ranges = true;
7359 /* We only deal with integral and pointer types. */
7360 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7361 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7362 return NULL_TREE;
7364 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7365 (code, op0, op1, strict_overflow_p)))
7366 return ret;
7367 if (only_ranges)
7368 *only_ranges = false;
7369 /* Do not use compare_names during propagation, it's quadratic. */
7370 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7371 && use_equiv_p)
7372 return compare_names (code, op0, op1, strict_overflow_p);
7373 else if (TREE_CODE (op0) == SSA_NAME)
7374 return compare_name_with_value (code, op0, op1,
7375 strict_overflow_p, use_equiv_p);
7376 else if (TREE_CODE (op1) == SSA_NAME)
7377 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7378 strict_overflow_p, use_equiv_p);
7379 return NULL_TREE;
7382 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7383 information. Return NULL if the conditional can not be evaluated.
7384 The ranges of all the names equivalent with the operands in COND
7385 will be used when trying to compute the value. If the result is
7386 based on undefined signed overflow, issue a warning if
7387 appropriate. */
7389 static tree
7390 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7392 bool sop;
7393 tree ret;
7394 bool only_ranges;
7396 /* Some passes and foldings leak constants with overflow flag set
7397 into the IL. Avoid doing wrong things with these and bail out. */
7398 if ((TREE_CODE (op0) == INTEGER_CST
7399 && TREE_OVERFLOW (op0))
7400 || (TREE_CODE (op1) == INTEGER_CST
7401 && TREE_OVERFLOW (op1)))
7402 return NULL_TREE;
7404 sop = false;
7405 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7406 &only_ranges);
7408 if (ret && sop)
7410 enum warn_strict_overflow_code wc;
7411 const char* warnmsg;
7413 if (is_gimple_min_invariant (ret))
7415 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7416 warnmsg = G_("assuming signed overflow does not occur when "
7417 "simplifying conditional to constant");
7419 else
7421 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7422 warnmsg = G_("assuming signed overflow does not occur when "
7423 "simplifying conditional");
7426 if (issue_strict_overflow_warning (wc))
7428 location_t location;
7430 if (!gimple_has_location (stmt))
7431 location = input_location;
7432 else
7433 location = gimple_location (stmt);
7434 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7438 if (warn_type_limits
7439 && ret && only_ranges
7440 && TREE_CODE_CLASS (code) == tcc_comparison
7441 && TREE_CODE (op0) == SSA_NAME)
7443 /* If the comparison is being folded and the operand on the LHS
7444 is being compared against a constant value that is outside of
7445 the natural range of OP0's type, then the predicate will
7446 always fold regardless of the value of OP0. If -Wtype-limits
7447 was specified, emit a warning. */
7448 tree type = TREE_TYPE (op0);
7449 value_range *vr0 = get_value_range (op0);
7451 if (vr0->type == VR_RANGE
7452 && INTEGRAL_TYPE_P (type)
7453 && vrp_val_is_min (vr0->min)
7454 && vrp_val_is_max (vr0->max)
7455 && is_gimple_min_invariant (op1))
7457 location_t location;
7459 if (!gimple_has_location (stmt))
7460 location = input_location;
7461 else
7462 location = gimple_location (stmt);
7464 warning_at (location, OPT_Wtype_limits,
7465 integer_zerop (ret)
7466 ? G_("comparison always false "
7467 "due to limited range of data type")
7468 : G_("comparison always true "
7469 "due to limited range of data type"));
7473 return ret;
7477 /* Visit conditional statement STMT. If we can determine which edge
7478 will be taken out of STMT's basic block, record it in
7479 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7481 static void
7482 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7484 tree val;
7485 bool sop;
7487 *taken_edge_p = NULL;
7489 if (dump_file && (dump_flags & TDF_DETAILS))
7491 tree use;
7492 ssa_op_iter i;
7494 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7495 print_gimple_stmt (dump_file, stmt, 0, 0);
7496 fprintf (dump_file, "\nWith known ranges\n");
7498 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7500 fprintf (dump_file, "\t");
7501 print_generic_expr (dump_file, use, 0);
7502 fprintf (dump_file, ": ");
7503 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7506 fprintf (dump_file, "\n");
7509 /* Compute the value of the predicate COND by checking the known
7510 ranges of each of its operands.
7512 Note that we cannot evaluate all the equivalent ranges here
7513 because those ranges may not yet be final and with the current
7514 propagation strategy, we cannot determine when the value ranges
7515 of the names in the equivalence set have changed.
7517 For instance, given the following code fragment
7519 i_5 = PHI <8, i_13>
7521 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7522 if (i_14 == 1)
7525 Assume that on the first visit to i_14, i_5 has the temporary
7526 range [8, 8] because the second argument to the PHI function is
7527 not yet executable. We derive the range ~[0, 0] for i_14 and the
7528 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7529 the first time, since i_14 is equivalent to the range [8, 8], we
7530 determine that the predicate is always false.
7532 On the next round of propagation, i_13 is determined to be
7533 VARYING, which causes i_5 to drop down to VARYING. So, another
7534 visit to i_14 is scheduled. In this second visit, we compute the
7535 exact same range and equivalence set for i_14, namely ~[0, 0] and
7536 { i_5 }. But we did not have the previous range for i_5
7537 registered, so vrp_visit_assignment thinks that the range for
7538 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7539 is not visited again, which stops propagation from visiting
7540 statements in the THEN clause of that if().
7542 To properly fix this we would need to keep the previous range
7543 value for the names in the equivalence set. This way we would've
7544 discovered that from one visit to the other i_5 changed from
7545 range [8, 8] to VR_VARYING.
7547 However, fixing this apparent limitation may not be worth the
7548 additional checking. Testing on several code bases (GCC, DLV,
7549 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7550 4 more predicates folded in SPEC. */
7551 sop = false;
7553 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7554 gimple_cond_lhs (stmt),
7555 gimple_cond_rhs (stmt),
7556 false, &sop, NULL);
7557 if (val)
7559 if (!sop)
7560 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7561 else
7563 if (dump_file && (dump_flags & TDF_DETAILS))
7564 fprintf (dump_file,
7565 "\nIgnoring predicate evaluation because "
7566 "it assumes that signed overflow is undefined");
7567 val = NULL_TREE;
7571 if (dump_file && (dump_flags & TDF_DETAILS))
7573 fprintf (dump_file, "\nPredicate evaluates to: ");
7574 if (val == NULL_TREE)
7575 fprintf (dump_file, "DON'T KNOW\n");
7576 else
7577 print_generic_stmt (dump_file, val, 0);
7581 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7582 that includes the value VAL. The search is restricted to the range
7583 [START_IDX, n - 1] where n is the size of VEC.
7585 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7586 returned.
7588 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7589 it is placed in IDX and false is returned.
7591 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7592 returned. */
7594 static bool
7595 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7597 size_t n = gimple_switch_num_labels (stmt);
7598 size_t low, high;
7600 /* Find case label for minimum of the value range or the next one.
7601 At each iteration we are searching in [low, high - 1]. */
7603 for (low = start_idx, high = n; high != low; )
7605 tree t;
7606 int cmp;
7607 /* Note that i != high, so we never ask for n. */
7608 size_t i = (high + low) / 2;
7609 t = gimple_switch_label (stmt, i);
7611 /* Cache the result of comparing CASE_LOW and val. */
7612 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7614 if (cmp == 0)
7616 /* Ranges cannot be empty. */
7617 *idx = i;
7618 return true;
7620 else if (cmp > 0)
7621 high = i;
7622 else
7624 low = i + 1;
7625 if (CASE_HIGH (t) != NULL
7626 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7628 *idx = i;
7629 return true;
7634 *idx = high;
7635 return false;
7638 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7639 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7640 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7641 then MAX_IDX < MIN_IDX.
7642 Returns true if the default label is not needed. */
7644 static bool
7645 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7646 size_t *max_idx)
7648 size_t i, j;
7649 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7650 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7652 if (i == j
7653 && min_take_default
7654 && max_take_default)
7656 /* Only the default case label reached.
7657 Return an empty range. */
7658 *min_idx = 1;
7659 *max_idx = 0;
7660 return false;
7662 else
7664 bool take_default = min_take_default || max_take_default;
7665 tree low, high;
7666 size_t k;
7668 if (max_take_default)
7669 j--;
7671 /* If the case label range is continuous, we do not need
7672 the default case label. Verify that. */
7673 high = CASE_LOW (gimple_switch_label (stmt, i));
7674 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7675 high = CASE_HIGH (gimple_switch_label (stmt, i));
7676 for (k = i + 1; k <= j; ++k)
7678 low = CASE_LOW (gimple_switch_label (stmt, k));
7679 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7681 take_default = true;
7682 break;
7684 high = low;
7685 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7686 high = CASE_HIGH (gimple_switch_label (stmt, k));
7689 *min_idx = i;
7690 *max_idx = j;
7691 return !take_default;
7695 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7696 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7697 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7698 Returns true if the default label is not needed. */
7700 static bool
7701 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7702 size_t *max_idx1, size_t *min_idx2,
7703 size_t *max_idx2)
7705 size_t i, j, k, l;
7706 unsigned int n = gimple_switch_num_labels (stmt);
7707 bool take_default;
7708 tree case_low, case_high;
7709 tree min = vr->min, max = vr->max;
7711 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7713 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7715 /* Set second range to emtpy. */
7716 *min_idx2 = 1;
7717 *max_idx2 = 0;
7719 if (vr->type == VR_RANGE)
7721 *min_idx1 = i;
7722 *max_idx1 = j;
7723 return !take_default;
7726 /* Set first range to all case labels. */
7727 *min_idx1 = 1;
7728 *max_idx1 = n - 1;
7730 if (i > j)
7731 return false;
7733 /* Make sure all the values of case labels [i , j] are contained in
7734 range [MIN, MAX]. */
7735 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7736 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7737 if (tree_int_cst_compare (case_low, min) < 0)
7738 i += 1;
7739 if (case_high != NULL_TREE
7740 && tree_int_cst_compare (max, case_high) < 0)
7741 j -= 1;
7743 if (i > j)
7744 return false;
7746 /* If the range spans case labels [i, j], the corresponding anti-range spans
7747 the labels [1, i - 1] and [j + 1, n - 1]. */
7748 k = j + 1;
7749 l = n - 1;
7750 if (k > l)
7752 k = 1;
7753 l = 0;
7756 j = i - 1;
7757 i = 1;
7758 if (i > j)
7760 i = k;
7761 j = l;
7762 k = 1;
7763 l = 0;
7766 *min_idx1 = i;
7767 *max_idx1 = j;
7768 *min_idx2 = k;
7769 *max_idx2 = l;
7770 return false;
7773 /* Visit switch statement STMT. If we can determine which edge
7774 will be taken out of STMT's basic block, record it in
7775 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7777 static void
7778 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7780 tree op, val;
7781 value_range *vr;
7782 size_t i = 0, j = 0, k, l;
7783 bool take_default;
7785 *taken_edge_p = NULL;
7786 op = gimple_switch_index (stmt);
7787 if (TREE_CODE (op) != SSA_NAME)
7788 return;
7790 vr = get_value_range (op);
7791 if (dump_file && (dump_flags & TDF_DETAILS))
7793 fprintf (dump_file, "\nVisiting switch expression with operand ");
7794 print_generic_expr (dump_file, op, 0);
7795 fprintf (dump_file, " with known range ");
7796 dump_value_range (dump_file, vr);
7797 fprintf (dump_file, "\n");
7800 if ((vr->type != VR_RANGE
7801 && vr->type != VR_ANTI_RANGE)
7802 || symbolic_range_p (vr))
7803 return;
7805 /* Find the single edge that is taken from the switch expression. */
7806 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7808 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7809 label */
7810 if (j < i)
7812 gcc_assert (take_default);
7813 val = gimple_switch_default_label (stmt);
7815 else
7817 /* Check if labels with index i to j and maybe the default label
7818 are all reaching the same label. */
7820 val = gimple_switch_label (stmt, i);
7821 if (take_default
7822 && CASE_LABEL (gimple_switch_default_label (stmt))
7823 != CASE_LABEL (val))
7825 if (dump_file && (dump_flags & TDF_DETAILS))
7826 fprintf (dump_file, " not a single destination for this "
7827 "range\n");
7828 return;
7830 for (++i; i <= j; ++i)
7832 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7834 if (dump_file && (dump_flags & TDF_DETAILS))
7835 fprintf (dump_file, " not a single destination for this "
7836 "range\n");
7837 return;
7840 for (; k <= l; ++k)
7842 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7844 if (dump_file && (dump_flags & TDF_DETAILS))
7845 fprintf (dump_file, " not a single destination for this "
7846 "range\n");
7847 return;
7852 *taken_edge_p = find_edge (gimple_bb (stmt),
7853 label_to_block (CASE_LABEL (val)));
7855 if (dump_file && (dump_flags & TDF_DETAILS))
7857 fprintf (dump_file, " will take edge to ");
7858 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7863 /* Evaluate statement STMT. If the statement produces a useful range,
7864 set VR and corepsponding OUTPUT_P.
7866 If STMT is a conditional branch and we can determine its truth
7867 value, the taken edge is recorded in *TAKEN_EDGE_P. */
7869 static void
7870 extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
7871 tree *output_p, value_range *vr)
7874 if (dump_file && (dump_flags & TDF_DETAILS))
7876 fprintf (dump_file, "\nVisiting statement:\n");
7877 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7880 if (!stmt_interesting_for_vrp (stmt))
7881 gcc_assert (stmt_ends_bb_p (stmt));
7882 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7883 vrp_visit_assignment_or_call (stmt, output_p, vr);
7884 else if (gimple_code (stmt) == GIMPLE_COND)
7885 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7886 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7887 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7890 /* Evaluate statement STMT. If the statement produces a useful range,
7891 return SSA_PROP_INTERESTING and record the SSA name with the
7892 interesting range into *OUTPUT_P.
7894 If STMT is a conditional branch and we can determine its truth
7895 value, the taken edge is recorded in *TAKEN_EDGE_P.
7897 If STMT produces a varying value, return SSA_PROP_VARYING. */
7899 static enum ssa_prop_result
7900 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7902 value_range vr = VR_INITIALIZER;
7903 tree lhs = gimple_get_lhs (stmt);
7904 tree def;
7905 ssa_op_iter iter;
7906 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
7908 if (*output_p)
7910 if (update_value_range (*output_p, &vr))
7912 if (dump_file && (dump_flags & TDF_DETAILS))
7914 fprintf (dump_file, "Found new range for ");
7915 print_generic_expr (dump_file, *output_p, 0);
7916 fprintf (dump_file, ": ");
7917 dump_value_range (dump_file, &vr);
7918 fprintf (dump_file, "\n");
7921 if (vr.type == VR_VARYING)
7922 return SSA_PROP_VARYING;
7924 return SSA_PROP_INTERESTING;
7926 return SSA_PROP_NOT_INTERESTING;
7929 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7930 switch (gimple_call_internal_fn (stmt))
7932 case IFN_ADD_OVERFLOW:
7933 case IFN_SUB_OVERFLOW:
7934 case IFN_MUL_OVERFLOW:
7935 /* These internal calls return _Complex integer type,
7936 which VRP does not track, but the immediate uses
7937 thereof might be interesting. */
7938 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7940 imm_use_iterator iter;
7941 use_operand_p use_p;
7942 enum ssa_prop_result res = SSA_PROP_VARYING;
7944 set_value_range_to_varying (get_value_range (lhs));
7946 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7948 gimple *use_stmt = USE_STMT (use_p);
7949 if (!is_gimple_assign (use_stmt))
7950 continue;
7951 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7952 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7953 continue;
7954 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7955 tree use_lhs = gimple_assign_lhs (use_stmt);
7956 if (TREE_CODE (rhs1) != rhs_code
7957 || TREE_OPERAND (rhs1, 0) != lhs
7958 || TREE_CODE (use_lhs) != SSA_NAME
7959 || !stmt_interesting_for_vrp (use_stmt)
7960 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7961 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7962 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7963 continue;
7965 /* If there is a change in the value range for any of the
7966 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7967 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7968 or IMAGPART_EXPR immediate uses, but none of them have
7969 a change in their value ranges, return
7970 SSA_PROP_NOT_INTERESTING. If there are no
7971 {REAL,IMAG}PART_EXPR uses at all,
7972 return SSA_PROP_VARYING. */
7973 value_range new_vr = VR_INITIALIZER;
7974 extract_range_basic (&new_vr, use_stmt);
7975 value_range *old_vr = get_value_range (use_lhs);
7976 if (old_vr->type != new_vr.type
7977 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7978 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7979 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7980 res = SSA_PROP_INTERESTING;
7981 else
7982 res = SSA_PROP_NOT_INTERESTING;
7983 BITMAP_FREE (new_vr.equiv);
7984 if (res == SSA_PROP_INTERESTING)
7986 *output_p = lhs;
7987 return res;
7991 return res;
7993 break;
7994 default:
7995 break;
7998 /* All other statements produce nothing of interest for VRP, so mark
7999 their outputs varying and prevent further simulation. */
8000 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
8001 set_value_range_to_varying (get_value_range (def));
8003 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
8006 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8007 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8008 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8009 possible such range. The resulting range is not canonicalized. */
8011 static void
8012 union_ranges (enum value_range_type *vr0type,
8013 tree *vr0min, tree *vr0max,
8014 enum value_range_type vr1type,
8015 tree vr1min, tree vr1max)
8017 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8018 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8020 /* [] is vr0, () is vr1 in the following classification comments. */
8021 if (mineq && maxeq)
8023 /* [( )] */
8024 if (*vr0type == vr1type)
8025 /* Nothing to do for equal ranges. */
8027 else if ((*vr0type == VR_RANGE
8028 && vr1type == VR_ANTI_RANGE)
8029 || (*vr0type == VR_ANTI_RANGE
8030 && vr1type == VR_RANGE))
8032 /* For anti-range with range union the result is varying. */
8033 goto give_up;
8035 else
8036 gcc_unreachable ();
8038 else if (operand_less_p (*vr0max, vr1min) == 1
8039 || operand_less_p (vr1max, *vr0min) == 1)
8041 /* [ ] ( ) or ( ) [ ]
8042 If the ranges have an empty intersection, result of the union
8043 operation is the anti-range or if both are anti-ranges
8044 it covers all. */
8045 if (*vr0type == VR_ANTI_RANGE
8046 && vr1type == VR_ANTI_RANGE)
8047 goto give_up;
8048 else if (*vr0type == VR_ANTI_RANGE
8049 && vr1type == VR_RANGE)
8051 else if (*vr0type == VR_RANGE
8052 && vr1type == VR_ANTI_RANGE)
8054 *vr0type = vr1type;
8055 *vr0min = vr1min;
8056 *vr0max = vr1max;
8058 else if (*vr0type == VR_RANGE
8059 && vr1type == VR_RANGE)
8061 /* The result is the convex hull of both ranges. */
8062 if (operand_less_p (*vr0max, vr1min) == 1)
8064 /* If the result can be an anti-range, create one. */
8065 if (TREE_CODE (*vr0max) == INTEGER_CST
8066 && TREE_CODE (vr1min) == INTEGER_CST
8067 && vrp_val_is_min (*vr0min)
8068 && vrp_val_is_max (vr1max))
8070 tree min = int_const_binop (PLUS_EXPR,
8071 *vr0max,
8072 build_int_cst (TREE_TYPE (*vr0max), 1));
8073 tree max = int_const_binop (MINUS_EXPR,
8074 vr1min,
8075 build_int_cst (TREE_TYPE (vr1min), 1));
8076 if (!operand_less_p (max, min))
8078 *vr0type = VR_ANTI_RANGE;
8079 *vr0min = min;
8080 *vr0max = max;
8082 else
8083 *vr0max = vr1max;
8085 else
8086 *vr0max = vr1max;
8088 else
8090 /* If the result can be an anti-range, create one. */
8091 if (TREE_CODE (vr1max) == INTEGER_CST
8092 && TREE_CODE (*vr0min) == INTEGER_CST
8093 && vrp_val_is_min (vr1min)
8094 && vrp_val_is_max (*vr0max))
8096 tree min = int_const_binop (PLUS_EXPR,
8097 vr1max,
8098 build_int_cst (TREE_TYPE (vr1max), 1));
8099 tree max = int_const_binop (MINUS_EXPR,
8100 *vr0min,
8101 build_int_cst (TREE_TYPE (*vr0min), 1));
8102 if (!operand_less_p (max, min))
8104 *vr0type = VR_ANTI_RANGE;
8105 *vr0min = min;
8106 *vr0max = max;
8108 else
8109 *vr0min = vr1min;
8111 else
8112 *vr0min = vr1min;
8115 else
8116 gcc_unreachable ();
8118 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8119 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8121 /* [ ( ) ] or [( ) ] or [ ( )] */
8122 if (*vr0type == VR_RANGE
8123 && vr1type == VR_RANGE)
8125 else if (*vr0type == VR_ANTI_RANGE
8126 && vr1type == VR_ANTI_RANGE)
8128 *vr0type = vr1type;
8129 *vr0min = vr1min;
8130 *vr0max = vr1max;
8132 else if (*vr0type == VR_ANTI_RANGE
8133 && vr1type == VR_RANGE)
8135 /* Arbitrarily choose the right or left gap. */
8136 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8137 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8138 build_int_cst (TREE_TYPE (vr1min), 1));
8139 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8140 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8141 build_int_cst (TREE_TYPE (vr1max), 1));
8142 else
8143 goto give_up;
8145 else if (*vr0type == VR_RANGE
8146 && vr1type == VR_ANTI_RANGE)
8147 /* The result covers everything. */
8148 goto give_up;
8149 else
8150 gcc_unreachable ();
8152 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8153 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8155 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8156 if (*vr0type == VR_RANGE
8157 && vr1type == VR_RANGE)
8159 *vr0type = vr1type;
8160 *vr0min = vr1min;
8161 *vr0max = vr1max;
8163 else if (*vr0type == VR_ANTI_RANGE
8164 && vr1type == VR_ANTI_RANGE)
8166 else if (*vr0type == VR_RANGE
8167 && vr1type == VR_ANTI_RANGE)
8169 *vr0type = VR_ANTI_RANGE;
8170 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8172 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8173 build_int_cst (TREE_TYPE (*vr0min), 1));
8174 *vr0min = vr1min;
8176 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8178 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8179 build_int_cst (TREE_TYPE (*vr0max), 1));
8180 *vr0max = vr1max;
8182 else
8183 goto give_up;
8185 else if (*vr0type == VR_ANTI_RANGE
8186 && vr1type == VR_RANGE)
8187 /* The result covers everything. */
8188 goto give_up;
8189 else
8190 gcc_unreachable ();
8192 else if ((operand_less_p (vr1min, *vr0max) == 1
8193 || operand_equal_p (vr1min, *vr0max, 0))
8194 && operand_less_p (*vr0min, vr1min) == 1
8195 && operand_less_p (*vr0max, vr1max) == 1)
8197 /* [ ( ] ) or [ ]( ) */
8198 if (*vr0type == VR_RANGE
8199 && vr1type == VR_RANGE)
8200 *vr0max = vr1max;
8201 else if (*vr0type == VR_ANTI_RANGE
8202 && vr1type == VR_ANTI_RANGE)
8203 *vr0min = vr1min;
8204 else if (*vr0type == VR_ANTI_RANGE
8205 && vr1type == VR_RANGE)
8207 if (TREE_CODE (vr1min) == INTEGER_CST)
8208 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8209 build_int_cst (TREE_TYPE (vr1min), 1));
8210 else
8211 goto give_up;
8213 else if (*vr0type == VR_RANGE
8214 && vr1type == VR_ANTI_RANGE)
8216 if (TREE_CODE (*vr0max) == INTEGER_CST)
8218 *vr0type = vr1type;
8219 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8220 build_int_cst (TREE_TYPE (*vr0max), 1));
8221 *vr0max = vr1max;
8223 else
8224 goto give_up;
8226 else
8227 gcc_unreachable ();
8229 else if ((operand_less_p (*vr0min, vr1max) == 1
8230 || operand_equal_p (*vr0min, vr1max, 0))
8231 && operand_less_p (vr1min, *vr0min) == 1
8232 && operand_less_p (vr1max, *vr0max) == 1)
8234 /* ( [ ) ] or ( )[ ] */
8235 if (*vr0type == VR_RANGE
8236 && vr1type == VR_RANGE)
8237 *vr0min = vr1min;
8238 else if (*vr0type == VR_ANTI_RANGE
8239 && vr1type == VR_ANTI_RANGE)
8240 *vr0max = vr1max;
8241 else if (*vr0type == VR_ANTI_RANGE
8242 && vr1type == VR_RANGE)
8244 if (TREE_CODE (vr1max) == INTEGER_CST)
8245 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8246 build_int_cst (TREE_TYPE (vr1max), 1));
8247 else
8248 goto give_up;
8250 else if (*vr0type == VR_RANGE
8251 && vr1type == VR_ANTI_RANGE)
8253 if (TREE_CODE (*vr0min) == INTEGER_CST)
8255 *vr0type = vr1type;
8256 *vr0min = vr1min;
8257 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8258 build_int_cst (TREE_TYPE (*vr0min), 1));
8260 else
8261 goto give_up;
8263 else
8264 gcc_unreachable ();
8266 else
8267 goto give_up;
8269 return;
8271 give_up:
8272 *vr0type = VR_VARYING;
8273 *vr0min = NULL_TREE;
8274 *vr0max = NULL_TREE;
8277 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8278 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8279 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8280 possible such range. The resulting range is not canonicalized. */
8282 static void
8283 intersect_ranges (enum value_range_type *vr0type,
8284 tree *vr0min, tree *vr0max,
8285 enum value_range_type vr1type,
8286 tree vr1min, tree vr1max)
8288 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8289 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8291 /* [] is vr0, () is vr1 in the following classification comments. */
8292 if (mineq && maxeq)
8294 /* [( )] */
8295 if (*vr0type == vr1type)
8296 /* Nothing to do for equal ranges. */
8298 else if ((*vr0type == VR_RANGE
8299 && vr1type == VR_ANTI_RANGE)
8300 || (*vr0type == VR_ANTI_RANGE
8301 && vr1type == VR_RANGE))
8303 /* For anti-range with range intersection the result is empty. */
8304 *vr0type = VR_UNDEFINED;
8305 *vr0min = NULL_TREE;
8306 *vr0max = NULL_TREE;
8308 else
8309 gcc_unreachable ();
8311 else if (operand_less_p (*vr0max, vr1min) == 1
8312 || operand_less_p (vr1max, *vr0min) == 1)
8314 /* [ ] ( ) or ( ) [ ]
8315 If the ranges have an empty intersection, the result of the
8316 intersect operation is the range for intersecting an
8317 anti-range with a range or empty when intersecting two ranges. */
8318 if (*vr0type == VR_RANGE
8319 && vr1type == VR_ANTI_RANGE)
8321 else if (*vr0type == VR_ANTI_RANGE
8322 && vr1type == VR_RANGE)
8324 *vr0type = vr1type;
8325 *vr0min = vr1min;
8326 *vr0max = vr1max;
8328 else if (*vr0type == VR_RANGE
8329 && vr1type == VR_RANGE)
8331 *vr0type = VR_UNDEFINED;
8332 *vr0min = NULL_TREE;
8333 *vr0max = NULL_TREE;
8335 else if (*vr0type == VR_ANTI_RANGE
8336 && vr1type == VR_ANTI_RANGE)
8338 /* If the anti-ranges are adjacent to each other merge them. */
8339 if (TREE_CODE (*vr0max) == INTEGER_CST
8340 && TREE_CODE (vr1min) == INTEGER_CST
8341 && operand_less_p (*vr0max, vr1min) == 1
8342 && integer_onep (int_const_binop (MINUS_EXPR,
8343 vr1min, *vr0max)))
8344 *vr0max = vr1max;
8345 else if (TREE_CODE (vr1max) == INTEGER_CST
8346 && TREE_CODE (*vr0min) == INTEGER_CST
8347 && operand_less_p (vr1max, *vr0min) == 1
8348 && integer_onep (int_const_binop (MINUS_EXPR,
8349 *vr0min, vr1max)))
8350 *vr0min = vr1min;
8351 /* Else arbitrarily take VR0. */
8354 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8355 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8357 /* [ ( ) ] or [( ) ] or [ ( )] */
8358 if (*vr0type == VR_RANGE
8359 && vr1type == VR_RANGE)
8361 /* If both are ranges the result is the inner one. */
8362 *vr0type = vr1type;
8363 *vr0min = vr1min;
8364 *vr0max = vr1max;
8366 else if (*vr0type == VR_RANGE
8367 && vr1type == VR_ANTI_RANGE)
8369 /* Choose the right gap if the left one is empty. */
8370 if (mineq)
8372 if (TREE_CODE (vr1max) == INTEGER_CST)
8373 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8374 build_int_cst (TREE_TYPE (vr1max), 1));
8375 else
8376 *vr0min = vr1max;
8378 /* Choose the left gap if the right one is empty. */
8379 else if (maxeq)
8381 if (TREE_CODE (vr1min) == INTEGER_CST)
8382 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8383 build_int_cst (TREE_TYPE (vr1min), 1));
8384 else
8385 *vr0max = vr1min;
8387 /* Choose the anti-range if the range is effectively varying. */
8388 else if (vrp_val_is_min (*vr0min)
8389 && vrp_val_is_max (*vr0max))
8391 *vr0type = vr1type;
8392 *vr0min = vr1min;
8393 *vr0max = vr1max;
8395 /* Else choose the range. */
8397 else if (*vr0type == VR_ANTI_RANGE
8398 && vr1type == VR_ANTI_RANGE)
8399 /* If both are anti-ranges the result is the outer one. */
8401 else if (*vr0type == VR_ANTI_RANGE
8402 && vr1type == VR_RANGE)
8404 /* The intersection is empty. */
8405 *vr0type = VR_UNDEFINED;
8406 *vr0min = NULL_TREE;
8407 *vr0max = NULL_TREE;
8409 else
8410 gcc_unreachable ();
8412 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8413 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8415 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8416 if (*vr0type == VR_RANGE
8417 && vr1type == VR_RANGE)
8418 /* Choose the inner range. */
8420 else if (*vr0type == VR_ANTI_RANGE
8421 && vr1type == VR_RANGE)
8423 /* Choose the right gap if the left is empty. */
8424 if (mineq)
8426 *vr0type = VR_RANGE;
8427 if (TREE_CODE (*vr0max) == INTEGER_CST)
8428 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8429 build_int_cst (TREE_TYPE (*vr0max), 1));
8430 else
8431 *vr0min = *vr0max;
8432 *vr0max = vr1max;
8434 /* Choose the left gap if the right is empty. */
8435 else if (maxeq)
8437 *vr0type = VR_RANGE;
8438 if (TREE_CODE (*vr0min) == INTEGER_CST)
8439 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8440 build_int_cst (TREE_TYPE (*vr0min), 1));
8441 else
8442 *vr0max = *vr0min;
8443 *vr0min = vr1min;
8445 /* Choose the anti-range if the range is effectively varying. */
8446 else if (vrp_val_is_min (vr1min)
8447 && vrp_val_is_max (vr1max))
8449 /* Else choose the range. */
8450 else
8452 *vr0type = vr1type;
8453 *vr0min = vr1min;
8454 *vr0max = vr1max;
8457 else if (*vr0type == VR_ANTI_RANGE
8458 && vr1type == VR_ANTI_RANGE)
8460 /* If both are anti-ranges the result is the outer one. */
8461 *vr0type = vr1type;
8462 *vr0min = vr1min;
8463 *vr0max = vr1max;
8465 else if (vr1type == VR_ANTI_RANGE
8466 && *vr0type == VR_RANGE)
8468 /* The intersection is empty. */
8469 *vr0type = VR_UNDEFINED;
8470 *vr0min = NULL_TREE;
8471 *vr0max = NULL_TREE;
8473 else
8474 gcc_unreachable ();
8476 else if ((operand_less_p (vr1min, *vr0max) == 1
8477 || operand_equal_p (vr1min, *vr0max, 0))
8478 && operand_less_p (*vr0min, vr1min) == 1)
8480 /* [ ( ] ) or [ ]( ) */
8481 if (*vr0type == VR_ANTI_RANGE
8482 && vr1type == VR_ANTI_RANGE)
8483 *vr0max = vr1max;
8484 else if (*vr0type == VR_RANGE
8485 && vr1type == VR_RANGE)
8486 *vr0min = vr1min;
8487 else if (*vr0type == VR_RANGE
8488 && vr1type == VR_ANTI_RANGE)
8490 if (TREE_CODE (vr1min) == INTEGER_CST)
8491 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8492 build_int_cst (TREE_TYPE (vr1min), 1));
8493 else
8494 *vr0max = vr1min;
8496 else if (*vr0type == VR_ANTI_RANGE
8497 && vr1type == VR_RANGE)
8499 *vr0type = VR_RANGE;
8500 if (TREE_CODE (*vr0max) == INTEGER_CST)
8501 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8502 build_int_cst (TREE_TYPE (*vr0max), 1));
8503 else
8504 *vr0min = *vr0max;
8505 *vr0max = vr1max;
8507 else
8508 gcc_unreachable ();
8510 else if ((operand_less_p (*vr0min, vr1max) == 1
8511 || operand_equal_p (*vr0min, vr1max, 0))
8512 && operand_less_p (vr1min, *vr0min) == 1)
8514 /* ( [ ) ] or ( )[ ] */
8515 if (*vr0type == VR_ANTI_RANGE
8516 && vr1type == VR_ANTI_RANGE)
8517 *vr0min = vr1min;
8518 else if (*vr0type == VR_RANGE
8519 && vr1type == VR_RANGE)
8520 *vr0max = vr1max;
8521 else if (*vr0type == VR_RANGE
8522 && vr1type == VR_ANTI_RANGE)
8524 if (TREE_CODE (vr1max) == INTEGER_CST)
8525 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8526 build_int_cst (TREE_TYPE (vr1max), 1));
8527 else
8528 *vr0min = vr1max;
8530 else if (*vr0type == VR_ANTI_RANGE
8531 && vr1type == VR_RANGE)
8533 *vr0type = VR_RANGE;
8534 if (TREE_CODE (*vr0min) == INTEGER_CST)
8535 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8536 build_int_cst (TREE_TYPE (*vr0min), 1));
8537 else
8538 *vr0max = *vr0min;
8539 *vr0min = vr1min;
8541 else
8542 gcc_unreachable ();
8545 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8546 result for the intersection. That's always a conservative
8547 correct estimate. */
8549 return;
8553 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8554 in *VR0. This may not be the smallest possible such range. */
8556 static void
8557 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8559 value_range saved;
8561 /* If either range is VR_VARYING the other one wins. */
8562 if (vr1->type == VR_VARYING)
8563 return;
8564 if (vr0->type == VR_VARYING)
8566 copy_value_range (vr0, vr1);
8567 return;
8570 /* When either range is VR_UNDEFINED the resulting range is
8571 VR_UNDEFINED, too. */
8572 if (vr0->type == VR_UNDEFINED)
8573 return;
8574 if (vr1->type == VR_UNDEFINED)
8576 set_value_range_to_undefined (vr0);
8577 return;
8580 /* Save the original vr0 so we can return it as conservative intersection
8581 result when our worker turns things to varying. */
8582 saved = *vr0;
8583 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8584 vr1->type, vr1->min, vr1->max);
8585 /* Make sure to canonicalize the result though as the inversion of a
8586 VR_RANGE can still be a VR_RANGE. */
8587 set_and_canonicalize_value_range (vr0, vr0->type,
8588 vr0->min, vr0->max, vr0->equiv);
8589 /* If that failed, use the saved original VR0. */
8590 if (vr0->type == VR_VARYING)
8592 *vr0 = saved;
8593 return;
8595 /* If the result is VR_UNDEFINED there is no need to mess with
8596 the equivalencies. */
8597 if (vr0->type == VR_UNDEFINED)
8598 return;
8600 /* The resulting set of equivalences for range intersection is the union of
8601 the two sets. */
8602 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8603 bitmap_ior_into (vr0->equiv, vr1->equiv);
8604 else if (vr1->equiv && !vr0->equiv)
8605 bitmap_copy (vr0->equiv, vr1->equiv);
8608 void
8609 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8611 if (dump_file && (dump_flags & TDF_DETAILS))
8613 fprintf (dump_file, "Intersecting\n ");
8614 dump_value_range (dump_file, vr0);
8615 fprintf (dump_file, "\nand\n ");
8616 dump_value_range (dump_file, vr1);
8617 fprintf (dump_file, "\n");
8619 vrp_intersect_ranges_1 (vr0, vr1);
8620 if (dump_file && (dump_flags & TDF_DETAILS))
8622 fprintf (dump_file, "to\n ");
8623 dump_value_range (dump_file, vr0);
8624 fprintf (dump_file, "\n");
8628 /* Meet operation for value ranges. Given two value ranges VR0 and
8629 VR1, store in VR0 a range that contains both VR0 and VR1. This
8630 may not be the smallest possible such range. */
8632 static void
8633 vrp_meet_1 (value_range *vr0, const value_range *vr1)
8635 value_range saved;
8637 if (vr0->type == VR_UNDEFINED)
8639 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8640 return;
8643 if (vr1->type == VR_UNDEFINED)
8645 /* VR0 already has the resulting range. */
8646 return;
8649 if (vr0->type == VR_VARYING)
8651 /* Nothing to do. VR0 already has the resulting range. */
8652 return;
8655 if (vr1->type == VR_VARYING)
8657 set_value_range_to_varying (vr0);
8658 return;
8661 saved = *vr0;
8662 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8663 vr1->type, vr1->min, vr1->max);
8664 if (vr0->type == VR_VARYING)
8666 /* Failed to find an efficient meet. Before giving up and setting
8667 the result to VARYING, see if we can at least derive a useful
8668 anti-range. FIXME, all this nonsense about distinguishing
8669 anti-ranges from ranges is necessary because of the odd
8670 semantics of range_includes_zero_p and friends. */
8671 if (((saved.type == VR_RANGE
8672 && range_includes_zero_p (saved.min, saved.max) == 0)
8673 || (saved.type == VR_ANTI_RANGE
8674 && range_includes_zero_p (saved.min, saved.max) == 1))
8675 && ((vr1->type == VR_RANGE
8676 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8677 || (vr1->type == VR_ANTI_RANGE
8678 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8680 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8682 /* Since this meet operation did not result from the meeting of
8683 two equivalent names, VR0 cannot have any equivalences. */
8684 if (vr0->equiv)
8685 bitmap_clear (vr0->equiv);
8686 return;
8689 set_value_range_to_varying (vr0);
8690 return;
8692 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8693 vr0->equiv);
8694 if (vr0->type == VR_VARYING)
8695 return;
8697 /* The resulting set of equivalences is always the intersection of
8698 the two sets. */
8699 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8700 bitmap_and_into (vr0->equiv, vr1->equiv);
8701 else if (vr0->equiv && !vr1->equiv)
8702 bitmap_clear (vr0->equiv);
8705 void
8706 vrp_meet (value_range *vr0, const value_range *vr1)
8708 if (dump_file && (dump_flags & TDF_DETAILS))
8710 fprintf (dump_file, "Meeting\n ");
8711 dump_value_range (dump_file, vr0);
8712 fprintf (dump_file, "\nand\n ");
8713 dump_value_range (dump_file, vr1);
8714 fprintf (dump_file, "\n");
8716 vrp_meet_1 (vr0, vr1);
8717 if (dump_file && (dump_flags & TDF_DETAILS))
8719 fprintf (dump_file, "to\n ");
8720 dump_value_range (dump_file, vr0);
8721 fprintf (dump_file, "\n");
8726 /* Visit all arguments for PHI node PHI that flow through executable
8727 edges. If a valid value range can be derived from all the incoming
8728 value ranges, set a new range in VR_RESULT. */
8730 static void
8731 extract_range_from_phi_node (gphi *phi, value_range *vr_result)
8733 size_t i;
8734 tree lhs = PHI_RESULT (phi);
8735 value_range *lhs_vr = get_value_range (lhs);
8736 bool first = true;
8737 int edges, old_edges;
8738 struct loop *l;
8740 if (dump_file && (dump_flags & TDF_DETAILS))
8742 fprintf (dump_file, "\nVisiting PHI node: ");
8743 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8746 bool may_simulate_backedge_again = false;
8747 edges = 0;
8748 for (i = 0; i < gimple_phi_num_args (phi); i++)
8750 edge e = gimple_phi_arg_edge (phi, i);
8752 if (dump_file && (dump_flags & TDF_DETAILS))
8754 fprintf (dump_file,
8755 " Argument #%d (%d -> %d %sexecutable)\n",
8756 (int) i, e->src->index, e->dest->index,
8757 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8760 if (e->flags & EDGE_EXECUTABLE)
8762 tree arg = PHI_ARG_DEF (phi, i);
8763 value_range vr_arg;
8765 ++edges;
8767 if (TREE_CODE (arg) == SSA_NAME)
8769 /* See if we are eventually going to change one of the args. */
8770 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
8771 if (! gimple_nop_p (def_stmt)
8772 && prop_simulate_again_p (def_stmt)
8773 && e->flags & EDGE_DFS_BACK)
8774 may_simulate_backedge_again = true;
8776 vr_arg = *(get_value_range (arg));
8777 /* Do not allow equivalences or symbolic ranges to leak in from
8778 backedges. That creates invalid equivalencies.
8779 See PR53465 and PR54767. */
8780 if (e->flags & EDGE_DFS_BACK)
8782 if (vr_arg.type == VR_RANGE
8783 || vr_arg.type == VR_ANTI_RANGE)
8785 vr_arg.equiv = NULL;
8786 if (symbolic_range_p (&vr_arg))
8788 vr_arg.type = VR_VARYING;
8789 vr_arg.min = NULL_TREE;
8790 vr_arg.max = NULL_TREE;
8794 else
8796 /* If the non-backedge arguments range is VR_VARYING then
8797 we can still try recording a simple equivalence. */
8798 if (vr_arg.type == VR_VARYING)
8800 vr_arg.type = VR_RANGE;
8801 vr_arg.min = arg;
8802 vr_arg.max = arg;
8803 vr_arg.equiv = NULL;
8807 else
8809 if (TREE_OVERFLOW_P (arg))
8810 arg = drop_tree_overflow (arg);
8812 vr_arg.type = VR_RANGE;
8813 vr_arg.min = arg;
8814 vr_arg.max = arg;
8815 vr_arg.equiv = NULL;
8818 if (dump_file && (dump_flags & TDF_DETAILS))
8820 fprintf (dump_file, "\t");
8821 print_generic_expr (dump_file, arg, dump_flags);
8822 fprintf (dump_file, ": ");
8823 dump_value_range (dump_file, &vr_arg);
8824 fprintf (dump_file, "\n");
8827 if (first)
8828 copy_value_range (vr_result, &vr_arg);
8829 else
8830 vrp_meet (vr_result, &vr_arg);
8831 first = false;
8833 if (vr_result->type == VR_VARYING)
8834 break;
8838 if (vr_result->type == VR_VARYING)
8839 goto varying;
8840 else if (vr_result->type == VR_UNDEFINED)
8841 goto update_range;
8843 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8844 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8846 /* To prevent infinite iterations in the algorithm, derive ranges
8847 when the new value is slightly bigger or smaller than the
8848 previous one. We don't do this if we have seen a new executable
8849 edge; this helps us avoid an overflow infinity for conditionals
8850 which are not in a loop. If the old value-range was VR_UNDEFINED
8851 use the updated range and iterate one more time. If we will not
8852 simulate this PHI again via the backedge allow us to iterate. */
8853 if (edges > 0
8854 && gimple_phi_num_args (phi) > 1
8855 && edges == old_edges
8856 && lhs_vr->type != VR_UNDEFINED
8857 && may_simulate_backedge_again)
8859 /* Compare old and new ranges, fall back to varying if the
8860 values are not comparable. */
8861 int cmp_min = compare_values (lhs_vr->min, vr_result->min);
8862 if (cmp_min == -2)
8863 goto varying;
8864 int cmp_max = compare_values (lhs_vr->max, vr_result->max);
8865 if (cmp_max == -2)
8866 goto varying;
8868 /* For non VR_RANGE or for pointers fall back to varying if
8869 the range changed. */
8870 if ((lhs_vr->type != VR_RANGE || vr_result->type != VR_RANGE
8871 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8872 && (cmp_min != 0 || cmp_max != 0))
8873 goto varying;
8875 /* If the new minimum is larger than the previous one
8876 retain the old value. If the new minimum value is smaller
8877 than the previous one and not -INF go all the way to -INF + 1.
8878 In the first case, to avoid infinite bouncing between different
8879 minimums, and in the other case to avoid iterating millions of
8880 times to reach -INF. Going to -INF + 1 also lets the following
8881 iteration compute whether there will be any overflow, at the
8882 expense of one additional iteration. */
8883 if (cmp_min < 0)
8884 vr_result->min = lhs_vr->min;
8885 else if (cmp_min > 0
8886 && !vrp_val_is_min (vr_result->min))
8887 vr_result->min
8888 = int_const_binop (PLUS_EXPR,
8889 vrp_val_min (TREE_TYPE (vr_result->min)),
8890 build_int_cst (TREE_TYPE (vr_result->min), 1));
8892 /* Similarly for the maximum value. */
8893 if (cmp_max > 0)
8894 vr_result->max = lhs_vr->max;
8895 else if (cmp_max < 0
8896 && !vrp_val_is_max (vr_result->max))
8897 vr_result->max
8898 = int_const_binop (MINUS_EXPR,
8899 vrp_val_max (TREE_TYPE (vr_result->min)),
8900 build_int_cst (TREE_TYPE (vr_result->min), 1));
8902 /* If we dropped either bound to +-INF then if this is a loop
8903 PHI node SCEV may known more about its value-range. */
8904 if (cmp_min > 0 || cmp_min < 0
8905 || cmp_max < 0 || cmp_max > 0)
8906 goto scev_check;
8908 goto infinite_check;
8911 goto update_range;
8913 varying:
8914 set_value_range_to_varying (vr_result);
8916 scev_check:
8917 /* If this is a loop PHI node SCEV may known more about its value-range.
8918 scev_check can be reached from two paths, one is a fall through from above
8919 "varying" label, the other is direct goto from code block which tries to
8920 avoid infinite simulation. */
8921 if ((l = loop_containing_stmt (phi))
8922 && l->header == gimple_bb (phi))
8923 adjust_range_with_scev (vr_result, l, phi, lhs);
8925 infinite_check:
8926 /* If we will end up with a (-INF, +INF) range, set it to
8927 VARYING. Same if the previous max value was invalid for
8928 the type and we end up with vr_result.min > vr_result.max. */
8929 if ((vr_result->type == VR_RANGE || vr_result->type == VR_ANTI_RANGE)
8930 && !((vrp_val_is_max (vr_result->max) && vrp_val_is_min (vr_result->min))
8931 || compare_values (vr_result->min, vr_result->max) > 0))
8933 else
8934 set_value_range_to_varying (vr_result);
8936 /* If the new range is different than the previous value, keep
8937 iterating. */
8938 update_range:
8939 return;
8942 /* Visit all arguments for PHI node PHI that flow through executable
8943 edges. If a valid value range can be derived from all the incoming
8944 value ranges, set a new range for the LHS of PHI. */
8946 static enum ssa_prop_result
8947 vrp_visit_phi_node (gphi *phi)
8949 tree lhs = PHI_RESULT (phi);
8950 value_range vr_result = VR_INITIALIZER;
8951 extract_range_from_phi_node (phi, &vr_result);
8952 if (update_value_range (lhs, &vr_result))
8954 if (dump_file && (dump_flags & TDF_DETAILS))
8956 fprintf (dump_file, "Found new range for ");
8957 print_generic_expr (dump_file, lhs, 0);
8958 fprintf (dump_file, ": ");
8959 dump_value_range (dump_file, &vr_result);
8960 fprintf (dump_file, "\n");
8963 if (vr_result.type == VR_VARYING)
8964 return SSA_PROP_VARYING;
8966 return SSA_PROP_INTERESTING;
8969 /* Nothing changed, don't add outgoing edges. */
8970 return SSA_PROP_NOT_INTERESTING;
8973 /* Simplify boolean operations if the source is known
8974 to be already a boolean. */
8975 static bool
8976 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8978 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8979 tree lhs, op0, op1;
8980 bool need_conversion;
8982 /* We handle only !=/== case here. */
8983 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8985 op0 = gimple_assign_rhs1 (stmt);
8986 if (!op_with_boolean_value_range_p (op0))
8987 return false;
8989 op1 = gimple_assign_rhs2 (stmt);
8990 if (!op_with_boolean_value_range_p (op1))
8991 return false;
8993 /* Reduce number of cases to handle to NE_EXPR. As there is no
8994 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8995 if (rhs_code == EQ_EXPR)
8997 if (TREE_CODE (op1) == INTEGER_CST)
8998 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8999 build_int_cst (TREE_TYPE (op1), 1));
9000 else
9001 return false;
9004 lhs = gimple_assign_lhs (stmt);
9005 need_conversion
9006 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
9008 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9009 if (need_conversion
9010 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9011 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9012 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9013 return false;
9015 /* For A != 0 we can substitute A itself. */
9016 if (integer_zerop (op1))
9017 gimple_assign_set_rhs_with_ops (gsi,
9018 need_conversion
9019 ? NOP_EXPR : TREE_CODE (op0), op0);
9020 /* For A != B we substitute A ^ B. Either with conversion. */
9021 else if (need_conversion)
9023 tree tem = make_ssa_name (TREE_TYPE (op0));
9024 gassign *newop
9025 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9026 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9027 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9028 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
9029 set_range_info (tem, VR_RANGE,
9030 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
9031 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
9032 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9034 /* Or without. */
9035 else
9036 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9037 update_stmt (gsi_stmt (*gsi));
9039 return true;
9042 /* Simplify a division or modulo operator to a right shift or
9043 bitwise and if the first operand is unsigned or is greater
9044 than zero and the second operand is an exact power of two.
9045 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9046 into just op0 if op0's range is known to be a subset of
9047 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9048 modulo. */
9050 static bool
9051 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9053 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9054 tree val = NULL;
9055 tree op0 = gimple_assign_rhs1 (stmt);
9056 tree op1 = gimple_assign_rhs2 (stmt);
9057 value_range *vr = get_value_range (op0);
9059 if (rhs_code == TRUNC_MOD_EXPR
9060 && TREE_CODE (op1) == INTEGER_CST
9061 && tree_int_cst_sgn (op1) == 1
9062 && range_int_cst_p (vr)
9063 && tree_int_cst_lt (vr->max, op1))
9065 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9066 || tree_int_cst_sgn (vr->min) >= 0
9067 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9068 vr->min))
9070 /* If op0 already has the range op0 % op1 has,
9071 then TRUNC_MOD_EXPR won't change anything. */
9072 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9073 gimple_assign_set_rhs_from_tree (&gsi, op0);
9074 update_stmt (stmt);
9075 return true;
9079 if (!integer_pow2p (op1))
9081 /* X % -Y can be only optimized into X % Y either if
9082 X is not INT_MIN, or Y is not -1. Fold it now, as after
9083 remove_range_assertions the range info might be not available
9084 anymore. */
9085 if (rhs_code == TRUNC_MOD_EXPR
9086 && fold_stmt (gsi, follow_single_use_edges))
9087 return true;
9088 return false;
9091 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9092 val = integer_one_node;
9093 else
9095 bool sop = false;
9097 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9099 if (val
9100 && sop
9101 && integer_onep (val)
9102 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9104 location_t location;
9106 if (!gimple_has_location (stmt))
9107 location = input_location;
9108 else
9109 location = gimple_location (stmt);
9110 warning_at (location, OPT_Wstrict_overflow,
9111 "assuming signed overflow does not occur when "
9112 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9116 if (val && integer_onep (val))
9118 tree t;
9120 if (rhs_code == TRUNC_DIV_EXPR)
9122 t = build_int_cst (integer_type_node, tree_log2 (op1));
9123 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9124 gimple_assign_set_rhs1 (stmt, op0);
9125 gimple_assign_set_rhs2 (stmt, t);
9127 else
9129 t = build_int_cst (TREE_TYPE (op1), 1);
9130 t = int_const_binop (MINUS_EXPR, op1, t);
9131 t = fold_convert (TREE_TYPE (op0), t);
9133 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9134 gimple_assign_set_rhs1 (stmt, op0);
9135 gimple_assign_set_rhs2 (stmt, t);
9138 update_stmt (stmt);
9139 return true;
9142 return false;
9145 /* Simplify a min or max if the ranges of the two operands are
9146 disjoint. Return true if we do simplify. */
9148 static bool
9149 simplify_min_or_max_using_ranges (gimple *stmt)
9151 tree op0 = gimple_assign_rhs1 (stmt);
9152 tree op1 = gimple_assign_rhs2 (stmt);
9153 bool sop = false;
9154 tree val;
9156 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9157 (LE_EXPR, op0, op1, &sop));
9158 if (!val)
9160 sop = false;
9161 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9162 (LT_EXPR, op0, op1, &sop));
9165 if (val)
9167 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9169 location_t location;
9171 if (!gimple_has_location (stmt))
9172 location = input_location;
9173 else
9174 location = gimple_location (stmt);
9175 warning_at (location, OPT_Wstrict_overflow,
9176 "assuming signed overflow does not occur when "
9177 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9180 /* VAL == TRUE -> OP0 < or <= op1
9181 VAL == FALSE -> OP0 > or >= op1. */
9182 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9183 == integer_zerop (val)) ? op0 : op1;
9184 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9185 gimple_assign_set_rhs_from_tree (&gsi, res);
9186 update_stmt (stmt);
9187 return true;
9190 return false;
9193 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9194 ABS_EXPR. If the operand is <= 0, then simplify the
9195 ABS_EXPR into a NEGATE_EXPR. */
9197 static bool
9198 simplify_abs_using_ranges (gimple *stmt)
9200 tree op = gimple_assign_rhs1 (stmt);
9201 value_range *vr = get_value_range (op);
9203 if (vr)
9205 tree val = NULL;
9206 bool sop = false;
9208 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9209 if (!val)
9211 /* The range is neither <= 0 nor > 0. Now see if it is
9212 either < 0 or >= 0. */
9213 sop = false;
9214 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9215 &sop);
9218 if (val)
9220 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9222 location_t location;
9224 if (!gimple_has_location (stmt))
9225 location = input_location;
9226 else
9227 location = gimple_location (stmt);
9228 warning_at (location, OPT_Wstrict_overflow,
9229 "assuming signed overflow does not occur when "
9230 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9233 gimple_assign_set_rhs1 (stmt, op);
9234 if (integer_zerop (val))
9235 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9236 else
9237 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9238 update_stmt (stmt);
9239 return true;
9243 return false;
9246 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9247 If all the bits that are being cleared by & are already
9248 known to be zero from VR, or all the bits that are being
9249 set by | are already known to be one from VR, the bit
9250 operation is redundant. */
9252 static bool
9253 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9255 tree op0 = gimple_assign_rhs1 (stmt);
9256 tree op1 = gimple_assign_rhs2 (stmt);
9257 tree op = NULL_TREE;
9258 value_range vr0 = VR_INITIALIZER;
9259 value_range vr1 = VR_INITIALIZER;
9260 wide_int may_be_nonzero0, may_be_nonzero1;
9261 wide_int must_be_nonzero0, must_be_nonzero1;
9262 wide_int mask;
9264 if (TREE_CODE (op0) == SSA_NAME)
9265 vr0 = *(get_value_range (op0));
9266 else if (is_gimple_min_invariant (op0))
9267 set_value_range_to_value (&vr0, op0, NULL);
9268 else
9269 return false;
9271 if (TREE_CODE (op1) == SSA_NAME)
9272 vr1 = *(get_value_range (op1));
9273 else if (is_gimple_min_invariant (op1))
9274 set_value_range_to_value (&vr1, op1, NULL);
9275 else
9276 return false;
9278 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9279 &must_be_nonzero0))
9280 return false;
9281 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9282 &must_be_nonzero1))
9283 return false;
9285 switch (gimple_assign_rhs_code (stmt))
9287 case BIT_AND_EXPR:
9288 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9289 if (mask == 0)
9291 op = op0;
9292 break;
9294 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9295 if (mask == 0)
9297 op = op1;
9298 break;
9300 break;
9301 case BIT_IOR_EXPR:
9302 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9303 if (mask == 0)
9305 op = op1;
9306 break;
9308 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9309 if (mask == 0)
9311 op = op0;
9312 break;
9314 break;
9315 default:
9316 gcc_unreachable ();
9319 if (op == NULL_TREE)
9320 return false;
9322 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9323 update_stmt (gsi_stmt (*gsi));
9324 return true;
9327 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9328 a known value range VR.
9330 If there is one and only one value which will satisfy the
9331 conditional, then return that value. Else return NULL.
9333 If signed overflow must be undefined for the value to satisfy
9334 the conditional, then set *STRICT_OVERFLOW_P to true. */
9336 static tree
9337 test_for_singularity (enum tree_code cond_code, tree op0,
9338 tree op1, value_range *vr,
9339 bool *strict_overflow_p)
9341 tree min = NULL;
9342 tree max = NULL;
9344 /* Extract minimum/maximum values which satisfy the conditional as it was
9345 written. */
9346 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9348 /* This should not be negative infinity; there is no overflow
9349 here. */
9350 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9352 max = op1;
9353 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9355 tree one = build_int_cst (TREE_TYPE (op0), 1);
9356 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9357 if (EXPR_P (max))
9358 TREE_NO_WARNING (max) = 1;
9361 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9363 /* This should not be positive infinity; there is no overflow
9364 here. */
9365 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9367 min = op1;
9368 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9370 tree one = build_int_cst (TREE_TYPE (op0), 1);
9371 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9372 if (EXPR_P (min))
9373 TREE_NO_WARNING (min) = 1;
9377 /* Now refine the minimum and maximum values using any
9378 value range information we have for op0. */
9379 if (min && max)
9381 if (compare_values (vr->min, min) == 1)
9382 min = vr->min;
9383 if (compare_values (vr->max, max) == -1)
9384 max = vr->max;
9386 /* If the new min/max values have converged to a single value,
9387 then there is only one value which can satisfy the condition,
9388 return that value. */
9389 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9391 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9392 && is_overflow_infinity (vr->max))
9393 *strict_overflow_p = true;
9394 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9395 && is_overflow_infinity (vr->min))
9396 *strict_overflow_p = true;
9398 return min;
9401 return NULL;
9404 /* Return whether the value range *VR fits in an integer type specified
9405 by PRECISION and UNSIGNED_P. */
9407 static bool
9408 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9410 tree src_type;
9411 unsigned src_precision;
9412 widest_int tem;
9413 signop src_sgn;
9415 /* We can only handle integral and pointer types. */
9416 src_type = TREE_TYPE (vr->min);
9417 if (!INTEGRAL_TYPE_P (src_type)
9418 && !POINTER_TYPE_P (src_type))
9419 return false;
9421 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9422 and so is an identity transform. */
9423 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9424 src_sgn = TYPE_SIGN (src_type);
9425 if ((src_precision < dest_precision
9426 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9427 || (src_precision == dest_precision && src_sgn == dest_sgn))
9428 return true;
9430 /* Now we can only handle ranges with constant bounds. */
9431 if (vr->type != VR_RANGE
9432 || TREE_CODE (vr->min) != INTEGER_CST
9433 || TREE_CODE (vr->max) != INTEGER_CST)
9434 return false;
9436 /* For sign changes, the MSB of the wide_int has to be clear.
9437 An unsigned value with its MSB set cannot be represented by
9438 a signed wide_int, while a negative value cannot be represented
9439 by an unsigned wide_int. */
9440 if (src_sgn != dest_sgn
9441 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9442 return false;
9444 /* Then we can perform the conversion on both ends and compare
9445 the result for equality. */
9446 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9447 if (tem != wi::to_widest (vr->min))
9448 return false;
9449 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9450 if (tem != wi::to_widest (vr->max))
9451 return false;
9453 return true;
9456 /* Simplify a conditional using a relational operator to an equality
9457 test if the range information indicates only one value can satisfy
9458 the original conditional. */
9460 static bool
9461 simplify_cond_using_ranges (gcond *stmt)
9463 tree op0 = gimple_cond_lhs (stmt);
9464 tree op1 = gimple_cond_rhs (stmt);
9465 enum tree_code cond_code = gimple_cond_code (stmt);
9467 if (cond_code != NE_EXPR
9468 && cond_code != EQ_EXPR
9469 && TREE_CODE (op0) == SSA_NAME
9470 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9471 && is_gimple_min_invariant (op1))
9473 value_range *vr = get_value_range (op0);
9475 /* If we have range information for OP0, then we might be
9476 able to simplify this conditional. */
9477 if (vr->type == VR_RANGE)
9479 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9480 bool sop = false;
9481 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9483 if (new_tree
9484 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9486 if (dump_file)
9488 fprintf (dump_file, "Simplified relational ");
9489 print_gimple_stmt (dump_file, stmt, 0, 0);
9490 fprintf (dump_file, " into ");
9493 gimple_cond_set_code (stmt, EQ_EXPR);
9494 gimple_cond_set_lhs (stmt, op0);
9495 gimple_cond_set_rhs (stmt, new_tree);
9497 update_stmt (stmt);
9499 if (dump_file)
9501 print_gimple_stmt (dump_file, stmt, 0, 0);
9502 fprintf (dump_file, "\n");
9505 if (sop && issue_strict_overflow_warning (wc))
9507 location_t location = input_location;
9508 if (gimple_has_location (stmt))
9509 location = gimple_location (stmt);
9511 warning_at (location, OPT_Wstrict_overflow,
9512 "assuming signed overflow does not occur when "
9513 "simplifying conditional");
9516 return true;
9519 /* Try again after inverting the condition. We only deal
9520 with integral types here, so no need to worry about
9521 issues with inverting FP comparisons. */
9522 sop = false;
9523 new_tree = test_for_singularity
9524 (invert_tree_comparison (cond_code, false),
9525 op0, op1, vr, &sop);
9527 if (new_tree
9528 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9530 if (dump_file)
9532 fprintf (dump_file, "Simplified relational ");
9533 print_gimple_stmt (dump_file, stmt, 0, 0);
9534 fprintf (dump_file, " into ");
9537 gimple_cond_set_code (stmt, NE_EXPR);
9538 gimple_cond_set_lhs (stmt, op0);
9539 gimple_cond_set_rhs (stmt, new_tree);
9541 update_stmt (stmt);
9543 if (dump_file)
9545 print_gimple_stmt (dump_file, stmt, 0, 0);
9546 fprintf (dump_file, "\n");
9549 if (sop && issue_strict_overflow_warning (wc))
9551 location_t location = input_location;
9552 if (gimple_has_location (stmt))
9553 location = gimple_location (stmt);
9555 warning_at (location, OPT_Wstrict_overflow,
9556 "assuming signed overflow does not occur when "
9557 "simplifying conditional");
9560 return true;
9565 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9566 see if OP0 was set by a type conversion where the source of
9567 the conversion is another SSA_NAME with a range that fits
9568 into the range of OP0's type.
9570 If so, the conversion is redundant as the earlier SSA_NAME can be
9571 used for the comparison directly if we just massage the constant in the
9572 comparison. */
9573 if (TREE_CODE (op0) == SSA_NAME
9574 && TREE_CODE (op1) == INTEGER_CST)
9576 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9577 tree innerop;
9579 if (!is_gimple_assign (def_stmt)
9580 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9581 return false;
9583 innerop = gimple_assign_rhs1 (def_stmt);
9585 if (TREE_CODE (innerop) == SSA_NAME
9586 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9587 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9588 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9590 value_range *vr = get_value_range (innerop);
9592 if (range_int_cst_p (vr)
9593 && range_fits_type_p (vr,
9594 TYPE_PRECISION (TREE_TYPE (op0)),
9595 TYPE_SIGN (TREE_TYPE (op0)))
9596 && int_fits_type_p (op1, TREE_TYPE (innerop))
9597 /* The range must not have overflowed, or if it did overflow
9598 we must not be wrapping/trapping overflow and optimizing
9599 with strict overflow semantics. */
9600 && ((!is_negative_overflow_infinity (vr->min)
9601 && !is_positive_overflow_infinity (vr->max))
9602 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9604 /* If the range overflowed and the user has asked for warnings
9605 when strict overflow semantics were used to optimize code,
9606 issue an appropriate warning. */
9607 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9608 && (is_negative_overflow_infinity (vr->min)
9609 || is_positive_overflow_infinity (vr->max))
9610 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9612 location_t location;
9614 if (!gimple_has_location (stmt))
9615 location = input_location;
9616 else
9617 location = gimple_location (stmt);
9618 warning_at (location, OPT_Wstrict_overflow,
9619 "assuming signed overflow does not occur when "
9620 "simplifying conditional");
9623 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9624 gimple_cond_set_lhs (stmt, innerop);
9625 gimple_cond_set_rhs (stmt, newconst);
9626 return true;
9631 return false;
9634 /* Simplify a switch statement using the value range of the switch
9635 argument. */
9637 static bool
9638 simplify_switch_using_ranges (gswitch *stmt)
9640 tree op = gimple_switch_index (stmt);
9641 value_range *vr = NULL;
9642 bool take_default;
9643 edge e;
9644 edge_iterator ei;
9645 size_t i = 0, j = 0, n, n2;
9646 tree vec2;
9647 switch_update su;
9648 size_t k = 1, l = 0;
9650 if (TREE_CODE (op) == SSA_NAME)
9652 vr = get_value_range (op);
9654 /* We can only handle integer ranges. */
9655 if ((vr->type != VR_RANGE
9656 && vr->type != VR_ANTI_RANGE)
9657 || symbolic_range_p (vr))
9658 return false;
9660 /* Find case label for min/max of the value range. */
9661 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9663 else if (TREE_CODE (op) == INTEGER_CST)
9665 take_default = !find_case_label_index (stmt, 1, op, &i);
9666 if (take_default)
9668 i = 1;
9669 j = 0;
9671 else
9673 j = i;
9676 else
9677 return false;
9679 n = gimple_switch_num_labels (stmt);
9681 /* We can truncate the case label ranges that partially overlap with OP's
9682 value range. */
9683 size_t min_idx = 1, max_idx = 0;
9684 if (vr != NULL)
9685 find_case_label_range (stmt, vr->min, vr->max, &min_idx, &max_idx);
9686 if (min_idx <= max_idx)
9688 tree min_label = gimple_switch_label (stmt, min_idx);
9689 tree max_label = gimple_switch_label (stmt, max_idx);
9691 /* Avoid changing the type of the case labels when truncating. */
9692 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
9693 tree vr_min = fold_convert (case_label_type, vr->min);
9694 tree vr_max = fold_convert (case_label_type, vr->max);
9696 if (vr->type == VR_RANGE)
9698 /* If OP's value range is [2,8] and the low label range is
9699 0 ... 3, truncate the label's range to 2 .. 3. */
9700 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9701 && CASE_HIGH (min_label) != NULL_TREE
9702 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9703 CASE_LOW (min_label) = vr_min;
9705 /* If OP's value range is [2,8] and the high label range is
9706 7 ... 10, truncate the label's range to 7 .. 8. */
9707 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9708 && CASE_HIGH (max_label) != NULL_TREE
9709 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9710 CASE_HIGH (max_label) = vr_max;
9712 else if (vr->type == VR_ANTI_RANGE)
9714 tree one_cst = build_one_cst (case_label_type);
9716 if (min_label == max_label)
9718 /* If OP's value range is ~[7,8] and the label's range is
9719 7 ... 10, truncate the label's range to 9 ... 10. */
9720 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
9721 && CASE_HIGH (min_label) != NULL_TREE
9722 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
9723 CASE_LOW (min_label)
9724 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9726 /* If OP's value range is ~[7,8] and the label's range is
9727 5 ... 8, truncate the label's range to 5 ... 6. */
9728 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9729 && CASE_HIGH (min_label) != NULL_TREE
9730 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
9731 CASE_HIGH (min_label)
9732 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9734 else
9736 /* If OP's value range is ~[2,8] and the low label range is
9737 0 ... 3, truncate the label's range to 0 ... 1. */
9738 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9739 && CASE_HIGH (min_label) != NULL_TREE
9740 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9741 CASE_HIGH (min_label)
9742 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9744 /* If OP's value range is ~[2,8] and the high label range is
9745 7 ... 10, truncate the label's range to 9 ... 10. */
9746 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9747 && CASE_HIGH (max_label) != NULL_TREE
9748 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9749 CASE_LOW (max_label)
9750 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9754 /* Canonicalize singleton case ranges. */
9755 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
9756 CASE_HIGH (min_label) = NULL_TREE;
9757 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
9758 CASE_HIGH (max_label) = NULL_TREE;
9761 /* We can also eliminate case labels that lie completely outside OP's value
9762 range. */
9764 /* Bail out if this is just all edges taken. */
9765 if (i == 1
9766 && j == n - 1
9767 && take_default)
9768 return false;
9770 /* Build a new vector of taken case labels. */
9771 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9772 n2 = 0;
9774 /* Add the default edge, if necessary. */
9775 if (take_default)
9776 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9778 for (; i <= j; ++i, ++n2)
9779 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9781 for (; k <= l; ++k, ++n2)
9782 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9784 /* Mark needed edges. */
9785 for (i = 0; i < n2; ++i)
9787 e = find_edge (gimple_bb (stmt),
9788 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9789 e->aux = (void *)-1;
9792 /* Queue not needed edges for later removal. */
9793 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9795 if (e->aux == (void *)-1)
9797 e->aux = NULL;
9798 continue;
9801 if (dump_file && (dump_flags & TDF_DETAILS))
9803 fprintf (dump_file, "removing unreachable case label\n");
9805 to_remove_edges.safe_push (e);
9806 e->flags &= ~EDGE_EXECUTABLE;
9809 /* And queue an update for the stmt. */
9810 su.stmt = stmt;
9811 su.vec = vec2;
9812 to_update_switch_stmts.safe_push (su);
9813 return false;
9816 /* Simplify an integral conversion from an SSA name in STMT. */
9818 static bool
9819 simplify_conversion_using_ranges (gimple *stmt)
9821 tree innerop, middleop, finaltype;
9822 gimple *def_stmt;
9823 signop inner_sgn, middle_sgn, final_sgn;
9824 unsigned inner_prec, middle_prec, final_prec;
9825 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9827 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9828 if (!INTEGRAL_TYPE_P (finaltype))
9829 return false;
9830 middleop = gimple_assign_rhs1 (stmt);
9831 def_stmt = SSA_NAME_DEF_STMT (middleop);
9832 if (!is_gimple_assign (def_stmt)
9833 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9834 return false;
9835 innerop = gimple_assign_rhs1 (def_stmt);
9836 if (TREE_CODE (innerop) != SSA_NAME
9837 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9838 return false;
9840 /* Get the value-range of the inner operand. Use get_range_info in
9841 case innerop was created during substitute-and-fold. */
9842 wide_int imin, imax;
9843 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
9844 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
9845 return false;
9846 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
9847 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
9849 /* Simulate the conversion chain to check if the result is equal if
9850 the middle conversion is removed. */
9851 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9852 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9853 final_prec = TYPE_PRECISION (finaltype);
9855 /* If the first conversion is not injective, the second must not
9856 be widening. */
9857 if (wi::gtu_p (innermax - innermin,
9858 wi::mask <widest_int> (middle_prec, false))
9859 && middle_prec < final_prec)
9860 return false;
9861 /* We also want a medium value so that we can track the effect that
9862 narrowing conversions with sign change have. */
9863 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9864 if (inner_sgn == UNSIGNED)
9865 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9866 else
9867 innermed = 0;
9868 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9869 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9870 innermed = innermin;
9872 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9873 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9874 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9875 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9877 /* Require that the final conversion applied to both the original
9878 and the intermediate range produces the same result. */
9879 final_sgn = TYPE_SIGN (finaltype);
9880 if (wi::ext (middlemin, final_prec, final_sgn)
9881 != wi::ext (innermin, final_prec, final_sgn)
9882 || wi::ext (middlemed, final_prec, final_sgn)
9883 != wi::ext (innermed, final_prec, final_sgn)
9884 || wi::ext (middlemax, final_prec, final_sgn)
9885 != wi::ext (innermax, final_prec, final_sgn))
9886 return false;
9888 gimple_assign_set_rhs1 (stmt, innerop);
9889 update_stmt (stmt);
9890 return true;
9893 /* Simplify a conversion from integral SSA name to float in STMT. */
9895 static bool
9896 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9897 gimple *stmt)
9899 tree rhs1 = gimple_assign_rhs1 (stmt);
9900 value_range *vr = get_value_range (rhs1);
9901 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9902 machine_mode mode;
9903 tree tem;
9904 gassign *conv;
9906 /* We can only handle constant ranges. */
9907 if (vr->type != VR_RANGE
9908 || TREE_CODE (vr->min) != INTEGER_CST
9909 || TREE_CODE (vr->max) != INTEGER_CST)
9910 return false;
9912 /* First check if we can use a signed type in place of an unsigned. */
9913 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9914 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9915 != CODE_FOR_nothing)
9916 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9917 mode = TYPE_MODE (TREE_TYPE (rhs1));
9918 /* If we can do the conversion in the current input mode do nothing. */
9919 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9920 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9921 return false;
9922 /* Otherwise search for a mode we can use, starting from the narrowest
9923 integer mode available. */
9924 else
9926 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9929 /* If we cannot do a signed conversion to float from mode
9930 or if the value-range does not fit in the signed type
9931 try with a wider mode. */
9932 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9933 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9934 break;
9936 mode = GET_MODE_WIDER_MODE (mode);
9937 /* But do not widen the input. Instead leave that to the
9938 optabs expansion code. */
9939 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9940 return false;
9942 while (mode != VOIDmode);
9943 if (mode == VOIDmode)
9944 return false;
9947 /* It works, insert a truncation or sign-change before the
9948 float conversion. */
9949 tem = make_ssa_name (build_nonstandard_integer_type
9950 (GET_MODE_PRECISION (mode), 0));
9951 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9952 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9953 gimple_assign_set_rhs1 (stmt, tem);
9954 update_stmt (stmt);
9956 return true;
9959 /* Simplify an internal fn call using ranges if possible. */
9961 static bool
9962 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9964 enum tree_code subcode;
9965 bool is_ubsan = false;
9966 bool ovf = false;
9967 switch (gimple_call_internal_fn (stmt))
9969 case IFN_UBSAN_CHECK_ADD:
9970 subcode = PLUS_EXPR;
9971 is_ubsan = true;
9972 break;
9973 case IFN_UBSAN_CHECK_SUB:
9974 subcode = MINUS_EXPR;
9975 is_ubsan = true;
9976 break;
9977 case IFN_UBSAN_CHECK_MUL:
9978 subcode = MULT_EXPR;
9979 is_ubsan = true;
9980 break;
9981 case IFN_ADD_OVERFLOW:
9982 subcode = PLUS_EXPR;
9983 break;
9984 case IFN_SUB_OVERFLOW:
9985 subcode = MINUS_EXPR;
9986 break;
9987 case IFN_MUL_OVERFLOW:
9988 subcode = MULT_EXPR;
9989 break;
9990 default:
9991 return false;
9994 tree op0 = gimple_call_arg (stmt, 0);
9995 tree op1 = gimple_call_arg (stmt, 1);
9996 tree type;
9997 if (is_ubsan)
9998 type = TREE_TYPE (op0);
9999 else if (gimple_call_lhs (stmt) == NULL_TREE)
10000 return false;
10001 else
10002 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
10003 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
10004 || (is_ubsan && ovf))
10005 return false;
10007 gimple *g;
10008 location_t loc = gimple_location (stmt);
10009 if (is_ubsan)
10010 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
10011 else
10013 int prec = TYPE_PRECISION (type);
10014 tree utype = type;
10015 if (ovf
10016 || !useless_type_conversion_p (type, TREE_TYPE (op0))
10017 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
10018 utype = build_nonstandard_integer_type (prec, 1);
10019 if (TREE_CODE (op0) == INTEGER_CST)
10020 op0 = fold_convert (utype, op0);
10021 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
10023 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
10024 gimple_set_location (g, loc);
10025 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10026 op0 = gimple_assign_lhs (g);
10028 if (TREE_CODE (op1) == INTEGER_CST)
10029 op1 = fold_convert (utype, op1);
10030 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
10032 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
10033 gimple_set_location (g, loc);
10034 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10035 op1 = gimple_assign_lhs (g);
10037 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
10038 gimple_set_location (g, loc);
10039 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10040 if (utype != type)
10042 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
10043 gimple_assign_lhs (g));
10044 gimple_set_location (g, loc);
10045 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10047 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
10048 gimple_assign_lhs (g),
10049 build_int_cst (type, ovf));
10051 gimple_set_location (g, loc);
10052 gsi_replace (gsi, g, false);
10053 return true;
10056 /* Return true if VAR is a two-valued variable. Set a and b with the
10057 two-values when it is true. Return false otherwise. */
10059 static bool
10060 two_valued_val_range_p (tree var, tree *a, tree *b)
10062 value_range *vr = get_value_range (var);
10063 if ((vr->type != VR_RANGE
10064 && vr->type != VR_ANTI_RANGE)
10065 || TREE_CODE (vr->min) != INTEGER_CST
10066 || TREE_CODE (vr->max) != INTEGER_CST)
10067 return false;
10069 if (vr->type == VR_RANGE
10070 && wi::sub (vr->max, vr->min) == 1)
10072 *a = vr->min;
10073 *b = vr->max;
10074 return true;
10077 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10078 if (vr->type == VR_ANTI_RANGE
10079 && wi::sub (vr->min, vrp_val_min (TREE_TYPE (var))) == 1
10080 && wi::sub (vrp_val_max (TREE_TYPE (var)), vr->max) == 1)
10082 *a = vrp_val_min (TREE_TYPE (var));
10083 *b = vrp_val_max (TREE_TYPE (var));
10084 return true;
10087 return false;
10090 /* Simplify STMT using ranges if possible. */
10092 static bool
10093 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
10095 gimple *stmt = gsi_stmt (*gsi);
10096 if (is_gimple_assign (stmt))
10098 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
10099 tree rhs1 = gimple_assign_rhs1 (stmt);
10100 tree rhs2 = gimple_assign_rhs2 (stmt);
10101 tree lhs = gimple_assign_lhs (stmt);
10102 tree val1 = NULL_TREE, val2 = NULL_TREE;
10103 use_operand_p use_p;
10104 gimple *use_stmt;
10106 /* Convert:
10107 LHS = CST BINOP VAR
10108 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10110 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10112 Also handles:
10113 LHS = VAR BINOP CST
10114 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10116 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10118 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
10119 && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10120 && ((TREE_CODE (rhs1) == INTEGER_CST
10121 && TREE_CODE (rhs2) == SSA_NAME)
10122 || (TREE_CODE (rhs2) == INTEGER_CST
10123 && TREE_CODE (rhs1) == SSA_NAME))
10124 && single_imm_use (lhs, &use_p, &use_stmt)
10125 && gimple_code (use_stmt) == GIMPLE_COND)
10128 tree new_rhs1 = NULL_TREE;
10129 tree new_rhs2 = NULL_TREE;
10130 tree cmp_var = NULL_TREE;
10132 if (TREE_CODE (rhs2) == SSA_NAME
10133 && two_valued_val_range_p (rhs2, &val1, &val2))
10135 /* Optimize RHS1 OP [VAL1, VAL2]. */
10136 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
10137 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
10138 cmp_var = rhs2;
10140 else if (TREE_CODE (rhs1) == SSA_NAME
10141 && two_valued_val_range_p (rhs1, &val1, &val2))
10143 /* Optimize [VAL1, VAL2] OP RHS2. */
10144 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
10145 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
10146 cmp_var = rhs1;
10149 /* If we could not find two-vals or the optimzation is invalid as
10150 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10151 if (new_rhs1 && new_rhs2)
10153 tree cond = build2 (EQ_EXPR, TREE_TYPE (cmp_var), cmp_var, val1);
10154 gimple_assign_set_rhs_with_ops (gsi,
10155 COND_EXPR, cond,
10156 new_rhs1,
10157 new_rhs2);
10158 update_stmt (gsi_stmt (*gsi));
10159 return true;
10163 switch (rhs_code)
10165 case EQ_EXPR:
10166 case NE_EXPR:
10167 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10168 if the RHS is zero or one, and the LHS are known to be boolean
10169 values. */
10170 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10171 return simplify_truth_ops_using_ranges (gsi, stmt);
10172 break;
10174 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10175 and BIT_AND_EXPR respectively if the first operand is greater
10176 than zero and the second operand is an exact power of two.
10177 Also optimize TRUNC_MOD_EXPR away if the second operand is
10178 constant and the first operand already has the right value
10179 range. */
10180 case TRUNC_DIV_EXPR:
10181 case TRUNC_MOD_EXPR:
10182 if (TREE_CODE (rhs1) == SSA_NAME
10183 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10184 return simplify_div_or_mod_using_ranges (gsi, stmt);
10185 break;
10187 /* Transform ABS (X) into X or -X as appropriate. */
10188 case ABS_EXPR:
10189 if (TREE_CODE (rhs1) == SSA_NAME
10190 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10191 return simplify_abs_using_ranges (stmt);
10192 break;
10194 case BIT_AND_EXPR:
10195 case BIT_IOR_EXPR:
10196 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10197 if all the bits being cleared are already cleared or
10198 all the bits being set are already set. */
10199 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10200 return simplify_bit_ops_using_ranges (gsi, stmt);
10201 break;
10203 CASE_CONVERT:
10204 if (TREE_CODE (rhs1) == SSA_NAME
10205 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10206 return simplify_conversion_using_ranges (stmt);
10207 break;
10209 case FLOAT_EXPR:
10210 if (TREE_CODE (rhs1) == SSA_NAME
10211 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10212 return simplify_float_conversion_using_ranges (gsi, stmt);
10213 break;
10215 case MIN_EXPR:
10216 case MAX_EXPR:
10217 return simplify_min_or_max_using_ranges (stmt);
10218 break;
10220 default:
10221 break;
10224 else if (gimple_code (stmt) == GIMPLE_COND)
10225 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
10226 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10227 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10228 else if (is_gimple_call (stmt)
10229 && gimple_call_internal_p (stmt))
10230 return simplify_internal_call_using_ranges (gsi, stmt);
10232 return false;
10235 /* If the statement pointed by SI has a predicate whose value can be
10236 computed using the value range information computed by VRP, compute
10237 its value and return true. Otherwise, return false. */
10239 static bool
10240 fold_predicate_in (gimple_stmt_iterator *si)
10242 bool assignment_p = false;
10243 tree val;
10244 gimple *stmt = gsi_stmt (*si);
10246 if (is_gimple_assign (stmt)
10247 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10249 assignment_p = true;
10250 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10251 gimple_assign_rhs1 (stmt),
10252 gimple_assign_rhs2 (stmt),
10253 stmt);
10255 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10256 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10257 gimple_cond_lhs (cond_stmt),
10258 gimple_cond_rhs (cond_stmt),
10259 stmt);
10260 else
10261 return false;
10263 if (val)
10265 if (assignment_p)
10266 val = fold_convert (gimple_expr_type (stmt), val);
10268 if (dump_file)
10270 fprintf (dump_file, "Folding predicate ");
10271 print_gimple_expr (dump_file, stmt, 0, 0);
10272 fprintf (dump_file, " to ");
10273 print_generic_expr (dump_file, val, 0);
10274 fprintf (dump_file, "\n");
10277 if (is_gimple_assign (stmt))
10278 gimple_assign_set_rhs_from_tree (si, val);
10279 else
10281 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10282 gcond *cond_stmt = as_a <gcond *> (stmt);
10283 if (integer_zerop (val))
10284 gimple_cond_make_false (cond_stmt);
10285 else if (integer_onep (val))
10286 gimple_cond_make_true (cond_stmt);
10287 else
10288 gcc_unreachable ();
10291 return true;
10294 return false;
10297 /* Callback for substitute_and_fold folding the stmt at *SI. */
10299 static bool
10300 vrp_fold_stmt (gimple_stmt_iterator *si)
10302 if (fold_predicate_in (si))
10303 return true;
10305 return simplify_stmt_using_ranges (si);
10308 /* Unwindable const/copy equivalences. */
10309 const_and_copies *equiv_stack;
10311 /* A trivial wrapper so that we can present the generic jump threading
10312 code with a simple API for simplifying statements. STMT is the
10313 statement we want to simplify, WITHIN_STMT provides the location
10314 for any overflow warnings. */
10316 static tree
10317 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10318 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10320 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10321 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10322 gimple_cond_lhs (cond_stmt),
10323 gimple_cond_rhs (cond_stmt),
10324 within_stmt);
10326 /* We simplify a switch statement by trying to determine which case label
10327 will be taken. If we are successful then we return the corresponding
10328 CASE_LABEL_EXPR. */
10329 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
10331 tree op = gimple_switch_index (switch_stmt);
10332 if (TREE_CODE (op) != SSA_NAME)
10333 return NULL_TREE;
10335 value_range *vr = get_value_range (op);
10336 if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE)
10337 || symbolic_range_p (vr))
10338 return NULL_TREE;
10340 if (vr->type == VR_RANGE)
10342 size_t i, j;
10343 /* Get the range of labels that contain a part of the operand's
10344 value range. */
10345 find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j);
10347 /* Is there only one such label? */
10348 if (i == j)
10350 tree label = gimple_switch_label (switch_stmt, i);
10352 /* The i'th label will be taken only if the value range of the
10353 operand is entirely within the bounds of this label. */
10354 if (CASE_HIGH (label) != NULL_TREE
10355 ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0
10356 && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0)
10357 : (tree_int_cst_equal (CASE_LOW (label), vr->min)
10358 && tree_int_cst_equal (vr->min, vr->max)))
10359 return label;
10362 /* If there are no such labels then the default label will be
10363 taken. */
10364 if (i > j)
10365 return gimple_switch_label (switch_stmt, 0);
10368 if (vr->type == VR_ANTI_RANGE)
10370 unsigned n = gimple_switch_num_labels (switch_stmt);
10371 tree min_label = gimple_switch_label (switch_stmt, 1);
10372 tree max_label = gimple_switch_label (switch_stmt, n - 1);
10374 /* The default label will be taken only if the anti-range of the
10375 operand is entirely outside the bounds of all the (non-default)
10376 case labels. */
10377 if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0
10378 && (CASE_HIGH (max_label) != NULL_TREE
10379 ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0
10380 : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0))
10381 return gimple_switch_label (switch_stmt, 0);
10384 return NULL_TREE;
10387 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10389 value_range new_vr = VR_INITIALIZER;
10390 tree lhs = gimple_assign_lhs (assign_stmt);
10392 if (TREE_CODE (lhs) == SSA_NAME
10393 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10394 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10396 extract_range_from_assignment (&new_vr, assign_stmt);
10397 if (range_int_cst_singleton_p (&new_vr))
10398 return new_vr.min;
10402 return NULL_TREE;
10405 /* Blocks which have more than one predecessor and more than
10406 one successor present jump threading opportunities, i.e.,
10407 when the block is reached from a specific predecessor, we
10408 may be able to determine which of the outgoing edges will
10409 be traversed. When this optimization applies, we are able
10410 to avoid conditionals at runtime and we may expose secondary
10411 optimization opportunities.
10413 This routine is effectively a driver for the generic jump
10414 threading code. It basically just presents the generic code
10415 with edges that may be suitable for jump threading.
10417 Unlike DOM, we do not iterate VRP if jump threading was successful.
10418 While iterating may expose new opportunities for VRP, it is expected
10419 those opportunities would be very limited and the compile time cost
10420 to expose those opportunities would be significant.
10422 As jump threading opportunities are discovered, they are registered
10423 for later realization. */
10425 static void
10426 identify_jump_threads (void)
10428 basic_block bb;
10429 gcond *dummy;
10430 int i;
10431 edge e;
10433 /* Ugh. When substituting values earlier in this pass we can
10434 wipe the dominance information. So rebuild the dominator
10435 information as we need it within the jump threading code. */
10436 calculate_dominance_info (CDI_DOMINATORS);
10438 /* We do not allow VRP information to be used for jump threading
10439 across a back edge in the CFG. Otherwise it becomes too
10440 difficult to avoid eliminating loop exit tests. Of course
10441 EDGE_DFS_BACK is not accurate at this time so we have to
10442 recompute it. */
10443 mark_dfs_back_edges ();
10445 /* Do not thread across edges we are about to remove. Just marking
10446 them as EDGE_IGNORE will do. */
10447 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10448 e->flags |= EDGE_IGNORE;
10450 /* Allocate our unwinder stack to unwind any temporary equivalences
10451 that might be recorded. */
10452 equiv_stack = new const_and_copies ();
10454 /* To avoid lots of silly node creation, we create a single
10455 conditional and just modify it in-place when attempting to
10456 thread jumps. */
10457 dummy = gimple_build_cond (EQ_EXPR,
10458 integer_zero_node, integer_zero_node,
10459 NULL, NULL);
10461 /* Walk through all the blocks finding those which present a
10462 potential jump threading opportunity. We could set this up
10463 as a dominator walker and record data during the walk, but
10464 I doubt it's worth the effort for the classes of jump
10465 threading opportunities we are trying to identify at this
10466 point in compilation. */
10467 FOR_EACH_BB_FN (bb, cfun)
10469 gimple *last;
10471 /* If the generic jump threading code does not find this block
10472 interesting, then there is nothing to do. */
10473 if (! potentially_threadable_block (bb))
10474 continue;
10476 last = last_stmt (bb);
10478 /* We're basically looking for a switch or any kind of conditional with
10479 integral or pointer type arguments. Note the type of the second
10480 argument will be the same as the first argument, so no need to
10481 check it explicitly.
10483 We also handle the case where there are no statements in the
10484 block. This come up with forwarder blocks that are not
10485 optimized away because they lead to a loop header. But we do
10486 want to thread through them as we can sometimes thread to the
10487 loop exit which is obviously profitable. */
10488 if (!last
10489 || gimple_code (last) == GIMPLE_SWITCH
10490 || (gimple_code (last) == GIMPLE_COND
10491 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10492 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10493 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10494 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10495 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10497 edge_iterator ei;
10499 /* We've got a block with multiple predecessors and multiple
10500 successors which also ends in a suitable conditional or
10501 switch statement. For each predecessor, see if we can thread
10502 it to a specific successor. */
10503 FOR_EACH_EDGE (e, ei, bb->preds)
10505 /* Do not thread across edges marked to ignoreor abnormal
10506 edges in the CFG. */
10507 if (e->flags & (EDGE_IGNORE | EDGE_COMPLEX))
10508 continue;
10510 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10511 simplify_stmt_for_jump_threading);
10516 /* Clear EDGE_IGNORE. */
10517 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10518 e->flags &= ~EDGE_IGNORE;
10520 /* We do not actually update the CFG or SSA graphs at this point as
10521 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10522 handle ASSERT_EXPRs gracefully. */
10525 /* We identified all the jump threading opportunities earlier, but could
10526 not transform the CFG at that time. This routine transforms the
10527 CFG and arranges for the dominator tree to be rebuilt if necessary.
10529 Note the SSA graph update will occur during the normal TODO
10530 processing by the pass manager. */
10531 static void
10532 finalize_jump_threads (void)
10534 thread_through_all_blocks (false);
10535 delete equiv_stack;
10538 /* Free VRP lattice. */
10540 static void
10541 vrp_free_lattice ()
10543 /* Free allocated memory. */
10544 free (vr_value);
10545 free (vr_phi_edge_counts);
10546 bitmap_obstack_release (&vrp_equiv_obstack);
10547 vrp_value_range_pool.release ();
10549 /* So that we can distinguish between VRP data being available
10550 and not available. */
10551 vr_value = NULL;
10552 vr_phi_edge_counts = NULL;
10555 /* Traverse all the blocks folding conditionals with known ranges. */
10557 static void
10558 vrp_finalize (bool warn_array_bounds_p)
10560 size_t i;
10562 values_propagated = true;
10564 if (dump_file)
10566 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10567 dump_all_value_ranges (dump_file);
10568 fprintf (dump_file, "\n");
10571 /* Set value range to non pointer SSA_NAMEs. */
10572 for (i = 0; i < num_vr_values; i++)
10573 if (vr_value[i])
10575 tree name = ssa_name (i);
10577 if (!name
10578 || POINTER_TYPE_P (TREE_TYPE (name))
10579 || (vr_value[i]->type == VR_VARYING)
10580 || (vr_value[i]->type == VR_UNDEFINED))
10581 continue;
10583 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10584 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10585 && (vr_value[i]->type == VR_RANGE
10586 || vr_value[i]->type == VR_ANTI_RANGE))
10587 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10588 vr_value[i]->max);
10591 substitute_and_fold (op_with_constant_singleton_value_range,
10592 vrp_fold_stmt, false);
10594 if (warn_array_bounds && warn_array_bounds_p)
10595 check_all_array_refs ();
10597 /* We must identify jump threading opportunities before we release
10598 the datastructures built by VRP. */
10599 identify_jump_threads ();
10602 /* evrp_dom_walker visits the basic blocks in the dominance order and set
10603 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
10604 discover more VRs. */
10606 class evrp_dom_walker : public dom_walker
10608 public:
10609 evrp_dom_walker ()
10610 : dom_walker (CDI_DOMINATORS), stack (10)
10612 stmts_to_fixup.create (0);
10613 need_eh_cleanup = BITMAP_ALLOC (NULL);
10615 ~evrp_dom_walker ()
10617 stmts_to_fixup.release ();
10618 BITMAP_FREE (need_eh_cleanup);
10620 virtual edge before_dom_children (basic_block);
10621 virtual void after_dom_children (basic_block);
10622 void push_value_range (const_tree var, value_range *vr);
10623 value_range *pop_value_range (const_tree var);
10625 /* Cond_stack holds the old VR. */
10626 auto_vec<std::pair <const_tree, value_range*> > stack;
10627 bitmap need_eh_cleanup;
10628 vec<gimple *> stmts_to_fixup;
10631 /* See if there is any new scope is entered with new VR and set that VR to
10632 ssa_name before visiting the statements in the scope. */
10634 edge
10635 evrp_dom_walker::before_dom_children (basic_block bb)
10637 value_range *new_vr = NULL;
10638 tree op0 = NULL_TREE;
10640 push_value_range (NULL_TREE, NULL);
10641 if (single_pred_p (bb))
10643 edge e = single_pred_edge (bb);
10644 value_range vr = VR_INITIALIZER;
10645 gimple *stmt = last_stmt (e->src);
10646 if (stmt
10647 && gimple_code (stmt) == GIMPLE_COND
10648 && (op0 = gimple_cond_lhs (stmt))
10649 && TREE_CODE (op0) == SSA_NAME
10650 && INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt))))
10652 /* Entering a new scope. Try to see if we can find a VR
10653 here. */
10654 tree op1 = gimple_cond_rhs (stmt);
10655 tree_code code = gimple_cond_code (stmt);
10656 value_range *old_vr = get_value_range (op0);
10658 if (TREE_OVERFLOW_P (op1))
10659 op1 = drop_tree_overflow (op1);
10661 /* If condition is false, invert the cond. */
10662 if (e->flags & EDGE_FALSE_VALUE)
10663 code = invert_tree_comparison (gimple_cond_code (stmt),
10664 HONOR_NANS (op0));
10665 /* Discover VR when condition is true. */
10666 extract_range_for_var_from_comparison_expr (op0, code, op0, op1, &vr);
10667 if (old_vr->type == VR_RANGE || old_vr->type == VR_ANTI_RANGE)
10668 vrp_intersect_ranges (&vr, old_vr);
10670 /* If we found any usable VR, set the VR to ssa_name and create a
10671 PUSH old value in the stack with the old VR. */
10672 if (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE)
10674 new_vr = vrp_value_range_pool.allocate ();
10675 *new_vr = vr;
10676 push_value_range (op0, new_vr);
10681 /* Visit PHI stmts and discover any new VRs possible. */
10682 gimple_stmt_iterator gsi;
10683 edge e;
10684 edge_iterator ei;
10685 bool has_unvisived_preds = false;
10687 FOR_EACH_EDGE (e, ei, bb->preds)
10688 if (!(e->src->flags & BB_VISITED))
10690 has_unvisived_preds = true;
10691 break;
10694 for (gphi_iterator gpi = gsi_start_phis (bb);
10695 !gsi_end_p (gpi); gsi_next (&gpi))
10697 gphi *phi = gpi.phi ();
10698 tree lhs = PHI_RESULT (phi);
10699 value_range vr_result = VR_INITIALIZER;
10700 if (!has_unvisived_preds
10701 && stmt_interesting_for_vrp (phi))
10702 extract_range_from_phi_node (phi, &vr_result);
10703 else
10704 set_value_range_to_varying (&vr_result);
10705 update_value_range (lhs, &vr_result);
10708 /* Visit all other stmts and discover any new VRs possible. */
10709 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
10711 gimple *stmt = gsi_stmt (gsi);
10712 edge taken_edge;
10713 tree output = NULL_TREE;
10714 gimple *old_stmt = stmt;
10715 bool was_noreturn = (is_gimple_call (stmt)
10716 && gimple_call_noreturn_p (stmt));
10718 /* TODO, if found taken_edge, we should visit (return it) and travel
10719 again to improve VR as done in DOM/SCCVN optimizations. It should
10720 be done carefully as stmts might prematurely leave a BB like
10721 in EH. */
10722 if (stmt_interesting_for_vrp (stmt))
10724 value_range vr = VR_INITIALIZER;
10725 extract_range_from_stmt (stmt, &taken_edge, &output, &vr);
10726 if (output
10727 && (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE))
10728 update_value_range (output, &vr);
10729 else
10731 tree def;
10732 ssa_op_iter iter;
10733 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
10734 set_value_range_to_varying (get_value_range (def));
10737 /* Try folding stmts with the VR discovered. */
10738 bool did_replace
10739 = replace_uses_in (stmt,
10740 op_with_constant_singleton_value_range);
10741 if (fold_stmt (&gsi, follow_single_use_edges)
10742 || did_replace)
10743 update_stmt (gsi_stmt (gsi));
10745 if (did_replace)
10747 /* If we cleaned up EH information from the statement,
10748 remove EH edges. */
10749 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
10750 bitmap_set_bit (need_eh_cleanup, bb->index);
10752 /* If we turned a not noreturn call into a noreturn one
10753 schedule it for fixup. */
10754 if (!was_noreturn
10755 && is_gimple_call (stmt)
10756 && gimple_call_noreturn_p (stmt))
10757 stmts_to_fixup.safe_push (stmt);
10759 if (gimple_assign_single_p (stmt))
10761 tree rhs = gimple_assign_rhs1 (stmt);
10762 if (TREE_CODE (rhs) == ADDR_EXPR)
10763 recompute_tree_invariant_for_addr_expr (rhs);
10767 def_operand_p def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
10768 /* Set the SSA with the value range. */
10769 if (def_p
10770 && TREE_CODE (DEF_FROM_PTR (def_p)) == SSA_NAME
10771 && INTEGRAL_TYPE_P (TREE_TYPE (DEF_FROM_PTR (def_p))))
10773 tree def = DEF_FROM_PTR (def_p);
10774 value_range *vr = get_value_range (def);
10776 if ((vr->type == VR_RANGE
10777 || vr->type == VR_ANTI_RANGE)
10778 && (TREE_CODE (vr->min) == INTEGER_CST)
10779 && (TREE_CODE (vr->max) == INTEGER_CST))
10780 set_range_info (def, vr->type, vr->min, vr->max);
10783 else
10785 tree def;
10786 ssa_op_iter iter;
10787 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
10788 set_value_range_to_varying (get_value_range (def));
10791 bb->flags |= BB_VISITED;
10792 return NULL;
10795 /* Restore/pop VRs valid only for BB when we leave BB. */
10797 void
10798 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
10800 gcc_checking_assert (!stack.is_empty ());
10801 while (stack.last ().first != NULL_TREE)
10802 pop_value_range (stack.last ().first);
10803 pop_value_range (stack.last ().first);
10806 /* Push the Value Range of VAR to the stack and update it with new VR. */
10808 void
10809 evrp_dom_walker::push_value_range (const_tree var, value_range *vr)
10811 if (vr != NULL)
10813 unsigned ver = SSA_NAME_VERSION (var);
10814 gcc_checking_assert (vr_value);
10815 stack.safe_push (std::make_pair (var, vr_value[ver]));
10817 if (ver < num_vr_values)
10818 vr_value[ver] = vr;
10820 else
10821 stack.safe_push (std::make_pair (var, vr));
10824 /* Pop the Value Range from the vrp_stack and update VAR with it. */
10826 value_range *
10827 evrp_dom_walker::pop_value_range (const_tree var)
10829 value_range *vr = stack.last ().second;
10830 if (vr != NULL)
10832 unsigned ver = SSA_NAME_VERSION (var);
10833 gcc_checking_assert (var == stack.last ().first);
10834 gcc_checking_assert (vr_value);
10836 if (ver < num_vr_values)
10837 vr_value[ver] = vr;
10839 stack.pop ();
10840 return vr;
10844 /* Main entry point for the early vrp pass which is a simplified non-iterative
10845 version of vrp where basic blocks are visited in dominance order. Value
10846 ranges discovered in early vrp will also be used by ipa-vrp. */
10848 static unsigned int
10849 execute_early_vrp ()
10851 edge e;
10852 edge_iterator ei;
10853 basic_block bb;
10855 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10856 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10857 scev_initialize ();
10858 calculate_dominance_info (CDI_DOMINATORS);
10859 FOR_EACH_BB_FN (bb, cfun)
10861 bb->flags &= ~BB_VISITED;
10862 FOR_EACH_EDGE (e, ei, bb->preds)
10863 e->flags |= EDGE_EXECUTABLE;
10865 vrp_initialize_lattice ();
10867 /* Walk stmts in dominance order and propagate VRP. */
10868 evrp_dom_walker walker;
10869 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
10871 if (!bitmap_empty_p (walker.need_eh_cleanup))
10872 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
10874 /* Fixup stmts that became noreturn calls. This may require splitting
10875 blocks and thus isn't possible during the dominator walk. Do this
10876 in reverse order so we don't inadvertedly remove a stmt we want to
10877 fixup by visiting a dominating now noreturn call first. */
10878 while (!walker.stmts_to_fixup.is_empty ())
10880 gimple *stmt = walker.stmts_to_fixup.pop ();
10881 fixup_noreturn_call (stmt);
10884 if (dump_file)
10886 fprintf (dump_file, "\nValue ranges after Early VRP:\n\n");
10887 dump_all_value_ranges (dump_file);
10888 fprintf (dump_file, "\n");
10890 vrp_free_lattice ();
10891 scev_finalize ();
10892 loop_optimizer_finalize ();
10893 FOR_EACH_BB_FN (bb, cfun)
10894 bb->flags &= ~BB_VISITED;
10895 return 0;
10899 /* Main entry point to VRP (Value Range Propagation). This pass is
10900 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10901 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10902 Programming Language Design and Implementation, pp. 67-78, 1995.
10903 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10905 This is essentially an SSA-CCP pass modified to deal with ranges
10906 instead of constants.
10908 While propagating ranges, we may find that two or more SSA name
10909 have equivalent, though distinct ranges. For instance,
10911 1 x_9 = p_3->a;
10912 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10913 3 if (p_4 == q_2)
10914 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10915 5 endif
10916 6 if (q_2)
10918 In the code above, pointer p_5 has range [q_2, q_2], but from the
10919 code we can also determine that p_5 cannot be NULL and, if q_2 had
10920 a non-varying range, p_5's range should also be compatible with it.
10922 These equivalences are created by two expressions: ASSERT_EXPR and
10923 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10924 result of another assertion, then we can use the fact that p_5 and
10925 p_4 are equivalent when evaluating p_5's range.
10927 Together with value ranges, we also propagate these equivalences
10928 between names so that we can take advantage of information from
10929 multiple ranges when doing final replacement. Note that this
10930 equivalency relation is transitive but not symmetric.
10932 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10933 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10934 in contexts where that assertion does not hold (e.g., in line 6).
10936 TODO, the main difference between this pass and Patterson's is that
10937 we do not propagate edge probabilities. We only compute whether
10938 edges can be taken or not. That is, instead of having a spectrum
10939 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10940 DON'T KNOW. In the future, it may be worthwhile to propagate
10941 probabilities to aid branch prediction. */
10943 static unsigned int
10944 execute_vrp (bool warn_array_bounds_p)
10946 int i;
10947 edge e;
10948 switch_update *su;
10950 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10951 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10952 scev_initialize ();
10954 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10955 Inserting assertions may split edges which will invalidate
10956 EDGE_DFS_BACK. */
10957 insert_range_assertions ();
10959 to_remove_edges.create (10);
10960 to_update_switch_stmts.create (5);
10961 threadedge_initialize_values ();
10963 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10964 mark_dfs_back_edges ();
10966 vrp_initialize_lattice ();
10967 vrp_initialize ();
10968 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10969 vrp_finalize (warn_array_bounds_p);
10970 vrp_free_lattice ();
10972 free_numbers_of_iterations_estimates (cfun);
10974 /* ASSERT_EXPRs must be removed before finalizing jump threads
10975 as finalizing jump threads calls the CFG cleanup code which
10976 does not properly handle ASSERT_EXPRs. */
10977 remove_range_assertions ();
10979 /* If we exposed any new variables, go ahead and put them into
10980 SSA form now, before we handle jump threading. This simplifies
10981 interactions between rewriting of _DECL nodes into SSA form
10982 and rewriting SSA_NAME nodes into SSA form after block
10983 duplication and CFG manipulation. */
10984 update_ssa (TODO_update_ssa);
10986 finalize_jump_threads ();
10988 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10989 CFG in a broken state and requires a cfg_cleanup run. */
10990 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10991 remove_edge (e);
10992 /* Update SWITCH_EXPR case label vector. */
10993 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10995 size_t j;
10996 size_t n = TREE_VEC_LENGTH (su->vec);
10997 tree label;
10998 gimple_switch_set_num_labels (su->stmt, n);
10999 for (j = 0; j < n; j++)
11000 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
11001 /* As we may have replaced the default label with a regular one
11002 make sure to make it a real default label again. This ensures
11003 optimal expansion. */
11004 label = gimple_switch_label (su->stmt, 0);
11005 CASE_LOW (label) = NULL_TREE;
11006 CASE_HIGH (label) = NULL_TREE;
11009 if (to_remove_edges.length () > 0)
11011 free_dominance_info (CDI_DOMINATORS);
11012 loops_state_set (LOOPS_NEED_FIXUP);
11015 to_remove_edges.release ();
11016 to_update_switch_stmts.release ();
11017 threadedge_finalize_values ();
11019 scev_finalize ();
11020 loop_optimizer_finalize ();
11021 return 0;
11024 namespace {
11026 const pass_data pass_data_vrp =
11028 GIMPLE_PASS, /* type */
11029 "vrp", /* name */
11030 OPTGROUP_NONE, /* optinfo_flags */
11031 TV_TREE_VRP, /* tv_id */
11032 PROP_ssa, /* properties_required */
11033 0, /* properties_provided */
11034 0, /* properties_destroyed */
11035 0, /* todo_flags_start */
11036 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
11039 class pass_vrp : public gimple_opt_pass
11041 public:
11042 pass_vrp (gcc::context *ctxt)
11043 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
11046 /* opt_pass methods: */
11047 opt_pass * clone () { return new pass_vrp (m_ctxt); }
11048 void set_pass_param (unsigned int n, bool param)
11050 gcc_assert (n == 0);
11051 warn_array_bounds_p = param;
11053 virtual bool gate (function *) { return flag_tree_vrp != 0; }
11054 virtual unsigned int execute (function *)
11055 { return execute_vrp (warn_array_bounds_p); }
11057 private:
11058 bool warn_array_bounds_p;
11059 }; // class pass_vrp
11061 } // anon namespace
11063 gimple_opt_pass *
11064 make_pass_vrp (gcc::context *ctxt)
11066 return new pass_vrp (ctxt);
11069 namespace {
11071 const pass_data pass_data_early_vrp =
11073 GIMPLE_PASS, /* type */
11074 "evrp", /* name */
11075 OPTGROUP_NONE, /* optinfo_flags */
11076 TV_TREE_EARLY_VRP, /* tv_id */
11077 PROP_ssa, /* properties_required */
11078 0, /* properties_provided */
11079 0, /* properties_destroyed */
11080 0, /* todo_flags_start */
11081 ( TODO_cleanup_cfg | TODO_update_ssa | TODO_verify_all ),
11084 class pass_early_vrp : public gimple_opt_pass
11086 public:
11087 pass_early_vrp (gcc::context *ctxt)
11088 : gimple_opt_pass (pass_data_early_vrp, ctxt)
11091 /* opt_pass methods: */
11092 opt_pass * clone () { return new pass_early_vrp (m_ctxt); }
11093 virtual bool gate (function *)
11095 return flag_tree_vrp != 0;
11097 virtual unsigned int execute (function *)
11098 { return execute_early_vrp (); }
11100 }; // class pass_vrp
11101 } // anon namespace
11103 gimple_opt_pass *
11104 make_pass_early_vrp (gcc::context *ctxt)
11106 return new pass_early_vrp (ctxt);