DR 1511 - const volatile variables and ODR
[official-gcc.git] / gcc / tree-vrp.c
blob00a08fdff6dd123b53955d3308e7f6c6a86b5ba5
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 /* Set value-ranges of all SSA names defined by STMT to varying. */
715 static void
716 set_defs_to_varying (gimple *stmt)
718 ssa_op_iter i;
719 tree def;
720 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
722 value_range *vr = get_value_range (def);
723 /* Avoid writing to vr_const_varying get_value_range may return. */
724 if (vr->type != VR_VARYING)
725 set_value_range_to_varying (vr);
730 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
732 static inline bool
733 vrp_operand_equal_p (const_tree val1, const_tree val2)
735 if (val1 == val2)
736 return true;
737 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
738 return false;
739 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
742 /* Return true, if the bitmaps B1 and B2 are equal. */
744 static inline bool
745 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
747 return (b1 == b2
748 || ((!b1 || bitmap_empty_p (b1))
749 && (!b2 || bitmap_empty_p (b2)))
750 || (b1 && b2
751 && bitmap_equal_p (b1, b2)));
754 /* Update the value range and equivalence set for variable VAR to
755 NEW_VR. Return true if NEW_VR is different from VAR's previous
756 value.
758 NOTE: This function assumes that NEW_VR is a temporary value range
759 object created for the sole purpose of updating VAR's range. The
760 storage used by the equivalence set from NEW_VR will be freed by
761 this function. Do not call update_value_range when NEW_VR
762 is the range object associated with another SSA name. */
764 static inline bool
765 update_value_range (const_tree var, value_range *new_vr)
767 value_range *old_vr;
768 bool is_new;
770 /* If there is a value-range on the SSA name from earlier analysis
771 factor that in. */
772 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
774 wide_int min, max;
775 value_range_type rtype = get_range_info (var, &min, &max);
776 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
778 tree nr_min, nr_max;
779 /* Range info on SSA names doesn't carry overflow information
780 so make sure to preserve the overflow bit on the lattice. */
781 if (rtype == VR_RANGE
782 && needs_overflow_infinity (TREE_TYPE (var))
783 && (new_vr->type == VR_VARYING
784 || (new_vr->type == VR_RANGE
785 && is_negative_overflow_infinity (new_vr->min)))
786 && wi::eq_p (vrp_val_min (TREE_TYPE (var)), min))
787 nr_min = negative_overflow_infinity (TREE_TYPE (var));
788 else
789 nr_min = wide_int_to_tree (TREE_TYPE (var), min);
790 if (rtype == VR_RANGE
791 && needs_overflow_infinity (TREE_TYPE (var))
792 && (new_vr->type == VR_VARYING
793 || (new_vr->type == VR_RANGE
794 && is_positive_overflow_infinity (new_vr->max)))
795 && wi::eq_p (vrp_val_max (TREE_TYPE (var)), max))
796 nr_max = positive_overflow_infinity (TREE_TYPE (var));
797 else
798 nr_max = wide_int_to_tree (TREE_TYPE (var), max);
799 value_range nr = VR_INITIALIZER;
800 set_and_canonicalize_value_range (&nr, rtype, nr_min, nr_max, NULL);
801 vrp_intersect_ranges (new_vr, &nr);
805 /* Update the value range, if necessary. */
806 old_vr = get_value_range (var);
807 is_new = old_vr->type != new_vr->type
808 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
809 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
810 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
812 if (is_new)
814 /* Do not allow transitions up the lattice. The following
815 is slightly more awkward than just new_vr->type < old_vr->type
816 because VR_RANGE and VR_ANTI_RANGE need to be considered
817 the same. We may not have is_new when transitioning to
818 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
819 called. */
820 if (new_vr->type == VR_UNDEFINED)
822 BITMAP_FREE (new_vr->equiv);
823 set_value_range_to_varying (old_vr);
824 set_value_range_to_varying (new_vr);
825 return true;
827 else
828 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
829 new_vr->equiv);
832 BITMAP_FREE (new_vr->equiv);
834 return is_new;
838 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
839 point where equivalence processing can be turned on/off. */
841 static void
842 add_equivalence (bitmap *equiv, const_tree var)
844 unsigned ver = SSA_NAME_VERSION (var);
845 value_range *vr = get_value_range (var);
847 if (*equiv == NULL)
848 *equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
849 bitmap_set_bit (*equiv, ver);
850 if (vr && vr->equiv)
851 bitmap_ior_into (*equiv, vr->equiv);
855 /* Return true if VR is ~[0, 0]. */
857 static inline bool
858 range_is_nonnull (value_range *vr)
860 return vr->type == VR_ANTI_RANGE
861 && integer_zerop (vr->min)
862 && integer_zerop (vr->max);
866 /* Return true if VR is [0, 0]. */
868 static inline bool
869 range_is_null (value_range *vr)
871 return vr->type == VR_RANGE
872 && integer_zerop (vr->min)
873 && integer_zerop (vr->max);
876 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
877 a singleton. */
879 static inline bool
880 range_int_cst_p (value_range *vr)
882 return (vr->type == VR_RANGE
883 && TREE_CODE (vr->max) == INTEGER_CST
884 && TREE_CODE (vr->min) == INTEGER_CST);
887 /* Return true if VR is a INTEGER_CST singleton. */
889 static inline bool
890 range_int_cst_singleton_p (value_range *vr)
892 return (range_int_cst_p (vr)
893 && !is_overflow_infinity (vr->min)
894 && !is_overflow_infinity (vr->max)
895 && tree_int_cst_equal (vr->min, vr->max));
898 /* Return true if value range VR involves at least one symbol. */
900 static inline bool
901 symbolic_range_p (value_range *vr)
903 return (!is_gimple_min_invariant (vr->min)
904 || !is_gimple_min_invariant (vr->max));
907 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
908 otherwise. We only handle additive operations and set NEG to true if the
909 symbol is negated and INV to the invariant part, if any. */
911 static tree
912 get_single_symbol (tree t, bool *neg, tree *inv)
914 bool neg_;
915 tree inv_;
917 *inv = NULL_TREE;
918 *neg = false;
920 if (TREE_CODE (t) == PLUS_EXPR
921 || TREE_CODE (t) == POINTER_PLUS_EXPR
922 || TREE_CODE (t) == MINUS_EXPR)
924 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
926 neg_ = (TREE_CODE (t) == MINUS_EXPR);
927 inv_ = TREE_OPERAND (t, 0);
928 t = TREE_OPERAND (t, 1);
930 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
932 neg_ = false;
933 inv_ = TREE_OPERAND (t, 1);
934 t = TREE_OPERAND (t, 0);
936 else
937 return NULL_TREE;
939 else
941 neg_ = false;
942 inv_ = NULL_TREE;
945 if (TREE_CODE (t) == NEGATE_EXPR)
947 t = TREE_OPERAND (t, 0);
948 neg_ = !neg_;
951 if (TREE_CODE (t) != SSA_NAME)
952 return NULL_TREE;
954 *neg = neg_;
955 *inv = inv_;
956 return t;
959 /* The reverse operation: build a symbolic expression with TYPE
960 from symbol SYM, negated according to NEG, and invariant INV. */
962 static tree
963 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
965 const bool pointer_p = POINTER_TYPE_P (type);
966 tree t = sym;
968 if (neg)
969 t = build1 (NEGATE_EXPR, type, t);
971 if (integer_zerop (inv))
972 return t;
974 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
977 /* Return true if value range VR involves exactly one symbol SYM. */
979 static bool
980 symbolic_range_based_on_p (value_range *vr, const_tree sym)
982 bool neg, min_has_symbol, max_has_symbol;
983 tree inv;
985 if (is_gimple_min_invariant (vr->min))
986 min_has_symbol = false;
987 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
988 min_has_symbol = true;
989 else
990 return false;
992 if (is_gimple_min_invariant (vr->max))
993 max_has_symbol = false;
994 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
995 max_has_symbol = true;
996 else
997 return false;
999 return (min_has_symbol || max_has_symbol);
1002 /* Return true if value range VR uses an overflow infinity. */
1004 static inline bool
1005 overflow_infinity_range_p (value_range *vr)
1007 return (vr->type == VR_RANGE
1008 && (is_overflow_infinity (vr->min)
1009 || is_overflow_infinity (vr->max)));
1012 /* Return false if we can not make a valid comparison based on VR;
1013 this will be the case if it uses an overflow infinity and overflow
1014 is not undefined (i.e., -fno-strict-overflow is in effect).
1015 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1016 uses an overflow infinity. */
1018 static bool
1019 usable_range_p (value_range *vr, bool *strict_overflow_p)
1021 gcc_assert (vr->type == VR_RANGE);
1022 if (is_overflow_infinity (vr->min))
1024 *strict_overflow_p = true;
1025 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1026 return false;
1028 if (is_overflow_infinity (vr->max))
1030 *strict_overflow_p = true;
1031 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1032 return false;
1034 return true;
1037 /* Return true if the result of assignment STMT is know to be non-zero.
1038 If the return value is based on the assumption that signed overflow is
1039 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1040 *STRICT_OVERFLOW_P.*/
1042 static bool
1043 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1045 enum tree_code code = gimple_assign_rhs_code (stmt);
1046 switch (get_gimple_rhs_class (code))
1048 case GIMPLE_UNARY_RHS:
1049 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1050 gimple_expr_type (stmt),
1051 gimple_assign_rhs1 (stmt),
1052 strict_overflow_p);
1053 case GIMPLE_BINARY_RHS:
1054 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1055 gimple_expr_type (stmt),
1056 gimple_assign_rhs1 (stmt),
1057 gimple_assign_rhs2 (stmt),
1058 strict_overflow_p);
1059 case GIMPLE_TERNARY_RHS:
1060 return false;
1061 case GIMPLE_SINGLE_RHS:
1062 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1063 strict_overflow_p);
1064 case GIMPLE_INVALID_RHS:
1065 gcc_unreachable ();
1066 default:
1067 gcc_unreachable ();
1071 /* Return true if STMT is known to compute a non-zero value.
1072 If the return value is based on the assumption that signed overflow is
1073 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1074 *STRICT_OVERFLOW_P.*/
1076 static bool
1077 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1079 switch (gimple_code (stmt))
1081 case GIMPLE_ASSIGN:
1082 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1083 case GIMPLE_CALL:
1085 tree fndecl = gimple_call_fndecl (stmt);
1086 if (!fndecl) return false;
1087 if (flag_delete_null_pointer_checks && !flag_check_new
1088 && DECL_IS_OPERATOR_NEW (fndecl)
1089 && !TREE_NOTHROW (fndecl))
1090 return true;
1091 /* References are always non-NULL. */
1092 if (flag_delete_null_pointer_checks
1093 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1094 return true;
1095 if (flag_delete_null_pointer_checks &&
1096 lookup_attribute ("returns_nonnull",
1097 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1098 return true;
1099 return gimple_alloca_call_p (stmt);
1101 default:
1102 gcc_unreachable ();
1106 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1107 obtained so far. */
1109 static bool
1110 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1112 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1113 return true;
1115 /* If we have an expression of the form &X->a, then the expression
1116 is nonnull if X is nonnull. */
1117 if (is_gimple_assign (stmt)
1118 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1120 tree expr = gimple_assign_rhs1 (stmt);
1121 tree base = get_base_address (TREE_OPERAND (expr, 0));
1123 if (base != NULL_TREE
1124 && TREE_CODE (base) == MEM_REF
1125 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1127 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1128 if (range_is_nonnull (vr))
1129 return true;
1133 return false;
1136 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1137 a gimple invariant, or SSA_NAME +- CST. */
1139 static bool
1140 valid_value_p (tree expr)
1142 if (TREE_CODE (expr) == SSA_NAME)
1143 return true;
1145 if (TREE_CODE (expr) == PLUS_EXPR
1146 || TREE_CODE (expr) == MINUS_EXPR)
1147 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1148 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1150 return is_gimple_min_invariant (expr);
1153 /* Return
1154 1 if VAL < VAL2
1155 0 if !(VAL < VAL2)
1156 -2 if those are incomparable. */
1157 static inline int
1158 operand_less_p (tree val, tree val2)
1160 /* LT is folded faster than GE and others. Inline the common case. */
1161 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1163 if (! is_positive_overflow_infinity (val2))
1164 return tree_int_cst_lt (val, val2);
1166 else
1168 tree tcmp;
1170 fold_defer_overflow_warnings ();
1172 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1174 fold_undefer_and_ignore_overflow_warnings ();
1176 if (!tcmp
1177 || TREE_CODE (tcmp) != INTEGER_CST)
1178 return -2;
1180 if (!integer_zerop (tcmp))
1181 return 1;
1184 /* val >= val2, not considering overflow infinity. */
1185 if (is_negative_overflow_infinity (val))
1186 return is_negative_overflow_infinity (val2) ? 0 : 1;
1187 else if (is_positive_overflow_infinity (val2))
1188 return is_positive_overflow_infinity (val) ? 0 : 1;
1190 return 0;
1193 /* Compare two values VAL1 and VAL2. Return
1195 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1196 -1 if VAL1 < VAL2,
1197 0 if VAL1 == VAL2,
1198 +1 if VAL1 > VAL2, and
1199 +2 if VAL1 != VAL2
1201 This is similar to tree_int_cst_compare but supports pointer values
1202 and values that cannot be compared at compile time.
1204 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1205 true if the return value is only valid if we assume that signed
1206 overflow is undefined. */
1208 static int
1209 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1211 if (val1 == val2)
1212 return 0;
1214 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1215 both integers. */
1216 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1217 == POINTER_TYPE_P (TREE_TYPE (val2)));
1219 /* Convert the two values into the same type. This is needed because
1220 sizetype causes sign extension even for unsigned types. */
1221 val2 = fold_convert (TREE_TYPE (val1), val2);
1222 STRIP_USELESS_TYPE_CONVERSION (val2);
1224 const bool overflow_undefined
1225 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
1226 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
1227 tree inv1, inv2;
1228 bool neg1, neg2;
1229 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
1230 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
1232 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1233 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1234 if (sym1 && sym2)
1236 /* Both values must use the same name with the same sign. */
1237 if (sym1 != sym2 || neg1 != neg2)
1238 return -2;
1240 /* [-]NAME + CST == [-]NAME + CST. */
1241 if (inv1 == inv2)
1242 return 0;
1244 /* If overflow is defined we cannot simplify more. */
1245 if (!overflow_undefined)
1246 return -2;
1248 if (strict_overflow_p != NULL
1249 && (!inv1 || !TREE_NO_WARNING (val1))
1250 && (!inv2 || !TREE_NO_WARNING (val2)))
1251 *strict_overflow_p = true;
1253 if (!inv1)
1254 inv1 = build_int_cst (TREE_TYPE (val1), 0);
1255 if (!inv2)
1256 inv2 = build_int_cst (TREE_TYPE (val2), 0);
1258 return compare_values_warnv (inv1, inv2, strict_overflow_p);
1261 const bool cst1 = is_gimple_min_invariant (val1);
1262 const bool cst2 = is_gimple_min_invariant (val2);
1264 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1265 it might be possible to say something depending on the constants. */
1266 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
1268 if (!overflow_undefined)
1269 return -2;
1271 if (strict_overflow_p != NULL
1272 && (!sym1 || !TREE_NO_WARNING (val1))
1273 && (!sym2 || !TREE_NO_WARNING (val2)))
1274 *strict_overflow_p = true;
1276 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
1277 tree cst = cst1 ? val1 : val2;
1278 tree inv = cst1 ? inv2 : inv1;
1280 /* Compute the difference between the constants. If it overflows or
1281 underflows, this means that we can trivially compare the NAME with
1282 it and, consequently, the two values with each other. */
1283 wide_int diff = wi::sub (cst, inv);
1284 if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn))
1286 const int res = wi::cmp (cst, inv, sgn);
1287 return cst1 ? res : -res;
1290 return -2;
1293 /* We cannot say anything more for non-constants. */
1294 if (!cst1 || !cst2)
1295 return -2;
1297 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1299 /* We cannot compare overflowed values, except for overflow
1300 infinities. */
1301 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1303 if (strict_overflow_p != NULL)
1304 *strict_overflow_p = true;
1305 if (is_negative_overflow_infinity (val1))
1306 return is_negative_overflow_infinity (val2) ? 0 : -1;
1307 else if (is_negative_overflow_infinity (val2))
1308 return 1;
1309 else if (is_positive_overflow_infinity (val1))
1310 return is_positive_overflow_infinity (val2) ? 0 : 1;
1311 else if (is_positive_overflow_infinity (val2))
1312 return -1;
1313 return -2;
1316 return tree_int_cst_compare (val1, val2);
1318 else
1320 tree t;
1322 /* First see if VAL1 and VAL2 are not the same. */
1323 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1324 return 0;
1326 /* If VAL1 is a lower address than VAL2, return -1. */
1327 if (operand_less_p (val1, val2) == 1)
1328 return -1;
1330 /* If VAL1 is a higher address than VAL2, return +1. */
1331 if (operand_less_p (val2, val1) == 1)
1332 return 1;
1334 /* If VAL1 is different than VAL2, return +2.
1335 For integer constants we either have already returned -1 or 1
1336 or they are equivalent. We still might succeed in proving
1337 something about non-trivial operands. */
1338 if (TREE_CODE (val1) != INTEGER_CST
1339 || TREE_CODE (val2) != INTEGER_CST)
1341 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1342 if (t && integer_onep (t))
1343 return 2;
1346 return -2;
1350 /* Compare values like compare_values_warnv, but treat comparisons of
1351 nonconstants which rely on undefined overflow as incomparable. */
1353 static int
1354 compare_values (tree val1, tree val2)
1356 bool sop;
1357 int ret;
1359 sop = false;
1360 ret = compare_values_warnv (val1, val2, &sop);
1361 if (sop
1362 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1363 ret = -2;
1364 return ret;
1368 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1369 0 if VAL is not inside [MIN, MAX],
1370 -2 if we cannot tell either way.
1372 Benchmark compile/20001226-1.c compilation time after changing this
1373 function. */
1375 static inline int
1376 value_inside_range (tree val, tree min, tree max)
1378 int cmp1, cmp2;
1380 cmp1 = operand_less_p (val, min);
1381 if (cmp1 == -2)
1382 return -2;
1383 if (cmp1 == 1)
1384 return 0;
1386 cmp2 = operand_less_p (max, val);
1387 if (cmp2 == -2)
1388 return -2;
1390 return !cmp2;
1394 /* Return true if value ranges VR0 and VR1 have a non-empty
1395 intersection.
1397 Benchmark compile/20001226-1.c compilation time after changing this
1398 function.
1401 static inline bool
1402 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1404 /* The value ranges do not intersect if the maximum of the first range is
1405 less than the minimum of the second range or vice versa.
1406 When those relations are unknown, we can't do any better. */
1407 if (operand_less_p (vr0->max, vr1->min) != 0)
1408 return false;
1409 if (operand_less_p (vr1->max, vr0->min) != 0)
1410 return false;
1411 return true;
1415 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1416 include the value zero, -2 if we cannot tell. */
1418 static inline int
1419 range_includes_zero_p (tree min, tree max)
1421 tree zero = build_int_cst (TREE_TYPE (min), 0);
1422 return value_inside_range (zero, min, max);
1425 /* Return true if *VR is know to only contain nonnegative values. */
1427 static inline bool
1428 value_range_nonnegative_p (value_range *vr)
1430 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1431 which would return a useful value should be encoded as a
1432 VR_RANGE. */
1433 if (vr->type == VR_RANGE)
1435 int result = compare_values (vr->min, integer_zero_node);
1436 return (result == 0 || result == 1);
1439 return false;
1442 /* If *VR has a value rante that is a single constant value return that,
1443 otherwise return NULL_TREE. */
1445 static tree
1446 value_range_constant_singleton (value_range *vr)
1448 if (vr->type == VR_RANGE
1449 && vrp_operand_equal_p (vr->min, vr->max)
1450 && is_gimple_min_invariant (vr->min))
1451 return vr->min;
1453 return NULL_TREE;
1456 /* If OP has a value range with a single constant value return that,
1457 otherwise return NULL_TREE. This returns OP itself if OP is a
1458 constant. */
1460 static tree
1461 op_with_constant_singleton_value_range (tree op)
1463 if (is_gimple_min_invariant (op))
1464 return op;
1466 if (TREE_CODE (op) != SSA_NAME)
1467 return NULL_TREE;
1469 return value_range_constant_singleton (get_value_range (op));
1472 /* Return true if op is in a boolean [0, 1] value-range. */
1474 static bool
1475 op_with_boolean_value_range_p (tree op)
1477 value_range *vr;
1479 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1480 return true;
1482 if (integer_zerop (op)
1483 || integer_onep (op))
1484 return true;
1486 if (TREE_CODE (op) != SSA_NAME)
1487 return false;
1489 vr = get_value_range (op);
1490 return (vr->type == VR_RANGE
1491 && integer_zerop (vr->min)
1492 && integer_onep (vr->max));
1495 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1496 true and store it in *VR_P. */
1498 static void
1499 extract_range_for_var_from_comparison_expr (tree var, enum tree_code cond_code,
1500 tree op, tree limit,
1501 value_range *vr_p)
1503 tree min, max, type;
1504 value_range *limit_vr;
1505 limit = avoid_overflow_infinity (limit);
1506 type = TREE_TYPE (var);
1507 gcc_assert (limit != var);
1509 /* For pointer arithmetic, we only keep track of pointer equality
1510 and inequality. */
1511 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1513 set_value_range_to_varying (vr_p);
1514 return;
1517 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1518 try to use LIMIT's range to avoid creating symbolic ranges
1519 unnecessarily. */
1520 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1522 /* LIMIT's range is only interesting if it has any useful information. */
1523 if (! limit_vr
1524 || limit_vr->type == VR_UNDEFINED
1525 || limit_vr->type == VR_VARYING
1526 || (symbolic_range_p (limit_vr)
1527 && ! (limit_vr->type == VR_RANGE
1528 && (limit_vr->min == limit_vr->max
1529 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
1530 limit_vr = NULL;
1532 /* Initially, the new range has the same set of equivalences of
1533 VAR's range. This will be revised before returning the final
1534 value. Since assertions may be chained via mutually exclusive
1535 predicates, we will need to trim the set of equivalences before
1536 we are done. */
1537 gcc_assert (vr_p->equiv == NULL);
1538 add_equivalence (&vr_p->equiv, var);
1540 /* Extract a new range based on the asserted comparison for VAR and
1541 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1542 will only use it for equality comparisons (EQ_EXPR). For any
1543 other kind of assertion, we cannot derive a range from LIMIT's
1544 anti-range that can be used to describe the new range. For
1545 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1546 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1547 no single range for x_2 that could describe LE_EXPR, so we might
1548 as well build the range [b_4, +INF] for it.
1549 One special case we handle is extracting a range from a
1550 range test encoded as (unsigned)var + CST <= limit. */
1551 if (TREE_CODE (op) == NOP_EXPR
1552 || TREE_CODE (op) == PLUS_EXPR)
1554 if (TREE_CODE (op) == PLUS_EXPR)
1556 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
1557 TREE_OPERAND (op, 1));
1558 max = int_const_binop (PLUS_EXPR, limit, min);
1559 op = TREE_OPERAND (op, 0);
1561 else
1563 min = build_int_cst (TREE_TYPE (var), 0);
1564 max = limit;
1567 /* Make sure to not set TREE_OVERFLOW on the final type
1568 conversion. We are willingly interpreting large positive
1569 unsigned values as negative signed values here. */
1570 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1571 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1573 /* We can transform a max, min range to an anti-range or
1574 vice-versa. Use set_and_canonicalize_value_range which does
1575 this for us. */
1576 if (cond_code == LE_EXPR)
1577 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1578 min, max, vr_p->equiv);
1579 else if (cond_code == GT_EXPR)
1580 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1581 min, max, vr_p->equiv);
1582 else
1583 gcc_unreachable ();
1585 else if (cond_code == EQ_EXPR)
1587 enum value_range_type range_type;
1589 if (limit_vr)
1591 range_type = limit_vr->type;
1592 min = limit_vr->min;
1593 max = limit_vr->max;
1595 else
1597 range_type = VR_RANGE;
1598 min = limit;
1599 max = limit;
1602 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1604 /* When asserting the equality VAR == LIMIT and LIMIT is another
1605 SSA name, the new range will also inherit the equivalence set
1606 from LIMIT. */
1607 if (TREE_CODE (limit) == SSA_NAME)
1608 add_equivalence (&vr_p->equiv, limit);
1610 else if (cond_code == NE_EXPR)
1612 /* As described above, when LIMIT's range is an anti-range and
1613 this assertion is an inequality (NE_EXPR), then we cannot
1614 derive anything from the anti-range. For instance, if
1615 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1616 not imply that VAR's range is [0, 0]. So, in the case of
1617 anti-ranges, we just assert the inequality using LIMIT and
1618 not its anti-range.
1620 If LIMIT_VR is a range, we can only use it to build a new
1621 anti-range if LIMIT_VR is a single-valued range. For
1622 instance, if LIMIT_VR is [0, 1], the predicate
1623 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1624 Rather, it means that for value 0 VAR should be ~[0, 0]
1625 and for value 1, VAR should be ~[1, 1]. We cannot
1626 represent these ranges.
1628 The only situation in which we can build a valid
1629 anti-range is when LIMIT_VR is a single-valued range
1630 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1631 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1632 if (limit_vr
1633 && limit_vr->type == VR_RANGE
1634 && compare_values (limit_vr->min, limit_vr->max) == 0)
1636 min = limit_vr->min;
1637 max = limit_vr->max;
1639 else
1641 /* In any other case, we cannot use LIMIT's range to build a
1642 valid anti-range. */
1643 min = max = limit;
1646 /* If MIN and MAX cover the whole range for their type, then
1647 just use the original LIMIT. */
1648 if (INTEGRAL_TYPE_P (type)
1649 && vrp_val_is_min (min)
1650 && vrp_val_is_max (max))
1651 min = max = limit;
1653 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1654 min, max, vr_p->equiv);
1656 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1658 min = TYPE_MIN_VALUE (type);
1660 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1661 max = limit;
1662 else
1664 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1665 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1666 LT_EXPR. */
1667 max = limit_vr->max;
1670 /* If the maximum value forces us to be out of bounds, simply punt.
1671 It would be pointless to try and do anything more since this
1672 all should be optimized away above us. */
1673 if ((cond_code == LT_EXPR
1674 && compare_values (max, min) == 0)
1675 || is_overflow_infinity (max))
1676 set_value_range_to_varying (vr_p);
1677 else
1679 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1680 if (cond_code == LT_EXPR)
1682 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1683 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1684 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1685 build_int_cst (TREE_TYPE (max), -1));
1686 else
1687 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1688 build_int_cst (TREE_TYPE (max), 1));
1689 if (EXPR_P (max))
1690 TREE_NO_WARNING (max) = 1;
1693 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1696 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1698 max = TYPE_MAX_VALUE (type);
1700 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1701 min = limit;
1702 else
1704 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1705 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1706 GT_EXPR. */
1707 min = limit_vr->min;
1710 /* If the minimum value forces us to be out of bounds, simply punt.
1711 It would be pointless to try and do anything more since this
1712 all should be optimized away above us. */
1713 if ((cond_code == GT_EXPR
1714 && compare_values (min, max) == 0)
1715 || is_overflow_infinity (min))
1716 set_value_range_to_varying (vr_p);
1717 else
1719 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1720 if (cond_code == GT_EXPR)
1722 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1723 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1724 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1725 build_int_cst (TREE_TYPE (min), -1));
1726 else
1727 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1728 build_int_cst (TREE_TYPE (min), 1));
1729 if (EXPR_P (min))
1730 TREE_NO_WARNING (min) = 1;
1733 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1736 else
1737 gcc_unreachable ();
1739 /* Finally intersect the new range with what we already know about var. */
1740 vrp_intersect_ranges (vr_p, get_value_range (var));
1743 /* Extract value range information from an ASSERT_EXPR EXPR and store
1744 it in *VR_P. */
1746 static void
1747 extract_range_from_assert (value_range *vr_p, tree expr)
1749 tree var = ASSERT_EXPR_VAR (expr);
1750 tree cond = ASSERT_EXPR_COND (expr);
1751 tree limit, op;
1752 enum tree_code cond_code;
1753 gcc_assert (COMPARISON_CLASS_P (cond));
1755 /* Find VAR in the ASSERT_EXPR conditional. */
1756 if (var == TREE_OPERAND (cond, 0)
1757 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1758 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1760 /* If the predicate is of the form VAR COMP LIMIT, then we just
1761 take LIMIT from the RHS and use the same comparison code. */
1762 cond_code = TREE_CODE (cond);
1763 limit = TREE_OPERAND (cond, 1);
1764 op = TREE_OPERAND (cond, 0);
1766 else
1768 /* If the predicate is of the form LIMIT COMP VAR, then we need
1769 to flip around the comparison code to create the proper range
1770 for VAR. */
1771 cond_code = swap_tree_comparison (TREE_CODE (cond));
1772 limit = TREE_OPERAND (cond, 0);
1773 op = TREE_OPERAND (cond, 1);
1775 extract_range_for_var_from_comparison_expr (var, cond_code, op,
1776 limit, vr_p);
1779 /* Extract range information from SSA name VAR and store it in VR. If
1780 VAR has an interesting range, use it. Otherwise, create the
1781 range [VAR, VAR] and return it. This is useful in situations where
1782 we may have conditionals testing values of VARYING names. For
1783 instance,
1785 x_3 = y_5;
1786 if (x_3 > y_5)
1789 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1790 always false. */
1792 static void
1793 extract_range_from_ssa_name (value_range *vr, tree var)
1795 value_range *var_vr = get_value_range (var);
1797 if (var_vr->type != VR_VARYING)
1798 copy_value_range (vr, var_vr);
1799 else
1800 set_value_range (vr, VR_RANGE, var, var, NULL);
1802 add_equivalence (&vr->equiv, var);
1806 /* Wrapper around int_const_binop. If the operation overflows and we
1807 are not using wrapping arithmetic, then adjust the result to be
1808 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1809 NULL_TREE if we need to use an overflow infinity representation but
1810 the type does not support it. */
1812 static tree
1813 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1815 tree res;
1817 res = int_const_binop (code, val1, val2);
1819 /* If we are using unsigned arithmetic, operate symbolically
1820 on -INF and +INF as int_const_binop only handles signed overflow. */
1821 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1823 int checkz = compare_values (res, val1);
1824 bool overflow = false;
1826 /* Ensure that res = val1 [+*] val2 >= val1
1827 or that res = val1 - val2 <= val1. */
1828 if ((code == PLUS_EXPR
1829 && !(checkz == 1 || checkz == 0))
1830 || (code == MINUS_EXPR
1831 && !(checkz == 0 || checkz == -1)))
1833 overflow = true;
1835 /* Checking for multiplication overflow is done by dividing the
1836 output of the multiplication by the first input of the
1837 multiplication. If the result of that division operation is
1838 not equal to the second input of the multiplication, then the
1839 multiplication overflowed. */
1840 else if (code == MULT_EXPR && !integer_zerop (val1))
1842 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1843 res,
1844 val1);
1845 int check = compare_values (tmp, val2);
1847 if (check != 0)
1848 overflow = true;
1851 if (overflow)
1853 res = copy_node (res);
1854 TREE_OVERFLOW (res) = 1;
1858 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1859 /* If the singed operation wraps then int_const_binop has done
1860 everything we want. */
1862 /* Signed division of -1/0 overflows and by the time it gets here
1863 returns NULL_TREE. */
1864 else if (!res)
1865 return NULL_TREE;
1866 else if ((TREE_OVERFLOW (res)
1867 && !TREE_OVERFLOW (val1)
1868 && !TREE_OVERFLOW (val2))
1869 || is_overflow_infinity (val1)
1870 || is_overflow_infinity (val2))
1872 /* If the operation overflowed but neither VAL1 nor VAL2 are
1873 overflown, return -INF or +INF depending on the operation
1874 and the combination of signs of the operands. */
1875 int sgn1 = tree_int_cst_sgn (val1);
1876 int sgn2 = tree_int_cst_sgn (val2);
1878 if (needs_overflow_infinity (TREE_TYPE (res))
1879 && !supports_overflow_infinity (TREE_TYPE (res)))
1880 return NULL_TREE;
1882 /* We have to punt on adding infinities of different signs,
1883 since we can't tell what the sign of the result should be.
1884 Likewise for subtracting infinities of the same sign. */
1885 if (((code == PLUS_EXPR && sgn1 != sgn2)
1886 || (code == MINUS_EXPR && sgn1 == sgn2))
1887 && is_overflow_infinity (val1)
1888 && is_overflow_infinity (val2))
1889 return NULL_TREE;
1891 /* Don't try to handle division or shifting of infinities. */
1892 if ((code == TRUNC_DIV_EXPR
1893 || code == FLOOR_DIV_EXPR
1894 || code == CEIL_DIV_EXPR
1895 || code == EXACT_DIV_EXPR
1896 || code == ROUND_DIV_EXPR
1897 || code == RSHIFT_EXPR)
1898 && (is_overflow_infinity (val1)
1899 || is_overflow_infinity (val2)))
1900 return NULL_TREE;
1902 /* Notice that we only need to handle the restricted set of
1903 operations handled by extract_range_from_binary_expr.
1904 Among them, only multiplication, addition and subtraction
1905 can yield overflow without overflown operands because we
1906 are working with integral types only... except in the
1907 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1908 for division too. */
1910 /* For multiplication, the sign of the overflow is given
1911 by the comparison of the signs of the operands. */
1912 if ((code == MULT_EXPR && sgn1 == sgn2)
1913 /* For addition, the operands must be of the same sign
1914 to yield an overflow. Its sign is therefore that
1915 of one of the operands, for example the first. For
1916 infinite operands X + -INF is negative, not positive. */
1917 || (code == PLUS_EXPR
1918 && (sgn1 >= 0
1919 ? !is_negative_overflow_infinity (val2)
1920 : is_positive_overflow_infinity (val2)))
1921 /* For subtraction, non-infinite operands must be of
1922 different signs to yield an overflow. Its sign is
1923 therefore that of the first operand or the opposite of
1924 that of the second operand. A first operand of 0 counts
1925 as positive here, for the corner case 0 - (-INF), which
1926 overflows, but must yield +INF. For infinite operands 0
1927 - INF is negative, not positive. */
1928 || (code == MINUS_EXPR
1929 && (sgn1 >= 0
1930 ? !is_positive_overflow_infinity (val2)
1931 : is_negative_overflow_infinity (val2)))
1932 /* We only get in here with positive shift count, so the
1933 overflow direction is the same as the sign of val1.
1934 Actually rshift does not overflow at all, but we only
1935 handle the case of shifting overflowed -INF and +INF. */
1936 || (code == RSHIFT_EXPR
1937 && sgn1 >= 0)
1938 /* For division, the only case is -INF / -1 = +INF. */
1939 || code == TRUNC_DIV_EXPR
1940 || code == FLOOR_DIV_EXPR
1941 || code == CEIL_DIV_EXPR
1942 || code == EXACT_DIV_EXPR
1943 || code == ROUND_DIV_EXPR)
1944 return (needs_overflow_infinity (TREE_TYPE (res))
1945 ? positive_overflow_infinity (TREE_TYPE (res))
1946 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1947 else
1948 return (needs_overflow_infinity (TREE_TYPE (res))
1949 ? negative_overflow_infinity (TREE_TYPE (res))
1950 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1953 return res;
1957 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1958 bitmask if some bit is unset, it means for all numbers in the range
1959 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1960 bitmask if some bit is set, it means for all numbers in the range
1961 the bit is 1, otherwise it might be 0 or 1. */
1963 static bool
1964 zero_nonzero_bits_from_vr (const tree expr_type,
1965 value_range *vr,
1966 wide_int *may_be_nonzero,
1967 wide_int *must_be_nonzero)
1969 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1970 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1971 if (!range_int_cst_p (vr)
1972 || is_overflow_infinity (vr->min)
1973 || is_overflow_infinity (vr->max))
1974 return false;
1976 if (range_int_cst_singleton_p (vr))
1978 *may_be_nonzero = vr->min;
1979 *must_be_nonzero = *may_be_nonzero;
1981 else if (tree_int_cst_sgn (vr->min) >= 0
1982 || tree_int_cst_sgn (vr->max) < 0)
1984 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1985 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1986 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1987 if (xor_mask != 0)
1989 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1990 may_be_nonzero->get_precision ());
1991 *may_be_nonzero = *may_be_nonzero | mask;
1992 *must_be_nonzero = must_be_nonzero->and_not (mask);
1996 return true;
1999 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2000 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2001 false otherwise. If *AR can be represented with a single range
2002 *VR1 will be VR_UNDEFINED. */
2004 static bool
2005 ranges_from_anti_range (value_range *ar,
2006 value_range *vr0, value_range *vr1)
2008 tree type = TREE_TYPE (ar->min);
2010 vr0->type = VR_UNDEFINED;
2011 vr1->type = VR_UNDEFINED;
2013 if (ar->type != VR_ANTI_RANGE
2014 || TREE_CODE (ar->min) != INTEGER_CST
2015 || TREE_CODE (ar->max) != INTEGER_CST
2016 || !vrp_val_min (type)
2017 || !vrp_val_max (type))
2018 return false;
2020 if (!vrp_val_is_min (ar->min))
2022 vr0->type = VR_RANGE;
2023 vr0->min = vrp_val_min (type);
2024 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2026 if (!vrp_val_is_max (ar->max))
2028 vr1->type = VR_RANGE;
2029 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2030 vr1->max = vrp_val_max (type);
2032 if (vr0->type == VR_UNDEFINED)
2034 *vr0 = *vr1;
2035 vr1->type = VR_UNDEFINED;
2038 return vr0->type != VR_UNDEFINED;
2041 /* Helper to extract a value-range *VR for a multiplicative operation
2042 *VR0 CODE *VR1. */
2044 static void
2045 extract_range_from_multiplicative_op_1 (value_range *vr,
2046 enum tree_code code,
2047 value_range *vr0, value_range *vr1)
2049 enum value_range_type type;
2050 tree val[4];
2051 size_t i;
2052 tree min, max;
2053 bool sop;
2054 int cmp;
2056 /* Multiplications, divisions and shifts are a bit tricky to handle,
2057 depending on the mix of signs we have in the two ranges, we
2058 need to operate on different values to get the minimum and
2059 maximum values for the new range. One approach is to figure
2060 out all the variations of range combinations and do the
2061 operations.
2063 However, this involves several calls to compare_values and it
2064 is pretty convoluted. It's simpler to do the 4 operations
2065 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2066 MAX1) and then figure the smallest and largest values to form
2067 the new range. */
2068 gcc_assert (code == MULT_EXPR
2069 || code == TRUNC_DIV_EXPR
2070 || code == FLOOR_DIV_EXPR
2071 || code == CEIL_DIV_EXPR
2072 || code == EXACT_DIV_EXPR
2073 || code == ROUND_DIV_EXPR
2074 || code == RSHIFT_EXPR
2075 || code == LSHIFT_EXPR);
2076 gcc_assert ((vr0->type == VR_RANGE
2077 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2078 && vr0->type == vr1->type);
2080 type = vr0->type;
2082 /* Compute the 4 cross operations. */
2083 sop = false;
2084 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2085 if (val[0] == NULL_TREE)
2086 sop = true;
2088 if (vr1->max == vr1->min)
2089 val[1] = NULL_TREE;
2090 else
2092 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2093 if (val[1] == NULL_TREE)
2094 sop = true;
2097 if (vr0->max == vr0->min)
2098 val[2] = NULL_TREE;
2099 else
2101 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2102 if (val[2] == NULL_TREE)
2103 sop = true;
2106 if (vr0->min == vr0->max || vr1->min == vr1->max)
2107 val[3] = NULL_TREE;
2108 else
2110 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2111 if (val[3] == NULL_TREE)
2112 sop = true;
2115 if (sop)
2117 set_value_range_to_varying (vr);
2118 return;
2121 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2122 of VAL[i]. */
2123 min = val[0];
2124 max = val[0];
2125 for (i = 1; i < 4; i++)
2127 if (!is_gimple_min_invariant (min)
2128 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2129 || !is_gimple_min_invariant (max)
2130 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2131 break;
2133 if (val[i])
2135 if (!is_gimple_min_invariant (val[i])
2136 || (TREE_OVERFLOW (val[i])
2137 && !is_overflow_infinity (val[i])))
2139 /* If we found an overflowed value, set MIN and MAX
2140 to it so that we set the resulting range to
2141 VARYING. */
2142 min = max = val[i];
2143 break;
2146 if (compare_values (val[i], min) == -1)
2147 min = val[i];
2149 if (compare_values (val[i], max) == 1)
2150 max = val[i];
2154 /* If either MIN or MAX overflowed, then set the resulting range to
2155 VARYING. But we do accept an overflow infinity
2156 representation. */
2157 if (min == NULL_TREE
2158 || !is_gimple_min_invariant (min)
2159 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2160 || max == NULL_TREE
2161 || !is_gimple_min_invariant (max)
2162 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2164 set_value_range_to_varying (vr);
2165 return;
2168 /* We punt if:
2169 1) [-INF, +INF]
2170 2) [-INF, +-INF(OVF)]
2171 3) [+-INF(OVF), +INF]
2172 4) [+-INF(OVF), +-INF(OVF)]
2173 We learn nothing when we have INF and INF(OVF) on both sides.
2174 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2175 overflow. */
2176 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2177 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2179 set_value_range_to_varying (vr);
2180 return;
2183 cmp = compare_values (min, max);
2184 if (cmp == -2 || cmp == 1)
2186 /* If the new range has its limits swapped around (MIN > MAX),
2187 then the operation caused one of them to wrap around, mark
2188 the new range VARYING. */
2189 set_value_range_to_varying (vr);
2191 else
2192 set_value_range (vr, type, min, max, NULL);
2195 /* Extract range information from a binary operation CODE based on
2196 the ranges of each of its operands *VR0 and *VR1 with resulting
2197 type EXPR_TYPE. The resulting range is stored in *VR. */
2199 static void
2200 extract_range_from_binary_expr_1 (value_range *vr,
2201 enum tree_code code, tree expr_type,
2202 value_range *vr0_, value_range *vr1_)
2204 value_range vr0 = *vr0_, vr1 = *vr1_;
2205 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2206 enum value_range_type type;
2207 tree min = NULL_TREE, max = NULL_TREE;
2208 int cmp;
2210 if (!INTEGRAL_TYPE_P (expr_type)
2211 && !POINTER_TYPE_P (expr_type))
2213 set_value_range_to_varying (vr);
2214 return;
2217 /* Not all binary expressions can be applied to ranges in a
2218 meaningful way. Handle only arithmetic operations. */
2219 if (code != PLUS_EXPR
2220 && code != MINUS_EXPR
2221 && code != POINTER_PLUS_EXPR
2222 && code != MULT_EXPR
2223 && code != TRUNC_DIV_EXPR
2224 && code != FLOOR_DIV_EXPR
2225 && code != CEIL_DIV_EXPR
2226 && code != EXACT_DIV_EXPR
2227 && code != ROUND_DIV_EXPR
2228 && code != TRUNC_MOD_EXPR
2229 && code != RSHIFT_EXPR
2230 && code != LSHIFT_EXPR
2231 && code != MIN_EXPR
2232 && code != MAX_EXPR
2233 && code != BIT_AND_EXPR
2234 && code != BIT_IOR_EXPR
2235 && code != BIT_XOR_EXPR)
2237 set_value_range_to_varying (vr);
2238 return;
2241 /* If both ranges are UNDEFINED, so is the result. */
2242 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2244 set_value_range_to_undefined (vr);
2245 return;
2247 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2248 code. At some point we may want to special-case operations that
2249 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2250 operand. */
2251 else if (vr0.type == VR_UNDEFINED)
2252 set_value_range_to_varying (&vr0);
2253 else if (vr1.type == VR_UNDEFINED)
2254 set_value_range_to_varying (&vr1);
2256 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2257 and express ~[] op X as ([]' op X) U ([]'' op X). */
2258 if (vr0.type == VR_ANTI_RANGE
2259 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2261 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2262 if (vrtem1.type != VR_UNDEFINED)
2264 value_range vrres = VR_INITIALIZER;
2265 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2266 &vrtem1, vr1_);
2267 vrp_meet (vr, &vrres);
2269 return;
2271 /* Likewise for X op ~[]. */
2272 if (vr1.type == VR_ANTI_RANGE
2273 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2275 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2276 if (vrtem1.type != VR_UNDEFINED)
2278 value_range vrres = VR_INITIALIZER;
2279 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2280 vr0_, &vrtem1);
2281 vrp_meet (vr, &vrres);
2283 return;
2286 /* The type of the resulting value range defaults to VR0.TYPE. */
2287 type = vr0.type;
2289 /* Refuse to operate on VARYING ranges, ranges of different kinds
2290 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2291 because we may be able to derive a useful range even if one of
2292 the operands is VR_VARYING or symbolic range. Similarly for
2293 divisions, MIN/MAX and PLUS/MINUS.
2295 TODO, we may be able to derive anti-ranges in some cases. */
2296 if (code != BIT_AND_EXPR
2297 && code != BIT_IOR_EXPR
2298 && code != TRUNC_DIV_EXPR
2299 && code != FLOOR_DIV_EXPR
2300 && code != CEIL_DIV_EXPR
2301 && code != EXACT_DIV_EXPR
2302 && code != ROUND_DIV_EXPR
2303 && code != TRUNC_MOD_EXPR
2304 && code != MIN_EXPR
2305 && code != MAX_EXPR
2306 && code != PLUS_EXPR
2307 && code != MINUS_EXPR
2308 && code != RSHIFT_EXPR
2309 && (vr0.type == VR_VARYING
2310 || vr1.type == VR_VARYING
2311 || vr0.type != vr1.type
2312 || symbolic_range_p (&vr0)
2313 || symbolic_range_p (&vr1)))
2315 set_value_range_to_varying (vr);
2316 return;
2319 /* Now evaluate the expression to determine the new range. */
2320 if (POINTER_TYPE_P (expr_type))
2322 if (code == MIN_EXPR || code == MAX_EXPR)
2324 /* For MIN/MAX expressions with pointers, we only care about
2325 nullness, if both are non null, then the result is nonnull.
2326 If both are null, then the result is null. Otherwise they
2327 are varying. */
2328 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2329 set_value_range_to_nonnull (vr, expr_type);
2330 else if (range_is_null (&vr0) && range_is_null (&vr1))
2331 set_value_range_to_null (vr, expr_type);
2332 else
2333 set_value_range_to_varying (vr);
2335 else if (code == POINTER_PLUS_EXPR)
2337 /* For pointer types, we are really only interested in asserting
2338 whether the expression evaluates to non-NULL. */
2339 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2340 set_value_range_to_nonnull (vr, expr_type);
2341 else if (range_is_null (&vr0) && range_is_null (&vr1))
2342 set_value_range_to_null (vr, expr_type);
2343 else
2344 set_value_range_to_varying (vr);
2346 else if (code == BIT_AND_EXPR)
2348 /* For pointer types, we are really only interested in asserting
2349 whether the expression evaluates to non-NULL. */
2350 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2351 set_value_range_to_nonnull (vr, expr_type);
2352 else if (range_is_null (&vr0) || range_is_null (&vr1))
2353 set_value_range_to_null (vr, expr_type);
2354 else
2355 set_value_range_to_varying (vr);
2357 else
2358 set_value_range_to_varying (vr);
2360 return;
2363 /* For integer ranges, apply the operation to each end of the
2364 range and see what we end up with. */
2365 if (code == PLUS_EXPR || code == MINUS_EXPR)
2367 const bool minus_p = (code == MINUS_EXPR);
2368 tree min_op0 = vr0.min;
2369 tree min_op1 = minus_p ? vr1.max : vr1.min;
2370 tree max_op0 = vr0.max;
2371 tree max_op1 = minus_p ? vr1.min : vr1.max;
2372 tree sym_min_op0 = NULL_TREE;
2373 tree sym_min_op1 = NULL_TREE;
2374 tree sym_max_op0 = NULL_TREE;
2375 tree sym_max_op1 = NULL_TREE;
2376 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2378 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2379 single-symbolic ranges, try to compute the precise resulting range,
2380 but only if we know that this resulting range will also be constant
2381 or single-symbolic. */
2382 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2383 && (TREE_CODE (min_op0) == INTEGER_CST
2384 || (sym_min_op0
2385 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2386 && (TREE_CODE (min_op1) == INTEGER_CST
2387 || (sym_min_op1
2388 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2389 && (!(sym_min_op0 && sym_min_op1)
2390 || (sym_min_op0 == sym_min_op1
2391 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2392 && (TREE_CODE (max_op0) == INTEGER_CST
2393 || (sym_max_op0
2394 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2395 && (TREE_CODE (max_op1) == INTEGER_CST
2396 || (sym_max_op1
2397 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2398 && (!(sym_max_op0 && sym_max_op1)
2399 || (sym_max_op0 == sym_max_op1
2400 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2402 const signop sgn = TYPE_SIGN (expr_type);
2403 const unsigned int prec = TYPE_PRECISION (expr_type);
2404 wide_int type_min, type_max, wmin, wmax;
2405 int min_ovf = 0;
2406 int max_ovf = 0;
2408 /* Get the lower and upper bounds of the type. */
2409 if (TYPE_OVERFLOW_WRAPS (expr_type))
2411 type_min = wi::min_value (prec, sgn);
2412 type_max = wi::max_value (prec, sgn);
2414 else
2416 type_min = vrp_val_min (expr_type);
2417 type_max = vrp_val_max (expr_type);
2420 /* Combine the lower bounds, if any. */
2421 if (min_op0 && min_op1)
2423 if (minus_p)
2425 wmin = wi::sub (min_op0, min_op1);
2427 /* Check for overflow. */
2428 if (wi::cmp (0, min_op1, sgn)
2429 != wi::cmp (wmin, min_op0, sgn))
2430 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2432 else
2434 wmin = wi::add (min_op0, min_op1);
2436 /* Check for overflow. */
2437 if (wi::cmp (min_op1, 0, sgn)
2438 != wi::cmp (wmin, min_op0, sgn))
2439 min_ovf = wi::cmp (min_op0, wmin, sgn);
2442 else if (min_op0)
2443 wmin = min_op0;
2444 else if (min_op1)
2445 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2446 else
2447 wmin = wi::shwi (0, prec);
2449 /* Combine the upper bounds, if any. */
2450 if (max_op0 && max_op1)
2452 if (minus_p)
2454 wmax = wi::sub (max_op0, max_op1);
2456 /* Check for overflow. */
2457 if (wi::cmp (0, max_op1, sgn)
2458 != wi::cmp (wmax, max_op0, sgn))
2459 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2461 else
2463 wmax = wi::add (max_op0, max_op1);
2465 if (wi::cmp (max_op1, 0, sgn)
2466 != wi::cmp (wmax, max_op0, sgn))
2467 max_ovf = wi::cmp (max_op0, wmax, sgn);
2470 else if (max_op0)
2471 wmax = max_op0;
2472 else if (max_op1)
2473 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2474 else
2475 wmax = wi::shwi (0, prec);
2477 /* Check for type overflow. */
2478 if (min_ovf == 0)
2480 if (wi::cmp (wmin, type_min, sgn) == -1)
2481 min_ovf = -1;
2482 else if (wi::cmp (wmin, type_max, sgn) == 1)
2483 min_ovf = 1;
2485 if (max_ovf == 0)
2487 if (wi::cmp (wmax, type_min, sgn) == -1)
2488 max_ovf = -1;
2489 else if (wi::cmp (wmax, type_max, sgn) == 1)
2490 max_ovf = 1;
2493 /* If we have overflow for the constant part and the resulting
2494 range will be symbolic, drop to VR_VARYING. */
2495 if ((min_ovf && sym_min_op0 != sym_min_op1)
2496 || (max_ovf && sym_max_op0 != sym_max_op1))
2498 set_value_range_to_varying (vr);
2499 return;
2502 if (TYPE_OVERFLOW_WRAPS (expr_type))
2504 /* If overflow wraps, truncate the values and adjust the
2505 range kind and bounds appropriately. */
2506 wide_int tmin = wide_int::from (wmin, prec, sgn);
2507 wide_int tmax = wide_int::from (wmax, prec, sgn);
2508 if (min_ovf == max_ovf)
2510 /* No overflow or both overflow or underflow. The
2511 range kind stays VR_RANGE. */
2512 min = wide_int_to_tree (expr_type, tmin);
2513 max = wide_int_to_tree (expr_type, tmax);
2515 else if ((min_ovf == -1 && max_ovf == 0)
2516 || (max_ovf == 1 && min_ovf == 0))
2518 /* Min underflow or max overflow. The range kind
2519 changes to VR_ANTI_RANGE. */
2520 bool covers = false;
2521 wide_int tem = tmin;
2522 type = VR_ANTI_RANGE;
2523 tmin = tmax + 1;
2524 if (wi::cmp (tmin, tmax, sgn) < 0)
2525 covers = true;
2526 tmax = tem - 1;
2527 if (wi::cmp (tmax, tem, sgn) > 0)
2528 covers = true;
2529 /* If the anti-range would cover nothing, drop to varying.
2530 Likewise if the anti-range bounds are outside of the
2531 types values. */
2532 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2534 set_value_range_to_varying (vr);
2535 return;
2537 min = wide_int_to_tree (expr_type, tmin);
2538 max = wide_int_to_tree (expr_type, tmax);
2540 else
2542 /* Other underflow and/or overflow, drop to VR_VARYING. */
2543 set_value_range_to_varying (vr);
2544 return;
2547 else
2549 /* If overflow does not wrap, saturate to the types min/max
2550 value. */
2551 if (min_ovf == -1)
2553 if (needs_overflow_infinity (expr_type)
2554 && supports_overflow_infinity (expr_type))
2555 min = negative_overflow_infinity (expr_type);
2556 else
2557 min = wide_int_to_tree (expr_type, type_min);
2559 else if (min_ovf == 1)
2561 if (needs_overflow_infinity (expr_type)
2562 && supports_overflow_infinity (expr_type))
2563 min = positive_overflow_infinity (expr_type);
2564 else
2565 min = wide_int_to_tree (expr_type, type_max);
2567 else
2568 min = wide_int_to_tree (expr_type, wmin);
2570 if (max_ovf == -1)
2572 if (needs_overflow_infinity (expr_type)
2573 && supports_overflow_infinity (expr_type))
2574 max = negative_overflow_infinity (expr_type);
2575 else
2576 max = wide_int_to_tree (expr_type, type_min);
2578 else if (max_ovf == 1)
2580 if (needs_overflow_infinity (expr_type)
2581 && supports_overflow_infinity (expr_type))
2582 max = positive_overflow_infinity (expr_type);
2583 else
2584 max = wide_int_to_tree (expr_type, type_max);
2586 else
2587 max = wide_int_to_tree (expr_type, wmax);
2590 if (needs_overflow_infinity (expr_type)
2591 && supports_overflow_infinity (expr_type))
2593 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2594 || (min_op1
2595 && (minus_p
2596 ? is_positive_overflow_infinity (min_op1)
2597 : is_negative_overflow_infinity (min_op1))))
2598 min = negative_overflow_infinity (expr_type);
2599 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2600 || (max_op1
2601 && (minus_p
2602 ? is_negative_overflow_infinity (max_op1)
2603 : is_positive_overflow_infinity (max_op1))))
2604 max = positive_overflow_infinity (expr_type);
2607 /* If the result lower bound is constant, we're done;
2608 otherwise, build the symbolic lower bound. */
2609 if (sym_min_op0 == sym_min_op1)
2611 else if (sym_min_op0)
2612 min = build_symbolic_expr (expr_type, sym_min_op0,
2613 neg_min_op0, min);
2614 else if (sym_min_op1)
2615 min = build_symbolic_expr (expr_type, sym_min_op1,
2616 neg_min_op1 ^ minus_p, min);
2618 /* Likewise for the upper bound. */
2619 if (sym_max_op0 == sym_max_op1)
2621 else if (sym_max_op0)
2622 max = build_symbolic_expr (expr_type, sym_max_op0,
2623 neg_max_op0, max);
2624 else if (sym_max_op1)
2625 max = build_symbolic_expr (expr_type, sym_max_op1,
2626 neg_max_op1 ^ minus_p, max);
2628 else
2630 /* For other cases, for example if we have a PLUS_EXPR with two
2631 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2632 to compute a precise range for such a case.
2633 ??? General even mixed range kind operations can be expressed
2634 by for example transforming ~[3, 5] + [1, 2] to range-only
2635 operations and a union primitive:
2636 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2637 [-INF+1, 4] U [6, +INF(OVF)]
2638 though usually the union is not exactly representable with
2639 a single range or anti-range as the above is
2640 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2641 but one could use a scheme similar to equivalences for this. */
2642 set_value_range_to_varying (vr);
2643 return;
2646 else if (code == MIN_EXPR
2647 || code == MAX_EXPR)
2649 if (vr0.type == VR_RANGE
2650 && !symbolic_range_p (&vr0))
2652 type = VR_RANGE;
2653 if (vr1.type == VR_RANGE
2654 && !symbolic_range_p (&vr1))
2656 /* For operations that make the resulting range directly
2657 proportional to the original ranges, apply the operation to
2658 the same end of each range. */
2659 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2660 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2662 else if (code == MIN_EXPR)
2664 min = vrp_val_min (expr_type);
2665 max = vr0.max;
2667 else if (code == MAX_EXPR)
2669 min = vr0.min;
2670 max = vrp_val_max (expr_type);
2673 else if (vr1.type == VR_RANGE
2674 && !symbolic_range_p (&vr1))
2676 type = VR_RANGE;
2677 if (code == MIN_EXPR)
2679 min = vrp_val_min (expr_type);
2680 max = vr1.max;
2682 else if (code == MAX_EXPR)
2684 min = vr1.min;
2685 max = vrp_val_max (expr_type);
2688 else
2690 set_value_range_to_varying (vr);
2691 return;
2694 else if (code == MULT_EXPR)
2696 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2697 drop to varying. This test requires 2*prec bits if both
2698 operands are signed and 2*prec + 2 bits if either is not. */
2700 signop sign = TYPE_SIGN (expr_type);
2701 unsigned int prec = TYPE_PRECISION (expr_type);
2703 if (range_int_cst_p (&vr0)
2704 && range_int_cst_p (&vr1)
2705 && TYPE_OVERFLOW_WRAPS (expr_type))
2707 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2708 typedef generic_wide_int
2709 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2710 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2711 vrp_int size = sizem1 + 1;
2713 /* Extend the values using the sign of the result to PREC2.
2714 From here on out, everthing is just signed math no matter
2715 what the input types were. */
2716 vrp_int min0 = vrp_int_cst (vr0.min);
2717 vrp_int max0 = vrp_int_cst (vr0.max);
2718 vrp_int min1 = vrp_int_cst (vr1.min);
2719 vrp_int max1 = vrp_int_cst (vr1.max);
2720 /* Canonicalize the intervals. */
2721 if (sign == UNSIGNED)
2723 if (wi::ltu_p (size, min0 + max0))
2725 min0 -= size;
2726 max0 -= size;
2729 if (wi::ltu_p (size, min1 + max1))
2731 min1 -= size;
2732 max1 -= size;
2736 vrp_int prod0 = min0 * min1;
2737 vrp_int prod1 = min0 * max1;
2738 vrp_int prod2 = max0 * min1;
2739 vrp_int prod3 = max0 * max1;
2741 /* Sort the 4 products so that min is in prod0 and max is in
2742 prod3. */
2743 /* min0min1 > max0max1 */
2744 if (prod0 > prod3)
2745 std::swap (prod0, prod3);
2747 /* min0max1 > max0min1 */
2748 if (prod1 > prod2)
2749 std::swap (prod1, prod2);
2751 if (prod0 > prod1)
2752 std::swap (prod0, prod1);
2754 if (prod2 > prod3)
2755 std::swap (prod2, prod3);
2757 /* diff = max - min. */
2758 prod2 = prod3 - prod0;
2759 if (wi::geu_p (prod2, sizem1))
2761 /* the range covers all values. */
2762 set_value_range_to_varying (vr);
2763 return;
2766 /* The following should handle the wrapping and selecting
2767 VR_ANTI_RANGE for us. */
2768 min = wide_int_to_tree (expr_type, prod0);
2769 max = wide_int_to_tree (expr_type, prod3);
2770 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2771 return;
2774 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2775 drop to VR_VARYING. It would take more effort to compute a
2776 precise range for such a case. For example, if we have
2777 op0 == 65536 and op1 == 65536 with their ranges both being
2778 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2779 we cannot claim that the product is in ~[0,0]. Note that we
2780 are guaranteed to have vr0.type == vr1.type at this
2781 point. */
2782 if (vr0.type == VR_ANTI_RANGE
2783 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2785 set_value_range_to_varying (vr);
2786 return;
2789 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2790 return;
2792 else if (code == RSHIFT_EXPR
2793 || code == LSHIFT_EXPR)
2795 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2796 then drop to VR_VARYING. Outside of this range we get undefined
2797 behavior from the shift operation. We cannot even trust
2798 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2799 shifts, and the operation at the tree level may be widened. */
2800 if (range_int_cst_p (&vr1)
2801 && compare_tree_int (vr1.min, 0) >= 0
2802 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2804 if (code == RSHIFT_EXPR)
2806 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2807 useful ranges just from the shift count. E.g.
2808 x >> 63 for signed 64-bit x is always [-1, 0]. */
2809 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2811 vr0.type = type = VR_RANGE;
2812 vr0.min = vrp_val_min (expr_type);
2813 vr0.max = vrp_val_max (expr_type);
2815 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2816 return;
2818 /* We can map lshifts by constants to MULT_EXPR handling. */
2819 else if (code == LSHIFT_EXPR
2820 && range_int_cst_singleton_p (&vr1))
2822 bool saved_flag_wrapv;
2823 value_range vr1p = VR_INITIALIZER;
2824 vr1p.type = VR_RANGE;
2825 vr1p.min = (wide_int_to_tree
2826 (expr_type,
2827 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2828 TYPE_PRECISION (expr_type))));
2829 vr1p.max = vr1p.min;
2830 /* We have to use a wrapping multiply though as signed overflow
2831 on lshifts is implementation defined in C89. */
2832 saved_flag_wrapv = flag_wrapv;
2833 flag_wrapv = 1;
2834 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2835 &vr0, &vr1p);
2836 flag_wrapv = saved_flag_wrapv;
2837 return;
2839 else if (code == LSHIFT_EXPR
2840 && range_int_cst_p (&vr0))
2842 int prec = TYPE_PRECISION (expr_type);
2843 int overflow_pos = prec;
2844 int bound_shift;
2845 wide_int low_bound, high_bound;
2846 bool uns = TYPE_UNSIGNED (expr_type);
2847 bool in_bounds = false;
2849 if (!uns)
2850 overflow_pos -= 1;
2852 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2853 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2854 overflow. However, for that to happen, vr1.max needs to be
2855 zero, which means vr1 is a singleton range of zero, which
2856 means it should be handled by the previous LSHIFT_EXPR
2857 if-clause. */
2858 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2859 wide_int complement = ~(bound - 1);
2861 if (uns)
2863 low_bound = bound;
2864 high_bound = complement;
2865 if (wi::ltu_p (vr0.max, low_bound))
2867 /* [5, 6] << [1, 2] == [10, 24]. */
2868 /* We're shifting out only zeroes, the value increases
2869 monotonically. */
2870 in_bounds = true;
2872 else if (wi::ltu_p (high_bound, vr0.min))
2874 /* [0xffffff00, 0xffffffff] << [1, 2]
2875 == [0xfffffc00, 0xfffffffe]. */
2876 /* We're shifting out only ones, the value decreases
2877 monotonically. */
2878 in_bounds = true;
2881 else
2883 /* [-1, 1] << [1, 2] == [-4, 4]. */
2884 low_bound = complement;
2885 high_bound = bound;
2886 if (wi::lts_p (vr0.max, high_bound)
2887 && wi::lts_p (low_bound, vr0.min))
2889 /* For non-negative numbers, we're shifting out only
2890 zeroes, the value increases monotonically.
2891 For negative numbers, we're shifting out only ones, the
2892 value decreases monotomically. */
2893 in_bounds = true;
2897 if (in_bounds)
2899 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2900 return;
2904 set_value_range_to_varying (vr);
2905 return;
2907 else if (code == TRUNC_DIV_EXPR
2908 || code == FLOOR_DIV_EXPR
2909 || code == CEIL_DIV_EXPR
2910 || code == EXACT_DIV_EXPR
2911 || code == ROUND_DIV_EXPR)
2913 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2915 /* For division, if op1 has VR_RANGE but op0 does not, something
2916 can be deduced just from that range. Say [min, max] / [4, max]
2917 gives [min / 4, max / 4] range. */
2918 if (vr1.type == VR_RANGE
2919 && !symbolic_range_p (&vr1)
2920 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2922 vr0.type = type = VR_RANGE;
2923 vr0.min = vrp_val_min (expr_type);
2924 vr0.max = vrp_val_max (expr_type);
2926 else
2928 set_value_range_to_varying (vr);
2929 return;
2933 /* For divisions, if flag_non_call_exceptions is true, we must
2934 not eliminate a division by zero. */
2935 if (cfun->can_throw_non_call_exceptions
2936 && (vr1.type != VR_RANGE
2937 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2939 set_value_range_to_varying (vr);
2940 return;
2943 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2944 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2945 include 0. */
2946 if (vr0.type == VR_RANGE
2947 && (vr1.type != VR_RANGE
2948 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2950 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2951 int cmp;
2953 min = NULL_TREE;
2954 max = NULL_TREE;
2955 if (TYPE_UNSIGNED (expr_type)
2956 || value_range_nonnegative_p (&vr1))
2958 /* For unsigned division or when divisor is known
2959 to be non-negative, the range has to cover
2960 all numbers from 0 to max for positive max
2961 and all numbers from min to 0 for negative min. */
2962 cmp = compare_values (vr0.max, zero);
2963 if (cmp == -1)
2965 /* When vr0.max < 0, vr1.min != 0 and value
2966 ranges for dividend and divisor are available. */
2967 if (vr1.type == VR_RANGE
2968 && !symbolic_range_p (&vr0)
2969 && !symbolic_range_p (&vr1)
2970 && compare_values (vr1.min, zero) != 0)
2971 max = int_const_binop (code, vr0.max, vr1.min);
2972 else
2973 max = zero;
2975 else if (cmp == 0 || cmp == 1)
2976 max = vr0.max;
2977 else
2978 type = VR_VARYING;
2979 cmp = compare_values (vr0.min, zero);
2980 if (cmp == 1)
2982 /* For unsigned division when value ranges for dividend
2983 and divisor are available. */
2984 if (vr1.type == VR_RANGE
2985 && !symbolic_range_p (&vr0)
2986 && !symbolic_range_p (&vr1)
2987 && compare_values (vr1.max, zero) != 0)
2988 min = int_const_binop (code, vr0.min, vr1.max);
2989 else
2990 min = zero;
2992 else if (cmp == 0 || cmp == -1)
2993 min = vr0.min;
2994 else
2995 type = VR_VARYING;
2997 else
2999 /* Otherwise the range is -max .. max or min .. -min
3000 depending on which bound is bigger in absolute value,
3001 as the division can change the sign. */
3002 abs_extent_range (vr, vr0.min, vr0.max);
3003 return;
3005 if (type == VR_VARYING)
3007 set_value_range_to_varying (vr);
3008 return;
3011 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
3013 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3014 return;
3017 else if (code == TRUNC_MOD_EXPR)
3019 if (range_is_null (&vr1))
3021 set_value_range_to_undefined (vr);
3022 return;
3024 /* ABS (A % B) < ABS (B) and either
3025 0 <= A % B <= A or A <= A % B <= 0. */
3026 type = VR_RANGE;
3027 signop sgn = TYPE_SIGN (expr_type);
3028 unsigned int prec = TYPE_PRECISION (expr_type);
3029 wide_int wmin, wmax, tmp;
3030 wide_int zero = wi::zero (prec);
3031 wide_int one = wi::one (prec);
3032 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3034 wmax = wi::sub (vr1.max, one);
3035 if (sgn == SIGNED)
3037 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3038 wmax = wi::smax (wmax, tmp);
3041 else
3043 wmax = wi::max_value (prec, sgn);
3044 /* X % INT_MIN may be INT_MAX. */
3045 if (sgn == UNSIGNED)
3046 wmax = wmax - one;
3049 if (sgn == UNSIGNED)
3050 wmin = zero;
3051 else
3053 wmin = -wmax;
3054 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3056 tmp = vr0.min;
3057 if (wi::gts_p (tmp, zero))
3058 tmp = zero;
3059 wmin = wi::smax (wmin, tmp);
3063 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3065 tmp = vr0.max;
3066 if (sgn == SIGNED && wi::neg_p (tmp))
3067 tmp = zero;
3068 wmax = wi::min (wmax, tmp, sgn);
3071 min = wide_int_to_tree (expr_type, wmin);
3072 max = wide_int_to_tree (expr_type, wmax);
3074 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3076 bool int_cst_range0, int_cst_range1;
3077 wide_int may_be_nonzero0, may_be_nonzero1;
3078 wide_int must_be_nonzero0, must_be_nonzero1;
3080 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3081 &may_be_nonzero0,
3082 &must_be_nonzero0);
3083 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3084 &may_be_nonzero1,
3085 &must_be_nonzero1);
3087 type = VR_RANGE;
3088 if (code == BIT_AND_EXPR)
3090 min = wide_int_to_tree (expr_type,
3091 must_be_nonzero0 & must_be_nonzero1);
3092 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3093 /* If both input ranges contain only negative values we can
3094 truncate the result range maximum to the minimum of the
3095 input range maxima. */
3096 if (int_cst_range0 && int_cst_range1
3097 && tree_int_cst_sgn (vr0.max) < 0
3098 && tree_int_cst_sgn (vr1.max) < 0)
3100 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3101 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3103 /* If either input range contains only non-negative values
3104 we can truncate the result range maximum to the respective
3105 maximum of the input range. */
3106 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3107 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3108 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3109 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3110 max = wide_int_to_tree (expr_type, wmax);
3111 cmp = compare_values (min, max);
3112 /* PR68217: In case of signed & sign-bit-CST should
3113 result in [-INF, 0] instead of [-INF, INF]. */
3114 if (cmp == -2 || cmp == 1)
3116 wide_int sign_bit
3117 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
3118 TYPE_PRECISION (expr_type));
3119 if (!TYPE_UNSIGNED (expr_type)
3120 && ((value_range_constant_singleton (&vr0)
3121 && !wi::cmps (vr0.min, sign_bit))
3122 || (value_range_constant_singleton (&vr1)
3123 && !wi::cmps (vr1.min, sign_bit))))
3125 min = TYPE_MIN_VALUE (expr_type);
3126 max = build_int_cst (expr_type, 0);
3130 else if (code == BIT_IOR_EXPR)
3132 max = wide_int_to_tree (expr_type,
3133 may_be_nonzero0 | may_be_nonzero1);
3134 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3135 /* If the input ranges contain only positive values we can
3136 truncate the minimum of the result range to the maximum
3137 of the input range minima. */
3138 if (int_cst_range0 && int_cst_range1
3139 && tree_int_cst_sgn (vr0.min) >= 0
3140 && tree_int_cst_sgn (vr1.min) >= 0)
3142 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3143 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3145 /* If either input range contains only negative values
3146 we can truncate the minimum of the result range to the
3147 respective minimum range. */
3148 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3149 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3150 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3151 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3152 min = wide_int_to_tree (expr_type, wmin);
3154 else if (code == BIT_XOR_EXPR)
3156 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3157 | ~(may_be_nonzero0 | may_be_nonzero1));
3158 wide_int result_one_bits
3159 = (must_be_nonzero0.and_not (may_be_nonzero1)
3160 | must_be_nonzero1.and_not (may_be_nonzero0));
3161 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3162 min = wide_int_to_tree (expr_type, result_one_bits);
3163 /* If the range has all positive or all negative values the
3164 result is better than VARYING. */
3165 if (tree_int_cst_sgn (min) < 0
3166 || tree_int_cst_sgn (max) >= 0)
3168 else
3169 max = min = NULL_TREE;
3172 else
3173 gcc_unreachable ();
3175 /* If either MIN or MAX overflowed, then set the resulting range to
3176 VARYING. But we do accept an overflow infinity representation. */
3177 if (min == NULL_TREE
3178 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3179 || max == NULL_TREE
3180 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3182 set_value_range_to_varying (vr);
3183 return;
3186 /* We punt if:
3187 1) [-INF, +INF]
3188 2) [-INF, +-INF(OVF)]
3189 3) [+-INF(OVF), +INF]
3190 4) [+-INF(OVF), +-INF(OVF)]
3191 We learn nothing when we have INF and INF(OVF) on both sides.
3192 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3193 overflow. */
3194 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3195 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3197 set_value_range_to_varying (vr);
3198 return;
3201 cmp = compare_values (min, max);
3202 if (cmp == -2 || cmp == 1)
3204 /* If the new range has its limits swapped around (MIN > MAX),
3205 then the operation caused one of them to wrap around, mark
3206 the new range VARYING. */
3207 set_value_range_to_varying (vr);
3209 else
3210 set_value_range (vr, type, min, max, NULL);
3213 /* Extract range information from a binary expression OP0 CODE OP1 based on
3214 the ranges of each of its operands with resulting type EXPR_TYPE.
3215 The resulting range is stored in *VR. */
3217 static void
3218 extract_range_from_binary_expr (value_range *vr,
3219 enum tree_code code,
3220 tree expr_type, tree op0, tree op1)
3222 value_range vr0 = VR_INITIALIZER;
3223 value_range vr1 = VR_INITIALIZER;
3225 /* Get value ranges for each operand. For constant operands, create
3226 a new value range with the operand to simplify processing. */
3227 if (TREE_CODE (op0) == SSA_NAME)
3228 vr0 = *(get_value_range (op0));
3229 else if (is_gimple_min_invariant (op0))
3230 set_value_range_to_value (&vr0, op0, NULL);
3231 else
3232 set_value_range_to_varying (&vr0);
3234 if (TREE_CODE (op1) == SSA_NAME)
3235 vr1 = *(get_value_range (op1));
3236 else if (is_gimple_min_invariant (op1))
3237 set_value_range_to_value (&vr1, op1, NULL);
3238 else
3239 set_value_range_to_varying (&vr1);
3241 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3243 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3244 and based on the other operand, for example if it was deduced from a
3245 symbolic comparison. When a bound of the range of the first operand
3246 is invariant, we set the corresponding bound of the new range to INF
3247 in order to avoid recursing on the range of the second operand. */
3248 if (vr->type == VR_VARYING
3249 && (code == PLUS_EXPR || code == MINUS_EXPR)
3250 && TREE_CODE (op1) == SSA_NAME
3251 && vr0.type == VR_RANGE
3252 && symbolic_range_based_on_p (&vr0, op1))
3254 const bool minus_p = (code == MINUS_EXPR);
3255 value_range n_vr1 = VR_INITIALIZER;
3257 /* Try with VR0 and [-INF, OP1]. */
3258 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3259 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3261 /* Try with VR0 and [OP1, +INF]. */
3262 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3263 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3265 /* Try with VR0 and [OP1, OP1]. */
3266 else
3267 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3269 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3272 if (vr->type == VR_VARYING
3273 && (code == PLUS_EXPR || code == MINUS_EXPR)
3274 && TREE_CODE (op0) == SSA_NAME
3275 && vr1.type == VR_RANGE
3276 && symbolic_range_based_on_p (&vr1, op0))
3278 const bool minus_p = (code == MINUS_EXPR);
3279 value_range n_vr0 = VR_INITIALIZER;
3281 /* Try with [-INF, OP0] and VR1. */
3282 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3283 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3285 /* Try with [OP0, +INF] and VR1. */
3286 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3287 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3289 /* Try with [OP0, OP0] and VR1. */
3290 else
3291 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3293 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3297 /* Extract range information from a unary operation CODE based on
3298 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3299 The resulting range is stored in *VR. */
3301 void
3302 extract_range_from_unary_expr (value_range *vr,
3303 enum tree_code code, tree type,
3304 value_range *vr0_, tree op0_type)
3306 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3308 /* VRP only operates on integral and pointer types. */
3309 if (!(INTEGRAL_TYPE_P (op0_type)
3310 || POINTER_TYPE_P (op0_type))
3311 || !(INTEGRAL_TYPE_P (type)
3312 || POINTER_TYPE_P (type)))
3314 set_value_range_to_varying (vr);
3315 return;
3318 /* If VR0 is UNDEFINED, so is the result. */
3319 if (vr0.type == VR_UNDEFINED)
3321 set_value_range_to_undefined (vr);
3322 return;
3325 /* Handle operations that we express in terms of others. */
3326 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3328 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3329 copy_value_range (vr, &vr0);
3330 return;
3332 else if (code == NEGATE_EXPR)
3334 /* -X is simply 0 - X, so re-use existing code that also handles
3335 anti-ranges fine. */
3336 value_range zero = VR_INITIALIZER;
3337 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3338 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3339 return;
3341 else if (code == BIT_NOT_EXPR)
3343 /* ~X is simply -1 - X, so re-use existing code that also handles
3344 anti-ranges fine. */
3345 value_range minusone = VR_INITIALIZER;
3346 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3347 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3348 type, &minusone, &vr0);
3349 return;
3352 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3353 and express op ~[] as (op []') U (op []''). */
3354 if (vr0.type == VR_ANTI_RANGE
3355 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3357 extract_range_from_unary_expr (vr, code, type, &vrtem0, op0_type);
3358 if (vrtem1.type != VR_UNDEFINED)
3360 value_range vrres = VR_INITIALIZER;
3361 extract_range_from_unary_expr (&vrres, code, type,
3362 &vrtem1, op0_type);
3363 vrp_meet (vr, &vrres);
3365 return;
3368 if (CONVERT_EXPR_CODE_P (code))
3370 tree inner_type = op0_type;
3371 tree outer_type = type;
3373 /* If the expression evaluates to a pointer, we are only interested in
3374 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3375 if (POINTER_TYPE_P (type))
3377 if (range_is_nonnull (&vr0))
3378 set_value_range_to_nonnull (vr, type);
3379 else if (range_is_null (&vr0))
3380 set_value_range_to_null (vr, type);
3381 else
3382 set_value_range_to_varying (vr);
3383 return;
3386 /* If VR0 is varying and we increase the type precision, assume
3387 a full range for the following transformation. */
3388 if (vr0.type == VR_VARYING
3389 && INTEGRAL_TYPE_P (inner_type)
3390 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3392 vr0.type = VR_RANGE;
3393 vr0.min = TYPE_MIN_VALUE (inner_type);
3394 vr0.max = TYPE_MAX_VALUE (inner_type);
3397 /* If VR0 is a constant range or anti-range and the conversion is
3398 not truncating we can convert the min and max values and
3399 canonicalize the resulting range. Otherwise we can do the
3400 conversion if the size of the range is less than what the
3401 precision of the target type can represent and the range is
3402 not an anti-range. */
3403 if ((vr0.type == VR_RANGE
3404 || vr0.type == VR_ANTI_RANGE)
3405 && TREE_CODE (vr0.min) == INTEGER_CST
3406 && TREE_CODE (vr0.max) == INTEGER_CST
3407 && (!is_overflow_infinity (vr0.min)
3408 || (vr0.type == VR_RANGE
3409 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3410 && needs_overflow_infinity (outer_type)
3411 && supports_overflow_infinity (outer_type)))
3412 && (!is_overflow_infinity (vr0.max)
3413 || (vr0.type == VR_RANGE
3414 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3415 && needs_overflow_infinity (outer_type)
3416 && supports_overflow_infinity (outer_type)))
3417 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3418 || (vr0.type == VR_RANGE
3419 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3420 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3421 size_int (TYPE_PRECISION (outer_type)))))))
3423 tree new_min, new_max;
3424 if (is_overflow_infinity (vr0.min))
3425 new_min = negative_overflow_infinity (outer_type);
3426 else
3427 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3428 0, false);
3429 if (is_overflow_infinity (vr0.max))
3430 new_max = positive_overflow_infinity (outer_type);
3431 else
3432 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3433 0, false);
3434 set_and_canonicalize_value_range (vr, vr0.type,
3435 new_min, new_max, NULL);
3436 return;
3439 set_value_range_to_varying (vr);
3440 return;
3442 else if (code == ABS_EXPR)
3444 tree min, max;
3445 int cmp;
3447 /* Pass through vr0 in the easy cases. */
3448 if (TYPE_UNSIGNED (type)
3449 || value_range_nonnegative_p (&vr0))
3451 copy_value_range (vr, &vr0);
3452 return;
3455 /* For the remaining varying or symbolic ranges we can't do anything
3456 useful. */
3457 if (vr0.type == VR_VARYING
3458 || symbolic_range_p (&vr0))
3460 set_value_range_to_varying (vr);
3461 return;
3464 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3465 useful range. */
3466 if (!TYPE_OVERFLOW_UNDEFINED (type)
3467 && ((vr0.type == VR_RANGE
3468 && vrp_val_is_min (vr0.min))
3469 || (vr0.type == VR_ANTI_RANGE
3470 && !vrp_val_is_min (vr0.min))))
3472 set_value_range_to_varying (vr);
3473 return;
3476 /* ABS_EXPR may flip the range around, if the original range
3477 included negative values. */
3478 if (is_overflow_infinity (vr0.min))
3479 min = positive_overflow_infinity (type);
3480 else if (!vrp_val_is_min (vr0.min))
3481 min = fold_unary_to_constant (code, type, vr0.min);
3482 else if (!needs_overflow_infinity (type))
3483 min = TYPE_MAX_VALUE (type);
3484 else if (supports_overflow_infinity (type))
3485 min = positive_overflow_infinity (type);
3486 else
3488 set_value_range_to_varying (vr);
3489 return;
3492 if (is_overflow_infinity (vr0.max))
3493 max = positive_overflow_infinity (type);
3494 else if (!vrp_val_is_min (vr0.max))
3495 max = fold_unary_to_constant (code, type, vr0.max);
3496 else if (!needs_overflow_infinity (type))
3497 max = TYPE_MAX_VALUE (type);
3498 else if (supports_overflow_infinity (type)
3499 /* We shouldn't generate [+INF, +INF] as set_value_range
3500 doesn't like this and ICEs. */
3501 && !is_positive_overflow_infinity (min))
3502 max = positive_overflow_infinity (type);
3503 else
3505 set_value_range_to_varying (vr);
3506 return;
3509 cmp = compare_values (min, max);
3511 /* If a VR_ANTI_RANGEs contains zero, then we have
3512 ~[-INF, min(MIN, MAX)]. */
3513 if (vr0.type == VR_ANTI_RANGE)
3515 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3517 /* Take the lower of the two values. */
3518 if (cmp != 1)
3519 max = min;
3521 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3522 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3523 flag_wrapv is set and the original anti-range doesn't include
3524 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3525 if (TYPE_OVERFLOW_WRAPS (type))
3527 tree type_min_value = TYPE_MIN_VALUE (type);
3529 min = (vr0.min != type_min_value
3530 ? int_const_binop (PLUS_EXPR, type_min_value,
3531 build_int_cst (TREE_TYPE (type_min_value), 1))
3532 : type_min_value);
3534 else
3536 if (overflow_infinity_range_p (&vr0))
3537 min = negative_overflow_infinity (type);
3538 else
3539 min = TYPE_MIN_VALUE (type);
3542 else
3544 /* All else has failed, so create the range [0, INF], even for
3545 flag_wrapv since TYPE_MIN_VALUE is in the original
3546 anti-range. */
3547 vr0.type = VR_RANGE;
3548 min = build_int_cst (type, 0);
3549 if (needs_overflow_infinity (type))
3551 if (supports_overflow_infinity (type))
3552 max = positive_overflow_infinity (type);
3553 else
3555 set_value_range_to_varying (vr);
3556 return;
3559 else
3560 max = TYPE_MAX_VALUE (type);
3564 /* If the range contains zero then we know that the minimum value in the
3565 range will be zero. */
3566 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3568 if (cmp == 1)
3569 max = min;
3570 min = build_int_cst (type, 0);
3572 else
3574 /* If the range was reversed, swap MIN and MAX. */
3575 if (cmp == 1)
3576 std::swap (min, max);
3579 cmp = compare_values (min, max);
3580 if (cmp == -2 || cmp == 1)
3582 /* If the new range has its limits swapped around (MIN > MAX),
3583 then the operation caused one of them to wrap around, mark
3584 the new range VARYING. */
3585 set_value_range_to_varying (vr);
3587 else
3588 set_value_range (vr, vr0.type, min, max, NULL);
3589 return;
3592 /* For unhandled operations fall back to varying. */
3593 set_value_range_to_varying (vr);
3594 return;
3598 /* Extract range information from a unary expression CODE OP0 based on
3599 the range of its operand with resulting type TYPE.
3600 The resulting range is stored in *VR. */
3602 static void
3603 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3604 tree type, tree op0)
3606 value_range vr0 = VR_INITIALIZER;
3608 /* Get value ranges for the operand. For constant operands, create
3609 a new value range with the operand to simplify processing. */
3610 if (TREE_CODE (op0) == SSA_NAME)
3611 vr0 = *(get_value_range (op0));
3612 else if (is_gimple_min_invariant (op0))
3613 set_value_range_to_value (&vr0, op0, NULL);
3614 else
3615 set_value_range_to_varying (&vr0);
3617 extract_range_from_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0));
3621 /* Extract range information from a conditional expression STMT based on
3622 the ranges of each of its operands and the expression code. */
3624 static void
3625 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3627 tree op0, op1;
3628 value_range vr0 = VR_INITIALIZER;
3629 value_range vr1 = VR_INITIALIZER;
3631 /* Get value ranges for each operand. For constant operands, create
3632 a new value range with the operand to simplify processing. */
3633 op0 = gimple_assign_rhs2 (stmt);
3634 if (TREE_CODE (op0) == SSA_NAME)
3635 vr0 = *(get_value_range (op0));
3636 else if (is_gimple_min_invariant (op0))
3637 set_value_range_to_value (&vr0, op0, NULL);
3638 else
3639 set_value_range_to_varying (&vr0);
3641 op1 = gimple_assign_rhs3 (stmt);
3642 if (TREE_CODE (op1) == SSA_NAME)
3643 vr1 = *(get_value_range (op1));
3644 else if (is_gimple_min_invariant (op1))
3645 set_value_range_to_value (&vr1, op1, NULL);
3646 else
3647 set_value_range_to_varying (&vr1);
3649 /* The resulting value range is the union of the operand ranges */
3650 copy_value_range (vr, &vr0);
3651 vrp_meet (vr, &vr1);
3655 /* Extract range information from a comparison expression EXPR based
3656 on the range of its operand and the expression code. */
3658 static void
3659 extract_range_from_comparison (value_range *vr, enum tree_code code,
3660 tree type, tree op0, tree op1)
3662 bool sop = false;
3663 tree val;
3665 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3666 NULL);
3668 /* A disadvantage of using a special infinity as an overflow
3669 representation is that we lose the ability to record overflow
3670 when we don't have an infinity. So we have to ignore a result
3671 which relies on overflow. */
3673 if (val && !is_overflow_infinity (val) && !sop)
3675 /* Since this expression was found on the RHS of an assignment,
3676 its type may be different from _Bool. Convert VAL to EXPR's
3677 type. */
3678 val = fold_convert (type, val);
3679 if (is_gimple_min_invariant (val))
3680 set_value_range_to_value (vr, val, vr->equiv);
3681 else
3682 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3684 else
3685 /* The result of a comparison is always true or false. */
3686 set_value_range_to_truthvalue (vr, type);
3689 /* Helper function for simplify_internal_call_using_ranges and
3690 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3691 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3692 always overflow. Set *OVF to true if it is known to always
3693 overflow. */
3695 static bool
3696 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3697 tree op0, tree op1, bool *ovf)
3699 value_range vr0 = VR_INITIALIZER;
3700 value_range vr1 = VR_INITIALIZER;
3701 if (TREE_CODE (op0) == SSA_NAME)
3702 vr0 = *get_value_range (op0);
3703 else if (TREE_CODE (op0) == INTEGER_CST)
3704 set_value_range_to_value (&vr0, op0, NULL);
3705 else
3706 set_value_range_to_varying (&vr0);
3708 if (TREE_CODE (op1) == SSA_NAME)
3709 vr1 = *get_value_range (op1);
3710 else if (TREE_CODE (op1) == INTEGER_CST)
3711 set_value_range_to_value (&vr1, op1, NULL);
3712 else
3713 set_value_range_to_varying (&vr1);
3715 if (!range_int_cst_p (&vr0)
3716 || TREE_OVERFLOW (vr0.min)
3717 || TREE_OVERFLOW (vr0.max))
3719 vr0.min = vrp_val_min (TREE_TYPE (op0));
3720 vr0.max = vrp_val_max (TREE_TYPE (op0));
3722 if (!range_int_cst_p (&vr1)
3723 || TREE_OVERFLOW (vr1.min)
3724 || TREE_OVERFLOW (vr1.max))
3726 vr1.min = vrp_val_min (TREE_TYPE (op1));
3727 vr1.max = vrp_val_max (TREE_TYPE (op1));
3729 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3730 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3731 if (arith_overflowed_p (subcode, type, vr0.max,
3732 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3733 return false;
3734 if (subcode == MULT_EXPR)
3736 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3737 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3738 return false;
3740 if (*ovf)
3742 /* So far we found that there is an overflow on the boundaries.
3743 That doesn't prove that there is an overflow even for all values
3744 in between the boundaries. For that compute widest_int range
3745 of the result and see if it doesn't overlap the range of
3746 type. */
3747 widest_int wmin, wmax;
3748 widest_int w[4];
3749 int i;
3750 w[0] = wi::to_widest (vr0.min);
3751 w[1] = wi::to_widest (vr0.max);
3752 w[2] = wi::to_widest (vr1.min);
3753 w[3] = wi::to_widest (vr1.max);
3754 for (i = 0; i < 4; i++)
3756 widest_int wt;
3757 switch (subcode)
3759 case PLUS_EXPR:
3760 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3761 break;
3762 case MINUS_EXPR:
3763 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3764 break;
3765 case MULT_EXPR:
3766 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3767 break;
3768 default:
3769 gcc_unreachable ();
3771 if (i == 0)
3773 wmin = wt;
3774 wmax = wt;
3776 else
3778 wmin = wi::smin (wmin, wt);
3779 wmax = wi::smax (wmax, wt);
3782 /* The result of op0 CODE op1 is known to be in range
3783 [wmin, wmax]. */
3784 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3785 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3786 /* If all values in [wmin, wmax] are smaller than
3787 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3788 the arithmetic operation will always overflow. */
3789 if (wmax < wtmin || wmin > wtmax)
3790 return true;
3791 return false;
3793 return true;
3796 /* Try to derive a nonnegative or nonzero range out of STMT relying
3797 primarily on generic routines in fold in conjunction with range data.
3798 Store the result in *VR */
3800 static void
3801 extract_range_basic (value_range *vr, gimple *stmt)
3803 bool sop = false;
3804 tree type = gimple_expr_type (stmt);
3806 if (is_gimple_call (stmt))
3808 tree arg;
3809 int mini, maxi, zerov = 0, prec;
3810 enum tree_code subcode = ERROR_MARK;
3811 combined_fn cfn = gimple_call_combined_fn (stmt);
3813 switch (cfn)
3815 case CFN_BUILT_IN_CONSTANT_P:
3816 /* If the call is __builtin_constant_p and the argument is a
3817 function parameter resolve it to false. This avoids bogus
3818 array bound warnings.
3819 ??? We could do this as early as inlining is finished. */
3820 arg = gimple_call_arg (stmt, 0);
3821 if (TREE_CODE (arg) == SSA_NAME
3822 && SSA_NAME_IS_DEFAULT_DEF (arg)
3823 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
3824 && cfun->after_inlining)
3826 set_value_range_to_null (vr, type);
3827 return;
3829 break;
3830 /* Both __builtin_ffs* and __builtin_popcount return
3831 [0, prec]. */
3832 CASE_CFN_FFS:
3833 CASE_CFN_POPCOUNT:
3834 arg = gimple_call_arg (stmt, 0);
3835 prec = TYPE_PRECISION (TREE_TYPE (arg));
3836 mini = 0;
3837 maxi = prec;
3838 if (TREE_CODE (arg) == SSA_NAME)
3840 value_range *vr0 = get_value_range (arg);
3841 /* If arg is non-zero, then ffs or popcount
3842 are non-zero. */
3843 if (((vr0->type == VR_RANGE
3844 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3845 || (vr0->type == VR_ANTI_RANGE
3846 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3847 && !is_overflow_infinity (vr0->min)
3848 && !is_overflow_infinity (vr0->max))
3849 mini = 1;
3850 /* If some high bits are known to be zero,
3851 we can decrease the maximum. */
3852 if (vr0->type == VR_RANGE
3853 && TREE_CODE (vr0->max) == INTEGER_CST
3854 && !operand_less_p (vr0->min,
3855 build_zero_cst (TREE_TYPE (vr0->min)))
3856 && !is_overflow_infinity (vr0->max))
3857 maxi = tree_floor_log2 (vr0->max) + 1;
3859 goto bitop_builtin;
3860 /* __builtin_parity* returns [0, 1]. */
3861 CASE_CFN_PARITY:
3862 mini = 0;
3863 maxi = 1;
3864 goto bitop_builtin;
3865 /* __builtin_c[lt]z* return [0, prec-1], except for
3866 when the argument is 0, but that is undefined behavior.
3867 On many targets where the CLZ RTL or optab value is defined
3868 for 0 the value is prec, so include that in the range
3869 by default. */
3870 CASE_CFN_CLZ:
3871 arg = gimple_call_arg (stmt, 0);
3872 prec = TYPE_PRECISION (TREE_TYPE (arg));
3873 mini = 0;
3874 maxi = prec;
3875 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3876 != CODE_FOR_nothing
3877 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3878 zerov)
3879 /* Handle only the single common value. */
3880 && zerov != prec)
3881 /* Magic value to give up, unless vr0 proves
3882 arg is non-zero. */
3883 mini = -2;
3884 if (TREE_CODE (arg) == SSA_NAME)
3886 value_range *vr0 = get_value_range (arg);
3887 /* From clz of VR_RANGE minimum we can compute
3888 result maximum. */
3889 if (vr0->type == VR_RANGE
3890 && TREE_CODE (vr0->min) == INTEGER_CST
3891 && !is_overflow_infinity (vr0->min))
3893 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3894 if (maxi != prec)
3895 mini = 0;
3897 else if (vr0->type == VR_ANTI_RANGE
3898 && integer_zerop (vr0->min)
3899 && !is_overflow_infinity (vr0->min))
3901 maxi = prec - 1;
3902 mini = 0;
3904 if (mini == -2)
3905 break;
3906 /* From clz of VR_RANGE maximum we can compute
3907 result minimum. */
3908 if (vr0->type == VR_RANGE
3909 && TREE_CODE (vr0->max) == INTEGER_CST
3910 && !is_overflow_infinity (vr0->max))
3912 mini = prec - 1 - tree_floor_log2 (vr0->max);
3913 if (mini == prec)
3914 break;
3917 if (mini == -2)
3918 break;
3919 goto bitop_builtin;
3920 /* __builtin_ctz* return [0, prec-1], except for
3921 when the argument is 0, but that is undefined behavior.
3922 If there is a ctz optab for this mode and
3923 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3924 otherwise just assume 0 won't be seen. */
3925 CASE_CFN_CTZ:
3926 arg = gimple_call_arg (stmt, 0);
3927 prec = TYPE_PRECISION (TREE_TYPE (arg));
3928 mini = 0;
3929 maxi = prec - 1;
3930 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3931 != CODE_FOR_nothing
3932 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3933 zerov))
3935 /* Handle only the two common values. */
3936 if (zerov == -1)
3937 mini = -1;
3938 else if (zerov == prec)
3939 maxi = prec;
3940 else
3941 /* Magic value to give up, unless vr0 proves
3942 arg is non-zero. */
3943 mini = -2;
3945 if (TREE_CODE (arg) == SSA_NAME)
3947 value_range *vr0 = get_value_range (arg);
3948 /* If arg is non-zero, then use [0, prec - 1]. */
3949 if (((vr0->type == VR_RANGE
3950 && integer_nonzerop (vr0->min))
3951 || (vr0->type == VR_ANTI_RANGE
3952 && integer_zerop (vr0->min)))
3953 && !is_overflow_infinity (vr0->min))
3955 mini = 0;
3956 maxi = prec - 1;
3958 /* If some high bits are known to be zero,
3959 we can decrease the result maximum. */
3960 if (vr0->type == VR_RANGE
3961 && TREE_CODE (vr0->max) == INTEGER_CST
3962 && !is_overflow_infinity (vr0->max))
3964 maxi = tree_floor_log2 (vr0->max);
3965 /* For vr0 [0, 0] give up. */
3966 if (maxi == -1)
3967 break;
3970 if (mini == -2)
3971 break;
3972 goto bitop_builtin;
3973 /* __builtin_clrsb* returns [0, prec-1]. */
3974 CASE_CFN_CLRSB:
3975 arg = gimple_call_arg (stmt, 0);
3976 prec = TYPE_PRECISION (TREE_TYPE (arg));
3977 mini = 0;
3978 maxi = prec - 1;
3979 goto bitop_builtin;
3980 bitop_builtin:
3981 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3982 build_int_cst (type, maxi), NULL);
3983 return;
3984 case CFN_UBSAN_CHECK_ADD:
3985 subcode = PLUS_EXPR;
3986 break;
3987 case CFN_UBSAN_CHECK_SUB:
3988 subcode = MINUS_EXPR;
3989 break;
3990 case CFN_UBSAN_CHECK_MUL:
3991 subcode = MULT_EXPR;
3992 break;
3993 case CFN_GOACC_DIM_SIZE:
3994 case CFN_GOACC_DIM_POS:
3995 /* Optimizing these two internal functions helps the loop
3996 optimizer eliminate outer comparisons. Size is [1,N]
3997 and pos is [0,N-1]. */
3999 bool is_pos = cfn == CFN_GOACC_DIM_POS;
4000 int axis = get_oacc_ifn_dim_arg (stmt);
4001 int size = get_oacc_fn_dim_size (current_function_decl, axis);
4003 if (!size)
4004 /* If it's dynamic, the backend might know a hardware
4005 limitation. */
4006 size = targetm.goacc.dim_limit (axis);
4008 tree type = TREE_TYPE (gimple_call_lhs (stmt));
4009 set_value_range (vr, VR_RANGE,
4010 build_int_cst (type, is_pos ? 0 : 1),
4011 size ? build_int_cst (type, size - is_pos)
4012 : vrp_val_max (type), NULL);
4014 return;
4015 default:
4016 break;
4018 if (subcode != ERROR_MARK)
4020 bool saved_flag_wrapv = flag_wrapv;
4021 /* Pretend the arithmetics is wrapping. If there is
4022 any overflow, we'll complain, but will actually do
4023 wrapping operation. */
4024 flag_wrapv = 1;
4025 extract_range_from_binary_expr (vr, subcode, type,
4026 gimple_call_arg (stmt, 0),
4027 gimple_call_arg (stmt, 1));
4028 flag_wrapv = saved_flag_wrapv;
4030 /* If for both arguments vrp_valueize returned non-NULL,
4031 this should have been already folded and if not, it
4032 wasn't folded because of overflow. Avoid removing the
4033 UBSAN_CHECK_* calls in that case. */
4034 if (vr->type == VR_RANGE
4035 && (vr->min == vr->max
4036 || operand_equal_p (vr->min, vr->max, 0)))
4037 set_value_range_to_varying (vr);
4038 return;
4041 /* Handle extraction of the two results (result of arithmetics and
4042 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4043 internal function. */
4044 else if (is_gimple_assign (stmt)
4045 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4046 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4047 && INTEGRAL_TYPE_P (type))
4049 enum tree_code code = gimple_assign_rhs_code (stmt);
4050 tree op = gimple_assign_rhs1 (stmt);
4051 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4053 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4054 if (is_gimple_call (g) && gimple_call_internal_p (g))
4056 enum tree_code subcode = ERROR_MARK;
4057 switch (gimple_call_internal_fn (g))
4059 case IFN_ADD_OVERFLOW:
4060 subcode = PLUS_EXPR;
4061 break;
4062 case IFN_SUB_OVERFLOW:
4063 subcode = MINUS_EXPR;
4064 break;
4065 case IFN_MUL_OVERFLOW:
4066 subcode = MULT_EXPR;
4067 break;
4068 default:
4069 break;
4071 if (subcode != ERROR_MARK)
4073 tree op0 = gimple_call_arg (g, 0);
4074 tree op1 = gimple_call_arg (g, 1);
4075 if (code == IMAGPART_EXPR)
4077 bool ovf = false;
4078 if (check_for_binary_op_overflow (subcode, type,
4079 op0, op1, &ovf))
4080 set_value_range_to_value (vr,
4081 build_int_cst (type, ovf),
4082 NULL);
4083 else if (TYPE_PRECISION (type) == 1
4084 && !TYPE_UNSIGNED (type))
4085 set_value_range_to_varying (vr);
4086 else
4087 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4088 build_int_cst (type, 1), NULL);
4090 else if (types_compatible_p (type, TREE_TYPE (op0))
4091 && types_compatible_p (type, TREE_TYPE (op1)))
4093 bool saved_flag_wrapv = flag_wrapv;
4094 /* Pretend the arithmetics is wrapping. If there is
4095 any overflow, IMAGPART_EXPR will be set. */
4096 flag_wrapv = 1;
4097 extract_range_from_binary_expr (vr, subcode, type,
4098 op0, op1);
4099 flag_wrapv = saved_flag_wrapv;
4101 else
4103 value_range vr0 = VR_INITIALIZER;
4104 value_range vr1 = VR_INITIALIZER;
4105 bool saved_flag_wrapv = flag_wrapv;
4106 /* Pretend the arithmetics is wrapping. If there is
4107 any overflow, IMAGPART_EXPR will be set. */
4108 flag_wrapv = 1;
4109 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4110 type, op0);
4111 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4112 type, op1);
4113 extract_range_from_binary_expr_1 (vr, subcode, type,
4114 &vr0, &vr1);
4115 flag_wrapv = saved_flag_wrapv;
4117 return;
4122 if (INTEGRAL_TYPE_P (type)
4123 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4124 set_value_range_to_nonnegative (vr, type,
4125 sop || stmt_overflow_infinity (stmt));
4126 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4127 && !sop)
4128 set_value_range_to_nonnull (vr, type);
4129 else
4130 set_value_range_to_varying (vr);
4134 /* Try to compute a useful range out of assignment STMT and store it
4135 in *VR. */
4137 static void
4138 extract_range_from_assignment (value_range *vr, gassign *stmt)
4140 enum tree_code code = gimple_assign_rhs_code (stmt);
4142 if (code == ASSERT_EXPR)
4143 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4144 else if (code == SSA_NAME)
4145 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4146 else if (TREE_CODE_CLASS (code) == tcc_binary)
4147 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4148 gimple_expr_type (stmt),
4149 gimple_assign_rhs1 (stmt),
4150 gimple_assign_rhs2 (stmt));
4151 else if (TREE_CODE_CLASS (code) == tcc_unary)
4152 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4153 gimple_expr_type (stmt),
4154 gimple_assign_rhs1 (stmt));
4155 else if (code == COND_EXPR)
4156 extract_range_from_cond_expr (vr, stmt);
4157 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4158 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4159 gimple_expr_type (stmt),
4160 gimple_assign_rhs1 (stmt),
4161 gimple_assign_rhs2 (stmt));
4162 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4163 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4164 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4165 else
4166 set_value_range_to_varying (vr);
4168 if (vr->type == VR_VARYING)
4169 extract_range_basic (vr, stmt);
4172 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4173 would be profitable to adjust VR using scalar evolution information
4174 for VAR. If so, update VR with the new limits. */
4176 static void
4177 adjust_range_with_scev (value_range *vr, struct loop *loop,
4178 gimple *stmt, tree var)
4180 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4181 enum ev_direction dir;
4183 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4184 better opportunities than a regular range, but I'm not sure. */
4185 if (vr->type == VR_ANTI_RANGE)
4186 return;
4188 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4190 /* Like in PR19590, scev can return a constant function. */
4191 if (is_gimple_min_invariant (chrec))
4193 set_value_range_to_value (vr, chrec, vr->equiv);
4194 return;
4197 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4198 return;
4200 init = initial_condition_in_loop_num (chrec, loop->num);
4201 tem = op_with_constant_singleton_value_range (init);
4202 if (tem)
4203 init = tem;
4204 step = evolution_part_in_loop_num (chrec, loop->num);
4205 tem = op_with_constant_singleton_value_range (step);
4206 if (tem)
4207 step = tem;
4209 /* If STEP is symbolic, we can't know whether INIT will be the
4210 minimum or maximum value in the range. Also, unless INIT is
4211 a simple expression, compare_values and possibly other functions
4212 in tree-vrp won't be able to handle it. */
4213 if (step == NULL_TREE
4214 || !is_gimple_min_invariant (step)
4215 || !valid_value_p (init))
4216 return;
4218 dir = scev_direction (chrec);
4219 if (/* Do not adjust ranges if we do not know whether the iv increases
4220 or decreases, ... */
4221 dir == EV_DIR_UNKNOWN
4222 /* ... or if it may wrap. */
4223 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
4224 get_chrec_loop (chrec), true))
4225 return;
4227 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4228 negative_overflow_infinity and positive_overflow_infinity,
4229 because we have concluded that the loop probably does not
4230 wrap. */
4232 type = TREE_TYPE (var);
4233 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4234 tmin = lower_bound_in_type (type, type);
4235 else
4236 tmin = TYPE_MIN_VALUE (type);
4237 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4238 tmax = upper_bound_in_type (type, type);
4239 else
4240 tmax = TYPE_MAX_VALUE (type);
4242 /* Try to use estimated number of iterations for the loop to constrain the
4243 final value in the evolution. */
4244 if (TREE_CODE (step) == INTEGER_CST
4245 && is_gimple_val (init)
4246 && (TREE_CODE (init) != SSA_NAME
4247 || get_value_range (init)->type == VR_RANGE))
4249 widest_int nit;
4251 /* We are only entering here for loop header PHI nodes, so using
4252 the number of latch executions is the correct thing to use. */
4253 if (max_loop_iterations (loop, &nit))
4255 value_range maxvr = VR_INITIALIZER;
4256 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4257 bool overflow;
4259 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4260 &overflow);
4261 /* If the multiplication overflowed we can't do a meaningful
4262 adjustment. Likewise if the result doesn't fit in the type
4263 of the induction variable. For a signed type we have to
4264 check whether the result has the expected signedness which
4265 is that of the step as number of iterations is unsigned. */
4266 if (!overflow
4267 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4268 && (sgn == UNSIGNED
4269 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4271 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4272 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4273 TREE_TYPE (init), init, tem);
4274 /* Likewise if the addition did. */
4275 if (maxvr.type == VR_RANGE)
4277 value_range initvr = VR_INITIALIZER;
4279 if (TREE_CODE (init) == SSA_NAME)
4280 initvr = *(get_value_range (init));
4281 else if (is_gimple_min_invariant (init))
4282 set_value_range_to_value (&initvr, init, NULL);
4283 else
4284 return;
4286 /* Check if init + nit * step overflows. Though we checked
4287 scev {init, step}_loop doesn't wrap, it is not enough
4288 because the loop may exit immediately. Overflow could
4289 happen in the plus expression in this case. */
4290 if ((dir == EV_DIR_DECREASES
4291 && (is_negative_overflow_infinity (maxvr.min)
4292 || compare_values (maxvr.min, initvr.min) != -1))
4293 || (dir == EV_DIR_GROWS
4294 && (is_positive_overflow_infinity (maxvr.max)
4295 || compare_values (maxvr.max, initvr.max) != 1)))
4296 return;
4298 tmin = maxvr.min;
4299 tmax = maxvr.max;
4305 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4307 min = tmin;
4308 max = tmax;
4310 /* For VARYING or UNDEFINED ranges, just about anything we get
4311 from scalar evolutions should be better. */
4313 if (dir == EV_DIR_DECREASES)
4314 max = init;
4315 else
4316 min = init;
4318 else if (vr->type == VR_RANGE)
4320 min = vr->min;
4321 max = vr->max;
4323 if (dir == EV_DIR_DECREASES)
4325 /* INIT is the maximum value. If INIT is lower than VR->MAX
4326 but no smaller than VR->MIN, set VR->MAX to INIT. */
4327 if (compare_values (init, max) == -1)
4328 max = init;
4330 /* According to the loop information, the variable does not
4331 overflow. If we think it does, probably because of an
4332 overflow due to arithmetic on a different INF value,
4333 reset now. */
4334 if (is_negative_overflow_infinity (min)
4335 || compare_values (min, tmin) == -1)
4336 min = tmin;
4339 else
4341 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4342 if (compare_values (init, min) == 1)
4343 min = init;
4345 if (is_positive_overflow_infinity (max)
4346 || compare_values (tmax, max) == -1)
4347 max = tmax;
4350 else
4351 return;
4353 /* If we just created an invalid range with the minimum
4354 greater than the maximum, we fail conservatively.
4355 This should happen only in unreachable
4356 parts of code, or for invalid programs. */
4357 if (compare_values (min, max) == 1
4358 || (is_negative_overflow_infinity (min)
4359 && is_positive_overflow_infinity (max)))
4360 return;
4362 /* Even for valid range info, sometimes overflow flag will leak in.
4363 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4364 drop them except for +-overflow_infinity which still need special
4365 handling in vrp pass. */
4366 if (TREE_OVERFLOW_P (min)
4367 && ! is_negative_overflow_infinity (min))
4368 min = drop_tree_overflow (min);
4369 if (TREE_OVERFLOW_P (max)
4370 && ! is_positive_overflow_infinity (max))
4371 max = drop_tree_overflow (max);
4373 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4377 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4379 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4380 all the values in the ranges.
4382 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4384 - Return NULL_TREE if it is not always possible to determine the
4385 value of the comparison.
4387 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4388 overflow infinity was used in the test. */
4391 static tree
4392 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4393 bool *strict_overflow_p)
4395 /* VARYING or UNDEFINED ranges cannot be compared. */
4396 if (vr0->type == VR_VARYING
4397 || vr0->type == VR_UNDEFINED
4398 || vr1->type == VR_VARYING
4399 || vr1->type == VR_UNDEFINED)
4400 return NULL_TREE;
4402 /* Anti-ranges need to be handled separately. */
4403 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4405 /* If both are anti-ranges, then we cannot compute any
4406 comparison. */
4407 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4408 return NULL_TREE;
4410 /* These comparisons are never statically computable. */
4411 if (comp == GT_EXPR
4412 || comp == GE_EXPR
4413 || comp == LT_EXPR
4414 || comp == LE_EXPR)
4415 return NULL_TREE;
4417 /* Equality can be computed only between a range and an
4418 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4419 if (vr0->type == VR_RANGE)
4421 /* To simplify processing, make VR0 the anti-range. */
4422 value_range *tmp = vr0;
4423 vr0 = vr1;
4424 vr1 = tmp;
4427 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4429 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4430 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4431 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4433 return NULL_TREE;
4436 if (!usable_range_p (vr0, strict_overflow_p)
4437 || !usable_range_p (vr1, strict_overflow_p))
4438 return NULL_TREE;
4440 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4441 operands around and change the comparison code. */
4442 if (comp == GT_EXPR || comp == GE_EXPR)
4444 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4445 std::swap (vr0, vr1);
4448 if (comp == EQ_EXPR)
4450 /* Equality may only be computed if both ranges represent
4451 exactly one value. */
4452 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4453 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4455 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4456 strict_overflow_p);
4457 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4458 strict_overflow_p);
4459 if (cmp_min == 0 && cmp_max == 0)
4460 return boolean_true_node;
4461 else if (cmp_min != -2 && cmp_max != -2)
4462 return boolean_false_node;
4464 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4465 else if (compare_values_warnv (vr0->min, vr1->max,
4466 strict_overflow_p) == 1
4467 || compare_values_warnv (vr1->min, vr0->max,
4468 strict_overflow_p) == 1)
4469 return boolean_false_node;
4471 return NULL_TREE;
4473 else if (comp == NE_EXPR)
4475 int cmp1, cmp2;
4477 /* If VR0 is completely to the left or completely to the right
4478 of VR1, they are always different. Notice that we need to
4479 make sure that both comparisons yield similar results to
4480 avoid comparing values that cannot be compared at
4481 compile-time. */
4482 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4483 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4484 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4485 return boolean_true_node;
4487 /* If VR0 and VR1 represent a single value and are identical,
4488 return false. */
4489 else if (compare_values_warnv (vr0->min, vr0->max,
4490 strict_overflow_p) == 0
4491 && compare_values_warnv (vr1->min, vr1->max,
4492 strict_overflow_p) == 0
4493 && compare_values_warnv (vr0->min, vr1->min,
4494 strict_overflow_p) == 0
4495 && compare_values_warnv (vr0->max, vr1->max,
4496 strict_overflow_p) == 0)
4497 return boolean_false_node;
4499 /* Otherwise, they may or may not be different. */
4500 else
4501 return NULL_TREE;
4503 else if (comp == LT_EXPR || comp == LE_EXPR)
4505 int tst;
4507 /* If VR0 is to the left of VR1, return true. */
4508 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4509 if ((comp == LT_EXPR && tst == -1)
4510 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4512 if (overflow_infinity_range_p (vr0)
4513 || overflow_infinity_range_p (vr1))
4514 *strict_overflow_p = true;
4515 return boolean_true_node;
4518 /* If VR0 is to the right of VR1, return false. */
4519 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4520 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4521 || (comp == LE_EXPR && tst == 1))
4523 if (overflow_infinity_range_p (vr0)
4524 || overflow_infinity_range_p (vr1))
4525 *strict_overflow_p = true;
4526 return boolean_false_node;
4529 /* Otherwise, we don't know. */
4530 return NULL_TREE;
4533 gcc_unreachable ();
4537 /* Given a value range VR, a value VAL and a comparison code COMP, return
4538 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4539 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4540 always returns false. Return NULL_TREE if it is not always
4541 possible to determine the value of the comparison. Also set
4542 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4543 infinity was used in the test. */
4545 static tree
4546 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4547 bool *strict_overflow_p)
4549 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4550 return NULL_TREE;
4552 /* Anti-ranges need to be handled separately. */
4553 if (vr->type == VR_ANTI_RANGE)
4555 /* For anti-ranges, the only predicates that we can compute at
4556 compile time are equality and inequality. */
4557 if (comp == GT_EXPR
4558 || comp == GE_EXPR
4559 || comp == LT_EXPR
4560 || comp == LE_EXPR)
4561 return NULL_TREE;
4563 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4564 if (value_inside_range (val, vr->min, vr->max) == 1)
4565 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4567 return NULL_TREE;
4570 if (!usable_range_p (vr, strict_overflow_p))
4571 return NULL_TREE;
4573 if (comp == EQ_EXPR)
4575 /* EQ_EXPR may only be computed if VR represents exactly
4576 one value. */
4577 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4579 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4580 if (cmp == 0)
4581 return boolean_true_node;
4582 else if (cmp == -1 || cmp == 1 || cmp == 2)
4583 return boolean_false_node;
4585 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4586 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4587 return boolean_false_node;
4589 return NULL_TREE;
4591 else if (comp == NE_EXPR)
4593 /* If VAL is not inside VR, then they are always different. */
4594 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4595 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4596 return boolean_true_node;
4598 /* If VR represents exactly one value equal to VAL, then return
4599 false. */
4600 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4601 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4602 return boolean_false_node;
4604 /* Otherwise, they may or may not be different. */
4605 return NULL_TREE;
4607 else if (comp == LT_EXPR || comp == LE_EXPR)
4609 int tst;
4611 /* If VR is to the left of VAL, return true. */
4612 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4613 if ((comp == LT_EXPR && tst == -1)
4614 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4616 if (overflow_infinity_range_p (vr))
4617 *strict_overflow_p = true;
4618 return boolean_true_node;
4621 /* If VR is to the right of VAL, return false. */
4622 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4623 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4624 || (comp == LE_EXPR && tst == 1))
4626 if (overflow_infinity_range_p (vr))
4627 *strict_overflow_p = true;
4628 return boolean_false_node;
4631 /* Otherwise, we don't know. */
4632 return NULL_TREE;
4634 else if (comp == GT_EXPR || comp == GE_EXPR)
4636 int tst;
4638 /* If VR is to the right of VAL, return true. */
4639 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4640 if ((comp == GT_EXPR && tst == 1)
4641 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4643 if (overflow_infinity_range_p (vr))
4644 *strict_overflow_p = true;
4645 return boolean_true_node;
4648 /* If VR is to the left of VAL, return false. */
4649 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4650 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4651 || (comp == GE_EXPR && tst == -1))
4653 if (overflow_infinity_range_p (vr))
4654 *strict_overflow_p = true;
4655 return boolean_false_node;
4658 /* Otherwise, we don't know. */
4659 return NULL_TREE;
4662 gcc_unreachable ();
4666 /* Debugging dumps. */
4668 void dump_value_range (FILE *, const value_range *);
4669 void debug_value_range (value_range *);
4670 void dump_all_value_ranges (FILE *);
4671 void debug_all_value_ranges (void);
4672 void dump_vr_equiv (FILE *, bitmap);
4673 void debug_vr_equiv (bitmap);
4676 /* Dump value range VR to FILE. */
4678 void
4679 dump_value_range (FILE *file, const value_range *vr)
4681 if (vr == NULL)
4682 fprintf (file, "[]");
4683 else if (vr->type == VR_UNDEFINED)
4684 fprintf (file, "UNDEFINED");
4685 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4687 tree type = TREE_TYPE (vr->min);
4689 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4691 if (is_negative_overflow_infinity (vr->min))
4692 fprintf (file, "-INF(OVF)");
4693 else if (INTEGRAL_TYPE_P (type)
4694 && !TYPE_UNSIGNED (type)
4695 && vrp_val_is_min (vr->min))
4696 fprintf (file, "-INF");
4697 else
4698 print_generic_expr (file, vr->min, 0);
4700 fprintf (file, ", ");
4702 if (is_positive_overflow_infinity (vr->max))
4703 fprintf (file, "+INF(OVF)");
4704 else if (INTEGRAL_TYPE_P (type)
4705 && vrp_val_is_max (vr->max))
4706 fprintf (file, "+INF");
4707 else
4708 print_generic_expr (file, vr->max, 0);
4710 fprintf (file, "]");
4712 if (vr->equiv)
4714 bitmap_iterator bi;
4715 unsigned i, c = 0;
4717 fprintf (file, " EQUIVALENCES: { ");
4719 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4721 print_generic_expr (file, ssa_name (i), 0);
4722 fprintf (file, " ");
4723 c++;
4726 fprintf (file, "} (%u elements)", c);
4729 else if (vr->type == VR_VARYING)
4730 fprintf (file, "VARYING");
4731 else
4732 fprintf (file, "INVALID RANGE");
4736 /* Dump value range VR to stderr. */
4738 DEBUG_FUNCTION void
4739 debug_value_range (value_range *vr)
4741 dump_value_range (stderr, vr);
4742 fprintf (stderr, "\n");
4746 /* Dump value ranges of all SSA_NAMEs to FILE. */
4748 void
4749 dump_all_value_ranges (FILE *file)
4751 size_t i;
4753 for (i = 0; i < num_vr_values; i++)
4755 if (vr_value[i])
4757 print_generic_expr (file, ssa_name (i), 0);
4758 fprintf (file, ": ");
4759 dump_value_range (file, vr_value[i]);
4760 fprintf (file, "\n");
4764 fprintf (file, "\n");
4768 /* Dump all value ranges to stderr. */
4770 DEBUG_FUNCTION void
4771 debug_all_value_ranges (void)
4773 dump_all_value_ranges (stderr);
4777 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4778 create a new SSA name N and return the assertion assignment
4779 'N = ASSERT_EXPR <V, V OP W>'. */
4781 static gimple *
4782 build_assert_expr_for (tree cond, tree v)
4784 tree a;
4785 gassign *assertion;
4787 gcc_assert (TREE_CODE (v) == SSA_NAME
4788 && COMPARISON_CLASS_P (cond));
4790 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4791 assertion = gimple_build_assign (NULL_TREE, a);
4793 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4794 operand of the ASSERT_EXPR. Create it so the new name and the old one
4795 are registered in the replacement table so that we can fix the SSA web
4796 after adding all the ASSERT_EXPRs. */
4797 create_new_def_for (v, assertion, NULL);
4799 return assertion;
4803 /* Return false if EXPR is a predicate expression involving floating
4804 point values. */
4806 static inline bool
4807 fp_predicate (gimple *stmt)
4809 GIMPLE_CHECK (stmt, GIMPLE_COND);
4811 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4814 /* If the range of values taken by OP can be inferred after STMT executes,
4815 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4816 describes the inferred range. Return true if a range could be
4817 inferred. */
4819 static bool
4820 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4822 *val_p = NULL_TREE;
4823 *comp_code_p = ERROR_MARK;
4825 /* Do not attempt to infer anything in names that flow through
4826 abnormal edges. */
4827 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4828 return false;
4830 /* If STMT is the last statement of a basic block with no normal
4831 successors, there is no point inferring anything about any of its
4832 operands. We would not be able to find a proper insertion point
4833 for the assertion, anyway. */
4834 if (stmt_ends_bb_p (stmt))
4836 edge_iterator ei;
4837 edge e;
4839 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4840 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
4841 break;
4842 if (e == NULL)
4843 return false;
4846 if (infer_nonnull_range (stmt, op))
4848 *val_p = build_int_cst (TREE_TYPE (op), 0);
4849 *comp_code_p = NE_EXPR;
4850 return true;
4853 return false;
4857 void dump_asserts_for (FILE *, tree);
4858 void debug_asserts_for (tree);
4859 void dump_all_asserts (FILE *);
4860 void debug_all_asserts (void);
4862 /* Dump all the registered assertions for NAME to FILE. */
4864 void
4865 dump_asserts_for (FILE *file, tree name)
4867 assert_locus *loc;
4869 fprintf (file, "Assertions to be inserted for ");
4870 print_generic_expr (file, name, 0);
4871 fprintf (file, "\n");
4873 loc = asserts_for[SSA_NAME_VERSION (name)];
4874 while (loc)
4876 fprintf (file, "\t");
4877 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4878 fprintf (file, "\n\tBB #%d", loc->bb->index);
4879 if (loc->e)
4881 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4882 loc->e->dest->index);
4883 dump_edge_info (file, loc->e, dump_flags, 0);
4885 fprintf (file, "\n\tPREDICATE: ");
4886 print_generic_expr (file, loc->expr, 0);
4887 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4888 print_generic_expr (file, loc->val, 0);
4889 fprintf (file, "\n\n");
4890 loc = loc->next;
4893 fprintf (file, "\n");
4897 /* Dump all the registered assertions for NAME to stderr. */
4899 DEBUG_FUNCTION void
4900 debug_asserts_for (tree name)
4902 dump_asserts_for (stderr, name);
4906 /* Dump all the registered assertions for all the names to FILE. */
4908 void
4909 dump_all_asserts (FILE *file)
4911 unsigned i;
4912 bitmap_iterator bi;
4914 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4915 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4916 dump_asserts_for (file, ssa_name (i));
4917 fprintf (file, "\n");
4921 /* Dump all the registered assertions for all the names to stderr. */
4923 DEBUG_FUNCTION void
4924 debug_all_asserts (void)
4926 dump_all_asserts (stderr);
4930 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4931 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4932 E->DEST, then register this location as a possible insertion point
4933 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4935 BB, E and SI provide the exact insertion point for the new
4936 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4937 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4938 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4939 must not be NULL. */
4941 static void
4942 register_new_assert_for (tree name, tree expr,
4943 enum tree_code comp_code,
4944 tree val,
4945 basic_block bb,
4946 edge e,
4947 gimple_stmt_iterator si)
4949 assert_locus *n, *loc, *last_loc;
4950 basic_block dest_bb;
4952 gcc_checking_assert (bb == NULL || e == NULL);
4954 if (e == NULL)
4955 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4956 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4958 /* Never build an assert comparing against an integer constant with
4959 TREE_OVERFLOW set. This confuses our undefined overflow warning
4960 machinery. */
4961 if (TREE_OVERFLOW_P (val))
4962 val = drop_tree_overflow (val);
4964 /* The new assertion A will be inserted at BB or E. We need to
4965 determine if the new location is dominated by a previously
4966 registered location for A. If we are doing an edge insertion,
4967 assume that A will be inserted at E->DEST. Note that this is not
4968 necessarily true.
4970 If E is a critical edge, it will be split. But even if E is
4971 split, the new block will dominate the same set of blocks that
4972 E->DEST dominates.
4974 The reverse, however, is not true, blocks dominated by E->DEST
4975 will not be dominated by the new block created to split E. So,
4976 if the insertion location is on a critical edge, we will not use
4977 the new location to move another assertion previously registered
4978 at a block dominated by E->DEST. */
4979 dest_bb = (bb) ? bb : e->dest;
4981 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4982 VAL at a block dominating DEST_BB, then we don't need to insert a new
4983 one. Similarly, if the same assertion already exists at a block
4984 dominated by DEST_BB and the new location is not on a critical
4985 edge, then update the existing location for the assertion (i.e.,
4986 move the assertion up in the dominance tree).
4988 Note, this is implemented as a simple linked list because there
4989 should not be more than a handful of assertions registered per
4990 name. If this becomes a performance problem, a table hashed by
4991 COMP_CODE and VAL could be implemented. */
4992 loc = asserts_for[SSA_NAME_VERSION (name)];
4993 last_loc = loc;
4994 while (loc)
4996 if (loc->comp_code == comp_code
4997 && (loc->val == val
4998 || operand_equal_p (loc->val, val, 0))
4999 && (loc->expr == expr
5000 || operand_equal_p (loc->expr, expr, 0)))
5002 /* If E is not a critical edge and DEST_BB
5003 dominates the existing location for the assertion, move
5004 the assertion up in the dominance tree by updating its
5005 location information. */
5006 if ((e == NULL || !EDGE_CRITICAL_P (e))
5007 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5009 loc->bb = dest_bb;
5010 loc->e = e;
5011 loc->si = si;
5012 return;
5016 /* Update the last node of the list and move to the next one. */
5017 last_loc = loc;
5018 loc = loc->next;
5021 /* If we didn't find an assertion already registered for
5022 NAME COMP_CODE VAL, add a new one at the end of the list of
5023 assertions associated with NAME. */
5024 n = XNEW (struct assert_locus);
5025 n->bb = dest_bb;
5026 n->e = e;
5027 n->si = si;
5028 n->comp_code = comp_code;
5029 n->val = val;
5030 n->expr = expr;
5031 n->next = NULL;
5033 if (last_loc)
5034 last_loc->next = n;
5035 else
5036 asserts_for[SSA_NAME_VERSION (name)] = n;
5038 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5041 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5042 Extract a suitable test code and value and store them into *CODE_P and
5043 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5045 If no extraction was possible, return FALSE, otherwise return TRUE.
5047 If INVERT is true, then we invert the result stored into *CODE_P. */
5049 static bool
5050 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5051 tree cond_op0, tree cond_op1,
5052 bool invert, enum tree_code *code_p,
5053 tree *val_p)
5055 enum tree_code comp_code;
5056 tree val;
5058 /* Otherwise, we have a comparison of the form NAME COMP VAL
5059 or VAL COMP NAME. */
5060 if (name == cond_op1)
5062 /* If the predicate is of the form VAL COMP NAME, flip
5063 COMP around because we need to register NAME as the
5064 first operand in the predicate. */
5065 comp_code = swap_tree_comparison (cond_code);
5066 val = cond_op0;
5068 else if (name == cond_op0)
5070 /* The comparison is of the form NAME COMP VAL, so the
5071 comparison code remains unchanged. */
5072 comp_code = cond_code;
5073 val = cond_op1;
5075 else
5076 gcc_unreachable ();
5078 /* Invert the comparison code as necessary. */
5079 if (invert)
5080 comp_code = invert_tree_comparison (comp_code, 0);
5082 /* VRP only handles integral and pointer types. */
5083 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5084 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5085 return false;
5087 /* Do not register always-false predicates.
5088 FIXME: this works around a limitation in fold() when dealing with
5089 enumerations. Given 'enum { N1, N2 } x;', fold will not
5090 fold 'if (x > N2)' to 'if (0)'. */
5091 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5092 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5094 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5095 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5097 if (comp_code == GT_EXPR
5098 && (!max
5099 || compare_values (val, max) == 0))
5100 return false;
5102 if (comp_code == LT_EXPR
5103 && (!min
5104 || compare_values (val, min) == 0))
5105 return false;
5107 *code_p = comp_code;
5108 *val_p = val;
5109 return true;
5112 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5113 (otherwise return VAL). VAL and MASK must be zero-extended for
5114 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5115 (to transform signed values into unsigned) and at the end xor
5116 SGNBIT back. */
5118 static wide_int
5119 masked_increment (const wide_int &val_in, const wide_int &mask,
5120 const wide_int &sgnbit, unsigned int prec)
5122 wide_int bit = wi::one (prec), res;
5123 unsigned int i;
5125 wide_int val = val_in ^ sgnbit;
5126 for (i = 0; i < prec; i++, bit += bit)
5128 res = mask;
5129 if ((res & bit) == 0)
5130 continue;
5131 res = bit - 1;
5132 res = (val + bit).and_not (res);
5133 res &= mask;
5134 if (wi::gtu_p (res, val))
5135 return res ^ sgnbit;
5137 return val ^ sgnbit;
5140 /* Try to register an edge assertion for SSA name NAME on edge E for
5141 the condition COND contributing to the conditional jump pointed to by BSI.
5142 Invert the condition COND if INVERT is true. */
5144 static void
5145 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5146 enum tree_code cond_code,
5147 tree cond_op0, tree cond_op1, bool invert)
5149 tree val;
5150 enum tree_code comp_code;
5152 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5153 cond_op0,
5154 cond_op1,
5155 invert, &comp_code, &val))
5156 return;
5158 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5159 reachable from E. */
5160 if (live_on_edge (e, name))
5161 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5163 /* In the case of NAME <= CST and NAME being defined as
5164 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5165 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5166 This catches range and anti-range tests. */
5167 if ((comp_code == LE_EXPR
5168 || comp_code == GT_EXPR)
5169 && TREE_CODE (val) == INTEGER_CST
5170 && TYPE_UNSIGNED (TREE_TYPE (val)))
5172 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5173 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5175 /* Extract CST2 from the (optional) addition. */
5176 if (is_gimple_assign (def_stmt)
5177 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5179 name2 = gimple_assign_rhs1 (def_stmt);
5180 cst2 = gimple_assign_rhs2 (def_stmt);
5181 if (TREE_CODE (name2) == SSA_NAME
5182 && TREE_CODE (cst2) == INTEGER_CST)
5183 def_stmt = SSA_NAME_DEF_STMT (name2);
5186 /* Extract NAME2 from the (optional) sign-changing cast. */
5187 if (gimple_assign_cast_p (def_stmt))
5189 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5190 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5191 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5192 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5193 name3 = gimple_assign_rhs1 (def_stmt);
5196 /* If name3 is used later, create an ASSERT_EXPR for it. */
5197 if (name3 != NULL_TREE
5198 && TREE_CODE (name3) == SSA_NAME
5199 && (cst2 == NULL_TREE
5200 || TREE_CODE (cst2) == INTEGER_CST)
5201 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5202 && live_on_edge (e, name3))
5204 tree tmp;
5206 /* Build an expression for the range test. */
5207 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5208 if (cst2 != NULL_TREE)
5209 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5211 if (dump_file)
5213 fprintf (dump_file, "Adding assert for ");
5214 print_generic_expr (dump_file, name3, 0);
5215 fprintf (dump_file, " from ");
5216 print_generic_expr (dump_file, tmp, 0);
5217 fprintf (dump_file, "\n");
5220 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5223 /* If name2 is used later, create an ASSERT_EXPR for it. */
5224 if (name2 != NULL_TREE
5225 && TREE_CODE (name2) == SSA_NAME
5226 && TREE_CODE (cst2) == INTEGER_CST
5227 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5228 && live_on_edge (e, name2))
5230 tree tmp;
5232 /* Build an expression for the range test. */
5233 tmp = name2;
5234 if (TREE_TYPE (name) != TREE_TYPE (name2))
5235 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5236 if (cst2 != NULL_TREE)
5237 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5239 if (dump_file)
5241 fprintf (dump_file, "Adding assert for ");
5242 print_generic_expr (dump_file, name2, 0);
5243 fprintf (dump_file, " from ");
5244 print_generic_expr (dump_file, tmp, 0);
5245 fprintf (dump_file, "\n");
5248 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5252 /* In the case of post-in/decrement tests like if (i++) ... and uses
5253 of the in/decremented value on the edge the extra name we want to
5254 assert for is not on the def chain of the name compared. Instead
5255 it is in the set of use stmts.
5256 Similar cases happen for conversions that were simplified through
5257 fold_{sign_changed,widened}_comparison. */
5258 if ((comp_code == NE_EXPR
5259 || comp_code == EQ_EXPR)
5260 && TREE_CODE (val) == INTEGER_CST)
5262 imm_use_iterator ui;
5263 gimple *use_stmt;
5264 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5266 if (!is_gimple_assign (use_stmt))
5267 continue;
5269 /* Cut off to use-stmts that are dominating the predecessor. */
5270 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5271 continue;
5273 tree name2 = gimple_assign_lhs (use_stmt);
5274 if (TREE_CODE (name2) != SSA_NAME
5275 || !live_on_edge (e, name2))
5276 continue;
5278 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5279 tree cst;
5280 if (code == PLUS_EXPR
5281 || code == MINUS_EXPR)
5283 cst = gimple_assign_rhs2 (use_stmt);
5284 if (TREE_CODE (cst) != INTEGER_CST)
5285 continue;
5286 cst = int_const_binop (code, val, cst);
5288 else if (CONVERT_EXPR_CODE_P (code))
5290 /* For truncating conversions we cannot record
5291 an inequality. */
5292 if (comp_code == NE_EXPR
5293 && (TYPE_PRECISION (TREE_TYPE (name2))
5294 < TYPE_PRECISION (TREE_TYPE (name))))
5295 continue;
5296 cst = fold_convert (TREE_TYPE (name2), val);
5298 else
5299 continue;
5301 if (TREE_OVERFLOW_P (cst))
5302 cst = drop_tree_overflow (cst);
5303 register_new_assert_for (name2, name2, comp_code, cst,
5304 NULL, e, bsi);
5308 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5309 && TREE_CODE (val) == INTEGER_CST)
5311 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5312 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5313 tree val2 = NULL_TREE;
5314 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5315 wide_int mask = wi::zero (prec);
5316 unsigned int nprec = prec;
5317 enum tree_code rhs_code = ERROR_MARK;
5319 if (is_gimple_assign (def_stmt))
5320 rhs_code = gimple_assign_rhs_code (def_stmt);
5322 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5323 assert that A != CST1 -+ CST2. */
5324 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5325 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5327 tree op0 = gimple_assign_rhs1 (def_stmt);
5328 tree op1 = gimple_assign_rhs2 (def_stmt);
5329 if (TREE_CODE (op0) == SSA_NAME
5330 && TREE_CODE (op1) == INTEGER_CST
5331 && live_on_edge (e, op0))
5333 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5334 ? MINUS_EXPR : PLUS_EXPR);
5335 op1 = int_const_binop (reverse_op, val, op1);
5336 if (TREE_OVERFLOW (op1))
5337 op1 = drop_tree_overflow (op1);
5338 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5342 /* Add asserts for NAME cmp CST and NAME being defined
5343 as NAME = (int) NAME2. */
5344 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5345 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5346 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5347 && gimple_assign_cast_p (def_stmt))
5349 name2 = gimple_assign_rhs1 (def_stmt);
5350 if (CONVERT_EXPR_CODE_P (rhs_code)
5351 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5352 && TYPE_UNSIGNED (TREE_TYPE (name2))
5353 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5354 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5355 || !tree_int_cst_equal (val,
5356 TYPE_MIN_VALUE (TREE_TYPE (val))))
5357 && live_on_edge (e, name2))
5359 tree tmp, cst;
5360 enum tree_code new_comp_code = comp_code;
5362 cst = fold_convert (TREE_TYPE (name2),
5363 TYPE_MIN_VALUE (TREE_TYPE (val)));
5364 /* Build an expression for the range test. */
5365 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5366 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5367 fold_convert (TREE_TYPE (name2), val));
5368 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5370 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5371 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5372 build_int_cst (TREE_TYPE (name2), 1));
5375 if (dump_file)
5377 fprintf (dump_file, "Adding assert for ");
5378 print_generic_expr (dump_file, name2, 0);
5379 fprintf (dump_file, " from ");
5380 print_generic_expr (dump_file, tmp, 0);
5381 fprintf (dump_file, "\n");
5384 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5385 e, bsi);
5389 /* Add asserts for NAME cmp CST and NAME being defined as
5390 NAME = NAME2 >> CST2.
5392 Extract CST2 from the right shift. */
5393 if (rhs_code == RSHIFT_EXPR)
5395 name2 = gimple_assign_rhs1 (def_stmt);
5396 cst2 = gimple_assign_rhs2 (def_stmt);
5397 if (TREE_CODE (name2) == SSA_NAME
5398 && tree_fits_uhwi_p (cst2)
5399 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5400 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5401 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5402 && live_on_edge (e, name2))
5404 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5405 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5408 if (val2 != NULL_TREE
5409 && TREE_CODE (val2) == INTEGER_CST
5410 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5411 TREE_TYPE (val),
5412 val2, cst2), val))
5414 enum tree_code new_comp_code = comp_code;
5415 tree tmp, new_val;
5417 tmp = name2;
5418 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5420 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5422 tree type = build_nonstandard_integer_type (prec, 1);
5423 tmp = build1 (NOP_EXPR, type, name2);
5424 val2 = fold_convert (type, val2);
5426 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5427 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5428 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5430 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5432 wide_int minval
5433 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5434 new_val = val2;
5435 if (minval == new_val)
5436 new_val = NULL_TREE;
5438 else
5440 wide_int maxval
5441 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5442 mask |= val2;
5443 if (mask == maxval)
5444 new_val = NULL_TREE;
5445 else
5446 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5449 if (new_val)
5451 if (dump_file)
5453 fprintf (dump_file, "Adding assert for ");
5454 print_generic_expr (dump_file, name2, 0);
5455 fprintf (dump_file, " from ");
5456 print_generic_expr (dump_file, tmp, 0);
5457 fprintf (dump_file, "\n");
5460 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5461 NULL, e, bsi);
5465 /* Add asserts for NAME cmp CST and NAME being defined as
5466 NAME = NAME2 & CST2.
5468 Extract CST2 from the and.
5470 Also handle
5471 NAME = (unsigned) NAME2;
5472 casts where NAME's type is unsigned and has smaller precision
5473 than NAME2's type as if it was NAME = NAME2 & MASK. */
5474 names[0] = NULL_TREE;
5475 names[1] = NULL_TREE;
5476 cst2 = NULL_TREE;
5477 if (rhs_code == BIT_AND_EXPR
5478 || (CONVERT_EXPR_CODE_P (rhs_code)
5479 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5480 && TYPE_UNSIGNED (TREE_TYPE (val))
5481 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5482 > prec))
5484 name2 = gimple_assign_rhs1 (def_stmt);
5485 if (rhs_code == BIT_AND_EXPR)
5486 cst2 = gimple_assign_rhs2 (def_stmt);
5487 else
5489 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5490 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5492 if (TREE_CODE (name2) == SSA_NAME
5493 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5494 && TREE_CODE (cst2) == INTEGER_CST
5495 && !integer_zerop (cst2)
5496 && (nprec > 1
5497 || TYPE_UNSIGNED (TREE_TYPE (val))))
5499 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5500 if (gimple_assign_cast_p (def_stmt2))
5502 names[1] = gimple_assign_rhs1 (def_stmt2);
5503 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5504 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5505 || (TYPE_PRECISION (TREE_TYPE (name2))
5506 != TYPE_PRECISION (TREE_TYPE (names[1])))
5507 || !live_on_edge (e, names[1]))
5508 names[1] = NULL_TREE;
5510 if (live_on_edge (e, name2))
5511 names[0] = name2;
5514 if (names[0] || names[1])
5516 wide_int minv, maxv, valv, cst2v;
5517 wide_int tem, sgnbit;
5518 bool valid_p = false, valn, cst2n;
5519 enum tree_code ccode = comp_code;
5521 valv = wide_int::from (val, nprec, UNSIGNED);
5522 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5523 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5524 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5525 /* If CST2 doesn't have most significant bit set,
5526 but VAL is negative, we have comparison like
5527 if ((x & 0x123) > -4) (always true). Just give up. */
5528 if (!cst2n && valn)
5529 ccode = ERROR_MARK;
5530 if (cst2n)
5531 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5532 else
5533 sgnbit = wi::zero (nprec);
5534 minv = valv & cst2v;
5535 switch (ccode)
5537 case EQ_EXPR:
5538 /* Minimum unsigned value for equality is VAL & CST2
5539 (should be equal to VAL, otherwise we probably should
5540 have folded the comparison into false) and
5541 maximum unsigned value is VAL | ~CST2. */
5542 maxv = valv | ~cst2v;
5543 valid_p = true;
5544 break;
5546 case NE_EXPR:
5547 tem = valv | ~cst2v;
5548 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5549 if (valv == 0)
5551 cst2n = false;
5552 sgnbit = wi::zero (nprec);
5553 goto gt_expr;
5555 /* If (VAL | ~CST2) is all ones, handle it as
5556 (X & CST2) < VAL. */
5557 if (tem == -1)
5559 cst2n = false;
5560 valn = false;
5561 sgnbit = wi::zero (nprec);
5562 goto lt_expr;
5564 if (!cst2n && wi::neg_p (cst2v))
5565 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5566 if (sgnbit != 0)
5568 if (valv == sgnbit)
5570 cst2n = true;
5571 valn = true;
5572 goto gt_expr;
5574 if (tem == wi::mask (nprec - 1, false, nprec))
5576 cst2n = true;
5577 goto lt_expr;
5579 if (!cst2n)
5580 sgnbit = wi::zero (nprec);
5582 break;
5584 case GE_EXPR:
5585 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5586 is VAL and maximum unsigned value is ~0. For signed
5587 comparison, if CST2 doesn't have most significant bit
5588 set, handle it similarly. If CST2 has MSB set,
5589 the minimum is the same, and maximum is ~0U/2. */
5590 if (minv != valv)
5592 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5593 VAL. */
5594 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5595 if (minv == valv)
5596 break;
5598 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5599 valid_p = true;
5600 break;
5602 case GT_EXPR:
5603 gt_expr:
5604 /* Find out smallest MINV where MINV > VAL
5605 && (MINV & CST2) == MINV, if any. If VAL is signed and
5606 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5607 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5608 if (minv == valv)
5609 break;
5610 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5611 valid_p = true;
5612 break;
5614 case LE_EXPR:
5615 /* Minimum unsigned value for <= is 0 and maximum
5616 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5617 Otherwise, find smallest VAL2 where VAL2 > VAL
5618 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5619 as maximum.
5620 For signed comparison, if CST2 doesn't have most
5621 significant bit set, handle it similarly. If CST2 has
5622 MSB set, the maximum is the same and minimum is INT_MIN. */
5623 if (minv == valv)
5624 maxv = valv;
5625 else
5627 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5628 if (maxv == valv)
5629 break;
5630 maxv -= 1;
5632 maxv |= ~cst2v;
5633 minv = sgnbit;
5634 valid_p = true;
5635 break;
5637 case LT_EXPR:
5638 lt_expr:
5639 /* Minimum unsigned value for < is 0 and maximum
5640 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5641 Otherwise, find smallest VAL2 where VAL2 > VAL
5642 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5643 as maximum.
5644 For signed comparison, if CST2 doesn't have most
5645 significant bit set, handle it similarly. If CST2 has
5646 MSB set, the maximum is the same and minimum is INT_MIN. */
5647 if (minv == valv)
5649 if (valv == sgnbit)
5650 break;
5651 maxv = valv;
5653 else
5655 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5656 if (maxv == valv)
5657 break;
5659 maxv -= 1;
5660 maxv |= ~cst2v;
5661 minv = sgnbit;
5662 valid_p = true;
5663 break;
5665 default:
5666 break;
5668 if (valid_p
5669 && (maxv - minv) != -1)
5671 tree tmp, new_val, type;
5672 int i;
5674 for (i = 0; i < 2; i++)
5675 if (names[i])
5677 wide_int maxv2 = maxv;
5678 tmp = names[i];
5679 type = TREE_TYPE (names[i]);
5680 if (!TYPE_UNSIGNED (type))
5682 type = build_nonstandard_integer_type (nprec, 1);
5683 tmp = build1 (NOP_EXPR, type, names[i]);
5685 if (minv != 0)
5687 tmp = build2 (PLUS_EXPR, type, tmp,
5688 wide_int_to_tree (type, -minv));
5689 maxv2 = maxv - minv;
5691 new_val = wide_int_to_tree (type, maxv2);
5693 if (dump_file)
5695 fprintf (dump_file, "Adding assert for ");
5696 print_generic_expr (dump_file, names[i], 0);
5697 fprintf (dump_file, " from ");
5698 print_generic_expr (dump_file, tmp, 0);
5699 fprintf (dump_file, "\n");
5702 register_new_assert_for (names[i], tmp, LE_EXPR,
5703 new_val, NULL, e, bsi);
5710 /* OP is an operand of a truth value expression which is known to have
5711 a particular value. Register any asserts for OP and for any
5712 operands in OP's defining statement.
5714 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5715 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5717 static void
5718 register_edge_assert_for_1 (tree op, enum tree_code code,
5719 edge e, gimple_stmt_iterator bsi)
5721 gimple *op_def;
5722 tree val;
5723 enum tree_code rhs_code;
5725 /* We only care about SSA_NAMEs. */
5726 if (TREE_CODE (op) != SSA_NAME)
5727 return;
5729 /* We know that OP will have a zero or nonzero value. If OP is used
5730 more than once go ahead and register an assert for OP. */
5731 if (live_on_edge (e, op))
5733 val = build_int_cst (TREE_TYPE (op), 0);
5734 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5737 /* Now look at how OP is set. If it's set from a comparison,
5738 a truth operation or some bit operations, then we may be able
5739 to register information about the operands of that assignment. */
5740 op_def = SSA_NAME_DEF_STMT (op);
5741 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5742 return;
5744 rhs_code = gimple_assign_rhs_code (op_def);
5746 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5748 bool invert = (code == EQ_EXPR ? true : false);
5749 tree op0 = gimple_assign_rhs1 (op_def);
5750 tree op1 = gimple_assign_rhs2 (op_def);
5752 if (TREE_CODE (op0) == SSA_NAME)
5753 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5754 if (TREE_CODE (op1) == SSA_NAME)
5755 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5757 else if ((code == NE_EXPR
5758 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5759 || (code == EQ_EXPR
5760 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5762 /* Recurse on each operand. */
5763 tree op0 = gimple_assign_rhs1 (op_def);
5764 tree op1 = gimple_assign_rhs2 (op_def);
5765 if (TREE_CODE (op0) == SSA_NAME
5766 && has_single_use (op0))
5767 register_edge_assert_for_1 (op0, code, e, bsi);
5768 if (TREE_CODE (op1) == SSA_NAME
5769 && has_single_use (op1))
5770 register_edge_assert_for_1 (op1, code, e, bsi);
5772 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5773 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5775 /* Recurse, flipping CODE. */
5776 code = invert_tree_comparison (code, false);
5777 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5779 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5781 /* Recurse through the copy. */
5782 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5784 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5786 /* Recurse through the type conversion, unless it is a narrowing
5787 conversion or conversion from non-integral type. */
5788 tree rhs = gimple_assign_rhs1 (op_def);
5789 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5790 && (TYPE_PRECISION (TREE_TYPE (rhs))
5791 <= TYPE_PRECISION (TREE_TYPE (op))))
5792 register_edge_assert_for_1 (rhs, code, e, bsi);
5796 /* Try to register an edge assertion for SSA name NAME on edge E for
5797 the condition COND contributing to the conditional jump pointed to by
5798 SI. */
5800 static void
5801 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5802 enum tree_code cond_code, tree cond_op0,
5803 tree cond_op1)
5805 tree val;
5806 enum tree_code comp_code;
5807 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5809 /* Do not attempt to infer anything in names that flow through
5810 abnormal edges. */
5811 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5812 return;
5814 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5815 cond_op0, cond_op1,
5816 is_else_edge,
5817 &comp_code, &val))
5818 return;
5820 /* Register ASSERT_EXPRs for name. */
5821 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5822 cond_op1, is_else_edge);
5825 /* If COND is effectively an equality test of an SSA_NAME against
5826 the value zero or one, then we may be able to assert values
5827 for SSA_NAMEs which flow into COND. */
5829 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5830 statement of NAME we can assert both operands of the BIT_AND_EXPR
5831 have nonzero value. */
5832 if (((comp_code == EQ_EXPR && integer_onep (val))
5833 || (comp_code == NE_EXPR && integer_zerop (val))))
5835 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5837 if (is_gimple_assign (def_stmt)
5838 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5840 tree op0 = gimple_assign_rhs1 (def_stmt);
5841 tree op1 = gimple_assign_rhs2 (def_stmt);
5842 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5843 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5847 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5848 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5849 have zero value. */
5850 if (((comp_code == EQ_EXPR && integer_zerop (val))
5851 || (comp_code == NE_EXPR && integer_onep (val))))
5853 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5855 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5856 necessarily zero value, or if type-precision is one. */
5857 if (is_gimple_assign (def_stmt)
5858 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5859 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5860 || comp_code == EQ_EXPR)))
5862 tree op0 = gimple_assign_rhs1 (def_stmt);
5863 tree op1 = gimple_assign_rhs2 (def_stmt);
5864 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5865 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5871 /* Determine whether the outgoing edges of BB should receive an
5872 ASSERT_EXPR for each of the operands of BB's LAST statement.
5873 The last statement of BB must be a COND_EXPR.
5875 If any of the sub-graphs rooted at BB have an interesting use of
5876 the predicate operands, an assert location node is added to the
5877 list of assertions for the corresponding operands. */
5879 static void
5880 find_conditional_asserts (basic_block bb, gcond *last)
5882 gimple_stmt_iterator bsi;
5883 tree op;
5884 edge_iterator ei;
5885 edge e;
5886 ssa_op_iter iter;
5888 bsi = gsi_for_stmt (last);
5890 /* Look for uses of the operands in each of the sub-graphs
5891 rooted at BB. We need to check each of the outgoing edges
5892 separately, so that we know what kind of ASSERT_EXPR to
5893 insert. */
5894 FOR_EACH_EDGE (e, ei, bb->succs)
5896 if (e->dest == bb)
5897 continue;
5899 /* Register the necessary assertions for each operand in the
5900 conditional predicate. */
5901 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5902 register_edge_assert_for (op, e, bsi,
5903 gimple_cond_code (last),
5904 gimple_cond_lhs (last),
5905 gimple_cond_rhs (last));
5909 struct case_info
5911 tree expr;
5912 basic_block bb;
5915 /* Compare two case labels sorting first by the destination bb index
5916 and then by the case value. */
5918 static int
5919 compare_case_labels (const void *p1, const void *p2)
5921 const struct case_info *ci1 = (const struct case_info *) p1;
5922 const struct case_info *ci2 = (const struct case_info *) p2;
5923 int idx1 = ci1->bb->index;
5924 int idx2 = ci2->bb->index;
5926 if (idx1 < idx2)
5927 return -1;
5928 else if (idx1 == idx2)
5930 /* Make sure the default label is first in a group. */
5931 if (!CASE_LOW (ci1->expr))
5932 return -1;
5933 else if (!CASE_LOW (ci2->expr))
5934 return 1;
5935 else
5936 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5937 CASE_LOW (ci2->expr));
5939 else
5940 return 1;
5943 /* Determine whether the outgoing edges of BB should receive an
5944 ASSERT_EXPR for each of the operands of BB's LAST statement.
5945 The last statement of BB must be a SWITCH_EXPR.
5947 If any of the sub-graphs rooted at BB have an interesting use of
5948 the predicate operands, an assert location node is added to the
5949 list of assertions for the corresponding operands. */
5951 static void
5952 find_switch_asserts (basic_block bb, gswitch *last)
5954 gimple_stmt_iterator bsi;
5955 tree op;
5956 edge e;
5957 struct case_info *ci;
5958 size_t n = gimple_switch_num_labels (last);
5959 #if GCC_VERSION >= 4000
5960 unsigned int idx;
5961 #else
5962 /* Work around GCC 3.4 bug (PR 37086). */
5963 volatile unsigned int idx;
5964 #endif
5966 bsi = gsi_for_stmt (last);
5967 op = gimple_switch_index (last);
5968 if (TREE_CODE (op) != SSA_NAME)
5969 return;
5971 /* Build a vector of case labels sorted by destination label. */
5972 ci = XNEWVEC (struct case_info, n);
5973 for (idx = 0; idx < n; ++idx)
5975 ci[idx].expr = gimple_switch_label (last, idx);
5976 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5978 edge default_edge = find_edge (bb, ci[0].bb);
5979 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5981 for (idx = 0; idx < n; ++idx)
5983 tree min, max;
5984 tree cl = ci[idx].expr;
5985 basic_block cbb = ci[idx].bb;
5987 min = CASE_LOW (cl);
5988 max = CASE_HIGH (cl);
5990 /* If there are multiple case labels with the same destination
5991 we need to combine them to a single value range for the edge. */
5992 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5994 /* Skip labels until the last of the group. */
5995 do {
5996 ++idx;
5997 } while (idx < n && cbb == ci[idx].bb);
5998 --idx;
6000 /* Pick up the maximum of the case label range. */
6001 if (CASE_HIGH (ci[idx].expr))
6002 max = CASE_HIGH (ci[idx].expr);
6003 else
6004 max = CASE_LOW (ci[idx].expr);
6007 /* Can't extract a useful assertion out of a range that includes the
6008 default label. */
6009 if (min == NULL_TREE)
6010 continue;
6012 /* Find the edge to register the assert expr on. */
6013 e = find_edge (bb, cbb);
6015 /* Register the necessary assertions for the operand in the
6016 SWITCH_EXPR. */
6017 register_edge_assert_for (op, e, bsi,
6018 max ? GE_EXPR : EQ_EXPR,
6019 op, fold_convert (TREE_TYPE (op), min));
6020 if (max)
6021 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6022 fold_convert (TREE_TYPE (op), max));
6025 XDELETEVEC (ci);
6027 if (!live_on_edge (default_edge, op))
6028 return;
6030 /* Now register along the default label assertions that correspond to the
6031 anti-range of each label. */
6032 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
6033 for (idx = 1; idx < n; idx++)
6035 tree min, max;
6036 tree cl = gimple_switch_label (last, idx);
6038 min = CASE_LOW (cl);
6039 max = CASE_HIGH (cl);
6041 /* Combine contiguous case ranges to reduce the number of assertions
6042 to insert. */
6043 for (idx = idx + 1; idx < n; idx++)
6045 tree next_min, next_max;
6046 tree next_cl = gimple_switch_label (last, idx);
6048 next_min = CASE_LOW (next_cl);
6049 next_max = CASE_HIGH (next_cl);
6051 wide_int difference = wi::sub (next_min, max ? max : min);
6052 if (wi::eq_p (difference, 1))
6053 max = next_max ? next_max : next_min;
6054 else
6055 break;
6057 idx--;
6059 if (max == NULL_TREE)
6061 /* Register the assertion OP != MIN. */
6062 min = fold_convert (TREE_TYPE (op), min);
6063 register_edge_assert_for (op, default_edge, bsi, NE_EXPR, op, min);
6065 else
6067 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6068 which will give OP the anti-range ~[MIN,MAX]. */
6069 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
6070 min = fold_convert (TREE_TYPE (uop), min);
6071 max = fold_convert (TREE_TYPE (uop), max);
6073 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
6074 tree rhs = int_const_binop (MINUS_EXPR, max, min);
6075 register_new_assert_for (op, lhs, GT_EXPR, rhs,
6076 NULL, default_edge, bsi);
6079 if (--insertion_limit == 0)
6080 break;
6085 /* Traverse all the statements in block BB looking for statements that
6086 may generate useful assertions for the SSA names in their operand.
6087 If a statement produces a useful assertion A for name N_i, then the
6088 list of assertions already generated for N_i is scanned to
6089 determine if A is actually needed.
6091 If N_i already had the assertion A at a location dominating the
6092 current location, then nothing needs to be done. Otherwise, the
6093 new location for A is recorded instead.
6095 1- For every statement S in BB, all the variables used by S are
6096 added to bitmap FOUND_IN_SUBGRAPH.
6098 2- If statement S uses an operand N in a way that exposes a known
6099 value range for N, then if N was not already generated by an
6100 ASSERT_EXPR, create a new assert location for N. For instance,
6101 if N is a pointer and the statement dereferences it, we can
6102 assume that N is not NULL.
6104 3- COND_EXPRs are a special case of #2. We can derive range
6105 information from the predicate but need to insert different
6106 ASSERT_EXPRs for each of the sub-graphs rooted at the
6107 conditional block. If the last statement of BB is a conditional
6108 expression of the form 'X op Y', then
6110 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6112 b) If the conditional is the only entry point to the sub-graph
6113 corresponding to the THEN_CLAUSE, recurse into it. On
6114 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6115 an ASSERT_EXPR is added for the corresponding variable.
6117 c) Repeat step (b) on the ELSE_CLAUSE.
6119 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6121 For instance,
6123 if (a == 9)
6124 b = a;
6125 else
6126 b = c + 1;
6128 In this case, an assertion on the THEN clause is useful to
6129 determine that 'a' is always 9 on that edge. However, an assertion
6130 on the ELSE clause would be unnecessary.
6132 4- If BB does not end in a conditional expression, then we recurse
6133 into BB's dominator children.
6135 At the end of the recursive traversal, every SSA name will have a
6136 list of locations where ASSERT_EXPRs should be added. When a new
6137 location for name N is found, it is registered by calling
6138 register_new_assert_for. That function keeps track of all the
6139 registered assertions to prevent adding unnecessary assertions.
6140 For instance, if a pointer P_4 is dereferenced more than once in a
6141 dominator tree, only the location dominating all the dereference of
6142 P_4 will receive an ASSERT_EXPR. */
6144 static void
6145 find_assert_locations_1 (basic_block bb, sbitmap live)
6147 gimple *last;
6149 last = last_stmt (bb);
6151 /* If BB's last statement is a conditional statement involving integer
6152 operands, determine if we need to add ASSERT_EXPRs. */
6153 if (last
6154 && gimple_code (last) == GIMPLE_COND
6155 && !fp_predicate (last)
6156 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6157 find_conditional_asserts (bb, as_a <gcond *> (last));
6159 /* If BB's last statement is a switch statement involving integer
6160 operands, determine if we need to add ASSERT_EXPRs. */
6161 if (last
6162 && gimple_code (last) == GIMPLE_SWITCH
6163 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6164 find_switch_asserts (bb, as_a <gswitch *> (last));
6166 /* Traverse all the statements in BB marking used names and looking
6167 for statements that may infer assertions for their used operands. */
6168 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6169 gsi_prev (&si))
6171 gimple *stmt;
6172 tree op;
6173 ssa_op_iter i;
6175 stmt = gsi_stmt (si);
6177 if (is_gimple_debug (stmt))
6178 continue;
6180 /* See if we can derive an assertion for any of STMT's operands. */
6181 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6183 tree value;
6184 enum tree_code comp_code;
6186 /* If op is not live beyond this stmt, do not bother to insert
6187 asserts for it. */
6188 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6189 continue;
6191 /* If OP is used in such a way that we can infer a value
6192 range for it, and we don't find a previous assertion for
6193 it, create a new assertion location node for OP. */
6194 if (infer_value_range (stmt, op, &comp_code, &value))
6196 /* If we are able to infer a nonzero value range for OP,
6197 then walk backwards through the use-def chain to see if OP
6198 was set via a typecast.
6200 If so, then we can also infer a nonzero value range
6201 for the operand of the NOP_EXPR. */
6202 if (comp_code == NE_EXPR && integer_zerop (value))
6204 tree t = op;
6205 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6207 while (is_gimple_assign (def_stmt)
6208 && CONVERT_EXPR_CODE_P
6209 (gimple_assign_rhs_code (def_stmt))
6210 && TREE_CODE
6211 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6212 && POINTER_TYPE_P
6213 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6215 t = gimple_assign_rhs1 (def_stmt);
6216 def_stmt = SSA_NAME_DEF_STMT (t);
6218 /* Note we want to register the assert for the
6219 operand of the NOP_EXPR after SI, not after the
6220 conversion. */
6221 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6222 register_new_assert_for (t, t, comp_code, value,
6223 bb, NULL, si);
6227 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6231 /* Update live. */
6232 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6233 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6234 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6235 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6238 /* Traverse all PHI nodes in BB, updating live. */
6239 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6240 gsi_next (&si))
6242 use_operand_p arg_p;
6243 ssa_op_iter i;
6244 gphi *phi = si.phi ();
6245 tree res = gimple_phi_result (phi);
6247 if (virtual_operand_p (res))
6248 continue;
6250 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6252 tree arg = USE_FROM_PTR (arg_p);
6253 if (TREE_CODE (arg) == SSA_NAME)
6254 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6257 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6261 /* Do an RPO walk over the function computing SSA name liveness
6262 on-the-fly and deciding on assert expressions to insert. */
6264 static void
6265 find_assert_locations (void)
6267 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6268 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6269 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6270 int rpo_cnt, i;
6272 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6273 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6274 for (i = 0; i < rpo_cnt; ++i)
6275 bb_rpo[rpo[i]] = i;
6277 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6278 the order we compute liveness and insert asserts we otherwise
6279 fail to insert asserts into the loop latch. */
6280 loop_p loop;
6281 FOR_EACH_LOOP (loop, 0)
6283 i = loop->latch->index;
6284 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6285 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6286 !gsi_end_p (gsi); gsi_next (&gsi))
6288 gphi *phi = gsi.phi ();
6289 if (virtual_operand_p (gimple_phi_result (phi)))
6290 continue;
6291 tree arg = gimple_phi_arg_def (phi, j);
6292 if (TREE_CODE (arg) == SSA_NAME)
6294 if (live[i] == NULL)
6296 live[i] = sbitmap_alloc (num_ssa_names);
6297 bitmap_clear (live[i]);
6299 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6304 for (i = rpo_cnt - 1; i >= 0; --i)
6306 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6307 edge e;
6308 edge_iterator ei;
6310 if (!live[rpo[i]])
6312 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6313 bitmap_clear (live[rpo[i]]);
6316 /* Process BB and update the live information with uses in
6317 this block. */
6318 find_assert_locations_1 (bb, live[rpo[i]]);
6320 /* Merge liveness into the predecessor blocks and free it. */
6321 if (!bitmap_empty_p (live[rpo[i]]))
6323 int pred_rpo = i;
6324 FOR_EACH_EDGE (e, ei, bb->preds)
6326 int pred = e->src->index;
6327 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6328 continue;
6330 if (!live[pred])
6332 live[pred] = sbitmap_alloc (num_ssa_names);
6333 bitmap_clear (live[pred]);
6335 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6337 if (bb_rpo[pred] < pred_rpo)
6338 pred_rpo = bb_rpo[pred];
6341 /* Record the RPO number of the last visited block that needs
6342 live information from this block. */
6343 last_rpo[rpo[i]] = pred_rpo;
6345 else
6347 sbitmap_free (live[rpo[i]]);
6348 live[rpo[i]] = NULL;
6351 /* We can free all successors live bitmaps if all their
6352 predecessors have been visited already. */
6353 FOR_EACH_EDGE (e, ei, bb->succs)
6354 if (last_rpo[e->dest->index] == i
6355 && live[e->dest->index])
6357 sbitmap_free (live[e->dest->index]);
6358 live[e->dest->index] = NULL;
6362 XDELETEVEC (rpo);
6363 XDELETEVEC (bb_rpo);
6364 XDELETEVEC (last_rpo);
6365 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6366 if (live[i])
6367 sbitmap_free (live[i]);
6368 XDELETEVEC (live);
6371 /* Create an ASSERT_EXPR for NAME and insert it in the location
6372 indicated by LOC. Return true if we made any edge insertions. */
6374 static bool
6375 process_assert_insertions_for (tree name, assert_locus *loc)
6377 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6378 gimple *stmt;
6379 tree cond;
6380 gimple *assert_stmt;
6381 edge_iterator ei;
6382 edge e;
6384 /* If we have X <=> X do not insert an assert expr for that. */
6385 if (loc->expr == loc->val)
6386 return false;
6388 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6389 assert_stmt = build_assert_expr_for (cond, name);
6390 if (loc->e)
6392 /* We have been asked to insert the assertion on an edge. This
6393 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6394 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6395 || (gimple_code (gsi_stmt (loc->si))
6396 == GIMPLE_SWITCH));
6398 gsi_insert_on_edge (loc->e, assert_stmt);
6399 return true;
6402 /* Otherwise, we can insert right after LOC->SI iff the
6403 statement must not be the last statement in the block. */
6404 stmt = gsi_stmt (loc->si);
6405 if (!stmt_ends_bb_p (stmt))
6407 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6408 return false;
6411 /* If STMT must be the last statement in BB, we can only insert new
6412 assertions on the non-abnormal edge out of BB. Note that since
6413 STMT is not control flow, there may only be one non-abnormal/eh edge
6414 out of BB. */
6415 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6416 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
6418 gsi_insert_on_edge (e, assert_stmt);
6419 return true;
6422 gcc_unreachable ();
6426 /* Process all the insertions registered for every name N_i registered
6427 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6428 found in ASSERTS_FOR[i]. */
6430 static void
6431 process_assert_insertions (void)
6433 unsigned i;
6434 bitmap_iterator bi;
6435 bool update_edges_p = false;
6436 int num_asserts = 0;
6438 if (dump_file && (dump_flags & TDF_DETAILS))
6439 dump_all_asserts (dump_file);
6441 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6443 assert_locus *loc = asserts_for[i];
6444 gcc_assert (loc);
6446 while (loc)
6448 assert_locus *next = loc->next;
6449 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6450 free (loc);
6451 loc = next;
6452 num_asserts++;
6456 if (update_edges_p)
6457 gsi_commit_edge_inserts ();
6459 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6460 num_asserts);
6464 /* Traverse the flowgraph looking for conditional jumps to insert range
6465 expressions. These range expressions are meant to provide information
6466 to optimizations that need to reason in terms of value ranges. They
6467 will not be expanded into RTL. For instance, given:
6469 x = ...
6470 y = ...
6471 if (x < y)
6472 y = x - 2;
6473 else
6474 x = y + 3;
6476 this pass will transform the code into:
6478 x = ...
6479 y = ...
6480 if (x < y)
6482 x = ASSERT_EXPR <x, x < y>
6483 y = x - 2
6485 else
6487 y = ASSERT_EXPR <y, x >= y>
6488 x = y + 3
6491 The idea is that once copy and constant propagation have run, other
6492 optimizations will be able to determine what ranges of values can 'x'
6493 take in different paths of the code, simply by checking the reaching
6494 definition of 'x'. */
6496 static void
6497 insert_range_assertions (void)
6499 need_assert_for = BITMAP_ALLOC (NULL);
6500 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6502 calculate_dominance_info (CDI_DOMINATORS);
6504 find_assert_locations ();
6505 if (!bitmap_empty_p (need_assert_for))
6507 process_assert_insertions ();
6508 update_ssa (TODO_update_ssa_no_phi);
6511 if (dump_file && (dump_flags & TDF_DETAILS))
6513 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6514 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6517 free (asserts_for);
6518 BITMAP_FREE (need_assert_for);
6521 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6522 and "struct" hacks. If VRP can determine that the
6523 array subscript is a constant, check if it is outside valid
6524 range. If the array subscript is a RANGE, warn if it is
6525 non-overlapping with valid range.
6526 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6528 static void
6529 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6531 value_range *vr = NULL;
6532 tree low_sub, up_sub;
6533 tree low_bound, up_bound, up_bound_p1;
6535 if (TREE_NO_WARNING (ref))
6536 return;
6538 low_sub = up_sub = TREE_OPERAND (ref, 1);
6539 up_bound = array_ref_up_bound (ref);
6541 /* Can not check flexible arrays. */
6542 if (!up_bound
6543 || TREE_CODE (up_bound) != INTEGER_CST)
6544 return;
6546 /* Accesses to trailing arrays via pointers may access storage
6547 beyond the types array bounds. */
6548 if (warn_array_bounds < 2
6549 && array_at_struct_end_p (ref))
6550 return;
6552 low_bound = array_ref_low_bound (ref);
6553 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6554 build_int_cst (TREE_TYPE (up_bound), 1));
6556 /* Empty array. */
6557 if (tree_int_cst_equal (low_bound, up_bound_p1))
6559 warning_at (location, OPT_Warray_bounds,
6560 "array subscript is above array bounds");
6561 TREE_NO_WARNING (ref) = 1;
6564 if (TREE_CODE (low_sub) == SSA_NAME)
6566 vr = get_value_range (low_sub);
6567 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6569 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6570 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6574 if (vr && vr->type == VR_ANTI_RANGE)
6576 if (TREE_CODE (up_sub) == INTEGER_CST
6577 && (ignore_off_by_one
6578 ? tree_int_cst_lt (up_bound, up_sub)
6579 : tree_int_cst_le (up_bound, up_sub))
6580 && TREE_CODE (low_sub) == INTEGER_CST
6581 && tree_int_cst_le (low_sub, low_bound))
6583 warning_at (location, OPT_Warray_bounds,
6584 "array subscript is outside array bounds");
6585 TREE_NO_WARNING (ref) = 1;
6588 else if (TREE_CODE (up_sub) == INTEGER_CST
6589 && (ignore_off_by_one
6590 ? !tree_int_cst_le (up_sub, up_bound_p1)
6591 : !tree_int_cst_le (up_sub, up_bound)))
6593 if (dump_file && (dump_flags & TDF_DETAILS))
6595 fprintf (dump_file, "Array bound warning for ");
6596 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6597 fprintf (dump_file, "\n");
6599 warning_at (location, OPT_Warray_bounds,
6600 "array subscript is above array bounds");
6601 TREE_NO_WARNING (ref) = 1;
6603 else if (TREE_CODE (low_sub) == INTEGER_CST
6604 && tree_int_cst_lt (low_sub, low_bound))
6606 if (dump_file && (dump_flags & TDF_DETAILS))
6608 fprintf (dump_file, "Array bound warning for ");
6609 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6610 fprintf (dump_file, "\n");
6612 warning_at (location, OPT_Warray_bounds,
6613 "array subscript is below array bounds");
6614 TREE_NO_WARNING (ref) = 1;
6618 /* Searches if the expr T, located at LOCATION computes
6619 address of an ARRAY_REF, and call check_array_ref on it. */
6621 static void
6622 search_for_addr_array (tree t, location_t location)
6624 /* Check each ARRAY_REFs in the reference chain. */
6627 if (TREE_CODE (t) == ARRAY_REF)
6628 check_array_ref (location, t, true /*ignore_off_by_one*/);
6630 t = TREE_OPERAND (t, 0);
6632 while (handled_component_p (t));
6634 if (TREE_CODE (t) == MEM_REF
6635 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6636 && !TREE_NO_WARNING (t))
6638 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6639 tree low_bound, up_bound, el_sz;
6640 offset_int idx;
6641 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6642 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6643 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6644 return;
6646 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6647 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6648 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6649 if (!low_bound
6650 || TREE_CODE (low_bound) != INTEGER_CST
6651 || !up_bound
6652 || TREE_CODE (up_bound) != INTEGER_CST
6653 || !el_sz
6654 || TREE_CODE (el_sz) != INTEGER_CST)
6655 return;
6657 idx = mem_ref_offset (t);
6658 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6659 if (idx < 0)
6661 if (dump_file && (dump_flags & TDF_DETAILS))
6663 fprintf (dump_file, "Array bound warning for ");
6664 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6665 fprintf (dump_file, "\n");
6667 warning_at (location, OPT_Warray_bounds,
6668 "array subscript is below array bounds");
6669 TREE_NO_WARNING (t) = 1;
6671 else if (idx > (wi::to_offset (up_bound)
6672 - wi::to_offset (low_bound) + 1))
6674 if (dump_file && (dump_flags & TDF_DETAILS))
6676 fprintf (dump_file, "Array bound warning for ");
6677 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6678 fprintf (dump_file, "\n");
6680 warning_at (location, OPT_Warray_bounds,
6681 "array subscript is above array bounds");
6682 TREE_NO_WARNING (t) = 1;
6687 /* walk_tree() callback that checks if *TP is
6688 an ARRAY_REF inside an ADDR_EXPR (in which an array
6689 subscript one outside the valid range is allowed). Call
6690 check_array_ref for each ARRAY_REF found. The location is
6691 passed in DATA. */
6693 static tree
6694 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6696 tree t = *tp;
6697 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6698 location_t location;
6700 if (EXPR_HAS_LOCATION (t))
6701 location = EXPR_LOCATION (t);
6702 else
6704 location_t *locp = (location_t *) wi->info;
6705 location = *locp;
6708 *walk_subtree = TRUE;
6710 if (TREE_CODE (t) == ARRAY_REF)
6711 check_array_ref (location, t, false /*ignore_off_by_one*/);
6713 else if (TREE_CODE (t) == ADDR_EXPR)
6715 search_for_addr_array (t, location);
6716 *walk_subtree = FALSE;
6719 return NULL_TREE;
6722 /* Walk over all statements of all reachable BBs and call check_array_bounds
6723 on them. */
6725 static void
6726 check_all_array_refs (void)
6728 basic_block bb;
6729 gimple_stmt_iterator si;
6731 FOR_EACH_BB_FN (bb, cfun)
6733 edge_iterator ei;
6734 edge e;
6735 bool executable = false;
6737 /* Skip blocks that were found to be unreachable. */
6738 FOR_EACH_EDGE (e, ei, bb->preds)
6739 executable |= !!(e->flags & EDGE_EXECUTABLE);
6740 if (!executable)
6741 continue;
6743 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6745 gimple *stmt = gsi_stmt (si);
6746 struct walk_stmt_info wi;
6747 if (!gimple_has_location (stmt)
6748 || is_gimple_debug (stmt))
6749 continue;
6751 memset (&wi, 0, sizeof (wi));
6753 location_t loc = gimple_location (stmt);
6754 wi.info = &loc;
6756 walk_gimple_op (gsi_stmt (si),
6757 check_array_bounds,
6758 &wi);
6763 /* Return true if all imm uses of VAR are either in STMT, or
6764 feed (optionally through a chain of single imm uses) GIMPLE_COND
6765 in basic block COND_BB. */
6767 static bool
6768 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6770 use_operand_p use_p, use2_p;
6771 imm_use_iterator iter;
6773 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6774 if (USE_STMT (use_p) != stmt)
6776 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6777 if (is_gimple_debug (use_stmt))
6778 continue;
6779 while (is_gimple_assign (use_stmt)
6780 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6781 && single_imm_use (gimple_assign_lhs (use_stmt),
6782 &use2_p, &use_stmt2))
6783 use_stmt = use_stmt2;
6784 if (gimple_code (use_stmt) != GIMPLE_COND
6785 || gimple_bb (use_stmt) != cond_bb)
6786 return false;
6788 return true;
6791 /* Handle
6792 _4 = x_3 & 31;
6793 if (_4 != 0)
6794 goto <bb 6>;
6795 else
6796 goto <bb 7>;
6797 <bb 6>:
6798 __builtin_unreachable ();
6799 <bb 7>:
6800 x_5 = ASSERT_EXPR <x_3, ...>;
6801 If x_3 has no other immediate uses (checked by caller),
6802 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6803 from the non-zero bitmask. */
6805 static void
6806 maybe_set_nonzero_bits (basic_block bb, tree var)
6808 edge e = single_pred_edge (bb);
6809 basic_block cond_bb = e->src;
6810 gimple *stmt = last_stmt (cond_bb);
6811 tree cst;
6813 if (stmt == NULL
6814 || gimple_code (stmt) != GIMPLE_COND
6815 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6816 ? EQ_EXPR : NE_EXPR)
6817 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6818 || !integer_zerop (gimple_cond_rhs (stmt)))
6819 return;
6821 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6822 if (!is_gimple_assign (stmt)
6823 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6824 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6825 return;
6826 if (gimple_assign_rhs1 (stmt) != var)
6828 gimple *stmt2;
6830 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6831 return;
6832 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6833 if (!gimple_assign_cast_p (stmt2)
6834 || gimple_assign_rhs1 (stmt2) != var
6835 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6836 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6837 != TYPE_PRECISION (TREE_TYPE (var))))
6838 return;
6840 cst = gimple_assign_rhs2 (stmt);
6841 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6844 /* Convert range assertion expressions into the implied copies and
6845 copy propagate away the copies. Doing the trivial copy propagation
6846 here avoids the need to run the full copy propagation pass after
6847 VRP.
6849 FIXME, this will eventually lead to copy propagation removing the
6850 names that had useful range information attached to them. For
6851 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6852 then N_i will have the range [3, +INF].
6854 However, by converting the assertion into the implied copy
6855 operation N_i = N_j, we will then copy-propagate N_j into the uses
6856 of N_i and lose the range information. We may want to hold on to
6857 ASSERT_EXPRs a little while longer as the ranges could be used in
6858 things like jump threading.
6860 The problem with keeping ASSERT_EXPRs around is that passes after
6861 VRP need to handle them appropriately.
6863 Another approach would be to make the range information a first
6864 class property of the SSA_NAME so that it can be queried from
6865 any pass. This is made somewhat more complex by the need for
6866 multiple ranges to be associated with one SSA_NAME. */
6868 static void
6869 remove_range_assertions (void)
6871 basic_block bb;
6872 gimple_stmt_iterator si;
6873 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6874 a basic block preceeded by GIMPLE_COND branching to it and
6875 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6876 int is_unreachable;
6878 /* Note that the BSI iterator bump happens at the bottom of the
6879 loop and no bump is necessary if we're removing the statement
6880 referenced by the current BSI. */
6881 FOR_EACH_BB_FN (bb, cfun)
6882 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6884 gimple *stmt = gsi_stmt (si);
6885 gimple *use_stmt;
6887 if (is_gimple_assign (stmt)
6888 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6890 tree lhs = gimple_assign_lhs (stmt);
6891 tree rhs = gimple_assign_rhs1 (stmt);
6892 tree var;
6893 use_operand_p use_p;
6894 imm_use_iterator iter;
6896 var = ASSERT_EXPR_VAR (rhs);
6898 if (TREE_CODE (var) == SSA_NAME
6899 && !POINTER_TYPE_P (TREE_TYPE (lhs))
6900 && SSA_NAME_RANGE_INFO (lhs))
6902 if (is_unreachable == -1)
6904 is_unreachable = 0;
6905 if (single_pred_p (bb)
6906 && assert_unreachable_fallthru_edge_p
6907 (single_pred_edge (bb)))
6908 is_unreachable = 1;
6910 /* Handle
6911 if (x_7 >= 10 && x_7 < 20)
6912 __builtin_unreachable ();
6913 x_8 = ASSERT_EXPR <x_7, ...>;
6914 if the only uses of x_7 are in the ASSERT_EXPR and
6915 in the condition. In that case, we can copy the
6916 range info from x_8 computed in this pass also
6917 for x_7. */
6918 if (is_unreachable
6919 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6920 single_pred (bb)))
6922 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6923 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6924 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6925 maybe_set_nonzero_bits (bb, var);
6929 /* Propagate the RHS into every use of the LHS. */
6930 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6932 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6933 SET_USE (use_p, var);
6934 update_stmt (use_stmt);
6937 /* And finally, remove the copy, it is not needed. */
6938 gsi_remove (&si, true);
6939 release_defs (stmt);
6941 else
6943 if (!is_gimple_debug (gsi_stmt (si)))
6944 is_unreachable = 0;
6945 gsi_next (&si);
6951 /* Return true if STMT is interesting for VRP. */
6953 static bool
6954 stmt_interesting_for_vrp (gimple *stmt)
6956 if (gimple_code (stmt) == GIMPLE_PHI)
6958 tree res = gimple_phi_result (stmt);
6959 return (!virtual_operand_p (res)
6960 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6961 || POINTER_TYPE_P (TREE_TYPE (res))));
6963 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6965 tree lhs = gimple_get_lhs (stmt);
6967 /* In general, assignments with virtual operands are not useful
6968 for deriving ranges, with the obvious exception of calls to
6969 builtin functions. */
6970 if (lhs && TREE_CODE (lhs) == SSA_NAME
6971 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6972 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6973 && (is_gimple_call (stmt)
6974 || !gimple_vuse (stmt)))
6975 return true;
6976 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6977 switch (gimple_call_internal_fn (stmt))
6979 case IFN_ADD_OVERFLOW:
6980 case IFN_SUB_OVERFLOW:
6981 case IFN_MUL_OVERFLOW:
6982 /* These internal calls return _Complex integer type,
6983 but are interesting to VRP nevertheless. */
6984 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6985 return true;
6986 break;
6987 default:
6988 break;
6991 else if (gimple_code (stmt) == GIMPLE_COND
6992 || gimple_code (stmt) == GIMPLE_SWITCH)
6993 return true;
6995 return false;
6998 /* Initialize VRP lattice. */
7000 static void
7001 vrp_initialize_lattice ()
7003 values_propagated = false;
7004 num_vr_values = num_ssa_names;
7005 vr_value = XCNEWVEC (value_range *, num_vr_values);
7006 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7007 bitmap_obstack_initialize (&vrp_equiv_obstack);
7010 /* Initialization required by ssa_propagate engine. */
7012 static void
7013 vrp_initialize ()
7015 basic_block bb;
7017 FOR_EACH_BB_FN (bb, cfun)
7019 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7020 gsi_next (&si))
7022 gphi *phi = si.phi ();
7023 if (!stmt_interesting_for_vrp (phi))
7025 tree lhs = PHI_RESULT (phi);
7026 set_value_range_to_varying (get_value_range (lhs));
7027 prop_set_simulate_again (phi, false);
7029 else
7030 prop_set_simulate_again (phi, true);
7033 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7034 gsi_next (&si))
7036 gimple *stmt = gsi_stmt (si);
7038 /* If the statement is a control insn, then we do not
7039 want to avoid simulating the statement once. Failure
7040 to do so means that those edges will never get added. */
7041 if (stmt_ends_bb_p (stmt))
7042 prop_set_simulate_again (stmt, true);
7043 else if (!stmt_interesting_for_vrp (stmt))
7045 set_defs_to_varying (stmt);
7046 prop_set_simulate_again (stmt, false);
7048 else
7049 prop_set_simulate_again (stmt, true);
7054 /* Return the singleton value-range for NAME or NAME. */
7056 static inline tree
7057 vrp_valueize (tree name)
7059 if (TREE_CODE (name) == SSA_NAME)
7061 value_range *vr = get_value_range (name);
7062 if (vr->type == VR_RANGE
7063 && (TREE_CODE (vr->min) == SSA_NAME
7064 || is_gimple_min_invariant (vr->min))
7065 && vrp_operand_equal_p (vr->min, vr->max))
7066 return vr->min;
7068 return name;
7071 /* Return the singleton value-range for NAME if that is a constant
7072 but signal to not follow SSA edges. */
7074 static inline tree
7075 vrp_valueize_1 (tree name)
7077 if (TREE_CODE (name) == SSA_NAME)
7079 /* If the definition may be simulated again we cannot follow
7080 this SSA edge as the SSA propagator does not necessarily
7081 re-visit the use. */
7082 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7083 if (!gimple_nop_p (def_stmt)
7084 && prop_simulate_again_p (def_stmt))
7085 return NULL_TREE;
7086 value_range *vr = get_value_range (name);
7087 if (range_int_cst_singleton_p (vr))
7088 return vr->min;
7090 return name;
7093 /* Visit assignment STMT. If it produces an interesting range, record
7094 the range in VR and set LHS to OUTPUT_P. */
7096 static void
7097 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p, value_range *vr)
7099 tree lhs;
7100 enum gimple_code code = gimple_code (stmt);
7101 lhs = gimple_get_lhs (stmt);
7102 *output_p = NULL_TREE;
7104 /* We only keep track of ranges in integral and pointer types. */
7105 if (TREE_CODE (lhs) == SSA_NAME
7106 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7107 /* It is valid to have NULL MIN/MAX values on a type. See
7108 build_range_type. */
7109 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7110 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7111 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7113 /* Try folding the statement to a constant first. */
7114 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7115 vrp_valueize_1);
7116 if (tem && is_gimple_min_invariant (tem))
7117 set_value_range_to_value (vr, tem, NULL);
7118 /* Then dispatch to value-range extracting functions. */
7119 else if (code == GIMPLE_CALL)
7120 extract_range_basic (vr, stmt);
7121 else
7122 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
7123 *output_p = lhs;
7127 /* Helper that gets the value range of the SSA_NAME with version I
7128 or a symbolic range containing the SSA_NAME only if the value range
7129 is varying or undefined. */
7131 static inline value_range
7132 get_vr_for_comparison (int i)
7134 value_range vr = *get_value_range (ssa_name (i));
7136 /* If name N_i does not have a valid range, use N_i as its own
7137 range. This allows us to compare against names that may
7138 have N_i in their ranges. */
7139 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7141 vr.type = VR_RANGE;
7142 vr.min = ssa_name (i);
7143 vr.max = ssa_name (i);
7146 return vr;
7149 /* Compare all the value ranges for names equivalent to VAR with VAL
7150 using comparison code COMP. Return the same value returned by
7151 compare_range_with_value, including the setting of
7152 *STRICT_OVERFLOW_P. */
7154 static tree
7155 compare_name_with_value (enum tree_code comp, tree var, tree val,
7156 bool *strict_overflow_p, bool use_equiv_p)
7158 bitmap_iterator bi;
7159 unsigned i;
7160 bitmap e;
7161 tree retval, t;
7162 int used_strict_overflow;
7163 bool sop;
7164 value_range equiv_vr;
7166 /* Get the set of equivalences for VAR. */
7167 e = get_value_range (var)->equiv;
7169 /* Start at -1. Set it to 0 if we do a comparison without relying
7170 on overflow, or 1 if all comparisons rely on overflow. */
7171 used_strict_overflow = -1;
7173 /* Compare vars' value range with val. */
7174 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7175 sop = false;
7176 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7177 if (retval)
7178 used_strict_overflow = sop ? 1 : 0;
7180 /* If the equiv set is empty we have done all work we need to do. */
7181 if (e == NULL)
7183 if (retval
7184 && used_strict_overflow > 0)
7185 *strict_overflow_p = true;
7186 return retval;
7189 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7191 tree name = ssa_name (i);
7192 if (! name)
7193 continue;
7195 if (! use_equiv_p
7196 && ! SSA_NAME_IS_DEFAULT_DEF (name)
7197 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name)))
7198 continue;
7200 equiv_vr = get_vr_for_comparison (i);
7201 sop = false;
7202 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7203 if (t)
7205 /* If we get different answers from different members
7206 of the equivalence set this check must be in a dead
7207 code region. Folding it to a trap representation
7208 would be correct here. For now just return don't-know. */
7209 if (retval != NULL
7210 && t != retval)
7212 retval = NULL_TREE;
7213 break;
7215 retval = t;
7217 if (!sop)
7218 used_strict_overflow = 0;
7219 else if (used_strict_overflow < 0)
7220 used_strict_overflow = 1;
7224 if (retval
7225 && used_strict_overflow > 0)
7226 *strict_overflow_p = true;
7228 return retval;
7232 /* Given a comparison code COMP and names N1 and N2, compare all the
7233 ranges equivalent to N1 against all the ranges equivalent to N2
7234 to determine the value of N1 COMP N2. Return the same value
7235 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7236 whether we relied on an overflow infinity in the comparison. */
7239 static tree
7240 compare_names (enum tree_code comp, tree n1, tree n2,
7241 bool *strict_overflow_p)
7243 tree t, retval;
7244 bitmap e1, e2;
7245 bitmap_iterator bi1, bi2;
7246 unsigned i1, i2;
7247 int used_strict_overflow;
7248 static bitmap_obstack *s_obstack = NULL;
7249 static bitmap s_e1 = NULL, s_e2 = NULL;
7251 /* Compare the ranges of every name equivalent to N1 against the
7252 ranges of every name equivalent to N2. */
7253 e1 = get_value_range (n1)->equiv;
7254 e2 = get_value_range (n2)->equiv;
7256 /* Use the fake bitmaps if e1 or e2 are not available. */
7257 if (s_obstack == NULL)
7259 s_obstack = XNEW (bitmap_obstack);
7260 bitmap_obstack_initialize (s_obstack);
7261 s_e1 = BITMAP_ALLOC (s_obstack);
7262 s_e2 = BITMAP_ALLOC (s_obstack);
7264 if (e1 == NULL)
7265 e1 = s_e1;
7266 if (e2 == NULL)
7267 e2 = s_e2;
7269 /* Add N1 and N2 to their own set of equivalences to avoid
7270 duplicating the body of the loop just to check N1 and N2
7271 ranges. */
7272 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7273 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7275 /* If the equivalence sets have a common intersection, then the two
7276 names can be compared without checking their ranges. */
7277 if (bitmap_intersect_p (e1, e2))
7279 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7280 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7282 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7283 ? boolean_true_node
7284 : boolean_false_node;
7287 /* Start at -1. Set it to 0 if we do a comparison without relying
7288 on overflow, or 1 if all comparisons rely on overflow. */
7289 used_strict_overflow = -1;
7291 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7292 N2 to their own set of equivalences to avoid duplicating the body
7293 of the loop just to check N1 and N2 ranges. */
7294 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7296 if (! ssa_name (i1))
7297 continue;
7299 value_range vr1 = get_vr_for_comparison (i1);
7301 t = retval = NULL_TREE;
7302 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7304 if (! ssa_name (i2))
7305 continue;
7307 bool sop = false;
7309 value_range vr2 = get_vr_for_comparison (i2);
7311 t = compare_ranges (comp, &vr1, &vr2, &sop);
7312 if (t)
7314 /* If we get different answers from different members
7315 of the equivalence set this check must be in a dead
7316 code region. Folding it to a trap representation
7317 would be correct here. For now just return don't-know. */
7318 if (retval != NULL
7319 && t != retval)
7321 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7322 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7323 return NULL_TREE;
7325 retval = t;
7327 if (!sop)
7328 used_strict_overflow = 0;
7329 else if (used_strict_overflow < 0)
7330 used_strict_overflow = 1;
7334 if (retval)
7336 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7337 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7338 if (used_strict_overflow > 0)
7339 *strict_overflow_p = true;
7340 return retval;
7344 /* None of the equivalent ranges are useful in computing this
7345 comparison. */
7346 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7347 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7348 return NULL_TREE;
7351 /* Helper function for vrp_evaluate_conditional_warnv & other
7352 optimizers. */
7354 static tree
7355 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7356 tree op0, tree op1,
7357 bool * strict_overflow_p)
7359 value_range *vr0, *vr1;
7361 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7362 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7364 tree res = NULL_TREE;
7365 if (vr0 && vr1)
7366 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7367 if (!res && vr0)
7368 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7369 if (!res && vr1)
7370 res = (compare_range_with_value
7371 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7372 return res;
7375 /* Helper function for vrp_evaluate_conditional_warnv. */
7377 static tree
7378 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7379 tree op1, bool use_equiv_p,
7380 bool *strict_overflow_p, bool *only_ranges)
7382 tree ret;
7383 if (only_ranges)
7384 *only_ranges = true;
7386 /* We only deal with integral and pointer types. */
7387 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7388 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7389 return NULL_TREE;
7391 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7392 (code, op0, op1, strict_overflow_p)))
7393 return ret;
7394 if (only_ranges)
7395 *only_ranges = false;
7396 /* Do not use compare_names during propagation, it's quadratic. */
7397 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7398 && use_equiv_p)
7399 return compare_names (code, op0, op1, strict_overflow_p);
7400 else if (TREE_CODE (op0) == SSA_NAME)
7401 return compare_name_with_value (code, op0, op1,
7402 strict_overflow_p, use_equiv_p);
7403 else if (TREE_CODE (op1) == SSA_NAME)
7404 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7405 strict_overflow_p, use_equiv_p);
7406 return NULL_TREE;
7409 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7410 information. Return NULL if the conditional can not be evaluated.
7411 The ranges of all the names equivalent with the operands in COND
7412 will be used when trying to compute the value. If the result is
7413 based on undefined signed overflow, issue a warning if
7414 appropriate. */
7416 static tree
7417 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7419 bool sop;
7420 tree ret;
7421 bool only_ranges;
7423 /* Some passes and foldings leak constants with overflow flag set
7424 into the IL. Avoid doing wrong things with these and bail out. */
7425 if ((TREE_CODE (op0) == INTEGER_CST
7426 && TREE_OVERFLOW (op0))
7427 || (TREE_CODE (op1) == INTEGER_CST
7428 && TREE_OVERFLOW (op1)))
7429 return NULL_TREE;
7431 sop = false;
7432 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7433 &only_ranges);
7435 if (ret && sop)
7437 enum warn_strict_overflow_code wc;
7438 const char* warnmsg;
7440 if (is_gimple_min_invariant (ret))
7442 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7443 warnmsg = G_("assuming signed overflow does not occur when "
7444 "simplifying conditional to constant");
7446 else
7448 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7449 warnmsg = G_("assuming signed overflow does not occur when "
7450 "simplifying conditional");
7453 if (issue_strict_overflow_warning (wc))
7455 location_t location;
7457 if (!gimple_has_location (stmt))
7458 location = input_location;
7459 else
7460 location = gimple_location (stmt);
7461 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7465 if (warn_type_limits
7466 && ret && only_ranges
7467 && TREE_CODE_CLASS (code) == tcc_comparison
7468 && TREE_CODE (op0) == SSA_NAME)
7470 /* If the comparison is being folded and the operand on the LHS
7471 is being compared against a constant value that is outside of
7472 the natural range of OP0's type, then the predicate will
7473 always fold regardless of the value of OP0. If -Wtype-limits
7474 was specified, emit a warning. */
7475 tree type = TREE_TYPE (op0);
7476 value_range *vr0 = get_value_range (op0);
7478 if (vr0->type == VR_RANGE
7479 && INTEGRAL_TYPE_P (type)
7480 && vrp_val_is_min (vr0->min)
7481 && vrp_val_is_max (vr0->max)
7482 && is_gimple_min_invariant (op1))
7484 location_t location;
7486 if (!gimple_has_location (stmt))
7487 location = input_location;
7488 else
7489 location = gimple_location (stmt);
7491 warning_at (location, OPT_Wtype_limits,
7492 integer_zerop (ret)
7493 ? G_("comparison always false "
7494 "due to limited range of data type")
7495 : G_("comparison always true "
7496 "due to limited range of data type"));
7500 return ret;
7504 /* Visit conditional statement STMT. If we can determine which edge
7505 will be taken out of STMT's basic block, record it in
7506 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7508 static void
7509 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7511 tree val;
7512 bool sop;
7514 *taken_edge_p = NULL;
7516 if (dump_file && (dump_flags & TDF_DETAILS))
7518 tree use;
7519 ssa_op_iter i;
7521 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7522 print_gimple_stmt (dump_file, stmt, 0, 0);
7523 fprintf (dump_file, "\nWith known ranges\n");
7525 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7527 fprintf (dump_file, "\t");
7528 print_generic_expr (dump_file, use, 0);
7529 fprintf (dump_file, ": ");
7530 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7533 fprintf (dump_file, "\n");
7536 /* Compute the value of the predicate COND by checking the known
7537 ranges of each of its operands.
7539 Note that we cannot evaluate all the equivalent ranges here
7540 because those ranges may not yet be final and with the current
7541 propagation strategy, we cannot determine when the value ranges
7542 of the names in the equivalence set have changed.
7544 For instance, given the following code fragment
7546 i_5 = PHI <8, i_13>
7548 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7549 if (i_14 == 1)
7552 Assume that on the first visit to i_14, i_5 has the temporary
7553 range [8, 8] because the second argument to the PHI function is
7554 not yet executable. We derive the range ~[0, 0] for i_14 and the
7555 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7556 the first time, since i_14 is equivalent to the range [8, 8], we
7557 determine that the predicate is always false.
7559 On the next round of propagation, i_13 is determined to be
7560 VARYING, which causes i_5 to drop down to VARYING. So, another
7561 visit to i_14 is scheduled. In this second visit, we compute the
7562 exact same range and equivalence set for i_14, namely ~[0, 0] and
7563 { i_5 }. But we did not have the previous range for i_5
7564 registered, so vrp_visit_assignment thinks that the range for
7565 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7566 is not visited again, which stops propagation from visiting
7567 statements in the THEN clause of that if().
7569 To properly fix this we would need to keep the previous range
7570 value for the names in the equivalence set. This way we would've
7571 discovered that from one visit to the other i_5 changed from
7572 range [8, 8] to VR_VARYING.
7574 However, fixing this apparent limitation may not be worth the
7575 additional checking. Testing on several code bases (GCC, DLV,
7576 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7577 4 more predicates folded in SPEC. */
7578 sop = false;
7580 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7581 gimple_cond_lhs (stmt),
7582 gimple_cond_rhs (stmt),
7583 false, &sop, NULL);
7584 if (val)
7586 if (!sop)
7587 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7588 else
7590 if (dump_file && (dump_flags & TDF_DETAILS))
7591 fprintf (dump_file,
7592 "\nIgnoring predicate evaluation because "
7593 "it assumes that signed overflow is undefined");
7594 val = NULL_TREE;
7598 if (dump_file && (dump_flags & TDF_DETAILS))
7600 fprintf (dump_file, "\nPredicate evaluates to: ");
7601 if (val == NULL_TREE)
7602 fprintf (dump_file, "DON'T KNOW\n");
7603 else
7604 print_generic_stmt (dump_file, val, 0);
7608 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7609 that includes the value VAL. The search is restricted to the range
7610 [START_IDX, n - 1] where n is the size of VEC.
7612 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7613 returned.
7615 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7616 it is placed in IDX and false is returned.
7618 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7619 returned. */
7621 static bool
7622 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7624 size_t n = gimple_switch_num_labels (stmt);
7625 size_t low, high;
7627 /* Find case label for minimum of the value range or the next one.
7628 At each iteration we are searching in [low, high - 1]. */
7630 for (low = start_idx, high = n; high != low; )
7632 tree t;
7633 int cmp;
7634 /* Note that i != high, so we never ask for n. */
7635 size_t i = (high + low) / 2;
7636 t = gimple_switch_label (stmt, i);
7638 /* Cache the result of comparing CASE_LOW and val. */
7639 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7641 if (cmp == 0)
7643 /* Ranges cannot be empty. */
7644 *idx = i;
7645 return true;
7647 else if (cmp > 0)
7648 high = i;
7649 else
7651 low = i + 1;
7652 if (CASE_HIGH (t) != NULL
7653 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7655 *idx = i;
7656 return true;
7661 *idx = high;
7662 return false;
7665 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7666 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7667 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7668 then MAX_IDX < MIN_IDX.
7669 Returns true if the default label is not needed. */
7671 static bool
7672 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7673 size_t *max_idx)
7675 size_t i, j;
7676 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7677 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7679 if (i == j
7680 && min_take_default
7681 && max_take_default)
7683 /* Only the default case label reached.
7684 Return an empty range. */
7685 *min_idx = 1;
7686 *max_idx = 0;
7687 return false;
7689 else
7691 bool take_default = min_take_default || max_take_default;
7692 tree low, high;
7693 size_t k;
7695 if (max_take_default)
7696 j--;
7698 /* If the case label range is continuous, we do not need
7699 the default case label. Verify that. */
7700 high = CASE_LOW (gimple_switch_label (stmt, i));
7701 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7702 high = CASE_HIGH (gimple_switch_label (stmt, i));
7703 for (k = i + 1; k <= j; ++k)
7705 low = CASE_LOW (gimple_switch_label (stmt, k));
7706 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7708 take_default = true;
7709 break;
7711 high = low;
7712 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7713 high = CASE_HIGH (gimple_switch_label (stmt, k));
7716 *min_idx = i;
7717 *max_idx = j;
7718 return !take_default;
7722 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7723 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7724 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7725 Returns true if the default label is not needed. */
7727 static bool
7728 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7729 size_t *max_idx1, size_t *min_idx2,
7730 size_t *max_idx2)
7732 size_t i, j, k, l;
7733 unsigned int n = gimple_switch_num_labels (stmt);
7734 bool take_default;
7735 tree case_low, case_high;
7736 tree min = vr->min, max = vr->max;
7738 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7740 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7742 /* Set second range to emtpy. */
7743 *min_idx2 = 1;
7744 *max_idx2 = 0;
7746 if (vr->type == VR_RANGE)
7748 *min_idx1 = i;
7749 *max_idx1 = j;
7750 return !take_default;
7753 /* Set first range to all case labels. */
7754 *min_idx1 = 1;
7755 *max_idx1 = n - 1;
7757 if (i > j)
7758 return false;
7760 /* Make sure all the values of case labels [i , j] are contained in
7761 range [MIN, MAX]. */
7762 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7763 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7764 if (tree_int_cst_compare (case_low, min) < 0)
7765 i += 1;
7766 if (case_high != NULL_TREE
7767 && tree_int_cst_compare (max, case_high) < 0)
7768 j -= 1;
7770 if (i > j)
7771 return false;
7773 /* If the range spans case labels [i, j], the corresponding anti-range spans
7774 the labels [1, i - 1] and [j + 1, n - 1]. */
7775 k = j + 1;
7776 l = n - 1;
7777 if (k > l)
7779 k = 1;
7780 l = 0;
7783 j = i - 1;
7784 i = 1;
7785 if (i > j)
7787 i = k;
7788 j = l;
7789 k = 1;
7790 l = 0;
7793 *min_idx1 = i;
7794 *max_idx1 = j;
7795 *min_idx2 = k;
7796 *max_idx2 = l;
7797 return false;
7800 /* Visit switch statement STMT. If we can determine which edge
7801 will be taken out of STMT's basic block, record it in
7802 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7804 static void
7805 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7807 tree op, val;
7808 value_range *vr;
7809 size_t i = 0, j = 0, k, l;
7810 bool take_default;
7812 *taken_edge_p = NULL;
7813 op = gimple_switch_index (stmt);
7814 if (TREE_CODE (op) != SSA_NAME)
7815 return;
7817 vr = get_value_range (op);
7818 if (dump_file && (dump_flags & TDF_DETAILS))
7820 fprintf (dump_file, "\nVisiting switch expression with operand ");
7821 print_generic_expr (dump_file, op, 0);
7822 fprintf (dump_file, " with known range ");
7823 dump_value_range (dump_file, vr);
7824 fprintf (dump_file, "\n");
7827 if ((vr->type != VR_RANGE
7828 && vr->type != VR_ANTI_RANGE)
7829 || symbolic_range_p (vr))
7830 return;
7832 /* Find the single edge that is taken from the switch expression. */
7833 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7835 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7836 label */
7837 if (j < i)
7839 gcc_assert (take_default);
7840 val = gimple_switch_default_label (stmt);
7842 else
7844 /* Check if labels with index i to j and maybe the default label
7845 are all reaching the same label. */
7847 val = gimple_switch_label (stmt, i);
7848 if (take_default
7849 && CASE_LABEL (gimple_switch_default_label (stmt))
7850 != CASE_LABEL (val))
7852 if (dump_file && (dump_flags & TDF_DETAILS))
7853 fprintf (dump_file, " not a single destination for this "
7854 "range\n");
7855 return;
7857 for (++i; i <= j; ++i)
7859 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7861 if (dump_file && (dump_flags & TDF_DETAILS))
7862 fprintf (dump_file, " not a single destination for this "
7863 "range\n");
7864 return;
7867 for (; k <= l; ++k)
7869 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7871 if (dump_file && (dump_flags & TDF_DETAILS))
7872 fprintf (dump_file, " not a single destination for this "
7873 "range\n");
7874 return;
7879 *taken_edge_p = find_edge (gimple_bb (stmt),
7880 label_to_block (CASE_LABEL (val)));
7882 if (dump_file && (dump_flags & TDF_DETAILS))
7884 fprintf (dump_file, " will take edge to ");
7885 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7890 /* Evaluate statement STMT. If the statement produces a useful range,
7891 set VR and corepsponding OUTPUT_P.
7893 If STMT is a conditional branch and we can determine its truth
7894 value, the taken edge is recorded in *TAKEN_EDGE_P. */
7896 static void
7897 extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
7898 tree *output_p, value_range *vr)
7901 if (dump_file && (dump_flags & TDF_DETAILS))
7903 fprintf (dump_file, "\nVisiting statement:\n");
7904 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7907 if (!stmt_interesting_for_vrp (stmt))
7908 gcc_assert (stmt_ends_bb_p (stmt));
7909 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7910 vrp_visit_assignment_or_call (stmt, output_p, vr);
7911 else if (gimple_code (stmt) == GIMPLE_COND)
7912 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7913 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7914 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7917 /* Evaluate statement STMT. If the statement produces a useful range,
7918 return SSA_PROP_INTERESTING and record the SSA name with the
7919 interesting range into *OUTPUT_P.
7921 If STMT is a conditional branch and we can determine its truth
7922 value, the taken edge is recorded in *TAKEN_EDGE_P.
7924 If STMT produces a varying value, return SSA_PROP_VARYING. */
7926 static enum ssa_prop_result
7927 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7929 value_range vr = VR_INITIALIZER;
7930 tree lhs = gimple_get_lhs (stmt);
7931 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
7933 if (*output_p)
7935 if (update_value_range (*output_p, &vr))
7937 if (dump_file && (dump_flags & TDF_DETAILS))
7939 fprintf (dump_file, "Found new range for ");
7940 print_generic_expr (dump_file, *output_p, 0);
7941 fprintf (dump_file, ": ");
7942 dump_value_range (dump_file, &vr);
7943 fprintf (dump_file, "\n");
7946 if (vr.type == VR_VARYING)
7947 return SSA_PROP_VARYING;
7949 return SSA_PROP_INTERESTING;
7951 return SSA_PROP_NOT_INTERESTING;
7954 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7955 switch (gimple_call_internal_fn (stmt))
7957 case IFN_ADD_OVERFLOW:
7958 case IFN_SUB_OVERFLOW:
7959 case IFN_MUL_OVERFLOW:
7960 /* These internal calls return _Complex integer type,
7961 which VRP does not track, but the immediate uses
7962 thereof might be interesting. */
7963 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7965 imm_use_iterator iter;
7966 use_operand_p use_p;
7967 enum ssa_prop_result res = SSA_PROP_VARYING;
7969 set_value_range_to_varying (get_value_range (lhs));
7971 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7973 gimple *use_stmt = USE_STMT (use_p);
7974 if (!is_gimple_assign (use_stmt))
7975 continue;
7976 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7977 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7978 continue;
7979 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7980 tree use_lhs = gimple_assign_lhs (use_stmt);
7981 if (TREE_CODE (rhs1) != rhs_code
7982 || TREE_OPERAND (rhs1, 0) != lhs
7983 || TREE_CODE (use_lhs) != SSA_NAME
7984 || !stmt_interesting_for_vrp (use_stmt)
7985 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7986 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7987 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7988 continue;
7990 /* If there is a change in the value range for any of the
7991 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7992 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7993 or IMAGPART_EXPR immediate uses, but none of them have
7994 a change in their value ranges, return
7995 SSA_PROP_NOT_INTERESTING. If there are no
7996 {REAL,IMAG}PART_EXPR uses at all,
7997 return SSA_PROP_VARYING. */
7998 value_range new_vr = VR_INITIALIZER;
7999 extract_range_basic (&new_vr, use_stmt);
8000 value_range *old_vr = get_value_range (use_lhs);
8001 if (old_vr->type != new_vr.type
8002 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
8003 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
8004 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
8005 res = SSA_PROP_INTERESTING;
8006 else
8007 res = SSA_PROP_NOT_INTERESTING;
8008 BITMAP_FREE (new_vr.equiv);
8009 if (res == SSA_PROP_INTERESTING)
8011 *output_p = lhs;
8012 return res;
8016 return res;
8018 break;
8019 default:
8020 break;
8023 /* All other statements produce nothing of interest for VRP, so mark
8024 their outputs varying and prevent further simulation. */
8025 set_defs_to_varying (stmt);
8027 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
8030 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8031 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8032 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8033 possible such range. The resulting range is not canonicalized. */
8035 static void
8036 union_ranges (enum value_range_type *vr0type,
8037 tree *vr0min, tree *vr0max,
8038 enum value_range_type vr1type,
8039 tree vr1min, tree vr1max)
8041 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8042 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8044 /* [] is vr0, () is vr1 in the following classification comments. */
8045 if (mineq && maxeq)
8047 /* [( )] */
8048 if (*vr0type == vr1type)
8049 /* Nothing to do for equal ranges. */
8051 else if ((*vr0type == VR_RANGE
8052 && vr1type == VR_ANTI_RANGE)
8053 || (*vr0type == VR_ANTI_RANGE
8054 && vr1type == VR_RANGE))
8056 /* For anti-range with range union the result is varying. */
8057 goto give_up;
8059 else
8060 gcc_unreachable ();
8062 else if (operand_less_p (*vr0max, vr1min) == 1
8063 || operand_less_p (vr1max, *vr0min) == 1)
8065 /* [ ] ( ) or ( ) [ ]
8066 If the ranges have an empty intersection, result of the union
8067 operation is the anti-range or if both are anti-ranges
8068 it covers all. */
8069 if (*vr0type == VR_ANTI_RANGE
8070 && vr1type == VR_ANTI_RANGE)
8071 goto give_up;
8072 else if (*vr0type == VR_ANTI_RANGE
8073 && vr1type == VR_RANGE)
8075 else if (*vr0type == VR_RANGE
8076 && vr1type == VR_ANTI_RANGE)
8078 *vr0type = vr1type;
8079 *vr0min = vr1min;
8080 *vr0max = vr1max;
8082 else if (*vr0type == VR_RANGE
8083 && vr1type == VR_RANGE)
8085 /* The result is the convex hull of both ranges. */
8086 if (operand_less_p (*vr0max, vr1min) == 1)
8088 /* If the result can be an anti-range, create one. */
8089 if (TREE_CODE (*vr0max) == INTEGER_CST
8090 && TREE_CODE (vr1min) == INTEGER_CST
8091 && vrp_val_is_min (*vr0min)
8092 && vrp_val_is_max (vr1max))
8094 tree min = int_const_binop (PLUS_EXPR,
8095 *vr0max,
8096 build_int_cst (TREE_TYPE (*vr0max), 1));
8097 tree max = int_const_binop (MINUS_EXPR,
8098 vr1min,
8099 build_int_cst (TREE_TYPE (vr1min), 1));
8100 if (!operand_less_p (max, min))
8102 *vr0type = VR_ANTI_RANGE;
8103 *vr0min = min;
8104 *vr0max = max;
8106 else
8107 *vr0max = vr1max;
8109 else
8110 *vr0max = vr1max;
8112 else
8114 /* If the result can be an anti-range, create one. */
8115 if (TREE_CODE (vr1max) == INTEGER_CST
8116 && TREE_CODE (*vr0min) == INTEGER_CST
8117 && vrp_val_is_min (vr1min)
8118 && vrp_val_is_max (*vr0max))
8120 tree min = int_const_binop (PLUS_EXPR,
8121 vr1max,
8122 build_int_cst (TREE_TYPE (vr1max), 1));
8123 tree max = int_const_binop (MINUS_EXPR,
8124 *vr0min,
8125 build_int_cst (TREE_TYPE (*vr0min), 1));
8126 if (!operand_less_p (max, min))
8128 *vr0type = VR_ANTI_RANGE;
8129 *vr0min = min;
8130 *vr0max = max;
8132 else
8133 *vr0min = vr1min;
8135 else
8136 *vr0min = vr1min;
8139 else
8140 gcc_unreachable ();
8142 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8143 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8145 /* [ ( ) ] or [( ) ] or [ ( )] */
8146 if (*vr0type == VR_RANGE
8147 && vr1type == VR_RANGE)
8149 else if (*vr0type == VR_ANTI_RANGE
8150 && vr1type == VR_ANTI_RANGE)
8152 *vr0type = vr1type;
8153 *vr0min = vr1min;
8154 *vr0max = vr1max;
8156 else if (*vr0type == VR_ANTI_RANGE
8157 && vr1type == VR_RANGE)
8159 /* Arbitrarily choose the right or left gap. */
8160 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8161 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8162 build_int_cst (TREE_TYPE (vr1min), 1));
8163 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8164 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8165 build_int_cst (TREE_TYPE (vr1max), 1));
8166 else
8167 goto give_up;
8169 else if (*vr0type == VR_RANGE
8170 && vr1type == VR_ANTI_RANGE)
8171 /* The result covers everything. */
8172 goto give_up;
8173 else
8174 gcc_unreachable ();
8176 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8177 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8179 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8180 if (*vr0type == VR_RANGE
8181 && vr1type == VR_RANGE)
8183 *vr0type = vr1type;
8184 *vr0min = vr1min;
8185 *vr0max = vr1max;
8187 else if (*vr0type == VR_ANTI_RANGE
8188 && vr1type == VR_ANTI_RANGE)
8190 else if (*vr0type == VR_RANGE
8191 && vr1type == VR_ANTI_RANGE)
8193 *vr0type = VR_ANTI_RANGE;
8194 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8196 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8197 build_int_cst (TREE_TYPE (*vr0min), 1));
8198 *vr0min = vr1min;
8200 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8202 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8203 build_int_cst (TREE_TYPE (*vr0max), 1));
8204 *vr0max = vr1max;
8206 else
8207 goto give_up;
8209 else if (*vr0type == VR_ANTI_RANGE
8210 && vr1type == VR_RANGE)
8211 /* The result covers everything. */
8212 goto give_up;
8213 else
8214 gcc_unreachable ();
8216 else if ((operand_less_p (vr1min, *vr0max) == 1
8217 || operand_equal_p (vr1min, *vr0max, 0))
8218 && operand_less_p (*vr0min, vr1min) == 1
8219 && operand_less_p (*vr0max, vr1max) == 1)
8221 /* [ ( ] ) or [ ]( ) */
8222 if (*vr0type == VR_RANGE
8223 && vr1type == VR_RANGE)
8224 *vr0max = vr1max;
8225 else if (*vr0type == VR_ANTI_RANGE
8226 && vr1type == VR_ANTI_RANGE)
8227 *vr0min = vr1min;
8228 else if (*vr0type == VR_ANTI_RANGE
8229 && vr1type == VR_RANGE)
8231 if (TREE_CODE (vr1min) == INTEGER_CST)
8232 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8233 build_int_cst (TREE_TYPE (vr1min), 1));
8234 else
8235 goto give_up;
8237 else if (*vr0type == VR_RANGE
8238 && vr1type == VR_ANTI_RANGE)
8240 if (TREE_CODE (*vr0max) == INTEGER_CST)
8242 *vr0type = vr1type;
8243 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8244 build_int_cst (TREE_TYPE (*vr0max), 1));
8245 *vr0max = vr1max;
8247 else
8248 goto give_up;
8250 else
8251 gcc_unreachable ();
8253 else if ((operand_less_p (*vr0min, vr1max) == 1
8254 || operand_equal_p (*vr0min, vr1max, 0))
8255 && operand_less_p (vr1min, *vr0min) == 1
8256 && operand_less_p (vr1max, *vr0max) == 1)
8258 /* ( [ ) ] or ( )[ ] */
8259 if (*vr0type == VR_RANGE
8260 && vr1type == VR_RANGE)
8261 *vr0min = vr1min;
8262 else if (*vr0type == VR_ANTI_RANGE
8263 && vr1type == VR_ANTI_RANGE)
8264 *vr0max = vr1max;
8265 else if (*vr0type == VR_ANTI_RANGE
8266 && vr1type == VR_RANGE)
8268 if (TREE_CODE (vr1max) == INTEGER_CST)
8269 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8270 build_int_cst (TREE_TYPE (vr1max), 1));
8271 else
8272 goto give_up;
8274 else if (*vr0type == VR_RANGE
8275 && vr1type == VR_ANTI_RANGE)
8277 if (TREE_CODE (*vr0min) == INTEGER_CST)
8279 *vr0type = vr1type;
8280 *vr0min = vr1min;
8281 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8282 build_int_cst (TREE_TYPE (*vr0min), 1));
8284 else
8285 goto give_up;
8287 else
8288 gcc_unreachable ();
8290 else
8291 goto give_up;
8293 return;
8295 give_up:
8296 *vr0type = VR_VARYING;
8297 *vr0min = NULL_TREE;
8298 *vr0max = NULL_TREE;
8301 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8302 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8303 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8304 possible such range. The resulting range is not canonicalized. */
8306 static void
8307 intersect_ranges (enum value_range_type *vr0type,
8308 tree *vr0min, tree *vr0max,
8309 enum value_range_type vr1type,
8310 tree vr1min, tree vr1max)
8312 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8313 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8315 /* [] is vr0, () is vr1 in the following classification comments. */
8316 if (mineq && maxeq)
8318 /* [( )] */
8319 if (*vr0type == vr1type)
8320 /* Nothing to do for equal ranges. */
8322 else if ((*vr0type == VR_RANGE
8323 && vr1type == VR_ANTI_RANGE)
8324 || (*vr0type == VR_ANTI_RANGE
8325 && vr1type == VR_RANGE))
8327 /* For anti-range with range intersection the result is empty. */
8328 *vr0type = VR_UNDEFINED;
8329 *vr0min = NULL_TREE;
8330 *vr0max = NULL_TREE;
8332 else
8333 gcc_unreachable ();
8335 else if (operand_less_p (*vr0max, vr1min) == 1
8336 || operand_less_p (vr1max, *vr0min) == 1)
8338 /* [ ] ( ) or ( ) [ ]
8339 If the ranges have an empty intersection, the result of the
8340 intersect operation is the range for intersecting an
8341 anti-range with a range or empty when intersecting two ranges. */
8342 if (*vr0type == VR_RANGE
8343 && vr1type == VR_ANTI_RANGE)
8345 else if (*vr0type == VR_ANTI_RANGE
8346 && vr1type == VR_RANGE)
8348 *vr0type = vr1type;
8349 *vr0min = vr1min;
8350 *vr0max = vr1max;
8352 else if (*vr0type == VR_RANGE
8353 && vr1type == VR_RANGE)
8355 *vr0type = VR_UNDEFINED;
8356 *vr0min = NULL_TREE;
8357 *vr0max = NULL_TREE;
8359 else if (*vr0type == VR_ANTI_RANGE
8360 && vr1type == VR_ANTI_RANGE)
8362 /* If the anti-ranges are adjacent to each other merge them. */
8363 if (TREE_CODE (*vr0max) == INTEGER_CST
8364 && TREE_CODE (vr1min) == INTEGER_CST
8365 && operand_less_p (*vr0max, vr1min) == 1
8366 && integer_onep (int_const_binop (MINUS_EXPR,
8367 vr1min, *vr0max)))
8368 *vr0max = vr1max;
8369 else if (TREE_CODE (vr1max) == INTEGER_CST
8370 && TREE_CODE (*vr0min) == INTEGER_CST
8371 && operand_less_p (vr1max, *vr0min) == 1
8372 && integer_onep (int_const_binop (MINUS_EXPR,
8373 *vr0min, vr1max)))
8374 *vr0min = vr1min;
8375 /* Else arbitrarily take VR0. */
8378 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8379 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8381 /* [ ( ) ] or [( ) ] or [ ( )] */
8382 if (*vr0type == VR_RANGE
8383 && vr1type == VR_RANGE)
8385 /* If both are ranges the result is the inner one. */
8386 *vr0type = vr1type;
8387 *vr0min = vr1min;
8388 *vr0max = vr1max;
8390 else if (*vr0type == VR_RANGE
8391 && vr1type == VR_ANTI_RANGE)
8393 /* Choose the right gap if the left one is empty. */
8394 if (mineq)
8396 if (TREE_CODE (vr1max) == INTEGER_CST)
8397 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8398 build_int_cst (TREE_TYPE (vr1max), 1));
8399 else
8400 *vr0min = vr1max;
8402 /* Choose the left gap if the right one is empty. */
8403 else if (maxeq)
8405 if (TREE_CODE (vr1min) == INTEGER_CST)
8406 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8407 build_int_cst (TREE_TYPE (vr1min), 1));
8408 else
8409 *vr0max = vr1min;
8411 /* Choose the anti-range if the range is effectively varying. */
8412 else if (vrp_val_is_min (*vr0min)
8413 && vrp_val_is_max (*vr0max))
8415 *vr0type = vr1type;
8416 *vr0min = vr1min;
8417 *vr0max = vr1max;
8419 /* Else choose the range. */
8421 else if (*vr0type == VR_ANTI_RANGE
8422 && vr1type == VR_ANTI_RANGE)
8423 /* If both are anti-ranges the result is the outer one. */
8425 else if (*vr0type == VR_ANTI_RANGE
8426 && vr1type == VR_RANGE)
8428 /* The intersection is empty. */
8429 *vr0type = VR_UNDEFINED;
8430 *vr0min = NULL_TREE;
8431 *vr0max = NULL_TREE;
8433 else
8434 gcc_unreachable ();
8436 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8437 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8439 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8440 if (*vr0type == VR_RANGE
8441 && vr1type == VR_RANGE)
8442 /* Choose the inner range. */
8444 else if (*vr0type == VR_ANTI_RANGE
8445 && vr1type == VR_RANGE)
8447 /* Choose the right gap if the left is empty. */
8448 if (mineq)
8450 *vr0type = VR_RANGE;
8451 if (TREE_CODE (*vr0max) == INTEGER_CST)
8452 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8453 build_int_cst (TREE_TYPE (*vr0max), 1));
8454 else
8455 *vr0min = *vr0max;
8456 *vr0max = vr1max;
8458 /* Choose the left gap if the right is empty. */
8459 else if (maxeq)
8461 *vr0type = VR_RANGE;
8462 if (TREE_CODE (*vr0min) == INTEGER_CST)
8463 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8464 build_int_cst (TREE_TYPE (*vr0min), 1));
8465 else
8466 *vr0max = *vr0min;
8467 *vr0min = vr1min;
8469 /* Choose the anti-range if the range is effectively varying. */
8470 else if (vrp_val_is_min (vr1min)
8471 && vrp_val_is_max (vr1max))
8473 /* Else choose the range. */
8474 else
8476 *vr0type = vr1type;
8477 *vr0min = vr1min;
8478 *vr0max = vr1max;
8481 else if (*vr0type == VR_ANTI_RANGE
8482 && vr1type == VR_ANTI_RANGE)
8484 /* If both are anti-ranges the result is the outer one. */
8485 *vr0type = vr1type;
8486 *vr0min = vr1min;
8487 *vr0max = vr1max;
8489 else if (vr1type == VR_ANTI_RANGE
8490 && *vr0type == VR_RANGE)
8492 /* The intersection is empty. */
8493 *vr0type = VR_UNDEFINED;
8494 *vr0min = NULL_TREE;
8495 *vr0max = NULL_TREE;
8497 else
8498 gcc_unreachable ();
8500 else if ((operand_less_p (vr1min, *vr0max) == 1
8501 || operand_equal_p (vr1min, *vr0max, 0))
8502 && operand_less_p (*vr0min, vr1min) == 1)
8504 /* [ ( ] ) or [ ]( ) */
8505 if (*vr0type == VR_ANTI_RANGE
8506 && vr1type == VR_ANTI_RANGE)
8507 *vr0max = vr1max;
8508 else if (*vr0type == VR_RANGE
8509 && vr1type == VR_RANGE)
8510 *vr0min = vr1min;
8511 else if (*vr0type == VR_RANGE
8512 && vr1type == VR_ANTI_RANGE)
8514 if (TREE_CODE (vr1min) == INTEGER_CST)
8515 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8516 build_int_cst (TREE_TYPE (vr1min), 1));
8517 else
8518 *vr0max = vr1min;
8520 else if (*vr0type == VR_ANTI_RANGE
8521 && vr1type == VR_RANGE)
8523 *vr0type = VR_RANGE;
8524 if (TREE_CODE (*vr0max) == INTEGER_CST)
8525 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8526 build_int_cst (TREE_TYPE (*vr0max), 1));
8527 else
8528 *vr0min = *vr0max;
8529 *vr0max = vr1max;
8531 else
8532 gcc_unreachable ();
8534 else if ((operand_less_p (*vr0min, vr1max) == 1
8535 || operand_equal_p (*vr0min, vr1max, 0))
8536 && operand_less_p (vr1min, *vr0min) == 1)
8538 /* ( [ ) ] or ( )[ ] */
8539 if (*vr0type == VR_ANTI_RANGE
8540 && vr1type == VR_ANTI_RANGE)
8541 *vr0min = vr1min;
8542 else if (*vr0type == VR_RANGE
8543 && vr1type == VR_RANGE)
8544 *vr0max = vr1max;
8545 else if (*vr0type == VR_RANGE
8546 && vr1type == VR_ANTI_RANGE)
8548 if (TREE_CODE (vr1max) == INTEGER_CST)
8549 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8550 build_int_cst (TREE_TYPE (vr1max), 1));
8551 else
8552 *vr0min = vr1max;
8554 else if (*vr0type == VR_ANTI_RANGE
8555 && vr1type == VR_RANGE)
8557 *vr0type = VR_RANGE;
8558 if (TREE_CODE (*vr0min) == INTEGER_CST)
8559 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8560 build_int_cst (TREE_TYPE (*vr0min), 1));
8561 else
8562 *vr0max = *vr0min;
8563 *vr0min = vr1min;
8565 else
8566 gcc_unreachable ();
8569 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8570 result for the intersection. That's always a conservative
8571 correct estimate unless VR1 is a constant singleton range
8572 in which case we choose that. */
8573 if (vr1type == VR_RANGE
8574 && is_gimple_min_invariant (vr1min)
8575 && vrp_operand_equal_p (vr1min, vr1max))
8577 *vr0type = vr1type;
8578 *vr0min = vr1min;
8579 *vr0max = vr1max;
8582 return;
8586 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8587 in *VR0. This may not be the smallest possible such range. */
8589 static void
8590 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8592 value_range saved;
8594 /* If either range is VR_VARYING the other one wins. */
8595 if (vr1->type == VR_VARYING)
8596 return;
8597 if (vr0->type == VR_VARYING)
8599 copy_value_range (vr0, vr1);
8600 return;
8603 /* When either range is VR_UNDEFINED the resulting range is
8604 VR_UNDEFINED, too. */
8605 if (vr0->type == VR_UNDEFINED)
8606 return;
8607 if (vr1->type == VR_UNDEFINED)
8609 set_value_range_to_undefined (vr0);
8610 return;
8613 /* Save the original vr0 so we can return it as conservative intersection
8614 result when our worker turns things to varying. */
8615 saved = *vr0;
8616 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8617 vr1->type, vr1->min, vr1->max);
8618 /* Make sure to canonicalize the result though as the inversion of a
8619 VR_RANGE can still be a VR_RANGE. */
8620 set_and_canonicalize_value_range (vr0, vr0->type,
8621 vr0->min, vr0->max, vr0->equiv);
8622 /* If that failed, use the saved original VR0. */
8623 if (vr0->type == VR_VARYING)
8625 *vr0 = saved;
8626 return;
8628 /* If the result is VR_UNDEFINED there is no need to mess with
8629 the equivalencies. */
8630 if (vr0->type == VR_UNDEFINED)
8631 return;
8633 /* The resulting set of equivalences for range intersection is the union of
8634 the two sets. */
8635 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8636 bitmap_ior_into (vr0->equiv, vr1->equiv);
8637 else if (vr1->equiv && !vr0->equiv)
8639 vr0->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
8640 bitmap_copy (vr0->equiv, vr1->equiv);
8644 void
8645 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8647 if (dump_file && (dump_flags & TDF_DETAILS))
8649 fprintf (dump_file, "Intersecting\n ");
8650 dump_value_range (dump_file, vr0);
8651 fprintf (dump_file, "\nand\n ");
8652 dump_value_range (dump_file, vr1);
8653 fprintf (dump_file, "\n");
8655 vrp_intersect_ranges_1 (vr0, vr1);
8656 if (dump_file && (dump_flags & TDF_DETAILS))
8658 fprintf (dump_file, "to\n ");
8659 dump_value_range (dump_file, vr0);
8660 fprintf (dump_file, "\n");
8664 /* Meet operation for value ranges. Given two value ranges VR0 and
8665 VR1, store in VR0 a range that contains both VR0 and VR1. This
8666 may not be the smallest possible such range. */
8668 static void
8669 vrp_meet_1 (value_range *vr0, const value_range *vr1)
8671 value_range saved;
8673 if (vr0->type == VR_UNDEFINED)
8675 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8676 return;
8679 if (vr1->type == VR_UNDEFINED)
8681 /* VR0 already has the resulting range. */
8682 return;
8685 if (vr0->type == VR_VARYING)
8687 /* Nothing to do. VR0 already has the resulting range. */
8688 return;
8691 if (vr1->type == VR_VARYING)
8693 set_value_range_to_varying (vr0);
8694 return;
8697 saved = *vr0;
8698 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8699 vr1->type, vr1->min, vr1->max);
8700 if (vr0->type == VR_VARYING)
8702 /* Failed to find an efficient meet. Before giving up and setting
8703 the result to VARYING, see if we can at least derive a useful
8704 anti-range. FIXME, all this nonsense about distinguishing
8705 anti-ranges from ranges is necessary because of the odd
8706 semantics of range_includes_zero_p and friends. */
8707 if (((saved.type == VR_RANGE
8708 && range_includes_zero_p (saved.min, saved.max) == 0)
8709 || (saved.type == VR_ANTI_RANGE
8710 && range_includes_zero_p (saved.min, saved.max) == 1))
8711 && ((vr1->type == VR_RANGE
8712 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8713 || (vr1->type == VR_ANTI_RANGE
8714 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8716 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8718 /* Since this meet operation did not result from the meeting of
8719 two equivalent names, VR0 cannot have any equivalences. */
8720 if (vr0->equiv)
8721 bitmap_clear (vr0->equiv);
8722 return;
8725 set_value_range_to_varying (vr0);
8726 return;
8728 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8729 vr0->equiv);
8730 if (vr0->type == VR_VARYING)
8731 return;
8733 /* The resulting set of equivalences is always the intersection of
8734 the two sets. */
8735 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8736 bitmap_and_into (vr0->equiv, vr1->equiv);
8737 else if (vr0->equiv && !vr1->equiv)
8738 bitmap_clear (vr0->equiv);
8741 void
8742 vrp_meet (value_range *vr0, const value_range *vr1)
8744 if (dump_file && (dump_flags & TDF_DETAILS))
8746 fprintf (dump_file, "Meeting\n ");
8747 dump_value_range (dump_file, vr0);
8748 fprintf (dump_file, "\nand\n ");
8749 dump_value_range (dump_file, vr1);
8750 fprintf (dump_file, "\n");
8752 vrp_meet_1 (vr0, vr1);
8753 if (dump_file && (dump_flags & TDF_DETAILS))
8755 fprintf (dump_file, "to\n ");
8756 dump_value_range (dump_file, vr0);
8757 fprintf (dump_file, "\n");
8762 /* Visit all arguments for PHI node PHI that flow through executable
8763 edges. If a valid value range can be derived from all the incoming
8764 value ranges, set a new range in VR_RESULT. */
8766 static void
8767 extract_range_from_phi_node (gphi *phi, value_range *vr_result)
8769 size_t i;
8770 tree lhs = PHI_RESULT (phi);
8771 value_range *lhs_vr = get_value_range (lhs);
8772 bool first = true;
8773 int edges, old_edges;
8774 struct loop *l;
8776 if (dump_file && (dump_flags & TDF_DETAILS))
8778 fprintf (dump_file, "\nVisiting PHI node: ");
8779 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8782 bool may_simulate_backedge_again = false;
8783 edges = 0;
8784 for (i = 0; i < gimple_phi_num_args (phi); i++)
8786 edge e = gimple_phi_arg_edge (phi, i);
8788 if (dump_file && (dump_flags & TDF_DETAILS))
8790 fprintf (dump_file,
8791 " Argument #%d (%d -> %d %sexecutable)\n",
8792 (int) i, e->src->index, e->dest->index,
8793 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8796 if (e->flags & EDGE_EXECUTABLE)
8798 tree arg = PHI_ARG_DEF (phi, i);
8799 value_range vr_arg;
8801 ++edges;
8803 if (TREE_CODE (arg) == SSA_NAME)
8805 /* See if we are eventually going to change one of the args. */
8806 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
8807 if (! gimple_nop_p (def_stmt)
8808 && prop_simulate_again_p (def_stmt)
8809 && e->flags & EDGE_DFS_BACK)
8810 may_simulate_backedge_again = true;
8812 vr_arg = *(get_value_range (arg));
8813 /* Do not allow equivalences or symbolic ranges to leak in from
8814 backedges. That creates invalid equivalencies.
8815 See PR53465 and PR54767. */
8816 if (e->flags & EDGE_DFS_BACK)
8818 if (vr_arg.type == VR_RANGE
8819 || vr_arg.type == VR_ANTI_RANGE)
8821 vr_arg.equiv = NULL;
8822 if (symbolic_range_p (&vr_arg))
8824 vr_arg.type = VR_VARYING;
8825 vr_arg.min = NULL_TREE;
8826 vr_arg.max = NULL_TREE;
8830 else
8832 /* If the non-backedge arguments range is VR_VARYING then
8833 we can still try recording a simple equivalence. */
8834 if (vr_arg.type == VR_VARYING)
8836 vr_arg.type = VR_RANGE;
8837 vr_arg.min = arg;
8838 vr_arg.max = arg;
8839 vr_arg.equiv = NULL;
8843 else
8845 if (TREE_OVERFLOW_P (arg))
8846 arg = drop_tree_overflow (arg);
8848 vr_arg.type = VR_RANGE;
8849 vr_arg.min = arg;
8850 vr_arg.max = arg;
8851 vr_arg.equiv = NULL;
8854 if (dump_file && (dump_flags & TDF_DETAILS))
8856 fprintf (dump_file, "\t");
8857 print_generic_expr (dump_file, arg, dump_flags);
8858 fprintf (dump_file, ": ");
8859 dump_value_range (dump_file, &vr_arg);
8860 fprintf (dump_file, "\n");
8863 if (first)
8864 copy_value_range (vr_result, &vr_arg);
8865 else
8866 vrp_meet (vr_result, &vr_arg);
8867 first = false;
8869 if (vr_result->type == VR_VARYING)
8870 break;
8874 if (vr_result->type == VR_VARYING)
8875 goto varying;
8876 else if (vr_result->type == VR_UNDEFINED)
8877 goto update_range;
8879 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8880 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8882 /* To prevent infinite iterations in the algorithm, derive ranges
8883 when the new value is slightly bigger or smaller than the
8884 previous one. We don't do this if we have seen a new executable
8885 edge; this helps us avoid an overflow infinity for conditionals
8886 which are not in a loop. If the old value-range was VR_UNDEFINED
8887 use the updated range and iterate one more time. If we will not
8888 simulate this PHI again via the backedge allow us to iterate. */
8889 if (edges > 0
8890 && gimple_phi_num_args (phi) > 1
8891 && edges == old_edges
8892 && lhs_vr->type != VR_UNDEFINED
8893 && may_simulate_backedge_again)
8895 /* Compare old and new ranges, fall back to varying if the
8896 values are not comparable. */
8897 int cmp_min = compare_values (lhs_vr->min, vr_result->min);
8898 if (cmp_min == -2)
8899 goto varying;
8900 int cmp_max = compare_values (lhs_vr->max, vr_result->max);
8901 if (cmp_max == -2)
8902 goto varying;
8904 /* For non VR_RANGE or for pointers fall back to varying if
8905 the range changed. */
8906 if ((lhs_vr->type != VR_RANGE || vr_result->type != VR_RANGE
8907 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8908 && (cmp_min != 0 || cmp_max != 0))
8909 goto varying;
8911 /* If the new minimum is larger than the previous one
8912 retain the old value. If the new minimum value is smaller
8913 than the previous one and not -INF go all the way to -INF + 1.
8914 In the first case, to avoid infinite bouncing between different
8915 minimums, and in the other case to avoid iterating millions of
8916 times to reach -INF. Going to -INF + 1 also lets the following
8917 iteration compute whether there will be any overflow, at the
8918 expense of one additional iteration. */
8919 if (cmp_min < 0)
8920 vr_result->min = lhs_vr->min;
8921 else if (cmp_min > 0
8922 && !vrp_val_is_min (vr_result->min))
8923 vr_result->min
8924 = int_const_binop (PLUS_EXPR,
8925 vrp_val_min (TREE_TYPE (vr_result->min)),
8926 build_int_cst (TREE_TYPE (vr_result->min), 1));
8928 /* Similarly for the maximum value. */
8929 if (cmp_max > 0)
8930 vr_result->max = lhs_vr->max;
8931 else if (cmp_max < 0
8932 && !vrp_val_is_max (vr_result->max))
8933 vr_result->max
8934 = int_const_binop (MINUS_EXPR,
8935 vrp_val_max (TREE_TYPE (vr_result->min)),
8936 build_int_cst (TREE_TYPE (vr_result->min), 1));
8938 /* If we dropped either bound to +-INF then if this is a loop
8939 PHI node SCEV may known more about its value-range. */
8940 if (cmp_min > 0 || cmp_min < 0
8941 || cmp_max < 0 || cmp_max > 0)
8942 goto scev_check;
8944 goto infinite_check;
8947 goto update_range;
8949 varying:
8950 set_value_range_to_varying (vr_result);
8952 scev_check:
8953 /* If this is a loop PHI node SCEV may known more about its value-range.
8954 scev_check can be reached from two paths, one is a fall through from above
8955 "varying" label, the other is direct goto from code block which tries to
8956 avoid infinite simulation. */
8957 if ((l = loop_containing_stmt (phi))
8958 && l->header == gimple_bb (phi))
8959 adjust_range_with_scev (vr_result, l, phi, lhs);
8961 infinite_check:
8962 /* If we will end up with a (-INF, +INF) range, set it to
8963 VARYING. Same if the previous max value was invalid for
8964 the type and we end up with vr_result.min > vr_result.max. */
8965 if ((vr_result->type == VR_RANGE || vr_result->type == VR_ANTI_RANGE)
8966 && !((vrp_val_is_max (vr_result->max) && vrp_val_is_min (vr_result->min))
8967 || compare_values (vr_result->min, vr_result->max) > 0))
8969 else
8970 set_value_range_to_varying (vr_result);
8972 /* If the new range is different than the previous value, keep
8973 iterating. */
8974 update_range:
8975 return;
8978 /* Visit all arguments for PHI node PHI that flow through executable
8979 edges. If a valid value range can be derived from all the incoming
8980 value ranges, set a new range for the LHS of PHI. */
8982 static enum ssa_prop_result
8983 vrp_visit_phi_node (gphi *phi)
8985 tree lhs = PHI_RESULT (phi);
8986 value_range vr_result = VR_INITIALIZER;
8987 extract_range_from_phi_node (phi, &vr_result);
8988 if (update_value_range (lhs, &vr_result))
8990 if (dump_file && (dump_flags & TDF_DETAILS))
8992 fprintf (dump_file, "Found new range for ");
8993 print_generic_expr (dump_file, lhs, 0);
8994 fprintf (dump_file, ": ");
8995 dump_value_range (dump_file, &vr_result);
8996 fprintf (dump_file, "\n");
8999 if (vr_result.type == VR_VARYING)
9000 return SSA_PROP_VARYING;
9002 return SSA_PROP_INTERESTING;
9005 /* Nothing changed, don't add outgoing edges. */
9006 return SSA_PROP_NOT_INTERESTING;
9009 /* Simplify boolean operations if the source is known
9010 to be already a boolean. */
9011 static bool
9012 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9014 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9015 tree lhs, op0, op1;
9016 bool need_conversion;
9018 /* We handle only !=/== case here. */
9019 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
9021 op0 = gimple_assign_rhs1 (stmt);
9022 if (!op_with_boolean_value_range_p (op0))
9023 return false;
9025 op1 = gimple_assign_rhs2 (stmt);
9026 if (!op_with_boolean_value_range_p (op1))
9027 return false;
9029 /* Reduce number of cases to handle to NE_EXPR. As there is no
9030 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9031 if (rhs_code == EQ_EXPR)
9033 if (TREE_CODE (op1) == INTEGER_CST)
9034 op1 = int_const_binop (BIT_XOR_EXPR, op1,
9035 build_int_cst (TREE_TYPE (op1), 1));
9036 else
9037 return false;
9040 lhs = gimple_assign_lhs (stmt);
9041 need_conversion
9042 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
9044 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9045 if (need_conversion
9046 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9047 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9048 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9049 return false;
9051 /* For A != 0 we can substitute A itself. */
9052 if (integer_zerop (op1))
9053 gimple_assign_set_rhs_with_ops (gsi,
9054 need_conversion
9055 ? NOP_EXPR : TREE_CODE (op0), op0);
9056 /* For A != B we substitute A ^ B. Either with conversion. */
9057 else if (need_conversion)
9059 tree tem = make_ssa_name (TREE_TYPE (op0));
9060 gassign *newop
9061 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9062 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9063 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9064 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
9065 set_range_info (tem, VR_RANGE,
9066 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
9067 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
9068 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9070 /* Or without. */
9071 else
9072 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9073 update_stmt (gsi_stmt (*gsi));
9074 fold_stmt (gsi, follow_single_use_edges);
9076 return true;
9079 /* Simplify a division or modulo operator to a right shift or
9080 bitwise and if the first operand is unsigned or is greater
9081 than zero and the second operand is an exact power of two.
9082 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9083 into just op0 if op0's range is known to be a subset of
9084 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9085 modulo. */
9087 static bool
9088 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9090 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9091 tree val = NULL;
9092 tree op0 = gimple_assign_rhs1 (stmt);
9093 tree op1 = gimple_assign_rhs2 (stmt);
9094 value_range *vr = get_value_range (op0);
9096 if (rhs_code == TRUNC_MOD_EXPR
9097 && TREE_CODE (op1) == INTEGER_CST
9098 && tree_int_cst_sgn (op1) == 1
9099 && range_int_cst_p (vr)
9100 && tree_int_cst_lt (vr->max, op1))
9102 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9103 || tree_int_cst_sgn (vr->min) >= 0
9104 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9105 vr->min))
9107 /* If op0 already has the range op0 % op1 has,
9108 then TRUNC_MOD_EXPR won't change anything. */
9109 gimple_assign_set_rhs_from_tree (gsi, op0);
9110 return true;
9114 if (!integer_pow2p (op1))
9116 /* X % -Y can be only optimized into X % Y either if
9117 X is not INT_MIN, or Y is not -1. Fold it now, as after
9118 remove_range_assertions the range info might be not available
9119 anymore. */
9120 if (rhs_code == TRUNC_MOD_EXPR
9121 && fold_stmt (gsi, follow_single_use_edges))
9122 return true;
9123 return false;
9126 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9127 val = integer_one_node;
9128 else
9130 bool sop = false;
9132 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9134 if (val
9135 && sop
9136 && integer_onep (val)
9137 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9139 location_t location;
9141 if (!gimple_has_location (stmt))
9142 location = input_location;
9143 else
9144 location = gimple_location (stmt);
9145 warning_at (location, OPT_Wstrict_overflow,
9146 "assuming signed overflow does not occur when "
9147 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9151 if (val && integer_onep (val))
9153 tree t;
9155 if (rhs_code == TRUNC_DIV_EXPR)
9157 t = build_int_cst (integer_type_node, tree_log2 (op1));
9158 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9159 gimple_assign_set_rhs1 (stmt, op0);
9160 gimple_assign_set_rhs2 (stmt, t);
9162 else
9164 t = build_int_cst (TREE_TYPE (op1), 1);
9165 t = int_const_binop (MINUS_EXPR, op1, t);
9166 t = fold_convert (TREE_TYPE (op0), t);
9168 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9169 gimple_assign_set_rhs1 (stmt, op0);
9170 gimple_assign_set_rhs2 (stmt, t);
9173 update_stmt (stmt);
9174 fold_stmt (gsi, follow_single_use_edges);
9175 return true;
9178 return false;
9181 /* Simplify a min or max if the ranges of the two operands are
9182 disjoint. Return true if we do simplify. */
9184 static bool
9185 simplify_min_or_max_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9187 tree op0 = gimple_assign_rhs1 (stmt);
9188 tree op1 = gimple_assign_rhs2 (stmt);
9189 bool sop = false;
9190 tree val;
9192 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9193 (LE_EXPR, op0, op1, &sop));
9194 if (!val)
9196 sop = false;
9197 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9198 (LT_EXPR, op0, op1, &sop));
9201 if (val)
9203 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9205 location_t location;
9207 if (!gimple_has_location (stmt))
9208 location = input_location;
9209 else
9210 location = gimple_location (stmt);
9211 warning_at (location, OPT_Wstrict_overflow,
9212 "assuming signed overflow does not occur when "
9213 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9216 /* VAL == TRUE -> OP0 < or <= op1
9217 VAL == FALSE -> OP0 > or >= op1. */
9218 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9219 == integer_zerop (val)) ? op0 : op1;
9220 gimple_assign_set_rhs_from_tree (gsi, res);
9221 return true;
9224 return false;
9227 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9228 ABS_EXPR. If the operand is <= 0, then simplify the
9229 ABS_EXPR into a NEGATE_EXPR. */
9231 static bool
9232 simplify_abs_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9234 tree op = gimple_assign_rhs1 (stmt);
9235 value_range *vr = get_value_range (op);
9237 if (vr)
9239 tree val = NULL;
9240 bool sop = false;
9242 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9243 if (!val)
9245 /* The range is neither <= 0 nor > 0. Now see if it is
9246 either < 0 or >= 0. */
9247 sop = false;
9248 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9249 &sop);
9252 if (val)
9254 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9256 location_t location;
9258 if (!gimple_has_location (stmt))
9259 location = input_location;
9260 else
9261 location = gimple_location (stmt);
9262 warning_at (location, OPT_Wstrict_overflow,
9263 "assuming signed overflow does not occur when "
9264 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9267 gimple_assign_set_rhs1 (stmt, op);
9268 if (integer_zerop (val))
9269 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9270 else
9271 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9272 update_stmt (stmt);
9273 fold_stmt (gsi, follow_single_use_edges);
9274 return true;
9278 return false;
9281 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9282 If all the bits that are being cleared by & are already
9283 known to be zero from VR, or all the bits that are being
9284 set by | are already known to be one from VR, the bit
9285 operation is redundant. */
9287 static bool
9288 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9290 tree op0 = gimple_assign_rhs1 (stmt);
9291 tree op1 = gimple_assign_rhs2 (stmt);
9292 tree op = NULL_TREE;
9293 value_range vr0 = VR_INITIALIZER;
9294 value_range vr1 = VR_INITIALIZER;
9295 wide_int may_be_nonzero0, may_be_nonzero1;
9296 wide_int must_be_nonzero0, must_be_nonzero1;
9297 wide_int mask;
9299 if (TREE_CODE (op0) == SSA_NAME)
9300 vr0 = *(get_value_range (op0));
9301 else if (is_gimple_min_invariant (op0))
9302 set_value_range_to_value (&vr0, op0, NULL);
9303 else
9304 return false;
9306 if (TREE_CODE (op1) == SSA_NAME)
9307 vr1 = *(get_value_range (op1));
9308 else if (is_gimple_min_invariant (op1))
9309 set_value_range_to_value (&vr1, op1, NULL);
9310 else
9311 return false;
9313 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9314 &must_be_nonzero0))
9315 return false;
9316 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9317 &must_be_nonzero1))
9318 return false;
9320 switch (gimple_assign_rhs_code (stmt))
9322 case BIT_AND_EXPR:
9323 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9324 if (mask == 0)
9326 op = op0;
9327 break;
9329 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9330 if (mask == 0)
9332 op = op1;
9333 break;
9335 break;
9336 case BIT_IOR_EXPR:
9337 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9338 if (mask == 0)
9340 op = op1;
9341 break;
9343 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9344 if (mask == 0)
9346 op = op0;
9347 break;
9349 break;
9350 default:
9351 gcc_unreachable ();
9354 if (op == NULL_TREE)
9355 return false;
9357 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9358 update_stmt (gsi_stmt (*gsi));
9359 return true;
9362 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9363 a known value range VR.
9365 If there is one and only one value which will satisfy the
9366 conditional, then return that value. Else return NULL.
9368 If signed overflow must be undefined for the value to satisfy
9369 the conditional, then set *STRICT_OVERFLOW_P to true. */
9371 static tree
9372 test_for_singularity (enum tree_code cond_code, tree op0,
9373 tree op1, value_range *vr,
9374 bool *strict_overflow_p)
9376 tree min = NULL;
9377 tree max = NULL;
9379 /* Extract minimum/maximum values which satisfy the conditional as it was
9380 written. */
9381 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9383 /* This should not be negative infinity; there is no overflow
9384 here. */
9385 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9387 max = op1;
9388 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9390 tree one = build_int_cst (TREE_TYPE (op0), 1);
9391 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9392 if (EXPR_P (max))
9393 TREE_NO_WARNING (max) = 1;
9396 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9398 /* This should not be positive infinity; there is no overflow
9399 here. */
9400 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9402 min = op1;
9403 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9405 tree one = build_int_cst (TREE_TYPE (op0), 1);
9406 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9407 if (EXPR_P (min))
9408 TREE_NO_WARNING (min) = 1;
9412 /* Now refine the minimum and maximum values using any
9413 value range information we have for op0. */
9414 if (min && max)
9416 if (compare_values (vr->min, min) == 1)
9417 min = vr->min;
9418 if (compare_values (vr->max, max) == -1)
9419 max = vr->max;
9421 /* If the new min/max values have converged to a single value,
9422 then there is only one value which can satisfy the condition,
9423 return that value. */
9424 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9426 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9427 && is_overflow_infinity (vr->max))
9428 *strict_overflow_p = true;
9429 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9430 && is_overflow_infinity (vr->min))
9431 *strict_overflow_p = true;
9433 return min;
9436 return NULL;
9439 /* Return whether the value range *VR fits in an integer type specified
9440 by PRECISION and UNSIGNED_P. */
9442 static bool
9443 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9445 tree src_type;
9446 unsigned src_precision;
9447 widest_int tem;
9448 signop src_sgn;
9450 /* We can only handle integral and pointer types. */
9451 src_type = TREE_TYPE (vr->min);
9452 if (!INTEGRAL_TYPE_P (src_type)
9453 && !POINTER_TYPE_P (src_type))
9454 return false;
9456 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9457 and so is an identity transform. */
9458 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9459 src_sgn = TYPE_SIGN (src_type);
9460 if ((src_precision < dest_precision
9461 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9462 || (src_precision == dest_precision && src_sgn == dest_sgn))
9463 return true;
9465 /* Now we can only handle ranges with constant bounds. */
9466 if (vr->type != VR_RANGE
9467 || TREE_CODE (vr->min) != INTEGER_CST
9468 || TREE_CODE (vr->max) != INTEGER_CST)
9469 return false;
9471 /* For sign changes, the MSB of the wide_int has to be clear.
9472 An unsigned value with its MSB set cannot be represented by
9473 a signed wide_int, while a negative value cannot be represented
9474 by an unsigned wide_int. */
9475 if (src_sgn != dest_sgn
9476 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9477 return false;
9479 /* Then we can perform the conversion on both ends and compare
9480 the result for equality. */
9481 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9482 if (tem != wi::to_widest (vr->min))
9483 return false;
9484 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9485 if (tem != wi::to_widest (vr->max))
9486 return false;
9488 return true;
9491 /* Simplify a conditional using a relational operator to an equality
9492 test if the range information indicates only one value can satisfy
9493 the original conditional. */
9495 static bool
9496 simplify_cond_using_ranges (gcond *stmt)
9498 tree op0 = gimple_cond_lhs (stmt);
9499 tree op1 = gimple_cond_rhs (stmt);
9500 enum tree_code cond_code = gimple_cond_code (stmt);
9502 if (cond_code != NE_EXPR
9503 && cond_code != EQ_EXPR
9504 && TREE_CODE (op0) == SSA_NAME
9505 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9506 && is_gimple_min_invariant (op1))
9508 value_range *vr = get_value_range (op0);
9510 /* If we have range information for OP0, then we might be
9511 able to simplify this conditional. */
9512 if (vr->type == VR_RANGE)
9514 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9515 bool sop = false;
9516 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9518 if (new_tree
9519 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9521 if (dump_file)
9523 fprintf (dump_file, "Simplified relational ");
9524 print_gimple_stmt (dump_file, stmt, 0, 0);
9525 fprintf (dump_file, " into ");
9528 gimple_cond_set_code (stmt, EQ_EXPR);
9529 gimple_cond_set_lhs (stmt, op0);
9530 gimple_cond_set_rhs (stmt, new_tree);
9532 update_stmt (stmt);
9534 if (dump_file)
9536 print_gimple_stmt (dump_file, stmt, 0, 0);
9537 fprintf (dump_file, "\n");
9540 if (sop && issue_strict_overflow_warning (wc))
9542 location_t location = input_location;
9543 if (gimple_has_location (stmt))
9544 location = gimple_location (stmt);
9546 warning_at (location, OPT_Wstrict_overflow,
9547 "assuming signed overflow does not occur when "
9548 "simplifying conditional");
9551 return true;
9554 /* Try again after inverting the condition. We only deal
9555 with integral types here, so no need to worry about
9556 issues with inverting FP comparisons. */
9557 sop = false;
9558 new_tree = test_for_singularity
9559 (invert_tree_comparison (cond_code, false),
9560 op0, op1, vr, &sop);
9562 if (new_tree
9563 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9565 if (dump_file)
9567 fprintf (dump_file, "Simplified relational ");
9568 print_gimple_stmt (dump_file, stmt, 0, 0);
9569 fprintf (dump_file, " into ");
9572 gimple_cond_set_code (stmt, NE_EXPR);
9573 gimple_cond_set_lhs (stmt, op0);
9574 gimple_cond_set_rhs (stmt, new_tree);
9576 update_stmt (stmt);
9578 if (dump_file)
9580 print_gimple_stmt (dump_file, stmt, 0, 0);
9581 fprintf (dump_file, "\n");
9584 if (sop && issue_strict_overflow_warning (wc))
9586 location_t location = input_location;
9587 if (gimple_has_location (stmt))
9588 location = gimple_location (stmt);
9590 warning_at (location, OPT_Wstrict_overflow,
9591 "assuming signed overflow does not occur when "
9592 "simplifying conditional");
9595 return true;
9600 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9601 see if OP0 was set by a type conversion where the source of
9602 the conversion is another SSA_NAME with a range that fits
9603 into the range of OP0's type.
9605 If so, the conversion is redundant as the earlier SSA_NAME can be
9606 used for the comparison directly if we just massage the constant in the
9607 comparison. */
9608 if (TREE_CODE (op0) == SSA_NAME
9609 && TREE_CODE (op1) == INTEGER_CST)
9611 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9612 tree innerop;
9614 if (!is_gimple_assign (def_stmt)
9615 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9616 return false;
9618 innerop = gimple_assign_rhs1 (def_stmt);
9620 if (TREE_CODE (innerop) == SSA_NAME
9621 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9622 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9623 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9625 value_range *vr = get_value_range (innerop);
9627 if (range_int_cst_p (vr)
9628 && range_fits_type_p (vr,
9629 TYPE_PRECISION (TREE_TYPE (op0)),
9630 TYPE_SIGN (TREE_TYPE (op0)))
9631 && int_fits_type_p (op1, TREE_TYPE (innerop))
9632 /* The range must not have overflowed, or if it did overflow
9633 we must not be wrapping/trapping overflow and optimizing
9634 with strict overflow semantics. */
9635 && ((!is_negative_overflow_infinity (vr->min)
9636 && !is_positive_overflow_infinity (vr->max))
9637 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9639 /* If the range overflowed and the user has asked for warnings
9640 when strict overflow semantics were used to optimize code,
9641 issue an appropriate warning. */
9642 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9643 && (is_negative_overflow_infinity (vr->min)
9644 || is_positive_overflow_infinity (vr->max))
9645 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9647 location_t location;
9649 if (!gimple_has_location (stmt))
9650 location = input_location;
9651 else
9652 location = gimple_location (stmt);
9653 warning_at (location, OPT_Wstrict_overflow,
9654 "assuming signed overflow does not occur when "
9655 "simplifying conditional");
9658 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9659 gimple_cond_set_lhs (stmt, innerop);
9660 gimple_cond_set_rhs (stmt, newconst);
9661 return true;
9666 return false;
9669 /* Simplify a switch statement using the value range of the switch
9670 argument. */
9672 static bool
9673 simplify_switch_using_ranges (gswitch *stmt)
9675 tree op = gimple_switch_index (stmt);
9676 value_range *vr = NULL;
9677 bool take_default;
9678 edge e;
9679 edge_iterator ei;
9680 size_t i = 0, j = 0, n, n2;
9681 tree vec2;
9682 switch_update su;
9683 size_t k = 1, l = 0;
9685 if (TREE_CODE (op) == SSA_NAME)
9687 vr = get_value_range (op);
9689 /* We can only handle integer ranges. */
9690 if ((vr->type != VR_RANGE
9691 && vr->type != VR_ANTI_RANGE)
9692 || symbolic_range_p (vr))
9693 return false;
9695 /* Find case label for min/max of the value range. */
9696 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9698 else if (TREE_CODE (op) == INTEGER_CST)
9700 take_default = !find_case_label_index (stmt, 1, op, &i);
9701 if (take_default)
9703 i = 1;
9704 j = 0;
9706 else
9708 j = i;
9711 else
9712 return false;
9714 n = gimple_switch_num_labels (stmt);
9716 /* We can truncate the case label ranges that partially overlap with OP's
9717 value range. */
9718 size_t min_idx = 1, max_idx = 0;
9719 if (vr != NULL)
9720 find_case_label_range (stmt, vr->min, vr->max, &min_idx, &max_idx);
9721 if (min_idx <= max_idx)
9723 tree min_label = gimple_switch_label (stmt, min_idx);
9724 tree max_label = gimple_switch_label (stmt, max_idx);
9726 /* Avoid changing the type of the case labels when truncating. */
9727 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
9728 tree vr_min = fold_convert (case_label_type, vr->min);
9729 tree vr_max = fold_convert (case_label_type, vr->max);
9731 if (vr->type == VR_RANGE)
9733 /* If OP's value range is [2,8] and the low label range is
9734 0 ... 3, truncate the label's range to 2 .. 3. */
9735 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9736 && CASE_HIGH (min_label) != NULL_TREE
9737 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9738 CASE_LOW (min_label) = vr_min;
9740 /* If OP's value range is [2,8] and the high label range is
9741 7 ... 10, truncate the label's range to 7 .. 8. */
9742 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9743 && CASE_HIGH (max_label) != NULL_TREE
9744 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9745 CASE_HIGH (max_label) = vr_max;
9747 else if (vr->type == VR_ANTI_RANGE)
9749 tree one_cst = build_one_cst (case_label_type);
9751 if (min_label == max_label)
9753 /* If OP's value range is ~[7,8] and the label's range is
9754 7 ... 10, truncate the label's range to 9 ... 10. */
9755 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
9756 && CASE_HIGH (min_label) != NULL_TREE
9757 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
9758 CASE_LOW (min_label)
9759 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9761 /* If OP's value range is ~[7,8] and the label's range is
9762 5 ... 8, truncate the label's range to 5 ... 6. */
9763 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9764 && CASE_HIGH (min_label) != NULL_TREE
9765 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
9766 CASE_HIGH (min_label)
9767 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9769 else
9771 /* If OP's value range is ~[2,8] and the low label range is
9772 0 ... 3, truncate the label's range to 0 ... 1. */
9773 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9774 && CASE_HIGH (min_label) != NULL_TREE
9775 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9776 CASE_HIGH (min_label)
9777 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9779 /* If OP's value range is ~[2,8] and the high label range is
9780 7 ... 10, truncate the label's range to 9 ... 10. */
9781 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9782 && CASE_HIGH (max_label) != NULL_TREE
9783 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9784 CASE_LOW (max_label)
9785 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9789 /* Canonicalize singleton case ranges. */
9790 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
9791 CASE_HIGH (min_label) = NULL_TREE;
9792 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
9793 CASE_HIGH (max_label) = NULL_TREE;
9796 /* We can also eliminate case labels that lie completely outside OP's value
9797 range. */
9799 /* Bail out if this is just all edges taken. */
9800 if (i == 1
9801 && j == n - 1
9802 && take_default)
9803 return false;
9805 /* Build a new vector of taken case labels. */
9806 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9807 n2 = 0;
9809 /* Add the default edge, if necessary. */
9810 if (take_default)
9811 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9813 for (; i <= j; ++i, ++n2)
9814 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9816 for (; k <= l; ++k, ++n2)
9817 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9819 /* Mark needed edges. */
9820 for (i = 0; i < n2; ++i)
9822 e = find_edge (gimple_bb (stmt),
9823 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9824 e->aux = (void *)-1;
9827 /* Queue not needed edges for later removal. */
9828 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9830 if (e->aux == (void *)-1)
9832 e->aux = NULL;
9833 continue;
9836 if (dump_file && (dump_flags & TDF_DETAILS))
9838 fprintf (dump_file, "removing unreachable case label\n");
9840 to_remove_edges.safe_push (e);
9841 e->flags &= ~EDGE_EXECUTABLE;
9844 /* And queue an update for the stmt. */
9845 su.stmt = stmt;
9846 su.vec = vec2;
9847 to_update_switch_stmts.safe_push (su);
9848 return false;
9851 /* Simplify an integral conversion from an SSA name in STMT. */
9853 static bool
9854 simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9856 tree innerop, middleop, finaltype;
9857 gimple *def_stmt;
9858 signop inner_sgn, middle_sgn, final_sgn;
9859 unsigned inner_prec, middle_prec, final_prec;
9860 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9862 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9863 if (!INTEGRAL_TYPE_P (finaltype))
9864 return false;
9865 middleop = gimple_assign_rhs1 (stmt);
9866 def_stmt = SSA_NAME_DEF_STMT (middleop);
9867 if (!is_gimple_assign (def_stmt)
9868 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9869 return false;
9870 innerop = gimple_assign_rhs1 (def_stmt);
9871 if (TREE_CODE (innerop) != SSA_NAME
9872 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9873 return false;
9875 /* Get the value-range of the inner operand. Use get_range_info in
9876 case innerop was created during substitute-and-fold. */
9877 wide_int imin, imax;
9878 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
9879 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
9880 return false;
9881 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
9882 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
9884 /* Simulate the conversion chain to check if the result is equal if
9885 the middle conversion is removed. */
9886 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9887 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9888 final_prec = TYPE_PRECISION (finaltype);
9890 /* If the first conversion is not injective, the second must not
9891 be widening. */
9892 if (wi::gtu_p (innermax - innermin,
9893 wi::mask <widest_int> (middle_prec, false))
9894 && middle_prec < final_prec)
9895 return false;
9896 /* We also want a medium value so that we can track the effect that
9897 narrowing conversions with sign change have. */
9898 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9899 if (inner_sgn == UNSIGNED)
9900 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9901 else
9902 innermed = 0;
9903 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9904 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9905 innermed = innermin;
9907 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9908 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9909 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9910 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9912 /* Require that the final conversion applied to both the original
9913 and the intermediate range produces the same result. */
9914 final_sgn = TYPE_SIGN (finaltype);
9915 if (wi::ext (middlemin, final_prec, final_sgn)
9916 != wi::ext (innermin, final_prec, final_sgn)
9917 || wi::ext (middlemed, final_prec, final_sgn)
9918 != wi::ext (innermed, final_prec, final_sgn)
9919 || wi::ext (middlemax, final_prec, final_sgn)
9920 != wi::ext (innermax, final_prec, final_sgn))
9921 return false;
9923 gimple_assign_set_rhs1 (stmt, innerop);
9924 fold_stmt (gsi, follow_single_use_edges);
9925 return true;
9928 /* Simplify a conversion from integral SSA name to float in STMT. */
9930 static bool
9931 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9932 gimple *stmt)
9934 tree rhs1 = gimple_assign_rhs1 (stmt);
9935 value_range *vr = get_value_range (rhs1);
9936 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9937 machine_mode mode;
9938 tree tem;
9939 gassign *conv;
9941 /* We can only handle constant ranges. */
9942 if (vr->type != VR_RANGE
9943 || TREE_CODE (vr->min) != INTEGER_CST
9944 || TREE_CODE (vr->max) != INTEGER_CST)
9945 return false;
9947 /* First check if we can use a signed type in place of an unsigned. */
9948 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9949 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9950 != CODE_FOR_nothing)
9951 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9952 mode = TYPE_MODE (TREE_TYPE (rhs1));
9953 /* If we can do the conversion in the current input mode do nothing. */
9954 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9955 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9956 return false;
9957 /* Otherwise search for a mode we can use, starting from the narrowest
9958 integer mode available. */
9959 else
9961 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9964 /* If we cannot do a signed conversion to float from mode
9965 or if the value-range does not fit in the signed type
9966 try with a wider mode. */
9967 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9968 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9969 break;
9971 mode = GET_MODE_WIDER_MODE (mode);
9972 /* But do not widen the input. Instead leave that to the
9973 optabs expansion code. */
9974 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9975 return false;
9977 while (mode != VOIDmode);
9978 if (mode == VOIDmode)
9979 return false;
9982 /* It works, insert a truncation or sign-change before the
9983 float conversion. */
9984 tem = make_ssa_name (build_nonstandard_integer_type
9985 (GET_MODE_PRECISION (mode), 0));
9986 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9987 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9988 gimple_assign_set_rhs1 (stmt, tem);
9989 fold_stmt (gsi, follow_single_use_edges);
9991 return true;
9994 /* Simplify an internal fn call using ranges if possible. */
9996 static bool
9997 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9999 enum tree_code subcode;
10000 bool is_ubsan = false;
10001 bool ovf = false;
10002 switch (gimple_call_internal_fn (stmt))
10004 case IFN_UBSAN_CHECK_ADD:
10005 subcode = PLUS_EXPR;
10006 is_ubsan = true;
10007 break;
10008 case IFN_UBSAN_CHECK_SUB:
10009 subcode = MINUS_EXPR;
10010 is_ubsan = true;
10011 break;
10012 case IFN_UBSAN_CHECK_MUL:
10013 subcode = MULT_EXPR;
10014 is_ubsan = true;
10015 break;
10016 case IFN_ADD_OVERFLOW:
10017 subcode = PLUS_EXPR;
10018 break;
10019 case IFN_SUB_OVERFLOW:
10020 subcode = MINUS_EXPR;
10021 break;
10022 case IFN_MUL_OVERFLOW:
10023 subcode = MULT_EXPR;
10024 break;
10025 default:
10026 return false;
10029 tree op0 = gimple_call_arg (stmt, 0);
10030 tree op1 = gimple_call_arg (stmt, 1);
10031 tree type;
10032 if (is_ubsan)
10033 type = TREE_TYPE (op0);
10034 else if (gimple_call_lhs (stmt) == NULL_TREE)
10035 return false;
10036 else
10037 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
10038 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
10039 || (is_ubsan && ovf))
10040 return false;
10042 gimple *g;
10043 location_t loc = gimple_location (stmt);
10044 if (is_ubsan)
10045 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
10046 else
10048 int prec = TYPE_PRECISION (type);
10049 tree utype = type;
10050 if (ovf
10051 || !useless_type_conversion_p (type, TREE_TYPE (op0))
10052 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
10053 utype = build_nonstandard_integer_type (prec, 1);
10054 if (TREE_CODE (op0) == INTEGER_CST)
10055 op0 = fold_convert (utype, op0);
10056 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
10058 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
10059 gimple_set_location (g, loc);
10060 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10061 op0 = gimple_assign_lhs (g);
10063 if (TREE_CODE (op1) == INTEGER_CST)
10064 op1 = fold_convert (utype, op1);
10065 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
10067 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
10068 gimple_set_location (g, loc);
10069 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10070 op1 = gimple_assign_lhs (g);
10072 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
10073 gimple_set_location (g, loc);
10074 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10075 if (utype != type)
10077 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
10078 gimple_assign_lhs (g));
10079 gimple_set_location (g, loc);
10080 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10082 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
10083 gimple_assign_lhs (g),
10084 build_int_cst (type, ovf));
10086 gimple_set_location (g, loc);
10087 gsi_replace (gsi, g, false);
10088 return true;
10091 /* Return true if VAR is a two-valued variable. Set a and b with the
10092 two-values when it is true. Return false otherwise. */
10094 static bool
10095 two_valued_val_range_p (tree var, tree *a, tree *b)
10097 value_range *vr = get_value_range (var);
10098 if ((vr->type != VR_RANGE
10099 && vr->type != VR_ANTI_RANGE)
10100 || TREE_CODE (vr->min) != INTEGER_CST
10101 || TREE_CODE (vr->max) != INTEGER_CST)
10102 return false;
10104 if (vr->type == VR_RANGE
10105 && wi::sub (vr->max, vr->min) == 1)
10107 *a = vr->min;
10108 *b = vr->max;
10109 return true;
10112 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10113 if (vr->type == VR_ANTI_RANGE
10114 && wi::sub (vr->min, vrp_val_min (TREE_TYPE (var))) == 1
10115 && wi::sub (vrp_val_max (TREE_TYPE (var)), vr->max) == 1)
10117 *a = vrp_val_min (TREE_TYPE (var));
10118 *b = vrp_val_max (TREE_TYPE (var));
10119 return true;
10122 return false;
10125 /* Simplify STMT using ranges if possible. */
10127 static bool
10128 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
10130 gimple *stmt = gsi_stmt (*gsi);
10131 if (is_gimple_assign (stmt))
10133 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
10134 tree rhs1 = gimple_assign_rhs1 (stmt);
10135 tree rhs2 = gimple_assign_rhs2 (stmt);
10136 tree lhs = gimple_assign_lhs (stmt);
10137 tree val1 = NULL_TREE, val2 = NULL_TREE;
10138 use_operand_p use_p;
10139 gimple *use_stmt;
10141 /* Convert:
10142 LHS = CST BINOP VAR
10143 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10145 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10147 Also handles:
10148 LHS = VAR BINOP CST
10149 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10151 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10153 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
10154 && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10155 && ((TREE_CODE (rhs1) == INTEGER_CST
10156 && TREE_CODE (rhs2) == SSA_NAME)
10157 || (TREE_CODE (rhs2) == INTEGER_CST
10158 && TREE_CODE (rhs1) == SSA_NAME))
10159 && single_imm_use (lhs, &use_p, &use_stmt)
10160 && gimple_code (use_stmt) == GIMPLE_COND)
10163 tree new_rhs1 = NULL_TREE;
10164 tree new_rhs2 = NULL_TREE;
10165 tree cmp_var = NULL_TREE;
10167 if (TREE_CODE (rhs2) == SSA_NAME
10168 && two_valued_val_range_p (rhs2, &val1, &val2))
10170 /* Optimize RHS1 OP [VAL1, VAL2]. */
10171 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
10172 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
10173 cmp_var = rhs2;
10175 else if (TREE_CODE (rhs1) == SSA_NAME
10176 && two_valued_val_range_p (rhs1, &val1, &val2))
10178 /* Optimize [VAL1, VAL2] OP RHS2. */
10179 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
10180 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
10181 cmp_var = rhs1;
10184 /* If we could not find two-vals or the optimzation is invalid as
10185 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10186 if (new_rhs1 && new_rhs2)
10188 tree cond = build2 (EQ_EXPR, TREE_TYPE (cmp_var), cmp_var, val1);
10189 gimple_assign_set_rhs_with_ops (gsi,
10190 COND_EXPR, cond,
10191 new_rhs1,
10192 new_rhs2);
10193 update_stmt (gsi_stmt (*gsi));
10194 fold_stmt (gsi, follow_single_use_edges);
10195 return true;
10199 switch (rhs_code)
10201 case EQ_EXPR:
10202 case NE_EXPR:
10203 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10204 if the RHS is zero or one, and the LHS are known to be boolean
10205 values. */
10206 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10207 return simplify_truth_ops_using_ranges (gsi, stmt);
10208 break;
10210 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10211 and BIT_AND_EXPR respectively if the first operand is greater
10212 than zero and the second operand is an exact power of two.
10213 Also optimize TRUNC_MOD_EXPR away if the second operand is
10214 constant and the first operand already has the right value
10215 range. */
10216 case TRUNC_DIV_EXPR:
10217 case TRUNC_MOD_EXPR:
10218 if (TREE_CODE (rhs1) == SSA_NAME
10219 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10220 return simplify_div_or_mod_using_ranges (gsi, stmt);
10221 break;
10223 /* Transform ABS (X) into X or -X as appropriate. */
10224 case ABS_EXPR:
10225 if (TREE_CODE (rhs1) == SSA_NAME
10226 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10227 return simplify_abs_using_ranges (gsi, stmt);
10228 break;
10230 case BIT_AND_EXPR:
10231 case BIT_IOR_EXPR:
10232 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10233 if all the bits being cleared are already cleared or
10234 all the bits being set are already set. */
10235 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10236 return simplify_bit_ops_using_ranges (gsi, stmt);
10237 break;
10239 CASE_CONVERT:
10240 if (TREE_CODE (rhs1) == SSA_NAME
10241 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10242 return simplify_conversion_using_ranges (gsi, stmt);
10243 break;
10245 case FLOAT_EXPR:
10246 if (TREE_CODE (rhs1) == SSA_NAME
10247 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10248 return simplify_float_conversion_using_ranges (gsi, stmt);
10249 break;
10251 case MIN_EXPR:
10252 case MAX_EXPR:
10253 return simplify_min_or_max_using_ranges (gsi, stmt);
10255 default:
10256 break;
10259 else if (gimple_code (stmt) == GIMPLE_COND)
10260 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
10261 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10262 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10263 else if (is_gimple_call (stmt)
10264 && gimple_call_internal_p (stmt))
10265 return simplify_internal_call_using_ranges (gsi, stmt);
10267 return false;
10270 /* If the statement pointed by SI has a predicate whose value can be
10271 computed using the value range information computed by VRP, compute
10272 its value and return true. Otherwise, return false. */
10274 static bool
10275 fold_predicate_in (gimple_stmt_iterator *si)
10277 bool assignment_p = false;
10278 tree val;
10279 gimple *stmt = gsi_stmt (*si);
10281 if (is_gimple_assign (stmt)
10282 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10284 assignment_p = true;
10285 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10286 gimple_assign_rhs1 (stmt),
10287 gimple_assign_rhs2 (stmt),
10288 stmt);
10290 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10291 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10292 gimple_cond_lhs (cond_stmt),
10293 gimple_cond_rhs (cond_stmt),
10294 stmt);
10295 else
10296 return false;
10298 if (val)
10300 if (assignment_p)
10301 val = fold_convert (gimple_expr_type (stmt), val);
10303 if (dump_file)
10305 fprintf (dump_file, "Folding predicate ");
10306 print_gimple_expr (dump_file, stmt, 0, 0);
10307 fprintf (dump_file, " to ");
10308 print_generic_expr (dump_file, val, 0);
10309 fprintf (dump_file, "\n");
10312 if (is_gimple_assign (stmt))
10313 gimple_assign_set_rhs_from_tree (si, val);
10314 else
10316 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10317 gcond *cond_stmt = as_a <gcond *> (stmt);
10318 if (integer_zerop (val))
10319 gimple_cond_make_false (cond_stmt);
10320 else if (integer_onep (val))
10321 gimple_cond_make_true (cond_stmt);
10322 else
10323 gcc_unreachable ();
10326 return true;
10329 return false;
10332 /* Callback for substitute_and_fold folding the stmt at *SI. */
10334 static bool
10335 vrp_fold_stmt (gimple_stmt_iterator *si)
10337 if (fold_predicate_in (si))
10338 return true;
10340 return simplify_stmt_using_ranges (si);
10343 /* Unwindable const/copy equivalences. */
10344 const_and_copies *equiv_stack;
10346 /* A trivial wrapper so that we can present the generic jump threading
10347 code with a simple API for simplifying statements. STMT is the
10348 statement we want to simplify, WITHIN_STMT provides the location
10349 for any overflow warnings. */
10351 static tree
10352 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10353 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10355 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10356 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10357 gimple_cond_lhs (cond_stmt),
10358 gimple_cond_rhs (cond_stmt),
10359 within_stmt);
10361 /* We simplify a switch statement by trying to determine which case label
10362 will be taken. If we are successful then we return the corresponding
10363 CASE_LABEL_EXPR. */
10364 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
10366 tree op = gimple_switch_index (switch_stmt);
10367 if (TREE_CODE (op) != SSA_NAME)
10368 return NULL_TREE;
10370 value_range *vr = get_value_range (op);
10371 if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE)
10372 || symbolic_range_p (vr))
10373 return NULL_TREE;
10375 if (vr->type == VR_RANGE)
10377 size_t i, j;
10378 /* Get the range of labels that contain a part of the operand's
10379 value range. */
10380 find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j);
10382 /* Is there only one such label? */
10383 if (i == j)
10385 tree label = gimple_switch_label (switch_stmt, i);
10387 /* The i'th label will be taken only if the value range of the
10388 operand is entirely within the bounds of this label. */
10389 if (CASE_HIGH (label) != NULL_TREE
10390 ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0
10391 && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0)
10392 : (tree_int_cst_equal (CASE_LOW (label), vr->min)
10393 && tree_int_cst_equal (vr->min, vr->max)))
10394 return label;
10397 /* If there are no such labels then the default label will be
10398 taken. */
10399 if (i > j)
10400 return gimple_switch_label (switch_stmt, 0);
10403 if (vr->type == VR_ANTI_RANGE)
10405 unsigned n = gimple_switch_num_labels (switch_stmt);
10406 tree min_label = gimple_switch_label (switch_stmt, 1);
10407 tree max_label = gimple_switch_label (switch_stmt, n - 1);
10409 /* The default label will be taken only if the anti-range of the
10410 operand is entirely outside the bounds of all the (non-default)
10411 case labels. */
10412 if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0
10413 && (CASE_HIGH (max_label) != NULL_TREE
10414 ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0
10415 : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0))
10416 return gimple_switch_label (switch_stmt, 0);
10419 return NULL_TREE;
10422 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10424 value_range new_vr = VR_INITIALIZER;
10425 tree lhs = gimple_assign_lhs (assign_stmt);
10427 if (TREE_CODE (lhs) == SSA_NAME
10428 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10429 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10431 extract_range_from_assignment (&new_vr, assign_stmt);
10432 if (range_int_cst_singleton_p (&new_vr))
10433 return new_vr.min;
10437 return NULL_TREE;
10440 /* Blocks which have more than one predecessor and more than
10441 one successor present jump threading opportunities, i.e.,
10442 when the block is reached from a specific predecessor, we
10443 may be able to determine which of the outgoing edges will
10444 be traversed. When this optimization applies, we are able
10445 to avoid conditionals at runtime and we may expose secondary
10446 optimization opportunities.
10448 This routine is effectively a driver for the generic jump
10449 threading code. It basically just presents the generic code
10450 with edges that may be suitable for jump threading.
10452 Unlike DOM, we do not iterate VRP if jump threading was successful.
10453 While iterating may expose new opportunities for VRP, it is expected
10454 those opportunities would be very limited and the compile time cost
10455 to expose those opportunities would be significant.
10457 As jump threading opportunities are discovered, they are registered
10458 for later realization. */
10460 static void
10461 identify_jump_threads (void)
10463 basic_block bb;
10464 gcond *dummy;
10465 int i;
10466 edge e;
10468 /* Ugh. When substituting values earlier in this pass we can
10469 wipe the dominance information. So rebuild the dominator
10470 information as we need it within the jump threading code. */
10471 calculate_dominance_info (CDI_DOMINATORS);
10473 /* We do not allow VRP information to be used for jump threading
10474 across a back edge in the CFG. Otherwise it becomes too
10475 difficult to avoid eliminating loop exit tests. Of course
10476 EDGE_DFS_BACK is not accurate at this time so we have to
10477 recompute it. */
10478 mark_dfs_back_edges ();
10480 /* Do not thread across edges we are about to remove. Just marking
10481 them as EDGE_IGNORE will do. */
10482 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10483 e->flags |= EDGE_IGNORE;
10485 /* Allocate our unwinder stack to unwind any temporary equivalences
10486 that might be recorded. */
10487 equiv_stack = new const_and_copies ();
10489 /* To avoid lots of silly node creation, we create a single
10490 conditional and just modify it in-place when attempting to
10491 thread jumps. */
10492 dummy = gimple_build_cond (EQ_EXPR,
10493 integer_zero_node, integer_zero_node,
10494 NULL, NULL);
10496 /* Walk through all the blocks finding those which present a
10497 potential jump threading opportunity. We could set this up
10498 as a dominator walker and record data during the walk, but
10499 I doubt it's worth the effort for the classes of jump
10500 threading opportunities we are trying to identify at this
10501 point in compilation. */
10502 FOR_EACH_BB_FN (bb, cfun)
10504 gimple *last;
10506 /* If the generic jump threading code does not find this block
10507 interesting, then there is nothing to do. */
10508 if (! potentially_threadable_block (bb))
10509 continue;
10511 last = last_stmt (bb);
10513 /* We're basically looking for a switch or any kind of conditional with
10514 integral or pointer type arguments. Note the type of the second
10515 argument will be the same as the first argument, so no need to
10516 check it explicitly.
10518 We also handle the case where there are no statements in the
10519 block. This come up with forwarder blocks that are not
10520 optimized away because they lead to a loop header. But we do
10521 want to thread through them as we can sometimes thread to the
10522 loop exit which is obviously profitable. */
10523 if (!last
10524 || gimple_code (last) == GIMPLE_SWITCH
10525 || (gimple_code (last) == GIMPLE_COND
10526 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10527 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10528 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10529 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10530 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10532 edge_iterator ei;
10534 /* We've got a block with multiple predecessors and multiple
10535 successors which also ends in a suitable conditional or
10536 switch statement. For each predecessor, see if we can thread
10537 it to a specific successor. */
10538 FOR_EACH_EDGE (e, ei, bb->preds)
10540 /* Do not thread across edges marked to ignoreor abnormal
10541 edges in the CFG. */
10542 if (e->flags & (EDGE_IGNORE | EDGE_COMPLEX))
10543 continue;
10545 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10546 simplify_stmt_for_jump_threading);
10551 /* Clear EDGE_IGNORE. */
10552 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10553 e->flags &= ~EDGE_IGNORE;
10555 /* We do not actually update the CFG or SSA graphs at this point as
10556 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10557 handle ASSERT_EXPRs gracefully. */
10560 /* We identified all the jump threading opportunities earlier, but could
10561 not transform the CFG at that time. This routine transforms the
10562 CFG and arranges for the dominator tree to be rebuilt if necessary.
10564 Note the SSA graph update will occur during the normal TODO
10565 processing by the pass manager. */
10566 static void
10567 finalize_jump_threads (void)
10569 thread_through_all_blocks (false);
10570 delete equiv_stack;
10573 /* Free VRP lattice. */
10575 static void
10576 vrp_free_lattice ()
10578 /* Free allocated memory. */
10579 free (vr_value);
10580 free (vr_phi_edge_counts);
10581 bitmap_obstack_release (&vrp_equiv_obstack);
10582 vrp_value_range_pool.release ();
10584 /* So that we can distinguish between VRP data being available
10585 and not available. */
10586 vr_value = NULL;
10587 vr_phi_edge_counts = NULL;
10590 /* Traverse all the blocks folding conditionals with known ranges. */
10592 static void
10593 vrp_finalize (bool warn_array_bounds_p)
10595 size_t i;
10597 values_propagated = true;
10599 if (dump_file)
10601 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10602 dump_all_value_ranges (dump_file);
10603 fprintf (dump_file, "\n");
10606 /* Set value range to non pointer SSA_NAMEs. */
10607 for (i = 0; i < num_vr_values; i++)
10608 if (vr_value[i])
10610 tree name = ssa_name (i);
10612 if (!name
10613 || POINTER_TYPE_P (TREE_TYPE (name))
10614 || (vr_value[i]->type == VR_VARYING)
10615 || (vr_value[i]->type == VR_UNDEFINED))
10616 continue;
10618 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10619 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10620 && (vr_value[i]->type == VR_RANGE
10621 || vr_value[i]->type == VR_ANTI_RANGE))
10622 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10623 vr_value[i]->max);
10626 substitute_and_fold (op_with_constant_singleton_value_range,
10627 vrp_fold_stmt, true);
10629 if (warn_array_bounds && warn_array_bounds_p)
10630 check_all_array_refs ();
10632 /* We must identify jump threading opportunities before we release
10633 the datastructures built by VRP. */
10634 identify_jump_threads ();
10637 /* evrp_dom_walker visits the basic blocks in the dominance order and set
10638 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
10639 discover more VRs. */
10641 class evrp_dom_walker : public dom_walker
10643 public:
10644 evrp_dom_walker ()
10645 : dom_walker (CDI_DOMINATORS), stack (10)
10647 stmts_to_fixup.create (0);
10648 need_eh_cleanup = BITMAP_ALLOC (NULL);
10650 ~evrp_dom_walker ()
10652 stmts_to_fixup.release ();
10653 BITMAP_FREE (need_eh_cleanup);
10655 virtual edge before_dom_children (basic_block);
10656 virtual void after_dom_children (basic_block);
10657 void push_value_range (const_tree var, value_range *vr);
10658 value_range *pop_value_range (const_tree var);
10659 value_range *try_find_new_range (tree op, tree_code code, tree limit);
10661 /* Cond_stack holds the old VR. */
10662 auto_vec<std::pair <const_tree, value_range*> > stack;
10663 bitmap need_eh_cleanup;
10664 vec<gimple *> stmts_to_fixup;
10668 /* Find new range for OP such that (OP CODE LIMIT) is true. */
10670 value_range *
10671 evrp_dom_walker::try_find_new_range (tree op, tree_code code, tree limit)
10673 value_range vr = VR_INITIALIZER;
10674 value_range *old_vr = get_value_range (op);
10676 /* Discover VR when condition is true. */
10677 extract_range_for_var_from_comparison_expr (op, code, op,
10678 limit, &vr);
10679 if (old_vr->type == VR_RANGE || old_vr->type == VR_ANTI_RANGE)
10680 vrp_intersect_ranges (&vr, old_vr);
10681 /* If we found any usable VR, set the VR to ssa_name and create a
10682 PUSH old value in the stack with the old VR. */
10683 if (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE)
10685 value_range *new_vr = vrp_value_range_pool.allocate ();
10686 *new_vr = vr;
10687 return new_vr;
10689 return NULL;
10692 /* See if there is any new scope is entered with new VR and set that VR to
10693 ssa_name before visiting the statements in the scope. */
10695 edge
10696 evrp_dom_walker::before_dom_children (basic_block bb)
10698 tree op0 = NULL_TREE;
10700 push_value_range (NULL_TREE, NULL);
10701 if (single_pred_p (bb))
10703 edge e = single_pred_edge (bb);
10704 gimple *stmt = last_stmt (e->src);
10705 if (stmt
10706 && gimple_code (stmt) == GIMPLE_COND
10707 && (op0 = gimple_cond_lhs (stmt))
10708 && TREE_CODE (op0) == SSA_NAME
10709 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))
10710 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))))
10712 /* Entering a new scope. Try to see if we can find a VR
10713 here. */
10714 tree op1 = gimple_cond_rhs (stmt);
10715 tree_code code = gimple_cond_code (stmt);
10717 if (TREE_OVERFLOW_P (op1))
10718 op1 = drop_tree_overflow (op1);
10720 /* If condition is false, invert the cond. */
10721 if (e->flags & EDGE_FALSE_VALUE)
10722 code = invert_tree_comparison (gimple_cond_code (stmt),
10723 HONOR_NANS (op0));
10724 /* Add VR when (OP0 CODE OP1) condition is true. */
10725 value_range *op0_range = try_find_new_range (op0, code, op1);
10727 /* Register ranges for y in x < y where
10728 y might have ranges that are useful. */
10729 tree limit;
10730 tree_code new_code;
10731 if (TREE_CODE (op1) == SSA_NAME
10732 && extract_code_and_val_from_cond_with_ops (op1, code,
10733 op0, op1,
10734 false,
10735 &new_code, &limit))
10737 /* Add VR when (OP1 NEW_CODE LIMIT) condition is true. */
10738 value_range *op1_range = try_find_new_range (op1, new_code, limit);
10739 if (op1_range)
10740 push_value_range (op1, op1_range);
10743 if (op0_range)
10744 push_value_range (op0, op0_range);
10748 /* Visit PHI stmts and discover any new VRs possible. */
10749 gimple_stmt_iterator gsi;
10750 edge e;
10751 edge_iterator ei;
10752 bool has_unvisived_preds = false;
10754 FOR_EACH_EDGE (e, ei, bb->preds)
10755 if (!(e->src->flags & BB_VISITED))
10757 has_unvisived_preds = true;
10758 break;
10761 for (gphi_iterator gpi = gsi_start_phis (bb);
10762 !gsi_end_p (gpi); gsi_next (&gpi))
10764 gphi *phi = gpi.phi ();
10765 tree lhs = PHI_RESULT (phi);
10766 value_range vr_result = VR_INITIALIZER;
10767 if (!has_unvisived_preds
10768 && stmt_interesting_for_vrp (phi))
10769 extract_range_from_phi_node (phi, &vr_result);
10770 else
10771 set_value_range_to_varying (&vr_result);
10772 update_value_range (lhs, &vr_result);
10775 /* Visit all other stmts and discover any new VRs possible. */
10776 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
10778 gimple *stmt = gsi_stmt (gsi);
10779 edge taken_edge;
10780 tree output = NULL_TREE;
10781 gimple *old_stmt = stmt;
10782 bool was_noreturn = (is_gimple_call (stmt)
10783 && gimple_call_noreturn_p (stmt));
10785 /* TODO, if found taken_edge, we should visit (return it) and travel
10786 again to improve VR as done in DOM/SCCVN optimizations. It should
10787 be done carefully as stmts might prematurely leave a BB like
10788 in EH. */
10789 if (stmt_interesting_for_vrp (stmt))
10791 value_range vr = VR_INITIALIZER;
10792 extract_range_from_stmt (stmt, &taken_edge, &output, &vr);
10793 if (output
10794 && (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE))
10795 update_value_range (output, &vr);
10796 else
10797 set_defs_to_varying (stmt);
10799 /* Try folding stmts with the VR discovered. */
10800 bool did_replace
10801 = replace_uses_in (stmt,
10802 op_with_constant_singleton_value_range);
10803 if (fold_stmt (&gsi, follow_single_use_edges)
10804 || did_replace)
10805 update_stmt (gsi_stmt (gsi));
10807 if (did_replace)
10809 /* If we cleaned up EH information from the statement,
10810 remove EH edges. */
10811 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
10812 bitmap_set_bit (need_eh_cleanup, bb->index);
10814 /* If we turned a not noreturn call into a noreturn one
10815 schedule it for fixup. */
10816 if (!was_noreturn
10817 && is_gimple_call (stmt)
10818 && gimple_call_noreturn_p (stmt))
10819 stmts_to_fixup.safe_push (stmt);
10821 if (gimple_assign_single_p (stmt))
10823 tree rhs = gimple_assign_rhs1 (stmt);
10824 if (TREE_CODE (rhs) == ADDR_EXPR)
10825 recompute_tree_invariant_for_addr_expr (rhs);
10829 def_operand_p def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
10830 /* Set the SSA with the value range. */
10831 if (def_p
10832 && TREE_CODE (DEF_FROM_PTR (def_p)) == SSA_NAME
10833 && INTEGRAL_TYPE_P (TREE_TYPE (DEF_FROM_PTR (def_p))))
10835 tree def = DEF_FROM_PTR (def_p);
10836 value_range *vr = get_value_range (def);
10838 if ((vr->type == VR_RANGE
10839 || vr->type == VR_ANTI_RANGE)
10840 && (TREE_CODE (vr->min) == INTEGER_CST)
10841 && (TREE_CODE (vr->max) == INTEGER_CST))
10842 set_range_info (def, vr->type, vr->min, vr->max);
10845 else
10846 set_defs_to_varying (stmt);
10848 bb->flags |= BB_VISITED;
10849 return NULL;
10852 /* Restore/pop VRs valid only for BB when we leave BB. */
10854 void
10855 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
10857 gcc_checking_assert (!stack.is_empty ());
10858 while (stack.last ().first != NULL_TREE)
10859 pop_value_range (stack.last ().first);
10860 pop_value_range (stack.last ().first);
10863 /* Push the Value Range of VAR to the stack and update it with new VR. */
10865 void
10866 evrp_dom_walker::push_value_range (const_tree var, value_range *vr)
10868 if (vr != NULL)
10870 unsigned ver = SSA_NAME_VERSION (var);
10871 gcc_checking_assert (vr_value);
10872 stack.safe_push (std::make_pair (var, vr_value[ver]));
10874 if (ver < num_vr_values)
10875 vr_value[ver] = vr;
10877 else
10878 stack.safe_push (std::make_pair (var, vr));
10881 /* Pop the Value Range from the vrp_stack and update VAR with it. */
10883 value_range *
10884 evrp_dom_walker::pop_value_range (const_tree var)
10886 value_range *vr = stack.last ().second;
10887 if (vr != NULL)
10889 unsigned ver = SSA_NAME_VERSION (var);
10890 gcc_checking_assert (var == stack.last ().first);
10891 gcc_checking_assert (vr_value);
10893 if (ver < num_vr_values)
10894 vr_value[ver] = vr;
10896 stack.pop ();
10897 return vr;
10901 /* Main entry point for the early vrp pass which is a simplified non-iterative
10902 version of vrp where basic blocks are visited in dominance order. Value
10903 ranges discovered in early vrp will also be used by ipa-vrp. */
10905 static unsigned int
10906 execute_early_vrp ()
10908 edge e;
10909 edge_iterator ei;
10910 basic_block bb;
10912 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10913 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10914 scev_initialize ();
10915 calculate_dominance_info (CDI_DOMINATORS);
10916 FOR_EACH_BB_FN (bb, cfun)
10918 bb->flags &= ~BB_VISITED;
10919 FOR_EACH_EDGE (e, ei, bb->preds)
10920 e->flags |= EDGE_EXECUTABLE;
10922 vrp_initialize_lattice ();
10924 /* Walk stmts in dominance order and propagate VRP. */
10925 evrp_dom_walker walker;
10926 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
10928 if (!bitmap_empty_p (walker.need_eh_cleanup))
10929 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
10931 /* Fixup stmts that became noreturn calls. This may require splitting
10932 blocks and thus isn't possible during the dominator walk. Do this
10933 in reverse order so we don't inadvertedly remove a stmt we want to
10934 fixup by visiting a dominating now noreturn call first. */
10935 while (!walker.stmts_to_fixup.is_empty ())
10937 gimple *stmt = walker.stmts_to_fixup.pop ();
10938 fixup_noreturn_call (stmt);
10941 if (dump_file)
10943 fprintf (dump_file, "\nValue ranges after Early VRP:\n\n");
10944 dump_all_value_ranges (dump_file);
10945 fprintf (dump_file, "\n");
10947 vrp_free_lattice ();
10948 scev_finalize ();
10949 loop_optimizer_finalize ();
10950 FOR_EACH_BB_FN (bb, cfun)
10951 bb->flags &= ~BB_VISITED;
10952 return 0;
10956 /* Main entry point to VRP (Value Range Propagation). This pass is
10957 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10958 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10959 Programming Language Design and Implementation, pp. 67-78, 1995.
10960 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10962 This is essentially an SSA-CCP pass modified to deal with ranges
10963 instead of constants.
10965 While propagating ranges, we may find that two or more SSA name
10966 have equivalent, though distinct ranges. For instance,
10968 1 x_9 = p_3->a;
10969 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10970 3 if (p_4 == q_2)
10971 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10972 5 endif
10973 6 if (q_2)
10975 In the code above, pointer p_5 has range [q_2, q_2], but from the
10976 code we can also determine that p_5 cannot be NULL and, if q_2 had
10977 a non-varying range, p_5's range should also be compatible with it.
10979 These equivalences are created by two expressions: ASSERT_EXPR and
10980 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10981 result of another assertion, then we can use the fact that p_5 and
10982 p_4 are equivalent when evaluating p_5's range.
10984 Together with value ranges, we also propagate these equivalences
10985 between names so that we can take advantage of information from
10986 multiple ranges when doing final replacement. Note that this
10987 equivalency relation is transitive but not symmetric.
10989 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10990 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10991 in contexts where that assertion does not hold (e.g., in line 6).
10993 TODO, the main difference between this pass and Patterson's is that
10994 we do not propagate edge probabilities. We only compute whether
10995 edges can be taken or not. That is, instead of having a spectrum
10996 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10997 DON'T KNOW. In the future, it may be worthwhile to propagate
10998 probabilities to aid branch prediction. */
11000 static unsigned int
11001 execute_vrp (bool warn_array_bounds_p)
11003 int i;
11004 edge e;
11005 switch_update *su;
11007 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
11008 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
11009 scev_initialize ();
11011 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
11012 Inserting assertions may split edges which will invalidate
11013 EDGE_DFS_BACK. */
11014 insert_range_assertions ();
11016 to_remove_edges.create (10);
11017 to_update_switch_stmts.create (5);
11018 threadedge_initialize_values ();
11020 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
11021 mark_dfs_back_edges ();
11023 vrp_initialize_lattice ();
11024 vrp_initialize ();
11025 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
11026 vrp_finalize (warn_array_bounds_p);
11027 vrp_free_lattice ();
11029 free_numbers_of_iterations_estimates (cfun);
11031 /* ASSERT_EXPRs must be removed before finalizing jump threads
11032 as finalizing jump threads calls the CFG cleanup code which
11033 does not properly handle ASSERT_EXPRs. */
11034 remove_range_assertions ();
11036 /* If we exposed any new variables, go ahead and put them into
11037 SSA form now, before we handle jump threading. This simplifies
11038 interactions between rewriting of _DECL nodes into SSA form
11039 and rewriting SSA_NAME nodes into SSA form after block
11040 duplication and CFG manipulation. */
11041 update_ssa (TODO_update_ssa);
11043 finalize_jump_threads ();
11045 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
11046 CFG in a broken state and requires a cfg_cleanup run. */
11047 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
11048 remove_edge (e);
11049 /* Update SWITCH_EXPR case label vector. */
11050 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
11052 size_t j;
11053 size_t n = TREE_VEC_LENGTH (su->vec);
11054 tree label;
11055 gimple_switch_set_num_labels (su->stmt, n);
11056 for (j = 0; j < n; j++)
11057 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
11058 /* As we may have replaced the default label with a regular one
11059 make sure to make it a real default label again. This ensures
11060 optimal expansion. */
11061 label = gimple_switch_label (su->stmt, 0);
11062 CASE_LOW (label) = NULL_TREE;
11063 CASE_HIGH (label) = NULL_TREE;
11066 if (to_remove_edges.length () > 0)
11068 free_dominance_info (CDI_DOMINATORS);
11069 loops_state_set (LOOPS_NEED_FIXUP);
11072 to_remove_edges.release ();
11073 to_update_switch_stmts.release ();
11074 threadedge_finalize_values ();
11076 scev_finalize ();
11077 loop_optimizer_finalize ();
11078 return 0;
11081 namespace {
11083 const pass_data pass_data_vrp =
11085 GIMPLE_PASS, /* type */
11086 "vrp", /* name */
11087 OPTGROUP_NONE, /* optinfo_flags */
11088 TV_TREE_VRP, /* tv_id */
11089 PROP_ssa, /* properties_required */
11090 0, /* properties_provided */
11091 0, /* properties_destroyed */
11092 0, /* todo_flags_start */
11093 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
11096 class pass_vrp : public gimple_opt_pass
11098 public:
11099 pass_vrp (gcc::context *ctxt)
11100 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
11103 /* opt_pass methods: */
11104 opt_pass * clone () { return new pass_vrp (m_ctxt); }
11105 void set_pass_param (unsigned int n, bool param)
11107 gcc_assert (n == 0);
11108 warn_array_bounds_p = param;
11110 virtual bool gate (function *) { return flag_tree_vrp != 0; }
11111 virtual unsigned int execute (function *)
11112 { return execute_vrp (warn_array_bounds_p); }
11114 private:
11115 bool warn_array_bounds_p;
11116 }; // class pass_vrp
11118 } // anon namespace
11120 gimple_opt_pass *
11121 make_pass_vrp (gcc::context *ctxt)
11123 return new pass_vrp (ctxt);
11126 namespace {
11128 const pass_data pass_data_early_vrp =
11130 GIMPLE_PASS, /* type */
11131 "evrp", /* name */
11132 OPTGROUP_NONE, /* optinfo_flags */
11133 TV_TREE_EARLY_VRP, /* tv_id */
11134 PROP_ssa, /* properties_required */
11135 0, /* properties_provided */
11136 0, /* properties_destroyed */
11137 0, /* todo_flags_start */
11138 ( TODO_cleanup_cfg | TODO_update_ssa | TODO_verify_all ),
11141 class pass_early_vrp : public gimple_opt_pass
11143 public:
11144 pass_early_vrp (gcc::context *ctxt)
11145 : gimple_opt_pass (pass_data_early_vrp, ctxt)
11148 /* opt_pass methods: */
11149 opt_pass * clone () { return new pass_early_vrp (m_ctxt); }
11150 virtual bool gate (function *)
11152 return flag_tree_vrp != 0;
11154 virtual unsigned int execute (function *)
11155 { return execute_early_vrp (); }
11157 }; // class pass_vrp
11158 } // anon namespace
11160 gimple_opt_pass *
11161 make_pass_early_vrp (gcc::context *ctxt)
11163 return new pass_early_vrp (ctxt);